-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.rs
More file actions
229 lines (208 loc) · 7.04 KB
/
mod.rs
File metadata and controls
229 lines (208 loc) · 7.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
pub mod buttons;
pub mod ws2812;
pub use buttons::Buttons;
use embedded_hal_bus::spi::ExclusiveDevice;
use esp_hal::{
Async,
Blocking,
assign_resources,
clock::{
Clock,
CpuClock,
},
dma::{
DmaRxBuf,
DmaTxBuf,
},
dma_buffers,
gpio::{
Level,
Output,
OutputConfig,
},
rmt::{
Rmt,
Tx,
TxChannelConfig,
TxChannelCreator as _,
},
rom,
spi::master::{
Spi,
SpiDmaBus,
},
time::Rate,
};
pub type AddressableLeds<'a> = ws2812::Ws2812<'a, 10>;
type DisplaySpiInterface = mipidsi::interface::SpiInterface<
'static,
ExclusiveDevice<SpiDmaBus<'static, Async>, Output<'static>, esp_hal::delay::Delay>,
Output<'static>,
>;
pub type Display = mipidsi::Display<DisplaySpiInterface, mipidsi::models::ST7789, Output<'static>>;
impl<'a> From<LedResources<'a>> for AddressableLeds<'a> {
fn from(led_resources: LedResources<'a>) -> Self {
AddressableLeds::new(led_resources.into())
}
}
/// StaticCell helper from the `static_cell` crate, MIT or Apache 2.0 license
macro_rules! mk_static {
($t:ty,$val:expr) => {{
static STATIC_CELL: static_cell::StaticCell<$t> = static_cell::StaticCell::new();
#[deny(unused_attributes)]
let x = STATIC_CELL.uninit().write($val);
x
}};
}
assign_resources! {
pub Resources<'d> {
display: DisplayResources<'d> {
dc: GPIO15,
rst: GPIO7,
sck: GPIO4,
cs: GPIO6,
miso: GPIO16,
mosi: GPIO5,
spi: SPI2,
dma: DMA_CH0,
},
backlight: BacklightResource<'d> {
led: GPIO19,
},
buttons: ButtonResources<'d> {
up: GPIO11,
down: GPIO1,
left: GPIO21,
right: GPIO2,
stick: GPIO14,
a: GPIO13,
b: GPIO38,
start: GPIO12,
select: GPIO45,
},
leds: LedResources<'d> {
power: GPIO17,
io: GPIO18,
rmt: RMT,
},
vibra: VibraResources<'d> {
motor: GPIO20,
},
boot: BootResources<'d> {
pin: GPIO0,
}
}
}
/// Minimal CPU clock switcher for ESP32-S3.
/// Needed here so we can step the CPU frequency through an intermediate
/// value before moving to the final 240MHz setting.
fn set_cpu_clock(cpu_clock_speed: CpuClock) {
let _ = esp_hal::peripherals::SYSTEM::regs()
.sysclk_conf()
.modify(|_, w| unsafe { w.soc_clk_sel().bits(1) });
let _ = esp_hal::peripherals::SYSTEM::regs()
.cpu_per_conf()
.modify(|_, w| unsafe {
let _ = w.pll_freq_sel().set_bit();
w.cpuperiod_sel().bits(match cpu_clock_speed {
CpuClock::_80MHz => 0,
CpuClock::_160MHz => 1,
CpuClock::_240MHz => 2,
_ => panic!("Unsupported CPU clock speed"),
})
});
rom::ets_update_cpu_frequency_rom(cpu_clock_speed.frequency().as_mhz());
}
#[must_use]
pub fn init() -> esp_hal::peripherals::Peripherals {
set_cpu_clock(esp_hal::clock::CpuClock::_160MHz);
let config = esp_hal::Config::default().with_cpu_clock(esp_hal::clock::CpuClock::max());
esp_hal::init(config)
}
impl From<esp_hal::peripherals::Peripherals> for Resources<'_> {
fn from(peripherals: esp_hal::peripherals::Peripherals) -> Self {
split_resources!(peripherals)
}
}
impl<'a> From<LedResources<'a>> for esp_hal::rmt::Channel<'a, Blocking, Tx> {
fn from(led_resources: LedResources<'a>) -> Self {
// Initialize GPIO pin for WS2812 LEDs
let _ws_power = Output::new(led_resources.power, Level::High, OutputConfig::default());
// Initialize RMT for WS2812 control (40MHz clock for precise timing)
let rmt = Rmt::new(led_resources.rmt, Rate::from_mhz(40)).unwrap();
let rmt_io = led_resources.io;
let tx_config = TxChannelConfig::default().with_clk_divider(1);
rmt.channel0.configure_tx(rmt_io, tx_config).unwrap()
}
}
impl<'a> From<LedResources<'a>> for esp_hal::rmt::Channel<'a, Async, Tx> {
fn from(led_resources: LedResources<'a>) -> Self {
// Initialize GPIO pin for WS2812 LEDs
let _ws_power = Output::new(led_resources.power, Level::High, OutputConfig::default());
// Initialize RMT for WS2812 control (40MHz clock for precise timing)
let rmt = Rmt::new(led_resources.rmt, Rate::from_mhz(40))
.unwrap()
.into_async();
let rmt_io = led_resources.io;
let tx_config = TxChannelConfig::default().with_clk_divider(1);
rmt.channel0.configure_tx(rmt_io, tx_config).unwrap()
}
}
impl<'a> From<DisplayResources<'a>>
for mipidsi::Display<
mipidsi::interface::SpiInterface<
'a,
embedded_hal_bus::spi::ExclusiveDevice<
esp_hal::spi::master::SpiDmaBus<'a, Async>,
Output<'a>,
esp_hal::delay::Delay,
>,
Output<'a>,
>,
mipidsi::models::ST7789,
Output<'a>,
>
{
fn from(resources: DisplayResources<'a>) -> Self {
let (rx_buffer, rx_descriptors, tx_buffer, tx_descriptors) = dma_buffers!(32000);
let dma_rx_buf = DmaRxBuf::new(rx_descriptors, rx_buffer).unwrap();
let dma_tx_buf = DmaTxBuf::new(tx_descriptors, tx_buffer).unwrap();
let mut delay = esp_hal::delay::Delay::new();
// or embassy_time::Delay?
let dc = Output::new(resources.dc, Level::Low, OutputConfig::default());
let mut rst = Output::new(resources.rst, Level::Low, OutputConfig::default());
rst.set_high();
let sck = resources.sck;
let miso = resources.miso;
let mosi = resources.mosi;
let cs = resources.cs;
let spi = Spi::new(
resources.spi,
esp_hal::spi::master::Config::default().with_frequency(Rate::from_mhz(80)),
)
.unwrap()
.with_sck(sck)
.with_mosi(mosi)
.with_miso(miso)
.with_dma(resources.dma)
.with_buffers(dma_rx_buf, dma_tx_buf)
.into_async();
let cs_output = Output::new(cs, Level::High, OutputConfig::default());
let spi_device =
embedded_hal_bus::spi::ExclusiveDevice::new(spi, cs_output, delay).unwrap();
// Define the display interface with no chip select
let buffer = mk_static!([u8; 512], [0_u8; 512]);
let di = mipidsi::interface::SpiInterface::new(spi_device, dc, buffer);
// Define the display from the display interface and initialize it
mipidsi::Builder::new(mipidsi::models::ST7789, di)
.reset_pin(rst)
.display_size(170, 320)
.invert_colors(mipidsi::options::ColorInversion::Inverted)
.orientation(
mipidsi::options::Orientation::new().rotate(mipidsi::options::Rotation::Deg90),
)
.display_offset(35, 0)
.init(&mut delay)
.unwrap()
}
}