Appiko
Macros | Enumerations | Functions
Millisecond timer

Driver to use milli-second timers using the RTC peripheral. More...

Macros

#define MS_TIMER_RTC_USED   RTC_USED_MS_TIMER
 
#define MS_TIMER_CC_COUNT   CONCAT_3(RTC, MS_TIMER_RTC_USED, _CC_NUM)
 The number of CC registers in the RTC peripheral used for MS timer.
 
#define MS_TIMER_FREQ   32768
 
#define MS_TIMER_TICKS_MS(ms)   ((uint32_t) ROUNDED_DIV( (MS_TIMER_FREQ*(uint64_t)(ms)) , 1000) )
 

Enumerations

enum  ms_timer_num { MS_TIMER0, MS_TIMER1, MS_TIMER2, MS_TIMER_MAX }
 Enumeration used for specifying the timers that can be used with a RTC peripheral. More...
 
enum  ms_timer_mode { MS_SINGLE_CALL, MS_REPEATED_CALL }
 Enumeration to specify the mode of operation of the timer. More...
 

Functions

void ms_timer_init (uint32_t irq_priority)
 
void ms_timer_start (ms_timer_num id, ms_timer_mode mode, uint64_t ticks, void(*handler)(void))
 
void ms_timer_stop (ms_timer_num id)
 
bool ms_timer_get_on_status (ms_timer_num id)
 
uint32_t ms_timer_get_current_count (void)
 Return the current count (24 bit) of the RTC timer used. More...
 

Detailed Description

Warning
This module needs the LFCLK to be on and running to be able to work

Macro Definition Documentation

◆ MS_TIMER_FREQ

#define MS_TIMER_FREQ   32768

The frequency used by the RTC running the ms timer. Must be a power of 2 and at max 32768 Hz

Definition at line 55 of file ms_timer.h.

◆ MS_TIMER_RTC_USED

#define MS_TIMER_RTC_USED   RTC_USED_MS_TIMER

Specify which RTC peripheral would be used for the ms timer module

Definition at line 48 of file ms_timer.h.

◆ MS_TIMER_TICKS_MS

#define MS_TIMER_TICKS_MS (   ms)    ((uint32_t) ROUNDED_DIV( (MS_TIMER_FREQ*(uint64_t)(ms)) , 1000) )

Macro to find out the rounded number of MS_TIMER ticks for the passed time in milli-seconds

Definition at line 64 of file ms_timer.h.

Enumeration Type Documentation

◆ ms_timer_mode

Enumerator
MS_SINGLE_CALL 

One shot call of the timer.

MS_REPEATED_CALL 

Repeated call of the timer.

Definition at line 81 of file ms_timer.h.

◆ ms_timer_num

Enumerator
MS_TIMER0 

Millisecond Timer 0.

MS_TIMER1 

Millisecond Timer 1.

MS_TIMER2 

Millisecond Timer 2.

MS_TIMER_MAX 

Not a timer, just used to find the number of timers.

Definition at line 68 of file ms_timer.h.

Function Documentation

◆ ms_timer_get_current_count()

uint32_t ms_timer_get_current_count ( void  )
inline
Returns
The 24 bit count value. The most significant byte is 0.
Note
This operation takes 5 CPU cycles (ref: nRF5x reference manual)

Definition at line 127 of file ms_timer.h.

References CONCAT_2, and MS_TIMER_RTC_USED.

Referenced by device_tick_process(), and out_gen_get_ticks().

◆ ms_timer_get_on_status()

bool ms_timer_get_on_status ( ms_timer_num  id)

Returns if a timer is on

Parameters
idID of timer being enquired
Returns
Boolean value indicating if a timer is ON

Definition at line 186 of file ms_timer.c.

◆ ms_timer_init()

void ms_timer_init ( uint32_t  irq_priority)

Initialize the RTC peripheral of ID MS_TIMER_RTC_USED to use as a milli-second timer.

Parameters
irq_priorityThe priority of the interrupt level at which the callback function is called
Warning
LFCLK should be initialized before ms timer module is initialized. Use lfclk_init for this.

Definition at line 111 of file ms_timer.c.

References LFCLK_FREQ, MS_SINGLE_CALL, MS_TIMER0, MS_TIMER_FREQ, MS_TIMER_MAX, and ROUNDED_DIV.

Referenced by crystal_test(), and main().

◆ ms_timer_start()

void ms_timer_start ( ms_timer_num  id,
ms_timer_mode  mode,
uint64_t  ticks,
void(*)(void)  handler 
)

Start a milli-second timer

Parameters
idID of the timer to be used from ms_timer_num
modeMode of the timer as specified in ms_timer_mode
ticksThe number of ticks at MS_TIMER_FREQ after which the timer expires
handlerPointer to a function which needs to be called when the timer expires
Note
Starting an already started will restart the timer with the current number of ticks passed. ms_timer_get_on_status can be used to check if a timer is already running.
Todo:
Take care of values of ticks passed which are greater than 2^24, now it is suppressed to 2^24 when greater.
Warning
Works only for input less than 512000 milli-seconds or 8.5 min when MS_TIMER_FREQ is 32768. In other cases the max time must be calculated.

Definition at line 134 of file ms_timer.c.

References ASSERT, MS_REPEATED_CALL, MS_SINGLE_CALL, and ms_timer_stop().

Referenced by ble_adv_start(), crystal_test(), device_tick_init(), device_tick_process(), device_tick_switch_mode(), and green_process().

◆ ms_timer_stop()

void ms_timer_stop ( ms_timer_num  id)

Stop a milli-second timer

Parameters
idID of the timer to be stopped
Note
Stopping an already stopped timer will not be an issue

Definition at line 173 of file ms_timer.c.

References MS_SINGLE_CALL.

Referenced by ble_adv_stop(), ms_timer_start(), out_gen_stop(), and sensebe_tx_rx_stop().