From 7da90438dd7851f66abfa81eba2eeb36ff9c9c25 Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Mon, 18 Feb 2019 10:56:30 +0000 Subject: Add USB stack and simple HID implementation This patch adds the usb-device and stm32f103xx-usb crates that provide a USB stack. It introduces the HidClass struct, a basic implementation of the Human Interface Device (HID) USB class. Devices with that class are recognized as HID devices with the specified vendor and product ID, but do not provide the endpoints required for interaction. --- src/main.rs | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index d76cf33..b0ecafd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,14 +5,43 @@ #![no_main] extern crate panic_halt; -extern crate hal; -use cortex_m::asm; +mod device; +mod hid; + use cortex_m_rt::entry; +use hal::prelude::*; +use stm32f103xx_usb::UsbBus; +use usb_device::class::UsbClass; + +use crate::hid::{HidClass, Protocol, Subclass}; #[entry] fn main() -> ! { + let p = hal::stm32::Peripherals::take().unwrap(); + + let mut flash = p.FLASH.constrain(); + let mut rcc = p.RCC.constrain(); + + let clocks = rcc + .cfgr + .use_hse(8.mhz()) + .sysclk(48.mhz()) + .pclk1(24.mhz()) + .freeze(&mut flash.acr); + + assert!(clocks.usbclk_valid(), "USB clocks not valid"); + + let mut gpioa = p.GPIOA.split(&mut rcc.apb2); + + let usb_bus = UsbBus::usb_with_reset(p.USB, &mut rcc.apb1, &clocks, &mut gpioa.crh, gpioa.pa12); + let mut usb_class = HidClass::new(&usb_bus, Subclass::BootInterface, Protocol::Keyboard); + let mut usb_dev = device::create_usb_device(&usb_bus); + usb_dev.force_reset().expect("USB reset failed"); + loop { - asm::nop(); + if usb_dev.poll(&mut [&mut usb_class]) { + usb_class.poll(); + } } } -- cgit v1.2.1