Skip to content

Commit fbd6fd1

Browse files
Moved sleep timer wake-up into the existing UlpCoreWakeupSource enum, which allows ULP Timer to be disabled, and preserves backwards-compatibility
1 parent fbe774b commit fbd6fd1

File tree

2 files changed

+39
-51
lines changed

2 files changed

+39
-51
lines changed

esp-hal/src/soc/esp32s2/ulp_core.rs

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,25 @@ use crate::peripherals::LPWR;
4848
pub enum UlpCoreWakeupSource {
4949
/// Wakeup source from the HP (High Performance) CPU.
5050
HpCpu,
51+
/// Wakeup after the ULP Timer has elapsed.
52+
/// The actual period between wake-ups is affected by the runtime duration of the ULP program.
53+
Timer(UlpCoreTimerCycles),
5154
}
5255

53-
/// Configures `RTC_CNTL_ULP_CP_TIMER_1_REG` to set sleep cycles for ULP coprocessor timer.
56+
/// ULP Timer cycles are clocked at a rate of approximately 8MHz / 32768 = ~244 Hz.
5457
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
55-
pub struct UlpCoreSleepCycles {
58+
pub struct UlpCoreTimerCycles {
5659
cycles: u32,
5760
}
58-
impl UlpCoreSleepCycles {
59-
/// Creates a new sleep cycle configuration.
61+
impl UlpCoreTimerCycles {
62+
/// Creates a new Ulp Timer cycle count configuration.
6063
/// ## Panics
6164
///
6265
/// Panics if the cycles value is outside of the value range (0 ..= 0xFFFFFF).
6366
pub const fn new(cycles: u32) -> Self {
6467
::core::assert!(
6568
cycles <= 0xFFFFFF,
66-
"`RTC_CNTL_ULP_CP_TIMER_SLP_CYCLE` sleep cycles must be between 0 and 0xFFFFFF (inclusive)."
69+
"ULP Timer cycles must be between 0 and 0xFFFFFF (inclusive)."
6770
);
6871
Self { cycles }
6972
}
@@ -74,7 +77,7 @@ impl UlpCoreSleepCycles {
7477
self.cycles()
7578
}
7679
}
77-
impl Default for UlpCoreSleepCycles {
80+
impl Default for UlpCoreTimerCycles {
7881
fn default() -> Self {
7982
// ESP32-S2 Technical Reference Manual. Register 1.2. RTC_CNTL_ULP_CP_TIMER_1_REG (0x0130)
8083
// Field RTC_CNTL_ULP_CP_TIMER_SLP_CYCLE has a default value of 200 cycles.
@@ -102,15 +105,6 @@ impl<'d> UlpCore<'d> {
102105
}
103106
}
104107

105-
/// Sets the number of ULP Timer cycles to wait after the ULP halts, before starting it again.
106-
/// ULP Timer cycles are clocked at a rate of approximately 8MHz / 32768 = ~244 Hz.
107-
/// The actual period between wake-ups is affected by the runtime duration of the ULP program.
108-
pub fn with_sleep_cycles(self, cycles: UlpCoreSleepCycles) -> Self {
109-
let mut x = self;
110-
x._sleep_cycles = cycles;
111-
x
112-
}
113-
114108
// currently stopping the ULP doesn't work (while following the procedures
115109
// outlined in the TRM) - so don't offer this function for now
116110
//
@@ -125,13 +119,6 @@ impl<'d> UlpCore<'d> {
125119
}
126120
}
127121

128-
fn ulp_set_wakeup_period(sleep_cycles: UlpCoreSleepCycles) {
129-
let cycles = sleep_cycles.value() << 8;
130-
LPWR::regs()
131-
.ulp_cp_timer_1()
132-
.write(|w| unsafe { w.ulp_cp_timer_slp_cycle().bits(cycles) });
133-
}
134-
135122
#[allow(unused)] // TODO: remove cfg when implementation is corrected
136123
fn ulp_stop() {
137124
LPWR::regs()
@@ -193,7 +180,15 @@ fn ulp_run(wakeup_src: UlpCoreWakeupSource) {
193180
fn ulp_config_wakeup_source(wakeup_src: UlpCoreWakeupSource) {
194181
match wakeup_src {
195182
UlpCoreWakeupSource::HpCpu => {
196-
// use timer to wake up
183+
// only wake-up when the HpCpu calls .run()
184+
},
185+
UlpCoreWakeupSource::Timer(sleep_cycles) => {
186+
// configure timer duration
187+
let cycles = sleep_cycles.value() << 8;
188+
LPWR::regs()
189+
.ulp_cp_timer_1()
190+
.write(|w| unsafe { w.ulp_cp_timer_slp_cycle().bits(cycles) });
191+
// enable the timer
197192
LPWR::regs()
198193
.ulp_cp_ctrl()
199194
.modify(|_, w| w.ulp_cp_force_start_top().clear_bit());

esp-hal/src/soc/esp32s3/ulp_core.rs

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,25 @@ use crate::peripherals::LPWR;
4848
pub enum UlpCoreWakeupSource {
4949
/// Wakeup source from the HP (High Performance) CPU.
5050
HpCpu,
51+
/// Wakeup after the ULP Timer has elapsed.
52+
/// The actual period between wake-ups is affected by the runtime duration of the ULP program.
53+
Timer(UlpCoreTimerCycles),
5154
}
5255

53-
/// Configures `RTC_CNTL_ULP_CP_TIMER_1_REG` to set sleep cycles for ULP coprocessor timer.
56+
/// ULP Timer cycles are clocked at a rate of approximately 17.5MHz / 32768 = ~534 Hz.
5457
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
55-
pub struct UlpCoreSleepCycles {
58+
pub struct UlpCoreTimerCycles {
5659
cycles: u32,
5760
}
58-
impl UlpCoreSleepCycles {
59-
/// Creates a new sleep cycle configuration.
61+
impl UlpCoreTimerCycles {
62+
/// Creates a new Ulp Timer cycle count configuration.
6063
/// ## Panics
6164
///
6265
/// Panics if the cycles value is outside of the value range (0 ..= 0xFFFFFF).
6366
pub const fn new(cycles: u32) -> Self {
6467
::core::assert!(
6568
cycles <= 0xFFFFFF,
66-
"`RTC_CNTL_ULP_CP_TIMER_SLP_CYCLE` sleep cycles must be between 0 and 0xFFFFFF (inclusive)."
69+
"ULP Timer cycles must be between 0 and 0xFFFFFF (inclusive)."
6770
);
6871
Self { cycles }
6972
}
@@ -74,7 +77,7 @@ impl UlpCoreSleepCycles {
7477
self.cycles()
7578
}
7679
}
77-
impl Default for UlpCoreSleepCycles {
80+
impl Default for UlpCoreTimerCycles {
7881
fn default() -> Self {
7982
// ESP32-S3 Technical Reference Manual. Register 2.2. RTC_CNTL_ULP_CP_TIMER_1_REG (0x0134)
8083
// Field RTC_CNTL_ULP_CP_TIMER_SLP_CYCLE has a default value of 200 cycles.
@@ -85,15 +88,13 @@ impl Default for UlpCoreSleepCycles {
8588
/// Structure representing the ULP (Ultra-Low Power) core.
8689
pub struct UlpCore<'d> {
8790
_lp_core: crate::peripherals::ULP_RISCV_CORE<'d>,
88-
_sleep_cycles: UlpCoreSleepCycles,
8991
}
9092

9193
impl<'d> UlpCore<'d> {
9294
/// Creates a new instance of the `UlpCore` struct.
9395
pub fn new(lp_core: crate::peripherals::ULP_RISCV_CORE<'d>) -> Self {
9496
let mut this = Self {
9597
_lp_core: lp_core,
96-
_sleep_cycles: Default::default(),
9798
};
9899
this.stop();
99100

@@ -105,34 +106,17 @@ impl<'d> UlpCore<'d> {
105106
this
106107
}
107108

108-
/// Sets the number of ULP Timer cycles to wait after the ULP halts, before starting it again.
109-
/// ULP Timer cycles are clocked at a rate of approximately 17.5MHz / 32768 = ~534 Hz.
110-
/// The actual period between wake-ups is affected by the runtime duration of the ULP program.
111-
pub fn with_sleep_cycles(self, cycles: UlpCoreSleepCycles) -> Self {
112-
let mut x = self;
113-
x._sleep_cycles = cycles;
114-
x
115-
}
116-
117109
/// Stops the ULP core.
118110
pub fn stop(&mut self) {
119111
ulp_stop();
120112
}
121113

122114
/// Runs the ULP core with the specified wakeup source.
123115
pub fn run(&mut self, wakeup_src: UlpCoreWakeupSource) {
124-
ulp_set_wakeup_period(self._sleep_cycles);
125116
ulp_run(wakeup_src);
126117
}
127118
}
128119

129-
fn ulp_set_wakeup_period(sleep_cycles: UlpCoreSleepCycles) {
130-
let cycles = sleep_cycles.value() << 8;
131-
LPWR::regs()
132-
.ulp_cp_timer_1()
133-
.write(|w| unsafe { w.ulp_cp_timer_slp_cycle().bits(cycles) });
134-
}
135-
136120
fn ulp_stop() {
137121
let rtc_cntl = LPWR::regs();
138122
rtc_cntl
@@ -222,15 +206,24 @@ fn ulp_run(wakeup_src: UlpCoreWakeupSource) {
222206
}
223207

224208
fn ulp_config_wakeup_source(wakeup_src: UlpCoreWakeupSource) {
209+
// ESP-IDF source: https://github.com/espressif/esp-idf/blob/12f36a021f511cd4de41d3fffff146c5336ac1e7/components/ulp/ulp_riscv/ulp_riscv.c#L87
225210
match wakeup_src {
226211
UlpCoreWakeupSource::HpCpu => {
227-
// use timer to wake up
212+
// only wake-up when the HpCpu calls .run()
213+
},
214+
UlpCoreWakeupSource::Timer(sleep_cycles) => {
215+
// configure timer duration
216+
let cycles = sleep_cycles.value() << 8;
217+
LPWR::regs()
218+
.ulp_cp_timer_1()
219+
.write(|w| unsafe { w.ulp_cp_timer_slp_cycle().bits(cycles) });
220+
// enable the timer
228221
LPWR::regs()
229222
.ulp_cp_ctrl()
230223
.modify(|_, w| w.ulp_cp_force_start_top().clear_bit());
231224
LPWR::regs()
232225
.ulp_cp_timer()
233226
.modify(|_, w| w.ulp_cp_slp_timer_en().set_bit());
234-
}
227+
},
235228
}
236-
}
229+
}

0 commit comments

Comments
 (0)