The source files for the RTCDRV device driver library resides in the emdrv/rtcdrv folder, and are named rtcdriver.c and rtcdriver.h.
The RTCDRV driver use the RTC peripheral of a device in Silicon Laboratories Gecko microcontroller family to provide a user configurable number of software millisecond timers. Two kinds of timers are supported; oneshot timers and periodic timers. Timers will, when their timeout period has expired, call a user supplied callback function.
In addition to the timers, RTCDRV also offers an optional wallclock functionality. The wallclock keeps track of the number of seconds elapsed since RTCDRV was initialized.
Some properties of the RTCDRV driver are compile-time configurable. These properties are stored in a file named rtcdrv_config.h. A template for this file, containing default values, resides in the emdrv/config folder. Currently the configuration options are:
When integration with the SLEEP driver is enabled, RTCDRV will keep the SLEEP driver updated with regards to which energy mode that can be safely used.
To configure RTCDRV, provide your own configuration file. Here is a sample rtcdrv_config.h file:
#ifndef __SILICON_LABS_RTCDRV_CONFIG_H__ #define __SILICON_LABS_RTCDRV_CONFIG_H__ // Define how many timers RTCDRV provide. #define EMDRV_RTCDRV_NUM_TIMERS (4) // Uncomment the following line to include wallclock functionality. //#define EMDRV_RTCDRV_WALLCLOCK_CONFIG // Uncomment the following line to enable integration with SLEEP driver. //#define EMDRV_RTCDRV_SLEEPDRV_INTEGRATION #endif
This section contain brief descriptions of the functions in the API. You will find detailed information on input and output parameters and return values by clicking on the hyperlinked function names. Most functions return an error code, ECODE_EMDRV_RTCDRV_OK is returned on success, see ecode.h and rtcdriver.h for other error codes.
Your application code must include one header file: rtcdriver.h.
All functions defined in the API can be called from within interrupt handlers.
RTCDRV_Init(), RTCDRV_DeInit()
These functions initializes or deinitializes the RTCDRV driver. Typically RTCDRV_Init() is called once in your startup code.
RTCDRV_StartTimer(), RTCDRV_StopTimer()
Start/Stop a timer. When a timer expire, a user supplied callback function is called. A pointer to this function is passed to
RTCDRV_StartTimer(). Refer to TimerCallback for details of the callback prototype. Note that it is legal to start an already started timer, it will then just be restarted with the new timeout value.
RTCDRV_AllocateTimer(), RTCDRV_FreeTimer()
Reserve/release a timer. Many functions in the API require a timer ID as input parameter. Use RTCDRV_AllocateTimer() to aquire such a reference.
RTCDRV_TimeRemaining()
Get time left to timer expiry.
RTCDRV_Delay()
Millisecond delay function. This is an "active wait" delay function.
RTCDRV_IsRunning()
Check if a timer is running.
RTCDRV_GetWallClock(), RTCDRV_SetWallClock()
Get or set wallclock time.
RTCDRV_GetWallClockTicks32(), RTCDRV_GetWallClockTicks64()
Get wallclock time expressed as number of RTC/RTCC counter ticks, available both as 32bit and 64 bit values.
RTCDRV_MsecsToTicks(), RTCDRV_SecsToTicks(), RTCDRV_TicksToMsec(), RTCDRV_TicksToSec()
Conversion functions between seconds, milliseconds and RTC/RTCC counter ticks.
The timer expiry callback function:
The callback function, prototyped as RTCDRV_Callback_t(), is called from within the RTC peripheral interrupt handler on timer expiry. RTCDRV_Callback_t( RTCDRV_TimerID_t id ) is called with the timer id as input parameter.
The timer type:
Timers are either of oneshot type or of periodic type.
The timer type is an enumeration, see RTCDRV_TimerType_t for details.
#include "rtcdriver.h" int i = 0; RTCDRV_TimerID_t id; void myCallback( RTCDRV_TimerID_t id ) { // Timer has elapsed ! i++; if ( i < 10 ) { // Restart timer RTCDRV_StartTimer( id, rtcdrvTimerTypeOneshot, 100, myCallback ); } } int main( void ) { // Initialization of RTCDRV driver RTCDRV_Init(); // Reserve a timer RTCDRV_AllocateTimer( &id ); // Start a oneshot timer with 100 millisecond timeout RTCDRV_StartTimer( id, rtcdrvTimerTypeOneshot, 100, myCallback ); }