Transplantation of embedded operating system μC/OS-II on LPC2378

Publisher:紫色小猫Latest update time:2023-04-03 Source: elecfans Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

  O Introduction

  At present, the market and embedded system products for scientific research in colleges and universities, such as Vxworks, Linux and Windows CE, are quite mature and provide powerful development and debugging tools. However, some development costs are expensive and the cycle is long, while μC/OS- Ⅱ is a multi-task real-time source code open operating system with a streamlined kernel and strong portability, which is very suitable for the development of some small control and experimental systems.


  1 Introduction to operating system and CPU

  μC/OS-II is a priority-based preemptive real-time multi-tasking operating system, which includes tasks management, time management, inter-task synchronous communication (semaphores, mailboxes, message queues) and memory management functions. Most of the code is written in C language, and a very small number of codes closely related to the processor are written in assembly language to facilitate portability. As a real-time operating system with open source code, it can manage up to 64 tasks and supports a variety of inter-process communication mechanisms such as semaphores, mailboxes, and message queues. At the same time, users can tailor the functional modules in the kernel according to their needs.


  LPC2378 is an embedded reduced instruction set microcontroller based on the ARM7TDMI-S core. It contains an ARM7TDMI-SCPU that supports simulation and is suitable for applications that require serial communication for various purposes. The architecture supports 7 processor modes including user, soft interrupt, interrupt, management, abort, undefined, and system. There are 31 general-purpose 32-bit registers and 6 status registers inside the ARM7TDMI-S processor. LPC2378 contains a 10/100 EthernetMAC, USB 2.0 full-speed interface, 4 UART interfaces, 2 CAN channels, 1 SPI interface, 2 synchronous serial ports (SSP), 3 I2C interfaces, 1 I2S interface and MiniBus (MiniBus is only used for LPC2378, it is an 8-bit data/16-bit address parallel bus). The following takes the transplantation of μC/OS-Ⅱ on the industrial-grade chip LPC2378 as an example, and introduces the general methods and processes of μC/OS-Ⅱ operating system transplantation and the solutions to related problems by analyzing the operating system kernel.


  2 μC/OS-Ⅱ kernel structure and working principle

  2.1 Basic structure of the kernel

  Figure 1 is a simple kernel architecture diagram close to μC/OS-Ⅱ. The kernel has three interfaces reserved for upper-layer applications, namely soft protection, ITC and DSR. Since the μC/OS-Ⅱ operating system kernel is a deprivable real-time multitasking kernel, the highest priority task can always get the right to use the CPU once it is ready. If the interrupt service subroutine puts a high-priority task into the ready state, when the interrupt is completed, the interrupted task is suspended and the high-priority task starts running.

  


  2.2 Basic working principle of μC/OS-Ⅱ kernel

  In a multi-tasking system, the operating system kernel is responsible for managing each task, or allocating a CPU to each task, and is responsible for communication and collaboration between tasks. Task switching is a basic service provided by the kernel. The basic working principle of μC/OS-Ⅱ multi-tasking operating system is as follows:

  (1) Before using all services of μC/OS-Ⅱ, the initialization function OSInit() must be called to initialize all variables and data structures. At the same time, the idle task OSTaskIdle() must be created and given the lowest priority level and permanent ready state. , while completing the initialization of the task control block (TCB), the initialization of the TCB priority table, the initialization of the TCB linked list, and the initialization of the event control block (ECB) linked list.

  (2) Call OSTaskCreate() or OSTaskCreateExt() to create at least one new task and assign a certain priority to the task, and they have their own set of CPU registers and their own stack space.

  (3) Call the OSSTART() function to find the task control block with the highest priority established by the user from the task ready table, and then start multi-task scheduling.


  3 μC/OS-Ⅱ transplantation process on LPC2378 and analysis of related issues

  Taking the transplantation on the LPC2378 microcontroller as an example, we analyze the general method of transplanting the μC/OS-II operating system. The development environment used is the integrated development environment ADS1.2 of ARM Company.


  3.1 Porting code

  (1) The codes of μC/OS-Ⅱ that are independent of the CPU type include μC/OS-Ⅱ.H, μC/OS-Ⅱ.C, OS_CORE.C, OS_TASK.C, OS_TIME.C, OS_SEM.C, OS_MBOX.- C, OS_MUTEX.C, OS_FLAG.C, which means these files can be added directly without modification.

  (2) The codes related to the CPU type of μC/OS-II include OSCPU.H, OS_CPU_A.ASM, OS_CPU_C.C, which means that the user needs to modify these functions according to the selected CPU type before adding them to the kernel.


  3.2 Definition and modification of OS_CPU.H file

  The OS_CPU.H file defines compiler- and CPU-related data types, stack width and growth methods, and macro definitions for switching interrupts. Since the stack growth methods supported by microprocessors and microcontrollers are different, the macro OS_STK_GRWOTH needs to be defined based on the type supported by the selected chip LPC2378. Since the ARM7 TD-MI-S kernel stack supports the top-down growth method, Therefore the following definition should be made:

  #define OS_STK_GROWTH 1 //The stack is the other three macros OS_CRITICAL_METHOD, OS_ENTER_CRITICAL(), and OS_EXIT_CRITICAL() in the OS_CPU.H file that are long from top to bottom. They are used to define the switch interrupt method and the implementation of the switch interrupt. Protect critical code by calling switch interrupt 2 macros as follows:

  

