--- Analysis of osal based on ti cc254x
when the tool chain configuration is complete, source Insight to show you a source project, without the help of Baidu and development documents, in one or two hours to understand the source of the composition of the framework and interface, for rapid development?
In the author's previous blog posts, has been advocating two embedded learning and development concepts: to improve the embedded system architecture and software level to form a bigger picture; Master the methodology of understanding new systems and technologies from a demand perspective. Based on the background knowledge of the new learning system, software bigger picture can quickly understand and master a new system from the point of view of software requirement. In this paper, ti bluetooth ble cc254x Source Library and Engineering as an example for analysis and research.
First, operating system-related concepts
1. Operating System
the most common common operating systems are Windows and Linux. The operating system provides the application layer with process scheduling, interprocess communication, memory management, drive management, file management, interrupt management, time management and other related interfaces. In the embedded domain, in addition to the common embedded Linux operating system, more is a custom-built operating system. Custom operating systems are generally closed-source and highly-cropped systems that provide the necessary functions and management modules according to the needs of the application. The application layer and kernel layer of the resource-rich system use different CPU operating modes, only the kernel layer can access the hardware resources, and the application layer and kernel of the resource-scarce system are running the same mode, and can access the hardware resources.
Embedded system has a single-task operating system and multi-tasking operating system. Single-tasking operating systems are typically used for simple electronic control and processing products such as toys, industrial controls, home appliances, and so on. Multitasking operating systems are used in more complex and feature-rich electronic products, such as mobile phones, video surveillance, and so on.
in general simple electronic products, the operating system is only a hierarchical concept, its management function is weaker, and sometimes even for the effect and memory to further reduce its existence, but the software level can make software engineering appear more orderly and easy to maintain and management. As long as interrupt management, GPIO and driver, and time management are separate modules, we can assume that it forms an operating system.
2. kernel
kernel is the core of multi-task operating system, the most basic function is task scheduling and communication between tasks. Since it is multi-tasking and only one CPU (assuming a single-core CPU), it is necessary to do the task scheduling and synchronization and communication between tasks. Linux is an operating system (OS), its kernel (kernel) In addition to task scheduling, communication between tasks, memory management, network interface, etc. ucos is a common multi-tasking kernel, which is used for resource-limited embedded systems, which provides priority preemption task scheduling and semaphore, mailbox, Communication between tasks such as Message Queuing, although the Ucos also provides memory management, it can be cut off completely.
3. concurrency
A system may have multiple independent tasks, but whether it is a multitasking operating system, is the standard for whether these tasks are concurrent. The so-called concurrency refers to whether each independent task can get a fair chance of running. If a task must wait for another task to complete before it can be executed, both are serial, and if one task can preempt the CPU during another task execution, it isconsidered concurrent execution. Concurrent execution is closely related to the task context, where a single-tasking operating system has only one user context, and each task of a multitasking operating system has a context, and task switching is the switch to the context.
4. TI cc254x Demand Analysis
TI cc254x is the integration of Bluetooth BLE4.0 low power single chip on the 8051 core, it can be expected to be a resource-constrained embedded system, and cc254x is to complete the Bluetooth connection and simple control functions (i.e. Gap Profile and GATT profile), So TI provides the library also focus on Bluetooth connection and control transmission design, but not the general operating system supported by the file, drive management and other functions, and the Bluetooth protocol is a layered protocol stack, each layer has a separate business and processing process, it will be multi-tasking application?
TI to provide users with a programming framework called Osal(operating system abstraction layer), in addition to Bluetooth-related underlying protocol opaque, OS-related task scheduling and communication, Bluetooth high-level protocol is user-transparent. As with the above analysis of the single-task operating system, OASL is also a weaker operating system, containing only the kernel of dispatch and communication, and a hardware abstraction layer. But it doesn't prevent us from taking it as an operating system to understand.
5. How to develop quickly
after understanding the project architecture, task scheduling, task communication, user message input and output, you can enter the rapid development phase. Based on the above analysis, in order to develop rapidly on OASL, we need to focus on osal task scheduling, inter-task communication, user message input and processing. For Bluetooth protocol stack related content (GAP, GATT and other application profiles), is not the focus of this article, and then another author elaborated.
Second, the task scheduling of Osal
1. Background knowledge
each task in an embedded system is a large loop of while (1). There may also be different scenarios in a single task, such as each feature menu can be considered a scene, each with its own message cycle, but essentially a while (1) loop. In a multitasking operating system, each task has its own message loop. The co-operation between tasks relies on active release control (active hibernation) and passive suspend (such as the semaphore waiting for another task). Each task must be in a ready state before it is possible to get the chance to run.
2. OAL Application Requirements
osal support Bluetooth protocol stack, including link layer, adaptation layer, Gap connection management, ATT attribute management, Gap Profile application, GATT profiles application, in addition to support the user's key message input processing. Each layer has independent division of labor and responsibility. Is it a multi-tasking to support this model, or a single-tasking multi-scene? See!
3. osal Code Analysis (take Simpleperipheral as an example)
1 Start,main->osal_init_system-> osalinittasks from main , such as:
taskscnt is the number of tasks for the system, such as:
each member of the array is a task-handling process. First give tasksevents application space,tasksevents is what to do, regardless of it first.
osalinittasks Then there is the initialization of each task, and the message loop is not entered. We note that TaskID will add 1 after each task is initialized, and at the beginning of each task initialization, the incoming TaskID is logged and saved as a token of the task.
2 ) main-> Osal_start_system, such as:
We saw the cycle. Keep looking:
Visible, TaskID In addition to the task ID, there is also the meaning of the priority. Each element of the tasksevents array corresponds to the ready state of each task, 0 for no event to handle, and 0 for events to handle.
Tracking a task processing process, we found that there is no while (1) loop, all wait until a task is completed before returning, return to the Osal_start_system large loop. So the system is essentially a single-tasking operation with only one context. But OASL's programming model does look like a multitasking system, or it borrows a lot of multitasking programming ideas, such as seeing a lot of ucos shadows in OASL.
Why set the return value to tasksevents, because it is essentially a single-task loop, and if a subtask takes too long to perform, the response to a higher-priority task slows down and affects overall performance. So if a task executes longer, it is appropriate to split it, set the return value there to continue processing the event, and then return to cycle to see if there are any higher priority tasks that need to be performed.
Iii. Inter-mission communication between OASL
1. Background Knowledge
Linux Inter-task communication includes message queue, shared variable, and semaphore to synchronize; UCOS also has the means of Message Queuing, mailbox and condition variables, semaphores, etc. to communicate and synchronize. Osal is a single-tasking operating system that needs to be synchronized by the user layer's own constraints on each task process.
Messages typically include key messages, time messages, drawing messages, and so on, and semaphores are used for synchronization between tasks, and are not part of message delivery.
2. Code Analysis for osal
tasksevents element In addition to being a ready state, another function is to pass in the application process as a parameter. We track a processing process:
as you can see, the event may be user-defined events, such as periodic events to be done by this task and the event that initiates the connection. In addition, the event may also be a system message events (SYS_EVENT_MSG), what is it? How messages and events are understood in the osal.
In fact, it is easier to understand the event as the type of the message, the event is represented by an integer with a number, and from the code point of view, each bit represents an event that can represent up to 16 events. For a keystroke message, because it may have multiple different key values, it should not be represented by multiple events, but rather by a key event, and the key value of the key is passed through the message queue.
Send Osal_msg_send (uint8 destination_task, uint8 *msg_ptr) and message receive osal_msg_receive via trace messages (uint8 task_id ) interface, you can find that all tasks share a single linked list of message queues, each of which records the event type/value of the message and the taskid of the receiving task.
So what about the sending interface of the event? Such as
That is, the TaskID member of the Tasksevents is written to the corresponding event value. This will give the target task a chance to execute in subsequent cycle. Message sending Osal_msg_send not only inserts the message into the global Message queue list, but also finally calls the Osal_set_event interface to fill in the Sys_event_msg event.
In addition, there is an interface Osal_start_timerex, which sends events on a timed basis, which sends the event only after the specified time has arrived, so that the target TaskID the opportunity to execute the event.
iv. osal Message Processing
osal provides a HAL hardware abstraction Layer, we mainly analyze the key input and serial output is good.
using TI CC2540 for the development of Bluetooth program or a few ago, at that time, one hours to understand the osal, but now the understanding of the idea of the time to restore the finishing out spent almost four hours, Mainly because in the writing process constantly consider how to let readers better understand this article's learning ideas, that is, to summarize the knowledge of software architecture, and how to understand the new system from the perspective of demand. Whining, or leave the osal message processing process to the next article.
The next step will be the introduction of the Internet of Things-Bluetooth and WiFi access related technology sharing. Please pay attention!
More original share please pay attention to the public number: embedded Penguin Circle!
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
How to quickly understand a new embedded operating system