1use core::fmt;
12
13use crate::pac::USB;
14use crate::rcc::{Enable, Reset};
15use stm32_usbd::UsbPeripheral;
16
17use crate::gpio;
18use crate::gpio::gpioa::{PA11, PA12};
19#[allow(clippy::module_name_repetitions)]
20pub use stm32_usbd::UsbBus;
21
22pub trait DmPin: crate::private::Sealed {}
24
25pub trait DpPin: crate::private::Sealed {}
27
28#[cfg(any(feature = "stm32f303xb", feature = "stm32f303xc"))]
29impl DmPin for PA11<gpio::AF14<gpio::PushPull>> {}
30
31#[cfg(any(feature = "stm32f303xb", feature = "stm32f303xc"))]
32impl DpPin for PA12<gpio::AF14<gpio::PushPull>> {}
33
34#[cfg(any(feature = "stm32f303xd", feature = "stm32f303xe"))]
35impl<Mode> DmPin for PA11<Mode> {}
36
37#[cfg(any(feature = "stm32f303xd", feature = "stm32f303xe"))]
38impl<Mode> DpPin for PA12<Mode> {}
39
40pub struct Peripheral<Dm: DmPin, Dp: DpPin> {
45 pub usb: USB,
47 pub pin_dm: Dm,
49 pub pin_dp: Dp,
51}
52
53#[cfg(feature = "defmt")]
54impl<Dm: DmPin + defmt::Format, Dp: DpPin + defmt::Format> defmt::Format for Peripheral<Dm, Dp> {
55 fn format(&self, f: defmt::Formatter) {
56 defmt::write!(
57 f,
58 "Peripheral {{ usb: USB, pin_dm: {}, pin_dp: {}}}",
59 self.pin_dm,
60 self.pin_dp
61 );
62 }
63}
64
65impl<Dm, Dp> fmt::Debug for Peripheral<Dm, Dp>
66where
67 Dm: DmPin + fmt::Debug,
68 Dp: DpPin + fmt::Debug,
69{
70 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
71 f.debug_struct("Peripheral")
72 .field("usb", &"USB")
73 .field("pin_dm", &self.pin_dm)
74 .field("pin_dp", &self.pin_dp)
75 .finish()
76 }
77}
78
79unsafe impl<Dm: DmPin, Dp: DpPin> Sync for Peripheral<Dm, Dp> {}
82
83unsafe impl<Dm: DmPin + Send, Dp: DpPin + Send> UsbPeripheral for Peripheral<Dm, Dp> {
85 const REGISTERS: *const () = USB::ptr().cast::<()>();
86 const DP_PULL_UP_FEATURE: bool = false;
87 const EP_MEMORY: *const () = 0x4000_6000 as _;
88 #[cfg(any(feature = "stm32f303xb", feature = "stm32f303xc"))]
89 const EP_MEMORY_SIZE: usize = 512;
90 #[cfg(any(feature = "stm32f303xd", feature = "stm32f303xe"))]
91 const EP_MEMORY_SIZE: usize = 1024;
92 #[cfg(any(feature = "stm32f303xb", feature = "stm32f303xc"))]
93 const EP_MEMORY_ACCESS_2X16: bool = false;
94 #[cfg(any(feature = "stm32f303xd", feature = "stm32f303xe"))]
95 const EP_MEMORY_ACCESS_2X16: bool = true;
96
97 fn enable() {
98 critical_section::with(|_| unsafe {
101 USB::enable_unchecked();
103 USB::reset_unchecked();
105 });
106 }
107
108 fn startup_delay() {
109 cortex_m::asm::delay(72);
113 }
114}
115
116#[cfg(any(feature = "stm32f303xb", feature = "stm32f303xc"))]
118#[allow(clippy::module_name_repetitions)]
119pub type UsbBusType<Dm = PA11<gpio::AF14<gpio::PushPull>>, Dp = PA12<gpio::AF14<gpio::PushPull>>> =
120 UsbBus<Peripheral<Dm, Dp>>;
121
122#[cfg(any(feature = "stm32f303xd", feature = "stm32f303xe"))]
124pub type UsbBusType<Dm = PA11<gpio::Input>, Dp = PA12<gpio::Input>> = UsbBus<Peripheral<Dm, Dp>>;