Clock project - is RTC available?

Hi,
I would like to use Core and LCD module as a clock, so I’m looking for the way, how I can store current time/date. In SDK, there is a bc_rtc driver, but is blank. So my question is, whether there is RTC available on Core, or I have to use external module, or less precise way without RTC.

In future, I would like use Turris as a source of exact time, so less precise mode should be not an issue. But having RTC makes it easier :wink:

1 Like

Hello,

yes, the RTC subsytem with 32kHz crystal is running for the needs of scheduler, but there is no implementation yet for date/time.

For now it should be possible to use STM32 HAL library, which is in the BigClown SDK, hovewer we try to avoid these libraries because of the code size and implement the functionality ourselves.

sdk\stm\hal\inc\stm32l0xx_hal_rtc.h

and functions

/* RTC Time and Date functions ************************************************/
HAL_StatusTypeDef HAL_RTC_SetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format);
HAL_StatusTypeDef HAL_RTC_GetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format);
HAL_StatusTypeDef HAL_RTC_SetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format);
HAL_StatusTypeDef HAL_RTC_GetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format);

There is only small issue that you need to pass the RTC_HandleTypeDef parameter which has to have set the Instance item in the structure. Or you can search for these functions and implement them without the need of the RTC_HandleTypeDef structure. You can access directly the date register with only RTC->DR.

We try to implement this part in the SDK, it is not the first request and for future crypto we would need know the time anyway.

Let us know if this helped you or you would need some more help.
Martin

Hi,
thanks for the info.

Hi,

I’ve discovered it is quite easy to work with the RTC. It is decribed here:

Date and time are stored as BCD(binary coded decimals) in two 32bit registers:

  • DR:=ddwwmmyy
  • TR:=00HHMMSS

Reading them is simple. You have to include file:
#include <stm32l0xx.h>
and then you are able to read time register:
RTC->TR;
and date register
RTC->DR;

But writing these registers is tricky, because they are write protected. It has to be done like this:

// Disable write protection
RTC->WPR = 0xca;
RTC->WPR = 0x53;

// Enable initialization mode, this stops the clock
RTC->ISR |= RTC_ISR_INIT;

// Wait for RTC to be in initialization mode...
while ((RTC->ISR & RTC_ISR_INITF) == 0)
{
    continue;
}

// Write the values
RTC->DR = date;
RTC->TR = time;

// Exit from initialization mode
RTC->ISR &= ~RTC_ISR_INIT;

// Enable write protection
RTC->WPR = 0xff;
1 Like

Thanks, I’ll check it.

It seems like bc_rtc.c is good file for start :smiley:

#include <bc_rtc.h>
#include <bc_irq.h>
#include <stm32l0xx.h>

void bc_rtc_init(void)
{

}

There was something in past: https://github.com/bigclownlabs/bcf-sdk/blob/56ace1d5ad0a7d090d5fb1b69818110cd06429ff/bcl/src/bc_rtc.c

Contents of earlier version of this file were moved to bc_module_core.c, mainly to function _bc_module_core_init_rtc. That is the place, where I got inspiration for code I posted earlier :slight_smile:

OK. So we need to implement bc_rtc_get_time(), bc_rtc_get_date() and bc_rtc_set_datetime() functions.

I’ve ordered DCF1 arduino module and would like to use it for initial setting and later corrections.

It looks like formats of those registers I’ve shown earlier are wrong. The time is almost correct, but the date is completely different.

Actual format is this:

TR := 00000000 0AHHHHHH 0MMMMMMM 0SSSSSSS
DR := 00000000 yyyyyyyy wwwmmmmm 00dddddd

where

  • A is AM/PM flag (but this is actually not used now, because the clock is configured in 24h mode)
  • H are bits for hours (00…23)
  • M are minutes (00…59)
  • S are seconds (00…59)
  • y are years (00…99)
  • w is number of day in a week (1…7)
  • m are months (01…12)
  • d are days (01…28/29/30/31)

some macros showing positions of the bits are defined in header stm32l083xx.h around line 5000