cancel
Showing results for 
Search instead for 
Did you mean: 

Using the RTC

dscdsc
Senior
Posted on March 20, 2015 at 14:12

I'm having issues making the RTC work as expected.

This is the code which is called periodically:

RTC_TimeTypeDef currentTime;
HAL_RTC_GetTime(&hrtc, &currentTime, FORMAT_BIN);
char
uartMsg[32] = 
''Time: ''
;
char
time[16] = {0};
sprintf(time, 
''%d:%d:%d %d\n''
, currentTime.Hours, currentTime.Minutes, currentTime.Seconds, currentTime.SubSeconds);
strcat(uartMsg, time);
HAL_UART_Transmit(&huart1, (uint8_t *)uartMsg, strlen(uartMsg), 10000);

The output is always something like this:

Time: 0:0:0 29
Time: 0:0:0 27
Time: 0:0:0 25
Time: 0:0:0 24

With hours, minutes and seconds staying at 0, while sub-seconds oscillate periodically between 0 and RTC initialization code generated by CubeMX:

void
MX_RTC_Init(
void
)
{
RTC_TimeTypeDef sTime;
RTC_DateTypeDef sDate;
RTC_AlarmTypeDef sAlarm;
/* Initialize RTC and set the Time and Date */
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;
HAL_RTC_Init(&hrtc);
sTime.Hours = 0;
sTime.Minutes = 0;
sTime.Seconds = 0;
sTime.SubSeconds = 0;
sTime.TimeFormat = RTC_HOURFORMAT12_AM;
sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
sTime.StoreOperation = RTC_STOREOPERATION_RESET;
HAL_RTC_SetTime(&hrtc, &sTime, FORMAT_BIN);
sDate.WeekDay = RTC_WEEKDAY_MONDAY;
sDate.Month = RTC_MONTH_JANUARY;
sDate.Date = 1;
sDate.Year = 0;
HAL_RTC_SetDate(&hrtc, &sDate, FORMAT_BIN);
/* Enable the Alarm A */
sAlarm.AlarmTime.Hours = 0;
sAlarm.AlarmTime.Minutes = 0;
sAlarm.AlarmTime.Seconds = 0;
sAlarm.AlarmTime.SubSeconds = 0;
sAlarm.AlarmTime.TimeFormat = RTC_HOURFORMAT12_AM;
sAlarm.AlarmTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
sAlarm.AlarmTime.StoreOperation = RTC_STOREOPERATION_RESET;
sAlarm.AlarmMask = RTC_ALARMMASK_NONE;
sAlarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_ALL;
sAlarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_DATE;
sAlarm.AlarmDateWeekDay = 1;
sAlarm.Alarm = RTC_ALARM_A;
HAL_RTC_SetAlarm(&hrtc, &sAlarm, FORMAT_BIN);
}

With different values of

AsynchPrediv

and

SynchPrediv, hours, minutes and seconds are set to different initial values (not all 0's as by default), but they never change with time not matter what I try.
 
 I'm using STM32F051 and RTC is clocked by 40kHz LSI.
 
 What's the problem?
 

#rtc #stm32
16 REPLIES 16
dscdsc
Senior
Posted on March 25, 2015 at 09:32

Bump.

stm32forum
Associate II
Posted on March 25, 2015 at 12:08

What's your clock setup code?
 
 

dscdsc
Senior
Posted on March 25, 2015 at 12:31

Code:

void
SystemClock_Config(
void
)
{
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_PeriphCLKInitTypeDef PeriphClkInit;
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_LSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = 16;
RCC_OscInitStruct.LSIState = RCC_LSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL10;
RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1;
HAL_RCC_OscConfig(&RCC_OscInitStruct);
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1);
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1|RCC_PERIPHCLK_RTC;
PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK1;
PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);
__SYSCFG_CLK_ENABLE();
}

Diagram: 0690X000006031BQAQ.jpg
dscdsc
Senior
Posted on March 30, 2015 at 13:07

Bump #2.

dscdsc
Senior
Posted on March 30, 2015 at 15:22

Update:

RTC works as expected when single-stepping through (I've added synchronisation function just in case) this code:

HAL_RTC_WaitForSynchro(&hrtc);
HAL_RTC_GetTime(&hrtc, &currentTime, FORMAT_BIN);

But when I let the code execute freely (this code snippet is called ~ once a second), it stops changing all the components except sub-seconds.
dscdsc
Senior
Posted on March 30, 2015 at 15:53

Thanks, seems like ST keeps up the efforts to make sure we do not forget about the underlying hardware while using their API design.

schubertjw
Associate II
Posted on April 16, 2015 at 08:21

Dear members of forum,

I am a beginner with stm32f0xx-DISCOVERY. But I have some experiences with PIC programming I dit not turn out to use with RTC. I would like to write time and date to a HD44780 display. The display is working and the RTC does not work. I did not find any working complett example program to learn. There are example program for stm32f1, stm32f4 but not for stm32f0.  So if it is possible can anybody to upload an complet example or send me to schubertjw@gmail.com. If it is not possible I would like to exuse me......... 

Sorry for my basic asking...

Best regards,

Jozsef Schubert

Posted on April 17, 2015 at 12:19

Szia Jozsef,

1. start your own thread

2. you can use example from any other family as the start, RTCs are very similar to each other.

JW