R1 code for micro:bit based train controller code, requires second micro:bit running rx code to operate - see https://meanderingpi.wordpress.com/ for more information

Fork of nrf51-sdk by Lancaster University

Embed: (wiki syntax)

« Back to documentation index

Application Timer

Application Timer

Application timer functionality. More...

Typedefs

typedef app_timer_t * app_timer_id_t
 Timer ID type.
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

enum  app_timer_mode_t { APP_TIMER_MODE_SINGLE_SHOT, APP_TIMER_MODE_REPEATED }
 

Timer modes.

More...

Functions

uint32_t app_timer_init (uint32_t prescaler, uint8_t op_queues_size, void *p_buffer, app_timer_evt_schedule_func_t evt_schedule_func)
 Function for initializing the timer module.
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.
uint32_t app_timer_start (app_timer_id_t timer_id, uint32_t timeout_ticks, void *p_context)
 Function for starting a timer.
uint32_t app_timer_stop (app_timer_id_t timer_id)
 Function for stopping the specified timer.
uint32_t app_timer_stop_all (void)
 Function for stopping all running timers.
uint32_t app_timer_cnt_get (uint32_t *p_ticks)
 Function for returning the current value of the RTC1 counter.
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.

Detailed Description

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 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.


Typedef Documentation

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.

Definition at line 136 of file app_timer.h.

typedef 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.

Definition at line 120 of file app_timer.h.

typedef void(* app_timer_timeout_handler_t)(void *p_context)

Application time-out handler type.

Definition at line 133 of file app_timer.h.


Enumeration Type Documentation

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 140 of file app_timer.h.


Function Documentation

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.

Parameters:
[in]ticks_toValue returned by app_timer_cnt_get().
[in]ticks_fromValue returned by app_timer_cnt_get().
[out]p_ticks_diffNumber of ticks from ticks_from to ticks_to.
Return values:
NRF_SUCCESSIf the counter difference was successfully computed.
uint32_t app_timer_cnt_get ( uint32_t *  p_ticks )

Function for returning the current value of the RTC1 counter.

Parameters:
[out]p_ticksCurrent value of the RTC1 counter.
Return values:
NRF_SUCCESSIf the counter was successfully read.
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.

Parameters:
[in]p_timer_idPointer to timer identifier.
[in]modeTimer mode.
[in]timeout_handlerFunction to be executed when the timer expires.
Return values:
NRF_SUCCESSIf the timer was successfully created.
NRF_ERROR_INVALID_PARAMIf a parameter was invalid.
NRF_ERROR_INVALID_STATEIf the application timer module has not been initialized or the timer is running.
Note:
This function does the timer allocation in the caller's context. It is also not protected by a critical region. Therefore care must be taken not to call it from several interrupt levels simultaneously.
The function can be called again on the timer instance and will re-initialize the instance if the timer is not running.
Attention:
The FreeRTOS and RTX app_timer implementation does not allow app_timer_create to be called on the previously initialized instance.
uint32_t app_timer_init ( uint32_t  prescaler,
uint8_t  op_queues_size,
void *  p_buffer,
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).

Parameters:
[in]prescalerValue of the RTC1 PRESCALER register. Set to 0 for no prescaling.
[in]op_queues_sizeSize of queues holding timer operations that are pending execution. Note that due to the queue implementation, this size must be one more than the size that is actually needed.
[in]p_bufferPointer to memory buffer for internal use in the app_timer module. The size of the buffer can be computed using the APP_TIMER_BUF_SIZE() macro. The buffer must be aligned to a 4 byte boundary.
[in]evt_schedule_funcFunction 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.
Return values:
NRF_SUCCESSIf the module was initialized successfully.
NRF_ERROR_INVALID_PARAMIf a parameter was invalid (buffer not aligned to a 4 byte boundary or NULL).
uint32_t app_timer_start ( app_timer_id_t  timer_id,
uint32_t  timeout_ticks,
void *  p_context 
)

Function for starting a timer.

Parameters:
[in]timer_idTimer identifier.
[in]timeout_ticksNumber of ticks (of RTC1, including prescaling) to time-out event (minimum 5 ticks).
[in]p_contextGeneral purpose pointer. Will be passed to the time-out handler when the timer expires.
Return values:
NRF_SUCCESSIf the timer was successfully started.
NRF_ERROR_INVALID_PARAMIf a parameter was invalid.
NRF_ERROR_INVALID_STATEIf the application timer module has not been initialized or the timer has not been created.
NRF_ERROR_NO_MEMIf the timer operations queue was full.
Note:
The minimum timeout_ticks value is 5.
For multiple active timers, time-outs occurring in close proximity to each other (in the range of 1 to 3 ticks) will have a positive jitter of maximum 3 ticks.
When calling this method on a timer that is already running, the second start operation is ignored.
uint32_t app_timer_stop ( app_timer_id_t  timer_id )

Function for stopping the specified timer.

Parameters:
[in]timer_idTimer identifier.
Return values:
NRF_SUCCESSIf the timer was successfully stopped.
NRF_ERROR_INVALID_PARAMIf a parameter was invalid.
NRF_ERROR_INVALID_STATEIf the application timer module has not been initialized or the timer has not been created.
NRF_ERROR_NO_MEMIf the timer operations queue was full.
uint32_t app_timer_stop_all ( void   )

Function for stopping all running timers.

Return values:
NRF_SUCCESSIf all timers were successfully stopped.
NRF_ERROR_INVALID_STATEIf the application timer module has not been initialized.
NRF_ERROR_NO_MEMIf the timer operations queue was full.