Skip to main content
Fede Rico
Associate III
October 23, 2017
Question

RTC usage on F1 serie

  • October 23, 2017
  • 3 replies
  • 770 views
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
This topic has been closed for replies.

3 replies

Tesla DeLorean
Guru
October 23, 2017
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 (See Profile) Up vote any posts that you find helpful, it shows what's working..
Fede Rico
Fede RicoAuthor
Associate III
October 24, 2017
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.