@@ -48,22 +48,25 @@ use crate::peripherals::LPWR;
4848pub 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.
8689pub struct UlpCore < ' d > {
8790 _lp_core : crate :: peripherals:: ULP_RISCV_CORE < ' d > ,
88- _sleep_cycles : UlpCoreSleepCycles ,
8991}
9092
9193impl < ' 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-
136120fn ulp_stop ( ) {
137121 let rtc_cntl = LPWR :: regs ( ) ;
138122 rtc_cntl
@@ -222,15 +206,24 @@ fn ulp_run(wakeup_src: UlpCoreWakeupSource) {
222206}
223207
224208fn 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