PortentaH7 RTC was not working(H7 Lite & Breakout Board)

Hello Arduino developers, Wishing you all a Happy New Year!

I am using Portenta H7 for development and experiencing "Abnormal behavior" in RTC operation. I am looking for solutions to resolve this issue.

My Problem : When powered on, I can confirm the time incrementing correctly through the HAL_GET_TIME function. However, when I power off and use the battery to operate the RTC, the actual elapsed time and the time incremented using the RTC do not match.

Is it possible to find a library to properly operate the RTC of Portenta H7 in the Arduino IDE? The current library provided by the Breakoutboard is not functioning correctly, and my attempts to solve the issue have failed to prove the accuracy of the RTC. I have two main problems:

  1. Difficulty achieving accurate 1-second frequency:
    When operating the RTC of Portenta H7, it uses a 32.768kHz LSE, and I cannot confirm if it operates accurately. Below is my code:
#include "Arduino.h"
#include "stm32h7xx_hal.h"

/* Private variables ---------------------------------------------------------*/
RTC_HandleTypeDef hrtc;
RTC_TimeTypeDef sTime;
RTC_DateTypeDef sDate;
RTC_AlarmTypeDef sAlarm;

//#define RTC_SET_UTC

#define RTC_GET_UTC

/* Function prototypes */
void SystemClock_Config(void);
void Error_Handler(void);
void MX_RTC_Init(void);

void Error_Handler_RTC_INIT(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
    Serial.println("[Err_RTC_INIT] RTC_INIT was failed");
  }
  /* USER CODE END Error_Handler_Debug */
}

void Error_Handler_OSC_INIT(void) {
    while (1) {
          Serial.println("[Err_RCC_OSC_INIT] RCC_OscInitStruct was not HAL_OK.");
        /* User can add custom code to deal with errors */
    }
}

/* Error Handler */
void Error_Handler_CLK_INIT(void) {
    while (1) {
         Serial.println("[Err_RCC_CLK_INIT] RCC_ClkInitStruct was not HAL_OK.");
        /* User can add custom code to deal with errors */
    }
}

void Error_Handler_Calibration(void) {
    while (1) {
         Serial.println("[Err_RTC_CALIBRATION] Calibration was not OK.");
        /* User can add custom code to deal with errors */
    }
}

void Error_Handler(void) {
  while(1) {
    Serial.println("[NOT DEFINED] null ");
  }
}

void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Supply configuration update enable
  */
  HAL_PWREx_ConfigSupply(PWR_DIRECT_SMPS_SUPPLY);

  /** Configure the main internal regulator output voltage
  */
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);

  while(!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {}

  /** Configure LSE Drive Capability
  */
  HAL_PWR_EnableBkUpAccess();
  __HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_LOW);

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  // RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_LSE;
  RCC_OscInitStruct.LSEState = RCC_LSE_ON;
  RCC_OscInitStruct.HSIState = RCC_HSI_DIV1;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler_OSC_INIT();
  }

  /** 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_D3PCLK1|RCC_CLOCKTYPE_D1PCLK1;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB3CLKDivider = RCC_APB3_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_APB2_DIV1;
  RCC_ClkInitStruct.APB4CLKDivider = RCC_APB4_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  {
    Error_Handler_CLK_INIT();
  }

  /** Enables the Clock Security System
  */
  HAL_RCCEx_EnableLSECSS();
}

/**
  * @brief RTC Initialization Function
  * @param None
  * @retval None
  */
