2015-03-20 06:12 AM
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, ¤tTime, 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
2015-03-25 01:32 AM
Bump.
2015-03-25 04:08 AM
What's your clock setup code?
2015-03-25 04:31 AM
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:
2015-03-30 04:07 AM
Bump #2.
2015-03-30 06:22 AM
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, ¤tTime, 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.
2015-03-30 06:41 AM
2015-03-30 06:53 AM
Thanks, seems like ST keeps up the efforts to make sure we do not forget about the underlying hardware while using their API design.
2015-04-15 11:21 PM
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 Schubert2015-04-17 03:19 AM
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