1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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());
}