microbit_bsp/
board.rs

1pub use embassy_nrf::config::Config;
2use embassy_nrf::gpio::{Input, Level, Output, OutputDrive, Pin, Pull};
3pub use embassy_nrf::interrupt::Priority;
4use embassy_nrf::peripherals::{
5    P0_00, P0_01, P0_02, P0_03, P0_04, P0_05, P0_06, P0_08, P0_09, P0_10, P0_12, P0_13, P0_16, P0_17, P0_20, P0_26,
6    P1_00, P1_02, P1_08, PPI_CH0, PPI_CH1, PWM0, PWM1, PWM2, PWM3, RNG, SAADC, TIMER0, TWISPI0, TWISPI1, UARTE0,
7    UARTE1,
8};
9pub use embassy_nrf::wdt;
10use embassy_nrf::Peri;
11
12#[cfg(feature = "trouble")]
13use crate::ble;
14use crate::display::LedMatrix as LedMatrixDriver;
15
16/// LED matrix peripheral for the micro:bit
17pub type LedMatrix = LedMatrixDriver<Output<'static>, 5, 5>;
18
19/// Button 'A'
20pub type Button = Input<'static>;
21
22/// Represents all the peripherals and pins available for the BBC micro:bit.
23pub struct Microbit {
24    /// LED matrix display
25    pub display: LedMatrix,
26    /// Button 'A'
27    pub btn_a: Button,
28    /// Button 'B'
29    pub btn_b: Button,
30    /// UART0 peripheral
31    pub uarte0: Peri<'static, UARTE0>,
32    /// UART1 peripheral
33    pub uarte1: Peri<'static, UARTE1>,
34    /// TIMER0 peripheral
35    pub timer0: Peri<'static, TIMER0>,
36    /// Speaker pin
37    pub speaker: Peri<'static, P0_00>,
38    /// Microphone pin
39    pub microphone: Peri<'static, P0_05>,
40    /// Microphone pin enable
41    pub micen: Peri<'static, P0_20>,
42
43    /// P0 connector pin
44    pub p0: Peri<'static, P0_02>,
45    /// P1 connector pin
46    pub p1: Peri<'static, P0_03>,
47    /// P2 connector pin
48    pub p2: Peri<'static, P0_04>,
49    /// P8 connector pin
50    pub p8: Peri<'static, P0_10>,
51    /// P9 connector pin
52    pub p9: Peri<'static, P0_09>,
53    /// P12 connector pin
54    pub p12: Peri<'static, P0_12>,
55    /// P13 connector pin
56    pub p13: Peri<'static, P0_17>,
57    /// P14 connector pin
58    pub p14: Peri<'static, P0_01>,
59    /// P15 connector pin
60    pub p15: Peri<'static, P0_13>,
61    /// P16 connector pin
62    pub p16: Peri<'static, P1_02>,
63    /// P19 connector pin
64    pub p19: Peri<'static, P0_26>,
65    /// P20 connector pin
66    pub p20: Peri<'static, P1_00>,
67
68    /// Internal I2C/TWI SCL to accelerometer & debug MCU
69    pub i2c_int_scl: Peri<'static, P0_08>,
70    /// Internal I2C/TWI SDA to accelerometer & debug MCU
71    pub i2c_int_sda: Peri<'static, P0_16>,
72
73    /// UART TX to debug MCU
74    pub uart_int_tx: Peri<'static, P1_08>,
75    /// UART RX to debug MCU
76    pub uart_int_rx: Peri<'static, P0_06>,
77
78    /// SPI0/I2C0 peripheral
79    pub twispi0: Peri<'static, TWISPI0>,
80    /// SPI1/I2C1 peripheral
81    pub twispi1: Peri<'static, TWISPI1>,
82    /// PWM0 peripheral
83    pub pwm0: Peri<'static, PWM0>,
84    /// PWM1 peripheral
85    pub pwm1: Peri<'static, PWM1>,
86    /// PWM2 peripheral
87    pub pwm2: Peri<'static, PWM2>,
88    /// PWM3 peripheral
89    pub pwm3: Peri<'static, PWM3>,
90    /// PPI channel 0
91    pub ppi_ch0: Peri<'static, PPI_CH0>,
92    /// PPI channel 1
93    pub ppi_ch1: Peri<'static, PPI_CH1>,
94    /// Random number generator
95    pub rng: Peri<'static, RNG>,
96    /// Analog digital converter
97    pub saadc: Peri<'static, SAADC>,
98    #[cfg(feature = "trouble")]
99    /// Bluetooth Low Energy peripheral
100    pub ble: ble::BleControllerBuilder<'static>,
101}
102
103impl Default for Microbit {
104    fn default() -> Self {
105        Self::new(Default::default())
106    }
107}
108
109impl Microbit {
110    /// Create a new instance based on HAL configuration
111    pub fn new(config: embassy_nrf::config::Config) -> Self {
112        let p = embassy_nrf::init(config);
113        // LED Matrix
114        let rows = [
115            output_pin(p.P0_21),
116            output_pin(p.P0_22),
117            output_pin(p.P0_15),
118            output_pin(p.P0_24),
119            output_pin(p.P0_19),
120        ];
121
122        let cols = [
123            output_pin(p.P0_28),
124            output_pin(p.P0_11),
125            output_pin(p.P0_31),
126            output_pin(p.P1_05),
127            output_pin(p.P0_30),
128        ];
129
130        Self {
131            display: LedMatrixDriver::new(rows, cols),
132            btn_a: Input::new(p.P0_14, Pull::None),
133            btn_b: Input::new(p.P0_23, Pull::None),
134            uarte0: p.UARTE0,
135            uarte1: p.UARTE1,
136            timer0: p.TIMER0,
137            speaker: p.P0_00,
138            microphone: p.P0_05,
139            micen: p.P0_20,
140            p0: p.P0_02,
141            p1: p.P0_03,
142            p2: p.P0_04,
143            p8: p.P0_10,
144            p9: p.P0_09,
145            p12: p.P0_12,
146            p13: p.P0_17,
147            p14: p.P0_01,
148            p15: p.P0_13,
149            p16: p.P1_02,
150            p19: p.P0_26,
151            p20: p.P1_00,
152            i2c_int_scl: p.P0_08,
153            i2c_int_sda: p.P0_16,
154            uart_int_tx: p.P1_08,
155            uart_int_rx: p.P0_06,
156            ppi_ch0: p.PPI_CH0,
157            ppi_ch1: p.PPI_CH1,
158            twispi0: p.TWISPI0,
159            twispi1: p.TWISPI1,
160            pwm0: p.PWM0,
161            pwm1: p.PWM1,
162            pwm2: p.PWM2,
163            pwm3: p.PWM3,
164            rng: p.RNG,
165            saadc: p.SAADC,
166            #[cfg(feature = "trouble")]
167            ble: ble::BleControllerBuilder::new(
168                p.RTC0, p.TEMP, p.PPI_CH17, p.PPI_CH18, p.PPI_CH19, p.PPI_CH20, p.PPI_CH21, p.PPI_CH22, p.PPI_CH23,
169                p.PPI_CH24, p.PPI_CH25, p.PPI_CH26, p.PPI_CH27, p.PPI_CH28, p.PPI_CH29, p.PPI_CH30, p.PPI_CH31,
170            ),
171        }
172    }
173}
174
175fn output_pin(pin: Peri<'static, impl Pin>) -> Output<'static> {
176    Output::new(pin, Level::Low, OutputDrive::Standard)
177}