program


  3.3 Definition and writing of main functions in the OS_CPU_C.C file

  OS_CPU_C.C requires users to write 10 simple functions:

  

program


  Combined with the hardware and register characteristics of the CPU core to be transplanted, briefly analyze and create the task stack initialization function:

  


  The other nine functions must be declared, but do not necessarily contain task code.


  3.4 Transplantation of μC/OS-Ⅱ

  The transplantation of μC/OS-Ⅱ also requires the preparation of four simple assembly language functions; namely OSStartHighRdy(), OSintCtxSw(), OSTIckISR(), OSCtxSw().


  3.4.1 Function OSStart() calls OSStartHighRdy()

  The function OSStart() is used to call OSStartHighRdy() to start the task with the highest priority among the ready tasks:

  

program


  3.4.2 Clock interrupt service routine

  μC/OS-Ⅱ requires the user to provide a periodic clock source to implement time delay and timeout confirmation functions. The clock beat occurs 10 to 100 times per second. The clock tick interrupt must be started after starting multitasking. However, since the Osatart() function will not return, the user cannot implement this operation. Therefore, the tick can be initialized in the first task started by μC/OS after OSStart() is run. Interrupt. The simple code of OSTicklSR() based on LPC2378 transplantation is written as follows:

  

program


  When a clock beat interrupt occurs, the CPU will automatically push the CPU register into the stack, but does not include the storage page register PPAGE. If the addressing range of the microcontroller system exceeds 64 KB, you need to assign a value to PPAGE to distinguish different 16 KB addresses. , you need to push PPAGE into the stack. When the time delay item OSTCBDly in the task control block of a task decreases to zero, OSTi-mtick() enters the ready state. OSIntExit() will call the interrupt-level task switching function OSIntCtxSw to perform task switching instead of executing subsequent instructions. If no higher priority task enters the ready state, the CPU will return to the pre-interrupt state.


  3.4.3 Task-level task switching

  In fact, task-level switching is achieved by executing soft interrupt instructions or, depending on the processor, executing TRAP instructions. The vector address of the interrupt service subroutine, TRAP or exception handling must point to OSCtXSW(), and the system will automatically push the breakpoint pointer into the stack when jumping to the interrupt service routine, and store the breakpoint pointer on the stack. The interrupt return instruction IRET can push the breakpoint pointer into the PC register function of the CPU and restore the breakpoint of the task to be run, so that the breakpoint can be saved and restored.

  

program


  3.4.4 Interrupt level task switching

  OSIntExit() executes the task switching function in the ISR by calling OSIntSw(). Because OSIntCtxSw() is called in the ISR, it is assumed that all processor registers are correctly saved to the stack of the interrupted task. Most of the code of the OSIntSw() function is the same as the OSCtxSw() function. The only difference is that because the ISR has saved the CPU register, there is no need to save the CPU register in the OSIntSw() function. When transplanting the operating system, the code of this program is as follows:

  

program


  

program


  3.5 Problems in transplantation

  The software debugging of the ARM processor runs directly on the system's external SRAM through the JTAG port. Therefore, before program debugging, the ARM processor's development environment software first calls the initialization file (*.ini). The user can adjust the external memory and device according to the system's external memory. address to modify the file. If this file is incorrect, the development environment software will not be able to communicate with the processor via JTAG. During system debugging, programs often run away. After testing and analysis, there are four main reasons:

  (1) The interrupt vector address of the interrupt handler is not assigned correctly, resulting in the CPU being unable to run to the interrupt handler location after an interrupt occurs;

  (2) Add several NOP empty statements after the task switching statements in OsctxSw and OSIntctxSw to ensure that the corresponding instructions for task switching are completed. If no corresponding no-operation instructions are added at these locations, the program will also run away;

