calliope_common/
gpio.rs

1// Copied from https://github.com/nrf-rs/microbit/tree/main/microbit-common/src/v1
2// ToDo: Needs Calliope adjustments
3
4//! Named GPIO pin types
5//!
6//! This module maps the GPIO pin names as described in the
7//! [Pins and Signals section of the micro:bit site](https://tech.microbit.org/hardware/edgeconnector/#pins-and-signals)
8//! Where appropriate the pins are restricted with the appropriate `MODE`
9//! from `nrf-hal`.
10#![allow(clippy::upper_case_acronyms, missing_docs)]
11use crate::hal::gpio::{p0, Floating, Input, Output, Pin, PushPull};
12
13/* GPIO pads */
14pub type PAD0<MODE> = p0::P0_03<MODE>;
15pub type PAD1<MODE> = p0::P0_02<MODE>;
16pub type PAD2<MODE> = p0::P0_01<MODE>;
17
18/* LED display */
19pub const NUM_COLS: usize = 9;
20pub type COL1 = p0::P0_04<Output<PushPull>>;
21pub type COL2 = p0::P0_05<Output<PushPull>>;
22pub type COL3 = p0::P0_06<Output<PushPull>>;
23pub type COL4 = p0::P0_07<Output<PushPull>>;
24pub type COL5 = p0::P0_08<Output<PushPull>>;
25pub type COL6 = p0::P0_09<Output<PushPull>>;
26pub type COL7 = p0::P0_10<Output<PushPull>>;
27pub type COL8 = p0::P0_11<Output<PushPull>>;
28pub type COL9 = p0::P0_12<Output<PushPull>>;
29
30pub const NUM_ROWS: usize = 3;
31pub type ROW1 = p0::P0_13<Output<PushPull>>;
32pub type ROW2 = p0::P0_14<Output<PushPull>>;
33pub type ROW3 = p0::P0_15<Output<PushPull>>;
34
35/// GPIO pins connected to the LED matrix
36///
37/// The pins are represented as a [3x9 matrix on the micro:bit
38/// V1](https://tech.microbit.org/hardware/1-5-revision/#display).
39/// This is mapped to the physical 5x5 LED matrix in the [crate::display]
40/// modules.
41///
42/// Use the [display_pins] macro for easier construction.
43pub struct DisplayPins {
44    pub col1: COL1,
45    pub col2: COL2,
46    pub col3: COL3,
47    pub col4: COL4,
48    pub col5: COL5,
49    pub col6: COL6,
50    pub col7: COL7,
51    pub col8: COL8,
52    pub col9: COL9,
53    pub row1: ROW1,
54    pub row2: ROW2,
55    pub row3: ROW3,
56}
57
58type LED = Pin<Output<PushPull>>;
59
60impl DisplayPins {
61    pub fn degrade(self) -> ([LED; NUM_COLS], [LED; NUM_ROWS]) {
62        (
63            [
64                self.col1.degrade(),
65                self.col2.degrade(),
66                self.col3.degrade(),
67                self.col4.degrade(),
68                self.col5.degrade(),
69                self.col6.degrade(),
70                self.col7.degrade(),
71                self.col8.degrade(),
72                self.col9.degrade(),
73            ],
74            [
75                self.row1.degrade(),
76                self.row2.degrade(),
77                self.row3.degrade(),
78            ],
79        )
80    }
81}
82
83/// Create [DisplayPins] from a [GPIO Parts](crate::hal::gpio::p0::Parts)
84///
85/// # Example
86///
87/// ```no_run
88/// # use microbit_common as microbit;
89/// use microbit::{
90///     display_pins,
91///     pac,
92///     hal::gpio::p0::Parts as P0Parts,
93/// };
94///
95/// // take the peripherals
96/// let p = pac::Peripherals::take().unwrap();
97/// // split off the P0 GPIO port
98/// let p0parts = P0Parts::new(p.GPIO);
99///
100/// let pins = display_pins!(p0parts);
101/// ```
102#[macro_export]
103macro_rules! display_pins {
104    ( $p0parts:expr ) => {{
105        use microbit::{gpio::DisplayPins, hal::gpio::Level};
106
107        DisplayPins {
108            row1: $p0parts.p0_13.into_push_pull_output(Level::Low),
109            row2: $p0parts.p0_14.into_push_pull_output(Level::Low),
110            row3: $p0parts.p0_15.into_push_pull_output(Level::Low),
111            col1: $p0parts.p0_04.into_push_pull_output(Level::Low),
112            col2: $p0parts.p0_05.into_push_pull_output(Level::Low),
113            col3: $p0parts.p0_06.into_push_pull_output(Level::Low),
114            col4: $p0parts.p0_07.into_push_pull_output(Level::Low),
115            col5: $p0parts.p0_08.into_push_pull_output(Level::Low),
116            col6: $p0parts.p0_09.into_push_pull_output(Level::Low),
117            col7: $p0parts.p0_10.into_push_pull_output(Level::Low),
118            col8: $p0parts.p0_11.into_push_pull_output(Level::Low),
119            col9: $p0parts.p0_12.into_push_pull_output(Level::Low),
120        }
121    }};
122}
123
124/* buttons */
125pub type BTN_A = p0::P0_17<Input<Floating>>;
126pub type BTN_B = p0::P0_26<Input<Floating>>;
127
128/* spi */
129pub type MOSI<MODE> = p0::P0_21<MODE>;
130pub type MISO<MODE> = p0::P0_22<MODE>;
131pub type SCK<MODE> = p0::P0_23<MODE>;
132
133/* i2c - shared external and internal */
134pub type SCL = p0::P0_00<Input<Floating>>;
135pub type SDA = p0::P0_30<Input<Floating>>;
136
137/* uart */
138pub type UART_TX = p0::P0_24<Output<PushPull>>;
139pub type UART_RX = p0::P0_25<Input<Floating>>;
140
141/* edge connector */
142pub type EDGE03 = COL1;
143pub type EDGE00<MODE> = PAD0<MODE>; // <- big pad 1
144pub type EDGE04 = COL2;
145pub type EDGE05 = BTN_A;
146pub type EDGE06 = COL9;
147pub type EDGE07 = COL8;
148pub type EDGE01<MODE> = PAD1<MODE>; // <- big pad 2
149pub type EDGE08<MODE> = p0::P0_18<MODE>;
150pub type EDGE09 = COL7;
151pub type EDGE10 = COL3;
152pub type EDGE11 = BTN_B;
153pub type EDGE12<MODE> = p0::P0_20<MODE>;
154pub type EDGE02<MODE> = PAD2<MODE>; // <- big pad 3
155pub type EDGE13<MODE> = SCK<MODE>;
156pub type EDGE14<MODE> = MISO<MODE>;
157pub type EDGE15<MODE> = MOSI<MODE>;
158pub type EDGE16<MODE> = p0::P0_16<MODE>;
159// EDGE18 -> +V
160// EDGE19 -> +V
161// EDGE20 -> +V
162pub type EDGE19 = SCL;
163pub type EDGE20 = SDA;
164// EDGE23 -> GND
165// EDGE24 -> GND
166// EDGE25 -> GND