Skip to content

Commit ff7ee41

Browse files
committed
feat: better peripherals init
1 parent 3403a41 commit ff7ee41

File tree

2 files changed

+268
-201
lines changed

2 files changed

+268
-201
lines changed

src/board.rs

Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
use crate::utils::stackmat::{DEC_DIGITS, DOT_MOD};
2+
use adv_shift_registers::wrappers::ShifterValueRange;
3+
use anyhow::Result;
4+
use esp_hal::{
5+
gpio::{AnyPin, GpioPin, Input, InputConfig, Level, Output, Pin, Pull},
6+
peripherals::{Peripherals, ADC1, BT, RADIO_CLK, SPI2, TIMG0, TIMG1, UART1, WIFI},
7+
rng::Rng,
8+
timer::timg::TimerGroup,
9+
};
10+
11+
pub struct Board {
12+
// peripherals
13+
pub timg0: TimerGroup<TIMG0>,
14+
pub timg1: TimerGroup<TIMG1>,
15+
pub rng: Rng,
16+
pub uart1: UART1,
17+
pub spi2: SPI2,
18+
pub adc1: ADC1,
19+
pub radio_clk: RADIO_CLK,
20+
pub wifi: WIFI,
21+
pub bt: BT,
22+
#[cfg(feature = "esp32c3")]
23+
pub spi_dma: esp_hal::dma::DmaChannel0,
24+
#[cfg(feature = "esp32")]
25+
pub spi_dma: esp_hal::dma::Spi2DmaChannel,
26+
27+
// spi
28+
pub miso: AnyPin,
29+
pub mosi: AnyPin,
30+
pub sck: AnyPin,
31+
#[cfg(feature = "esp32c3")]
32+
pub cs: adv_shift_registers::wrappers::ShifterPin,
33+
#[cfg(feature = "esp32")]
34+
pub cs: Output<'static>,
35+
36+
pub stackmat_rx: AnyPin,
37+
38+
#[cfg(feature = "esp32c3")]
39+
pub battery: GpioPin<2>,
40+
#[cfg(feature = "esp32")]
41+
pub battery: GpioPin<34>,
42+
43+
#[cfg(feature = "esp32c3")]
44+
pub button_input: Input<'static>,
45+
#[cfg(feature = "esp32")]
46+
pub button_input: [Input<'static>; 4],
47+
48+
pub digits_shifters: ShifterValueRange,
49+
50+
#[cfg(feature = "esp32c3")]
51+
pub buttons_shifter: adv_shift_registers::wrappers::ShifterValue,
52+
#[cfg(feature = "esp32c3")]
53+
pub lcd: adv_shift_registers::wrappers::ShifterValue,
54+
#[cfg(feature = "esp32")]
55+
pub lcd: esp_hal::i2c::master::I2c<'static, esp_hal::Blocking>,
56+
}
57+
58+
#[cfg(feature = "esp32c3")]
59+
impl Board {
60+
pub fn init(peripherals: Peripherals) -> Result<Board> {
61+
use embedded_hal::digital::OutputPin;
62+
63+
esp_alloc::heap_allocator!(size: 120 * 1024);
64+
65+
let timg0 = TimerGroup::new(peripherals.TIMG0);
66+
let timg1 = TimerGroup::new(peripherals.TIMG1);
67+
let rng = Rng::new(peripherals.RNG);
68+
let uart1 = peripherals.UART1;
69+
let spi2 = peripherals.SPI2;
70+
let spi_dma = peripherals.DMA_CH0;
71+
let adc1 = peripherals.ADC1;
72+
let radio_clk = peripherals.RADIO_CLK;
73+
let wifi = peripherals.WIFI;
74+
let bt = peripherals.BT;
75+
76+
let sck = peripherals.GPIO4.degrade();
77+
let miso = peripherals.GPIO5.degrade();
78+
let mosi = peripherals.GPIO6.degrade();
79+
let battery = peripherals.GPIO2;
80+
let stackmat_rx = peripherals.GPIO20.degrade();
81+
82+
let button_input = Input::new(
83+
peripherals.GPIO3,
84+
InputConfig::default().with_pull(Pull::Down),
85+
);
86+
87+
let shifter_data_pin = Output::new(peripherals.GPIO10, Level::Low, Default::default());
88+
let shifter_latch_pin = Output::new(peripherals.GPIO1, Level::Low, Default::default());
89+
let shifter_clk_pin = Output::new(peripherals.GPIO21, Level::Low, Default::default());
90+
91+
let adv_shift_reg = adv_shift_registers::AdvancedShiftRegister::<8, _>::new(
92+
shifter_data_pin,
93+
shifter_clk_pin,
94+
shifter_latch_pin,
95+
0,
96+
);
97+
let adv_shift_reg = alloc::boxed::Box::new(adv_shift_reg);
98+
let adv_shift_reg = alloc::boxed::Box::leak(adv_shift_reg);
99+
100+
let mut backlight = adv_shift_reg.get_pin_mut(1, 1, false);
101+
_ = backlight.set_high();
102+
103+
let buttons_shifter = adv_shift_reg.get_shifter_mut(0);
104+
let lcd = adv_shift_reg.get_shifter_mut(1);
105+
let digits_shifters = adv_shift_reg.get_shifter_range_mut(2..8);
106+
digits_shifters.set_data(&[!DEC_DIGITS[8] ^ DOT_MOD; 6]);
107+
108+
let mut cs = adv_shift_reg.get_pin_mut(1, 0, true);
109+
_ = cs.set_high();
110+
111+
Ok(Board {
112+
timg0,
113+
timg1,
114+
rng,
115+
uart1,
116+
spi2,
117+
spi_dma,
118+
adc1,
119+
radio_clk,
120+
wifi,
121+
bt,
122+
123+
miso,
124+
mosi,
125+
sck,
126+
cs,
127+
128+
battery,
129+
stackmat_rx,
130+
button_input,
131+
132+
buttons_shifter,
133+
digits_shifters,
134+
lcd,
135+
})
136+
}
137+
}
138+
139+
#[cfg(feature = "esp32")]
140+
impl Board {
141+
pub fn init(peripherals: Peripherals) -> Result<Board> {
142+
use embedded_hal::digital::OutputPin;
143+
144+
let timg0 = TimerGroup::new(peripherals.TIMG0);
145+
let timg1 = TimerGroup::new(peripherals.TIMG1);
146+
let rng = Rng::new(peripherals.RNG);
147+
let uart1 = peripherals.UART1;
148+
let spi2 = peripherals.SPI2;
149+
let spi_dma = peripherals.DMA_SPI2;
150+
let adc1 = peripherals.ADC1;
151+
let radio_clk = peripherals.RADIO_CLK;
152+
let wifi = peripherals.WIFI;
153+
let bt = peripherals.BT;
154+
155+
let sck = peripherals.GPIO18.degrade();
156+
let miso = peripherals.GPIO19.degrade();
157+
let mosi = peripherals.GPIO23.degrade();
158+
let cs = Output::new(peripherals.GPIO5, Level::High, Default::default());
159+
let battery = peripherals.GPIO34;
160+
let stackmat_rx = peripherals.GPIO4.degrade();
161+
162+
let button1 = Input::new(
163+
peripherals.GPIO27,
164+
InputConfig::default().with_pull(Pull::Up),
165+
);
166+
let button2 = Input::new(
167+
peripherals.GPIO26,
168+
InputConfig::default().with_pull(Pull::Up),
169+
);
170+
let button3 = Input::new(
171+
peripherals.GPIO33,
172+
InputConfig::default().with_pull(Pull::Up),
173+
);
174+
let button4 = Input::new(
175+
peripherals.GPIO32,
176+
InputConfig::default().with_pull(Pull::Up),
177+
);
178+
179+
let shifter_data_pin = Output::new(peripherals.GPIO16, Level::Low, Default::default());
180+
let shifter_latch_pin = Output::new(peripherals.GPIO12, Level::Low, Default::default());
181+
let shifter_clk_pin = Output::new(peripherals.GPIO17, Level::Low, Default::default());
182+
183+
let adv_shift_reg = adv_shift_registers::AdvancedShiftRegister::<8, _>::new(
184+
shifter_data_pin,
185+
shifter_clk_pin,
186+
shifter_latch_pin,
187+
0,
188+
);
189+
let adv_shift_reg = alloc::boxed::Box::new(adv_shift_reg);
190+
let adv_shift_reg = alloc::boxed::Box::leak(adv_shift_reg);
191+
192+
let mut backlight = adv_shift_reg.get_pin_mut(1, 1, false);
193+
_ = backlight.set_high();
194+
195+
let digits_shifters = adv_shift_reg.get_shifter_range_mut(0..6);
196+
digits_shifters.set_data(&[!DEC_DIGITS[8] ^ DOT_MOD; 6]);
197+
198+
let lcd = esp_hal::i2c::master::I2c::new(
199+
peripherals.I2C0,
200+
esp_hal::i2c::master::Config::default()
201+
.with_frequency(esp_hal::time::Rate::from_khz(100))
202+
.with_timeout(esp_hal::i2c::master::BusTimeout::Maximum),
203+
)?
204+
.with_sda(peripherals.GPIO21)
205+
.with_scl(peripherals.GPIO22);
206+
207+
Ok(Board {
208+
timg0,
209+
timg1,
210+
rng,
211+
uart1,
212+
spi2,
213+
spi_dma,
214+
adc1,
215+
radio_clk,
216+
wifi,
217+
bt,
218+
219+
miso,
220+
mosi,
221+
sck,
222+
cs,
223+
224+
battery,
225+
stackmat_rx,
226+
button_input: [button1, button2, button3, button4],
227+
228+
lcd,
229+
digits_shifters,
230+
})
231+
}
232+
}

0 commit comments

Comments
 (0)