The transplantation of μC/OS-II on the ARM platform is an important learning process, which helps to improve the knowledge and understanding of RTOS, thereby improving the theoretical and technical level of embedded workers. μC/OS-II is a small real-time kernel with open source code and detailed explanations. It is precisely because of its small kernel that it is easy to study, understand and master. In addition, referring to the TCP/IP protocol, standards and some public books, it is also very convenient to add the TCP/IP protocol stack, Bluetooth communication software and infrared communication protocol to μC/OS-II, and its commercial value has been recognized.
With the development of science and technology, the complexity of embedded applications is getting higher and higher. At the same time, the price of ARM system processors is getting lower and lower. The use of ARM platform + real-time operating system architecture will become more and more widespread. In view of this, this paper conducts an in-depth discussion on the transplantation of μC/OS-II on the ARM platform.
1 Introduction to μC/OS-II operating system and S3C2410 development platform
1.1 Introduction to μC/OS-II
μC/OS was first published in 1992 by Jean J.Labrosse, an American embedded system expert, in a series of articles published in the May and June issues of the magazine "Embedded System Programming". The source code of μC/OS was also published on the magazine's BBS. μC/OS-II is the latest version.
μC/OS-II is specially designed for embedded computer applications, and most of the code is written in C language. The relevant parts of the CPU are written in assembly language, and the total number of assembly language, about 200 lines, is compressed to a minimum, in order to facilitate porting to any other CPU. μC/OS-II has the characteristics of high execution efficiency, small footprint, excellent real-time performance, and scalability. The minimum kernel can be compiled to 2 KB. μC/OS-II can be ported to almost all well-known CPUs.
1.2 Composition of μC/OS-II
Strictly speaking, μC/OS-II is just a real-time operating system kernel, which only includes basic functions such as task scheduling, task management, time management, memory management, and communication and synchronization between tasks. No additional services such as input and output management, file system, network communication, etc. are proposed. However, due to the good scalability and open source code of μC/OS-II, these non-essential functions can be implemented by users according to their own needs.
μC/OS-II can be roughly divided into five parts: core, task processing, time processing, task synchronization and communication, and CPU porting [1].
(1) Core part (OSCore.c): The processing core of the operating system, including operating system initialization, operating system operation, interrupt in and out preamble, clock beats, task scheduling, event processing, etc.
(2) Task processing part (OSTask.c): The part closely related to task operation, including task creation, deletion, suspension, and resumption, etc.
(3) Clock part (OSTime.c): The smallest clock unit in μC/OS-II is timetick (clock beat). Task delay and other operations are completed here.
(4) Task synchronization and communication part: It is the event processing part, including semaphores, mailboxes, mailbox queues, event flags, etc., which are mainly used for the mutual connection between tasks and the access to critical resources.
(5) The interface with the CPU: This refers to the part of μC/OS-II that needs to be rewritten for the CPU used. Since μC/OS-II is a general-purpose operating system, its open source code is written based on the X86 kernel as an example. When applied to other processor platforms, this part of the code must be changed accordingly.
1.3 Introduction to ARM Hardware Development Platform
The hardware development platform used for debugging is a development platform based on Samsung S3C2410A chip. The S3C2410 development board is a general ARM9 development board. Its basic configuration uses Samsung's S3C2410 ARM920T chip with a main frequency of 203 MHz. It integrates SDRAM controller, NAND Flash controller, SD card reader, USB Host and USB Device controller, LCD controller, I2C bus controller, SPI bus interface, etc. The Flash space on the development board is 32 MB, and the SDRAM capacity is 128 MB.
2 S3C2410 bootloader
The original bootloader of the development board was provided by VIVI, and its operation process is divided into two stages. The code of the first stage is programmed in assembly language, and mainly completes the following tasks: (1) Initialize the CPU speed, memory, memory configuration registers, and hardware resources such as serial ports; (2) Establish a mapping map of the memory space to bring the system's software and hardware environment to a suitable state, preparing for the final call to the operating system kernel; (3) Load the operating system image into the memory; (4) Set up relevant registers and resources, jump to the main() function, and enter the second stage.
The second stage of the code is written in C language, starting from the main() function. The main tasks include: initializing the external interface of the development board (I/O interface, UART interface, LCD interface, etc.), initializing the memory mapping and memory management unit, and finally starting the Linux kernel. There are a large number of articles that have made detailed analysis of the boot program of this development board [3]. This article will not repeat them here. The focus of this article is to integrate the boot program with the μC/OS-II operating system. It not only utilizes the UART port, LCD and touch screen interface programs provided by the development board source code; the rich driver programs and interface programs such as clock and memory management, but also successfully completes the transplantation and integration of the μC/OS-II real-time operating system. [page]
3 Transplantation Tips
The kernel of μC/OS-II is divided into two parts, the code that is not related to the processor and the code that is related to the processor. During the transplantation process, three files need to be rewritten according to the characteristics of the S3C2410 processor and the ADSV1.2 development platform (here the factor of the compilation platform is specially emphasized, mainly considering that the understanding of the data format of each compilation platform is slightly different). They are OS_CPU.H and OS_CPU_C.C written in C language and OS_CPU_A.ASM written in assembly language. In addition, to integrate the S3C2410 development board boot program and the μC/OS-II kernel program, their respective main() functions must also be integrated.
3.1 Porting of OS_CPU.H
The OS_CPU.H code in the μC/OS-II kernel is written based on the X86 kernel, and the data format definition is not completely consistent with the ARM9 kernel and the ADSv1.2 development platform. The transplantation of OS_CPU.H is divided into the following 4 parts:
(1) Data type definition: During debugging, it was found that although no error was reported during compilation when defining 8-bit or 16-bit data types, these variables were not initialized or assigned values correctly as required, and errors often occurred during operation. Therefore, when rewriting the OS_CPU.H code, all variables were defined as 32-bit or 64-bit;
(2) Stack growth direction definition: ARM's stack grows from top to bottom, and OS_STK_GROWTH is defined as 1;
(3) Macro definition of switch interrupt: implemented using the assembly function of switch interrupt and placed in the OS_CPU_A.ASM file.
(4) Macro definition OS_TASK_SW(): This macro definition is the code called when μC/OS-II switches from a low priority task to a high priority task outside of ARM interrupt processing. It is always called in task-level code. In some materials, OS_TASK_SW() and OSIntCtxSw() are equated, which is not possible in the ARM kernel because the latter is the task switching function of the ARM kernel in interrupt mode, and the register groups of the processor in different modes are different, and the register contents to be protected are also different. After debugging, it is found that the following code can achieve the purpose.
OS_TASK_SW stmfd sp!, {lr}; PC is pushed into the stack, lr is actually the return address of the task, stmfd sp!, {r0-r12, lr} mrs r4, cpsr stmfd sp!, {r4} ; finally save CPSR ldr r4, =OSTCBCur ldr r5, [r4] str sp, [r5] ;Save SP in the control block of the current task ldr r5, =OSTCBHighRdy ldr r5, [r5] str r5, [r4] ;OSTCBCur = OSTCBHighRdy ldr r6, =OSPrioHighRdy ldr r6, [r6] ldr r4, =OSPrioCur str r6, [r4] ;OSPrioCur = OSPrioHighRdy ldr sp, [r5] ; get the stack pointer of the new task ldr r4, [sp], #4 msr cpsr_cxsf, r4 ; restore CPSR first ldmfd sp!, {r0-r12, lr, pc}
3.2 Porting of OS_CPU_C.CH
In OS_CPU_C.C, the most important function is OSTaskStkInit(), which is used to initialize the task stack structure when the task is created. The other hook functions can be left unchanged. The code of this function is relatively simple [2]. It should be noted that in the system described in this article, the user task runs in SVC mode and the SPSR register is not saved.
3.3 Porting of OS_CPU_A.ASM
The assembler of the OS_CPU_A.ASM file is the key and difficulty of the μC/OS-II transplantation project. It usually includes OSStartHighRdy(), OSIntCtxSw(), OSTickISR() and switch interrupt code. Among them, the main work of OSStartHighRdy() is to restore all registers corresponding to the highest priority task from the task stack in order, and its code is simple. For the switch interrupt function, the code used during debugging is as follows:
EnterCritical mrs r1, cpsr str r1, [r0] orr r1, r1, #NOINT msr cpsr_cxsf, r1 mov pc, lr ExitCritical ldr r1, [r0] msr cpsr_cxsf, r1 mov pc, lr
Previous article:Analysis of ARM-based infrared light vehicle speed management system
Next article:Embedded brain blood oxygen parameter monitor based on ARM processor
Recommended ReadingLatest update time:2024-11-16 21:54
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- e8 emulator connection keeps disconnecting
- 【Qinheng Trial】ch554 lights up lcd1602
- How is 5G charged?
- 【NXP Rapid IoT Review】+ 5. Comprehensive application of sensors
- WiFi 6 and "CHIP" will strongly promote the popularization of the Internet of Things. What do you think?
- OMRON H3Y-2 0-10s TIMER
- Cortex-M3 Technical Reference Manual
- Π-type LC filter in power supply in single chip microcomputer circuit
- Low power external wake-up
- FAQ_ How to lock the program in BlueNRG software to prevent it from being read out