px-fwlib 0.10.0
Cross-platform embedded library and documentation for 8/16/32-bit microcontrollers generated with Doxygen 1.9.2
px_systmr.h : Polled software timers

Description

Non-blocking software timers that have to be polled to determine if they have expired.

File(s):

These timers are suitable for applications that are not timing critical. A global counter is incremented with each clock tick and this value is polled to determine if a timer has expired. Provision is made for counter roll-over.

This module depends on a system clock module, e.g. px_sysclk.h : System Clock using a TMRx peripheral to return a counter that is incremented with every system clock tick. The number of ticks per second (Hz) is defined with PX_SYSTMR_TICKS_PER_SEC.

Example:

#include "px_sysclk.h"
#include "px_systmr.h"
#include "px_compiler.h"
void px_systmr_test(void)
{
// Create a timer object
// Initialise module
// Enable interrupts
px_interrupts_enable();
// Start timer with a 250 ms timeout
px_systmr_start(&tmr, TMR_MS_TO_TICKS(250));
// Loop forever
while(true)
{
// Wait until timer has expired
while(!px_systmr_has_expired(&tmr))
{
;
}
// Restart timer
// Do someting...
}
}
void px_systmr_start(px_systmr_t *systmr, const px_systmr_ticks_t delay_in_ticks)
Start a timer.
Definition: px_systmr.c:35
void px_systmr_restart(px_systmr_t *systmr)
Restart a timer with the delay set with px_systmr_start().
Definition: px_systmr.c:109
bool px_systmr_has_expired(px_systmr_t *systmr)
See if a timer has expired.
Definition: px_systmr.c:57
Structure to track state of a timer.
Definition: px_systmr.h:75
void px_sysclk_init(void)
Start system clock (one clock tick every 1/PX_SYSCLK_TICKS_PER_SEC seconds)
Definition: px_sysclk.c:79

Data Structures

struct  px_systmr_t
 Structure to track state of a timer. More...
 

Macros

#define PX_SYSTMR_TICKS_PER_SEC   PX_SYSCLK_CFG_TICKS_PER_SEC
 The number of timer ticks per second. More...
 
#define PX_SYSTMR_MS_TO_TICKS(delay_in_ms)    PX_UDIV_ROUND((delay_in_ms) * PX_SYSTMR_TICKS_PER_SEC, 1000ul)
 Macro used to convert a timeout in milliseconds to timer ticks. More...
 

Typedefs

typedef px_sysclk_ticks_t px_systmr_ticks_t
 Size definition of the tick counter. More...
 

Enumerations

enum  px_systmr_state_t
 Timer state. More...
 

Functions

void px_systmr_start (px_systmr_t *systmr, const px_systmr_ticks_t delay_in_ticks)
 Start a timer. More...
 
bool px_systmr_has_started (const px_systmr_t *systmr)
 See if a timer has been started. More...
 
bool px_systmr_has_expired (px_systmr_t *systmr)
 See if a timer has expired. More...
 
void px_systmr_stop (px_systmr_t *systmr)
 Stop a running timer. More...
 
void px_systmr_restart (px_systmr_t *systmr)
 Restart a timer with the delay set with px_systmr_start(). More...
 
void px_systmr_reset (px_systmr_t *systmr)
 Reset a timer with the delay set with px_systmr_start(). More...
 
void px_systmr_wait (const px_systmr_ticks_t delay_in_ticks)
 Blocking wait for specified number of ticks. More...
 
px_systmr_ticks_t px_systmr_ticks_elapsed (px_systmr_t *systmr)
 Return the number of ticks that have elapsed sinced the timer has been started. More...
 

Data Structure Documentation

◆ px_systmr_t

struct px_systmr_t

Structure to track state of a timer.

Definition at line 74 of file px_systmr.h.

Data Fields
px_systmr_state_t state State of timer: STOPPED, STARTED or EXPIRED.
px_systmr_ticks_t start_tick Tick when timer started.
px_systmr_ticks_t delay_in_ticks Timer delay, used for subsequent timer restarts/resets.

