In the LPM demo code (MQX3.8 - Basic Examples - lowpower) the code that changes low power mode works correctly and takes around 2 mSec to change from RUN to WAIT or from WAIT to RUN. (changing the clock config and operating mode). When I port the code to my project, it works correctly however switching to LPM_OPERATION_MODE_WAIT (line 2) takes about 150mSec (way too long). Narrowed it down to one line of assembly code within the Cpu_SetOperationMode function in cpu.c, that is the WFI (wait for interrupt instruction). My project and the demo use the same BSP build and same hardware. Note: modes referred to here are MQX modes, i.e. using MQX_WAIT which means Kinetis_VLPR, not using MQX_SLEEP which corresponds to Kinetis_WAIT.
Below is an oversimplified view of code that runs at two speeds.
// full speed code here
_lpm_set_clock_configuration(BSP_CLOCK_CONFIGURATION_2MHZ); // line 1, takes about 0.7msec
_lpm_set_operation_mode2(LPM_OPERATION_MODE_WAIT); // line 2, takes 1.7msec in demo, 150mSec ported to my project
// low power slower code here
_lpm_set_operation_mode(LPM_OPERATION_MODE_RUN)); // line 3 , takes 0.7 msec
_lpm_set_clock_configuration(BSP_CLOCK_CONFIGURATION_DEFAULT)); // line 4, takes 0.8msec
Why might the switch to LPM_OPERATION_MODE_WAIT take so long when it hits the WFI instruction, and what interrupt releases it in the demo code?
WFI is needed to invoke Kinetis_WAIT (=MQX_SLEEP) and Kinetis_STOP (=MQX_STOP) modes for the chip, but why does the MQX LPM call WFI when transitioning to Kinetis_VLPR (=MQX_WAIT)? No need to hang around and wait for an interrupt.