Firmware SDK
|
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. | |
Driver for real-time clock.
uint32_t twr_rtc_datetime_to_timestamp | ( | struct tm * | tm | ) |
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:
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.
[out] | tm | A pointer to target struct tm variable to hold the result |
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:
The function uses most recent value memoization and pre-computed conversion tables to amortize the run time across successive invocations.
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.
[in] | tm | Struct tm with calendar date and time to set in the RTC |
[in] | ms | Sub-second time (number of milliseconds, 0 if unknown) |