Macro Definition Documentation

◆ PX_SYSTMR_TICKS_PER_SEC

#define PX_SYSTMR_TICKS_PER_SEC   PX_SYSCLK_CFG_TICKS_PER_SEC

The number of timer ticks per second.

Definition at line 59 of file px_systmr.h.

◆ PX_SYSTMR_MS_TO_TICKS

#define PX_SYSTMR_MS_TO_TICKS (   delay_in_ms)     PX_UDIV_ROUND((delay_in_ms) * PX_SYSTMR_TICKS_PER_SEC, 1000ul)

Macro used to convert a timeout in milliseconds to timer ticks.

Parameters
[in]delay_in_msDelay in milliseconds
Returns
Delay in timer ticks

Definition at line 177 of file px_systmr.h.

Typedef Documentation

◆ px_systmr_ticks_t

Size definition of the tick counter.

Definition at line 63 of file px_systmr.h.

Enumeration Type Documentation

◆ px_systmr_state_t

Timer state.

Definition at line 66 of file px_systmr.h.

Function Documentation

◆ px_systmr_start()

void px_systmr_start ( px_systmr_t systmr,
const px_systmr_ticks_t  delay_in_ticks 
)

Start a timer.

Parameters
[in,out]systmrPointer to a timer object
[in]delay_in_ticksDelay in timer ticks

Definition at line 35 of file px_systmr.c.

◆ px_systmr_has_started()

bool px_systmr_has_started ( const px_systmr_t systmr)

See if a timer has been started.

Parameters
[in]systmrPointer to a timer object
Return values
truetimer started
falsetimer stopped or expired

Definition at line 45 of file px_systmr.c.

◆ px_systmr_has_expired()

bool px_systmr_has_expired ( px_systmr_t systmr)

See if a timer has expired.

Parameters
[in,out]systmrPointer to a timer object
Return values
truetimer expired
falsetimer not expired or timer stopped

Definition at line 57 of file px_systmr.c.

◆ px_systmr_stop()

void px_systmr_stop ( px_systmr_t systmr)

Stop a running timer.

Parameters
[in,out]systmrPointer to a timer object

Definition at line 103 of file px_systmr.c.

◆ px_systmr_restart()

void px_systmr_restart ( px_systmr_t systmr)

Restart a timer with the delay set with px_systmr_start().

The timer will expire from the current timer tick + systmr->delay_in_ticks.

Parameters
[in,out]systmrPointer to a timer object

Definition at line 109 of file px_systmr.c.

◆ px_systmr_reset()

void px_systmr_reset ( px_systmr_t systmr)

Reset a timer with the delay set with px_systmr_start().

The timer will expire on systmr->start_tick + systmr->delay_in_ticks.

Use this function instead of px_systmr_restart() for periodic timers, because the frequency will not drift over time. An error may accumulate if there is a delay between px_systmr_has_expired() and px_systmr_restart(). Thus the prefered usage for a periodic timer is:

while(true)
{
// Wait until timer has expired
while(px_systmr_has_expired(&systmr) == false)
{
;
}
// Reset periodic timer
px_systmr_reset(&systmr);
// Do something...
}
void px_systmr_reset(px_systmr_t *systmr)
Reset a timer with the delay set with px_systmr_start().
Definition: px_systmr.c:117
Parameters
[in,out]systmrPointer to a timer object

Definition at line 117 of file px_systmr.c.

◆ px_systmr_wait()

void px_systmr_wait ( const px_systmr_ticks_t  delay_in_ticks)

Blocking wait for specified number of ticks.

Parameters
[in]delay_in_ticksDelay in timer ticks

Definition at line 125 of file px_systmr.c.

◆ px_systmr_ticks_elapsed()

px_systmr_ticks_t px_systmr_ticks_elapsed ( px_systmr_t systmr)

Return the number of ticks that have elapsed sinced the timer has been started.

Parameters
[in,out]systmrPointer to a timer object
Returns
px_systmr_ticks_t Number of ticks elapsed

Definition at line 135 of file px_systmr.c.