use core::borrow::BorrowMut;
use cfg_if::cfg_if;
pub use stm32_usbd::UsbBus;
use stm32_usbd::{MemoryAccess, UsbPeripheral};
#[cfg(any(feature = "l4", feature = "l5", feature = "g0"))]
use crate::pac::PWR;
use crate::{pac, pac::USB, util::rcc_en_reset};
pub struct Peripheral {
pub regs: 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(feature = "c0")]
const EP_MEMORY: *const () = 0x4000_9800 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(feature = "c0")]
const EP_MEMORY_SIZE: usize = 2_048;
#[cfg(any(feature = "l4", feature = "l5", feature = "g4", feature = "wb"))]
const EP_MEMORY_ACCESS: MemoryAccess = MemoryAccess::Word16x2;
#[cfg(any(feature = "f3", feature = "g0"))]
const EP_MEMORY_ACCESS: MemoryAccess = MemoryAccess::Word16x2;
#[cfg(any(feature = "c0", feature = "h5"))]
const EP_MEMORY_ACCESS: MemoryAccess = MemoryAccess::Word32x1;
fn enable() {
let rcc = unsafe { &*pac::RCC::ptr() };
cfg_if! {
if #[cfg(feature = "l4")] {
cfg_if! {
if #[cfg(feature = "l4x5")] {
rcc_en_reset!(ahb2, otgfs, rcc); } else {
rcc_en_reset!(apb1, usbfs, rcc);
}
}
} else if #[cfg(feature = "l5")] {
rcc.apb1enr2().modify(|_, w| w.usbfsen().bit(true));
rcc.apb1rstr2().modify(|_, w| w.usbfsrst().bit(true));
rcc.apb1rstr2().modify(|_ , w| w.usbfsrst().clear_bit());
} else if #[cfg(feature = "wb")] {
rcc.apb1enr1().modify(|_, w| w.usben().bit(true));
rcc.apb1rstr1().modify(|_, w| w.usbfsrst().bit(true));
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() {
let pwr = unsafe { &*pac::PWR::ptr() };
pwr.cr2().modify(|_, w| w.usv().bit(true));
}