AnsweredAssumed Answered

VREFINT magically turns on, MCU does not know about it

Question asked by leppie_w on Mar 27, 2015
Latest reply on Mar 31, 2015 by leppie_w
It would seem VREFINT magically turns on after coming out of sleep/stop even though it is not being used on the STM32L053 (nucleo board). It turns off for the first sleep/stop, then it wont turn off again. The MCU does not even seem to know about it.

The code is a minimal repro of the issue. It is based on the tamper timestamp example.

For the first stop, I get ~7.6uA. Subsequently, it uses ~9.1uA. 

If one comments out line 45 (HAL_PWREx_EnableUltraLowPower()), usage is ~9.1uA for both.

I have the code on Github too: https://gist.github.com/leppie/f3a5a64b2cc1f3622545

001./*
002.stm32l0xx_hal_conf.h
003.#define HAL_MODULE_ENABLED
004.#define HAL_DMA_MODULE_ENABLED
005.#define HAL_FLASH_MODULE_ENABLED
006.#define HAL_GPIO_MODULE_ENABLED
007.#define HAL_PWR_MODULE_ENABLED
008.#define HAL_RCC_MODULE_ENABLED
009.#define HAL_RTC_MODULE_ENABLED
010.#define HAL_SPI_MODULE_ENABLED
011.#define HAL_CORTEX_MODULE_ENABLED
012.*/
013. 
014.#include "stm32l0xx_hal.h"
015.#include "stm32l0xx_nucleo.h"
016. 
017.#define RTC_ASYNCH_PREDIV  0x07
018.#define RTC_SYNCH_PREDIV   0x0FFF
019. 
020.RTC_HandleTypeDef RtcHandle;
021. 
022.static void SystemClock_Config(void);
023.static void RTC_TimeStampConfig(void);
024. 
025.int main(void)
026.{
027.  HAL_Init();
028.  SystemClock_Config();
029.  HAL_SuspendTick();
030. 
031.  RtcHandle.Instance = RTC;
032.  RtcHandle.Init.HourFormat = RTC_HOURFORMAT_12;
033.  RtcHandle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV;
034.  RtcHandle.Init.SynchPrediv = RTC_SYNCH_PREDIV;
035.  RtcHandle.Init.OutPut = RTC_OUTPUT_DISABLE;
036.  RtcHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
037.  RtcHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
038.   
039.  if(HAL_RTC_Init(&RtcHandle) != HAL_OK) while(1);
040. 
041.  RTC_TimeStampConfig();
042. 
043.  // this disables VREFINT in sleep/stop
044.  // comment out to see the issue/difference
045.  HAL_PWREx_EnableUltraLowPower();
046. 
047.  while (1)
048.  {
049.    // happens in sleep too
050.    HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_SLEEPENTRY_WFI);
051.  }
052.}
053. 
054.static void SystemClock_Config(void)
055.{
056.  RCC_ClkInitTypeDef RCC_ClkInitStruct;
057.  RCC_OscInitTypeDef RCC_OscInitStruct;
058.   
059.  __PWR_CLK_ENABLE();
060.  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);
061.   
062.  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
063.  RCC_OscInitStruct.MSIState = RCC_MSI_ON;
064.  RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_5;
065.  RCC_OscInitStruct.MSICalibrationValue = 0x00;
066.  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
067.  if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) while(1);
068. 
069.  RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
070.  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_MSI;
071.  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
072.  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; 
073.  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; 
074.  if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK) while(1);
075.}
076. 
077.static void RTC_TimeStampConfig(void)
078.{
079.  RTC_DateTypeDef sdatestructure;
080.  RTC_TimeTypeDef stimestructure;
081.  RTC_TamperTypeDef stamperstructure;
082.   
083.  stamperstructure.Filter = RTC_TAMPERFILTER_DISABLE;
084.  stamperstructure.Tamper = RTC_TAMPER_2;
085.  stamperstructure.Trigger = RTC_TAMPERTRIGGER_RISINGEDGE;
086.  stamperstructure.SamplingFrequency = RTC_TAMPERSAMPLINGFREQ_RTCCLK_DIV256;
087.  stamperstructure.PrechargeDuration = RTC_TAMPERPRECHARGEDURATION_1RTCCLK ;
088.  stamperstructure.TamperPullUp = RTC_TAMPER_PULLUP_DISABLE;
089.  stamperstructure.TimeStampOnTamperDetection = RTC_TIMESTAMPONTAMPERDETECTION_ENABLE;
090.  HAL_RTCEx_SetTamper(&RtcHandle, &stamperstructure);
091.   
092.  HAL_RTCEx_SetTimeStamp_IT(&RtcHandle, RTC_TIMESTAMPEDGE_RISING, RTC_TIMESTAMPPIN_PC13);
093.   
094.  __HAL_RTC_TIMESTAMP_CLEAR_FLAG(&RtcHandle,RTC_FLAG_TSF);
095. 
096.  sdatestructure.Year = 0x14;
097.  sdatestructure.Month = RTC_MONTH_APRIL;
098.  sdatestructure.Date = 0x14;
099.  sdatestructure.WeekDay = RTC_WEEKDAY_MONDAY;
100.  if(HAL_RTC_SetDate(&RtcHandle,&sdatestructure,FORMAT_BCD) != HAL_OK) while(1);
101.   
102.  stimestructure.Hours = 0x08;
103.  stimestructure.Minutes = 0x10;
104.  stimestructure.Seconds = 0x00;
105.  stimestructure.TimeFormat = RTC_HOURFORMAT12_AM;
106.  stimestructure.DayLightSaving = RTC_DAYLIGHTSAVING_NONE ;
107.  stimestructure.StoreOperation = RTC_STOREOPERATION_RESET;
108.  if(HAL_RTC_SetTime(&RtcHandle,&stimestructure,FORMAT_BCD) != HAL_OK) while(1);
109.}
110. 
111.void HAL_RTCEx_TimeStampEventCallback(RTC_HandleTypeDef *hrtc)
112.{
113.  RTC_DateTypeDef sTimeStampDateget;
114.  RTC_TimeTypeDef sTimeStampget;
115. 
116.  __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc,RTC_FLAG_TAMP2F);
117. 
118.  HAL_RTCEx_GetTimeStamp(&RtcHandle, &sTimeStampget, &sTimeStampDateget, FORMAT_BIN);
119.}
120. 
121.void HAL_RTC_MspInit(RTC_HandleTypeDef *hrtc __attribute((unused)))
122.{
123.  RCC_OscInitTypeDef RCC_OscInitStruct;
124.  RCC_PeriphCLKInitTypeDef  PeriphClkInitStruct;
125. 
126.  RCC_OscInitStruct.OscillatorType =  RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE;
127.  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
128.  RCC_OscInitStruct.LSEState = RCC_LSE_ON;
129.  RCC_OscInitStruct.LSIState = RCC_LSI_OFF;
130.  if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) while(1);
131. 
132.  PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
133.  PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
134.  if(HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) while(1);
135. 
136.  __HAL_RCC_RTC_ENABLE();
137.  HAL_NVIC_SetPriority(RTC_IRQn, 0x0, 0);
138.  HAL_NVIC_EnableIRQ(RTC_IRQn);
139.}
140. 
141.void RTC_IRQHandler(void)
142.{
143.  HAL_RTCEx_TamperTimeStampIRQHandler(&RtcHandle);
144.}

I have tried just about everything I could think of.

Any ideas?

Outcomes