From 62fdd309c55286222d1add4ca3051353da0e11b8 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Wed, 31 Mar 2021 17:40:53 +0200 Subject: Enable USB CDC ACM for eval-clock-cw-tx --- sw/eval-clock-cw-tx/src/main.rs | 14 +++++++ sw/eval-clock-cw-tx/src/usb.rs | 91 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 sw/eval-clock-cw-tx/src/usb.rs (limited to 'sw/eval-clock-cw-tx/src') diff --git a/sw/eval-clock-cw-tx/src/main.rs b/sw/eval-clock-cw-tx/src/main.rs index c91c545..d30318b 100644 --- a/sw/eval-clock-cw-tx/src/main.rs +++ b/sw/eval-clock-cw-tx/src/main.rs @@ -46,6 +46,7 @@ use embedded_hal::digital::v2::InputPin; use hd44780_driver::{Cursor, CursorBlink, Display, DisplayMode, HD44780}; pub mod ui; +pub mod usb; pub mod cw; pub mod state; pub mod si_clock; @@ -92,8 +93,14 @@ fn main() -> ! { let mut rcc = dp.RCC.constrain(); let mut afio = dp.AFIO.constrain(&mut rcc.apb2); let clocks = rcc.cfgr + .use_hse(16.mhz()) + .sysclk(48.mhz()) + .pclk1(24.mhz()) .adcclk(2.mhz()) .freeze(&mut flash.acr); + + assert!(clocks.usbclk_valid()); + let mut delay = Delay::new(cp.SYST, clocks); delay.delay_ms(200u16); @@ -102,6 +109,10 @@ fn main() -> ! { let mut gpiob = dp.GPIOB.split(&mut rcc.apb2); let mut gpioc = dp.GPIOC.split(&mut rcc.apb2); + let usb_dm = gpioa.pa11; + let usb_dp = gpioa.pa12.into_floating_input(&mut gpioa.crh); + let usb = usb::USB::new(dp.USB, usb_dm, usb_dp); + // Buttons as analog inputs (multi-level) let pb0 = gpiob.pb0.into_floating_input(&mut gpiob.crl); // BTN1 Button B, has external pullup let pb1 = gpiob.pb1.into_floating_input(&mut gpiob.crl); // BTN0 Button A, has external pullup @@ -173,6 +184,7 @@ fn main() -> ! { lcd.write_str(" 30m CW TX 2021 ", &mut delay).unwrap(); delay.delay_ms(1_500u16); + usb.enable_interrupts(); let mut siclock = { let shared = unsafe { &mut *SHARED.as_mut_ptr() }; @@ -349,6 +361,7 @@ fn TIM2() { } } +#[allow(non_snake_case)] #[cortex_m_rt::exception] fn HardFault(ef: &ExceptionFrame) -> ! { let periph = unsafe { cortex_m::Peripherals::steal() }; @@ -360,6 +373,7 @@ fn HardFault(ef: &ExceptionFrame) -> ! { loop { } } +#[allow(non_snake_case)] #[cortex_m_rt::exception] fn DefaultHandler(irqn: i16) { hprintln!("Unhandled exception (IRQn = {})", irqn).unwrap(); diff --git a/sw/eval-clock-cw-tx/src/usb.rs b/sw/eval-clock-cw-tx/src/usb.rs new file mode 100644 index 0000000..941a024 --- /dev/null +++ b/sw/eval-clock-cw-tx/src/usb.rs @@ -0,0 +1,91 @@ + +use stm32f1xx_hal::gpio; +use stm32f1xx_hal::pac; +use stm32f1xx_hal::pac::{interrupt, Interrupt}; +use stm32f1xx_hal::usb::{Peripheral, UsbBus, UsbBusType}; +use usb_device::{bus::UsbBusAllocator, prelude::*}; +use usbd_serial::{SerialPort, USB_CLASS_CDC}; + +static mut USB_BUS: Option> = None; +static mut USB_SERIAL: Option> = None; +static mut USB_DEVICE: Option> = None; + +pub struct USB { +} + +impl USB { + pub fn new( + usb: stm32f1xx_hal::pac::USB, + pin_dm: gpio::gpioa::PA11>, + pin_dp: gpio::gpioa::PA12>) -> Self { + + let peripheral = Peripheral { + usb, + pin_dm, + pin_dp, + }; + + unsafe { + let bus = UsbBus::new(peripheral); + + USB_BUS = Some(bus); + + USB_SERIAL = Some(SerialPort::new(USB_BUS.as_ref().unwrap())); + + let usb_dev = UsbDeviceBuilder::new(USB_BUS.as_ref().unwrap(), UsbVidPid(0x1d50, 0x5120)) // Openmoko Neo1973 serial + .manufacturer("HB9EGM") + .product("Beep Machine") + .serial_number("1") + .device_class(USB_CLASS_CDC) + .build(); + + USB_DEVICE = Some(usb_dev); + } + + USB{} + } + + pub fn enable_interrupts(&self) { + unsafe { + pac::NVIC::unmask(Interrupt::USB_HP_CAN_TX); + pac::NVIC::unmask(Interrupt::USB_LP_CAN_RX0); + } + } +} + +#[allow(non_snake_case)] +#[interrupt] +fn USB_HP_CAN_TX() { + usb_interrupt(); +} + +#[allow(non_snake_case)] +#[interrupt] +fn USB_LP_CAN_RX0() { + usb_interrupt(); +} + +fn usb_interrupt() { + let usb_dev = unsafe { USB_DEVICE.as_mut().unwrap() }; + let serial = unsafe { USB_SERIAL.as_mut().unwrap() }; + + if !usb_dev.poll(&mut [serial]) { + return; + } + + let mut buf = [0u8; 8]; + + match serial.read(&mut buf) { + Ok(count) if count > 0 => { + // Echo back in upper case + for c in buf[0..count].iter_mut() { + if 0x61 <= *c && *c <= 0x7a { + *c &= !0x20; + } + } + + serial.write(&buf[0..count]).ok(); + } + _ => {} + } +} -- cgit v1.2.3