![]() |
EVE 1.0
|
Application timer functionality. More...
![]() |
Macros | |
#define | APP_TIMER_CLOCK_FREQ CLOCK_SECOND |
#define | APP_TIMER_MIN_TIMEOUT_TICKS 1 |
#define | APP_TIMER_TICKS(MS, PRESCALER) MS_TO_TICKS(MS) |
Convert milliseconds to timer ticks. More... | |
#define | APP_TIMER_DEF(timer_id) |
Create a timer identifier and statically allocate memory for the timer. More... | |
#define | APP_TIMER_INIT(PRESCALER, OP_QUEUES_SIZE, SCHEDULER_FUNC) |
Initialize the application timer module. More... | |
Typedefs | |
typedef struct app_timer_t * | app_timer_id_t |
Timer ID type. Never declare a variable of this type, but use the macro APP_TIMER_DEF instead. | |
typedef void(* | app_timer_timeout_handler_t) (void *p_context) |
Application time-out handler type. | |
typedef uint32_t(* | app_timer_evt_schedule_func_t) (app_timer_timeout_handler_t timeout_handler, void *p_context) |
Type of function for passing events from the timer module to the scheduler. | |
Enumerations |
Functions | |
uint32_t | app_timer_init (app_timer_evt_schedule_func_t evt_schedule_func) |
Function for initializing the timer module. More... | |
uint32_t | app_timer_create (app_timer_id_t const *p_timer_id, app_timer_mode_t mode, app_timer_timeout_handler_t timeout_handler) |
Function for creating a timer instance. More... | |
uint32_t | app_timer_start (app_timer_id_t timer_id, uint32_t timeout_ticks, void *p_context) |
Function for starting a timer. More... | |
uint32_t | app_timer_stop (app_timer_id_t timer_id) |
Function for stopping the specified timer. More... | |
uint32_t | app_timer_stop_all (void) |
Function for stopping all running timers. More... | |
uint32_t | app_timer_cnt_get (uint32_t *p_ticks) |
Function for returning the current value of the RTC1 counter. More... | |
uint32_t | app_timer_cnt_diff_compute (uint32_t ticks_to, uint32_t ticks_from, uint32_t *p_ticks_diff) |
Function for computing the difference between two RTC1 counter values. More... | |
Application timer functionality.
This module enables the application to create multiple timer instances based on the RTC1 peripheral. Checking for time-outs and invokation of user time-out handlers is performed in the RTC1 interrupt handler. List handling is done using a software interrupt (SWI0). Both interrupt handlers are running in APP_LOW priority level.
When calling app_timer_start() or app_timer_stop(), the timer operation is just queued, and the software interrupt is triggered. The actual timer start/stop operation is executed by the SWI0 interrupt handler. Since the SWI0 interrupt is running in APP_LOW, if the application code calling the timer function is running in APP_LOW or APP_HIGH, the timer operation will not be performed until the application handler has returned. This will be the case, for example, when stopping a timer from a time-out handler when not using the scheduler.
Use the USE_SCHEDULER parameter of the APP_TIMER_INIT() macro to select if the app_scheduler should be used or not. Even if the scheduler is not used, app_timer.h will include app_scheduler.h, so when compiling, app_scheduler.h must be available in one of the compiler include paths.
#define APP_TIMER_CLOCK_FREQ CLOCK_SECOND |
Clock frequency of the RTC timer used to implement the app timer module.
Definition at line 52 of file app_timer.h.
#define APP_TIMER_MIN_TIMEOUT_TICKS 1 |
Minimum value of the timeout_ticks parameter of app_timer_start().
Definition at line 53 of file app_timer.h.
#define APP_TIMER_TICKS | ( | MS, | |
PRESCALER | |||
) | MS_TO_TICKS(MS) |
Convert milliseconds to timer ticks.
This macro uses 64-bit integer arithmetic, but as long as the macro parameters are constants (i.e. defines), the computation will be done by the preprocessor.
When using this macro, ensure that the values provided as input result in an output value that is supported by the app_timer_start function. For example, when the ticks for 1 ms is needed, the maximum possible value of PRESCALER must be 6, when APP_TIMER_CLOCK_FREQ is 32768. This will result in a ticks value as 5. Any higher value for PRESCALER will result in a ticks value that is not supported by this module.
[in] | MS | Milliseconds. |
[in] | PRESCALER | Value of the RTC1 PRESCALER register (must be the same value that was passed to APP_TIMER_INIT()). |
Definition at line 73 of file app_timer.h.
#define APP_TIMER_DEF | ( | timer_id | ) |
Create a timer identifier and statically allocate memory for the timer.
timer_id | Name of the timer identifier variable that will be used to control the timer. |
Definition at line 87 of file app_timer.h.
#define APP_TIMER_INIT | ( | PRESCALER, | |
OP_QUEUES_SIZE, | |||
SCHEDULER_FUNC | |||
) |
Initialize the application timer module.
This macro handles dimensioning and allocation of the memory buffer required by the timer, making sure that the buffer is correctly aligned. It will also connect the timer module to the scheduler (if specified).
[in] | PRESCALER | Value of the RTC1 PRESCALER register. This will decide the timer tick rate. Set to 0 for no prescaling. |
[in] | OP_QUEUES_SIZE | Size of queues holding timer operations that are pending execution. |
[in] | SCHEDULER_FUNC | Pointer to scheduler event handler |
Definition at line 141 of file app_timer.h.
enum app_timer_mode_t |
Timer modes.
Enumerator | |
---|---|
APP_TIMER_MODE_SINGLE_SHOT |
The timer will expire only once. |
APP_TIMER_MODE_REPEATED |
The timer will restart each time it expires. |
Definition at line 103 of file app_timer.h.
uint32_t app_timer_init | ( | app_timer_evt_schedule_func_t | evt_schedule_func | ) |
Function for initializing the timer module.
Normally, initialization should be done using the APP_TIMER_INIT() macro, because that macro will both allocate the buffers needed by the timer module (including aligning the buffers correctly) and take care of connecting the timer module to the scheduler (if specified).
[in] | evt_schedule_func | Function for passing time-out events to the scheduler. Point to app_timer_evt_schedule() to connect to the scheduler. Set to NULL to make the timer module call the time-out handler directly from the timer interrupt handler. |
NRF_SUCCESS | If the module was initialized successfully. |
NRF_ERROR_INVALID_PARAM | If a parameter was invalid (buffer not aligned to a 4 byte boundary or NULL). |
Definition at line 24 of file app_timer.c.
uint32_t app_timer_create | ( | app_timer_id_t const * | p_timer_id, |
app_timer_mode_t | mode, | ||
app_timer_timeout_handler_t | timeout_handler | ||
) |
Function for creating a timer instance.
[in] | p_timer_id | Pointer to timer identifier. |
[in] | mode | Timer mode. |
[in] | timeout_handler | Function to be executed when the timer expires. |
NRF_SUCCESS | If the timer was successfully created. |
NRF_ERROR_INVALID_PARAM | If a parameter was invalid. |
NRF_ERROR_INVALID_STATE | If the application timer module has not been initialized or the timer is running. |
Definition at line 30 of file app_timer.c.
uint32_t app_timer_start | ( | app_timer_id_t | timer_id, |
uint32_t | timeout_ticks, | ||
void * | p_context | ||
) |
Function for starting a timer.
[in] | timer_id | Timer identifier. |
[in] | timeout_ticks | Number of ticks (of RTC1, including prescaling) to time-out event (minimum 5 ticks). |
[in] | p_context | General purpose pointer. Will be passed to the time-out handler when the timer expires. |
NRF_SUCCESS | If the timer was successfully started. |
NRF_ERROR_INVALID_PARAM | If a parameter was invalid. |
NRF_ERROR_INVALID_STATE | If the application timer module has not been initialized or the timer has not been created. |
NRF_ERROR_NO_MEM | If the timer operations queue was full. |
Definition at line 43 of file app_timer.c.
References mwork_now(), mwork_schedule(), swint_disable(), and swint_enable().
uint32_t app_timer_stop | ( | app_timer_id_t | timer_id | ) |
Function for stopping the specified timer.
[in] | timer_id | Timer identifier. |
NRF_SUCCESS | If the timer was successfully stopped. |
NRF_ERROR_INVALID_PARAM | If a parameter was invalid. |
NRF_ERROR_INVALID_STATE | If the application timer module has not been initialized or the timer has not been created. |
NRF_ERROR_NO_MEM | If the timer operations queue was full. |
Definition at line 56 of file app_timer.c.
References mwork_cancel().
uint32_t app_timer_stop_all | ( | void | ) |
Function for stopping all running timers.
NRF_SUCCESS | If all timers were successfully stopped. |
NRF_ERROR_INVALID_STATE | If the application timer module has not been initialized. |
NRF_ERROR_NO_MEM | If the timer operations queue was full. |
uint32_t app_timer_cnt_get | ( | uint32_t * | p_ticks | ) |
Function for returning the current value of the RTC1 counter.
[out] | p_ticks | Current value of the RTC1 counter. |
NRF_SUCCESS | If the counter was successfully read. |
uint32_t app_timer_cnt_diff_compute | ( | uint32_t | ticks_to, |
uint32_t | ticks_from, | ||
uint32_t * | p_ticks_diff | ||
) |
Function for computing the difference between two RTC1 counter values.
[in] | ticks_to | Value returned by app_timer_cnt_get(). |
[in] | ticks_from | Value returned by app_timer_cnt_get(). |
[out] | p_ticks_diff | Number of ticks from ticks_from to ticks_to. |
NRF_SUCCESS | If the counter difference was successfully computed. |