2021-09-21 19:47:43 +10:00
|
|
|
//! # Pico USB Serial (with Interrupts) Example
|
2021-09-09 23:03:12 +10:00
|
|
|
//!
|
2021-09-21 19:47:43 +10:00
|
|
|
//! Creates a USB Serial device on a Pico board, with the USB driver running in
|
|
|
|
//! the USB interrupt.
|
|
|
|
//!
|
|
|
|
//! 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 23:03:12 +10:00
|
|
|
#![no_std]
|
|
|
|
#![no_main]
|
|
|
|
|
2021-09-21 19:47:43 +10:00
|
|
|
// The macro for our start-up function
|
2021-09-09 23:03:12 +10:00
|
|
|
use cortex_m_rt::entry;
|
2021-09-21 19:47:43 +10:00
|
|
|
|
|
|
|
// The macro for marking our interrupt functions
|
|
|
|
use pico::hal::pac::interrupt;
|
|
|
|
|
|
|
|
// GPIO traits
|
|
|
|
use embedded_hal::digital::v2::OutputPin;
|
|
|
|
|
|
|
|
// Time handling traits
|
|
|
|
use embedded_time::rate::*;
|
|
|
|
|
|
|
|
// Ensure we halt the program on panic (if we don't mention this crate it won't
|
|
|
|
// be linked)
|
2021-09-09 23:03:12 +10:00
|
|
|
use panic_halt as _;
|
2021-09-21 19:47:43 +10:00
|
|
|
|
|
|
|
// Pull in any important traits
|
|
|
|
use pico::hal::prelude::*;
|
|
|
|
|
|
|
|
// 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 23:03:12 +10:00
|
|
|
use usb_device::{class_prelude::*, prelude::*};
|
2021-09-21 19:47:43 +10:00
|
|
|
|
|
|
|
// USB Communications Class Device support
|
2021-09-09 23:03:12 +10:00
|
|
|
use usbd_serial::SerialPort;
|
|
|
|
|
2021-09-28 02:30:50 +10:00
|
|
|
//// The linker will place this boot block at the start of our program image. We
|
|
|
|
//// need this to help the ROM bootloader get our code up and running.
|
2021-09-09 23:03:12 +10:00
|
|
|
#[link_section = ".boot2"]
|
|
|
|
#[used]
|
2021-10-18 20:52:01 +11:00
|
|
|
pub static BOOT2: [u8; 256] = rp2040_boot2::BOOT_LOADER_W25Q080;
|
2021-09-09 23:03:12 +10:00
|
|
|
|
2021-09-21 19:47:43 +10:00
|
|
|
/// The USB Device Driver (shared with the interrupt).
|
|
|
|
static mut USB_DEVICE: Option<UsbDevice<hal::usb::UsbBus>> = None;
|
2021-09-09 23:03:12 +10:00
|
|
|
|
2021-09-21 19:47:43 +10:00
|
|
|
/// The USB Bus Driver (shared with the interrupt).
|
|
|
|
static mut USB_BUS: Option<UsbBusAllocator<hal::usb::UsbBus>> = None;
|
2021-09-09 23:03:12 +10:00
|
|
|
|
2021-09-21 19:47:43 +10:00
|
|
|
/// The USB Serial Device Driver (shared with the interrupt).
|
|
|
|
static mut USB_SERIAL: Option<SerialPort<hal::usb::UsbBus>> = None;
|
|
|
|
|
|
|
|
/// 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 blinks the LED in an
|
|
|
|
/// infinite loop.
|
2021-09-09 23:03:12 +10:00
|
|
|
#[entry]
|
|
|
|
fn main() -> ! {
|
2021-09-21 19:47:43 +10:00
|
|
|
// Grab our singleton objects
|
2021-09-09 23:03:12 +10:00
|
|
|
let mut pac = pac::Peripherals::take().unwrap();
|
2021-09-21 19:47:43 +10:00
|
|
|
let core = pac::CorePeripherals::take().unwrap();
|
2021-09-09 23:03:12 +10:00
|
|
|
|
2021-09-21 19:47:43 +10:00
|
|
|
// Set up the watchdog driver - needed by the clock setup code
|
2021-12-04 17:30:47 +11:00
|
|
|
let mut watchdog = hal::Watchdog::new(pac.WATCHDOG);
|
2021-09-21 19:47:43 +10:00
|
|
|
|
|
|
|
// 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:47:43 +10:00
|
|
|
let clocks = hal::clocks::init_clocks_and_plls(
|
|
|
|
pico::XOSC_CRYSTAL_FREQ,
|
2021-09-09 23:03:12 +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
|
2021-09-21 19:47:43 +10:00
|
|
|
let usb_bus = UsbBusAllocator::new(hal::usb::UsbBus::new(
|
2021-09-09 23:03:12 +10:00
|
|
|
pac.USBCTRL_REGS,
|
|
|
|
pac.USBCTRL_DPRAM,
|
|
|
|
clocks.usb_clock,
|
|
|
|
true,
|
|
|
|
&mut pac.RESETS,
|
|
|
|
));
|
|
|
|
unsafe {
|
2021-09-21 19:47:43 +10:00
|
|
|
// Note (safety): This is safe as interrupts haven't been started yet
|
2021-09-09 23:03:12 +10:00
|
|
|
USB_BUS = Some(usb_bus);
|
|
|
|
}
|
|
|
|
|
2021-09-21 21:43:16 +10:00
|
|
|
// Grab a reference to the USB Bus allocator. We are promising to the
|
|
|
|
// compiler not to take mutable access to this global variable whilst this
|
|
|
|
// reference exists!
|
2021-09-21 21:37:44 +10:00
|
|
|
let bus_ref = unsafe { USB_BUS.as_ref().unwrap() };
|
|
|
|
|
2021-09-21 19:54:13 +10:00
|
|
|
// Set up the USB Communications Class Device driver
|
2021-09-21 21:37:44 +10:00
|
|
|
let serial = SerialPort::new(bus_ref);
|
2021-09-09 23:03:12 +10:00
|
|
|
unsafe {
|
|
|
|
USB_SERIAL = Some(serial);
|
|
|
|
}
|
|
|
|
|
2021-09-21 19:54:13 +10:00
|
|
|
// Create a USB device with a fake VID and PID
|
2021-09-21 21:37:44 +10:00
|
|
|
let usb_dev = UsbDeviceBuilder::new(bus_ref, 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-09-09 23:03:12 +10:00
|
|
|
unsafe {
|
2021-09-21 19:47:43 +10:00
|
|
|
// Note (safety): This is safe as interrupts haven't been started yet
|
2021-09-09 23:03:12 +10:00
|
|
|
USB_DEVICE = Some(usb_dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enable the USB interrupt
|
|
|
|
unsafe {
|
|
|
|
pac::NVIC::unmask(hal::pac::Interrupt::USBCTRL_IRQ);
|
|
|
|
};
|
|
|
|
|
2021-09-21 19:47:43 +10:00
|
|
|
// No more USB code after this point in main! We can do anything we want in
|
|
|
|
// here since USB is handled in the interrupt - let's blink an LED!
|
|
|
|
|
|
|
|
// The delay object lets us wait for specified amounts of time (in
|
|
|
|
// milliseconds)
|
2021-09-09 23:03:12 +10:00
|
|
|
let mut delay = cortex_m::delay::Delay::new(core.SYST, clocks.system_clock.freq().integer());
|
|
|
|
|
2021-09-21 19:47:43 +10:00
|
|
|
// The single-cycle I/O block controls our GPIO pins
|
2021-12-04 15:52:46 +11:00
|
|
|
let sio = hal::Sio::new(pac.SIO);
|
2021-09-21 19:47:43 +10:00
|
|
|
|
|
|
|
// Set the pins up according to their function on this particular board
|
|
|
|
let pins = pico::Pins::new(
|
2021-09-09 23:03:12 +10:00
|
|
|
pac.IO_BANK0,
|
|
|
|
pac.PADS_BANK0,
|
|
|
|
sio.gpio_bank0,
|
|
|
|
&mut pac.RESETS,
|
|
|
|
);
|
2021-09-21 19:47:43 +10:00
|
|
|
|
|
|
|
// Set the LED to be an output
|
2021-09-09 23:03:12 +10:00
|
|
|
let mut led_pin = pins.led.into_push_pull_output();
|
2021-09-21 19:47:43 +10:00
|
|
|
|
|
|
|
// Blink the LED at 1 Hz
|
2021-09-09 23:03:12 +10:00
|
|
|
loop {
|
|
|
|
led_pin.set_high().unwrap();
|
|
|
|
delay.delay_ms(500);
|
|
|
|
led_pin.set_low().unwrap();
|
|
|
|
delay.delay_ms(500);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-21 19:47:43 +10:00
|
|
|
/// This function is called whenever the USB Hardware generates an Interrupt
|
|
|
|
/// Request.
|
|
|
|
///
|
|
|
|
/// We do all our USB work under interrupt, so the main thread can continue on
|
|
|
|
/// knowing nothing about USB.
|
2021-09-09 23:03:12 +10:00
|
|
|
#[allow(non_snake_case)]
|
|
|
|
#[interrupt]
|
|
|
|
unsafe fn USBCTRL_IRQ() {
|
2021-09-21 19:47:43 +10:00
|
|
|
use core::sync::atomic::{AtomicBool, Ordering};
|
|
|
|
|
|
|
|
/// Note whether we've already printed the "hello" message.
|
|
|
|
static SAID_HELLO: AtomicBool = AtomicBool::new(false);
|
|
|
|
|
|
|
|
// Grab the global objects. This is OK as we only access them under interrupt.
|
2021-09-09 23:03:12 +10:00
|
|
|
let usb_dev = USB_DEVICE.as_mut().unwrap();
|
|
|
|
let serial = USB_SERIAL.as_mut().unwrap();
|
|
|
|
|
2021-09-21 19:47:43 +10:00
|
|
|
// Say hello exactly once on start-up
|
2021-09-21 21:42:34 +10:00
|
|
|
if !SAID_HELLO.load(Ordering::Relaxed) {
|
2021-09-21 19:47:43 +10:00
|
|
|
SAID_HELLO.store(true, Ordering::Relaxed);
|
|
|
|
let _ = serial.write(b"Hello, World!\r\n");
|
2021-09-09 23:03:12 +10:00
|
|
|
}
|
|
|
|
|
2021-09-21 19:47:43 +10:00
|
|
|
// Poll the USB driver with all of our supported USB Classes
|
2021-09-09 23:03:12 +10:00
|
|
|
if usb_dev.poll(&mut [serial]) {
|
2021-09-21 19:47:43 +10:00
|
|
|
let mut buf = [0u8; 64];
|
|
|
|
match serial.read(&mut buf) {
|
|
|
|
Err(_e) => {
|
|
|
|
// Do nothing
|
2021-09-09 23:03:12 +10:00
|
|
|
}
|
2021-09-21 19:47:43 +10:00
|
|
|
Ok(0) => {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
Ok(count) => {
|
|
|
|
// Convert to upper case
|
|
|
|
buf.iter_mut().take(count).for_each(|b| {
|
|
|
|
b.make_ascii_uppercase();
|
|
|
|
});
|
2021-09-09 23:03:12 +10:00
|
|
|
|
2021-09-21 19:47:43 +10:00
|
|
|
// Send back to the host
|
|
|
|
let mut wr_ptr = &buf[..count];
|
|
|
|
while !wr_ptr.is_empty() {
|
|
|
|
let _ = serial.write(wr_ptr).map(|len| {
|
|
|
|
wr_ptr = &wr_ptr[len..];
|
|
|
|
});
|
2021-09-09 23:03:12 +10:00
|
|
|
}
|
|
|
|
}
|
2021-09-21 19:47:43 +10:00
|
|
|
}
|
2021-09-09 23:03:12 +10:00
|
|
|
}
|
|
|
|
}
|
2021-09-21 19:47:43 +10:00
|
|
|
|
|
|
|
// End of file
|