aboutsummaryrefslogtreecommitdiffstats
path: root/sw/eval-clock-cw-tx/src
diff options
context:
space:
mode:
authorMatthias P. Braendli <matthias.braendli@mpb.li>2021-03-31 17:40:53 +0200
committerMatthias P. Braendli <matthias.braendli@mpb.li>2021-03-31 17:40:53 +0200
commit62fdd309c55286222d1add4ca3051353da0e11b8 (patch)
tree1beb88a3d2e4b977b170f41f6d6de020aad8bf46 /sw/eval-clock-cw-tx/src
parentb2b6ac9eea2eb5066942237bfced3f7f8b48c43c (diff)
downloadpicardy-62fdd309c55286222d1add4ca3051353da0e11b8.tar.gz
picardy-62fdd309c55286222d1add4ca3051353da0e11b8.tar.bz2
picardy-62fdd309c55286222d1add4ca3051353da0e11b8.zip
Enable USB CDC ACM for eval-clock-cw-tx
Diffstat (limited to 'sw/eval-clock-cw-tx/src')
-rw-r--r--sw/eval-clock-cw-tx/src/main.rs14
-rw-r--r--sw/eval-clock-cw-tx/src/usb.rs91
2 files changed, 105 insertions, 0 deletions
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<UsbBusAllocator<UsbBusType>> = None;
+static mut USB_SERIAL: Option<usbd_serial::SerialPort<UsbBusType>> = None;
+static mut USB_DEVICE: Option<UsbDevice<UsbBusType>> = None;
+
+pub struct USB {
+}
+
+impl USB {
+ pub fn new(
+ usb: stm32f1xx_hal::pac::USB,
+ pin_dm: gpio::gpioa::PA11<gpio::Input<gpio::Floating>>,
+ pin_dp: gpio::gpioa::PA12<gpio::Input<gpio::Floating>>) -> 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();
+ }
+ _ => {}
+ }
+}