py32f030_hal 0.1.0

Peripheral Hal Crate for Puya's PY32F030 microcontroller
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
//! General purpose input/output (GPIO) driver.
//!
//! Output
//!
//! ```rust, ignore
//! let p = hal::init(Default::default());
//! let gpioa = p.GPIOA.split();
//! let mut led = Output::new(gpioa.PA10, PinIoType::PullDown, PinSpeed::Low);
//! ```
//!
//! Input
//! ```rust, ignore
//! let p = hal::init(Default::default());
//! let gpioa = p.GPIOA.split();
//! let key = Input::new(gpioa.PA12, PinPullUpDown::PullUp, PinSpeed::Low);
//! ```
//!
//! AF
//! ```rust, ignore
//! let gpioa = p.GPIOA.split();
//! let _mco_pin = Af::new(
//!    gpioa.PA1,
//!    PinAF::AF15,
//!    PinSpeed::VeryHigh,
//!    PinIoType::PullUp,
//! );
//! Mco::select(clock::McoSelect::SysClk, clock::McoDIV::DIV1);
//! ```

pub(crate) mod hal;
mod types;

use hal::*;

pub use types::*;

use crate::clock::peripheral::{PeripheralClockIndex, PeripheralIdToClockIndex};
use embassy_hal_internal::{impl_peripheral, into_ref, Peripheral, PeripheralRef};

/// GPIO Port Index
#[allow(clippy::upper_case_acronyms)]
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum GpioPort {
    GPIOA = 0,
    GPIOB = 1,
    GPIOF = 2,
}

impl PeripheralIdToClockIndex for GpioPort {
    fn clock(&self) -> PeripheralClockIndex {
        match *self {
            GpioPort::GPIOA => PeripheralClockIndex::GPIOA,
            GpioPort::GPIOB => PeripheralClockIndex::GPIOB,
            GpioPort::GPIOF => PeripheralClockIndex::GPIOF,
        }
    }
}

impl From<usize> for GpioPort {
    fn from(value: usize) -> Self {
        match value {
            0 => GpioPort::GPIOA,
            1 => GpioPort::GPIOB,
            2 => GpioPort::GPIOF,
            _ => unreachable!(),
        }
    }
}

/// AnyPin
pub struct AnyPin {
    port_pin: u8,
}

impl_peripheral!(AnyPin);
impl sealed::Pin for AnyPin {
    fn port_pin(&self) -> u8 {
        self.port_pin
    }
}

impl Pin for AnyPin {
    fn degrade(self) -> AnyPin {
        self
    }
}

/// Flex 接口的 pin
/// 该结构体不对 api 调用做任何保护,例如引脚调用输出配置后,再读取,也许硬件不支持这种使用,但
/// 这个对象依旧不阻止调用,如果希望更强的保护,推荐使用 `Input,Output,Analog` 等对象,保证
/// 接口不被乱用
pub struct Flex<'d> {
    pub(crate) pin: PeripheralRef<'d, AnyPin>,
}

pub trait Pin: Peripheral<P = Self> + Into<AnyPin> + sealed::Pin + Sized + 'static {
    fn degrade(self) -> AnyPin {
        AnyPin {
            port_pin: self.port_pin(),
        }
    }
}

use sealed::Pin as _;

impl<'d> Flex<'d> {
    #[inline]
    pub fn new(pin: impl Peripheral<P = impl Pin> + 'd) -> Self {
        into_ref!(pin);
        Self {
            pin: pin.map_into(),
        }
    }

    pub fn port(&self) -> GpioPort {
        self.pin.port()
    }

    pub fn pin(&self) -> usize {
        self.pin.pin()
    }

    /// 重新设置引脚为输入模式
    pub fn set_as_input(&self, pull: PinPullUpDown, speed: PinSpeed) {
        critical_section::with(|_| {
            self.pin.set_mode(PinMode::Input);
            self.pin.set_push_pull(pull);
            self.pin.set_speed(speed);
        });
    }

    /// 重新设置引脚为输出模式
    pub fn set_as_output(&self, io_type: PinIoType, speed: PinSpeed) {
        critical_section::with(|_| {
            self.pin.set_mode(PinMode::Output);
            self.pin.set_io_type(io_type);
            self.pin.set_speed(speed);
        });
    }

