AnsweredAssumed Answered

RTC Backup register is not preserved on STM32F4xx within Cube code

Question asked by dolezal.ivan on Jan 23, 2015
Hello all,
I have a user board with STM32F405VGT6 using RTC (LSE). Firmware is based on STMCubeF4. I have found that RTC loses the time during a power-down although a battery is properly connected to VBAT pin. Then I check the RTC drop-out by a simpler way: testing of a preservation of a mark value in one from the RTC Backup registers.
Adapted example STM32F4xx_StdPeriph_Examples\RTC\RTC_BKPDomain works O.K. (as LEDs show) but the same procedure based on HAL drivers and Cube code (see bellow) does not.
I have found that it works without the SystemClock_Config function.
If the function is inserted either in 1st or 2nd position, the backup register is preserved after reset but not after power-down.
It is very strange, isn't it?
I cannot debug the function immediatelly after power-up, since the JTAG adapter has to be initialized sooner.
Does anybody has any opinion?
Thank you.
Ivan

#define BACKUP_MARK         12345678
#define BACKUP_TEST_REG 19
 
#include "stm32f4xx_hal.h"
 
static void MX_GPIO_Init(void);
static void SystemClock_Config(void);
 
// Macro -------------------------------------------------------------
#define  SETPIN(port,pin)  (port->BSRRL = pin)
#define  CLRPIN(port,pin)  (port->BSRRH = pin)
 
// Pin definitions
#define  LED_B_PIN      GPIO_PIN_12 // modra LED
#define  LED_DR_PIN     GPIO_PIN_14 // cervena z dvojite LED
#define  LED_DG_PIN     GPIO_PIN_13 // zelena z dvojite LED
#define  LED_PORT       GPIOE
 
RTC_HandleTypeDef hrtc;
 
//------------------------------------------------------------------------------
int main(void)
{
  HAL_Init();
  // SystemClock_Config();      // Pos #1, causes reset of Backup Register if the power has been off
  MX_GPIO_Init();
  __PWR_CLK_ENABLE();   // normally in SystemClock_Config()
  HAL_PWR_EnableBkUpAccess();
 
  if (HAL_RTCEx_BKUPRead(&hrtc, BACKUP_TEST_REG) == BACKUP_MARK)
      SETPIN(LED_PORT, LED_B_PIN);  // blue LED ON, if the backup value persists
  else
      SETPIN(LED_PORT, LED_DR_PIN); // red LED ON, if the backup value dismisses
  // SystemClock_Config();      // Pos #2, causes reset of Backup Register if the power has been off
  __HAL_RTC_WRITEPROTECTION_DISABLE(&hrtc); // set the backup register
  HAL_RTCEx_BKUPWrite(&hrtc, BACKUP_TEST_REG, BACKUP_MARK);
  __HAL_RTC_WRITEPROTECTION_ENABLE(&hrtc);
 
  HAL_PWREx_EnableBkUpReg();     // turn on VBAT regulator
 
  while (1);
}
 
// System Clock Configuration ----------------------------------------------
void SystemClock_Config(void)
{
  RCC_ClkInitTypeDef RCC_ClkInitStruct;
  RCC_OscInitTypeDef RCC_OscInitStruct;
 
  __PWR_CLK_ENABLE();
 
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
 
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE|RCC_OSCILLATORTYPE_LSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.LSEState = RCC_LSE_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLM = 8;
  RCC_OscInitStruct.PLL.PLLN = 336;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 7;
  HAL_RCC_OscConfig(&RCC_OscInitStruct);
 
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1
                              |RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
  HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);
}
 
//----------------------------------------------
void MX_GPIO_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStruct;
 
  __GPIOE_CLK_ENABLE();
  /* Configure GPIO pins : LEDs */
  GPIO_InitStruct.Pin = GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
  HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
}

Outcomes