void MX_RTC_Init(void)
{

  /* USER CODE BEGIN RTC_Init 0 */

  /* USER CODE END RTC_Init 0 */

  // RTC_TimeTypeDef sTime = {0};
  // RTC_DateTypeDef sDate = {0};
  // RTC_AlarmTypeDef sAlarm = {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.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
  hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
  hrtc.Init.OutPutRemap = RTC_OUTPUT_REMAP_NONE;
  if (HAL_RTC_Init(&hrtc) != HAL_OK)
  {
    Error_Handler();
  }

  /* USER CODE BEGIN Check_RTC_BKUP */

  /* USER CODE END Check_RTC_BKUP */

  /** Initialize RTC and set the Time and Date
  */

  // sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
  // sTime.StoreOperation = RTC_STOREOPERATION_SET;
  if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD) != HAL_OK)
  {
    Error_Handler();
  }
  sDate.WeekDay = RTC_WEEKDAY_MONDAY;
  sDate.Month = RTC_MONTH_JANUARY;
  sDate.Date = 0x1;
  sDate.Year = 0x0;
  if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BCD) != HAL_OK)
  {
    Error_Handler();
  }

  /** Enable the Alarm A
  */
  sAlarm.AlarmTime.Hours = 0x0;
  sAlarm.AlarmTime.Minutes = 0x0;
  sAlarm.AlarmTime.Seconds = 0x0;
  sAlarm.AlarmTime.SubSeconds = 0x0;
  sAlarm.AlarmTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
  sAlarm.AlarmTime.StoreOperation = RTC_STOREOPERATION_RESET;
  sAlarm.AlarmMask = RTC_ALARMMASK_DATEWEEKDAY|RTC_ALARMMASK_HOURS |RTC_ALARMMASK_MINUTES;
  sAlarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_ALL;
  sAlarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_DATE;
  sAlarm.AlarmDateWeekDay = 0x1;
  sAlarm.Alarm = RTC_ALARM_A;
  if (HAL_RTC_SetAlarm_IT(&hrtc, &sAlarm, RTC_FORMAT_BCD) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN RTC_Init 2 */

  /* USER CODE END RTC_Init 2 */

}

void rtc_set_time()
{
  
  sTime.Hours = 0x12;
  sTime.Minutes = 0x0;
  sTime.Seconds = 0x01;
  HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN);
}

void rtc_get_time()
{
  HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BIN);
  Serial.print("[Last] Current Time: ");
  Serial.print(sTime.Hours);
  Serial.print(":");
  Serial.print(sTime.Minutes);
  Serial.print(":");
  Serial.println(sTime.Seconds);
  delay(1000);
}

void setup() {
  // put your setup code here, to run once:

  HAL_Init();

  SystemClock_Config();

  MX_RTC_Init();


  #ifdef RTC_SET_UTC
  rtc_set_time();
  #endif
}

void loop() {
  
#ifdef RTC_GET_UTC
  rtc_get_time();
 #endif
}

I am aware that the Portenta H7 Lite board's LSE (32.768kHz) wiring may not be correct, leading to improper functionality. The only information about the revision of the Portenta board I am using is E237771 2115 on the back. Is there a way to find the revision of this Portenta board?

  1. Couldn't Find library src on Arduino-Portenta H7
    I have used the STM32 HAL library for Portenta H7 based on STM32H747 ,since "I could not find a precise library provided by Arduino." If anyone has experienced similar issues or has successfully resolved them, especially with Arduino code, I would appreciate your assistance.

I m waiting for your good advice, Thanks. (It would be helpful for someone else.)

The LSI runs at about 32kHZ so there may be the cause of your time discrepancy. If the Portenta H7 has an actual 32.768kHz crystal mounted across PC14 and PC15, then you will want to select LSE as clock source. When you figure that out, let me know.

Because,

Me ke aloha, and welcome to the Arduino forum!
Mark

Sorry for my mistakes. Absolutely, I setted LSE(32.768Khz) ,Not LSI(32KHz).
The content in the main text has been modified. Any recommend do u have?

I just ran that code on a GIGA R1 and was ticking along in serial. Powered the board down and crashed the board on powering back up. No red light of doom, just no more serial. Tried re-up the code and no-joy. Sorry for the unintended pun.

Out of my depth with Portenta boards, but am empathetic to your concerns.

I must ask what sort of time difference you observe between the H7 on powering back up, and I assume some other reference, like a wall clock, whatnot. On resetting the board on my first attempt, I saw that the clock reset back to zero, and then ticked along.

