gd32-hal 0.0.1

Hardware abstraction layer for the GD32 MCUs
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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
//! This module provides functionality for General Purpose Input and Output (GPIO) pins,
//! including all GPIOx register functions. It also configures GPIO interrupts using SYSCFG and EXTI
//! registers as appropriate.

// todo: WL is missing port C here due to some pins being missing, and this being tough
// todo to change with our current model. Note sure if PAC, or MCU limitation
// todo: WL is also missing interrupt support.

#[cfg(feature = "embedded-hal")]
use core::convert::Infallible;

use cortex_m::interrupt::free;

use crate::{
    pac::{self, RCU},
    rcu_en_reset, // todo?
};

#[cfg(feature = "embedded-hal")]
use embedded_hal::digital::v2::{InputPin, OutputPin, ToggleableOutputPin};

use cfg_if::cfg_if;
use paste::paste;

#[derive(Copy, Clone)]
#[repr(u8)]
/// Values for `GPIOx_MODER`
pub enum PinMode {
    Input,
    Output,
    Alt(u8),
    Analog,
}

impl PinMode {
    /// We use this function to find the value bits due to being unable to repr(u8) with
    /// the wrapped `AltFn` value.
    fn val(&self) -> u8 {
        match self {
            Self::Input => 0b01_00,  // default to float input
            Self::Output => 0b00_11, // default to PushPull 50MHz
            Self::Alt(_) => 0b10_11, // default to PushPull 50MHz
            Self::Analog => 0b00_00,
        }
    }
}

#[derive(Copy, Clone)]
#[repr(u8)]
/// Values for `GPIOx_OTYPER`
pub enum OutputType {
    PushPull = 0,
    OpenDrain = 1,
}

#[derive(Copy, Clone)]
#[repr(u8)]
/// Values for `GPIOx_OSPEEDR`. This configures I/O output speed. See the user manual
/// for your MCU for what speeds these are. Note that Fast speed (0b10) is not
/// available on all STM32 families.
pub enum OutputSpeed {
    Low = 0b00,
    Medium = 0b01,
    High = 0b11, 
    VeryHigh = 0b111,
}

#[derive(Copy, Clone)]
#[repr(u8)]
/// Values for `GPIOx_PUPDR`
pub enum Pull {
    Floating = 0b00,
    Up = 0b01,
    Dn = 0b10,
}

#[derive(Copy, Clone)]
#[repr(u8)]
/// Values for `GPIOx_ISTAT` and `GPIOx_ODR`.
pub enum PinState {
    High = 1,
    Low = 0,
}

#[derive(Copy, Clone)]
#[repr(u8)]
/// Values for `GPIOx_LCKR`.
pub enum CfgLock {
    NotLocked = 0,
    Locked = 1,
}

#[derive(Copy, Clone)]
#[repr(u8)]
/// Values for `GPIOx_BRR`.
pub enum ResetState {
    NoAction = 0,
    Reset = 1,
}

// todo: If you get rid of Port struct, rename this enum Port
#[derive(Copy, Clone)]
/// GPIO port letter
pub enum Port {
    A,
    B,
    C,
    D,
    E,
    F,
    G,
}

impl Port {
    /// See F303 RM section 12.1.3: each reg has an associated value
    fn cr_val(&self) -> u8 {
        match self {
            Self::A => 0,
            Self::B => 1,
            Self::C => 2,
            Self::D => 3,
            Self::E => 4,
            Self::F => 5,
            Self::G => 6,
        }
    }
}

#[derive(Copy, Clone, Debug)]
/// The pulse edge used to trigger interrupts.
pub enum Edge {
    Rising,
    Falling,
    Edge,
}

// These macros are used to interate over pin number, for use with PAC fields.
macro_rules! set_field {
    ($regs: expr, $pin:expr, $reg:ident, $field_bit_val:tt, [$($num:expr),+]) => {
        paste! {
            unsafe {
                match $pin {
                    $(
                        $num => (*$regs).$reg.modify(|_, w| set_field!(@expand_fields_op w, $field_bit_val, $num)),
                    )+
                    _ => panic!("GPIO pins must be 0 - 15."),
                }
            }
        }
    };

    (@expand_fields_op $w:ident, [$(($field:ident, $bit:ident, $val:expr)),+] , $num:expr) => {
        paste!{
            $w.$([<$field $num>]().$bit($val)).+
        }
    };
}


