Abstract: Obtains from the Keil C51 memory space mode of administration, discusses real-time operating system’s in task scheduling time re-entry issue emphatically, analyzes some solution re-entry the basic mode and the method: Analyzes the real-time operating system task scheduling to take the lead the nature, proposed must takes the lead the task scheduling is can suit in the Keil C51 one dispatcher way. Therefore, constructs this real-time operating system, and introduces this system’s pile of management, the duty establishment target-oriented by the mansion duty cut and so on.
Key word: 51 monolithic integrated circuit real-time operating system duty heavy eight dispatches
At present, the majority product developments are in carry on based on some low-power monolithic integrated circuits on. 51 series monolithic integrated circuits, are our country use one at present most monolithic integrated circuit series, has had the very general application environment and the prospect, for many years resources accumulation, causes 51 series monolithic integrated circuits was still many exploiter’s first choices. In view of this kind of situation, emerges in recent years many based on 51 essence expansion chips, the function is getting more and more complete, the speed is getting quicker and quicker, also explained 51 series monolithic integrated circuits from a side in the domestic vitality.
For many years we have wanted to look for an appropriate real-time operating system, takes own development foundation. According to the development demand, conformity some commonly used embedded components, by save the development time, with every effort reduces the development work load; Moreover, requests this real-time operating system to be able to insert very easily to the low-power chip. After all, the large-scale system is minority, but the small application is most and widespread. Obviously, μC/OS-II not too suitably requests in above, but Keil C brings RTX Tiny does not have the source code, does not have the transparency, as for its FULL edition say nothing.
1 KeiI C51 and re-entry question
Speaks of the real-time operating system, has no alternative but to consider the re-entry question. Speaking of the PC machine such big memory processor, this as if is not a very troublesome question, borrows μC/OS-II the RTOS view, namely request in re-entry function, use local variable. But the 5l series monolithic integrated circuit warehouse space is very small, only limits in 256 bytes, is unable to assign a partial pile of space for each function. Was precisely as a result of this reason, Keil C51 uses so-called has been possible to cover the technology:
①The local variable saves in the overall situation RAM space (did not consider that expands the exterior memory’s situation);
②When translation link, namely already completed the local variable the localization;
③If between various functions not direct or indirect transfer relations, then its local variable space then may cover.
Is precisely as a result of the above reason, in Keil under C51 environment, pure function, if does not process (for example increases a simulation stack), is unable the heavy person. Then in Keil under the C5l environment, how to enable its function to have may the heavy human nature? Following analysis under real-time operating system, duty basic structure and pattern:
vold TaskA(void*ptr) {
UINT8 vaL_a;
// other variable definition
do {
// actual user task processing code
} while(1);
}
void TaskB(void*ptr) {
UINT8 vaLb;
// other variable definition
do {
Funcl();
// other actual user task processing code
) while(1);
void Funcl() {
UlNT8 v al_fa;
// other variable definition
// function processing code
}
In the above code, TaskA and TaskB do not have direct or the indirect transfer relations, thus its local variable val_a and val_b are may cover mutually, namely it is possibly located in some same RAM space. Thus, when TaskA moves period of time, after changing val_a, TaskB obtains when the CPU domination and moves, then possibly will change val_b. As a result of its directional same RAM space, causes when TaskA obtains the CPU domination, the val-a value already changed, thus causes the program run not to be correct, in turn also however. On the other hand, Funcl() and TaskB have the direct transfer relations, thus its local variable val_fa and val_b will not be covered mutually, but cannot guarantee that its local variable val_fa not and TaskA or the other duty local variable forms may cover the relations.
Val_a, val_b as well as val_fa and so on local variable defines for the static variable (adds on static instruction symbol) to be possible to solve this problem. But the question is, defines the massive static type variable, will cause the RAM space massive occupancies, has the possibility to cause the RAM space not to be insufficient directly. Especially in some low-power monolithic integrated circuits, has 128 or 256 bytes generally, the massive static variable definition, not was too obviously appropriate under the so small RAM status of resources. From this had another solution, the following code shows:
void TaskC(void) {
UINT8 x, v;
whlk(1) {
OS_ENTER_CRITICAL();
x=GetX(); (1)
y=GetY(); (2)
// duty other codes
OS_EXIT_CRITICAL(); (3)
0SSleep(100); (4)
}
}
In above code TaskC used the critical protection method to protect the code not to interrupt takes the lead, solved the RAM space to be too small truly effectively, not suitable massive definition static variable question. However if each duty uses this kind of structure, the duty from the very beginning, the closure interrupt, will enable timeliness not to be able to obtain the guarantee. The fact proved that this kind of time delay is quite considerable. Explained with an example, if wants to use a dynamic refurbishing in the system the LED monitor, with difficulty guarantees the demonstration stabilizing with continuously, even if is uses an independent timer after the system to do this work (enters critical zone, EA=0). Next, this kind of structure will take the lead in fact the task scheduling transforms as the task scheduling which must takes the lead. In fact if has not occurred by chance between (3) and (4) interrupts and causes a task scheduling, that may understand that for is the duty gives up CPU on own initiative the control. If produced one between (3) and (4) to interrupt and to cause a task scheduling by chance, has only carried out an unnecessary task scheduling, moreover did not hope after (3) had 2 time even multiple task schedulings, believed that the reader also had this desire.
In addition, but may also discover the duty a characteristic: When the duty from (1) makes a fresh start, the local variable x and y is any value does not care about, namely x and y, even if after (3) changed, also already no longer important, will not affect the procedure the accuracy. Actually this characteristic is also the majority of duties, is at least too partial duty majority local variable general character - -, if the duty in the entire implementation, (will not be taken the lead) gives up the CPU domination, then its local variable majority does not need to carry on the special protection, namely its function territory only will be the duty works as a execution, in view of the above code, will be in the critical protectorate code region.
2 real-time operating systems must take the lead
By the above analysis, if must maintain a function may the heavy person, the use static variable, system’s RAM resources will be a stern test; If the use critical zone protects the movement environment, system’s timeliness also cannot obtain the guarantee, will have will take the lead the type task scheduling to transfer must takes the lead the task scheduling risk. Obviously, the use static variable is simple, but has more unworthiness, to the future function’s adjustment also will be a hindrance, generally is not used. Then, only could work hard from the environmental protection, but only could enter the critical zone sacrifice system’s timeliness really to guarantee that the duty does not take the lead? Below has a look at critical protection this method the basic mentality:
①In a duty, if the local variable does not take the lead in its function territory the cut, then these variables after the duty has been eliminated the CPU domination, did not care that its value will not affect the duty the correct execution;
②Above the use critical zone protection, may meet the requirements which mentioned;
③From this causes the real-time performance with takes the lead cuts is weaken may accept. Thus it may be known, does not take the lead is the duty protection local variable key. Such being the case, what does not discard takes the lead-like the task scheduling? This is a good starting point. In view of Keil C51, must takes the lead the type task scheduling, possibly is one better method, can coordinate 51 series monolithic integrated circuits to decide the resources. Following compiles this kind of system:
①The use must takes the lead the type task scheduling;
②May use in the low-power chip, the development goal is, even if is 8051 such small chips, may also use this real-time operating system;
③The support priority dispatch, guarantees its timeliness as far as possible.
3 real-time operating system’s realization
Based on the above analysis and the goal, had recently completed this operating system. Borrows RTx in the storehouse the management, namely the current mission uses the complete pile of space, as shown in Figure 1.

3.1 storehouse initialization and duty foundation
The storehouse initialization is the initialization 0STaskStackBotton array actually, and assigns the current mission for the idle duty, the next movement duty assigns for the highest priority duty, namely the priority is zero duty. When initialization, saves person OSTaslkStackBotton[O], SP 2 the SP value the value to store OSTaskStacKBotton[1], ex analogia. But the duty transfers the 0STa-skCreate function establishment. In fact is only the duty (supposition is n duty) the address fills in the human to correspond the position which OSTaskStackBotton[n] aims, and moves backward SP 2 bytes, as shown in Figure 2.

Why can by this kind of kind of rule, but is not other ways? This is because after task creation, before has not carried on the task scheduling, various duties’ storehouse in fact is they own address, thus its storehouse depth is 2, simple and fills in directly for the procedure.
void main(void) {
OSInit(); /* initialization OSTaskStackBcBotton formation */
TMOD=(TMOD&0XFO)│ 0XOl;
TL0=0xBF;
TH0=0xFC;
TRO=1;
ETO=1;
TFO=O:
OSTaskCreate(TaskA, NULL,0);
OSTaskCreate (TaskB.NULL,1);
OSTaskCreate(TaskC, NULL,2);
OSStart();
In above this section of codes, after all task creation, then transfers OSStart() to start the task scheduling. OSStart() is a great definition, as follows shows:
#deflne OSStart() d0 {\
OSTaskCreate(TaskIdle, NULL, OS_MAX_TASKS); \
EA=l: \
return; \
} while(O)
First, it founded an idle duty and opens the interrupt, then returns. Has returned to where? We knew that the idle duty is the priority lowest duty, when adjusts the OSTaskCreate establishment, will fill in its address the human to the SP position, and will move SP 2 bytes (to see backward Figure 2 and explanation), thus this time will occupy the storehouse peak, certainly will be idle duty Taslddle. This will cause here return certainly to return to the idle duty. Hence, the system enters the normal operation condition.
3.2 duty cuts
The duty cut is divided two kind of situations, when the current mission priority is lower than next obtains the CPU domination the duty, next obtains the CPU domination the duty stack top to between the current mission stack top content to the RAM space high-end removal, vacates the complete RAM space to make the next duty a pile of space, simultaneously renews corresponding OSTaskStackBotton, causes it to aim at the new correct task the storehouse stack bottom. If current mission’s priority is higher than the next duty the priority, then makes the opposite removal, like Figure 3 with shown in Figure 4.

All duties must transfer OSSleep on own initiative, gives up CPU the domination. After task call OSSleep, will choose the priority highest ready task movement.
Conclusion
After the system completes, the essence code quantity about more than 400 bytes, takes 1 timer interrupt and the small amount memory space. The system establishment capacity is 8 duties, the user actual available duty is 7, can meet the general need, also has achieved in the low-power chip the application development request. Because has not used takes the lead-like the task scheduling, except outside entire journey related individual duty’s some local variables, other local variables already did not have the cover relations, because is the duty gives up the CPU domination on own initiative, regarding the variable which individual needs to protect carries on processing also becomes alone easy. In the system, the entire journey does not need repeatedly the switch interrupt, the real-time performance is also very good. (For example DSl8820) is an exception to the individual succession request strict peripheral device.