21:50:00.626 -> [Last] Current Time: 0:1:10
21:50:01.616 -> [Last] Current Time: 0:1:11
21:50:02.630 -> [Last] Current Time: 0:1:12
21:50:03.632 -> [Last] Current Time: 0:1:13
21:50:04.595 -> [Last] Current Time: 0:1:14
21:50:05.615 -> [Last] Current Time: 0:1:15
21:50:06.633 -> [Last] Current Time: 0:1:16
21:50:07.633 -> [Last] Current Time: 0:1:17
21:50:08.634 -> [Last] Current Time: 0:1:18
21:50:12.171 -> [Last] Current Time: 0:0:2
21:50:13.195 -> [Last] Current Time: 0:0:3
21:50:14.179 -> [Last] Current Time: 0:0:4
21:50:15.196 -> [Last] Current Time: 0:0:5
21:50:16.186 -> [Last] Current Time: 0:0:6
21:50:17.154 -> [Last] Current Time: 0:0:7
21:50:18.170 -> [Last] Current Time: 0:0:8
21:50:19.207 -> [Last] Current Time: 0:0:9
21:50:20.185 -> [Last] Current Time: 0:0:10

Thank u for your help. Sincerly

what sort of time difference you observe between the H7 on powering back up,
->
"If the RTC functions correctly, the elapsed time from the serial terminal logs and the RTC time (LAST Current time) should be the same.

-> 21:50:00.626 -> [Last] Current Time: 0:1:10
21:50:01.616 -> [Last] Current Time: 0:1:11
21:50:02.630 -> [Last] Current Time: 0:1:12
21:50:03.632 -> [Last] Current Time: 0:1:13
21:50:04.595 -> [Last] Current Time: 0:1:14
21:50:05.615 -> [Last] Current Time: 0:1:15
21:50:06.633 -> [Last] Current Time: 0:1:16
21:50:07.633 -> [Last] Current Time: 0:1:17
21:50:08.634 -> [Last] Current Time: 0:1:18

Terminal Log time 8Second , [Last} Current Time : 8Second(0:1:10 ~ 0:1:18) is Right.

it was right, cause Power on (USB Power)

Eamaple Situation, if u take Power off ,At (21:50:08.634) -> (Current Time : 0:1:18)
and After 10 seconds have passed." Then,(Using Coin Battery, RTC is running in spite of power off)

At (22:00:08.634) -> (Current Time : 0:1:23 or 27 or 30, ... not 28, it must be "28") is it error

21:50:12.171 -> [Last] Current Time: 0:0:2
21:50:13.195 -> [Last] Current Time: 0:0:3
21:50:14.179 -> [Last] Current Time: 0:0:4
21:50:15.196 -> [Last] Current Time: 0:0:5
21:50:16.186 -> [Last] Current Time: 0:0:6
21:50:17.154 -> [Last] Current Time: 0:0:7
21:50:18.170 -> [Last] Current Time: 0:0:8
21:50:19.207 -> [Last] Current Time: 0:0:9
21:50:20.185 -> [Last] Current Time: 0:0:10

Upper log is maybe HAL_SET_TIME log, Please , have to following step below.

first step : HAL_SET_TIME, Second STEP : HAL_GET_TIME(Only)

And must insert Coin cell battery.

I wished my reply for help you.

So, back to my original question, what sort of time discrepancy do you observe after removing power for some period? Are you experiencing a one second per minute error, one second per hour error?

I know what results I received on my end. In spite of having a coin battery attached to my board serving power up to D3, on reset, the clock reset to zero.

Good morning or evening!

Are you experiencing a one second per minute error, one second per hour error?

-> I experienced one second per minute error, "I couldn't confirm any plus second even after removing the battery every second

