|
| 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, ResetEnable}; |
| 11 | +use crate::stm32::{self, USB}; |
| 12 | +use core::marker::PhantomData; |
| 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) -> UsbDevice; |
| 23 | +} |
| 24 | + |
| 25 | +impl UsbExt for stm32::USB { |
| 26 | + fn usb(self, rec: rec::Usb, _pin_dm: DmPin, _pin_dp: DpPin) -> UsbDevice { |
| 27 | + if let USBSEL::Disable = rec.get_kernel_clk_mux() { |
| 28 | + rec.kernel_clk_mux(USBSEL::Hsi48); |
| 29 | + }; |
| 30 | + |
| 31 | + UsbDevice { _usb: self } |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +#[derive(Debug)] |
| 36 | +pub struct UsbDevice { |
| 37 | + /// USB register block |
| 38 | + _usb: USB, |
| 39 | +} |
| 40 | + |
| 41 | +#[cfg(feature = "defmt")] |
| 42 | +impl defmt::Format for UsbDevice { |
| 43 | + fn format(&self, f: defmt::Formatter) { |
| 44 | + defmt::write!(f, "UsbDevice {{ usb: USB }}"); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +// SAFETY: Implementation of UsbDevice is thread-safe by using cricitcal sections to ensure |
| 49 | +// mutually exclusive access to the USB peripheral |
| 50 | +unsafe impl Sync for UsbDevice {} |
| 51 | + |
| 52 | +// SAFETY: The peripheral has the same regiter blockout as the STM32 USBFS |
| 53 | +unsafe impl UsbPeripheral for UsbDevice { |
| 54 | + const REGISTERS: *const () = USB::ptr().cast::<()>(); |
| 55 | + const DP_PULL_UP_FEATURE: bool = true; |
| 56 | + const EP_MEMORY: *const () = 0x4001_6400 as _; |
| 57 | + const EP_MEMORY_SIZE: usize = 2048; |
| 58 | + const EP_MEMORY_ACCESS: stm32_usbd::MemoryAccess = |
| 59 | + stm32_usbd::MemoryAccess::Word32x1; |
| 60 | + |
| 61 | + fn enable() { |
| 62 | + cortex_m::interrupt::free(|_| { |
| 63 | + #[cfg(any(feature = "h523_h533", feature = "h56x_h573"))] |
| 64 | + { |
| 65 | + // Safety: we are only touching the usbscr which |
| 66 | + // is specific for this peripheral. This together with |
| 67 | + // the critical section unsures exclusive access |
| 68 | + let pwr = unsafe { &*stm32::PWR::ptr() }; |
| 69 | + |
| 70 | + // Enable USB supply level detector |
| 71 | + pwr.usbscr().modify(|_, w| w.usb33den().set_bit()); |
| 72 | + |
| 73 | + // Await good usb supply voltage |
| 74 | + while pwr.vmsr().read().usb33rdy().bit_is_clear() {} |
| 75 | + |
| 76 | + // Set bit to confirm that USB supply level is good |
| 77 | + pwr.usbscr().modify(|_, w| w.usb33sv().set_bit()); |
| 78 | + } |
| 79 | + |
| 80 | + // Reset and enable USB peripheral |
| 81 | + rec::Usb { |
| 82 | + _marker: PhantomData, |
| 83 | + } |
| 84 | + .reset() |
| 85 | + .enable(); |
| 86 | + }); |
| 87 | + } |
| 88 | + |
| 89 | + fn startup_delay() { |
| 90 | + // There is a chip specific startup delay. For STM32H503,523,533,56x and 573 it's |
| 91 | + // 1µs and this should wait for at least that long. |
| 92 | + // 250 Mhz is the highest frequency, so this ensures a minimum of 1µs wait time. |
| 93 | + cortex_m::asm::delay(250); |
| 94 | + } |
| 95 | +} |
0 commit comments