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:
- 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?
- 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.)