-
Notifications
You must be signed in to change notification settings - Fork 409
Expand file tree
/
Copy pathgpio.rs
More file actions
1654 lines (1409 loc) · 47.9 KB
/
gpio.rs
File metadata and controls
1654 lines (1409 loc) · 47.9 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
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
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! General Purpose I/Os
//!
//! To get access to the pins, you first need to convert them into a HAL
//! designed struct from the pac struct `GPIO` and `IO_MUX` using `IO::new`.
//!
//! ```no_run
//! let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);
//! let mut led = io.pins.gpio5.into_push_pull_output();
//! ```
use core::marker::PhantomData;
#[doc(hidden)]
#[cfg_attr(esp32, path = "gpio/esp32.rs")]
#[cfg_attr(esp32c2, path = "gpio/esp32c2.rs")]
#[cfg_attr(esp32c3, path = "gpio/esp32c3.rs")]
#[cfg_attr(esp32s2, path = "gpio/esp32s2.rs")]
#[cfg_attr(esp32s3, path = "gpio/esp32s3.rs")]
pub mod types;
use core::convert::Infallible;
pub use crate::types::*;
use crate::{
pac::{GPIO, IO_MUX},
types::{
get_io_mux_reg,
gpio_intr_enable,
OutputSignalType,
GPIO_FUNCTION,
INPUT_SIGNAL_MAX,
OUTPUT_SIGNAL_MAX,
},
};
#[derive(Copy, Clone)]
pub enum Event {
RisingEdge = 1,
FallingEdge = 2,
AnyEdge = 3,
LowLevel = 4,
HighLevel = 5,
}
pub struct Unknown {}
pub struct Input<MODE> {
_mode: PhantomData<MODE>,
}
pub struct RTCInput<MODE> {
_mode: PhantomData<MODE>,
}
pub struct Floating;
pub struct PullDown;
pub struct PullUp;
pub struct Output<MODE> {
_mode: PhantomData<MODE>,
}
pub struct RTCOutput<MODE> {
_mode: PhantomData<MODE>,
}
pub struct OpenDrain;
pub struct PushPull;
pub struct Analog;
pub struct Alternate<MODE> {
_mode: PhantomData<MODE>,
}
#[doc(hidden)]
pub struct AF0;
#[doc(hidden)]
pub struct AF1;
#[doc(hidden)]
pub struct AF2;
pub enum DriveStrength {
I5mA = 0,
I10mA = 1,
I20mA = 2,
I40mA = 3,
}
#[derive(PartialEq)]
pub enum AlternateFunction {
Function0 = 0,
Function1 = 1,
Function2 = 2,
Function3 = 3,
Function4 = 4,
Function5 = 5,
}
pub trait RTCPin {}
pub trait AnalogPin {}
pub trait Pin {
fn number(&self) -> u8;
fn sleep_mode(&mut self, on: bool) -> &mut Self;
fn set_alternate_function(&mut self, alternate: AlternateFunction) -> &mut Self;
fn listen(&mut self, event: Event) {
self.listen_with_options(event, true, false, false)
}
fn is_listening(&self) -> bool;
fn listen_with_options(
&mut self,
event: Event,
int_enable: bool,
nmi_enable: bool,
wake_up_from_light_sleep: bool,
);
fn unlisten(&mut self);
fn clear_interrupt(&mut self);
fn is_pcore_interrupt_set(&self) -> bool;
fn is_pcore_non_maskable_interrupt_set(&self) -> bool;
fn is_acore_interrupt_set(&self) -> bool;
fn is_acore_non_maskable_interrupt_set(&self) -> bool;
fn enable_hold(&mut self, on: bool);
}
pub trait InputPin: Pin {
fn set_to_input(&mut self) -> &mut Self;
fn enable_input(&mut self, on: bool) -> &mut Self;
fn enable_input_in_sleep_mode(&mut self, on: bool) -> &mut Self;
fn is_input_high(&self) -> bool;
fn connect_input_to_peripheral(&mut self, signal: InputSignal) -> &mut Self {
self.connect_input_to_peripheral_with_options(signal, false, false)
}
fn connect_input_to_peripheral_with_options(
&mut self,
signal: InputSignal,
invert: bool,
force_via_gpio_mux: bool,
) -> &mut Self;
/// Remove a connected `signal` from this input pin.
///
/// Clears the entry in the GPIO matrix / IO mux that associates this input
/// pin with the given [input `signal`](`InputSignal`). Any other
/// connected signals remain intact.
fn disconnect_input_from_peripheral(&mut self, signal: InputSignal) -> &mut Self;
}
pub trait OutputPin: Pin {
fn set_to_open_drain_output(&mut self) -> &mut Self;
fn set_to_push_pull_output(&mut self) -> &mut Self;
fn enable_output(&mut self, on: bool) -> &mut Self;
fn set_output_high(&mut self, on: bool) -> &mut Self;
fn set_drive_strength(&mut self, strength: DriveStrength) -> &mut Self;
fn enable_open_drain(&mut self, on: bool) -> &mut Self;
fn enable_output_in_sleep_mode(&mut self, on: bool) -> &mut Self;
fn internal_pull_up_in_sleep_mode(&mut self, on: bool) -> &mut Self;
fn internal_pull_down_in_sleep_mode(&mut self, on: bool) -> &mut Self;
fn connect_peripheral_to_output(&mut self, signal: OutputSignal) -> &mut Self {
self.connect_peripheral_to_output_with_options(signal, false, false, false, false)
}
fn connect_peripheral_to_output_with_options(
&mut self,
signal: OutputSignal,
invert: bool,
invert_enable: bool,
enable_from_gpio: bool,
force_via_gpio_mux: bool,
) -> &mut Self;
/// Remove this output pin from a connected [signal](`InputSignal`).
///
/// Clears the entry in the GPIO matrix / IO mux that associates this output
/// pin with a previously connected [signal](`InputSignal`). Any other
/// outputs connected to the signal remain intact.
fn disconnect_peripheral_from_output(&mut self) -> &mut Self;
fn internal_pull_up(&mut self, on: bool) -> &mut Self;
fn internal_pull_down(&mut self, on: bool) -> &mut Self;
}
#[doc(hidden)]
pub struct SingleCoreInteruptStatusRegisterAccess {}
#[doc(hidden)]
pub struct DualCoreInteruptStatusRegisterAccess {}
#[doc(hidden)]
pub trait InteruptStatusRegisterAccess {
fn pro_cpu_interrupt_status_read() -> u32;
fn pro_cpu_nmi_status_read() -> u32;
fn app_cpu_interrupt_status_read() -> u32;
fn app_cpu_nmi_status_read() -> u32;
}
impl InteruptStatusRegisterAccess for SingleCoreInteruptStatusRegisterAccess {
fn pro_cpu_interrupt_status_read() -> u32 {
unsafe { &*GPIO::PTR }.pcpu_int.read().bits()
}
fn pro_cpu_nmi_status_read() -> u32 {
unsafe { &*GPIO::PTR }.pcpu_nmi_int.read().bits()
}
fn app_cpu_interrupt_status_read() -> u32 {
unsafe { &*GPIO::PTR }.pcpu_int.read().bits()
}
fn app_cpu_nmi_status_read() -> u32 {
unsafe { &*GPIO::PTR }.pcpu_nmi_int.read().bits()
}
}
// ESP32S3 is a dual-core chip however pro cpu and app cpu shares the same
// interrupt enable bit see
// https://github.com/espressif/esp-idf/blob/c04803e88b871a4044da152dfb3699cf47354d18/components/hal/esp32s3/include/hal/gpio_ll.h#L32
// Treating it as SingleCore in the gpio macro makes this work.
#[cfg(not(any(esp32c2, esp32c3, esp32s2, esp32s3)))]
impl InteruptStatusRegisterAccess for DualCoreInteruptStatusRegisterAccess {
fn pro_cpu_interrupt_status_read() -> u32 {
unsafe { &*GPIO::PTR }.pcpu_int.read().bits()
}
fn pro_cpu_nmi_status_read() -> u32 {
unsafe { &*GPIO::PTR }.pcpu_nmi_int.read().bits()
}
fn app_cpu_interrupt_status_read() -> u32 {
unsafe { &*GPIO::PTR }.acpu_int.read().bits()
}
fn app_cpu_nmi_status_read() -> u32 {
unsafe { &*GPIO::PTR }.acpu_nmi_int.read().bits()
}
}
#[doc(hidden)]
pub trait InterruptStatusRegisters<RegisterAccess>
where
RegisterAccess: InteruptStatusRegisterAccess,
{
fn pro_cpu_interrupt_status_read(&self) -> u32 {
RegisterAccess::pro_cpu_interrupt_status_read()
}
fn pro_cpu_nmi_status_read(&self) -> u32 {
RegisterAccess::pro_cpu_nmi_status_read()
}
fn app_cpu_interrupt_status_read(&self) -> u32 {
RegisterAccess::app_cpu_interrupt_status_read()
}
fn app_cpu_nmi_status_read(&self) -> u32 {
RegisterAccess::app_cpu_nmi_status_read()
}
}
#[doc(hidden)]
pub struct Bank0GpioRegisterAccess;
#[doc(hidden)]
pub struct Bank1GpioRegisterAccess;
#[doc(hidden)]
pub trait BankGpioRegisterAccess {
fn write_out_en_clear(&self, word: u32);
fn write_out_en_set(&self, word: u32);
fn read_input(&self) -> u32;
fn read_output(&self) -> u32;
fn write_interrupt_status_clear(&self, word: u32);
fn write_output_set(&self, word: u32);
fn write_output_clear(&self, word: u32);
fn set_output_signal(&self, gpio_num: u8, signal: u32) {
let gpio = unsafe { &*crate::pac::GPIO::PTR };
gpio.func_out_sel_cfg[gpio_num as usize]
.modify(|_, w| unsafe { w.out_sel().bits(signal as OutputSignalType) });
}
fn configure_out_sel(&self, gpio_num: u8, signal: u32, invert: bool, oen: bool, oen_inv: bool) {
let gpio = unsafe { &*crate::pac::GPIO::PTR };
gpio.func_out_sel_cfg[gpio_num as usize].modify(|_, w| unsafe {
w.out_sel()
.bits(signal as OutputSignalType)
.inv_sel()
.bit(invert)
.oen_sel()
.bit(oen)
.oen_inv_sel()
.bit(oen_inv)
});
}
fn set_signal_to_level(&self, signal: u32, high: bool) {
let gpio = unsafe { &*crate::pac::GPIO::PTR };
gpio.func_in_sel_cfg[signal as usize].modify(|_, w| unsafe {
w.sel()
.set_bit()
.in_inv_sel()
.bit(false)
.in_sel()
.bits(if high { ONE_INPUT } else { ZERO_INPUT })
});
}
fn clear_func_in_sel(&self, signal: u32) {
let gpio = unsafe { &*crate::pac::GPIO::PTR };
gpio.func_in_sel_cfg[signal as usize].modify(|_, w| w.sel().clear_bit());
}
fn set_int_enable(
&self,
gpio_num: u8,
int_ena: u32,
int_type: u8,
wake_up_from_light_sleep: bool,
) {
let gpio = unsafe { &*crate::pac::GPIO::PTR };
gpio.pin[gpio_num as usize].modify(|_, w| unsafe {
w.int_ena()
.bits(int_ena as u8)
.int_type()
.bits(int_type as u8)
.wakeup_enable()
.bit(wake_up_from_light_sleep)
});
}
fn set_open_drain(&self, gpio_num: u8, open_drain: bool) {
let gpio = unsafe { &*crate::pac::GPIO::PTR };
gpio.pin[gpio_num as usize].modify(|_, w| w.pad_driver().bit(open_drain));
}
}
impl BankGpioRegisterAccess for Bank0GpioRegisterAccess {
fn write_out_en_clear(&self, word: u32) {
unsafe { &*GPIO::PTR }
.enable_w1tc
.write(|w| unsafe { w.bits(word) });
}
fn write_out_en_set(&self, word: u32) {
unsafe { &*GPIO::PTR }
.enable_w1ts
.write(|w| unsafe { w.bits(word) });
}
fn read_input(&self) -> u32 {
unsafe { &*GPIO::PTR }.in_.read().bits()
}
fn read_output(&self) -> u32 {
unsafe { &*GPIO::PTR }.out.read().bits()
}
fn write_interrupt_status_clear(&self, word: u32) {
unsafe { &*GPIO::PTR }
.status_w1tc
.write(|w| unsafe { w.bits(word) });
}
fn write_output_set(&self, word: u32) {
unsafe { &*GPIO::PTR }
.out_w1ts
.write(|w| unsafe { w.bits(word) });
}
fn write_output_clear(&self, word: u32) {
unsafe { &*GPIO::PTR }
.out_w1tc
.write(|w| unsafe { w.bits(word) });
}
}
#[cfg(not(any(esp32c2, esp32c3)))]
impl BankGpioRegisterAccess for Bank1GpioRegisterAccess {
fn write_out_en_clear(&self, word: u32) {
unsafe { &*GPIO::PTR }
.enable1_w1tc
.write(|w| unsafe { w.bits(word) });
}
fn write_out_en_set(&self, word: u32) {
unsafe { &*GPIO::PTR }
.enable1_w1ts
.write(|w| unsafe { w.bits(word) });
}
fn read_input(&self) -> u32 {
unsafe { &*GPIO::PTR }.in1.read().bits()
}
fn read_output(&self) -> u32 {
unsafe { &*GPIO::PTR }.out1.read().bits()
}
fn write_interrupt_status_clear(&self, word: u32) {
unsafe { &*GPIO::PTR }
.status1_w1tc
.write(|w| unsafe { w.bits(word) });
}
fn write_output_set(&self, word: u32) {
unsafe { &*GPIO::PTR }
.out1_w1ts
.write(|w| unsafe { w.bits(word) });
}
fn write_output_clear(&self, word: u32) {
unsafe { &*GPIO::PTR }
.out1_w1tc
.write(|w| unsafe { w.bits(word) });
}
}
pub fn connect_low_to_peripheral(signal: InputSignal) {
unsafe { &*GPIO::PTR }.func_in_sel_cfg[signal as usize].modify(|_, w| unsafe {
w.sel()
.set_bit()
.in_inv_sel()
.bit(false)
.in_sel()
.bits(ZERO_INPUT)
});
}
pub fn connect_high_to_peripheral(signal: InputSignal) {
unsafe { &*GPIO::PTR }.func_in_sel_cfg[signal as usize].modify(|_, w| unsafe {
w.sel()
.set_bit()
.in_inv_sel()
.bit(false)
.in_sel()
.bits(ONE_INPUT)
});
}
#[doc(hidden)]
pub trait PinType {}
#[doc(hidden)]
pub trait IsOutputPin: PinType {}
#[doc(hidden)]
pub trait IsInputPin: PinType {}
#[doc(hidden)]
pub trait IsAnalogPin: PinType {}
#[doc(hidden)]
pub struct InputOutputPinType;
#[doc(hidden)]
pub struct InputOnlyPinType;
#[doc(hidden)]
pub struct InputOutputAnalogPinType;
#[doc(hidden)]
pub struct InputOnlyAnalogPinType;
impl PinType for InputOutputPinType {}
impl IsOutputPin for InputOutputPinType {}
impl IsInputPin for InputOutputPinType {}
impl PinType for InputOnlyPinType {}
impl IsInputPin for InputOnlyPinType {}
impl PinType for InputOutputAnalogPinType {}
impl IsOutputPin for InputOutputAnalogPinType {}
impl IsInputPin for InputOutputAnalogPinType {}
impl IsAnalogPin for InputOutputAnalogPinType {}
impl PinType for InputOnlyAnalogPinType {}
impl IsInputPin for InputOnlyAnalogPinType {}
impl IsAnalogPin for InputOnlyAnalogPinType {}
pub struct GpioPin<MODE, RA, PINTYPE, const GPIONUM: u8>
where
RA: BankGpioRegisterAccess,
PINTYPE: PinType,
{
_mode: PhantomData<MODE>,
_pintype: PhantomData<PINTYPE>,
reg_access: RA,
af_input_signals: [Option<InputSignal>; 6],
af_output_signals: [Option<OutputSignal>; 6],
}
impl<MODE, RA, PINTYPE, const GPIONUM: u8> embedded_hal::digital::v2::InputPin
for GpioPin<Input<MODE>, RA, PINTYPE, GPIONUM>
where
RA: BankGpioRegisterAccess,
PINTYPE: PinType,
{
type Error = Infallible;
fn is_high(&self) -> Result<bool, Self::Error> {
Ok(self.reg_access.read_input() & (1 << (GPIONUM % 32)) != 0)
}
fn is_low(&self) -> Result<bool, Self::Error> {
Ok(!self.is_high()?)
}
}
impl<RA, PINTYPE, const GPIONUM: u8> embedded_hal::digital::v2::InputPin
for GpioPin<Output<OpenDrain>, RA, PINTYPE, GPIONUM>
where
RA: BankGpioRegisterAccess,
PINTYPE: PinType,
{
type Error = Infallible;
fn is_high(&self) -> Result<bool, Self::Error> {
Ok(self.reg_access.read_input() & (1 << (GPIONUM % 32)) != 0)
}
fn is_low(&self) -> Result<bool, Self::Error> {
Ok(!self.is_high()?)
}
}
#[cfg(feature = "eh1")]
impl<MODE, RA, PINTYPE, const GPIONUM: u8> embedded_hal_1::digital::ErrorType
for GpioPin<Input<MODE>, RA, PINTYPE, GPIONUM>
where
RA: BankGpioRegisterAccess,
PINTYPE: PinType,
{
type Error = Infallible;
}
#[cfg(feature = "eh1")]
impl<MODE, RA, PINTYPE, const GPIONUM: u8> embedded_hal_1::digital::InputPin
for GpioPin<Input<MODE>, RA, PINTYPE, GPIONUM>
where
RA: BankGpioRegisterAccess,
PINTYPE: PinType,
{
fn is_high(&self) -> Result<bool, Self::Error> {
Ok(self.reg_access.read_input() & (1 << (GPIONUM % 32)) != 0)
}
fn is_low(&self) -> Result<bool, Self::Error> {
Ok(!self.is_high()?)
}
}
impl<MODE, RA, PINTYPE, const GPIONUM: u8> GpioPin<MODE, RA, PINTYPE, GPIONUM>
where
RA: BankGpioRegisterAccess,
PINTYPE: PinType,
{
fn init_input(&self, pull_down: bool, pull_up: bool) {
let gpio = unsafe { &*GPIO::PTR };
self.reg_access.write_out_en_clear(1 << (GPIONUM % 32));
gpio.func_out_sel_cfg[GPIONUM as usize]
.modify(|_, w| unsafe { w.out_sel().bits(OutputSignal::GPIO as OutputSignalType) });
#[cfg(esp32)]
types::errata36(GPIONUM, pull_up, pull_down);
get_io_mux_reg(GPIONUM).modify(|_, w| unsafe {
w.mcu_sel()
.bits(GPIO_FUNCTION as u8)
.fun_ie()
.set_bit()
.fun_wpd()
.bit(pull_down)
.fun_wpu()
.bit(pull_up)
.slp_sel()
.clear_bit()
});
}
pub fn into_floating_input(self) -> GpioPin<Input<Floating>, RA, PINTYPE, GPIONUM> {
self.init_input(false, false);
GpioPin {
_mode: PhantomData,
_pintype: PhantomData,
reg_access: self.reg_access,
af_input_signals: self.af_input_signals,
af_output_signals: self.af_output_signals,
}
}
pub fn into_pull_up_input(self) -> GpioPin<Input<PullUp>, RA, PINTYPE, GPIONUM> {
self.init_input(false, true);
GpioPin {
_mode: PhantomData,
_pintype: PhantomData,
reg_access: self.reg_access,
af_input_signals: self.af_input_signals,
af_output_signals: self.af_output_signals,
}
}
pub fn into_pull_down_input(self) -> GpioPin<Input<PullDown>, RA, PINTYPE, GPIONUM> {
self.init_input(true, false);
GpioPin {
_mode: PhantomData,
_pintype: PhantomData,
reg_access: self.reg_access,
af_input_signals: self.af_input_signals,
af_output_signals: self.af_output_signals,
}
}
}
impl<MODE, RA, PINTYPE, const GPIONUM: u8> InputPin for GpioPin<MODE, RA, PINTYPE, GPIONUM>
where
RA: BankGpioRegisterAccess,
PINTYPE: PinType,
{
fn set_to_input(&mut self) -> &mut Self {
self.init_input(false, false);
self
}
fn enable_input(&mut self, on: bool) -> &mut Self {
get_io_mux_reg(GPIONUM).modify(|_, w| w.fun_ie().bit(on));
self
}
fn enable_input_in_sleep_mode(&mut self, on: bool) -> &mut Self {
get_io_mux_reg(GPIONUM).modify(|_, w| w.mcu_ie().bit(on));
self
}
fn is_input_high(&self) -> bool {
self.reg_access.read_input() & (1 << (GPIONUM % 32)) != 0
}
fn connect_input_to_peripheral_with_options(
&mut self,
signal: InputSignal,
invert: bool,
force_via_gpio_mux: bool,
) -> &mut Self {
let af = if force_via_gpio_mux {
GPIO_FUNCTION
} else {
let mut res = GPIO_FUNCTION;
for (i, input_signal) in self.af_input_signals.iter().enumerate() {
if let Some(input_signal) = input_signal {
if *input_signal == signal {
res = match i {
0 => AlternateFunction::Function0,
1 => AlternateFunction::Function1,
2 => AlternateFunction::Function2,
3 => AlternateFunction::Function3,
4 => AlternateFunction::Function4,
5 => AlternateFunction::Function5,
_ => unreachable!(),
};
break;
}
}
}
res
};
if af == GPIO_FUNCTION && signal as usize > INPUT_SIGNAL_MAX as usize {
panic!("Cannot connect GPIO to this peripheral");
}
self.set_alternate_function(af);
if (signal as usize) <= INPUT_SIGNAL_MAX as usize {
unsafe { &*GPIO::PTR }.func_in_sel_cfg[signal as usize].modify(|_, w| unsafe {
w.sel()
.set_bit()
.in_inv_sel()
.bit(invert)
.in_sel()
.bits(GPIONUM)
});
}
self
}
fn disconnect_input_from_peripheral(&mut self, signal: InputSignal) -> &mut Self {
self.set_alternate_function(GPIO_FUNCTION);
unsafe { &*GPIO::PTR }.func_in_sel_cfg[signal as usize].modify(|_, w| w.sel().clear_bit());
self
}
}
impl<MODE, RA, PINTYPE, const GPIONUM: u8> Pin for GpioPin<MODE, RA, PINTYPE, GPIONUM>
where
RA: BankGpioRegisterAccess,
PINTYPE: PinType,
{
fn number(&self) -> u8 {
GPIONUM
}
fn sleep_mode(&mut self, on: bool) -> &mut Self {
get_io_mux_reg(GPIONUM).modify(|_, w| w.slp_sel().bit(on));
self
}
fn set_alternate_function(&mut self, alternate: AlternateFunction) -> &mut Self {
get_io_mux_reg(GPIONUM).modify(|_, w| unsafe { w.mcu_sel().bits(alternate as u8) });
self
}
fn listen_with_options(
&mut self,
event: Event,
int_enable: bool,
nmi_enable: bool,
wake_up_from_light_sleep: bool,
) {
if wake_up_from_light_sleep {
match event {
Event::AnyEdge | Event::RisingEdge | Event::FallingEdge => {
panic!("Edge triggering is not supported for wake-up from light sleep");
}
_ => {}
}
}
unsafe {
(&*GPIO::PTR).pin[GPIONUM as usize].modify(|_, w| {
w.int_ena()
.bits(gpio_intr_enable(int_enable, nmi_enable))
.int_type()
.bits(event as u8)
.wakeup_enable()
.bit(wake_up_from_light_sleep)
});
}
}
fn is_listening(&self) -> bool {
let bits = unsafe { &*GPIO::PTR }.pin[GPIONUM as usize].read().int_ena().bits();
bits != 0
}
fn unlisten(&mut self) {
unsafe {
(&*GPIO::PTR).pin[GPIONUM as usize]
.modify(|_, w| w.int_ena().bits(0).int_type().bits(0).int_ena().bits(0));
}
}
fn clear_interrupt(&mut self) {
self.reg_access
.write_interrupt_status_clear(1 << (GPIONUM % 32));
}
fn is_pcore_interrupt_set(&self) -> bool {
(self.pro_cpu_interrupt_status_read() & (1 << (GPIONUM % 32))) != 0
}
fn is_pcore_non_maskable_interrupt_set(&self) -> bool {
(self.pro_cpu_nmi_status_read() & (1 << (GPIONUM % 32))) != 0
}
fn is_acore_interrupt_set(&self) -> bool {
(self.app_cpu_interrupt_status_read() & (1 << (GPIONUM % 32))) != 0
}
fn is_acore_non_maskable_interrupt_set(&self) -> bool {
(self.app_cpu_nmi_status_read() & (1 << (GPIONUM % 32))) != 0
}
fn enable_hold(&mut self, _on: bool) {
todo!();
}
}
impl<MODE, RA, PINTYPE, const GPIONUM: u8> embedded_hal::digital::v2::OutputPin
for GpioPin<Output<MODE>, RA, PINTYPE, GPIONUM>
where
RA: BankGpioRegisterAccess,
PINTYPE: IsOutputPin,
{
type Error = Infallible;
fn set_high(&mut self) -> Result<(), Self::Error> {
self.reg_access.write_output_set(1 << (GPIONUM % 32));
Ok(())
}
fn set_low(&mut self) -> Result<(), Self::Error> {
self.reg_access.write_output_clear(1 << (GPIONUM % 32));
Ok(())
}
}
impl<MODE, RA, PINTYPE, const GPIONUM: u8> embedded_hal::digital::v2::StatefulOutputPin
for GpioPin<Output<MODE>, RA, PINTYPE, GPIONUM>
where
RA: BankGpioRegisterAccess,
PINTYPE: IsOutputPin,
{
fn is_set_high(&self) -> Result<bool, Self::Error> {
Ok(self.reg_access.read_output() & (1 << (GPIONUM % 32)) != 0)
}
fn is_set_low(&self) -> Result<bool, Self::Error> {
Ok(!self.is_set_high()?)
}
}
impl<MODE, RA, PINTYPE, const GPIONUM: u8> embedded_hal::digital::v2::ToggleableOutputPin
for GpioPin<Output<MODE>, RA, PINTYPE, GPIONUM>
where
RA: BankGpioRegisterAccess,
PINTYPE: IsOutputPin,
{
type Error = Infallible;
fn toggle(&mut self) -> Result<(), Self::Error> {
use embedded_hal::digital::v2::{OutputPin as _, StatefulOutputPin as _};
if self.is_set_high()? {
Ok(self.set_low()?)
} else {
Ok(self.set_high()?)
}
}
}
#[cfg(feature = "eh1")]
impl<MODE, RA, PINTYPE, const GPIONUM: u8> embedded_hal_1::digital::ErrorType
for GpioPin<Output<MODE>, RA, PINTYPE, GPIONUM>
where
RA: BankGpioRegisterAccess,
PINTYPE: IsOutputPin,
{
type Error = Infallible;
}
#[cfg(feature = "eh1")]
impl<MODE, RA, PINTYPE, const GPIONUM: u8> embedded_hal_1::digital::OutputPin
for GpioPin<Output<MODE>, RA, PINTYPE, GPIONUM>
where
RA: BankGpioRegisterAccess,
PINTYPE: IsOutputPin,
{
fn set_low(&mut self) -> Result<(), Self::Error> {
self.reg_access.write_output_clear(1 << (GPIONUM % 32));
Ok(())
}
fn set_high(&mut self) -> Result<(), Self::Error> {
self.reg_access.write_output_set(1 << (GPIONUM % 32));
Ok(())
}
}
#[cfg(feature = "eh1")]
impl<MODE, RA, PINTYPE, const GPIONUM: u8> embedded_hal_1::digital::StatefulOutputPin
for GpioPin<Output<MODE>, RA, PINTYPE, GPIONUM>
where
RA: BankGpioRegisterAccess,
PINTYPE: IsOutputPin,
{
fn is_set_high(&self) -> Result<bool, Self::Error> {
Ok(self.reg_access.read_output() & (1 << (GPIONUM % 32)) != 0)
}
fn is_set_low(&self) -> Result<bool, Self::Error> {
Ok(!self.is_set_high()?)
}
}
#[cfg(feature = "eh1")]
impl<MODE, RA, PINTYPE, const GPIONUM: u8> embedded_hal_1::digital::ToggleableOutputPin
for GpioPin<Output<MODE>, RA, PINTYPE, GPIONUM>
where
RA: BankGpioRegisterAccess,
PINTYPE: IsOutputPin,
{
fn toggle(&mut self) -> Result<(), Self::Error> {
use embedded_hal_1::digital::{OutputPin as _, StatefulOutputPin as _};
if self.is_set_high()? {
Ok(self.set_low()?)
} else {
Ok(self.set_high()?)
}
}
}
impl<RA, PINTYPE, const GPIONUM: u8> From<GpioPin<Unknown, RA, PINTYPE, GPIONUM>>
for GpioPin<Input<Floating>, RA, PINTYPE, GPIONUM>
where
RA: BankGpioRegisterAccess,
PINTYPE: IsOutputPin,
{
fn from(
pin: GpioPin<Unknown, RA, PINTYPE, GPIONUM>,
) -> GpioPin<Input<Floating>, RA, PINTYPE, GPIONUM> {
pin.into_floating_input()
}
}
impl<RA, PINTYPE, const GPIONUM: u8> From<GpioPin<Unknown, RA, PINTYPE, GPIONUM>>
for GpioPin<Input<PullUp>, RA, PINTYPE, GPIONUM>
where
RA: BankGpioRegisterAccess,
PINTYPE: IsOutputPin,
{
fn from(
pin: GpioPin<Unknown, RA, PINTYPE, GPIONUM>,
) -> GpioPin<Input<PullUp>, RA, PINTYPE, GPIONUM> {
pin.into_pull_up_input()
}
}
impl<RA, PINTYPE, const GPIONUM: u8> From<GpioPin<Unknown, RA, PINTYPE, GPIONUM>>
for GpioPin<Input<PullDown>, RA, PINTYPE, GPIONUM>
where
RA: BankGpioRegisterAccess,
PINTYPE: IsInputPin,
{
fn from(
pin: GpioPin<Unknown, RA, PINTYPE, GPIONUM>,
) -> GpioPin<Input<PullDown>, RA, PINTYPE, GPIONUM> {
pin.into_pull_down_input()
}
}
impl<RA, PINTYPE, const GPIONUM: u8> From<GpioPin<Unknown, RA, PINTYPE, GPIONUM>>
for GpioPin<Output<PushPull>, RA, PINTYPE, GPIONUM>
where
RA: BankGpioRegisterAccess,
PINTYPE: IsOutputPin,
{
fn from(
pin: GpioPin<Unknown, RA, PINTYPE, GPIONUM>,
) -> GpioPin<Output<PushPull>, RA, PINTYPE, GPIONUM> {
pin.into_push_pull_output()
}
}
impl<RA, PINTYPE, const GPIONUM: u8> From<GpioPin<Unknown, RA, PINTYPE, GPIONUM>>
for GpioPin<Output<OpenDrain>, RA, PINTYPE, GPIONUM>
where
RA: BankGpioRegisterAccess,
PINTYPE: IsOutputPin,
{
fn from(
pin: GpioPin<Unknown, RA, PINTYPE, GPIONUM>,
) -> GpioPin<Output<OpenDrain>, RA, PINTYPE, GPIONUM> {
pin.into_open_drain_output()
}
}
impl<RA, PINTYPE, const GPIONUM: u8> From<GpioPin<Unknown, RA, PINTYPE, GPIONUM>>
for GpioPin<Alternate<AF1>, RA, PINTYPE, GPIONUM>
where
RA: BankGpioRegisterAccess,
PINTYPE: IsOutputPin,
{
fn from(
pin: GpioPin<Unknown, RA, PINTYPE, GPIONUM>,
) -> GpioPin<Alternate<AF1>, RA, PINTYPE, GPIONUM> {
pin.into_alternate_1()
}
}
impl<RA, PINTYPE, const GPIONUM: u8> From<GpioPin<Unknown, RA, PINTYPE, GPIONUM>>
for GpioPin<Alternate<AF2>, RA, PINTYPE, GPIONUM>
where
RA: BankGpioRegisterAccess,
PINTYPE: IsOutputPin,
{