Please check that image
After turning off the power for 10 seconds and then turning it back on, the actual increase is not 10 seconds; it might be 7 seconds, 6 seconds, or another value. Similarly, when the power is turned off for minutes, for example, if turned off for 1 minute and then turned back on, it increases by 40 seconds, other seconds or
""even if turned back on after 1 minute, the time appears to have rolled back to 1 minute ago (this is crucial)""

Yes, good morning to you!

Can you run this bit of code and respond with the Serial output you receive? Thank you.

void setup() {
Serial.begin(9600);
delay(4000);
Serial.println("test");
delay(1000);
Serial.println(RCC->BDCR, BIN);
}
void loop() {}

Sure. Check the RCC Register value is good choice.

I attached my log. and I m used Portenta Lite Board and tested Breakout board

10:34:57.756 -> test

10:34:58.780 -> 1000001001100101

Thanks.

"And, I have debugged the following code in STM32CubeIDE.(STM32H747XIH6, Debugging on Breeakoutboard with Portenta H7) It breaks at the specified location but does not proceed further."

int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */
/* USER CODE BEGIN Boot_Mode_Sequence_0 */
  int32_t timeout;
/* USER CODE END Boot_Mode_Sequence_0 */

/* USER CODE BEGIN Boot_Mode_Sequence_1 */
  /* Wait until CPU2 boots and enters in stop mode or timeout*/
  timeout = 0xFFFF;
  while((__HAL_RCC_GET_FLAG(RCC_FLAG_D2CKRDY) != RESET) && (timeout-- > 0));
  if ( timeout < 0 )
  {
  Error_Handler();
  }
/* USER CODE END Boot_Mode_Sequence_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 Boot_Mode_Sequence_2 */
/* When system initialization is finished, Cortex-M7 will release Cortex-M4 by means of
HSEM notification */
/*HW semaphore Clock enable*/
__HAL_RCC_HSEM_CLK_ENABLE();
/*Take HSEM */
HAL_HSEM_FastTake(HSEM_ID_0);
/*Release HSEM in order to notify the CPU2(CM4)*/
HAL_HSEM_Release(HSEM_ID_0,0);
/* wait until CPU2 wakes up from stop mode */
timeout = 0xFFFF;
while((__HAL_RCC_GET_FLAG(RCC_FLAG_D2CKRDY) == RESET) && (timeout-- > 0));
if ( timeout < 0 )
{
Error_Handler();
}
/* USER CODE END Boot_Mode_Sequence_2 */

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_RTC_Init();
  /* USER CODE BEGIN 2 */
  /* Set the time (optional) */
  RTC_TimeTypeDef sTime = {0};
  sTime.Hours = 12;
  sTime.Minutes = 0;
  sTime.Seconds = 0;
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
     HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BIN);

  Serial.print("[Last] Current Time: ");
  Serial.print(sTime.Hours);
  Serial.print(":");
  Serial.print(sTime.Minutes);
  Serial.print(":");
  Serial.println(sTime.Seconds);
    /* 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};

  /** Supply configuration update enable
  */
  HAL_PWREx_ConfigSupply(PWR_DIRECT_SMPS_SUPPLY);

  /** Configure the main internal regulator output voltage
  */
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);

  while(!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {}

  /** Configure LSE Drive Capability
  */
  HAL_PWR_EnableBkUpAccess();
  __HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_LOW);

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_LSE;
  RCC_OscInitStruct.LSEState = RCC_LSE_ON;
  RCC_OscInitStruct.HSIState = RCC_HSI_DIV1;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  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_D3PCLK1|RCC_CLOCKTYPE_D1PCLK1;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV2;
  RCC_ClkInitStruct.APB3CLKDivider = RCC_APB3_DIV2;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV2;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_APB2_DIV2;
  RCC_ClkInitStruct.APB4CLKDivider = RCC_APB4_DIV2;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  {
    Error_Handler();
  }
}

"Breakpoint is triggered at this location.
while((__HAL_RCC_GET_FLAG(RCC_FLAG_D2CKRDY) != RESET) && (timeout-- > 0));

What does this RCC_FLAG_D2CKRDY?"

