2017-10-23 07:55 AM
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:
.
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:
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-stm32f1xx2017-10-23 10:07 AM
It is an arbitrary number, not an unlock, merely there to confirm if your code initialized things or not.
The RTC on the F1 series is a 32-bit second counter, any time/date handling is specific to the HAL implementation
2017-10-24 02:25 AM
Hi Clive,
thanks for the reply.
I read the RTC HAL code and the HAL library compute the data transformation from counter value to date time.
This is the structure where the HAL return the date:
typedef struct
{ uint8_t WeekDay; /*!< Specifies the RTC Date WeekDay (not necessary for HAL_RTC_SetDate). This parameter can be a value of @ref RTC_WeekDay_Definitions */ uint8_t Month; /*!< Specifies the RTC Date Month (in BCD format). This parameter can be a value of @ref RTC_Month_Date_Definitions */uint8_t Date; /*!< Specifies the RTC Date.
This parameter must be a number between Min_Data = 1 and Max_Data = 31 */ uint8_t Year; /*!< Specifies the RTC Date Year. This parameter must be a number between Min_Data = 0 and Max_Data = 99 */ }RTC_DateTypeDef;The year is between 0 - 99.
How I can set the year? i.e 2017? Should I set 2017 - 2000 or 2017 -1970 (unix time compliant)?
Thanks for your time.
2017-10-24 11:01 AM
Hello Clive,
now the RTC works with the HAL functions. I assumes that the time starts from year 2000.
Now I have a new problem. When I reboot the system via a reset the Time remains stored into the RTC but I lost the Date. The date start from the beginning (01/01/2000).
I check if during the initialisation, the RTC is set again but BKP register D1 is set to 0x32F2 and the RTC CUBE initialisation is skipped.
Any clues?
Thanks!