    /// 设置引脚为模拟引脚
    pub fn set_as_analog(&self) {
        critical_section::with(|_| {
            self.pin.set_mode(PinMode::Analog);
        });
    }

    /// 设置引脚为外设功能模式
    pub fn set_as_af(&self, af: PinAF, speed: PinSpeed, io_type: PinIoType) {
        critical_section::with(|_| {
            self.pin.set_mode(PinMode::Af);
            self.pin.set_af(af);
            self.pin.set_speed(speed);
            self.pin.set_io_type(io_type);
        });
    }

    /// 设置引脚上下拉配置
    pub fn set_push_pull(&self, push_pull: PinPullUpDown) {
        self.pin.set_push_pull(push_pull);
    }

    /// 设置引脚的输出开漏配置
    pub fn set_open_drain(&self, open_drain: PinOutputType) {
        self.pin.set_output_type(open_drain);
    }

    /// 设置引脚的输入输出配置
    pub fn set_io_type(&self, io_type: PinIoType) {
        self.pin.set_io_type(io_type)
    }

    /// 引脚状态读取
    pub fn read(&self) -> PinLevel {
        self.pin.read()
    }

    /// 引脚状态写
    pub fn write(&self, level: PinLevel) {
        self.pin.write(level)
    }
}

impl AnyPin {
    /// # Safety
    #[inline]
    pub unsafe fn steal(port: GpioPort, pin: u8) -> Self {
        assert!(pin < 16);
        // safe
        let port_pin = ((port as u8) << 4) | pin;
        Self { port_pin }
    }
}

/// 输入类型的引脚
pub struct Input<'d> {
    pub(crate) pin: Flex<'d>,
}

/// 输出类型的引脚
pub struct Output<'d> {
    pub(crate) pin: Flex<'d>,
}

/// 功能复用类型的引脚
pub struct Af<'d> {
    pub(crate) _pin: Flex<'d>,
}

/// 模拟引脚
pub struct Analog<'d> {
    pub(crate) _pin: Flex<'d>,
}

impl<'d> Input<'d> {
    pub fn new(
        pin: impl Peripheral<P = impl Pin> + 'd,
        pull: PinPullUpDown,
        speed: PinSpeed,
    ) -> Self {
        let pin = Flex::new(pin);

        pin.set_as_input(pull, speed);

        Self { pin }
    }

    /// 读取引脚状态
    pub fn read(&self) -> PinLevel {
        self.pin.read()
    }
}

impl<'d> Output<'d> {
    /// 创建对象
    pub fn new(
        pin: impl Peripheral<P = impl Pin> + 'd,
        io_type: PinIoType,
        speed: PinSpeed,
    ) -> Self {
        let pin = Flex::new(pin);

        pin.set_as_output(io_type, speed);

        Self { pin }
    }

    /// 读取引脚电平
    pub fn read(&self) -> PinLevel {
        self.pin.read()
    }

    /// 设置引脚电平
    pub fn write(&self, level: PinLevel) {
        self.pin.write(level)
    }
}

impl<'d> Af<'d> {
    /// 新建 AF 引脚
    pub fn new(
        pin: impl Peripheral<P = impl Pin> + 'd,
        af: impl Into<PinAF>,
        speed: PinSpeed,
        io_type: PinIoType,
    ) -> Self {
        let pin = Flex::new(pin);

        pin.set_as_af(af.into(), speed, io_type);

        Self { _pin: pin }
    }
}

impl<'d> Analog<'d> {
    /// 新建模拟引脚
    pub fn new(pin: impl Peripheral<P = impl Pin> + 'd) -> Self {
        let pin = Flex::new(pin);

        pin.set_as_analog();

        Self { _pin: pin }
    }
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

pub mod v2 {
    use super::{Flex, Input, Output, PinLevel};
    use core::convert::Infallible;

    impl<'d> embedded_hal::digital::v2::InputPin for Input<'d> {
        type Error = Infallible;
        fn is_low(&self) -> Result<bool, Self::Error> {
            Ok(self.read() == PinLevel::Low)
        }

        fn is_high(&self) -> Result<bool, Self::Error> {
            Ok(self.read() == PinLevel::Hight)
        }
    }

