Application of M16C62 in uC/OS-II embedded system 13:41:25:
Preface
With the development of micro-electronics technology and networks, people are increasingly familiar with networks. Network terminal products are getting more and more people's attention. The application of embedded operating systems has also gained unprecedented development, and people have made great progress in Embedded research. The Design and Application of a real-time and multi-task system based on an operating system has become a new development trend of Single-chip Microcomputer Applications.
UC/OS is an open source embedded operating system, uC/OS-II is by uC/OS
V1.1 version upgrade, on the basis of the original version, uC/OS-II has a lot of new features, such as: memory management; in the process of task creation, deletion, task switching, clock cycle switching, allowed
User calls custom functions; supports task control block (TCB) Function Extension; can verify the usage of each stack; and some other new functions [2 ]. M16C62 single-chip microcomputer was pushed by Mitsubishi Corporation at the end of 1990s
The new 16-bit single-chip microcomputer is one of the most widely used 16-bit single-chip microcomputer. The M16C microcontroller integrates the advantages of register-based and memory-based architectures to achieve similar
The high-speed processing performance of server guard. The M16C62 microcontroller features ultra-low power consumption, strong anti-interference capabilities, and high C programming efficiency. It integrates 10-bit A/D converter and DMA control.
A variety of peripheral function circuit modules, such as asynchronous communication channels and timers, are very suitable for small real-time, multi-task, and embedded systems [1 ]. Transplant uC/OS-II to M16C62 Microcontroller
To form a real-time multi-task application system. Next we will talk about how to transplant uC/OS-II to M16C62 microcontroller, and design a real-time multi-task system.
1. Transplantation of uC/OS-II
UC/OS-II uses a fully preemptible Real-Time Kernel that can manage up to 56 tasks, each corresponding to a different priority, therefore, uC/OS-II operating system does not support time slice
Transfer Scheduling Method. But all UC/OS-II function calls and the execution time of the service are known, that is, the execution time of the uC/OS-II system service does not depend on the number of application tasks. Another
In addition, each task in uC/OS-II has its own stack, the size of each stack can be allocated according to the needs of the application, which lowers the system's demand for RAM; in terms of interrupt management, uC
An interruption in the/OS-II can suspend ongoing tasks, and if a higher-priority task is awakened by an interruption, a higher-priority task is executed immediately after all of the interrupt nesting exits, In the uC/OS-II
The broken nesting can reach 255 layers.
Most of the source code for uC/OS-II is ansi c with strong portability
Written. Only a small part of the Code related to the microprocessor is compiled by sink, so porting the uC/OS-II to MC16C62 is much easier, mainly to modify the processor-related
Code, such as: OS-CPU.H, OS-CPU-A.ASM, OS-CPU-C.C.
(1), OS-CUP.H header file
The OS _CUP.H header file mainly defines the processor-related data types. The data types in M16C62 include unsigned integer, signed integer, unsigned complex, and signed complex. Because
Here, the OS _CPU.H header file completely defines these types. In addition to data type definitions related to the processor, an increase in interrupt prohibition, interrupt permit, and stack was made in the OS-CUP.H
Long direction and some other simple macro definitions.
(2), OS-CPU-A.ASM files
Contains four assembler languages in the OS-CPU-A.ASM File
Such as OSStartHighRdy (), OSCtxsw (), OSIntCtxsw (), and OSTickISR (). Ready in uC/OS-II
The stack structure of the task looks the same as the just-interrupted situation. To run the highest-priority task, you need to recover all the registers of the processor from the task stack in order and run
An interrupt return statement is used to switch tasks. Therefore, OSStartHighRdy () refers to the memory unit in which the stack pointer of the task to be restored is directed to the zero-offset memory block of the task control block. That is to say
Data stored in the task stack and CPU registers, such as R0, R1, R2, R3, A0, A1, SB, and FB, are returned to the current system register, and pointing the current stack pointer to the PC pointer
Location.
In uC/OS-II, task switching problems are accomplished by sending software interrupt commands or relying on the processor to execute trap commands. However, service interruption routines, traps, or Exception Handling
The vector address of the routine must point to OSCtxSw () [1 ]. In the M16C62 microcontroller, you can define the software interrupt 0 to complete the task switching. Therefore, the interrupt vector table in M16C62
The 0 software interrupt address points to OSCtxSW (). The corresponding interrupt number is 0.
OSInCtxSw () is used to perform the switchover function in ISR. Because this function is called during the interrupt process, the status of the registers has been properly saved during the interrupt process. In the OSInCtxSw () function, stack cleaning is required. Only in this way can the stack content of the interrupted task be correctly returned.
OSTickISR () This function when UC/OS-II requires the clock baseline, that is, the clock beat, the clock beat frequency of uC/OS-II is between 10 to 100, usually for the compute side
And set it to an integer. Multiple timer counters in M16C62 can be used as the clock benchmark of the system. In this system, the clock timer A0 is used to generate a clock cycle of 100.
OSTickISR () is an interrupt response function. Therefore, in the interrupt vector table of M16C62, the interrupt vector of A0 should be allocated to OSTickISR (), and the corresponding interrupt vector number is
21 [4 ].
(3) OS _CPU_C.C File
This c file contains six simple C functions, which are the most closely related to the transplantation.
Ostaskinit () function, which is used to create a Task Stack. Ostaskcreat () and ostaskext () call this function to initialize the task.
From this point of view, ostaskinit () is the key to porting. In the previous OS _cpu_a.asm file, the task is switched by calling a Soft Interrupt 0.
Switch, the data in the stack is returned to the CPU register through the interrupt return command, so that the task with the highest priority occupies the CPU. Therefore, the ostaskinit () function is to simulate the interruption
When the processor presses the stack, the CPU register content is pressed into the task stack. In the m16c62 microcontroller, the system is divided into two stacks: User stack and interrupt stack, while in the UC/OS-II
Line task switching is achieved through Soft Interrupt 0, so the uC/OS-II Task Stack is the interrupt stack in m16c62 [1 ]. In m16c62, respond to the status of the stack after interruption
(1:
Medium
The content in the program counter PC and flg of the Mark register is saved in turn in the disconnected stack. Therefore, the ostaskstkinit () function is to simulate such a pressure stack process. Press in first
The four-digit high position of the flag and the four-digit high position of the PC pointer are pushed into the flag low position, the eight-digit middle position of the PC, and the eight-digit low position of the PC. After saving the PC and flag bit, it should be the CPU register FB,
SB, A1, A0, R3, R2, R1, and R0 allocate storage space [3 ]. The ostaskinit () function returns a pointer to the task stack.
2. Multi-task System Design
The Design of Multi-task system is based on m16c62 single chip microcomputer as the CPU, with uC/OS-II as the operating system to form a real-time multi-task system, the system includes a temperature sensor based on SPI bus
(Ds1722), an I2C bus-based Real-time clock (x1226), an LCD (jm202a), and a keyboard. The m16c62 works in the microprocessor mode and extends one
32 K * 16-bit RAM (cy7c1021b) and memory consisting of two pieces of EEPROM (EEP29010-90. Multi-task system design mainly includes: single-chip microcomputer Resource Allocation
And multi-task design.
(1) m16c62 single-chip microcomputer Resource Allocation
The m16c62 microcontroller is a 16-Bit Microcontroller with a linear addressing space
1 m, but the ram size in the disk is only 3 kbyte. Therefore, resources must be allocated reasonably to ensure the normal and stable operation of the multi-task system. All kernel code in UC/OS-II must be in the ram Zone
System stack Partition Block to 3 K RAM
Outside the zone. Through the ncrt0.a30 and sect30.inc m16c62 configuration files, you can complete the division of single chip microcomputer resources. The nc30 compiler will compile
The ncrt0.a30 and sect30.inc files initialize the CPU and allocate resources, including memory space, Ram zone allocation, interrupt vector allocation, and stack partitioning.
Classification [5] [4] [6 ].
(2) multi-task design
In addition to the idle tasks of uC/OS-II, the system also includes real-time clock tasks
Set tasks and keyboard interrupt tasks and data storage tasks. The real-time clock is mainly used to accurately record the system date. The task has a priority of 10, which is always in the working state. The temperature collection task has a priority of 20, mainly
It is the collection of temperature data. The data storage task records the time and temperature when the temperature changes greatly. The task priority is 30, in general, this task is suspended, once the temperature
If the change exceeds the preset range, the temperature collection task will issue a valid semaphore to convert the data storage task in the suspended state to the ready state. The keyboard interrupt task uses the M16C62 keyboard interrupt to set parameters.
The task is completed in the form of an interrupt handler. The system program flowchart (2) is shown.
3. Conclusion
Set
UC/OS-II is transplanted to M16C62 microcontroller, and M16C62 microcontroller as the microprocessor to form a real-time multi-task system, not only the system design is simple, the hardware structure is relative to 51 series of Single Chip
Machine is also simple, and has strong anti-interference capabilities and system stability. M16C62 is used as the microprocessor and uC/OS-II is used as the real-time operating system.
Real-Time Multitasking systems have good application prospects.
Figure (2): system main program flow diagram