2021-01-19 08:45 PM
I seem to be unable to get the RTC to start. I can set the time, and read from it, but it doesn't increment.
I have tried using the external and internal oscillator, neither have been successful.
Everything has been set up with CubeMx, and I have used the RTC before on an STM32L0 micro with no issue, comparing it to my old code I cant see what I am doing differently.
I have attached the relevant parts of the code, if you know what I am missing please let me know.
Thanks,
Luke
static void MX_RTC_Init(void)
{
RTC_TimeTypeDef sTime;
RTC_DateTypeDef sDate;
/**Initialize RTC Only
*/
hrtc.Instance = RTC;
if(HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR0) != 0x32F2){
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;
if (HAL_RTC_Init(&hrtc) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
/**Initialize RTC and set the Time and Date
*/
sTime.Hours = 0x0;
sTime.Minutes = 0x0;
sTime.Seconds = 0x0;
sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
sTime.StoreOperation = RTC_STOREOPERATION_RESET;
if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
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(__FILE__, __LINE__);
}
HAL_RTCEx_BKUPWrite(&hrtc,RTC_BKP_DR0,0x32F2);
}
}
/* USER CODE BEGIN PV */
RTC_TimeTypeDef sTimeGet;
RTC_TimeTypeDef sTimeSet;
/* Private variables ---------------------------------------------------------*/
/* USER CODE END PV */
void updateTime(void){
//RTC_TimeTypeDef sTimeGet;
//RTC_TimeTypeDef sTimeSet;
HAL_RTC_GetTime(&hrtc, &sTimeGet, RTC_FORMAT_BIN);
seconds = sTimeGet.Seconds;
minutes = sTimeGet.Minutes;
hours = sTimeGet.Hours;
if(hours>12){
hours = 0;
sTimeSet.Hours = hours;
HAL_RTC_SetTime(&hrtc, &sTimeSet, RTC_FORMAT_BIN);
}
}
Solved! Go to Solution.
2021-01-20 12:26 AM
2021-01-19 11:21 PM
Do you read date after reading time?
JW
2021-01-19 11:26 PM
No I haven't tried. I likely wont end up using that function.
2021-01-19 11:27 PM
Call HAL_RTC_GetDate() afterwards even if you don't use it. There is a note about that in the source code. The topic pops up here frequently.
2021-01-20 12:26 AM
2021-01-20 10:36 PM
No I had completely missed it. I checked it out now.
I tried both, together and independently and I am got the same result.
I currently am reading the date after the time. Strangely if i step my way through the time and date my time variables update, but if i run through like normal they don't, do you have any thoughts on this?
void updateTime(void){
//RTC_TimeTypeDef sTimeGet;
//RTC_TimeTypeDef sTimeSet;
RTC_DateTypeDef sDateGet;
HAL_RTC_GetTime(&hrtc, &sTimeGet, RTC_FORMAT_BIN);
HAL_RTC_GetDate(&hrtc, &sDateGet, RTC_FORMAT_BIN);
seconds = sTimeGet.Seconds;
minutes = sTimeGet.Minutes;
hours = sTimeGet.Hours;
if(hours>12){
hours = 0;
sTimeSet.Hours = hours;
HAL_RTC_SetTime(&hrtc, &sTimeSet, RTC_FORMAT_BIN);
}
}
2021-01-20 10:36 PM
I currently am reading the date after the time. Strangely if i step my way through the time and date my time variables update, but if i run through like normal they don't, do you have any thoughts on this?
void updateTime(void){
//RTC_TimeTypeDef sTimeGet;
//RTC_TimeTypeDef sTimeSet;
RTC_DateTypeDef sDateGet;
HAL_RTC_GetTime(&hrtc, &sTimeGet, RTC_FORMAT_BIN);
HAL_RTC_GetDate(&hrtc, &sDateGet, RTC_FORMAT_BIN);
seconds = sTimeGet.Seconds;
minutes = sTimeGet.Minutes;
hours = sTimeGet.Hours;
if(hours>12){
hours = 0;
sTimeSet.Hours = hours;
HAL_RTC_SetTime(&hrtc, &sTimeSet, RTC_FORMAT_BIN);
}
}
2021-01-20 11:07 PM
You may use STM32CubeIDE to generate default code for init. Mine looks like (STM32L4, RTC running on LSI clock):
/**
* @brief RTC Initialization Function
* @param None
* @retval None
*/
static 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};
/* USER CODE BEGIN RTC_Init 1 */
/* USER CODE END RTC_Init 1 */
/** Initialize RTC Only
*/
hrtc.Instance = RTC;
hrtc.Init.HourFormat = RTC_HOURFORMAT_12;
hrtc.Init.AsynchPrediv = 127;
hrtc.Init.SynchPrediv = 249;
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;
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.Hours = 1;
sTime.Minutes = 0;
sTime.Seconds = 0;
sTime.TimeFormat = RTC_HOURFORMAT12_AM;
sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
sTime.StoreOperation = RTC_STOREOPERATION_RESET;
if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN) != HAL_OK)
{
Error_Handler();
}
sDate.WeekDay = RTC_WEEKDAY_MONDAY;
sDate.Month = RTC_MONTH_JANUARY;
sDate.Date = 1;
sDate.Year = 0;
if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN RTC_Init 2 */
/* USER CODE END RTC_Init 2 */
}
and reading
HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BIN);
HAL_RTC_GetDate(&hrtc, &sDate, RTC_FORMAT_BIN);
in a loop is sufficient. The RTC can handle AM/PM if configured so.
hth
KnarfB
2021-01-20 11:16 PM
Sorry but I dont understand how this is much different than the init code I already have. What would this do differently?
I am calling my updateTime() function, which contains the getTime getDate, with a timer interrupt and it is being called regularly.
2021-01-20 11:24 PM
My code is only for your reference, there must be differences, othewise your code would work. Your code snippet does not define sTimeSet, must be defined elsewhere,...