    impl<'d> embedded_hal::digital::v2::OutputPin for Output<'d> {
        type Error = Infallible;

        fn set_low(&mut self) -> Result<(), Self::Error> {
            self.write(PinLevel::Low);
            Ok(())
        }

        fn set_high(&mut self) -> Result<(), Self::Error> {
            self.write(PinLevel::Hight);
            Ok(())
        }
    }

    impl<'d> embedded_hal::digital::v2::StatefulOutputPin for Output<'d> {
        fn is_set_low(&self) -> Result<bool, Self::Error> {
            Ok(self.read() == PinLevel::Low)
        }

        fn is_set_high(&self) -> Result<bool, Self::Error> {
            Ok(self.read() == PinLevel::Hight)
        }
    }

    impl<'d> embedded_hal::digital::v2::ToggleableOutputPin for Output<'d> {
        type Error = Infallible;
        fn toggle(&mut self) -> Result<(), Self::Error> {
            self.write(!(self.read()));
            Ok(())
        }
    }

    impl<'d> embedded_hal::digital::v2::InputPin for Flex<'d> {
        type Error = Infallible;
        fn is_low(&self) -> Result<bool, Self::Error> {
            Ok(self.read() == PinLevel::Low)
        }

        fn is_high(&self) -> Result<bool, Self::Error> {
            Ok(self.read() == PinLevel::Hight)
        }
    }

    impl<'d> embedded_hal::digital::v2::OutputPin for Flex<'d> {
        type Error = Infallible;

        fn set_low(&mut self) -> Result<(), Self::Error> {
            self.write(PinLevel::Low);
            Ok(())
        }

        fn set_high(&mut self) -> Result<(), Self::Error> {
            self.write(PinLevel::Hight);
            Ok(())
        }
    }

    impl<'d> embedded_hal::digital::v2::StatefulOutputPin for Flex<'d> {
        fn is_set_low(&self) -> Result<bool, Self::Error> {
            Ok(self.read() == PinLevel::Low)
        }

        fn is_set_high(&self) -> Result<bool, Self::Error> {
            Ok(self.read() == PinLevel::Hight)
        }
    }

