cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L4 entering and exiting Sleep mode

1991red1991
Associate III

I can’t understand how to enter and exit sleep mode. Can someone share an example of how to enter and exit sleep mode on the L4 series? I use ADC + DMA + TIM and I want to go to sleep mode, and after the interruption comes from DMA, I want the microcontroller to wake up, process the result and go back to sleep. How can I do this? There is no clear instruction in the Datasheet ...

6 REPLIES 6
Piranha
Chief II

If you really mean sleep mode, then that is very simple:

__DSB();
__WFI();

For more information read PM0214 section 2.5.

    /* Configure low-power mode */
    SCB->SCR &= ~( SCB_SCR_SLEEPDEEP_Msk );  // low-power mode = sleep mode
    SCB->SCR |= SCB_SCR_SLEEPONEXIT_Msk;     // reenter low-power mode after ISR
 
    /* Ensure Flash memory stays on */
    FLASH->ACR &= ~FLASH_ACR_SLEEP_PD;
    __WFI();  // enter low-power mode

I wrote this code after starting the timer. I thought that when an interrupt from the timer comes, the processor itself will wake from sleep mode. Now I can’t flash the microcontroller. What should I do?

I do not understand what code do I need to use to send processes to the sleep mode?

Under what condition does the processor wake up? When will the interruption come? Do I need to use any code after an interrupt arrives to return the processor to run mode?

I correctly understood that in this mode, the microcontroller wakes up performs an interruption and falls asleep again, and so on?

1991red1991
Associate III

Now in STM32CubeMX, he created an empty project on stm32l431cb, turned on only external 8MHz quartz and SWD. Enabled ART Accelerator by enabling Prefetch, Instruction cache, Data cache. It does not have a separate enable bit, as I understand it, it is turned on by turning on cache and prefetch. Set PWR to Range 2. If you believe the power consumption calculator: consumption should be 903μA.0690X00000AsNYnQAN.png

On the multimeter, I see 1.41mA (tested on several, plus an attorney), if you consider that somehow ART Accelerator is not turned on, then the consumption should still be 1.14mA:

0690X00000AsNYsQAN.png

On the diagram, it wasn’t worth anything except the controller and its bindings. Why the consumption values do not coincide very much with the calculated and indicated in the Datashits? What am I doing wrong?

Piranha
Chief II

By default you don't need clearing those SLEEPDEEP and SLEEP_PD bits as they already are in that state after MCU reset. SLEEPONEXIT bit enables mode where CPU doesn't execute main (non-interrupt) code at all - that is not what is typically needed. Typically you put the CPU in sleep mode, then some interrupt wakes up the CPU, CPU enters interrupt mode, in interrupt code you set some flags, the CPU returns from interrupt to main code, main code checks and processes the flags, and then main code puts the CPU again in sleep mode.

To enable debugging in sleep mode, set the DBG_SLEEP bit in DBGMCU_CR register, but bear in mind that debugger interface consumes few mA.

To flash the MCU, which has entered sleep mode with debug interface turned off (default setting) set "connect under reset" in your debugger configuration or you can try simply manually pressing Reset button shortly at the moment when debugger is connecting to CPU.

"By default you don't need clearing those SLEEPDEEP and SLEEP_PD bits as they already are in that state after MCU reset. SLEEPONEXIT bit enables mode where CPU doesn't execute main (non-interrupt) code at all - that is not what is typically needed. Typically you put the CPU in sleep mode, then some interrupt wakes up the CPU, CPU enters interrupt mode, in interrupt code you set some flags, the CPU returns from interrupt to main code, main code checks and processes the flags, and then main code puts the CPU again in sleep mode."

If the SLEEPONEXIT bit is set to 1, then does the interrupt awaken the processor and put it into run mode? And as soon as the program exits the interrupt, does the processor again enter the sleep mode?

That is, when an interrupt from DMA comes, can I process the value from the ADC in the interrupt, and as soon as the interrupt is over the processor itself goes into the sleep mode?

"To enable debugging in sleep mode, set the DBG_SLEEP bit in DBGMCU_CR register, but bear in mind that debugger interface consumes few mA."

Got it, thanks for the information.

"To flash the MCU, which has entered sleep mode with debug interface turned off (default setting) set "connect under reset" in your debugger configuration or you can try simply manually pressing Reset button shortly at the moment when debugger is connecting to CPU."

Understood, thanks!

P.S. Could you still help with my previous message? I can’t understand why my current consumption is bigger than the calculated value? In run mode, I connected only the external quartz resonator and the SWD interface, and the consumption was 1.41mA but should be ~ 900uA.