Skip to content

Commit d97d7db

Browse files
authored
Merge pull request #33 from robyoung/add-gpio-pin-names
Add GPIO pin names and extract LED pins
2 parents ac65801 + 5531237 commit d97d7db

File tree

4 files changed

+122
-41
lines changed

4 files changed

+122
-41
lines changed

examples/led_blocking.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,21 @@ fn main() -> ! {
2121
let p0parts = P0Parts::new(p.GPIO);
2222

2323
// Display
24-
let row1 = p0parts.p0_13.into_push_pull_output(Level::Low);
25-
let row2 = p0parts.p0_14.into_push_pull_output(Level::Low);
26-
let row3 = p0parts.p0_15.into_push_pull_output(Level::Low);
27-
let col1 = p0parts.p0_04.into_push_pull_output(Level::Low);
28-
let col2 = p0parts.p0_05.into_push_pull_output(Level::Low);
29-
let col3 = p0parts.p0_06.into_push_pull_output(Level::Low);
30-
let col4 = p0parts.p0_07.into_push_pull_output(Level::Low);
31-
let col5 = p0parts.p0_08.into_push_pull_output(Level::Low);
32-
let col6 = p0parts.p0_09.into_push_pull_output(Level::Low);
33-
let col7 = p0parts.p0_10.into_push_pull_output(Level::Low);
34-
let col8 = p0parts.p0_11.into_push_pull_output(Level::Low);
35-
let col9 = p0parts.p0_12.into_push_pull_output(Level::Low);
36-
let mut leds = led::Display::new(
37-
col1, col2, col3, col4, col5, col6, col7, col8, col9, row1, row2, row3,
38-
);
24+
let pins = led::Pins {
25+
row1: p0parts.p0_13.into_push_pull_output(Level::Low),
26+
row2: p0parts.p0_14.into_push_pull_output(Level::Low),
27+
row3: p0parts.p0_15.into_push_pull_output(Level::Low),
28+
col1: p0parts.p0_04.into_push_pull_output(Level::Low),
29+
col2: p0parts.p0_05.into_push_pull_output(Level::Low),
30+
col3: p0parts.p0_06.into_push_pull_output(Level::Low),
31+
col4: p0parts.p0_07.into_push_pull_output(Level::Low),
32+
col5: p0parts.p0_08.into_push_pull_output(Level::Low),
33+
col6: p0parts.p0_09.into_push_pull_output(Level::Low),
34+
col7: p0parts.p0_10.into_push_pull_output(Level::Low),
35+
col8: p0parts.p0_11.into_push_pull_output(Level::Low),
36+
col9: p0parts.p0_12.into_push_pull_output(Level::Low),
37+
};
38+
let mut leds = led::Display::new(pins);
3939

4040
#[allow(non_snake_case)]
4141
let letter_I = [

src/gpio.rs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
//! Named GPIO pin types
2+
//!
3+
//! This module maps the GPIO pin names as described in the
4+
//! [v1.5 schematic](https://github.com/bbcmicrobit/hardware/tree/master/V1.5).
5+
//! Where appropriate the pins are restricted with the appropriate `MODE`
6+
//! from `nrf-hal`.
7+
#![allow(clippy::upper_case_acronyms)]
8+
use crate::hal::gpio::{p0, Floating, Input, Output, PushPull};
9+
10+
/* GPIO pads */
11+
pub type PAD1<MODE> = p0::P0_03<MODE>;
12+
pub type PAD2<MODE> = p0::P0_02<MODE>;
13+
pub type PAD3<MODE> = p0::P0_01<MODE>;
14+
15+
/* LED display */
16+
pub type COL1 = p0::P0_04<Output<PushPull>>;
17+
pub type COL2 = p0::P0_05<Output<PushPull>>;
18+
pub type COL3 = p0::P0_06<Output<PushPull>>;
19+
pub type COL4 = p0::P0_07<Output<PushPull>>;
20+
pub type COL5 = p0::P0_08<Output<PushPull>>;
21+
pub type COL6 = p0::P0_09<Output<PushPull>>;
22+
pub type COL7 = p0::P0_10<Output<PushPull>>;
23+
pub type COL8 = p0::P0_11<Output<PushPull>>;
24+
pub type COL9 = p0::P0_12<Output<PushPull>>;
25+
26+
pub type ROW1 = p0::P0_13<Output<PushPull>>;
27+
pub type ROW2 = p0::P0_14<Output<PushPull>>;
28+
pub type ROW3 = p0::P0_15<Output<PushPull>>;
29+
30+
/* buttons */
31+
pub type BTN_A = p0::P0_17<Input<Floating>>;
32+
pub type BTN_B = p0::P0_26<Input<Floating>>;
33+
34+
/* spi */
35+
pub type MOSI<MODE> = p0::P0_21<MODE>;
36+
pub type MISO<MODE> = p0::P0_22<MODE>;
37+
pub type SCK<MODE> = p0::P0_23<MODE>;
38+
39+
/* i2c */
40+
pub type SCL = p0::P0_00<Input<Floating>>;
41+
pub type SDA = p0::P0_30<Input<Floating>>;
42+
43+
/* uart */
44+
pub type UART_TX = p0::P0_24<Output<PushPull>>;
45+
pub type UART_RX = p0::P0_25<Input<Floating>>;
46+
47+
/* edge connector */
48+
pub type EDGE01 = COL1;
49+
pub type EDGE02<MODE> = PAD1<MODE>; // <- big pad 1
50+
pub type EDGE03 = COL2;
51+
pub type EDGE04 = BTN_A;
52+
pub type EDGE05 = COL9;
53+
pub type EDGE06 = COL8;
54+
pub type EDGE07<MODE> = PAD2<MODE>; // <- big pad 2
55+
pub type EDGE08<MODE> = p0::P0_18<MODE>;
56+
pub type EDGE09 = COL7;
57+
pub type EDGE10 = COL3;
58+
pub type EDGE11 = BTN_B;
59+
pub type EDGE12<MODE> = p0::P0_20<MODE>;
60+
pub type EDGE13<MODE> = PAD3<MODE>; // <- big pad 3
61+
pub type EDGE14<MODE> = SCK<MODE>;
62+
pub type EDGE15<MODE> = MISO<MODE>;
63+
pub type EDGE16<MODE> = MOSI<MODE>;
64+
pub type EDGE17<MODE> = p0::P0_16<MODE>;
65+
// EDGE18 -> +V
66+
// EDGE19 -> +V
67+
// EDGE20 -> +V
68+
pub type EDGE21 = SCL;
69+
pub type EDGE22 = SDA;
70+
// EDGE23 -> GND
71+
// EDGE24 -> GND
72+
// EDGE25 -> GND

src/led.rs

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
//! On-board user LEDs
22
33
use crate::hal::{
4-
gpio::{p0, Output, Pin, PushPull},
4+
gpio::{Output, Pin, PushPull},
55
prelude::*,
66
};
77

8+
use crate::gpio::{COL1, COL2, COL3, COL4, COL5, COL6, COL7, COL8, COL9, ROW1, ROW2, ROW3};
9+
810
use embedded_hal::blocking::delay::DelayUs;
911

1012
#[allow(clippy::upper_case_acronyms)]
11-
type LED = Pin<Output<PushPull>>;
13+
pub(crate) type LED = Pin<Output<PushPull>>;
1214

1315
const DEFAULT_DELAY_MS: u32 = 2;
1416
const LED_LAYOUT: [[(usize, usize); 5]; 5] = [
@@ -19,6 +21,21 @@ const LED_LAYOUT: [[(usize, usize); 5]; 5] = [
1921
[(2, 2), (1, 6), (2, 0), (1, 5), (2, 1)],
2022
];
2123

24+
pub struct Pins {
25+
pub col1: COL1,
26+
pub col2: COL2,
27+
pub col3: COL3,
28+
pub col4: COL4,
29+
pub col5: COL5,
30+
pub col6: COL6,
31+
pub col7: COL7,
32+
pub col8: COL8,
33+
pub col9: COL9,
34+
pub row1: ROW1,
35+
pub row2: ROW2,
36+
pub row3: ROW3,
37+
}
38+
2239
/// Array of all the LEDs in the 5x5 display on the board
2340
pub struct Display {
2441
delay_ms: u32,
@@ -29,33 +46,24 @@ pub struct Display {
2946
impl Display {
3047
/// Initializes all the user LEDs
3148
#[allow(clippy::too_many_arguments)]
32-
pub fn new(
33-
col1: p0::P0_04<Output<PushPull>>,
34-
col2: p0::P0_05<Output<PushPull>>,
35-
col3: p0::P0_06<Output<PushPull>>,
36-
col4: p0::P0_07<Output<PushPull>>,
37-
col5: p0::P0_08<Output<PushPull>>,
38-
col6: p0::P0_09<Output<PushPull>>,
39-
col7: p0::P0_10<Output<PushPull>>,
40-
col8: p0::P0_11<Output<PushPull>>,
41-
col9: p0::P0_12<Output<PushPull>>,
42-
row1: p0::P0_13<Output<PushPull>>,
43-
row2: p0::P0_14<Output<PushPull>>,
44-
row3: p0::P0_15<Output<PushPull>>,
45-
) -> Self {
49+
pub fn new(pins: Pins) -> Self {
4650
let mut retval = Display {
4751
delay_ms: DEFAULT_DELAY_MS,
48-
rows: [row1.degrade(), row2.degrade(), row3.degrade()],
52+
rows: [
53+
pins.row1.degrade(),
54+
pins.row2.degrade(),
55+
pins.row3.degrade(),
56+
],
4957
cols: [
50-
col1.degrade(),
51-
col2.degrade(),
52-
col3.degrade(),
53-
col4.degrade(),
54-
col5.degrade(),
55-
col6.degrade(),
56-
col7.degrade(),
57-
col8.degrade(),
58-
col9.degrade(),
58+
pins.col1.degrade(),
59+
pins.col2.degrade(),
60+
pins.col3.degrade(),
61+
pins.col4.degrade(),
62+
pins.col5.degrade(),
63+
pins.col6.degrade(),
64+
pins.col7.degrade(),
65+
pins.col8.degrade(),
66+
pins.col9.degrade(),
5967
],
6068
};
6169
// This is needed to reduce flickering on reset

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pub use nrf51_hal as hal;
88
pub use nb::*;
99

1010
pub mod display;
11+
pub mod gpio;
1112
pub mod led;
1213

1314
#[macro_export]

0 commit comments

Comments
 (0)