Porting μC/OS-II to Cortex-M3 series microcontrollers
2026-04-06 05:59:50··#1
Introduction μC/OSII is a simple, efficient, and open-source real-time embedded operating system with excellent scalability and portability, widely used in various embedded processors. It plays a significant role in improving product quality, reducing development cycles, and lowering costs. This paper focuses on the porting process and application of μC/OSII, specifically the ARM CortexM3 core microprocessor. 1. Introduction to μC/OSII and ARM CortexM3 The real-time operating system μC/OSII is a priority-based preemptive real-time kernel. It boasts high program readability, good portability, fixed code, customizability, and high flexibility. To date, μC/OSII has been running on over 40 different microprocessor architectures, ranging from 8-bit to 64-bit. The main features of μC/OSII include: it is a priority-preemptive real-time multitasking operating system; it can handle and schedule 56 user tasks, with dynamically adjustable task priorities; it provides semaphores, mailboxes, and message queues for inter-task communication and synchronization; and it has excellent customizability, minimizing system ROM and RAM size. ARM is currently the most widely used RISC microprocessor architecture in the embedded field. Its advantages, including low cost, low power consumption, and high performance, have secured its leading position in embedded system applications. The current ARM series includes processors such as ARM7, ARM9, ARM9E, ARM10, and ARM11. The Cortex-M3 core, launched by ARM in 2006, is a high-performance processor core and a microcontroller version of the new ARM V7 instruction set architecture. It can be used in enterprise applications, automotive systems, home networks, and wireless technologies. Its main features are: ① Low power consumption; ② Low gate count, resulting in excellent cost-effectiveness; ③ Short interrupt latency; ④ Low debugging cost; ⑤ Nested Vectored Interrupt Controller (NVIC), tightly integrated with the processor core for low-latency interrupt handling; ⑥ Reducible Memory Protection Unit (MPU) for memory protection. 2. Porting μC/OSII: Luminary Micro's LM3S series microcontrollers include an ARM Cortex-M3 MCU core operating at 50 MHz, embedded Flash and SRAM, a low-dropout regulator, integrated power-down and power-on reset functions, analog comparators, a 10-bit ADC, SSI, GPIO, watchdog timer and general-purpose timers, UART, I2C, motion control PWM, and quadrature encoder inputs. They are ideal for applications in building and home automation, factory automation and control, industrial power supplies, stepper motors, brushed and brushless DC motors, and AC induction motors. This porting was performed in the following environment: the compiler used was IAR FOR ARM, and the target board was the EasyARM8962 development board from Zhou Ligong. The host computer was connected to the target board via LMLINK JTAG to establish a cross-development and debugging environment. During the porting process, the μC/OSII core source code does not need to be modified and can be placed directly in the μC/OSII Source folder. The μC/OSII\\Ports directory contains the porting code for μC/OSII based on the LM3S microcontroller, including three essential files: OS_CPU_C.C, OS_CPU_A.ASM, and OS_CPU.H. The Startup.S file in the Target directory contains the microcontroller's startup code and interrupt vector table. Target.C and Target.H provide the microcontroller initialization function TargetInit() and other simple peripheral control API functions. The hierarchical structure is shown in Figure 1. [img=339,387]http://cms.cn50hz.com/files/RemoteFiles/20081225/736639001.gif[/img] Figure 1 Hierarchical Structure Porting μC/OSII to an ARM processor requires modifying three ARM architecture-related files: OS_CPU.H, OS_CPU_A.ASM, and OS_CPU_C.C. The porting process for these three files is described below. (1) The OS_CPU.H file contains constants, macros, and custom types required by μC/OSII. ① Data types defined in OS_CPU.H. In this porting, μC/OSII redefined the data types, as shown below: [img=207,210]http://cms.cn50hz.com/files/RemoteFiles/20081225/736639002.jpg[/img] ② Modify the content related to the ARM processor. The stack growth direction of different processors is different. The stack of ARM CortexM3 grows from high address to low address. Set OS_STK_GROWTH to 1, as shown in the following program: #define OS_STK_GROWTH1 (2) In the C functions defined in OS_CPU_C.C, the OSTaskStkInit() function is related to the CPU, so the porting code needs to modify this function. The program is as follows (this function is called to initialize the stack used by the task when initializing the task): [img=525,422]http://cms.cn50hz.com/files/RemoteFiles/20081225/736639003.jpg[/img] (3) The porting of the OS_CPU_A.ASM file μC/OSII requires writing 5 simple assembly language functions. ① OS_ENTER_CRITICAL(): Disable interrupt sources. ② OS_EXIT_CRITICAL(): Re-enable interrupt sources. ③ OSStartHighRdy(): Run the task with the highest current priority. ④ OSCtxSw(): Called when a task relinquishes CPU usage rights. ⑤ OSIntCtxSw(): Called in the interrupt service function OSIntExit() to implement interrupt-level task switching. Because the LM3S microcontroller currently only supports the high 3 bits of the 8-bit interrupt priority, shifting 1 left by 5 bits gives 00100000B, whose macro definition is OS_CRITICAL_INT_PRIOEQU (1<<5). The ARM CortexM3 uses the OSPendSV() function for quick context switching. The C language expression for OSPendSV() is as follows: [img=283,228]http://cms.cn50hz.com/files/RemoteFiles/20081225/736639004.jpg[/img] After completing the above work, simply write the 3 files in the Target directory according to the actual situation of the target board, and μC/OSII can run on the LM3S8962 microcontroller. 3 Practical Application After the porting work was completed, a program was written that can perform CAN communication, control LEDs with buttons, and connect to the host via RS232 serial port to perform operations such as reading and writing to the SD card. Below is a portion of the program code: [img=412,155]http://cms.cn50hz.com/files/RemoteFiles/20081225/736639005.jpg[/img] The task priority is defined in Main.H as: [img=181,90]http://cms.cn50hz.com/files/RemoteFiles/20081225/736639006.jpg[/img] The task code for creating the task is: [img=580,408]http://cms.cn50hz.com/files/RemoteFiles/20081225/736639007.jpg[/img] SDExample is a GUI interface written for easy observation of SD card operation tasks. Select the serial port baud rate corresponding to the program and connect the hardware. As shown in Figure 2, the SD card can be successfully operated. [img=313,216]http://cms.cn50hz.com/files/RemoteFiles/20081225/736639008.gif[/img]Figure 2 GUI interface for SD card operation task writing[b]Conclusion[/b]μC/OSII, as an excellent real-time operating system, has been ported to microprocessors of various architectures. This design successfully ported it to the LM3S8962 and verified the correctness of the porting through an example. This porting only did some basic work. Further development can be carried out on this basis to make full use of the performance of the LM3S series microcontrollers and the characteristics of μC/OSII, and play a certain role in the field of testing and maintenance. References [1] Labrosse Jean J. Embedded Real-Time Operating System μC/OSII [M]. Translated by Shao Beibei, et al. 2nd edition. Beijing: Beijing University of Aeronautics and Astronautics Press, 2003. [2] Zhao Ning, Chen Ming, He Pengju. Porting and Application of Embedded Operating System μC/OSII on ARM [J]. Computer Technology and Application, 2004 (4): 2931. [3] Zhou Ligong, et al. CortexM3 Development Guide - Based on LM3S8000, 2007. [4] Zhou Ligong. ARM Microcontroller Fundamentals and Practice [M]. Beijing: Beijing University of Aeronautics and Astronautics Press, 2003.