cancel
Showing results for 
Search instead for 
Did you mean: 

RTC is getting set off by 1hour in STM32U5. I am using Nucleo board and I am checking the RTC behavior in STOP3 mode of stm32u5.

STee.3
Associate II
#include "main.h"
#include <stdio.h>
#include <string.h>
RTC_HandleTypeDef hrtc;
 
UART_HandleTypeDef huart1;
 
/* USER CODE BEGIN PV */
#define UartHandle &huart1
#define RtcHandle &hrtc
static uint16_t YearStart = 2000;
 
char message[50];
uint8_t size;
 
uint8_t Rx_data;
 
void SystemClock_Config(void);
static void SystemPower_Config(void);
static void MX_GPIO_Init(void);
static void MX_RTC_Init(void);
static void MX_USART1_UART_Init(void);
 
void set_time (void)
{
	RTC_TimeTypeDef sTime;
	      RTC_DateTypeDef sDate;
 
	      size = sprintf(message, "\n\r RTC in Stop Mode\n\r");
	      HAL_UART_Transmit(UartHandle, (uint8_t*)message, size, 100);
 
	      // Set date and time
	      sDate.WeekDay = RTC_WEEKDAY_TUESDAY;
	      sDate.Date = 22;
	      sDate.Month = 03;
	      sDate.Year = 23;    // from 0-99 (1972 == 72, start year = 1900)
	      HAL_RTC_SetDate(RtcHandle, &sDate, RTC_FORMAT_BIN);
	      sTime.Hours = 10;
	      sTime.Minutes = 45;
	      HAL_RTC_SetTime(RtcHandle, &sTime, RTC_FORMAT_BIN);
 
	      HAL_RTCEx_BKUPWrite(RtcHandle, RTC_BKP_DR0, 0xBEBE);
 
}
void get_time(void)
{
 
        static RTC_TimeTypeDef sTime;
		static RTC_DateTypeDef sDate;
		static char message[50];
		static size_t size;
 
		// Get current time and date
 
		HAL_RTC_GetDate(&hrtc, &sDate, RTC_FORMAT_BIN);
		HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BIN);
 
		// Send time and date over uart
		size = sprintf(message, "Date: %2.2u-%2.2u-%4.4u  Time: %2.2u:%2.2u:%2.2u\n\r", sDate.Date, sDate.Month, sDate.Year + YearStart,sTime.Hours, sTime.Minutes, sTime.Seconds);
		HAL_UART_Transmit(UartHandle, (uint8_t*)message, size, 100);
		HAL_Delay(1000);
 
}
 
void HAL_GPIO_EXTI_Rising_Callback(uint16_t GPIO_Pin)
{
	if(GPIO_Pin == GPIO_PIN_13)
	{
		    SystemClock_Config();
 
			size = sprintf(message, "\n\r After Sleep Mode \n\r");
		    HAL_UART_Transmit(UartHandle, (uint8_t*)message, size, 100);
		    get_time();
	}
 
}
 
int main(void)
{
 
  HAL_Init();
 
  SystemClock_Config();
 
  SystemPower_Config();
 
  
  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_RTC_Init();
  MX_USART1_UART_Init();
  /* USER CODE BEGIN 2 */
    if(HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR0) != 0xBEBE )
      {
         set_time();
      }
    size = sprintf(message, "\n\r Before Stop Mode \n\r");
    HAL_UART_Transmit(UartHandle, (uint8_t*)message, size, 50);
    get_time();
    
    /* Check and Clear the Wakeup flag */
    /* Enable Backup domain access */
 
 
    HAL_SuspendTick();
 
   // wake up using external interrupt using user button
    HAL_PWREx_EnterSTOP3Mode(PWR_STOPENTRY_WFI);
	
    HAL_ResumeTick();
  /* USER CODE END 2 */
 
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */
 
    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}
 
/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
 
  /** Configure the main internal regulator output voltage
  */
  if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE4) != HAL_OK)
  {
    Error_Handler();
  }
 
  /** Configure LSE Drive Capability
  */
  HAL_PWR_EnableBkUpAccess();
  __HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_LOW);
 
  /** Initializes the CPU, AHB and APB buses clocks
  */
  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_NONE;
  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_CLOCKTYPE_PCLK3;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSE;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB3CLKDivider = RCC_HCLK_DIV1;
 
  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  {
    Error_Handler();
  }
}
 
/**
  * @brief Power Configuration
  * @retval None
  */
static void SystemPower_Config(void)
{
 
  /*
   * Switch to SMPS regulator instead of LDO
   */
  if (HAL_PWREx_ConfigSupply(PWR_SMPS_SUPPLY) != HAL_OK)
  {
    Error_Handler();
  }
}
 
static void MX_RTC_Init(void)
{
 
  /* USER CODE BEGIN RTC_Init 0 */
 
  /* USER CODE END RTC_Init 0 */
 
  RTC_PrivilegeStateTypeDef privilegeState = {0};
 
  /* USER CODE BEGIN RTC_Init 1 */
 
  /* USER CODE END RTC_Init 1 */
 
  /** Initialize RTC Only
  */
  hrtc.Instance = RTC;
  hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
  hrtc.Init.AsynchPrediv = 127;
  hrtc.Init.SynchPrediv = 255;
  hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
  hrtc.Init.OutPutRemap = RTC_OUTPUT_REMAP_NONE;
  hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
  hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
  hrtc.Init.OutPutPullUp = RTC_OUTPUT_PULLUP_NONE;
  hrtc.Init.BinMode = RTC_BINARY_NONE;
  if (HAL_RTC_Init(&hrtc) != HAL_OK)
  {
    Error_Handler();
  }
  privilegeState.rtcPrivilegeFull = RTC_PRIVILEGE_FULL_NO;
  privilegeState.backupRegisterPrivZone = RTC_PRIVILEGE_BKUP_ZONE_NONE;
  privilegeState.backupRegisterStartZone2 = RTC_BKP_DR0;
  privilegeState.backupRegisterStartZone3 = RTC_BKP_DR0;
  if (HAL_RTCEx_PrivilegeModeSet(&hrtc, &privilegeState) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN RTC_Init 2 */
 
  /* USER CODE END RTC_Init 2 */
 
}
 
}
 
static void MX_GPIO_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};
 
  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOC_CLK_ENABLE();
  __HAL_RCC_GPIOH_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();
 
  /*Configure GPIO pin : PC13 */
  GPIO_InitStruct.Pin = GPIO_PIN_13;
  GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
 
  /* EXTI interrupt init*/
  HAL_NVIC_SetPriority(EXTI13_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(EXTI13_IRQn);
 
}

1 ACCEPTED SOLUTION

Accepted Solutions

Initialize the local structs in set_time():

void set_time (void)
{
	RTC_TimeTypeDef sTime = {0};
	      RTC_DateTypeDef sDate = {0};
...

JW

View solution in original post

2 REPLIES 2
LCE
Principal

Exactly 1 hour?

Daylight Saving Time?

Initialize the local structs in set_time():

void set_time (void)
{
	RTC_TimeTypeDef sTime = {0};
	      RTC_DateTypeDef sDate = {0};
...

JW