|
| 1 | +//! USB peripheral. |
| 2 | +//! |
| 3 | +//! Provides the required implementation for use of the [`stm32-usbd`] crate. |
| 4 | +
|
| 5 | +use crate::stm32::rcc::ccipr4::USBSEL; |
| 6 | +pub use stm32_usbd::UsbBus; |
| 7 | + |
| 8 | +use crate::gpio; |
| 9 | +use crate::gpio::gpioa::{PA11, PA12}; |
| 10 | +use crate::rcc::rec; |
| 11 | +use crate::stm32::{self, USB}; |
| 12 | +use core::fmt; |
| 13 | +use stm32_usbd::UsbPeripheral; |
| 14 | + |
| 15 | +/// Type for pin that can be the "D-" pin for the USB peripheral |
| 16 | +pub type DmPin = PA11<gpio::Alternate<10>>; |
| 17 | + |
| 18 | +/// Type for pin that can be the "D+" pin for the USB peripheral |
| 19 | +pub type DpPin = PA12<gpio::Alternate<10>>; |
| 20 | + |
| 21 | +pub trait UsbExt { |
| 22 | + fn usb(self, rec: rec::Usb, pin_dm: DmPin, pin_dp: DpPin) -> Peripheral; |
| 23 | +} |
| 24 | + |
| 25 | +impl UsbExt for stm32::USB { |
| 26 | + fn usb(self, rec: rec::Usb, pin_dm: DmPin, pin_dp: DpPin) -> Peripheral { |
| 27 | + if let USBSEL::Disable = rec.get_kernel_clk_mux() { |
| 28 | + rec.kernel_clk_mux(USBSEL::Hsi48); |
| 29 | + }; |
| 30 | + |
| 31 | + Peripheral { |
| 32 | + _usb: self, |
| 33 | + pin_dm, |
| 34 | + pin_dp, |
| 35 | + } |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +pub struct Peripheral { |
| 40 | + /// USB register block |
| 41 | + _usb: USB, |
| 42 | + /// Data negative pin |
| 43 | + pin_dm: DmPin, |
| 44 | + /// Data positive pin |
| 45 | + pin_dp: DpPin, |
| 46 | +} |
| 47 | + |
| 48 | +#[cfg(feature = "defmt")] |
| 49 | +impl defmt::Format for Peripheral { |
| 50 | + fn format(&self, f: defmt::Formatter) { |
| 51 | + defmt::write!( |
| 52 | + f, |
| 53 | + "Peripheral {{ usb: USB, pin_dm: {}, pin_dp: {}}}", |
| 54 | + self.pin_dm, |
| 55 | + self.pin_dp |
| 56 | + ); |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +impl fmt::Debug for Peripheral { |
| 61 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 62 | + f.debug_struct("Peripheral") |
| 63 | + .field("usb", &"USB") |
| 64 | + .field("pin_dm", &self.pin_dm) |
| 65 | + .field("pin_dp", &self.pin_dp) |
| 66 | + .finish() |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +// SAFETY: Implementation of Peripheral is thread-safe by using cricitcal sections to ensure |
| 71 | +// mutually exclusive access to the USB peripheral |
| 72 | +unsafe impl Sync for Peripheral {} |
| 73 | + |
| 74 | +// SAFETY: The peripheral has the same regiter blockout as the STM32 USBFS |
| 75 | +unsafe impl UsbPeripheral for Peripheral { |
| 76 | + const REGISTERS: *const () = USB::ptr().cast::<()>(); |
| 77 | + const DP_PULL_UP_FEATURE: bool = true; |
| 78 | + const EP_MEMORY: *const () = 0x4001_6400 as _; |
| 79 | + const EP_MEMORY_SIZE: usize = 2048; |
| 80 | + const EP_MEMORY_ACCESS: stm32_usbd::MemoryAccess = |
| 81 | + stm32_usbd::MemoryAccess::Word32x1; |
| 82 | + |
| 83 | + fn enable() { |
| 84 | + cortex_m::interrupt::free(|_| { |
| 85 | + let rcc = unsafe { &*stm32::RCC::ptr() }; |
| 86 | + |
| 87 | + #[cfg(any(feature = "h523_h533", feature = "h56x_h573"))] |
| 88 | + { |
| 89 | + let pwr = unsafe { &*stm32::PWR::ptr() }; |
| 90 | + |
| 91 | + // Enable USB supply level detector |
| 92 | + pwr.usbscr().modify(|_, w| w.usb33den().set_bit()); |
| 93 | + |
| 94 | + // Await good usb supply voltage |
| 95 | + while pwr.vmsr().read().usb33rdy().bit_is_clear() {} |
| 96 | + |
| 97 | + // Set bit to confirm that USB supply level is good |
| 98 | + pwr.usbscr().modify(|_, w| w.usb33sv().set_bit()); |
| 99 | + } |
| 100 | + |
| 101 | + // Enable USB peripheral |
| 102 | + rcc.apb2enr().modify(|_, w| w.usben().set_bit()); |
| 103 | + |
| 104 | + // Reset USB peripheral |
| 105 | + rcc.apb2rstr().modify(|_, w| w.usbrst().set_bit()); |
| 106 | + rcc.apb2rstr().modify(|_, w| w.usbrst().clear_bit()); |
| 107 | + }); |
| 108 | + } |
| 109 | + |
| 110 | + fn startup_delay() { |
| 111 | + // There is a chip specific startup delay. For STM32H503,523,533,56x and 573 it's |
| 112 | + // 1µs and this should wait for at least that long. |
| 113 | + // 250 Mhz is the highest frequency, so this ensures a minimum of 1µs wait time. |
| 114 | + cortex_m::asm::delay(250); |
| 115 | + } |
| 116 | +} |
0 commit comments