Timers

This section is aimed to provide users an overview on available timers, timer usage, and availability across various power states.

Table 7. Timer Overview

Timer

Availability in Power Mode

Resolution

General Purpose Timer

Available in Active mode and Idle mode.

(1/48)us

Watchdog Timer

Available in Active mode, Idle mode, and Standby mode.

(1/32)us

System Timer

Available in Active mode and Idle mode.

1us or 250ns

Real-Time Clock

Available in Active mode, Idle mode, and Standby mode.

8us or 524ms

Clock DPL

Available in Active mode, Idle mode, and Standby mode.

1us

FreeRTOS Timer

Available in Active mode and Idle mode.

10ms

Hardware Timers

There are 4 types of HW timer available for the application to use.

General Purpose Timer (LGPT)

The General Purpose Timer (LGPT) runs off of the High Frequency Clock, which is only available in Active or Idle mode. LGPT is often used for high accuracy measurement, timer comparison, DMA triggering, PWM, etc. For more information, please refer to the CC23xx datasheet.

Watchdog Timer

The Watchdog Timer is used as a recovery mechanism to regain control. Upon timer expiration, the CC23xx will be reset. Because the CC23xx behaves differently than previous generations, a workaround is needed to reduce the power consumption of the Watchdog Timer. The Watchdog Timer Example Code explains this workaround.

Real-Time Clock (RTC)

The RTC is a 2-channel timer (compare channel and capture channel) and runs on the Low Frequency Clock. The compare channel is used by system software to program the CC23xx’s future wake up time before going into Standby mode.

System Timer (SYSTIM)

SYSTIM has 5 channels; 4 channels are reserved by system software and radio operation. There is only one available for the user. SYSTIM is synchronized to RTC in hardware.

To get current device timestamp, users should use ClockP_getSystemTicks() from ClockP Module.

Attention

For more Hardware Timer information, please refer to the CC23xx datasheet. In general, it is recommended to use ClockP Module if users want to realize the functionality provided by SYSTIM.

Software Timers

ClockP Module

The ClockP module is developed by Texas Instruments and uses one SYSTIM channel with 1 us resolution and is synchronized with the RTC.

ClockP can be used to do the following:

  1. Get current timestamp

  2. Get CPU clock frequency

  3. Periodic timer to trigger periodic task

  4. One shot timer to trigger task

  5. Refresh the watchdog when standby is enabled.

Note

To see the available APIs, please navigate to TI Drivers API Reference

Using the ClockP module, a user can make multiple instances of “clocks” in their application, where a “clock” refers to a distinct instance of ClockP. The “clocks” constructed can either be one-shot or periodic, and can each have different timeouts (and periods, for the periodic clocks.) Given that the ClockP module allows for creation of clocks that can run in standby, it is also possible to create a clock instances that can help achieve a variety of objectives which involve waking the device up from standby to do something.

Examples of applications where a ClockP based clock can be used:

  1. Non-blocking way of scheduling a function call for a given delta of time in the future (A one-shot ClockP based clock can be used for this.)

  2. Trigger a task to start execution upon waking up from standby. This can be achieved together with the use of semaphores to synchronize activities in the system.

The ClockP module keeps track of all the clock instances that have been created in the application. It then dynamically checks for the timeout that is closest in the near future, and programs that to be the next timeout duration for the System Timer. The System Timer then generates an interrupt upon the expiration of the timeout, which eventually calls the user defined callback for the clock that timed-out.

Upon timeout, the ClockP module calls a user-defined callback function. In the case of multiple ClockP instances, the user has two choices when it comes to defining a callback function. It is possible to define individual callback functions to each of the clocks. Alternatively, it is also possible to define the same callback function for each of the clocks, but assign a unique argument to that clock instance. In the case of the timeout, and having a shared callback function for all the ClockP based clocks in the system, the callback function will be called, and the user can add handling in the callback function to determine which clock timed-out by processing the argument that the callback function was called with.

For more information on how the ClockP module can be used in an embedded application, please refer to the following training that showcases how a ClockP clock can be used together with the Data Stream Example: ClockP with the Basic_Ble example

Following is a simple code snippet that shows how to initialize a One-shot clock instance. (A similar procedure can be followed for creating a periodic clock):

Listing 24. Following is the variable, and callback definition required before initializing dockerany ClockP clock:
 1   // Include the ClockP header file that contains the variable and function definitions
 2   #include <ti/drivers/dpl/ClockP.h>
 3
 4   /* The following three variables can be either global or local. Note that the
 5   * clockParams and the myClockHandle variables must not be shared between clock
 6   * instances. It is quintessential that each clock instance have it's own clock
 7   * handle, and clock parameters.
 8   */
 9
10   // Struct that contains ClockP_parameters
11   ClockP_Params clockParams;
12
13   // ClockP handle that will be assigned to this specific ClockP clock.
14   ClockP_Handle myClockHandle;
15
16   // Argument used for this clock instance
17   uintptr_t argForClockP;
18
19   // Function prototype for the user-defined callback function:
20   uintptr_t somePointer_1;
21   uintptr_t somePointer_2;
22   void myClockTimeoutHandler (uintptr_t arg)
23   {
24      /*
25      * User defined code here:
26      */
27      if(arg == somePointer_1)
28      {
29         // Do something
30      }
31      else if ( arg == somePointer_2)
32      {
33         // Do something different
34      }
35      else
36      {
37         // Do something else. In this example code, the "else" block will run.
38      }
39   }

Add the following inside the function body where you want to start the clock from:

 1   argForClockP = 0xAA;
 2
 3   ClockP_Params_init(&clockParams);
 4
 5   // Period for the clock, 0 indicates one-shot clock
 6   clockParams.period    = 0;
 7   clockParams.startFlag = false;
 8
 9   /* .arg is the argument used for this clock instance. It will be passed onto
10   * the user-defined callback function. The argument must be of type uintptr_t.
11   */
12   clockParams.arg    = argForClockP;
13   myClockHandle      = ClockP_create(myClockTimeoutHandler, 0, &clockParams);
14   if (NULL == myClockHandle)
15   {
16      return 0;
17   }
18
19   // Set the clock timeout to be equal to 1s
20   ClockP_setTimeout(myClockHandle, 1000000);
21
22   // Start the clock
23   ClockP_start(myClockHandle);

FreeRTOS Timer

FreeRTOS has its own timer module. For more information, please refer to: FreeRTOS Software Timers