[1] [2]
Reference address:Transplantation of embedded operating system μC/OS-II on LPC2378

Previous article:Design of motion control system based on CAN bus
Next article:Design of intelligent digital acquisition board based on LPC2138 chip and LP02138 SOC

Recommended ReadingLatest update time:2024-11-22 21:49

FM radio + automatic station search + station storage designed by STC89C52RC single chip microcomputer
The radio has just been debugged, but the signal is not very good yet. The radio module uses the TEA5767 chip and LCD 1602 display. Enthusiasts can download and debug it by themselves. Can manually adjust the automatic search station The source code of the microcontroller is as follows: #include reg52.h #include "t
[Microcontroller]
FM radio + automatic station search + station storage designed by STC89C52RC single chip microcomputer
Tiny4412 C language implementation of water lamp, Tiny4412 bare metal program [3]
Earlier we used assembly to complete a running light experiment: Tiny4412 assembly running light code, Tiny4412 bare metal LED operation     ----   -   -- -- -- -  --   -- Revise: # ${MKBL2} ${SOURCE_FILE} bl2.bin 14336                                  ./${MKBL2} ${SOURCE_FILE} bl2.bin 14336 or: MKBL2
[Microcontroller]
Tiny4412 C language implementation of water lamp, Tiny4412 bare metal program [3]
Interface Design and Programming between AT89S51 Single Chip Microcomputer and 82C55
  1. Hardware interface circuit      The figure below shows the circuit diagram of an AT89S51 microcontroller with an 82C55 expansion chip. In the figure, 74LS373 is an address latch, P0.1 and P0.0 are connected to the address lines A1 and A0 of the 82C55 through 74LS373; P0.7 is connected to the chip select terminal
[Microcontroller]
Interface Design and Programming between AT89S51 Single Chip Microcomputer and 82C55
How to use JLink to indirectly burn Nor and Nand Flash of S3C2410 and S3C2440 development boards
1. Brief description JLink's debugging and Flash burning functions are very powerful, but the Flash operations of S3C2410 and S3C2440 are a bit troublesome: SDRAM needs to be set when burning Nor Flash, otherwise the speed is very slow; burning Nand Flash is only theoretically possible, but no one has directly impleme
[Microcontroller]
How to use JLink to indirectly burn Nor and Nand Flash of S3C2410 and S3C2440 development boards
Design of 0.01℃ digital thermometer based on AT89C51 control
Temperature measurement is particularly important in the fields of physics experiments, medical care, food production, etc., especially in thermal experiments (such as teaching experiments on specific heat capacity, heat of vaporization, heat-work equivalent, pressure-temperature coefficient, etc.). Thermometers cur
[Microcontroller]
Design of 0.01℃ digital thermometer based on AT89C51 control
Let's learn mini2440 bare metal development (Part 4) -- S3C2440 timer learning
Overview of S3C2440 Timer Principle The s3c2440 has five 16-bit timers, timers 0, 1, 2, and 3 have pulse width modulation (PWM) functions, so these four timers are also called PWM timers. Timer 4 is an internal timer with no external output pins. The clock source of the timer is PCLK. The frequency required for t
[Microcontroller]
Let's learn mini2440 bare metal development (Part 4) -- S3C2440 timer learning
Design and implementation of communication interface between S3C44B0X and multi-channel simulator control board based on ARM 7
  0 Introduction   ARM (Advanced RISC Machines) is a 32-bit microprocessor that has been increasingly widely used in various fields in recent years. It is not only the name of a company, but also the general name of a class of microprocessors, including ARM7, ARM9, ARM9E, ARM10E, SecurCore and other series. Among th
[Microcontroller]
Design and implementation of communication interface between S3C44B0X and multi-channel simulator control board based on ARM 7
PIC microcontroller port expansion technology based on I2C bus
1. Overview With the development of single-chip microcomputer technology, single-chip microcomputers are increasingly used in the field of electromechanical products. On the one hand, it is hoped that the embedded system composed of single-chip microcomputers is as powerful as possible, with strong processing
[Microcontroller]
PIC microcontroller port expansion technology based on I2C bus
Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号