Detailed explanation of small rtos based on 51 microcontroller (1)

Publisher:cwk2003Latest update time:2015-07-23 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
      Chen Mingji, this person needs to be known, because he wrote the small rtos, which was written based on the principle of ucos. This small rtos can run on the 51 microcontroller, but it is limited by the ARM memory. As a microcontroller developer, you have to think about the memory problem at all times, because the 51 chip has limited resources. Okay, let's start the learning journey without further ado.
 
        First, you can download the small rtos source code from 51hei  http://www.51hei.com/f/small_rtos1.12.1.zip    or follow me step by step. After the keil51 tool compiles the code, it will generate a .m51 file. You need to learn how to read this file because it will list the addresses of some of your memory allocations and function addresses.  
      The tasks of the operating system are actually an infinite loop. The operating system we wrote actually points the pointer of the $P microcontroller to the first address of the task. So we must save the first address so that it can be used when switching tasks. When it is saved in the OSTsakStackBotton[] array in the small rtos. So how do we get the first address of the task? Here it is statically stored in the array. That is, void (* code TaskFuction[OS_MAX_TASKS])(void)={TaskA,TaskB,TaskC}; This is an array of function pointers. If you don't understand, you can check the information. Here it saves the addresses of 3 functions. 
 You can also see relevant information by viewing the .m51 file after compilation. (This is the default path of the file I compiled, which is E:emp. Of course, you can change the path.)
             CODE 03A4H 000AH UNIT ?PR?TASKA?EXT1
            CODE 03AEH 000AH UNIT ?PR?TASKB?EXT1
            CODE 03B8H 000AH UNIT ?PR?TASKC?EXT1
            C:03B7H PUBLIC TaskFuction
    From the above, we can see that the first address of TaskFuction is C:03CBH. After the program is running, we can directly look at the address C:03B7H to see if the next address is stored.       It should be consistent with .m51. Now that we have the first address of the task, let's look at its code. There is not much code in mian(), just the initialization of timer 0, and the function OSStart(). This function actually initializes the stack and switches the system to task A. As for how to handle the critical stack, let's take a closer look. I run to the top pointer of the mian() function.   If you want to know why it is 0x15, look at .m51 

 


This actually tells you. The 8+8 DATA data of group 0 working registers are defined. Then main() calls OSStart()


Jump to OSStart() It can be seen here that 2 bytes (51 is a byte) are pushed in.   From the memory, it can be seen that the address pushed in is the address of the next code. The smart ones may have figured it out. Replace the address of task A with 86 03 here, then execute RET to jump to task A to execute the code. Is this exciting for you? Then let's continue.   What is this STACK? Haha, the code in the assembly part is as follows:  


Here the stack is relocated and a memory unit is defined. As for its location,    it depends on .m51. It is just at the RAM address where the main() function pointer is stored. But if you think about it carefully, this is natural. But you can also find a way to avoid coincidences. However, this is the best way to make full use of the RAM space to overwrite the main stack pointer of task A. Then the following code should be easy to understand. The first address of the task is stored in the randomly changing OSTsakStackBotton. A function pointer for the idle task is also added here. Then the stack space for each task begins to be deployed. He pushes the first address of task A and then moves the top of the stack pointer to the high address of task A. Then, starting from the tail, it stores the bottom 0, priority task, 0, task C, 0, and task B. Then the empty code part in the middle can be used as a memory block that can be used by task A. Then jump to task A to execute the code

     So far I have switched the first task and parsed it out. 

Reference address:Detailed explanation of small rtos based on 51 microcontroller (1)

Previous article:AT89S52 programming method under Linux
Next article:The electronic clock I wrote using a single chip microcomputer

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号