# What's causing this power spike in STM32 low power mode

I'm using an STM32L073RZ on a bare board with just the CPU and decoupling caps. I'm powering the board straight from an Otii Arc and measuring current consumption. I'm running MbedOS 5.11.2.

When I call the sleep() function the CPU goes into a low power mode, with an occasional 5mA spike in current consumption approximately every second, see the image below:

What is the cause of this? I'm attempting to place the CPU into STOP mode with an RTC running - this should draw, according to the datasheet, 1µA current.

Further to this, how can I tell which low power mode the sleep() function has chosen? I'm trying to stay away from the HAL because I've had many issues configuring interrupts and the like.

For completeness, here's the code that's running on the board:

#include "mbed.h"

int main() {
sleep();
}

• I would guess your mcu is waking up once a second for some reason – Colin Mar 15 at 13:58
• It seems quite plausible that the RTC is generating an interrupt once per second. Put a breakpoint on the RTC interrupt handler. – Jeremy Mar 15 at 14:41
• @Jeremy thanks for the suggestion, doesn't appear to be an RTC interrupt as the handler doesn't trigger if I include it. – Adam Mitchell Mar 15 at 14:47
• @Adam - Even if the interrupt is masked, if the RTC is active it may be bringing elements out of low-power mode anyway. – Jeremy Mar 15 at 14:49
• @Jeremy that's a fair suggestion. I think I'll have to get to grips with the HAL so I can be absolutely sure which timers are running, which power mode the CPU is in etc.. Thanks! – Adam Mitchell Mar 15 at 14:53

I can't speak about mbed specifically, but the general idea is that sleep() causes the execution of the current process to pause for some number of seconds, or indefinitely if no argument (equivalent to an argument of 0) is given.

In a multiprocess environment, that means that it simply yields the CPU to other processes. If there are no other processes ready to run, the OS may or may not put the CPU into a low-power state while waiting for interrupts — it depends on how the idle() task is written. This would not generally be the lowest-power state available on the CPU, however, since it wants to wake quickly when interrupts occur.

In your case, it appears to be waking up once a second to handle the system timer tick.

If you really want to get into a lower-power state, there are generally platform-specific calls for that, and that's exactly the sort of thing the HAL is for. You shouldn't avoid it, you should learn it.

After a quick search, I discovered that the documentation here: APIs - power management discusses this specifically.

• Thanks for your response, very useful. In Mbed, sleep() is supposed to place to CPU into a low-power mode, but no documentation that I can find explains this logic. I've disabled the SysTick IRQ in the NVIC to no avail, I guess I'll just keep turning off timers/counters until it disappears.. – Adam Mitchell Mar 15 at 14:34
• @AdamMitchell - more likely what you need to do is actually read through the Mbed code (it is open source, even if by default they link a binary version of it) and figure out what they are doing, then decide if that can be brought into alignment with your goals, or if their goals are just too different. – Chris Stratton Mar 15 at 15:32

As explained in the documentation, the following drivers can prevent deep sleep:

• Ticker
• Timeout
• Timer
• SPI
• I2C
• CAN
• SerialBase

If you need to identify what is blocking deep sleep, you can build from the command line, and enable the verbose debugging - even though it seems you do not have any in your example.

You can also review the tickless documentation. I believe this mode is relatively new, so it is possible that your platform has some problems in the HAL.

If you enable tickless mode (via MBED_TICKLESS=1) and just pause the thread (via wait_ms(10000)) you won't see any spikes, and the MCU will stay in Stop mode (at least on my STM32F4 board).

So I've managed to figure this out; Sean, Dave's and Jan's answers were extremely helpful and pointed me in the right direction (as were the comments).

As mentioned, I was using MbedOS 5.11.2. This contained this issue whereby the CPU was locked out of deepsleep mode and wasn't disabling any timers/tickers etc... Updating to version 5.11.4 solved this issue and allowed the device to go into what I assume was STOP mode.

I was then able to utilise the HAL and go into STANDBY mode, the resulting end current draw of my idle loop was ~ 550 nA. Here's how I am putting the device into STANDBY mode:

#include "mbed.h"

int main()
{
HAL_Init();

__HAL_RCC_PWR_CLK_ENABLE();

__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOH_CLK_ENABLE();
__HAL_RCC_GPIOE_CLK_ENABLE();

GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.Pin = GPIO_PIN_All;
GPIO_InitStructure.Mode = GPIO_MODE_ANALOG;
GPIO_InitStructure.Pull = GPIO_NOPULL;

HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
HAL_GPIO_Init(GPIOD, &GPIO_InitStructure);
HAL_GPIO_Init(GPIOH, &GPIO_InitStructure);
HAL_GPIO_Init(GPIOE, &GPIO_InitStructure);

/* Disable GPIOs clock */
__HAL_RCC_GPIOA_CLK_DISABLE();
__HAL_RCC_GPIOB_CLK_DISABLE();
__HAL_RCC_GPIOC_CLK_DISABLE();
__HAL_RCC_GPIOD_CLK_DISABLE();
__HAL_RCC_GPIOH_CLK_DISABLE();
__HAL_RCC_GPIOE_CLK_DISABLE();

/* Enable Ultra low power mode */
HAL_PWREx_EnableUltraLowPower();

__HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);
HAL_PWR_EnterSTANDBYMode();
}