nrf51822 code reading notes two

Scheduler

For the scheduling of 51822, first create a directory, and then supplement the detailed source code analysis, mainly referring to the official library function description, which can be said to be a rough translation. The link address of the original text is as follows:
https://infocenter.nordicsemi.com/ index.jsp?topic=%2Fcom.nordic.infocenter.sdk5.v13.0.0%2Fgroup__app__scheduler.html

Purpose of the scheduler

According to the official documentation provided by nrf, the scheduler is to allow the program to switch to the officially running program when it is interrupted. It is mainly used for interrupts implemented by soft timers. For hard interrupts, including IO port interrupts, you can choose whether to enable the scheduler during initialization.

Scheduled initialization

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

The official library gives the function of scheduling initialization. The initialization function must be called before entering the main function to start the scheduler.

max_event_size

max_event_size specifies the maximum event size.

queue_size

queue_size specifies the maximum number of dispatch queues.

p_evt_buffer

p_evt_buffer Pointer to the memory buffer used to hold scheduler queues. It must be annotated with the APP_SCHED_BUFFER_SIZE() macro. Buffers must be aligned to 4-byte boundaries.

APP_SCHED_INIT

It is recommended to use the macro APP_SCHED_INIT for initialization, which can be guaranteed to be allocated to the scheduled buffer and can be 4-byte aligned.
Here is the implementation of APP_SCHED_INIT:

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)

The previous article parsed the CEIL_DIV function, which guarantees 4-byte alignment. There is a static area in the buf space APP_SCHED_BUF of the fetcher it applies for, and it can be aligned by 4 bytes and released at the end of the program.

Pause and resume of schedules

Nodic officially provides a scheduler without pause and resume functions.

void app_sched_pause    (   void        )
void app_sched_resume   (   void        )   

app_sched_pause and app_sched_resume can schedule and resume from pause, respectively. Two functions can be called repeatedly, but must be paired. The use of the potential macro APP_SCHEDULER_WITH_PAUSE is required to enable the scheduled pause function.

Get the free space of the dispatch queue

uint16_t app_sched_queue_space_get

If scheduling is added from the interrupt, you can first check the remaining space of the queue at this time.

observed queue utilization

uint16_t app_sched_queue_utilization_get    (   void        )   

Get the utilization of the queue. Note that when calling, the potential energy macro APP_SCHEDULER_WITH_PROFILER. Enabling scheduler profiling is required to enable scheduling analysis.

Put an event into the dispatch queue

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

[in] p_event_data Pointer to event data to be scheduled.
The pointer points to the address where the data needs to be scheduled.
[in] event_size Size of event data to be scheduled. Size of data to be scheduled
.
[in] handler Event handler to receive the event.
The dispatched handler function.

start event scheduling

void  app_sched_execute (void) 

Scheduled event flow schematic

Receiving an event from the ble stack causing a service event
Receiving an event from the ble stack causing a service event

The battery timer expires
The battery timer expires

Schematic of event flow without scheduling

Receiving an event from the ble stack causing a service event
Receiving an event from the ble stack causing a service event

The battery timer expires]
The battery timer expires

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325598365&siteId=291194637