forked from stm32-rs/stm32h5xx-hal
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgpio.rs
More file actions
523 lines (472 loc) · 16 KB
/
Copy pathgpio.rs
File metadata and controls
523 lines (472 loc) · 16 KB
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
//! General Purpose Input / Output
//!
//! The GPIO pins are organised into groups of 16 pins which can be accessed through the
//! `gpioa`, `gpiob`... modules. To get access to the pins, you first need to convert them into a
//! HAL designed struct from the `pac` struct using the [split](trait.GpioExt.html#tymethod.split) function.
//! ```rust
//! // Acquire the GPIOA peripheral
//! // NOTE: `dp` is the device peripherals from the `PAC` crate
//! let mut gpioa = dp.GPIOA.split();
//! ```
//!
//! This gives you a struct containing all the pins `px0..px15`.
//! By default pins are in floating input mode. You can change their modes.
//! For example, to set `pa5` high, you would call
//!
//! ```rust
//! let output = gpioa.pa5.into_push_pull_output();
//! output.set_high();
//! ```
//!
//! ## Modes
//!
//! Each GPIO pin can be set to various modes:
//!
//! - **Alternate**: Pin mode required when the pin is driven by other peripherals
//! - **Analog**: Analog input to be used with ADC.
//! - **Dynamic**: Pin mode is selected at runtime. See changing configurations for more details
//! - Input
//! - **PullUp**: Input connected to high with a weak pull up resistor. Will be high when nothing
//! is connected
//! - **PullDown**: Input connected to high with a weak pull up resistor. Will be low when nothing
//! is connected
//! - **Floating**: Input not pulled to high or low. Will be undefined when nothing is connected
//! - Output
//! - **PushPull**: Output which either drives the pin high or low
//! - **OpenDrain**: Output which leaves the gate floating, or pulls it do ground in drain
//! mode. Can be used as an input in the `open` configuration
//!
//! ## Changing modes
//! The simplest way to change the pin mode is to use the `into_<mode>` functions. These return a
//! new struct with the correct mode that you can use the input or output functions on.
//!
//! If you need a more temporary mode change, and can not use the `into_<mode>` functions for
//! ownership reasons, you can use the closure based `with_<mode>` functions to temporarily change the pin type, do
//! some output or input, and then have it change back once done.
//!
//! ### Dynamic Mode Change
//! The above mode change methods guarantee that you can only call input functions when the pin is
//! in input mode, and output when in output modes, but can lead to some issues. Therefore, there
//! is also a mode where the state is kept track of at runtime, allowing you to change the mode
//! often, and without problems with ownership, or references, at the cost of some performance and
//! the risk of runtime errors.
//!
//! To make a pin dynamic, use the `into_dynamic` function, and then use the `make_<mode>` functions to
//! change the mode
mod convert;
mod dynamic;
mod erased;
mod exti;
mod gpio_def;
mod hal;
mod partially_erased;
use core::{fmt, marker::PhantomData};
use crate::rcc::ResetEnable;
pub use convert::PinMode;
pub use dynamic::{Dynamic, DynamicPin};
pub use embedded_hal::digital::PinState;
pub use erased::{EPin, ErasedPin};
pub use exti::ExtiPin;
pub use gpio_def::*;
pub use partially_erased::{PEPin, PartiallyErasedPin};
/// A filler pin type
#[derive(Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct NoPin;
/// Extension trait to split a GPIO peripheral into independent pins and
/// registers
pub trait GpioExt {
/// The parts to split the GPIO into
type Parts;
/// The Reset and Enable control block for this GPIO block
type Rec: ResetEnable;
/// Takes the GPIO peripheral and splits it into Zero-Sized Types
/// (ZSTs) representing individual pins. These are public
/// members of the return type.
///
/// ```
/// let device_peripherals = stm32::Peripherals.take().unwrap();
/// let ccdr = ...; // From RCC
///
/// let gpioa = device_peripherals.GPIOA.split(ccdr.peripheral.GPIOA);
///
/// let pa0 = gpioa.pa0; // Pin 0
/// ```
fn split(self, prec: Self::Rec) -> Self::Parts;
/// As [split](GpioExt#tymethod.split), but does not reset the GPIO
/// peripheral in the RCC_AHB4RSTR register. However it still enables the
/// peripheral in RCC_AHB4ENR, so our accesses to the peripheral memory will
/// always be valid.
///
/// This is useful for situations where some GPIO functionality
/// was already activated outside the HAL in early startup code
/// or a bootloader. That might be needed for watchdogs, clock
/// circuits, or executing from an external memory. In this
/// case, `split_without_reset` allows this GPIO HAL to be used
/// without generating unwanted edges on already initialised
/// pins.
///
/// However, the user takes responsibility that the GPIO
/// peripheral is in a valid state already. Note that the
/// registers accessed and written by this HAL may change in any
/// patch revision.
fn split_without_reset(self, prec: Self::Rec) -> Self::Parts;
}
/// GPIO peripheral corresponding to GPIOA, GPIOB, etc
pub(crate) struct Gpio<const P: char>;
/// Id, port and mode for any pin
pub trait PinExt {
/// Current pin mode
type Mode;
/// Pin number
fn pin_id(&self) -> u8;
/// Port number starting from 0
fn port_id(&self) -> u8;
}
/// Some alternate mode (type state)
pub struct Alternate<const A: u8, Otype = PushPull>(PhantomData<Otype>);
/// Input mode (type state)
pub struct Input;
/// Pull setting for an input.
#[derive(Debug, Eq, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Pull {
/// Floating
None = 0,
/// Pulled up
Up = 1,
/// Pulled down
Down = 2,
}
/// Open drain input or output (type state)
pub struct OpenDrain;
/// Output mode (type state)
pub struct Output<MODE = PushPull> {
_mode: PhantomData<MODE>,
}
/// Push pull output (type state)
pub struct PushPull;
/// Analog mode (type state)
pub struct Analog;
/// JTAG/SWD mode (type state)
pub type Debugger = Alternate<0, PushPull>;
mod marker {
/// Marker trait that show if `ExtiPin` can be implemented
pub trait Interruptable {}
/// Marker trait for readable pin modes
pub trait Readable {}
/// Marker trait for slew rate configurable pin modes
pub trait OutputSpeed {}
/// Marker trait for active pin modes
pub trait Active {}
/// Marker trait for all pin modes except alternate
pub trait NotAlt {}
/// Marker trait for pins with alternate function `A` mapping
pub trait IntoAf<const A: u8> {}
}
impl<MODE> marker::Interruptable for Output<MODE> {}
impl marker::Interruptable for Input {}
impl marker::Readable for Input {}
impl<const A: u8, MODE> marker::Readable for Alternate<A, MODE> {}
impl marker::Readable for Output<OpenDrain> {}
impl marker::Active for Input {}
impl<Otype> marker::OutputSpeed for Output<Otype> {}
impl<const A: u8, Otype> marker::OutputSpeed for Alternate<A, Otype> {}
impl<Otype> marker::Active for Output<Otype> {}
impl<const A: u8, Otype> marker::Active for Alternate<A, Otype> {}
impl marker::NotAlt for Input {}
impl<Otype> marker::NotAlt for Output<Otype> {}
impl marker::NotAlt for Analog {}
/// GPIO Pin speed selection
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum Speed {
/// Low speed
Low = 0,
/// Medium speed
Medium = 1,
/// High speed
High = 2,
/// Very high speed
VeryHigh = 3,
}
/// GPIO interrupt trigger edge selection
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum Edge {
/// Rising edge of voltage
Rising,
/// Falling edge of voltage
Falling,
/// Rising and falling edge of voltage
RisingFalling,
}
#[doc = "Alternate function 0 (type state)"]
pub type AF0<Otype = PushPull> = Alternate<0, Otype>;
#[doc = "Alternate function 1 (type state)"]
pub type AF1<Otype = PushPull> = Alternate<1, Otype>;
#[doc = "Alternate function 2 (type state)"]
pub type AF2<Otype = PushPull> = Alternate<2, Otype>;
#[doc = "Alternate function 3 (type state)"]
pub type AF3<Otype = PushPull> = Alternate<3, Otype>;
#[doc = "Alternate function 4 (type state)"]
pub type AF4<Otype = PushPull> = Alternate<4, Otype>;
#[doc = "Alternate function 5 (type state)"]
pub type AF5<Otype = PushPull> = Alternate<5, Otype>;
#[doc = "Alternate function 6 (type state)"]
pub type AF6<Otype = PushPull> = Alternate<6, Otype>;
#[doc = "Alternate function 7 (type state)"]
pub type AF7<Otype = PushPull> = Alternate<7, Otype>;
#[doc = "Alternate function 8 (type state)"]
pub type AF8<Otype = PushPull> = Alternate<8, Otype>;
#[doc = "Alternate function 9 (type state)"]
pub type AF9<Otype = PushPull> = Alternate<9, Otype>;
#[doc = "Alternate function 10 (type state)"]
pub type AF10<Otype = PushPull> = Alternate<10, Otype>;
#[doc = "Alternate function 11 (type state)"]
pub type AF11<Otype = PushPull> = Alternate<11, Otype>;
#[doc = "Alternate function 12 (type state)"]
pub type AF12<Otype = PushPull> = Alternate<12, Otype>;
#[doc = "Alternate function 13 (type state)"]
pub type AF13<Otype = PushPull> = Alternate<13, Otype>;
#[doc = "Alternate function 14 (type state)"]
pub type AF14<Otype = PushPull> = Alternate<14, Otype>;
#[doc = "Alternate function 15 (type state)"]
pub type AF15<Otype = PushPull> = Alternate<15, Otype>;
/// Generic pin type
///
/// - `MODE` is one of the pin modes (see [Modes](crate::gpio#modes) section).
/// - `P` is port name: `A` for GPIOA, `B` for GPIOB, etc.
/// - `N` is pin number: from `0` to `15`.
pub struct Pin<const P: char, const N: u8, MODE = Analog> {
_mode: PhantomData<MODE>,
}
impl<const P: char, const N: u8, MODE> Pin<P, N, MODE> {
const fn new() -> Self {
Self { _mode: PhantomData }
}
}
impl<const P: char, const N: u8, MODE> fmt::Debug for Pin<P, N, MODE> {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_fmt(format_args!(
"P{}{}<{}>",
P,
N,
crate::stripped_type_name::<MODE>()
))
}
}
#[cfg(feature = "defmt")]
impl<const P: char, const N: u8, MODE> defmt::Format for Pin<P, N, MODE> {
fn format(&self, f: defmt::Formatter) {
defmt::write!(
f,
"P{}{}<{}>",
P,
N,
crate::stripped_type_name::<MODE>()
);
}
}
impl<const P: char, const N: u8, MODE> PinExt for Pin<P, N, MODE> {
type Mode = MODE;
#[inline(always)]
fn pin_id(&self) -> u8 {
N
}
#[inline(always)]
fn port_id(&self) -> u8 {
P as u8 - b'A'
}
}
impl<const P: char, const N: u8, MODE> Pin<P, N, MODE>
where
MODE: marker::OutputSpeed,
{
/// Set pin speed
pub fn set_speed(&mut self, speed: Speed) {
let offset = N;
unsafe {
(*Gpio::<P>::ptr())
.ospeedr()
.modify(|_r, w| w.ospeed(offset).bits(speed as u8));
}
}
/// Set pin speed
pub fn speed(mut self, speed: Speed) -> Self {
self.set_speed(speed);
self
}
}
impl<const P: char, const N: u8, MODE> Pin<P, N, MODE>
where
MODE: marker::Active,
{
/// Set the internal pull-up and pull-down resistor
pub fn set_internal_resistor(&mut self, resistor: Pull) {
let offset = N;
let value = resistor as u8;
unsafe {
(*Gpio::<P>::ptr())
.pupdr()
.modify(|_r, w| w.pupd(offset).bits(value));
}
}
/// Set the internal pull-up and pull-down resistor
pub fn internal_resistor(mut self, resistor: Pull) -> Self {
self.set_internal_resistor(resistor);
self
}
/// Enables / disables the internal pull up
pub fn internal_pull_up(self, on: bool) -> Self {
if on {
self.internal_resistor(Pull::Up)
} else {
self.internal_resistor(Pull::None)
}
}
/// Enables / disables the internal pull down
pub fn internal_pull_down(self, on: bool) -> Self {
if on {
self.internal_resistor(Pull::Down)
} else {
self.internal_resistor(Pull::None)
}
}
}
impl<const P: char, const N: u8, MODE> Pin<P, N, MODE> {
/// Erases the pin number from the type
///
/// This is useful when you want to collect the pins into an array where you
/// need all the elements to have the same type
pub fn erase_number(self) -> PartiallyErasedPin<P, MODE> {
PartiallyErasedPin::new(N)
}
/// Erases the pin number and the port from the type
///
/// This is useful when you want to collect the pins into an array where you
/// need all the elements to have the same type
pub fn erase(self) -> ErasedPin<MODE> {
ErasedPin::new(P as u8 - b'A', N)
}
}
impl<const P: char, const N: u8, MODE> From<Pin<P, N, MODE>>
for PartiallyErasedPin<P, MODE>
{
/// Pin-to-partially erased pin conversion using the [`From`] trait.
///
/// Note that [`From`] is the reciprocal of [`Into`].
fn from(p: Pin<P, N, MODE>) -> Self {
p.erase_number()
}
}
impl<const P: char, const N: u8, MODE> From<Pin<P, N, MODE>>
for ErasedPin<MODE>
{
/// Pin-to-erased pin conversion using the [`From`] trait.
///
/// Note that [`From`] is the reciprocal of [`Into`].
fn from(p: Pin<P, N, MODE>) -> Self {
p.erase()
}
}
impl<const P: char, const N: u8, MODE> Pin<P, N, MODE> {
/// Set the output of the pin regardless of its mode.
/// Primarily used to set the output value of the pin
/// before changing its mode to an output to avoid
/// a short spike of an incorrect value
#[inline(always)]
fn _set_state(&mut self, state: PinState) {
match state {
PinState::High => self._set_high(),
PinState::Low => self._set_low(),
}
}
#[inline(always)]
fn _set_high(&mut self) {
// NOTE(unsafe) atomic write to a stateless register
unsafe {
(*Gpio::<P>::ptr()).bsrr().write(|w| w.bs(N).set_bit());
}
}
#[inline(always)]
fn _set_low(&mut self) {
// NOTE(unsafe) atomic write to a stateless register
unsafe {
(*Gpio::<P>::ptr()).bsrr().write(|w| w.br(N).set_bit());
}
}
#[inline(always)]
fn _is_set_low(&self) -> bool {
// NOTE(unsafe) atomic read with no side effects
unsafe { (*Gpio::<P>::ptr()).odr().read().od(N).is_low() }
}
#[inline(always)]
fn _is_low(&self) -> bool {
// NOTE(unsafe) atomic read with no side effects
unsafe { (*Gpio::<P>::ptr()).idr().read().id(N).is_low() }
}
}
impl<const P: char, const N: u8, MODE> Pin<P, N, Output<MODE>> {
/// Drives the pin high
#[inline(always)]
pub fn set_high(&mut self) {
self._set_high()
}
/// Drives the pin low
#[inline(always)]
pub fn set_low(&mut self) {
self._set_low()
}
/// Is the pin in drive high or low mode?
#[inline(always)]
pub fn get_state(&mut self) -> PinState {
if self.is_set_low() {
PinState::Low
} else {
PinState::High
}
}
/// Drives the pin high or low depending on the provided value
#[inline(always)]
pub fn set_state(&mut self, state: PinState) {
match state {
PinState::Low => self.set_low(),
PinState::High => self.set_high(),
}
}
/// Is the pin in drive high mode?
#[inline(always)]
pub fn is_set_high(&mut self) -> bool {
!self.is_set_low()
}
/// Is the pin in drive low mode?
#[inline(always)]
pub fn is_set_low(&mut self) -> bool {
self._is_set_low()
}
/// Toggle pin output
#[inline(always)]
pub fn toggle(&mut self) {
if self.is_set_low() {
self.set_high()
} else {
self.set_low()
}
}
}
impl<const P: char, const N: u8, MODE> Pin<P, N, MODE>
where
MODE: marker::Readable,
{
/// Is the input pin high?
#[inline(always)]
pub fn is_high(&mut self) -> bool {
!self.is_low()
}
/// Is the input pin low?
#[inline(always)]
pub fn is_low(&mut self) -> bool {
self._is_low()
}
}