nRF51 SDK
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages

The scheduler is used for transferring execution from the interrupt context to the main context. More...

Macros

#define APP_SCHED_EVENT_HEADER_SIZE   8
 
#define APP_SCHED_BUF_SIZE(EVENT_SIZE, QUEUE_SIZE)   (((EVENT_SIZE) + APP_SCHED_EVENT_HEADER_SIZE) * ((QUEUE_SIZE) + 1))
 Compute number of bytes required to hold the scheduler buffer. More...
 
#define APP_SCHED_INIT(EVENT_SIZE, QUEUE_SIZE)
 Macro for initializing the event scheduler. More...
 

Typedefs

typedef void(* app_sched_event_handler_t )(void *p_event_data, uint16_t event_size)
 Scheduler event handler type.
 

Functions

uint32_t app_sched_init (uint16_t max_event_size, uint16_t queue_size, void *p_evt_buffer)
 Function for initializing the Scheduler. More...
 
void app_sched_execute (void)
 Function for executing all scheduled events. More...
 
uint32_t app_sched_event_put (void *p_event_data, uint16_t event_size, app_sched_event_handler_t handler)
 Function for scheduling an event. More...
 

Detailed Description

See 'Sequence Diagrams' for sequence diagrams illustrating the flow of events when using the Scheduler.

Requirements:

Logic in main context:

Logic in interrupt context:

For an example usage of the scheduler, please see the implementations of HID Mouse Application and HID Keyboard Application.

scheduler_working.jpg
The high level design of the scheduler

Macro Definition Documentation

#define APP_SCHED_EVENT_HEADER_SIZE   8

Size of app_scheduler.event_header_t (only for use inside APP_SCHED_BUF_SIZE()).

#define APP_SCHED_BUF_SIZE (   EVENT_SIZE,
  QUEUE_SIZE 
)    (((EVENT_SIZE) + APP_SCHED_EVENT_HEADER_SIZE) * ((QUEUE_SIZE) + 1))
Parameters
[in]EVENT_SIZEMaximum size of events to be passed through the scheduler.
[in]QUEUE_SIZENumber of entries in scheduler queue (i.e. the maximum number of events that can be scheduled for execution).
Returns
Required scheduler buffer size (in bytes).
#define APP_SCHED_INIT (   EVENT_SIZE,
  QUEUE_SIZE 
)
Value:
do \
{ \
static uint32_t APP_SCHED_BUF[CEIL_DIV(APP_SCHED_BUF_SIZE((EVENT_SIZE), (QUEUE_SIZE)), \
sizeof(uint32_t))]; \
uint32_t ERR_CODE = app_sched_init((EVENT_SIZE), (QUEUE_SIZE), APP_SCHED_BUF); \
APP_ERROR_CHECK(ERR_CODE); \
} while (0)

It will also handle dimensioning and allocation of the memory buffer required by the scheduler, making sure the buffer is correctly aligned.

Parameters
[in]EVENT_SIZEMaximum size of events to be passed through the scheduler.
[in]QUEUE_SIZENumber of entries in scheduler queue (i.e. the maximum number of events that can be scheduled for execution).
Note
Since this macro allocates a buffer, it must only be called once (it is OK to call it several times as long as it is from the same location, e.g. to do a reinitialization).

Function Documentation

uint32_t app_sched_init ( uint16_t  max_event_size,
uint16_t  queue_size,
void *  p_evt_buffer 
)

It must be called before entering the main loop.

Parameters
[in]max_event_sizeMaximum size of events to be passed through the scheduler.
[in]queue_sizeNumber of entries in scheduler queue (i.e. the maximum number of events that can be scheduled for execution).
[in]p_event_bufferPointer to memory buffer for holding the scheduler queue. It must be dimensioned using the APP_SCHED_BUFFER_SIZE() macro. The buffer must be aligned to a 4 byte boundary.
Note
Normally initialization should be done using the APP_SCHED_INIT() macro, as that will both allocate the scheduler buffer, and also align the buffer correctly.
Return values
NRF_SUCCESSSuccessful initialization.
NRF_ERROR_INVALID_PARAMInvalid parameter (buffer not aligned to a 4 byte boundary).
void app_sched_execute ( void  )

This function must be called from within the main loop. It will execute all events scheduled since the last time it was called.

uint32_t app_sched_event_put ( void *  p_event_data,
uint16_t  event_size,
app_sched_event_handler_t  handler 
)

Puts an event into the event queue.

Parameters
[in]p_event_dataPointer to event data to be scheduled.
[in]p_event_sizeSize of event data to be scheduled.
[in]handlerEvent handler to receive the event.
Returns
NRF_SUCCESS on success, otherwise an error code.