macro_rules! set_mode_and_ctl_field {
    ($regs: expr, $pin:expr, $val:expr, [$(($num:expr, $regid: expr)),+]) => {
        paste! {
            unsafe {
                match $pin {
                    $(
                        $num => {
                            (*$regs).[<ctl $regid>].modify(|_, w| w.[<md $num>]().bits($val));
                            (*$regs).[<ctl $regid>].modify(|_, w| w.[<ctl $num>]().bits($val >> 2));
                        },
                    )+
                    _ => panic!("GPIO pins must be 0 - 15."),
                }
            }
        }
    }
}

macro_rules! set_mode_or_ctl_field {
    ($regs: expr, $pin:expr, $field:expr, $val:expr, [$(($num:expr, $regid: expr)),+]) => {
        paste! {
            unsafe {
                match $pin {
                    $(
                        $num => {
                            (*$regs).[<ctl $regid>].modify(|_, w| w.[<$field $num>]().bits($val));
                        },
                    )+
                    _ => panic!("GPIO pins must be 0 - 15."),
                }
            }
        }
    }
}

macro_rules! set_alt {
    ($regs: expr, $pin:expr, $field_af:ident, $val:expr, [$(($num:expr, $zo:ident)),+]) => {
        paste! {
            unsafe {
                match $pin {
                    $(
                        $num => {
                            (*$regs).[<ctl $zo>].modify(|_, w| w.[<moder $num>]().bits(PinMode::Alt(0).val()));
                        }
                    )+
                    _ => panic!("GPIO pins must be 0 - 15."),
                }
            }
        }
    }
}

macro_rules! get_input_data {
    ($regs: expr, $pin:expr, [$($num:expr),+]) => {
        paste! {
            unsafe {
                match $pin {
                    $(
                        $num => (*$regs).istat.read().[<istat $num>]().bit_is_set(),
                    )+
                    _ => panic!("GPIO pins must be 0 - 15."),
                }
            }
        }
    }
}

macro_rules! set_state {
    ($regs: expr, $pin:expr, $offset: expr, [$($num:expr),+]) => {
        paste! {
            unsafe {
                match $pin {
                    $(
                        $num => (*$regs).bop.write(|w| w.bits(1 << ($offset + $num))),
                    )+
                    _ => panic!("GPIO pins must be 0 - 15."),
                }
            }
        }
    }
}

// todo: Consolidate these exti macros

// Reduce DRY for setting up interrupts.
macro_rules! set_exti {
    ($pin:expr, $trigger:expr, $val:expr, [$(($num:expr, $crnum:expr)),+]) => {
        let exti = unsafe { &(*pac::EXTI::ptr()) };
        let afio = unsafe { &(*pac::AFIO::ptr() )};
        paste! {
            match $pin {
                $(
                    $num => {
                    // todo: Core 2 interrupts for wb. (?)
                        cfg_if! {
                            if #[cfg(feature = "f3")] {
                                exti.inten.modify(|_, w| w.[<inten $num>]().set_bit());
                            }
                        }

                        cfg_if! {
                            if #[cfg(feature = "f3")] {
                                exti.rten.modify(|_, w| w.[<rten $num>]().bit($trigger.0));
                                exti.ften.modify(|_, w| w.[<ften $num>]().bit(!$trigger.1));
                            }
                        }
                        afio
                            .[<extiss $crnum>]
                            .modify(|_, w| unsafe { w.[<exti $num _ss>]().bits($val) });
                    }
                )+
                _ => panic!("GPIO pins must be 0 - 15."),
            }
        }
    }
}


/// Represents a single GPIO pin. Allows configuration, and reading/setting state.
pub struct Pin {
    /// The GPIO Port letter. Eg A, B, C.
    pub port: Port,
    /// The pin number: 1 - 15.
    pub pin: u8,
}

impl Pin {
    /// Internal function to get the appropriate GPIO block pointer.
    const fn regs(&self) -> *const pac::gpioa::RegisterBlock {
        // Note that we use this `const` fn and pointer casting since not all ports actually
        // deref to GPIOA in PAC.
        regs(self.port)
    }

