microbit_common/v2/
gpio.rs

1#![allow(clippy::upper_case_acronyms, missing_docs)]
2use nrf52833_hal::gpio::{p0, p1, Floating, Input, OpenDrain, Output, Pin, PushPull};
3
4/* GPIO pads */
5pub type PAD0<MODE> = p0::P0_02<MODE>;
6pub type PAD1<MODE> = p0::P0_03<MODE>;
7pub type PAD2<MODE> = p0::P0_04<MODE>;
8
9/* LED display */
10pub const NUM_COLS: usize = 5;
11pub type COL1 = p0::P0_28<Output<PushPull>>;
12pub type COL2 = p0::P0_11<Output<PushPull>>;
13pub type COL3 = p0::P0_31<Output<PushPull>>;
14pub type COL4 = p1::P1_05<Output<PushPull>>;
15pub type COL5 = p0::P0_30<Output<PushPull>>;
16
17pub const NUM_ROWS: usize = 5;
18pub type ROW1 = p0::P0_21<Output<PushPull>>;
19pub type ROW2 = p0::P0_22<Output<PushPull>>;
20pub type ROW3 = p0::P0_15<Output<PushPull>>;
21pub type ROW4 = p0::P0_24<Output<PushPull>>;
22pub type ROW5 = p0::P0_19<Output<PushPull>>;
23
24/// GPIO pins connected to the LED matrix
25///
26/// Use the [display_pins] macro for easier construction.
27pub struct DisplayPins {
28    pub col1: COL1,
29    pub col2: COL2,
30    pub col3: COL3,
31    pub col4: COL4,
32    pub col5: COL5,
33    pub row1: ROW1,
34    pub row2: ROW2,
35    pub row3: ROW3,
36    pub row4: ROW4,
37    pub row5: ROW5,
38}
39
40/// GPIO pins connected to the microphone
41pub struct MicrophonePins {
42    pub mic_in: p0::P0_05<Input<Floating>>,
43    pub mic_run: p0::P0_20<Output<OpenDrain>>,
44}
45
46type LED = Pin<Output<PushPull>>;
47
48impl DisplayPins {
49    pub fn degrade(self) -> ([LED; NUM_COLS], [LED; NUM_ROWS]) {
50        (
51            [
52                self.col1.degrade(),
53                self.col2.degrade(),
54                self.col3.degrade(),
55                self.col4.degrade(),
56                self.col5.degrade(),
57            ],
58            [
59                self.row1.degrade(),
60                self.row2.degrade(),
61                self.row3.degrade(),
62                self.row4.degrade(),
63                self.row5.degrade(),
64            ],
65        )
66    }
67}
68
69/// Create [DisplayPins] from a [GPIO Parts](crate::hal::gpio::p0::Parts)
70///
71/// # Example
72///
73/// ```no_run
74/// # use microbit_common as microbit;
75/// use microbit::{
76///     display_pins,
77///     pac,
78///     hal::gpio::{p0::Parts as P0Parts, p1::Parts as P1Parts},
79/// };
80///
81/// // take the peripherals
82/// let p = pac::Peripherals::take().unwrap();
83/// // split off the P0 GPIO port
84/// let p0parts = P0Parts::new(p.P0);
85/// // split off the P1 GPIO port
86/// let p1parts = P1Parts::new(p.P1);
87///
88/// let pins = display_pins!(p0parts, p1parts);
89/// ```
90#[macro_export]
91macro_rules! display_pins {
92    ( $p0parts:expr, $p1parts:expr ) => {{
93        use microbit::{gpio::DisplayPins, hal::gpio::Level};
94
95        DisplayPins {
96            col1: $p0parts.p0_28.into_push_pull_output(Level::Low),
97            col2: $p0parts.p0_11.into_push_pull_output(Level::Low),
98            col3: $p0parts.p0_31.into_push_pull_output(Level::Low),
99            col4: $p1parts.p1_05.into_push_pull_output(Level::Low),
100            col5: $p0parts.p0_30.into_push_pull_output(Level::Low),
101            row1: $p0parts.p0_21.into_push_pull_output(Level::Low),
102            row2: $p0parts.p0_22.into_push_pull_output(Level::Low),
103            row3: $p0parts.p0_15.into_push_pull_output(Level::Low),
104            row4: $p0parts.p0_24.into_push_pull_output(Level::Low),
105            row5: $p0parts.p0_19.into_push_pull_output(Level::Low),
106        }
107    }};
108}
109
110/* buttons */
111pub type BTN_A = p0::P0_14<Input<Floating>>;
112pub type BTN_B = p0::P0_23<Input<Floating>>;
113
114/* spi */
115pub type MOSI<MODE> = p0::P0_13<MODE>;
116pub type MISO<MODE> = p0::P0_01<MODE>;
117pub type SCK<MODE> = p0::P0_17<MODE>;
118
119/* i2c - internal */
120pub type INT_SCL = p0::P0_08<Input<Floating>>;
121pub type INT_SDA = p0::P0_16<Input<Floating>>;
122
123/* i2c - external */
124pub type SCL = p0::P0_26<Input<Floating>>;
125pub type SDA = p1::P1_00<Input<Floating>>;
126
127/* uart */
128pub type UART_TX = p0::P0_06<Output<PushPull>>;
129pub type UART_RX = p1::P1_08<Input<Floating>>;
130
131/* speaker */
132pub type SPEAKER = p0::P0_00<Output<PushPull>>;
133
134/* edge connector */
135pub type EDGE03 = COL3;
136pub type EDGE00<MODE> = PAD0<MODE>; // <- big pad 1
137pub type EDGE04 = COL1;
138pub type EDGE05 = BTN_A;
139pub type EDGE06 = COL4;
140pub type EDGE07 = COL2;
141pub type EDGE01<MODE> = PAD1<MODE>; // <- big pad 2
142pub type EDGE08<MODE> = p0::P0_10<MODE>;
143pub type EDGE09<MODE> = p0::P0_09<MODE>;
144pub type EDGE10 = COL5;
145pub type EDGE11 = BTN_B;
146pub type EDGE12<MODE> = p0::P0_12<MODE>;
147pub type EDGE02<MODE> = PAD2<MODE>; // <- big pad 3
148pub type EDGE13<MODE> = SCK<MODE>;
149pub type EDGE14<MODE> = MISO<MODE>;
150pub type EDGE15<MODE> = MOSI<MODE>;
151pub type EDGE16<MODE> = p1::P1_02<MODE>;
152// EDGE18 -> +V
153// EDGE19 -> +V
154// EDGE20 -> +V
155pub type EDGE19 = SCL;
156pub type EDGE20 = SDA;
157// EDGE23 -> GND
158// EDGE24 -> GND
159// EDGE25 -> GND