2021-09-21 19:54:13 +10:00
|
|
|
//! # Pico USB Serial Example
|
2021-09-09 17:55:30 +10:00
|
|
|
//!
|
2021-09-21 19:54:13 +10:00
|
|
|
//! Creates a USB Serial device on a Pico board, with the USB driver running in
|
|
|
|
//! the main thread.
|
|
|
|
//!
|
|
|
|
//! This will create a USB Serial device echoing anything it receives. Incoming
|
|
|
|
//! ASCII characters are converted to upercase, so you can tell it is working
|
|
|
|
//! and not just local-echo!
|
|
|
|
//!
|
|
|
|
//! See the `Cargo.toml` file for Copyright and licence details.
|
|
|
|
|
2021-09-09 17:55:30 +10:00
|
|
|
#![no_std]
|
|
|
|
#![no_main]
|
|
|
|
|
2021-09-21 19:54:13 +10:00
|
|
|
// The macro for our start-up function
|
2021-09-09 17:55:30 +10:00
|
|
|
use cortex_m_rt::entry;
|
2021-09-21 19:54:13 +10:00
|
|
|
|
|
|
|
// Ensure we halt the program on panic (if we don't mention this crate it won't
|
|
|
|
// be linked)
|
2021-09-09 17:55:30 +10:00
|
|
|
use panic_halt as _;
|
2021-09-21 19:54:13 +10:00
|
|
|
|
|
|
|
// A shorter alias for the Peripheral Access Crate, which provides low-level
|
|
|
|
// register access
|
|
|
|
use pico::hal::pac;
|
|
|
|
|
|
|
|
// A shorter alias for the Hardware Abstraction Layer, which provides
|
|
|
|
// higher-level drivers.
|
|
|
|
use pico::hal;
|
|
|
|
|
|
|
|
// USB Device support
|
2021-09-09 17:55:30 +10:00
|
|
|
use usb_device::{class_prelude::*, prelude::*};
|
2021-09-21 19:54:13 +10:00
|
|
|
|
|
|
|
// USB Communications Class Device support
|
2021-09-09 17:55:30 +10:00
|
|
|
use usbd_serial::SerialPort;
|
|
|
|
|
2021-09-21 19:54:13 +10:00
|
|
|
/// Entry point to our bare-metal application.
|
|
|
|
///
|
|
|
|
/// The `#[entry]` macro ensures the Cortex-M start-up code calls this function
|
|
|
|
/// as soon as all global variables are initialised.
|
|
|
|
///
|
|
|
|
/// The function configures the RP2040 peripherals, then echoes any characters
|
|
|
|
/// received over USB Serial.
|
2021-09-09 17:55:30 +10:00
|
|
|
#[entry]
|
|
|
|
fn main() -> ! {
|
2021-09-21 19:54:13 +10:00
|
|
|
// Grab our singleton objects
|
2021-09-09 17:55:30 +10:00
|
|
|
let mut pac = pac::Peripherals::take().unwrap();
|
|
|
|
|
2021-09-21 19:54:13 +10:00
|
|
|
// Set up the watchdog driver - needed by the clock setup code
|
|
|
|
let mut watchdog = hal::watchdog::Watchdog::new(pac.WATCHDOG);
|
|
|
|
|
|
|
|
// Configure the clocks
|
|
|
|
//
|
2021-09-28 03:37:59 +10:00
|
|
|
// The default is to generate a 125 MHz system clock
|
2021-09-21 19:54:13 +10:00
|
|
|
let clocks = hal::clocks::init_clocks_and_plls(
|
|
|
|
pico::XOSC_CRYSTAL_FREQ,
|
2021-09-09 17:55:30 +10:00
|
|
|
pac.XOSC,
|
|
|
|
pac.CLOCKS,
|
|
|
|
pac.PLL_SYS,
|
|
|
|
pac.PLL_USB,
|
|
|
|
&mut pac.RESETS,
|
|
|
|
&mut watchdog,
|
|
|
|
)
|
|
|
|
.ok()
|
|
|
|
.unwrap();
|
|
|
|
|
2021-09-21 19:54:13 +10:00
|
|
|
// Set up the USB driver
|
|
|
|
let usb_bus = UsbBusAllocator::new(hal::usb::UsbBus::new(
|
2021-09-09 17:55:30 +10:00
|
|
|
pac.USBCTRL_REGS,
|
|
|
|
pac.USBCTRL_DPRAM,
|
|
|
|
clocks.usb_clock,
|
|
|
|
true,
|
|
|
|
&mut pac.RESETS,
|
|
|
|
));
|
2021-09-21 19:54:13 +10:00
|
|
|
|
|
|
|
// Set up the USB Communications Class Device driver
|
2021-09-09 17:55:30 +10:00
|
|
|
let mut serial = SerialPort::new(&usb_bus);
|
2021-09-21 19:54:13 +10:00
|
|
|
|
|
|
|
// Create a USB device with a fake VID and PID
|
2021-09-09 17:55:30 +10:00
|
|
|
let mut usb_dev = UsbDeviceBuilder::new(&usb_bus, UsbVidPid(0x16c0, 0x27dd))
|
|
|
|
.manufacturer("Fake company")
|
|
|
|
.product("Serial port")
|
|
|
|
.serial_number("TEST")
|
|
|
|
.device_class(2) // from: https://www.usb.org/defined-class-codes
|
|
|
|
.build();
|
|
|
|
|
2021-10-02 15:41:04 +10:00
|
|
|
let timer = hal::timer::Timer::new(pac.TIMER, &mut pac.RESETS);
|
2021-09-09 17:55:30 +10:00
|
|
|
let mut said_hello = false;
|
|
|
|
loop {
|
2021-09-21 19:54:13 +10:00
|
|
|
// A welcome message at the beginning
|
2021-09-09 17:55:30 +10:00
|
|
|
if !said_hello && timer.get_counter() >= 2_000_000 {
|
|
|
|
said_hello = true;
|
2021-09-21 19:54:13 +10:00
|
|
|
let _ = serial.write(b"Hello, World!\r\n");
|
2021-09-09 17:55:30 +10:00
|
|
|
}
|
|
|
|
|
2021-09-21 19:54:13 +10:00
|
|
|
// Check for new data
|
|
|
|
if usb_dev.poll(&mut [&mut serial]) {
|
|
|
|
let mut buf = [0u8; 64];
|
|
|
|
match serial.read(&mut buf) {
|
|
|
|
Err(_e) => {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
Ok(0) => {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
Ok(count) => {
|
|
|
|
// Convert to upper case
|
|
|
|
buf.iter_mut().take(count).for_each(|b| {
|
|
|
|
b.make_ascii_uppercase();
|
|
|
|
});
|
|
|
|
// Send back to the host
|
|
|
|
let mut wr_ptr = &buf[..count];
|
|
|
|
while !wr_ptr.is_empty() {
|
2021-11-27 07:43:02 +11:00
|
|
|
match serial.write(wr_ptr) {
|
|
|
|
Ok(len) => wr_ptr = &wr_ptr[len..],
|
|
|
|
// On error, just drop unwritten data.
|
|
|
|
// One possible error is Err(WouldBlock), meaning the USB
|
|
|
|
// write buffer is full.
|
|
|
|
Err(_) => break,
|
|
|
|
};
|
2021-09-21 19:54:13 +10:00
|
|
|
}
|
2021-09-09 17:55:30 +10:00
|
|
|
}
|
|
|
|
}
|
2021-09-21 19:54:13 +10:00
|
|
|
}
|
2021-09-09 17:55:30 +10:00
|
|
|
}
|
|
|
|
}
|
2021-09-21 19:54:13 +10:00
|
|
|
|
|
|
|
// End of file
|