I repeated all measurements and compared project directories, this time I measured with ST-LINK disconnected during current measurement.
Here is connections of STM32L011F4P6:
- Pin 1 (boot) to ground
- Pin 4 (nrst) 100nF cap to ground.
- Pin 5 (VDDA) to 3.0V (same as pin 16)
- Pin 8 (PA2) set as GPIO_Output in IOC file
- Pin 15 (VSS) to GND
- Pin 16 (VDD) to 3.0V
- Pin 19 and Pin 20 are SWD, connected only during programming, not during current measurement.
- 100nF cap and 10uF cap between VDD and VSS
First test:
- in IOC all pins off expcept PA2 which is gpio output
- in main infinite loop:
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_2);
HAL_Delay(10);
- When run this consumes 51uA with 3.0V VDD
Second test:
- in IOC I enabled pin PA0 to be analog input
- no other change in code but MX_ADC_Init appeared
- stm32l0xx_hal_conf.h now have uncommented #define HAL_ADC_MODULE_ENABLED
- I'm not actually measuring anything analog, just enabled pin
- When run this consumes 286uA with 3.0V VDD
- Src/Main.c enabled HSI oscillator, this IMHO explains the current raise:
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_MSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
previously it was:
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
So it changes the oscillator from MSI to HSI when ADC is used and that causes the raise in current consumption. Now I need to return back to MSI when I'm done measuring ADC and it should return back to 51uA.
Update: following code indeed switches power consumption back to 51uA:
// Turn off ADC
HAL_ADC_Stop(&hadc);
HAL_ADC_DeInit(&hadc);
// Switch back to MSI
// Enable MSI oscillator
__HAL_RCC_MSI_ENABLE();
// Wait until MSI is ready
while (__HAL_RCC_GET_FLAG(RCC_FLAG_MSIRDY) == RESET);
// Select MSI as the system clock source
__HAL_RCC_SYSCLK_CONFIG(RCC_SYSCLKSOURCE_MSI);
// Wait until MSI is used as the system clock source
while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_MSI);
// (Optional) Disable HSI if no longer needed
__HAL_RCC_HSI_DISABLE();