Design of simple operating system based on 51 single chip microcomputer

Source: Internet
Author: User

Preface

See a long time operating system principle, Ucos source also see half, but feel always mengmengdongdong, with sentence popular network language is always on the car, later on the Internet was recommended an article "to build a own operating system", this article is really good, also attached to the source code, But do not know whether I find the article is wrong or what, I based on the article provides the source code, can not compile, and then began to read the code to modify the code finally successfully compiled but not on the hardware platform to run at all. Later and intermittent to see Ucos source code, anyway what data structure AH of communication what makes people headache, later the University of the single-chip computer principle, school arrangements, I chose the clock timer (a bit like the alarm clock), this open-loop bare metal development is no difficulty, Idle is also free to pick up a few months ago did not complete the OS, this reopening the pit, the code completely self-knocking, starting from the basic functions of the implementation of the OS, like the "build a own operating system" introduced by the same. Worked for two days and finally succeeded, and successfully transplanted the clock timer to their OS ran, to tell the truth in the OS run than bare metal front and back of the effect is much better (OS running press button and the real feeling is synchronized, the effect of the front and back of the button when the digital display will be black screen), but 51 of the hardware resources too little , only 128 bytes of RAM, so this design does not have a unified task communication interface, can only achieve the basic priority, delay service or polling service. In the successful establishment of their own OS in the Ucos to see the source more smoothly, the task has not been able to understand the mission of communication can be understood one or two (but also thanks to Ren Zhe wrote the "Embedded Operating system Foundation Ucos-ⅱ and Linux (2nd edition)"), nonsense said, the text is as follows:

PS: The first time to write this kind of blog, write bad hope Understanding, because "to build a own operating system" itself is written in detail, so I only write the core part of the OS establishment.

Body

1, Task population address: In the OS, the task is invoked in such a way that the program name (parameter) is not directly used. How about that? This part of the "set up an operating system of their own" is very detailed, we search by themselves.

2, Task scheduling: Learn the principle of single-chip microcomputer know that the CPU has SP and PC two special registers, SP is a stack pointer, in 51 it can point to any unit of the data area, the PC is a program counter, it always saves the next program instruction address. 51C language can be directly controlled SP, but the PC does not, so to find ways to indirectly control the PC, the right, is through the compression stack and the implementation of the stack, in the Program execution Breakpoint (transfer subroutine or interrupt), the CPU will automatically put the value of the PC stack, the return breakpoint will automatically bounce the value of the stack back to the PC, This is the key, if we change the SP before bouncing back, we can control the PC indirectly! This allows the CPU to perform other tasks;

3, artificial stacks: one of the most important things in operating system theory is context switching, so each task must have its own stack, called the artificial stack. The establishment of artificial stacks is very fastidious, can not be short or too long, short will be overflow may modify other tasks of the artificial stack, resulting in scheduling disorder. Too long will waste space, especially like 51 of this kind of hardware resources this is a small single-chip microcomputer. The reservation for the space of the stack is divided by an array. When creating a task, to initialize the stack (which is also critical), the task entry address to the bottom (the different microcontroller case, here 51 for example, the following is also), and then the SP points to the correct stack position (different microcontroller case, to save the number of registers different), the individual in the design found that, In order to keep the SP out of bounds, it is best to reserve the bottom of the stack, avoiding waste that can be used to save task information, such as stack usage.

void Task_creak (void (*pfun) (void), int8u *pstack,int8u task_id) {        *pSt;     Os_enter_critical ();     PSt=pstack;      * (++PST) =(int16u) pfun;     * (++PST) = (int16u) pfun>>8;    OS_TCB[TASK_ID]. ostcbsp= (int8u) pst+;    Os_task_list|=osmaptbl[task_id];     OS_TCB[TASK_ID]. ostcbdly=0;     Os_exit_critical ();}

4, Task control block: As with the manual stack each task also has its own task control block, depending on the system requirements member definition, for the free-delay service of the OS, only one save the task SP's member variable and save the delay time of the member variable.

