Mistake on this page?
Report an issue in GitHub or email us
Modules | Functions
RTC hal

The RTC hal provides a low level interface to the Real Time Counter (RTC) of a target. More...

Modules

 RTC hal tests
 The RTC test validate proper implementation of the RTC hal.
 

Functions

void rtc_init (void)
 Initialize the RTC peripheral. More...
 
void rtc_free (void)
 Deinitialize RTC. More...
 
int rtc_isenabled (void)
 Check if the RTC has the time set and is counting. More...
 
time_t rtc_read (void)
 Get the current time from the RTC peripheral. More...
 
void rtc_write (time_t t)
 Write the current time in seconds to the RTC peripheral. More...
 

Detailed Description

The RTC hal provides a low level interface to the Real Time Counter (RTC) of a target.

Defined behaviour

Undefined behaviour

Potential bugs

See also
RTC hal tests

Function Documentation

void rtc_free ( void  )

Deinitialize RTC.

Powerdown the RTC in preparation for sleep, powerdown or reset. That should only affect the CPU domain and not the time keeping logic. After this function is called no other RTC functions should be called except for rtc_init.

Note
This function does not stop the RTC from counting - Tested by rtc_persist_test

Example Implementation Pseudo Code:

1 void rtc_free()
2 {
3  // Disable clock gate since processor no longer needs to read RTC registers
4  POWER_CTRL &= ~POWER_CTRL_RTC_Msk;
5 }
void rtc_init ( void  )

Initialize the RTC peripheral.

Powerup the RTC in perpetration for access. This function must be called before any other RTC functions ares called. This does not change the state of the RTC. It just enables access to it.

Note
This function is safe to call repeatedly - Tested by rtc_init_test

Example Implementation Pseudo Code:

1 void rtc_init()
2 {
3  // Enable clock gate so processor can read RTC registers
4  POWER_CTRL |= POWER_CTRL_RTC_Msk;
5 
6  // See if the RTC is already setup
7  if (!(RTC_STATUS & RTC_STATUS_COUNTING_Msk)) {
8 
9  // Setup the RTC clock source
10  RTC_CTRL |= RTC_CTRL_CLK32_Msk;
11  }
12 }
int rtc_isenabled ( void  )

Check if the RTC has the time set and is counting.

Return values
0The time reported by the RTC is not valid
1The time has been set the RTC is counting

Example Implementation Pseudo Code:

1 int rtc_isenabled()
2 {
3  if (RTC_STATUS & RTC_STATUS_COUNTING_Msk) {
4  return 1;
5  } else {
6  return 0;
7  }
8 }
time_t rtc_read ( void  )

Get the current time from the RTC peripheral.

Returns
The current time in seconds
Note
Some RTCs are not synchronized with the main clock. If this is the case with your RTC then you must read the RTC time in a loop to prevent reading the wrong time due to a glitch. The test rtc_glitch_test is intended to catch this bug.

Example implementation for an unsynchronized ripple counter:

1 time_t rtc_read()
2 {
3  uint32_t val;
4  uint32_t last_val;
5 
6  // Loop until the same value is read twice
7  val = RTC_SECONDS;
8  do {
9  last_val = val;
10  val = RTC_SECONDS;
11  } while (last_val != val);
12 
13  return (time_t)val;
14 }
void rtc_write ( time_t  t)

Write the current time in seconds to the RTC peripheral.

Parameters
tThe current time to be set in seconds.

Example Implementation Pseudo Code:

1 void rtc_write(time_t t)
2 {
3  RTC_SECONDS = t;
4 }
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.