RTC usage on F1 serie
Hi there,
I started the RTC implementation on my STM32F103 with Cube.
This is the initialisation code developed by Cube:
static void MX_RTC_Init(void)
{RTC_TimeTypeDef sTime;
RTC_DateTypeDef DateToUpdate;/**Initialize RTC Only
*/ hrtc.Instance = RTC; hrtc.Init.AsynchPrediv = RTC_AUTO_1_SECOND; hrtc.Init.OutPut = RTC_OUTPUTSOURCE_ALARM; if (HAL_RTC_Init(&hrtc) != HAL_OK) { _Error_Handler(__FILE__, __LINE__); }/**Initialize RTC and set the Time and Date
*/ if(HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR1) != 0x32F2){ sTime.Hours = 1; sTime.Minutes = 0; sTime.Seconds = 0;if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN) != HAL_OK)
{ _Error_Handler(__FILE__, __LINE__); }DateToUpdate.WeekDay = RTC_WEEKDAY_MONDAY;
DateToUpdate.Month = RTC_MONTH_JANUARY; DateToUpdate.Date = 1; DateToUpdate.Year = 0;if (HAL_RTC_SetDate(&hrtc, &DateToUpdate, RTC_FORMAT_BIN) != HAL_OK)
{ _Error_Handler(__FILE__, __LINE__); }HAL_RTCEx_BKUPWrite(&hrtc,RTC_BKP_DR1,0x32F2);
}}
I have some questions:
- Is possible to set the 24 hours format? I saw example where the parameter 'hrtc
.
Init.
HourFormat=
RTC_HOURFORMAT_24'exists.
Regarding the Backup register D1, is the value '0x32F2' platform specific or I can set any values?
To unlock the Backup register D1, can I write any values different from '0x32F2'?
I wrote these function to read the date and time from the RTC:
StatusCode_e RTC_GetDate( RTC_Date_t* date )
{ StatusCode_e opResult = StatusCode_ERROR; RTC_DateTypeDef dateRtc; if( RTC_MutexWait() != StatusCode_OK ) return StatusCode_ERROR; if( HAL_RTC_GetDate(&hrtc, &dateRtc, RTC_FORMAT_BIN) == HAL_OK ) { date->weekDay = dateRtc.WeekDay; date->date = dateRtc.Date; date->month = dateRtc.Month; date->year = dateRtc.Year; opResult = StatusCode_OK; } RTC_MutexRelease(); return opResult;}StatusCode_e RTC_GetTime( RTC_Time_t* time )
{ StatusCode_e opResult = StatusCode_ERROR; RTC_TimeTypeDef timeRtc; if( RTC_MutexWait() != StatusCode_OK ) return StatusCode_ERROR; if( HAL_RTC_GetTime(&hrtc, &timeRtc, RTC_FORMAT_BIN) == HAL_OK ) { time->seconds = timeRtc.Seconds; time->minutes = timeRtc.Minutes; time->hours = timeRtc.Hours; opResult = StatusCode_OK; } RTC_MutexRelease(); return opResult;}These are my question:
- Year start from 0 to 99. To get the year, can I simply add 2000 to
dateRtc.Year?
When I set the time and/or date, should I implement controls regarding only the data integrity ( i.e. day < 30 or 31) ? For the leap year, does the RTC act by itself?
Thanks in advance
#rtc-backup #rtc-time-and-date #rtc-stm32f1xx