use crate::{
pac::{PWR, RCC},
rcc_en_reset,
};
#[cfg(not(feature = "g4"))]
use crate::pac::USB;
#[cfg(feature = "g4")]
use crate::pac::USB_FS_DEVICE as USB;
pub use stm32_usbd::UsbBus;
use stm32_usbd::UsbPeripheral;
use cfg_if::cfg_if;
pub struct Peripheral {
pub usb: USB,
}
unsafe impl Sync for Peripheral {}
unsafe impl UsbPeripheral for Peripheral {
const REGISTERS: *const () = USB::ptr() as *const ();
#[cfg(feature = "f3")]
const DP_PULL_UP_FEATURE: bool = false;
#[cfg(not(feature = "f3"))]
const DP_PULL_UP_FEATURE: bool = true;
#[cfg(any(feature = "l4", feature = "wb"))]
const EP_MEMORY: *const () = 0x4000_6c00 as _;
#[cfg(feature = "l5")]
const EP_MEMORY: *const () = 0x5000_d800 as _;
#[cfg(feature = "g0")]
const EP_MEMORY: *const () = 0x4000_5c00 as _;
#[cfg(any(feature = "f3", feature = "g4"))]
const EP_MEMORY: *const () = 0x4000_6000 as _;
#[cfg(feature = "f3")]
const EP_MEMORY_SIZE: usize = 512;
#[cfg(any(feature = "l4", feature = "l5", feature = "g4", feature = "wb"))]
const EP_MEMORY_SIZE: usize = 1_024;
#[cfg(feature = "g0")]
const EP_MEMORY_SIZE: usize = 2_048;
#[cfg(any(feature = "l4", feature = "l5", feature = "g4", feature = "wb"))]
const EP_MEMORY_ACCESS_2X16: bool = true;
#[cfg(any(feature = "f3", feature = "g0"))]
const EP_MEMORY_ACCESS_2X16: bool = false;
fn enable() {
let rcc = unsafe { &*RCC::ptr() };
cortex_m::interrupt::free(|_| {
cfg_if! {
if #[cfg(feature = "l4")] {
rcc_en_reset!(apb1, usbfs, rcc);
} else if #[cfg(feature = "l5")] {
rcc.apb1enr2.modify(|_, w| w.usbfsen().set_bit());
rcc.apb1rstr2.modify(|_, w| w.usbfsrst().set_bit());
rcc.apb1rstr2.modify(|_ , w| w.usbfsrst().clear_bit());
} else if #[cfg(feature = "wb")] {
rcc.apb1enr1.modify(|_, w| w.usben().set_bit());
rcc.apb1rstr1.modify(|_, w| w.usbfsrst().set_bit());
rcc.apb1rstr1.modify(|_ , w| w.usbfsrst().clear_bit());
} else { rcc_en_reset!(apb1, usb, rcc);
}
}
});
}
fn startup_delay() {
cortex_m::asm::delay(80);
}
}
pub type UsbBusType = UsbBus<Peripheral>;
#[cfg(any(feature = "l4", feature = "l5", feature = "g0"))]
pub fn enable_usb_pwr(pwr: &mut PWR, rcc: &mut RCC) {
pwr.cr2.modify(|_, w| w.usv().set_bit());
}