I hope it will be help you. Thanks.

Did not notice in reviewing your code previously that LSEBYP is set. Also RTCSEL is set to LSI. And bit 6, LSECSSD set a flag.

For comparison, following terminal output is what I received on my GIGA R1...

12:05:19.830 -> test
12:05:20.874 -> 1000000101100001

I too suffer poor RTC performance, which I suspect is due to faults similar to that of early production Portenta H7's. Which is very disappointing.

In regard your question...

Bit 15 D2CKRDY: D2 domain clocks ready flag
Set by hardware to indicate that the D2 domain clocks (CPU, bus and peripheral) are available.
0: D2 domain clocks are not available (default after reset)
1: D2 domain clocks are available

I Checked on datasheet(STM32H747 RCC Register, LSECSSD set a flag. is 1: Failure detected on 32 kHz oscillato)

"Does this mean that no matter how much I try to turn on the LSE (Low-Speed External) oscillator, the detection itself does not occur, regardless of the process(Selected LSE or LSE ON)?" On portenta H7 Board

I too suffer poor RTC performance, which I suspect is due to faults similar to that of early production Portenta H7's. Which is very disappointing.

-> Then, how to resolve this issues? only using RTC Module(using I2c RTC Module or other board?)

any methods? Thanks.

Little choice but to run a DS3231 for the time being. Quite vexing.

Perhaps try this.

Can you insert the code I provided someplace in your code to read the register every so often? Use your last code that was working. As an error check, after all the peripherals and other clocks have started up.

"Of course! I was already in the process of performing that task." Using HSI with LSE(RTC)

#include "Arduino.h"
#include "stm32h7xx_hal.h"

/* Private variables ---------------------------------------------------------*/
RTC_HandleTypeDef hrtc;
RTC_TimeTypeDef sTime;
RTC_DateTypeDef sDate;
RTC_AlarmTypeDef sAlarm;

//#define RTC_SET_UTC

#define RTC_GET_UTC

/* Function prototypes */
void SystemClock_Config(void);
void Error_Handler(void);
void MX_RTC_Init(void);

void Error_Handler_RTC_INIT(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
    Serial.println("[Err_RTC_INIT] RTC_INIT was failed");
  }
  /* USER CODE END Error_Handler_Debug */
}

void Error_Handler_OSC_INIT(void) {
    while (1) {
          Serial.println("[Err_RCC_OSC_INIT] RCC_OscInitStruct was not HAL_OK.");
        /* User can add custom code to deal with errors */
    }
}

/* Error Handler */
void Error_Handler_CLK_INIT(void) {
    while (1) {
         Serial.println("[Err_RCC_CLK_INIT] RCC_ClkInitStruct was not HAL_OK.");
        /* User can add custom code to deal with errors */
    }
}

void Error_Handler_Calibration(void) {
    while (1) {
         Serial.println("[Err_RTC_CALIBRATION] Calibration was not OK.");
        /* User can add custom code to deal with errors */
    }
}

void Error_Handler(void) {
  while(1) {
    Serial.println("[NOT DEFINED] null ");
  }
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Supply configuration update enable
  */
  HAL_PWREx_ConfigSupply(PWR_DIRECT_SMPS_SUPPLY);

  /** Configure the main internal regulator output voltage
  */
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);

  while(!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {}

  /** Configure LSE Drive Capability
  */
  HAL_PWR_EnableBkUpAccess();
  __HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_LOW);

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_LSE;
   //RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;
  //RCC_OscInitStruct.LSEState = RCC_LSE_ON;
  RCC_OscInitStruct.HSIState = RCC_HSI_DIV1;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler_OSC_INIT();
  }

  /** 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_D3PCLK1|RCC_CLOCKTYPE_D1PCLK1;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB3CLKDivider = RCC_APB3_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_APB2_DIV1;
  RCC_ClkInitStruct.APB4CLKDivider = RCC_APB4_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  {
    Error_Handler_CLK_INIT();
  }

  /** Enables the Clock Security System
  */
  HAL_RCCEx_EnableLSECSS();
}

