In many MCU projects, due to the size of the operating system and the background knowledge used, if it is adopted, it may cause the project to deviate from the main business direction. At this time, it is necessary to use simple coroutine multitasking.
1 Characteristics of coroutine multitasking
Each task has equal priority
Each task actively releases CPU control
2 Characteristics of UCOS and other operating systems
Tasks have different priorities, which makes it easy to allocate CPU resources.
For ucos tasks, each task thinks that it has exclusive CPU and can sleep at will. This way, there are fewer restrictions on the code style and it is easier to modify existing modules.
Operating systems generally provide more services and are better for complex applications.
3 Application scenarios of coroutine multitasking
The coroutine method is suitable for simple multi-tasking. Each task must confirm that it conforms to the coroutine model, cannot block the CPU operation, and actively release the CPU. If you consider multi-person cooperation or the introduction of third-party code, then the coroutine method may be too heavy (model modification check), then it is best to use the operating system.
The establishment of coroutines or operating system platforms all require accumulation, and there is no difference between high-level and low-level. It is just a matter of choosing different scenarios and developers.
4. Implementation of coroutine multitasking
For ease of use and future system upgrades, multitasking is defined and implemented in an interface-based manner.
4.1 Definition of Tasks
Each task will manage its own data and provide an external interface. Each task provides the following structure
Struct _task1
{
//Task external interface, function pointer
Void (*start)(Struct _task1*handle);
Void (*run)(Struct _task1*handle);
Void (*stop)(Struct _task1*handle);
//Other external interfaces
...
//Task private data
} task1;
The reason why the external interface is placed in the task structure is to emphasize that these are the external interfaces of the task, and the client can only call the interfaces inside. In addition, the function pointer also conveniently provides a separation layer between the interface and the implementation.
4.2 Task Implementation
At the task implementation level, users can evolve the system based on their understanding of the system and business without affecting the use of external interfaces.
//Task interface implementation function
Void task1_start(Struct _task1*handle)
{
//Set the task start flag
}
Void task1_run(Struct _task1*handle)
{
If(handle->status1)
{
//deal with
}
Else if(handle->status2)
{
//deal with
}
}
Void task1_stop(Struct _task1*handle)
{
//Set the task end flag
}
//Task initialization function, construct task structure
Void task1_init(Struct _task1*handle)
{
Handle->start = task_start;
Handle->run = task_run;
Handle->stop = task_stop;
}
4.3 Calling form
4.3.1 Define the global task structure
Struct _task1
{
//Task external interface, function pointer
Void (*start)(Struct _task1*handle);
Void (*run)(Struct _task1*handle);
Void (*stop)(Struct _task1*handle);
//Other external interfaces
...
//Task private data
} task1;
4.3.2 Task Initialization
Task1_init(&task1);
4.3.3 Start the task
Task1->start(&task1);
4.3.4 Ending the task
Task1->stop(&task1);
4.3.5 Task running
Generally, after the system is started, there is an infinite loop statement that runs
While(1)
{
Task1->run(&task1);
Task2->run(&task2)
...other tasks run
}
5. Timer and delay implementation method
Coroutine multitasking does not use the sleep() method to block the CPU for timers or hibernation in the code. The timer will have a separate task, which provides interfaces such as adding timers, deleting timers, and querying timer values to provide software timer functions.
6 Inter-task communication
In principle, communication is done through the interface provided by the task. Of course, if the communication workload is too large, we do not object to using third-party tasks to complete the communication.
7 Driver layer module task examples
For MCU coroutine multitasking, the application layer and the driver layer are tasks with the same priority. For many drivers, such as UART drivers, many of their sending and receiving are carried out in a query manner. The following is a simple multitasking structure to illustrate the writing of the driver.
Struct _com{
//Task external interface, function pointer
Void (*start)(Struct _com*handle);
Void (*run)(Struct _com*handle);
Void (*stop)(Struct _com*handle);
//Other external interfaces
Void read(char* ch)
Void write(char* ch)
...
//Task private data
//Drive status,
Char status;
}com1;
// Run the code
Void Com_run(Struct _com * handle)
{
If(read)
{
If(register1)
{
}
Else if(register2)
{
}
}
Else if(write)
{
If(register3)
{
}
Else if(register4)
{
}
}
}
As can be seen from the above code, the driver module is actually no different from ordinary tasks.
Previous article:Microcontroller Experiment Report and Experience
Next article:Microcontroller driver experience
- Popular Resources
- Popular amplifiers
Recommended Content
Latest Microcontroller Articles
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
He Limin Column
Microcontroller and Embedded Systems Bible
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
MoreSelected Circuit Diagrams
MorePopular Articles
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
MoreDaily News
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- Brief Analysis of Automotive Ethernet Test Content and Test Methods
- How haptic technology can enhance driving safety
- Let’s talk about the “Three Musketeers” of radar in autonomous driving
- Why software-defined vehicles transform cars from tools into living spaces
- How Lucid is overtaking Tesla with smaller motors
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
Guess you like
- EEWorld interviews Toshiba PCIM booth engineer: 2-minute short video shows you key products and solutions
- Is it necessary to learn TI's KeystoneII series multi-core heterogeneous processors?
- cc2540 cc2541 low power consumption measurement and summary - and precautions - low power consumption less than 10uA
- Design of Serial Universal Asynchronous Receiver/Transmitter Based on FPGA
- Inductive whistle problem in digital phase-shifted full-bridge circuit, please help
- Tesla Robot Optimus Prime Conference Uncut 4K HD Episode 2
- FPGA error, added clock timing, and how to solve this error
- [Project source code] FPGA-based Gigabit Ethernet module UDP design example program
- Here it comes! Summary of 2021 National College Entrance Examination Essay Questions
- LE Audio audio sharing will stimulate a new wave of audio innovation