stm32f1xx_hal/
usb.rs

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
7use crate::pac::USB;
8use crate::rcc::{Enable, Reset};
9use stm32_usbd::{MemoryAccess, UsbPeripheral};
10
11use crate::gpio::gpioa::{PA11, PA12};
12use crate::gpio::{Floating, Input};
13pub use stm32_usbd::UsbBus;
14
15pub struct Peripheral {
16    pub usb: USB,
17    pub pin_dm: PA11<Input<Floating>>,
18    pub pin_dp: PA12<Input<Floating>>,
19}
20
21unsafe impl Sync for Peripheral {}
22
23unsafe impl UsbPeripheral for Peripheral {
24    const REGISTERS: *const () = USB::ptr() as *const ();
25    const DP_PULL_UP_FEATURE: bool = false;
26    const EP_MEMORY: *const () = 0x4000_6000 as _;
27    const EP_MEMORY_SIZE: usize = 512;
28    const EP_MEMORY_ACCESS: MemoryAccess = MemoryAccess::Word16x1;
29
30    fn enable() {
31        unsafe {
32            // Enable USB peripheral
33            USB::enable_unchecked();
34            // Reset USB peripheral
35            USB::reset_unchecked();
36        }
37    }
38
39    fn startup_delay() {
40        // There is a chip specific startup delay. For STM32F103xx it's 1µs and this should wait for
41        // at least that long.
42        cortex_m::asm::delay(72);
43    }
44}
45
46pub type UsbBusType = UsbBus<Peripheral>;