|
| 1 | +//! CDC-ACM serial port example using interrupts. |
| 2 | +//! Target board: any STM32F4 with a OTG FS peripheral and a 8MHz HSE crystal |
| 3 | +#![no_std] |
| 4 | +#![no_main] |
| 5 | + |
| 6 | +use panic_halt as _; |
| 7 | + |
| 8 | +use core::cell::RefCell; |
| 9 | +use cortex_m::interrupt::Mutex; |
| 10 | +use cortex_m_rt::entry; |
| 11 | +use stm32f4xx_hal::otg_fs::{UsbBus, USB}; |
| 12 | +use stm32f4xx_hal::pac::{interrupt, Interrupt}; |
| 13 | +use stm32f4xx_hal::{pac, prelude::*}; |
| 14 | +use usb_device::class_prelude::UsbBusAllocator; |
| 15 | +use usb_device::prelude::*; |
| 16 | +use usbd_serial::SerialPort; |
| 17 | + |
| 18 | +// Make USB serial device globally available |
| 19 | +static G_USB_SERIAL: Mutex<RefCell<Option<SerialPort<UsbBus<USB>>>>> = |
| 20 | + Mutex::new(RefCell::new(None)); |
| 21 | + |
| 22 | +// Make USB device globally available |
| 23 | +static G_USB_DEVICE: Mutex<RefCell<Option<UsbDevice<UsbBus<USB>>>>> = |
| 24 | + Mutex::new(RefCell::new(None)); |
| 25 | + |
| 26 | +#[entry] |
| 27 | +fn main() -> ! { |
| 28 | + static mut EP_MEMORY: [u32; 1024] = [0; 1024]; |
| 29 | + static mut USB_BUS: Option<UsbBusAllocator<stm32f4xx_hal::otg_fs::UsbBusType>> = None; |
| 30 | + |
| 31 | + let dp = pac::Peripherals::take().unwrap(); |
| 32 | + |
| 33 | + let rcc = dp.RCC.constrain(); |
| 34 | + |
| 35 | + let clocks = rcc.cfgr.sysclk((168).MHz()).pclk1((8).MHz()).freeze(); |
| 36 | + |
| 37 | + let gpioa = dp.GPIOA.split(); |
| 38 | + |
| 39 | + let usb = USB { |
| 40 | + usb_global: dp.OTG_FS_GLOBAL, |
| 41 | + usb_device: dp.OTG_FS_DEVICE, |
| 42 | + usb_pwrclk: dp.OTG_FS_PWRCLK, |
| 43 | + pin_dm: gpioa.pa11.into_alternate(), |
| 44 | + pin_dp: gpioa.pa12.into_alternate(), |
| 45 | + hclk: clocks.hclk(), |
| 46 | + }; |
| 47 | + |
| 48 | + *USB_BUS = Some(stm32f4xx_hal::otg_fs::UsbBusType::new(usb, EP_MEMORY)); |
| 49 | + let usb_bus = USB_BUS.as_ref().unwrap(); |
| 50 | + |
| 51 | + cortex_m::interrupt::free(|cs| { |
| 52 | + *G_USB_SERIAL.borrow(cs).borrow_mut() = Some(SerialPort::new(usb_bus)); |
| 53 | + |
| 54 | + *G_USB_DEVICE.borrow(cs).borrow_mut() = Some( |
| 55 | + UsbDeviceBuilder::new(usb_bus, UsbVidPid(0x16c0, 0x27dd)) |
| 56 | + .manufacturer("Fake company") |
| 57 | + .product("Serial port") |
| 58 | + .serial_number("TEST") |
| 59 | + .device_class(usbd_serial::USB_CLASS_CDC) |
| 60 | + .build(), |
| 61 | + ); |
| 62 | + }); |
| 63 | + |
| 64 | + unsafe { |
| 65 | + cortex_m::peripheral::NVIC::unmask(Interrupt::OTG_FS); |
| 66 | + } |
| 67 | + |
| 68 | + loop { |
| 69 | + // Do nothing. Everything is done in the IRQ handler |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +#[interrupt] |
| 74 | +fn OTG_FS() { |
| 75 | + static mut USB_SERIAL: Option<SerialPort<UsbBus<USB>>> = None; |
| 76 | + static mut USB_DEVICE: Option<UsbDevice<UsbBus<USB>>> = None; |
| 77 | + |
| 78 | + let usb_dev = USB_DEVICE.get_or_insert_with(|| { |
| 79 | + cortex_m::interrupt::free(|cs| { |
| 80 | + // Move USB device here, leaving a None in its place |
| 81 | + G_USB_DEVICE.borrow(cs).replace(None).unwrap() |
| 82 | + }) |
| 83 | + }); |
| 84 | + |
| 85 | + let serial = USB_SERIAL.get_or_insert_with(|| { |
| 86 | + cortex_m::interrupt::free(|cs| { |
| 87 | + // Move USB serial device here, leaving a None in its place |
| 88 | + G_USB_SERIAL.borrow(cs).replace(None).unwrap() |
| 89 | + }) |
| 90 | + }); |
| 91 | + |
| 92 | + if usb_dev.poll(&mut [serial]) { |
| 93 | + let mut buf = [0u8; 64]; |
| 94 | + |
| 95 | + match serial.read(&mut buf) { |
| 96 | + Ok(count) if count > 0 => { |
| 97 | + // Echo back in upper case |
| 98 | + for c in buf[0..count].iter_mut() { |
| 99 | + if 0x61 <= *c && *c <= 0x7a { |
| 100 | + *c &= !0x20; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + let mut write_offset = 0; |
| 105 | + while write_offset < count { |
| 106 | + match serial.write(&buf[write_offset..count]) { |
| 107 | + Ok(len) if len > 0 => { |
| 108 | + write_offset += len; |
| 109 | + } |
| 110 | + _ => {} |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + _ => {} |
| 115 | + } |
| 116 | + } |
| 117 | +} |
0 commit comments