    /// Create a new pin, with a specific mode. Enables the RCU peripheral clock to the port,
    /// if not already enabled. Example: `let pa1 = Pin::new(Port::A, 1);`
    pub fn new(port: Port, pin: u8, mode: PinMode) -> Self {
        assert!(pin <= 15, "Pin must be 0 - 15.");

        free(|_| {
            let rcu = unsafe { &(*RCU::ptr()) };

            match port {
                Port::A => {
                    cfg_if! {
                        if #[cfg(feature = "f3")] {
                            if rcu.apb2en.read().paen().bit_is_clear() {
                                rcu_en_reset!(apb2, pa, rcu);
                            }
                        }
                    }
                }
                Port::B => {
                    cfg_if! {
                        if #[cfg(feature = "f3")] {
                            if rcu.apb2en.read().pben().bit_is_clear() {
                                rcu_en_reset!(apb2, pb, rcu);
                            }
                        }
                    }
                }
                #[cfg(not(feature = "wl"))]
                Port::C => {
                    cfg_if! {
                        if #[cfg(feature = "f3")] {
                            if rcu.apb2en.read().pcen().bit_is_clear() {
                                rcu_en_reset!(apb2, pc, rcu);
                            }
                        }
                    }
                }
                #[cfg(not(any(feature = "f410", feature = "wl")))]
                Port::D => {
                    cfg_if! {
                        if #[cfg(feature = "f3")] {
                            if rcu.apb2en.read().pden().bit_is_clear() {
                                rcu_en_reset!(apb2, pd, rcu);
                            }
                        }
                    }
                }
                Port::E => {
                    cfg_if! {
                        if #[cfg(feature = "f3")] {
                            if rcu.apb2en.read().peen().bit_is_clear() {
                                rcu_en_reset!(apb2, pe, rcu);
                            }
                        }
                    }
                }
                Port::F => {
                    cfg_if! {
                        if #[cfg(feature = "f3")] {
                            if rcu.apb2en.read().pfen().bit_is_clear() {
                                rcu_en_reset!(apb2, pf, rcu);
                            }
                        }
                    }
                }
                Port::G => {
                    cfg_if! {
                        if #[cfg(feature = "f3")] {
                            if rcu.apb2en.read().pgen().bit_is_clear() {
                                rcu_en_reset!(apb2, pg, rcu);
                            }
                        }
                    }
                }
            }
        });

        let mut result = Self { port, pin };
        result.mode(mode);

        result
    }

    /// Set pin mode. Eg, Output, Input, Analog, or Alt. Sets the `MODER` register.
    pub fn mode(&mut self, value: PinMode) {
        set_mode_and_ctl_field!(
            self.regs(),
            self.pin,
            value.val(),
            [(0,0), (1,0), (2,0), (3,0), (4,0), (5,0), (6,0), (7,0), (8,1), (9,1), (10,1), (11,1), (12,1), (13,1), (14,1), (15,1)]
        );

        #[cfg(any(feature = "f310", feature = "f350", feature = "f370"))]
        {
            if let PinMode::Alt(alt) = value {
                self.alt_fn(alt);
            }
        }
    }

    /// Set output type. Sets the `OTYPER` register.
    pub fn output_type(&mut self, value: OutputType, mode:PinMode) {
        let value = match (value, mode) {
            (OutputType::PushPull, PinMode::Output) => 00,
            (OutputType::OpenDrain, PinMode::Output) => 01,
            (OutputType::PushPull, PinMode::Alt(_)) => 10,
            (OutputType::OpenDrain, PinMode::Alt(_)) => 11,
            _ => {panic!("not supported")},
        };
        set_mode_or_ctl_field!(
            self.regs(),
            self.pin,
            ctl,
            value as u8,
            [(0,0), (1,0), (2,0), (3,0), (4,0), (5,0), (6,0), (7,0), (8,1), (9,1), (10,1), (11,1), (12,1), (13,1), (14,1), (15,1)]
        );
    }

    /// Set output speed to Low, Medium, or High. Sets the `OSPEEDR` register.
    pub fn output_speed(&mut self, value: OutputSpeed) {

        let spd_val = if let OutputSpeed::VeryHigh = value {
            true
        } else {
            false
        };

        let value = match value {
            OutputSpeed::Low => 0b10,
            OutputSpeed::Medium => 0b01,
            OutputSpeed::High => 0b11,
            OutputSpeed::VeryHigh => 0b11,
        };

        set_mode_or_ctl_field!(
            self.regs(),
            self.pin,
            md,
            value as u8,
            [(0,0), (1,0), (2,0), (3,0), (4,0), (5,0), (6,0), (7,0), (8,1), (9,1), (10,1), (11,1), (12,1), (13,1), (14,1), (15,1)]
        );

        set_field!(
            self.regs(),
            self.pin,
            spd,
            [(spd,bit,spd_val)],
            [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
        );

        
    }

    /// Set internal pull resistor: Pull up, pull down, or floating. Sets the `PUPDR` register.
    pub fn pull(&mut self, value: Pull) {
        if let Pull::Floating = value {
            set_mode_or_ctl_field!(
                self.regs(),
                self.pin,
                ctl,
                0b01,
                [(0,0), (1,0), (2,0), (3,0), (4,0), (5,0), (6,0), (7,0), (8,1), (9,1), (10,1), (11,1), (12,1), (13,1), (14,1), (15,1)]
            );
        } else {
            let v = if let Pull::Up = value {
                true
            } else {
                false
            };

            set_mode_or_ctl_field!(
                self.regs(),
                self.pin,
                ctl,
                0b10,
                [(0,0), (1,0), (2,0), (3,0), (4,0), (5,0), (6,0), (7,0), (8,1), (9,1), (10,1), (11,1), (12,1), (13,1), (14,1), (15,1)]
            );

            set_field!(
                self.regs(),
                self.pin,
                octl,
                [(octl,bit,v)],
                [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
            );
        }
        
    }

    /// Lock or unlock a port configuration. Sets the `LCKR` register.
    pub fn cfg_lock(&mut self, value: CfgLock) {
        set_field!(
            self.regs(),
            self.pin,
            lock,
            [(lk,
            bit,
            value as u8 != 0)],
            [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
        );
    }

    /// Read the input data register. Eg determine if the pin is high or low. See also `is_high()`
    /// and `is_low()`. Reads from the `ISTAT` register.
    pub fn get_state(&mut self) -> PinState {
        let val = get_input_data!(
            self.regs(),
            self.pin,
            [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
        );
        if val {
            PinState::High
        } else {
            PinState::Low
        }
    }

    /// Set a pin state (ie set high or low output voltage level). See also `set_high()` and
    /// `set_low()`. Sets the `BSRR` register. Atomic.
    pub fn set_state(&mut self, value: PinState) {
        let offset = match value {
            PinState::Low => 16,
            PinState::High => 0,
        };

        set_state!(
            self.regs(),
            self.pin,
            offset,
            [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
        );
    }

    /// Set up a pin's alternate function. We set this up initially using `mode()`.
    /// TODO and Note: GD32F30x and GD32F3x0 has different ways to handle alternative functions, GD32F3x0 use AF Registers and each pin has a 4-bit config field, while GD32F30x use remap
    /// So, this function is keep here for GD32F3x0 series and similar devices.
    #[cfg(any(feature = "f310", feature = "f350", feature = "f370"))]
    fn alt_fn(&mut self, value: u8) {
        assert!(value <= 15, "Alt function must be 0 to 15.");

        cfg_if! {
            if #[cfg(any(feature = "f3"))] {
                // TODO
            }
        }
    }

    #[cfg(any(feature = "f303"))]
    /// Configure this pin as an interrupt source. Set the edge as Rising or Falling.
    pub fn enable_interrupt(&mut self, edge: Edge) {
        let rise_trigger = match edge {
            Edge::Rising => {
                // configure EXTI line to trigger on rising edge, disable trigger on falling edge.
                (true,false)
            }
            Edge::Falling => {
                // configure EXTI line to trigger on falling edge, disable trigger on rising edge.
                (false, true)
            }
            Edge::Edge => {
                (true, true)
            }
        };

        cfg_if! {
            if #[cfg(feature = "f3")] {
                set_exti!(self.pin, rise_trigger, self.port.cr_val(), [(0, 0), (1, 0), (2, 0),
                    (3, 0), (4, 1), (5, 1), (6, 1), (7, 1), (8, 2), (9, 2), (10, 2), (11, 2), (12, 3),
                    (13, 3), (14, 3), (15, 3)]
                );
            }
        }
    }

    /// Check if the pin's input voltage is high. Reads from the `ISTAT` register.
    pub fn is_high(&self) -> bool {
        get_input_data!(
            self.regs(),
            self.pin,
            [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
        )
    }

    /// Check if the pin's input voltage is low. Reads from the `ISTAT` register.
    pub fn is_low(&self) -> bool {
        !self.is_high()
    }

    /// Set the pin's output voltage to high. Sets the `BSRR` register. Atomic.
    pub fn set_high(&mut self) {
        self.set_state(PinState::High);
    }

    /// Set the pin's output voltage to low. Sets the `BSRR` register. Atomic.
    pub fn set_low(&mut self) {
        self.set_state(PinState::Low);
    }
}
//
#[cfg(feature = "embedded-hal")]
// #[cfg_attr(docsrs, doc(cfg(feature = "embedded-hal")))]
impl InputPin for Pin {
    type Error = Infallible;

    fn is_high(&self) -> Result<bool, Self::Error> {
        Ok(Pin::is_high(self))
    }

    fn is_low(&self) -> Result<bool, Self::Error> {
        Ok(Pin::is_low(self))
    }
}

#[cfg(feature = "embedded-hal")]
// #[cfg_attr(docsrs, doc(cfg(feature = "embedded-hal")))]
impl OutputPin for Pin {
    type Error = Infallible;

    fn set_low(&mut self) -> Result<(), Self::Error> {
        Pin::set_low(self);
        Ok(())
    }

    fn set_high(&mut self) -> Result<(), Self::Error> {
        Pin::set_high(self);
        Ok(())
    }
}

#[cfg(feature = "embedded-hal")]
// #[cfg_attr(docsrs, doc(cfg(feature = "embedded-hal")))]
impl ToggleableOutputPin for Pin {
    type Error = Infallible;

    fn toggle(&mut self) -> Result<(), Self::Error> {
        if self.is_high() {
            Pin::set_low(self);
        } else {
            Pin::set_high(self);
        }
        Ok(())
    }
}

/// Check if a pin's input voltage is high. Reads from the `ISTAT` register.
/// Does not require a `Pin` struct.
pub fn is_high(port: Port, pin: u8) -> bool {
    get_input_data!(
        regs(port),
        pin,
        [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
    )
}

/// Check if a pin's input voltage is low. Reads from the `ISTAT` register.
/// Does not require a `Pin` struct.
pub fn is_low(port: Port, pin: u8) -> bool {
    !is_high(port, pin)
}

/// Set a pin's output voltage to high. Sets the `BSRR` register. Atomic.
/// Does not require a `Pin` struct.
pub fn set_high(port: Port, pin: u8) {
    set_state(port, pin, PinState::High);
}

/// Set a pin's output voltage to low. Sets the `BSRR` register. Atomic.
/// Does not require a `Pin` struct.
pub fn set_low(port: Port, pin: u8) {
    set_state(port, pin, PinState::Low);
}

/// Set a pin state (ie set high or low output voltage level). See also `set_high()` and
/// `set_low()`. Sets the `BSRR` register. Atomic.
/// Does not require a `Pin` struct.
fn set_state(port: Port, pin: u8, value: PinState) {
    let offset = match value {
        PinState::Low => 16,
        PinState::High => 0,
    };

    set_state!(
        regs(port),
        pin,
        offset,
        [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
    );
}

const fn regs(port: Port) -> *const pac::gpioa::RegisterBlock {
    // Note that we use this `const` fn and pointer casting since not all ports actually
    // deref to GPIOA in PAC.
    match port {
        Port::A => crate::pac::GPIOA::ptr(),
        Port::B => crate::pac::GPIOB::ptr() as _,
        Port::C => crate::pac::GPIOC::ptr() as _,
        Port::D => crate::pac::GPIOD::ptr() as _,
        Port::E => crate::pac::GPIOE::ptr() as _,
        Port::F => crate::pac::GPIOF::ptr() as _,
        Port::G => crate::pac::GPIOG::ptr() as _,
    }
}