cancel
Showing results for 
Search instead for 
Did you mean: 

RTC usage on F1 serie

Fede Rico
Associate III
Posted on October 23, 2017 at 16:55

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:

  1. Is possible to set the 24 hours format? I saw example where the parameter 'hrtc

    .

    Init

    .

    HourFormat

    =

    RTC_HOURFORMAT_24'

     exists.

  2. Regarding the Backup register D1, is the value '0x32F2' platform specific or I can set any values?

  3. 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:

  1. Year start from 0 to 99. To get the year, can I simply add 2000 to 

    dateRtc.Year?

  2. 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
3 REPLIES 3
Posted on October 23, 2017 at 19:07

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

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on October 24, 2017 at 09:25

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.

Posted on October 24, 2017 at 18:01

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!