cortex-m icon indicating copy to clipboard operation
cortex-m copied to clipboard

Delay should sleep the CPU

Open jonathanpallant opened this issue 2 years ago • 5 comments

Rather than busy-wait the CPU, the delay implementation should set the systick to generate an interrupt on overflow, and sleep with a wfi. This would reduce power consumption.

You might need to put it behind a flag because you'd have to replace DefaultHandler_ for systick with something that didn't loop forever...

jonathanpallant avatar Jan 11 '24 15:01 jonathanpallant

SysTick can not be an option because user can use it, and overriding duration and systick registers may bring unexpected results to user's program And the most basic usage of SysTick which is executing handler once in N cycles is expected to continue executing while waiting for delay.

skibon02 avatar Jan 18 '24 20:01 skibon02

That's not possible because the cortex_m::Delay struct takes ownership of the SYSTICK singleton, and the Delay::delay_us method already changes all the registers in order to perform the sleep. The only thing we'd need to change is setting the NVIC bits to enable interrupt on Systick overflow, and I think that can be done atomically without needing a &mut cortex_m::NVIC (or even &cortex_m::NVIC), so the API to the Delay object doesn't even need to change.

jonathanpallant avatar Jan 19 '24 09:01 jonathanpallant

Oh, i see I didn't know we have SysTick based delay already Using sleep mode is a good point

skibon02 avatar Jan 22 '24 09:01 skibon02