/**
  * @brief RTC Initialization Function
  * @param None
  * @retval None
  */
void MX_RTC_Init(void)
{

  /* USER CODE BEGIN RTC_Init 0 */

  /* USER CODE END RTC_Init 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.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
  hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
  hrtc.Init.OutPutRemap = RTC_OUTPUT_REMAP_NONE;
  if (HAL_RTC_Init(&hrtc) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN RTC_Init 2 */

  /* USER CODE END RTC_Init 2 */

}

/**
  * @brief RTC Initialization Function
  * @param None
  * @retval None
  */
// void MX_RTC_Init(void)
// {

//   /* USER CODE BEGIN RTC_Init 0 */

//   /* USER CODE END RTC_Init 0 */

//   // RTC_TimeTypeDef sTime = {0};
//   // RTC_DateTypeDef sDate = {0};
//   // RTC_AlarmTypeDef sAlarm = {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.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
//   hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
//   hrtc.Init.OutPutRemap = RTC_OUTPUT_REMAP_NONE;
//   if (HAL_RTC_Init(&hrtc) != HAL_OK)
//   {
//     Error_Handler();
//   }

//   /* USER CODE BEGIN Check_RTC_BKUP */

//   /* USER CODE END Check_RTC_BKUP */

//   /** Initialize RTC and set the Time and Date
//   */

//   // sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
//   // sTime.StoreOperation = RTC_STOREOPERATION_SET;
//   if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD) != HAL_OK)
//   {
//     Error_Handler();
//   }
//   sDate.WeekDay = RTC_WEEKDAY_MONDAY;
//   sDate.Month = RTC_MONTH_JANUARY;
//   sDate.Date = 0x1;
//   sDate.Year = 0x0;
//   if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BCD) != HAL_OK)
//   {
//     Error_Handler();
//   }

//   /** Enable the Alarm A
//   */
//   sAlarm.AlarmTime.Hours = 0x0;
//   sAlarm.AlarmTime.Minutes = 0x0;
//   sAlarm.AlarmTime.Seconds = 0x0;
//   sAlarm.AlarmTime.SubSeconds = 0x0;
//   sAlarm.AlarmTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
//   sAlarm.AlarmTime.StoreOperation = RTC_STOREOPERATION_RESET;
//   sAlarm.AlarmMask = RTC_ALARMMASK_DATEWEEKDAY|RTC_ALARMMASK_HOURS |RTC_ALARMMASK_MINUTES;
//   sAlarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_ALL;
//   sAlarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_DATE;
//   sAlarm.AlarmDateWeekDay = 0x1;
//   sAlarm.Alarm = RTC_ALARM_A;
//   if (HAL_RTC_SetAlarm_IT(&hrtc, &sAlarm, RTC_FORMAT_BCD) != HAL_OK)
//   {
//     Error_Handler();
//   }
//   /* USER CODE BEGIN RTC_Init 2 */

//   /* USER CODE END RTC_Init 2 */

// }

void rtc_set_time()
{
  
  sTime.Hours = 0x12;
  sTime.Minutes = 0x0;
  sTime.Seconds = 0x01;
  HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN);
  Serial.print("[Last] RTC SET TIME: ");
  Serial.print(sTime.Hours);
  Serial.print(":");
  Serial.print(sTime.Minutes);
  Serial.print(":");
  Serial.println(sTime.Seconds);
  delay(1000);
}

void rtc_get_time()
{
  HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BIN);
  Serial.print("[Last] GET_Current Time: ");
  Serial.print(sTime.Hours);
  Serial.print(":");
  Serial.print(sTime.Minutes);
  Serial.print(":");
  Serial.println(sTime.Seconds);
  delay(1000);
}

