-
Notifications
You must be signed in to change notification settings - Fork 172
/
ulp.rs
367 lines (305 loc) · 9.78 KB
/
ulp.rs
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub enum SleepTimer {
First = 0,
Second = 1,
Third = 2,
Fourth = 3,
Fifth = 4,
#[cfg(esp32s2)]
Sixth = 5,
}
impl Default for SleepTimer {
fn default() -> Self {
Self::First
}
}
#[cfg(any(
all(
not(esp_idf_version_major = "4"),
esp_idf_ulp_coproc_enabled,
esp_idf_ulp_coproc_type_fsm
),
all(esp32, esp_idf_esp32_ulp_coproc_enabled),
all(
esp32s2,
esp_idf_esp32s2_ulp_coproc_enabled,
not(esp_idf_esp32s2_ulp_coproc_riscv)
),
all(
esp32s3,
esp_idf_esp32s3_ulp_coproc_enabled,
not(esp_idf_esp32s3_ulp_coproc_riscv)
)
))]
#[derive(Copy, Clone, Debug)]
pub struct Word {
pc: u16,
value: u16,
}
#[cfg(any(
all(
not(esp_idf_version_major = "4"),
esp_idf_ulp_coproc_enabled,
esp_idf_ulp_coproc_type_fsm
),
all(esp32, esp_idf_esp32_ulp_coproc_enabled),
all(
esp32s2,
esp_idf_esp32s2_ulp_coproc_enabled,
not(esp_idf_esp32s2_ulp_coproc_riscv)
),
all(
esp32s3,
esp_idf_esp32s3_ulp_coproc_enabled,
not(esp_idf_esp32s3_ulp_coproc_riscv)
)
))]
impl Word {
pub fn pc(&self) -> u16 {
self.pc
}
pub fn value(&self) -> u16 {
self.value
}
pub fn is_modified(&self) -> bool {
self.pc != 0
}
}
#[cfg(any(
all(not(esp_idf_version_major = "4"), esp_idf_ulp_coproc_enabled),
all(esp32, esp_idf_esp32_ulp_coproc_enabled),
all(esp32s2, esp_idf_esp32s2_ulp_coproc_enabled),
all(esp32s3, esp_idf_esp32s3_ulp_coproc_enabled)
))]
pub struct UlpDriver<'d>(crate::peripheral::PeripheralRef<'d, ULP>);
#[cfg(any(
all(not(esp_idf_version_major = "4"), esp_idf_ulp_coproc_enabled),
all(esp32, esp_idf_esp32_ulp_coproc_enabled),
all(esp32s2, esp_idf_esp32s2_ulp_coproc_enabled),
all(esp32s3, esp_idf_esp32s3_ulp_coproc_enabled)
))]
unsafe impl<'d> Send for UlpDriver<'d> {}
#[cfg(any(
all(not(esp_idf_version_major = "4"), esp_idf_ulp_coproc_enabled),
all(esp32, esp_idf_esp32_ulp_coproc_enabled),
all(esp32s2, esp_idf_esp32s2_ulp_coproc_enabled),
all(esp32s3, esp_idf_esp32s3_ulp_coproc_enabled)
))]
impl<'d> UlpDriver<'d> {
pub fn new(
ulp: impl crate::peripheral::Peripheral<P = ULP> + 'd,
) -> Result<Self, esp_idf_sys::EspError> {
crate::into_ref!(ulp);
Ok(Self(ulp))
}
pub fn stop(&mut self) -> Result<(), esp_idf_sys::EspError> {
unsafe {
// disable ULP timer
core::ptr::write_volatile(
ULP::TIMER_REG,
core::ptr::read_volatile(ULP::TIMER_REG) & !ULP::TIMER_EN_BIT,
);
// wait for at least 1 RTC_SLOW_CLK cycle
esp_idf_sys::esp_rom_delay_us(10);
}
Ok(())
}
pub fn is_started(&self) -> Result<bool, esp_idf_sys::EspError> {
unsafe {
let enabled = (core::ptr::read_volatile(ULP::TIMER_REG) & ULP::TIMER_EN_BIT) != 0;
Ok(enabled)
}
}
pub fn set_sleep_period_default(
&mut self,
duration: core::time::Duration,
) -> Result<(), esp_idf_sys::EspError> {
self.set_sleep_period(Default::default(), duration)
}
pub fn set_sleep_period(
&mut self,
timer: SleepTimer,
duration: core::time::Duration,
) -> Result<(), esp_idf_sys::EspError> {
esp_idf_sys::esp!(unsafe {
esp_idf_sys::ulp_set_wakeup_period(timer as usize, duration.as_micros() as u32)
})?;
Ok(())
}
fn check_boundaries<T>(ptr: *const T) -> Result<(), esp_idf_sys::EspError> {
let ptr = ptr as *const u8;
let mem_start = ULP::MEM_START as *const u8;
unsafe {
if ptr < mem_start
|| ptr.offset(core::mem::size_of::<T>() as _) > mem_start.offset(ULP::MEM_SIZE as _)
{
return Err(esp_idf_sys::EspError::from_infallible::<
{ esp_idf_sys::ESP_ERR_INVALID_SIZE },
>());
}
}
Ok(())
}
}
#[cfg(any(
all(
not(esp_idf_version_major = "4"),
esp_idf_ulp_coproc_enabled,
esp_idf_ulp_coproc_type_fsm
),
all(esp32, esp_idf_esp32_ulp_coproc_enabled),
all(
esp32s2,
esp_idf_esp32s2_ulp_coproc_enabled,
not(esp_idf_esp32s2_ulp_coproc_riscv)
),
all(
esp32s3,
esp_idf_esp32s3_ulp_coproc_enabled,
not(esp_idf_esp32s3_ulp_coproc_riscv)
)
))]
impl<'d> UlpDriver<'d> {
pub unsafe fn load_at_ulp_address(
&mut self,
address: *mut core::ffi::c_void,
program: &[u8],
) -> Result<(), esp_idf_sys::EspError> {
let address: usize = core::mem::transmute(address);
if address % core::mem::size_of::<u32>() != 0 {
return Err(esp_idf_sys::EspError::from_infallible::<
{ esp_idf_sys::ESP_ERR_INVALID_ARG },
>());
}
if program.len() % core::mem::size_of::<u32>() != 0 {
return Err(esp_idf_sys::EspError::from_infallible::<
{ esp_idf_sys::ESP_ERR_INVALID_SIZE },
>());
}
esp_idf_sys::esp!(esp_idf_sys::ulp_load_binary(
(address / core::mem::size_of::<u32>()) as u32,
program.as_ptr(),
program.len() / core::mem::size_of::<u32>()
))?;
Ok(())
}
pub unsafe fn load(&mut self, program: &[u8]) -> Result<(), esp_idf_sys::EspError> {
self.load_at_ulp_address(ULP::MEM_START_ULP, program)
}
pub unsafe fn start(&mut self, address: *const u32) -> Result<(), esp_idf_sys::EspError> {
esp_idf_sys::esp!(esp_idf_sys::ulp_run(address as u32))?;
Ok(())
}
pub unsafe fn read_word(&self, ptr: *const u32) -> Result<Word, esp_idf_sys::EspError> {
Self::check_boundaries(ptr)?;
Self::check_alignment(ptr)?;
let value = core::ptr::read_volatile(ptr);
Ok(Word {
pc: ((value >> 16) & 0xffff_u32) as u16,
value: (value & 0xffff_u32) as u16,
})
}
pub unsafe fn write_word(
&self,
ptr: *mut u32,
value: u16,
) -> Result<(), esp_idf_sys::EspError> {
Self::check_boundaries(ptr)?;
Self::check_alignment(ptr)?;
core::ptr::write_volatile(ptr as *mut u32, value as u32);
Ok(())
}
pub unsafe fn swap_word(
&mut self,
ptr: *mut u32,
value: u16,
) -> Result<Word, esp_idf_sys::EspError> {
let old_value = self.read_word(ptr)?;
self.write_word(ptr, value)?;
Ok(old_value)
}
fn check_alignment<T>(ptr: *const T) -> Result<(), esp_idf_sys::EspError> {
let ptr_usize: usize = unsafe { core::mem::transmute(ptr) };
if ptr_usize % core::mem::size_of::<T>() != 0 {
return Err(esp_idf_sys::EspError::from_infallible::<
{ esp_idf_sys::ESP_ERR_INVALID_SIZE },
>());
}
Ok(())
}
}
#[cfg(any(
all(
not(esp_idf_version_major = "4"),
esp_idf_ulp_coproc_enabled,
not(esp_idf_ulp_coproc_type_fsm)
),
all(
esp32s2,
esp_idf_esp32s2_ulp_coproc_enabled,
esp_idf_esp32s2_ulp_coproc_riscv
),
all(
esp32s3,
esp_idf_esp32s3_ulp_coproc_enabled,
esp_idf_esp32s3_ulp_coproc_riscv
),
))]
impl<'d> UlpDriver<'d> {
pub unsafe fn load(&mut self, program: &[u8]) -> Result<(), esp_idf_sys::EspError> {
esp_idf_sys::esp!(esp_idf_sys::ulp_riscv_load_binary(
program.as_ptr(),
program.len() as _
))?;
Ok(())
}
pub unsafe fn start(&mut self) -> Result<(), esp_idf_sys::EspError> {
esp_idf_sys::esp!(esp_idf_sys::ulp_riscv_run())?;
Ok(())
}
pub unsafe fn read_var<T>(&self, src: *const T) -> Result<T, esp_idf_sys::EspError> {
Self::check_boundaries(src)?;
Ok(core::ptr::read_volatile(src))
}
pub unsafe fn write_var<T>(&self, dst: *mut T, value: T) -> Result<(), esp_idf_sys::EspError> {
Self::check_boundaries(dst)?;
core::ptr::write_volatile(dst, value);
Ok(())
}
pub unsafe fn swap_var<T>(
&mut self,
ptr: *mut T,
value: T,
) -> Result<T, esp_idf_sys::EspError> {
let old_value = self.read_var(ptr)?;
self.write_var(ptr, value)?;
Ok(old_value)
}
}
crate::impl_peripheral!(ULP);
#[cfg(any(
all(not(esp_idf_version_major = "4"), esp_idf_ulp_coproc_enabled),
all(esp32, esp_idf_esp32_ulp_coproc_enabled),
all(esp32s2, esp_idf_esp32s2_ulp_coproc_enabled),
all(esp32s3, esp_idf_esp32s3_ulp_coproc_enabled)
))]
impl ULP {
const RTC_SLOW_MEM: u32 = 0x5000_0000_u32;
pub const MEM_START_ULP: *mut core::ffi::c_void = 0_u32 as _;
pub const MEM_START: *mut core::ffi::c_void = Self::RTC_SLOW_MEM as _;
#[cfg(all(esp32, esp_idf_version_major = "4"))]
pub const MEM_SIZE: usize = esp_idf_sys::CONFIG_ESP32_ULP_COPROC_RESERVE_MEM as _;
#[cfg(all(esp32s2, esp_idf_version_major = "4"))]
pub const MEM_SIZE: usize = esp_idf_sys::CONFIG_ESP32S2_ULP_COPROC_RESERVE_MEM as _;
#[cfg(all(esp32s3, esp_idf_version_major = "4"))]
pub const MEM_SIZE: usize = esp_idf_sys::CONFIG_ESP32S3_ULP_COPROC_RESERVE_MEM as _;
#[cfg(not(esp_idf_version_major = "4"))]
pub const MEM_SIZE: usize = esp_idf_sys::CONFIG_ULP_COPROC_RESERVE_MEM as _;
#[cfg(esp32)]
const TIMER_REG: *mut u32 = esp_idf_sys::RTC_CNTL_STATE0_REG as _;
#[cfg(any(esp32s2, esp32s3))]
const TIMER_REG: *mut u32 = esp_idf_sys::RTC_CNTL_ULP_CP_TIMER_REG as _;
#[cfg(any(esp32, esp32s2, esp32s3))]
const TIMER_EN_BIT: u32 =
esp_idf_sys::RTC_CNTL_ULP_CP_SLP_TIMER_EN_V << esp_idf_sys::RTC_CNTL_ULP_CP_SLP_TIMER_EN_S;
}