Unable to store the internal rtc time in back-up registers
Hey buddies, I am working on STM2F103C8T6 microcontroller. I am using LSI Clock of 40 MHz & using external clock of 36 MHz. I am trying to read the clock after power OFF & ON Low by using the following code(below) . Even though the 3.3v is detected on the VBAT pin, the clock in not getting updated during the power OFF. It is again reading from the previous stored value in the backup register. Pleas help me to resolve this issue.
/ USER CODE BEGIN Header / /** ****************************************************************************** * @file : main.c * @brief : Main program body ****************************************************************************** * @attention * * Copyright (c) 2024 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** */ / USER CODE END Header / /* Includes ------------------------------------------------------------------*/ #include "main.h" /* Private includes ----------------------------------------------------------*/ / USER CODE BEGIN Includes / / USER CODE END Includes / /* Private typedef -----------------------------------------------------------*/ / USER CODE BEGIN PTD / / USER CODE END PTD / /* Private define ------------------------------------------------------------*/ / USER CODE BEGIN PD / / USER CODE END PD / /* Private macro -------------------------------------------------------------*/ / USER CODE BEGIN PM / / USER CODE END PM / /* Private variables ---------------------------------------------------------*/ RTC_HandleTypeDef hrtc; UART_HandleTypeDef huart3; / USER CODE BEGIN PV / char time[20]; char date[20]; / USER CODE END PV / /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); static void MX_GPIO_Init(void); static void MX_RTC_Init(void); static void MX_USART3_UART_Init(void); / USER CODE BEGIN PFP / / USER CODE END PFP / /* Private user code ---------------------------------------------------------*/ / USER CODE BEGIN 0 / void set_time(void) { RTC_TimeTypeDef sTime; RTC_DateTypeDef DateToUpdate; sTime.Hours = 0x11; sTime.Minutes = 0x20; sTime.Seconds = 0x30; if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD) != HAL_OK) { Error_Handler(); } DateToUpdate.WeekDay = RTC_WEEKDAY_FRIDAY; DateToUpdate.Month = RTC_MONTH_JULY; DateToUpdate.Date = 0x26; DateToUpdate.Year = 0x0; if (HAL_RTC_SetDate(&hrtc, &DateToUpdate, RTC_FORMAT_BCD) != HAL_OK) { Error_Handler(); } HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR1, 0x32F2); } void get_time(void) { RTC_DateTypeDef gDate; RTC_TimeTypeDef gTime; / Get the RTC current Time / HAL_RTC_GetTime(&hrtc, &gTime, RTC_FORMAT_BIN); / Get the RTC current Date / HAL_RTC_GetDate(&hrtc, &gDate, RTC_FORMAT_BIN); / Display time Format: hh:mm:ss / sprintf((char*)time,"%02d:%02d:%02d",gTime.Hours, gTime.Minutes, gTime.Seconds); / Display date Format: dd-mm-yy / sprintf((char*)date,"%02d-%02d-%2d",gDate.Date, gDate.Month, 2000 + gDate.Year); } void UART_DISPLAY(void) { HAL_UART_Transmit(&huart3, time, sizeof(time), HAL_MAX_DELAY); HAL_UART_Transmit(&huart3, date, sizeof(date), HAL_MAX_DELAY); } / USER CODE END 0 / /** * @brief The application entry point. * @retval int */ int main(void) { / USER CODE BEGIN 1 / / USER CODE END 1 / /* MCU Configuration--------------------------------------------------------*/ / Reset of all peripherals, Initializes the Flash interface and the Systick. / HAL_Init(); / USER CODE BEGIN Init / / USER CODE END Init / / Configure the system clock / SystemClock_Config(); / USER CODE BEGIN SysInit / / USER CODE END SysInit / / Initialize all configured peripherals / MX_GPIO_Init(); MX_RTC_Init(); MX_USART3_UART_Init(); / USER CODE BEGIN 2 / / USER CODE END 2 / if(HAL_RTCEx_BKUPRead(&hrtc,RTC_BKP_DR1)!=0X32F2) { set_time(); } HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15,GPIO_PIN_SET ); HAL_Delay(200); HAL_GPIO_WritePin(GPIOB, GPIO_PIN_13, GPIO_PIN_SET); HAL_Delay(200); HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15,GPIO_PIN_RESET ); HAL_Delay(2000); / Infinite loop / / USER CODE BEGIN WHILE / while (1) { / USER CODE END WHILE / / USER CODE BEGIN 3 / get_time(); UART_DISPLAY(); HAL_Delay(1000); } / USER CODE END 3 / } /** * @brief System Clock Configuration * @retval None */ void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; RCC_PeriphCLKInitTypeDef PeriphClkInit = {0}; /** Initializes the RCC Oscillators according to the specified parameters * in the RCC_OscInitTypeDef structure. */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI|RCC_OSCILLATORTYPE_HSE; RCC_OscInitStruct.HSEState = RCC_HSE_ON; RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1; RCC_OscInitStruct.HSIState = RCC_HSI_ON; RCC_OscInitStruct.LSIState = RCC_LSI_ON; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } /** Initializes the CPU, AHB and APB buses clocks */ RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|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_DIV2; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) { Error_Handler(); } PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_RTC; PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSI; if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) { Error_Handler(); } } /** * @brief RTC Initialization Function *

