Analysis of Ti ble protocol stack Software Framework

Source: Internet
Author: User

When looking at the source code, it generally starts from the entrance of the entire code, and the source code of the Ti ble protocol stack is no exception. Its main () function is the entry of the entire program, which is automatically called when the system powers on.

 

It mainly does the following:

(1) underlying hardware Initialization Configuration

(2) create a task and initialize the task Configuration

(3) Detect and execute valid task events

 

The source code of the main () function is as follows:


I. underlying hardware initialization settings

Line 75: set the system clock to enable the memory buffer function.

Line 78: Guanzhong disconnected. The system is unstable when it is just started. It is usually disconnected first.

Line 81: hardware-related I/O port configuration.

Line 84 initializes the flash in the MCU.

Line 92: Open interrupt. When the system runs here, the status is very stable and the interrupt can be opened.

95 rows, I/O function configuration, and button callback function pointer.

Line 98: configure the power-saving mode.

 

Ii. Create a task and initialize the task Configuration

Line 89. The main function is to create all tasks.

 

3. Check and execute valid task events

 

Row 3: This function is the core of the entire program. Once entered, all tasks will be cyclically executed and will never end.

 

 

 

The main source code of these three parts is listed below, and analyzed based on the key Configuration:

========================================================== ======================================

I. underlying hardware initialization settings


A. haldriverinit () is mainly used to initialize the hardware abstraction layer and configure the working mode of the pin.

Such as ADC, UART, key, and LCD.

In row 3, halkeyinit () is the key I/O configuration. The following is an example:



Line 211-223, configure the corresponding PIN as gpio, input mode.

Row 3: Initialize the key callback function pointer phalkeyprocessfunction to null.

The purpose of this callback function in the program is to call the callback function pointer when the driver layer detects a key interrupt. The user layer determines the function to be executed, this feature separates the user layer from the driver layer to improve code modularization and operational features.

 

B. initboard (ob_ready) to configure the interrupt function of key gpio


Row 3, halkeyconfig (onboardkeyintenable, onboard_keycallback), which configures the interrupt enable in this function, and sets the key callback function pointer phalkeyprocessfunction to onboard_keycallback.

Onboard_keycallback () will continue to call the onboard_sendkeys () function. Its function is to send a key message to the corresponding task.

 

After the hardware is configured, it can only indicate that the hardware has the conditions to complete the corresponding functions. However, to implement its functions, you need to create corresponding applications for the hardware to work, this program is called a task. How is the task created?

 

Next, we will analyze how to create a task.

 

Ii. Create a task and initialize the task Configuration

 

The main function main () 89 osal_init_system () is used to initialize system settings. One of the most important functions is to create a task. The following is the source code for creating a task.

 

Row 3: Apply for tasksevents memory space

Line 3 clears tasksevents memory space

Line 3, link layer task Initialization

Row 3, Hardware Abstraction Layer task Initialization

Row 3: Initialize tasks at the interface layer of the Host Control.

Line 3, Logical Link Control and adaptive protocol layer task Initialization

144-156 rows, general attributes configuration file layer task Initialization

Row 3: security management task Initialization

Line 3: Task initialization at the customer application layer

 

Each task is assigned a taskid at initialization and increases from 0.

Taking hal_init (taskid ++) as an example, we can see from the code above that the taskid value of the hardware abstraction layer is 1.

 

92 rows. Assign the task_id parameter to hal_taskid. Therefore, after initialization, hal_taskid equals to 1.

Since hal_taskid is defined as a global variable, all things related to hal_taskid in the whole program will be handed over to the Hal-layer task for processing.

 

Similarly, simpleblecentral_init (taskid ++) defines a simplebletaskid. All things related to simplebletaskid will also be handed over to the application-layer task function for processing.

 

 

Where are the task functions defined at each layer?

In the osal_simpleblecentral.c file, a function pointer array is defined as follows:


Row 87: hal_processevent is the task processing function pointer of the Hal layer. It is the first element of the array, tasksarr [1]. That is to say, if the program calls the Hal layer task, directly write the statement "tasksarr [1] ();"

The hal_processevent () function will be executed.

 

Although a task has been created, it is required to execute the functions we have provided for it. Where does it execute?

Next, we will analyze the task execution issues in detail.

 

 

 

 

3. Check and execute valid task events

 

The execution of tasks in the system is driven by events (evnet). The program cyclically detects all tasks. If a new event of a task is not processed, this task will be called.

 

Osal_start_system () is the core of the entire program. It is a for endless loop and the osal_run_system () function is called continuously. Its actual function is to continuously check whether events are generated, execute the corresponding task. See the source code:


Row 3: The timer query function. It checks all the timers. If a timer time arrives, it adds the corresponding event to tasksevents [task_id]. here, the value of task_id is set when the timer is added. For details, see the osal_set_event () function code.

Row 1110-1115: Check all tasks for any event to be executed and record the index idx of this event.

Row 3: taskcnt indicates the number of tasks added by the system, that is, the number of elements in the tasksarr [] array.

Row 3 saves all the events to be executed for the current task.

Row 1124 clears all events of the current task.

Row 3 saves the idx of the current task for the system to use.

Row 3: according to the index idx of the current task, address the function pointer of the current task in the pointer array tasksarr [], call the current task function, and process one of the events. After processing, returns an event that has not been processed.

Row 3 restores the unprocessed event to the event variable of the current task and saves the event variable. Wait for the next reprocessing until all the events are processed.

 

 

 

Summary:

 

This section only describes the main framework of the protocol stack. The protocol stack is only a basic platform and has different application functions in different solutions, you must add different tasks for the application to implement the actual functions.

 

This section describes how to add a task as follows:

1. Configure the I/O attributes required by the task at the Hal layer (skip this step if I/O operations are not involved)

2. Add the task processing function to the array tasksarr [].

3. initialize the task in the osalinittasks () function.

 

Based on the above steps, we can easily create a task. However, if no task event is generated in the program, the task will never run.

 

There is a new problem: Under what circumstances is event generated? How is it produced?

We will break it down in detail in the [event and message working mechanism] section.

 

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.