void setup() {
  // put your setup code here, to run once:

  HAL_Init();

  SystemClock_Config();

  MX_RTC_Init();

Serial.begin(9600);
delay(4000);
Serial.println("test");
delay(1000);
Serial.println(RCC->BDCR, BIN);


  #ifdef RTC_SET_UTC
  rtc_set_time();
  #endif
}

void loop() {
  
#ifdef RTC_GET_UTC
  rtc_get_time();

  delay(4000);
Serial.println("test");
delay(1000);
Serial.println(RCC->BDCR, BIN);

 #endif
}

"Since errors persist, I am unable to proceed to the next step in the flow."

11:30:00.658 -> [Err_RCC_OSC_INIT] RCC_OscInitStruct was not HAL_OK.

I changed from

RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_LSE;
RCC_OscInitStruct.LSEState = RCC_LSE_ON;

to

RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_LSE;
//RCC_OscInitStruct.LSEState = RCC_LSE_ON;

1:35:08.610 -> [Err_RCC_OSC_INIT] RCC_OscInitStruct was not HAL_OK.

11:35:08.610 -> [Err_RCC_OSC_INIT] RCC_OscInitStruct was not HAL_OK.

11:35:08.610 -> [Err_RCC_OSC_INIT] RCC_OscInitStruct was not HAL_OK.

Green : RTC_SET_TIME : 18:0:1

Red : RTC_GET_TIME (First try), Serial log : Plus ,41Seconds, but GET TIME : Plus ,29Seconds

Blue: RTC_GET_TIME(Second try), Serial log : Plus,18Seconds, but GET TIME : Plus, 12 Seconds

Regist value is

11:37:03.053 -> test
11:37:04.052 -> 1000001001100000 ("Always the same")

My question is

  1. LSE is not working, cause it can't be detected. [Err_RCC_OSC_INIT] RCC_OscInitStruct was not HAL_OK. is right?

  2. RTC is working, but abnormal -> it maybe can't use LSE(32.768Khz)

  • ps "For testing purposes, I proceeded by skipping the part with while((__HAL_RCC_GET_FLAG(RCC_FLAG_D2CKRDY) != RESET) && (timeout-- > 0));

Thanks.

Compared to the standalone code I provided, have you noticed that LSEON bit has been cleared in your code when looking at the register read?

1 Like

Yes, I noticed that.

your code is 13:00:23.576 -> 1000001001100101

and my code is

LSEON bit was cleared. LSEON is 0bit

Yes, but in running the small code I provided your bit:0 was set(1). On running your code, with that small code included, I now see that that bit is cleared.

Again, I must confess I have very little experience in this. What I have shared in helping you with this is only what I have learned in the past day or two. My experience, however modest it may be, has been solely with AVRs, and STM32 is totally new to me.

I now see that that bit is cleared.

Again, I must confess I have very little experience in this. -> On my code, i failed activated LSE CLOCK 1:35:08.610 -> [Err_RCC_OSC_INIT] RCC_OscInitStruct was not HAL_OK.

on state, LSE CLOCK wasn't set 1 (Cause,

//RCC_OscInitStruct.LSEState = RCC_LSE_ON

:wink:

By the way,
In this situation, do you think the RTC will operate normally if I forcibly create a register, turn on LSE, enable the Clock Security System (CSS), and set RTC Clock Source to LSE?
(

RCC->BDCR = 0b1000000100110011;) 

It seems that hardware-wise, LSE is not properly configured as artwork. Is there any way to determine directly if the Clock source is not supplied from LSE? Is checking the value of Bit 6 LSECSSD (LSE clock security system failure detection) in the
`

Serial.println(RCC->BDCR, BIN);

`
you mentioned earlier the only way to confirm that it is 1, indicating failure detected on the 32 kHz oscillator? It seems we are almost finish. The conclusion is in sight.

@joy-en
I want to follow up see what progress you may have made.

Some thoughts since last speaking on the LSECSSD...

I will presume to take "artwork" == "ought to work", and I think you may be correct. I am about one week out from having posted a question to the A-Team on this exact matter, with regard the GIGA R1, elsewhere.

As well,