Coroutine multitasking based on single chip microcomputer

Publisher:fuehrd努力的Latest update time:2015-11-10 Source: eefocusKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
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.
 
Keywords:MCU Reference address:Coroutine multitasking based on single chip microcomputer

Previous article:Microcontroller Experiment Report and Experience
Next article:Microcontroller driver experience

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号