stm32l4xx_hal/
usb.rs

1//! USB peripheral
2//!
3//! Requires the `stm32-usbd` feature.
4//!
5//! See <https://github.com/stm32-rs/stm32l4xx-hal/tree/master/examples>
6//! for usage examples.
7
8use crate::rcc::{Enable, Reset};
9use crate::stm32::USB;
10use stm32_usbd::UsbPeripheral;
11
12use crate::gpio::gpioa::{PA11, PA12};
13use crate::gpio::{Alternate, PushPull};
14pub use stm32_usbd::UsbBus;
15
16pub struct Peripheral {
17    pub usb: USB,
18    pub pin_dm: PA11<Alternate<PushPull, 10>>,
19    pub pin_dp: PA12<Alternate<PushPull, 10>>,
20}
21
22unsafe impl Sync for Peripheral {}
23
24unsafe impl UsbPeripheral for Peripheral {
25    const REGISTERS: *const () = USB::ptr() as *const ();
26    const DP_PULL_UP_FEATURE: bool = true;
27    const EP_MEMORY: *const () = 0x4000_6c00 as _;
28    const EP_MEMORY_SIZE: usize = 1024;
29    const EP_MEMORY_ACCESS_2X16: bool = true;
30
31    fn enable() {
32        cortex_m::interrupt::free(|_| unsafe {
33            // Enable USB peripheral
34            USB::enable_unchecked();
35
36            // Reset USB peripheral
37            USB::reset_unchecked();
38        });
39    }
40
41    fn startup_delay() {
42        // There is a chip specific startup delay. For STM32F103xx it's 1µs and this should wait for
43        // at least that long.
44        cortex_m::asm::delay(72);
45    }
46}
47
48pub type UsbBusType = UsbBus<Peripheral>;