How to compile a simple Embedded Operating System (1) simple task scheduling

Source: Internet
Author: User

Preface: after winter vacation, I finally have time to learn about embedded operating systems. I have always wanted to develop the embedded underlying layer, but I have never touched on this knowledge before. Now I want to learn and write a blog to share my learning history with you. I always think that the ability to write an operating system is the true knowledge of the operating system. So I chose Chen xuwu's "easily compile a small embedded operating system". However, after reading some of them, I felt that the habit of naming variables with pinyin in the book is that the memory usage of the operating system that has been compiled by the author is too high to let people talk about it. Therefore, this book can be turned over at ordinary times and won't be recommended as an entry-level teaching material. The blog content also draws on some of the excellent content in the book. From a modern point of view, the most simple task scheduling, a standard pc OS should provide the following features:
Process management)
Memory management)
File system)
Network Communication (Networking)
Security)
User interface)
Device drivers, but the simplest embedded operating system, can contain much less. The simplest operating system is usually centered on process management. Therefore, now you can try the next simplest "Operating System" and only perform manual task scheduling. For the sake of simplicity, use the simplest AT89S52 program to run the program: a small number of clear bytes in memory, peripherals only a few IO, simple structure, easy to write the operating system. 1. I believe everyone is familiar with the bare run tasks and operating system tasks. The program is generally written as the next big while endless loop:

void main (void) {while (1) /* repeat forever */ {do_something(); } }
Or, for example:
void main (void) {while (1) /* repeat forever */ {do_something1(); do_something2();//Catch data inputdo_something3();...} }
Each function completes an independent operation or task. These functions (or tasks) are executed in a certain order, one after another. Here, the task switching simply completes one execution and then another execution. Continuous Loop.
However, once more tasks are added, the execution sequence becomes a problem. In the above example, once the function do_something1 () runs for too long, it takes a long time for the main loop to be executed to do_something2 (). If do_something2 () is a function that receives input data, the data may be lost. Of course, we can also insert more do_something2 () function calls in a loop, or split do_something1 () into several smaller parts. However, this is a test of the skill of programmers. If there are too many tasks, programming will become quite complicated. At this time, an operating system that helps you allocate the running time of each task is necessary. In the operating system, tasks generally take the form:
void check_serial_io_task (void) _task_ 1 {/* This task checks for serial I/O */ }void process_serial_cmds_task (void) _task_ 2 {/* This task processes serial commands */ }void check_kbd_io_task (void) _task_ 3 {/* This task checks for keyboard I/O */ }
Switching between tasks has been handed over to the operating system, and the familiar main function and while (1) are generally invisible.
2. how to perform task switching or the single-chip microcomputer bare run, when the bare run, the C language file is compiled into a compilation, you can see that the CALL command is used to CALL a task function, after the execution is complete, exit with RET. However, this method is not suitable for frequently switched operating systems, because we cannot predict when to exit, that is, call RET. Task Switching looks very mysterious. To put it bluntly, it is to change the PC value of the program pointer. _ Task _ 1 and _ task _ 2 Written in front are stored in ROM after compilation. Point the PC to this ROM and then execute it. If you want to switch another task, point it to that task. That's simple. In this case, is PC a single address? No, because the vast majority of single-chip computers do not allow direct assignment to PC registers. The compiler reports an error. In general, the PC value is changed using the following methods:
Unsigned char Task_Stack1 [3]; Task_Stack1 [1] = (uint16) Task_1; Task_Stack1 [2] = (uint16) Task_1> 8; SP = Task_Stack1 + 2 ;} // compile to RET
The PC value cannot be changed directly, but it can be changed in other ways. After a function is executed, the PC is always changed. This is how PC is changed? Before the function is executed, the PC is pushed into the stack. When the function ends, you need to call the RET command, that is, the PC outputs the stack. Press the original PC value in the stack. Then the program pops up from the stack and returns to its original position. Here is to simulate this process: simulate a stack structure, load the function entry address to be executed (function name in c) into it, and point SP to the top of the stack created by itself. With a RET command, [SP] and [SP-1] are played into the PC. In this way, the PC changes to the entry address of the function to be executed and starts to execute the target function. (The PC of AT89s52 is 16 bits and is pressed to two bytes in the stack.) 3. The most simple idea of applying a manual scheduling system is to write a simplest 3-task manual scheduling system. The Code is as follows:
Typedef unsigned char uint8; typedef unsigned intuint16; # include <reg52.h> sbit led0 = P0 ^ 0; sbit led1 = P0 ^ 1; sbit led2 = P0 ^ 2; uint8 Cur_TaskID; // The currently running task No. uint8 Task_Stack0 [10]; // The stack uint8 Task_Stack1 [10]; uint8 Task_Stack2 [10]; uint8 Task_StackSP [3]; // top pointer of the three stacks // Task_StackSP [0]-> Task_Stack0 // Task_StackSP [1]-> Task_Stack1 // Task_StackSP [2]-> Task_Stack2void Task_0 (); // task 0 void Task_1 (); // task 1voi D Task_2 (); // Task 2 void Task_Scheduling (uint8 Task_ID); // task scheduling void main (void) {Task_Stack0 [1] = (uint16) Task_0; // load the task stack Task_Stack0 [2] = (uint16) Task_0> 8 according to the task function entry address in the small-end mode; Task_Stack1 [1] = (uint16) Task_1; task_Stack1 [2] = (uint16) Task_1> 8; Task_Stack2 [1] = (uint16) Task_2; Task_Stack2 [2] = (uint16) Task_2> 8; task_StackSP [0] = Task_Stack0; Task_StackSP [0] + = 2; // the first two elements of the stack. Obtain the stack top address, that is, Task_Stack0 [2] Task_StackSP [1] = Task_Stack1; Task_StackSP [1] + = 2; Task_StackSP [2] = Task_Stack2; task_StackSP [2] + = 2; Cur_TaskID = 0; SP = Task_StackSP [0]; // SP gets the stack top address of task 0} // uses the main return command RET, obtain the entry address of task no. 0 in PC // task scheduling function void Task_Scheduling (uint8 Task_ID) {Task_StackSP [Cur_TaskID] = SP; Cur_TaskID = Task_ID; SP = Task_StackSP [Cur_TaskID];} // void Task_0 () {while (1) {led0 = 0; Task_Scheduling (1) ;}// void Task_1 () {while (1) {led1 = 0; Task_Scheduling (2) ;}// void Task_2 () {while (1) {led2 = 0; task_Scheduling (0 );}}
What the code needs to do is to execute three tasks sequentially. The idea of the task scheduling function Task_Scheduling is also described above. Run the code in Keil. You can see that the program is executed sequentially in three tasks.





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.