usb.rs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. //! USB peripheral
  2. //!
  3. //! Requires the `stm32-usbd` feature.
  4. //! See https://github.com/stm32-rs/stm32f1xx-hal/tree/master/examples
  5. //! for usage examples.
  6. use crate::pac::{RCC, USB};
  7. use stm32_usbd::UsbPeripheral;
  8. use crate::gpio::gpioa::{PA11, PA12};
  9. use crate::gpio::{Floating, Input};
  10. pub use stm32_usbd::UsbBus;
  11. pub struct Peripheral {
  12. pub usb: USB,
  13. pub pin_dm: PA11<Input<Floating>>,
  14. pub pin_dp: PA12<Input<Floating>>,
  15. }
  16. unsafe impl Sync for Peripheral {}
  17. unsafe impl UsbPeripheral for Peripheral {
  18. const REGISTERS: *const () = USB::ptr() as *const ();
  19. const DP_PULL_UP_FEATURE: bool = false;
  20. const EP_MEMORY: *const () = 0x4000_6000 as _;
  21. const EP_MEMORY_SIZE: usize = 512;
  22. fn enable() {
  23. let rcc = unsafe { (&*RCC::ptr()) };
  24. cortex_m::interrupt::free(|_| {
  25. // Enable USB peripheral
  26. rcc.apb1enr.modify(|_, w| w.usben().set_bit());
  27. // Reset USB peripheral
  28. rcc.apb1rstr.modify(|_, w| w.usbrst().set_bit());
  29. rcc.apb1rstr.modify(|_, w| w.usbrst().clear_bit());
  30. });
  31. }
  32. fn startup_delay() {
  33. // There is a chip specific startup delay. For STM32F103xx it's 1µs and this should wait for
  34. // at least that long.
  35. cortex_m::asm::delay(72);
  36. }
  37. }
  38. pub type UsbBusType = UsbBus<Peripheral>;