struct {      int8u ostcbsp;      int8u  ostcbdly;    } OS_TCB;

5, System time: Also known as the Clock beats, is the heart of the system, there is hardware production, 51 can be used to produce a millisecond interrupt timer.

void Startticker (void) {tmod=0x01; TH0=0x0d8; TL0=0x0f0; ET0=1; TR0=1;}

6, the System Delay function interface: For the task delay, in the delay time to let the CPU to perform other tasks, improve the efficiency of the CPU (through practice, I personally think this is also the reason for soft real-time implementation), in this function to complete the SP save, the task kicked out of the ready table, and then dispatched.

void ostimedly (int8u ticks)    {        os_enter_critical ();        Os_tcb[curid]. ostcbdly=ticks;        Os_task_list&=~Osmaptbl[curid];        Os_exit_critical ();        OS_TASK_SW (); }

7, the scheduling function: There are two, one is the normal scheduling, for delay scheduling, so to insert assembly language before and after the field protection and site recovery, there is to complete the highest task and access to the SP. One is the scheduling of interrupt levels,

Used to interrupt the service program, because the C language compiled into the assembly when the compiler will automatically field protection, so long as in the dispatch function as long as the field recovery, there is the completion of the highest task and the SP acquisition, to complete the SP save, the task kicked out of the ready table.

voidOS_TASK_SW (void) {int8u i; EA=0; #pragmaAsmpush ACC push B push DPH push DPL push PSW MOV PSW, #00H push AR0 P USH AR1 push AR2 push AR3 push AR4 push AR5 push AR6 push AR7#pragmaEndasmOs_tcb[curid]. OSTCBSP=SP;  for(i=0; i<max_task;i++)  {  if(os_task_list&Osmaptbl[i]) {         Break; }} Curid=i; SP=Os_tcb[curid].       ostcbsp; #pragmaAsmpop AR7 pop AR6 pop AR5 pop AR4 pop AR3 pop AR2 pop AR1 PO P AR0 pop PSW pop DPL pop DPH pop B pop ACC#pragmaEndasmEA=1; #pragmaAsmRETI; #pragmaEndasm}

voidTickinterrupt (void) {int8u i;//sp-=2;         for(i=0; i<max_task;i++)    {             if(Os_tcb[i]. Ostcbdly>0) {Os_tcb[i]. ostcbdly--; if(Os_tcb[i]. ostcbdly==0) Os_task_list|=osmaptbl[i];}} SP-=2; Os_tcb[curid]. OSTCBSP=SP; //os_task_list&=~osmap[curid];    for(i=0; i<max_task;i++)  {  if(os_task_list&Osmaptbl[i]) {         Break; }} Curid=i; SP=Os_tcb[curid].                                             ostcbsp; #pragmaAsmpop AR7 pop AR6 pop AR5 pop AR4 pop AR3 pop AR2 pop AR1 PO P AR0 pop PSW pop DPL pop DPH pop B pop ACC#pragmaEndasmEA=1; #pragmaAsmRETI#pragmaEndasm }

8,SP control: In the process of scheduling, it is necessary to ensure that the entry address of the task breakpoint is saved at the bottom of the stack (the reservation unit above), again in the task scheduling process will inevitably call other functions to press the stack again, and may not be this breakpoint,

So the SP is lowered by two bits in the program being called. Therefore, in the OS design, the SP control must be very careful, can not task scheduling is bound to be chaotic.

9. Task function: The OS must have a task that cannot be actively requested to dispatch, called the System task, in order to the CPU in the absence of any task can be done, the individual in practice found no such task OS error.

There is also the need to grasp the scheduling of the task set (which is affected by the task priority design).

10: Ready table: The design of the ready table if it is important, "operating system principle" from reading.

Finally: I rookie a piece, The fault forgive me, thank you view!

PS: There will be a chance to post the project code and.

Design of simple operating system based on 51 single chip microcomputer

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.