aboutsummaryrefslogtreecommitdiffstats
path: root/sw/eval-clock-cw-tx/src/usb.rs
diff options
context:
space:
mode:
Diffstat (limited to 'sw/eval-clock-cw-tx/src/usb.rs')
-rw-r--r--sw/eval-clock-cw-tx/src/usb.rs91
1 files changed, 91 insertions, 0 deletions
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();
+ }
+ _ => {}
+ }
+}