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.

#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)
  // SystemClock_Config();      // Pos #1, causes reset of Backup Register if the power has been off
  __PWR_CLK_ENABLE();   // normally in SystemClock_Config()
      SETPIN(LED_PORT, LED_B_PIN);  // blue LED ON, if the backup value persists
      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_PWREx_EnableBkUpReg();     // turn on VBAT regulator
  while (1);
// System Clock Configuration ----------------------------------------------
void SystemClock_Config(void)
  RCC_ClkInitTypeDef RCC_ClkInitStruct;
  RCC_OscInitTypeDef RCC_OscInitStruct;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.LSEState = RCC_LSE_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLM = 8;
  RCC_OscInitStruct.PLL.PLLN = 336;
  RCC_OscInitStruct.PLL.PLLQ = 7;
  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;
  /* Configure GPIO pins : LEDs */
  GPIO_InitStruct.Pin = GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
  HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);