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
16pub type LedMatrix = LedMatrixDriver<Output<'static>, 5, 5>;
18
19pub type Button = Input<'static>;
21
22pub struct Microbit {
24 pub display: LedMatrix,
26 pub btn_a: Button,
28 pub btn_b: Button,
30 pub uarte0: Peri<'static, UARTE0>,
32 pub uarte1: Peri<'static, UARTE1>,
34 pub timer0: Peri<'static, TIMER0>,
36 pub speaker: Peri<'static, P0_00>,
38 pub microphone: Peri<'static, P0_05>,
40 pub micen: Peri<'static, P0_20>,
42
43 pub p0: Peri<'static, P0_02>,
45 pub p1: Peri<'static, P0_03>,
47 pub p2: Peri<'static, P0_04>,
49 pub p8: Peri<'static, P0_10>,
51 pub p9: Peri<'static, P0_09>,
53 pub p12: Peri<'static, P0_12>,
55 pub p13: Peri<'static, P0_17>,
57 pub p14: Peri<'static, P0_01>,
59 pub p15: Peri<'static, P0_13>,
61 pub p16: Peri<'static, P1_02>,
63 pub p19: Peri<'static, P0_26>,
65 pub p20: Peri<'static, P1_00>,
67
68 pub i2c_int_scl: Peri<'static, P0_08>,
70 pub i2c_int_sda: Peri<'static, P0_16>,
72
73 pub uart_int_tx: Peri<'static, P1_08>,
75 pub uart_int_rx: Peri<'static, P0_06>,
77
78 pub twispi0: Peri<'static, TWISPI0>,
80 pub twispi1: Peri<'static, TWISPI1>,
82 pub pwm0: Peri<'static, PWM0>,
84 pub pwm1: Peri<'static, PWM1>,
86 pub pwm2: Peri<'static, PWM2>,
88 pub pwm3: Peri<'static, PWM3>,
90 pub ppi_ch0: Peri<'static, PPI_CH0>,
92 pub ppi_ch1: Peri<'static, PPI_CH1>,
94 pub rng: Peri<'static, RNG>,
96 pub saadc: Peri<'static, SAADC>,
98 #[cfg(feature = "trouble")]
99 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 pub fn new(config: embassy_nrf::config::Config) -> Self {
112 let p = embassy_nrf::init(config);
113 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}