STM32F103 understand current consumption in RUN mode.
Hello,
I am developing a custom made PCB, for a pico balloon, mounting a STM32F103C8T6. For this project I will need to use low power techniques but before going deep into the Sleep/Standby/Stop mode I was working on understanding and reducing the power consumption of the STM32 during RUN mode.
For these tests, the STM32 has only the decoupling caps in the VDD, VBAT and VDDA pins, and the rest of the PCB are left unpopulated to ensure the current consumption comes only from the STM32.
I reduced the code to the minimum by just setting the Clock configuration for 8 MHz (directly from HSI, PLL and LSI off) , setting all the GPIO to INPUT + PULLDOWN + Set to RESET, configure 1 GPIO as output + set to RESET, and then wait forever in a while(1) loop.
The problem is that, when I compile the project without optimization "-O0" the STM32 consumes 9 mA, however when I set the "-O1" flag the same code runs at 2 mA. In the "-O1" compilation if, instead of an empty while I have a "HAL_Delay(5000);" the consumption goes up to 8mA. And in the "-O0" compilation, if I remove the set to RESET to the GPIO, the power goes to 2mA.
Tests:
- -O0 + HAL_GPIO_ WritePin(..., RESET): 9mA
- -O0 without HAL_GPIO_WritePin(): 2mA
- -O1 + HAL_GPIO_ WritePin(..., RESET): 2mA
- -O1 + HAL_Delay in while loop: 9mA
The pcb has nothing else conected, I have tried changing the GPIO that I set to RESET and the behaviour is the same. I have tried 2 ICs and the same. I have even exported the GPIO, RCC, PWR register contents and check that in all the compilations the peripherals are configured the same way.
So my main question is: Why is this happening? How the optimization is affecting the code? And how a simple HAL_Delay can add so much current consumption?
Thank you in advance.
The source code can be checked here: https://gist.github.com/LaboratorioGluon/e70cad16cc5aeccde5f150f94bfba9e1
