|
| 1 | +//! USB peripheral. |
| 2 | +//! |
| 3 | +//! Provides the required implementation for use of the [`stm32-usbd`] crate. |
| 4 | +
|
| 5 | +pub use stm32_usbd::UsbBus; |
| 6 | + |
| 7 | +use crate::gpio; |
| 8 | +use crate::gpio::gpioa::{PA11, PA12}; |
| 9 | +use crate::rcc::{Enable, Reset}; |
| 10 | +use crate::stm32::{RCC, USB}; |
| 11 | +use core::fmt; |
| 12 | +use stm32_usbd::UsbPeripheral; |
| 13 | + |
| 14 | +/// Trait implemented by all pins that can be the "D-" pin for the USB peripheral |
| 15 | +pub trait DmPin: crate::Sealed {} |
| 16 | + |
| 17 | +/// Trait implemented by all pins that can be the "D+" pin for the USB peripheral |
| 18 | +pub trait DpPin: crate::Sealed {} |
| 19 | + |
| 20 | +impl DmPin for PA11<gpio::Alternate<{ gpio::AF14 }>> {} |
| 21 | +impl DpPin for PA12<gpio::Alternate<{ gpio::AF14 }>> {} |
| 22 | + |
| 23 | +pub struct Peripheral<Dm: DmPin, Dp: DpPin> { |
| 24 | + /// USB register block |
| 25 | + pub usb: USB, |
| 26 | + /// Data negative pin |
| 27 | + pub pin_dm: Dm, |
| 28 | + /// Data positive pin |
| 29 | + pub pin_dp: Dp, |
| 30 | +} |
| 31 | + |
| 32 | +impl<Dm, Dp> fmt::Debug for Peripheral<Dm, Dp> |
| 33 | +where |
| 34 | + Dm: DmPin + fmt::Debug, |
| 35 | + Dp: DpPin + fmt::Debug, |
| 36 | +{ |
| 37 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 38 | + f.debug_struct("Peripheral") |
| 39 | + .field("usb", &"USB") |
| 40 | + .field("pin_dm", &self.pin_dm) |
| 41 | + .field("pin_dp", &self.pin_dp) |
| 42 | + .finish() |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +// SAFETY: Implementation of Peripheral is thread-safe by using cricitcal sections to ensure |
| 47 | +// mutually exclusive access to the USB peripheral |
| 48 | +unsafe impl<Dm: DmPin, Dp: DpPin> Sync for Peripheral<Dm, Dp> {} |
| 49 | + |
| 50 | +// SAFETY: The peripheral has the same regiter blockout as the STM32 USBFS |
| 51 | +unsafe impl<Dm: DmPin + Send, Dp: DpPin + Send> UsbPeripheral for Peripheral<Dm, Dp> { |
| 52 | + const REGISTERS: *const () = USB::ptr().cast::<()>(); |
| 53 | + const DP_PULL_UP_FEATURE: bool = true; |
| 54 | + const EP_MEMORY: *const () = 0x4000_6000 as _; |
| 55 | + const EP_MEMORY_SIZE: usize = 1024; |
| 56 | + const EP_MEMORY_ACCESS_2X16: bool = true; |
| 57 | + |
| 58 | + fn enable() { |
| 59 | + cortex_m::interrupt::free(|_| unsafe { |
| 60 | + let rcc_ptr = &(*RCC::ptr()); |
| 61 | + USB::enable(rcc_ptr); |
| 62 | + USB::reset(rcc_ptr); |
| 63 | + }); |
| 64 | + } |
| 65 | + |
| 66 | + fn startup_delay() { |
| 67 | + // not required |
| 68 | + } |
| 69 | +} |
0 commit comments