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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
//! This library provides high-level access to STM32 peripherals.
//!
//! **Current family support**: F3, Others need your help
//!
//! Please see the [Readme](https://github.com/gd32-rs/gd32-hal/blob/main/README.md) for a detailed overview,
// Some reg modifications are marked `unsafe` in some PAC crates, but not others.
// Disable these warnings.
// The `doc_cfg` feature allows us to show functionality that is feature-gated on `docs.rs`.
// todo: Re-implement the doc_cfg feature and the relevant tags (From all modules that impl EH traits)
// todo oncoe this is in stable.
// #![feature(doc_cfg)]
// todo: H7B3 has too many changes in v14 PAC; not supporting at this time. (2021-10-07)
compile_error!;
// Re-export of the [svd2rust](https://crates.io/crates/svd2rust) auto-generated API for
// stm32 peripherals.
pub use gd32f303 as pac;
// todo: U5 once SVD is out.
// #[cfg(not(any(feature = "f301", feature = "f302")))]
// pub mod adc;
// bxCAN families: F3, F4, L4,
// fdCAN families: L5, U5, G4, H7
// H7 suppords fd and can_ccu. (What's that?)
// WB and WL?
// todo: You could get CRC working on most of these with some effort.
// #[cfg(not(any(
// feature = "f4",
// feature = "g0",
// feature = "g4",
// feature = "l5",
// feature = "wb",
// feature = "wl"
// )))]
// pub mod crc;
// #[cfg(not(any(
// feature = "f401",
// feature = "f411",
// feature = "f412",
// feature = "wb",
// feature = "g0"
// )))]
// // WB doesn't have a DAC. Some G0 variants do - add it! Most F4 variants have it, some don't
// pub mod dac;
// // todo: G0 missing many DMA registers like CCR?
// // todo: F4 needs some mods. So, only working on L4 and G4.
// // todo: L5 has a PAC bug on CCR registers past 1.
// // https://github.com/stm32-rs/stm32-rs/issues/551
// #[cfg(not(any(feature = "f4", feature = "l5")))]
// pub mod dma;
// // #[cfg(not(any(feature = "h747cm4", feature = "h747cm7")))]
// // PAC error on bank 2 accessor for H747cmx.
// pub mod flash;
// // todo: PAC doesn't yet support these newer H7 MCUs that use FMAC.
// // #[cfg(any(feature = "h723", feature = "h725", feature = "h733", feature = "h735"))]
// // todo: Also G4.
// // pub mod fmac;
// // #[cfg(feature = "wb")]
// // pub mod bluetooth;
// // #[cfg(feature = "wb")]
// // pub mod tl_mbox; // Mailbox for communicating with the RF core.
// #[cfg(feature = "wb")]
// pub mod hsem;
// #[cfg(not(feature = "f4"))]
// pub mod i2c;
// #[cfg(feature = "f4")]
// pub mod i2c_f4;
// #[cfg(feature = "f4")]
// pub use i2c_f4 as i2c;
// #[cfg(feature = "wb")]
// pub mod ipcc;
// pub mod low_power;
// #[cfg(any(feature = "h747cm4", feature = "h747cm7"))]
// pub mod power;
// // F3, F4, L5, G0, and WL don't have Quad SPI.
// #[cfg(not(any(
// feature = "f3",
// feature = "f4",
// feature = "l4x3", // todo: PAC bug?
// feature = "l5",
// feature = "g0",
// feature = "g431",
// feature = "g441",
// feature = "g471",
// feature = "g491",
// feature = "g4a1",
// feature = "h7b3",
// feature = "wl",
// )))]
// pub mod qspi;
// // Note: Some F4 variants support RNG, but we haven't figured out the details yet. Send a PR if interested.
// #[cfg(not(any(
// feature = "f3",
// feature = "f4",
// feature = "g030",
// feature = "g031",
// feature = "g070",
// feature = "g071"
// )))]
// pub mod rng;
// pub mod rtc;
// #[cfg(not(any(
// feature = "f3",
// feature = "f4",
// feature = "g0",
// feature = "g4", // todo: G4 PAC issue re getting channel-specific reg blocks.
// feature = "h7b3",
// feature = "wl"
// )))]
// pub mod sai;
// pub mod spi;
// pub mod timer;
// pub mod usart;
// // See note at top of `usb` module for info on G0; not avail on modules the PAC has avail.
// cfg_if::cfg_if! {
// if #[cfg(all(
// feature = "usb",
// all(
// any(
// feature = "f303",
// feature = "l4x2",
// feature = "l412",
// feature = "l4x3",
// feature = "l5",
// feature = "g4",
// feature = "wb",
// ),
// not(feature = "g4a1"))
// ))] {
// pub mod usb;
// } else if #[cfg(all(
// // todo: I think only H7 has hs (high-speed), while F4 and L4 have FS only.
// any(feature = "usbotg_fs", feature = "usbotg_hs"),
// any(feature = "f4", feature = "l4x5", feature = "l4x6", feature = "h7")
// ))] {
// pub mod usb_otg;
// }
// }
// todo: should these helper macros be removed from this library? It has nothing to do with STM32.
/// Syntax helper for getting global variables of the form `Mutex<RefCell<Option>>>` from an interrupt-free
/// context - eg in interrupt handlers.
///
/// Example: `access_global!(DELAY, delay, cs)`
/// Syntax helper for setting global variables of the form `Mutex<RefCell<Option>>>`.
/// eg in interrupt handlers. Ideal for non-copy-type variables that can't be initialized
/// immediatiately.
///
/// Example: `make_globals!(
/// (USB_SERIAL, SerialPort<UsbBusType>),
/// (DELAY, Delay),
/// )`
;
}
/// Syntax helper for setting global variables of the form `Mutex<Cell<>>>`.
/// eg in interrupt handlers. Ideal for copy-type variables.
///
/// Example: `make_simple_globals!(
/// (VALUE, f32, 2.),
/// (SETTING, Setting, Setting::A),
/// )`
;
}
// todo: Remove this debug_workaroudn function on MCUs that don't require it. Ie, is this required on G4? G0?
use free;
// #[cfg(not(any(feature = "g0")))]
// /// Workaround due to debugger disconnecting in WFI (and low-power) modes.
// /// This affects most (all?) STM32 devices. In production on battery-powered
// /// devices that don't use DMA, consider removing this, to prevent power
// /// use by the DMA clock.
// /// For why we enable the DMA clock, see STM32F446 errata, section 2.1.1.
// pub fn debug_workaround() {
// free(|_| {
// let dbgmcu = unsafe { &(*pac::DBGMCU::ptr()) };
// cfg_if::cfg_if! {
// if #[cfg(all(feature = "h7", not(any(feature = "h747cm4", feature = "h747cm7"))))] {
// dbgmcu.cr.modify(|_, w| w.dbgsleep_d1().set_bit());
// dbgmcu.cr.modify(|_, w| w.dbgstop_d1().set_bit());
// dbgmcu.cr.modify(|_, w| w.dbgstby_d1().set_bit());
// } else if #[cfg(feature = "h7")] {
// dbgmcu.cr.modify(|_, w| w.dbgslpd1().set_bit());
// dbgmcu.cr.modify(|_, w| w.dbgstpd1().set_bit());
// dbgmcu.cr.modify(|_, w| w.dbgstbd1().set_bit());
// } else {
// #[cfg(not(feature = "l5"))]
// dbgmcu.cr.modify(|_, w| w.dbg_sleep().set_bit());
// dbgmcu.cr.modify(|_, w| w.dbg_stop().set_bit());
// dbgmcu.cr.modify(|_, w| w.dbg_standby().set_bit());
// }
// }
// });
// free(|_| {
// let rcc = unsafe { &(*pac::RCC::ptr()) };
// // todo Some MCUs may need the dbgmcu lines, but not DMA enabled.
// // todo: Remove this part on MCUs not affected. F4 and L4 are confirmed affected.
// #[cfg(feature = "f3")]
// rcc.ahbenr.modify(|_, w| w.dma1en().set_bit());
// #[cfg(not(feature = "f3"))]
// rcc.ahb1enr.modify(|_, w| w.dma1en().set_bit());
// })
// }
/// In the prelude, we export helper macros.