    impl<'d> embedded_hal::digital::v2::ToggleableOutputPin for Flex<'d> {
        type Error = Infallible;
        fn toggle(&mut self) -> Result<(), Self::Error> {
            self.write(!(self.read()));
            Ok(())
        }
    }
}

/// 所有 gpio 引脚定义和功能复用定义的宏
macro_rules! gpio_pin_def {
    (
        $gpio_mod: ident, $gpio_port: ident
            $(
                $port_pin_name:ident => $pin_name:ident : $pin_index: expr,
                    [
                        $($Af: ident = $number: expr),*
                    ]
            ),*
    ) => {
        pub mod $gpio_mod {
            use super::*;
            use crate::mcu::peripherals;

            // pub struct $gpio_port;

            pub struct Port {
                pin: u8,
            }

            impl Port {
                pub fn erase_port(self) -> AnyPin {
                    unsafe { AnyPin::steal(GpioPort::$gpio_port, self.pin) }
                }
            }

            pub struct Pins {
                $(
                    pub $port_pin_name: $port_pin_name,
                )*
            }

            impl peripherals::$gpio_port {
                pub fn split(self) -> Pins {
                    // 开启电平
                    self.enable();
                    Pins {
                        $(
                            $port_pin_name: $port_pin_name,
                        )*
                    }
                }

                pub fn enable(&self) {
                    PeripheralClockIndex::$gpio_port.open();
                }

                pub fn reset(&self) {
                    PeripheralClockIndex::$gpio_port.reset();
                }
            }

            $(
                pub struct $port_pin_name;

                impl_peripheral!($port_pin_name);

                impl $port_pin_name {
                    pub fn erase_pin(self) -> Port {
                        Port {
                            pin: $pin_index,
                        }
                    }
                }

                #[allow(clippy::upper_case_acronyms)]
                pub enum $pin_name{
                    $(
                        $Af = $number,
                    )*
                }

                impl From<$pin_name> for PinAF{
                    fn from(value: $pin_name) -> Self {
                        (value as u32).into()
                    }
                }

                impl sealed::Pin for $port_pin_name {
                    fn port_pin(&self) -> u8 {
                        (GpioPort::$gpio_port as u8) << 4 | $pin_index
                    }
                }

                impl Pin for $port_pin_name {
                    fn degrade(self) -> AnyPin {
                        AnyPin {
                            port_pin: (GpioPort::$gpio_port as u8) << 4 | $pin_index
                        }
                    }
                }

                impl From<$port_pin_name> for AnyPin {
                    fn from(value: $port_pin_name) -> Self {
                        // unsafe { AnyPin::steal(GpioPort::$gpio_port, $pin_index) }
                        value.degrade()
                    }
                }
            )*
        }
    };
}

// GPIOA
gpio_pin_def!(gpioa, GPIOA
    PA0 => A0:0,
    [
        SPI2_SCK = 0, USART1_CTS = 1, LED_DATA_B = 3,USART2_CTS = 4,COMP1_OUT = 7,USART2_TX = 9,
        SPI1_MISO = 10,TIM1_CH3 = 13,TIM1_CH1N = 14,IR_OUT = 15
    ],
    PA1 => A1:1,
    [
        SPI1_SCK = 0, USART1_RTS = 1, LED_DATA_C = 3,USART2_RTS = 4, EVENTOUT = 7,USART2_RX = 9,
        SPI1_MOSI = 10,TIM1_CH4 = 13,TIM1_CH2N = 14, MCO = 15
    ],
    PA2 => A2:2,
    [
        SPI1_MOSI = 0, USART1_TX = 1, LED_DATA_D = 3,USART2_TX = 4, COMP2_OUT = 7,SPI1_SCK = 10,
        I2C_SDA = 12, TIM3_CH1 = 13
    ],
    PA3 => A3:3,
    [
        SPI1_MOSI = 0, USART1_TX = 1, LED_DATA_D = 3,USART2_TX = 4, COMP2_OUT = 7,SPI1_SCK = 10,
        I2C_SDA = 12,TIM3_CH1 = 13
    ],
    PA4 => A4:4,
    [
        SPI1_NSS = 0, USART1_CK = 1, SPI2_MOSI = 2,LED_DATA_F = 3,TIM14_CH1 = 4, USART2_CK = 5,
        EVENTOUT = 7,USART2_TX = 9, TIM3_CH3 = 12,RTC_OUT = 13
    ],
    PA5 => A5:5,
    [
        SPI1_SCK = 0, LED_DATA_G = 3, LPTIM1_ETR = 5,EVENTOUT = 7,USART2_RX = 9, TIM3_CH2 = 13,
        MCO = 15
    ],
    PA6 => A6:6,
    [
        SPI1_MISO = 0, TIM3_CH1 = 1, TIM1_BKIN = 2,LED_DATA_DP = 3,TIM16_CH1 = 5, COMP1_OUT = 7,
        USART1_CK = 9, RTC_OUT = 15
    ],
    PA7 => A7:7,
    [
        SPI1_MOSI = 0, TIM3_CH2 = 1, TIM1_CH1N = 2,TIM14_CH1 = 4,TIM17_CH1 = 5, EVENTOUT = 6,
        COMP2_OUT = 7, USART1_TX = 8, USART2_TX = 9, SPI1_MISO = 10, I2C_SDA = 12
    ],
    PA8 => A8:8,
    [
        SPI2_NSS = 0, USART1_CK = 1, TIM1_CH1 = 2,USART2_CK = 4,MCO = 5, EVENTOUT = 7,
        USART1_RX = 8, USART2_RX = 9, SPI1_MOSI = 10, I2C_SCL = 12
    ],
    PA9 => A9:9,
    [
        SPI2_MISO = 0, USART1_TX = 1, TIM1_CH2 = 2,USART2_TX = 4,MCO = 5, I2C_SCL = 6,
        EVENTOUT = 7, USART1_RX = 8, SPI1_SCK = 10, I2C_SDA = 12, TIM1_BKIN = 13
    ],
    PA10 => A10:10,
    [
        SPI2_MOSI = 0, USART1_RX = 1, TIM1_CH3 = 2, USART2_RX = 4, TIM17_BKIN = 5, I2C_SDA = 6,
        EVENTOUT = 7, USART1_TX = 8, SPI1_NSS = 10, I2C_SCL = 12
    ],
    PA11 => A11:11,
    [
        SPI1_MISO = 0, USART1_CTS = 1, TIM1_CH4 = 2, USART2_CTS = 4, EVENTOUT = 5, I2C_SCL = 6,
        COMP1_OUT = 7
    ],
    PA12 => A12:12,
    [
        SPI1_MOSI = 0, USART1_RTS = 1, TIM1_ETR = 2, USART2_RTS = 4, EVENTOUT = 5, I2C_SDA = 6,
        COMP2_OUT = 7
    ],
    PA13 => A13:13,
    [
        SWDIO = 0, IR_OUT = 1, COMP2_OUT = 7, USART1_RX = 8, SPI1_MISO = 10, TIM1_CH2 = 13,
        MCO = 15
    ],
    PA14 => A14:14,
    [
        SWCLK = 0, USART1_TX = 1, USART2_TX = 4, EVENTOUT = 7, MCO = 15
    ],
    PA15 => A15:15,
    [
        SPI1_NSS = 0, USART1_RX = 1, USART2_RX = 4, EVENTOUT = 7, LED_COM0 = 6
    ]
);

// GPIOB
gpio_pin_def!(gpiob, GPIOB
    PB0 => B0:0,[SPI1_NSS = 0, TIM3_CH3 = 1, TIM1_CH2N = 2, EVENTOUT = 5,COMP1_OUT = 7],
    PB1 => B1:1,[TIM14_CH1 = 0, TIM3_CH4 = 1, TIM1_CH3N = 2, EVENTOUT = 7],
    PB2 => B2:2,[USART1_RX = 0, SPI2_SCK = 1, USART2_RX = 3],
    PB3 => B3:3,[SPI1_SCK = 0, TIM1_CH2 = 1, USART1_RTS = 3, USART2_RTS = 4,LED_COM1 = 6, EVENTOUT = 7],
    PB4 => B4:4,[SPI1_MISO = 0, TIM3_CH1 = 1, USART1_CTS = 3, USART2_CTS = 4,TIM17_BKIN = 5, LED_COM2 = 6,  EVENTOUT = 7],
    PB5 => B5:5,[SPI1_MOSI = 0, TIM3_CH2 = 1, TIM16_BKIN = 2, USART1_CK = 3,USART2_CK = 4, LPTIM_IN1 = 5,  LED_COM3 = 6, COMP1_OUT = 7],
    PB6 => B6:6,[USART1_TX = 0, TIM1_CH3 = 1, TIM16_CH1N = 2, SPI2_MISO = 3,USART2_TX = 4, LPTIM_ETR = 5,  I2C_SCL = 6, EVENTOUT = 7],
    PB7 => B7:7,[USART1_RX = 0, SPI2_MOSI = 1, TIM17_CH1N = 2, USART2_RX = 4,  I2C_SDA = 6, EVENTOUT = 7],
    PB8 => B8:8,[SPI2_SCK = 1, TIM16_CH1 = 2, LED_DATA_A = 3, USART2_TX = 4,  I2C_SCL = 6,  EVENTOUT = 7, USART1_TX = 8,
        SPI2_NSS = 11, I2C_SDA = 12, TIM17_CH1 = 13,  IR_OUT = 15]
);

// GPIOF
gpio_pin_def!(gpiof, GPIOF
    PF0_OSC_IN => F0:0,[TIM14_CH1 = 2, SPI2_SCK = 3, USART2_RX = 4, USART1_RX = 8, USART2_TX = 9, I2C_SDA = 12],
    PF1_OSC_OUT => F1:1,[SPI2_MISO = 3, USART2_TX = 4, USART1_TX = 8, USART2_RX = 9, SPI1_NSS = 10, I2C_SCL = 12, TIM14_CH1 = 13],
    PF2_NRST => F2:2,[SPI2_MOSI = 0, USART2_RX = 1, MCO = 3],
    PF3 => F3:3,[USART1_TX = 0, SPI2_MISO = 3, USART2_TX = 4],
    PF4_BOOT0 => F4:4,[]
);