Application skills/MCU software design based on RTX51[Copy link]
Abstract : With the increasing application of single-chip microcomputers, the requirements for its software development efficiency are getting higher and higher, from assembly to C language, and then to the operating system. MCS51 is the everlasting main force in the world of single-chip microcomputers, and the RTX51 used on it has been greatly developed. It has low hardware requirements, is easy to use and flexible, and is therefore increasingly widely used in single-chip microcomputer software development.
Keywords : MCU C51 RTX51
1 Overview
Many MCU applications need to execute many tasks at the same time. For such applications, we can use real-time operating systems to flexibly arrange system resources. RTX51 is a small real-time multitasking operating system developed by Keil in the United States for MCS51 series MCUs. It can work on all 8051 MCUs and derived families, simplifying complex software design and shortening project cycles. In practice, we use RTX51 to develop MCU software. We designed the software for the GPS receiver board controlled by the MCU and achieved good results.
2 Introduction to RTX51
RTX51 has 2 modes: RTX51 full mode and minimum mode. RTX51 minimum mode is a subset of RTX51 full version, which can be easily run on 8051 system without external RAM (DXATA). RTX51 full mode has 4 task priorities, which can be processed in parallel with interrupt functions. Tasks use the "mailbox" system to transmit signals and messages, and can apply for and release memory from the memory pool; at the same time, a task can be forced to stop execution and wait for an interrupt, or a semaphore or message from other interrupts. RTX51's requirements for system hardware are listed in Table 1.
2.1 RTX51 Tasks
RTX51 distinguishes 2 types of tasks: fast tasks and standard tasks. Fast tasks have a very fast response speed. Each fast task uses a separate register group of 8051 and has its own stack area. RTX51 supports up to 3 fast tasks at the same time. Standard tasks require a little more time to switch tasks, so the internal RAM used is relatively less than that of fast tasks. All standard tasks share 1 register group and stack. When the task is switched, the register state and stack content of the current task are transferred to the external memory. RTX51 supports up to 16 standard tasks.
RTX51 task status:
① RUNNIGN - The currently running task is in the RUNNING state. Only one task can run at the same time. ② READY - The task waiting to run is in the READY state. After the currently running task exits the running state, the task with the highest priority in the ready queue enters the running state. ③ BLOCKED - The task waiting for an event is in the BLOCKED state. If the event occurs and the priority is higher than the running task, this task enters the running state; if the priority is lower than the running task, this task enters the READY state. ④ DELETED - The task that has not been started is in the deleted state. ⑤ Task switching - RTX51 contains an event-driven task switching mechanism, which can switch according to the priority of the task, that is, a preemptive multitasking system; there is also an optional time slice rotation switching task mode. In the time slice rotation mode, tasks of the same level occupy the CPU according to the time slice. RTX51 tasks have 4 priorities: 0, 1, and 2 can be assigned to standard tasks, and priority 3 is reserved for fast tasks. Each task can wait for an event to occur without increasing the burden on the system; tasks can wait for messages, signals, interrupts, timeout events, or a combination of these. Task switching is performed according to certain rules, including: tasks with higher priority that enter the "ready" state are executed first; if several tasks in the "ready" state have the same priority, the one that enters the "ready" state first is executed first.
The RTX51 task switching diagram is shown in Figure 1.
2.2 RTX51 Events ◇Timeout: Suspends the running task for a specified number of time periods. ◇Interval: Similar to timeout, but the software timer is not reset. The typical application is to generate a clock. ◇Signal: Used for internal synchronization and coordination of tasks. ◇Message: Applicable to RTX51 Full, used for information exchange. We can deliver a message to a specific mailbox. The message consists of 2 bytes, which can be data defined by the user according to his own needs, or a pointer to the data. If the message list of the mailbox is full and the message is sent by an interrupt, the message will be lost; if the task sends the message, the task will enter the waiting state until the mailbox has a position to receive this message again. The mailbox manages messages according to the FIFO principle. If several tasks are waiting to receive messages, the first one to enter the waiting queue will receive the message. A mailbox can store up to 8 messages. When the mailbox is full, there can be up to 16 waiting tasks. ◇Interrupt: Applicable to RTX51 Full, semaphores are used to manage shared system resources. By using "tokens", only one task is allowed to use certain resources at the same time. If several tasks apply for access to the same resource, the first one to apply will be allowed access, and the other tasks will enter the waiting queue until the first task completes the operation before the next task can continue.
The os_wait() function suspends a task to wait for an event to occur. This can synchronize two or more tasks. Its working process is as follows: when the event that the task is waiting for does not occur, the system suspends the task; when the event occurs, the system switches the task according to the task switching rules.
2.3 RTX51 interrupt handling
RTX51 full mode provides two methods to handle interrupts: one is the C51 interrupt function, and the other is the RTX51 interrupt. It can be divided into fast task interrupt and standard task interrupt. For the interrupt function method, it can also be used without using RTX51. When an interrupt occurs, the program jumps to the corresponding interrupt function, which is independent of the running task. The interrupt processing is outside the RTX51 system and has nothing to do with the task switching rules. For the task interrupt method, whether fast or standard tasks are used to handle interrupts, if an interrupt occurs, the task waiting for the interrupt will enter the ready state from the "waiting" state and switch according to the task switching rules. This interrupt processing is fully integrated into the RTX51, and the processing of hardware interrupt events is exactly the same as the processing of signals and information. In the system response interrupt enable register, only in this way can the task switching rules be followed and the interrupt program can be correctly carried out. It must be noted that the interrupt enable register is fully controlled by RTX51 and manual modification by users is prohibited.
Application Examples
The following is the application of RTX51 on the GPS receiver board controlled by a single-chip microcomputer.
(1) System hardware consists of single-chip microcomputer W77E58, fast 8051 core, 32KB ROM, 1KB XDATA RAM, which meets the hardware requirements of using RTX51; keyboard, GPS positioning module, LCD display module. (2) System software consists of software operating environment KEIL uVision2 6.20 integrated development environment plus RTX51 complete version. Task KEY-BOARD monitors the keyboard status. If a key is pressed, the key code is updated to mailbox 1. External interrupt 1 waits for receiving GPS data, stores the data, and sends a signal to the DISPLAY task. Task DISPLAY processes according to the different signals and messages received. Task SEND-OUT processes the received data and sends it out. Task VOICE performs voice output.
The system hardware and software structure is shown in Figure 2.
Here is the abbreviated source program:
#include<RTX51.h> //Include RTX51 header file #define DISPLAY 0 #define SEND_OUT1 #define KEY_BOARD2 #define VOICE3 void main(void) { init system(); //System initialization os start system(DISPLAY); //Start RTX51 } void task0(void)_task_DISPLAY { os_set_slice(1000); //Set time slice size os_enable_isr(0); //Enable external interrupt 0 os_creat_task(SEND_OUT); //Start SEND_OUT task os_creat_task(VOICE); //Start VOICE task for(;;){ switch(os_wait(K_SIG+K_MBX+1,255,&keyboard))//Wait for receiving signals and keyboard messages, and process them by category {display1();break; case EVENT_MBOX;//When receiving data from the mailbox switch(keyboard) { case '1'; … os_send_signal(SEND_OUT); //Send signal to task SEND_OUT … os_send_signal(VOICE);} //Send signal to task VOICE …;} …;} } void task1(void)_task_SEND_OUT //Processing data sending task {while(1) { os_wait(K_SIG,255,0) //Wait for signal operation_send(); } void task3(void)_task_VOICE {while(1){ os_wait_signal(K_SIG,255,0); //Wait for voice processing signal voice();} } void interrupt(void)interrupt 2 using 1 { read_gps_data(p_gps_data); //Receive data isr_send_signal(DISPLAY); //Send signal to DISPLAY task } #pragma REGISTERBANK(2) //Use register bank 2 void task2(void)_task_KEYBOARSD_priority_3//Set as fast task { os_attach_interrupt(0); //Bind task and external interrupt 0 while(1){ os_wait(K_INT,255,0); //Wait for interrupt to occurKEY =iic_read_keyboard(); os_send_message(1,KEY,0);}//Send keyboard code to mailbox 1 }
4 Conclusion
Through practice, we can find that it is more convenient to use RTX51 to develop MCU programs. Especially for larger programs, it avoids the tedious work of writing message loops by yourself, and the efficiency is significantly improved. When the hardware resources are sufficient, the effect is more obvious.