Firmware SDK
twr_rtc

Driver for real-time clock. More...

Macros

#define TWR_RTC_PREDIV_S   256
 
#define TWR_RTC_PREDIV_A   128
 

Functions

void twr_rtc_init (void)
 
void twr_rtc_get_datetime (struct tm *tm)
 
void twr_rtc_get_timestamp (struct timespec *tv)
 
int twr_rtc_set_datetime (struct tm *tm, int ms)
 
uint32_t twr_rtc_datetime_to_timestamp (struct tm *tm)
 Convert date and time to UNIX timestamp. More...
 
void twr_rtc_set_init (bool state)
 Enable or disable RTC initialization mode. More...
 

Variables

int _twr_rtc_writable_semaphore
 Initialize real-time clock.
 

Detailed Description

Driver for real-time clock.

Function Documentation

◆ twr_rtc_datetime_to_timestamp()

uint32_t twr_rtc_datetime_to_timestamp ( struct tm *  tm)

Convert date and time to UNIX timestamp.

Parameters
[in]tmPointer to the date and time structure
Returns
unix timestamp

Definition at line 144 of file twr_rtc.c.

◆ twr_rtc_get_datetime()

void twr_rtc_get_datetime ( struct tm *  tm)

Obtain current date and time from RTC

This function retrieves the current date and time from the RTC peripheral and stores the result broken down in the given struct tm.

The function has been optimized for speed. It uses most recent value memoization to amortize run time across successive invocations. Pre-computed tables are used to speed up leap year and year-of-day conversions. Run times measured with 2.1 MHz system clock are as follows:

  • 88 us on first use or date register (RTC_DR) change
  • 50 us on time register (RTC_TR) change
  • 34 us on sub-second register (RTC_SSR) change only

Thus, when called repeatedly within a one second interval, the first invocation will complete in 50 us and any subsequent invocations will only take 34 us until the RTC_TR register changes.

Warning: The function does not check whether the RTC's shadow registers have been initialized. If invoked in a state where they might not be, e.g., after RTC initialization, system reset, or wake up from deep sleep, you need to perform the check yourself beforehand.

The RTC peripheral must be configured in 24-hour mode.

Parameters
[out]tmA pointer to target struct tm variable to hold the result

Definition at line 173 of file twr_rtc.c.

◆ twr_rtc_get_timestamp()

void twr_rtc_get_timestamp ( struct timespec *  tv)

Obtain current UNIX time from RTC

Retrieve the current date and time from the RTC peripheral and return it in the form of a UNIX timestamp. The timestamp is stored in a struct timespec. The attribute tv_sec contains the number of seconds since the Epoch (January 1st, 1970). The attribute tv_nsec contains an additional number of nanoseconds since tv_sec.

The resolution of this function depends on the value of the synchronous RTC prescaler. With the default value (256), the resolution is about 4 milliseconds.

In order for this function to work correctly, the RTC must be configured in the 24-hour mode, must be configured with UTC date and time, and the year must be between 2000 and 2099 (inclusive).

The run time of this function depends on the value of the synchronous RTC prescaler register. For optimum performance, the register's value should be a power of two minus one. The run times are as follows with the system clock running at 2.1 MHz:

  • 87 us on first use or date register (RTC_DR) change
  • 58 us on time register (RTC_TR) change
  • 38 us on sub-second register (RTC_SSR) change only

The function uses most recent value memoization and pre-computed conversion tables to amortize the run time across successive invocations.

Definition at line 218 of file twr_rtc.c.

◆ twr_rtc_set_datetime()

int twr_rtc_set_datetime ( struct tm *  tm,
int  ms 
)

Set date and time in RTC

This function configures the current date and time in the RTC peripheral. Date and time is passed broken down (struct tm) in the first parameter. A sub-second component is passed as the number of milliseconds in the second parameter.

Parameters
[in]tmStruct tm with calendar date and time to set in the RTC
[in]msSub-second time (number of milliseconds, 0 if unknown)
Returns
0 on success, a negative number on error

Definition at line 270 of file twr_rtc.c.

◆ twr_rtc_set_init()

void twr_rtc_set_init ( bool  state)

Enable or disable RTC initialization mode.

Parameters
[in]stateEnable when true, disable when false

Definition at line 328 of file twr_rtc.c.