I have always wanted to write an OS similar to uCOS. I have finally made some progress in my spare time while preparing for the postgraduate entrance examination. Back to the point, I think the most difficult part of OS is the context switching problem, which is related to the architecture of MCU, so for different MCUs, this part needs to be ported. Once this problem is solved, the whole OS is half successful, of course, for a simple OS.
Well, to write an OS, you first need a development board and an emulator. My development board is STM32F4DISCOVERY, which comes with ST-LINK V2 emulator. The onboard MCU is STM32F407VGT6, which supports FPU, 32-bit ARM Cortex-M4F core, 1024KB FLASH, 192 KB RAM, which is very powerful. For other STM32 series, this OS can be used almost without modification. The development environment is IAR for ARM 6.5. If it is MDK, it is also similar, and the assembly part needs to be modified.
After studying the Port part of UCOS-II's Cortex-M4, I found it very good, so I used it directly with very few modifications. First, let's take a look at several important functions in this part. Open the os_cpu_a.asm file and locate the following place. I changed the comments to Chinese. After the OS is initialized, execute OSStart, which finally calls the OSStartHighRdy function. Note that before this, the stacks of the thread mode and exception mode are both MSP, and after this, the stack of the thread mode is PSP, and the stack of the exception mode is still MSP.
OSStartHighRdy LDR R0, =NVIC_SYSPRI14 ; Set the exception interrupt priority of PendSV LDR R1, =NVIC_PENDSV_PRI STRB R1, [R0] MOVS R0, #0 ; Initialize PSP=0 MSR PSP, R0 LDR R0, =OS_CPU_ExceptStkBase ; Initialize exception stack MSP address LDR R1, [R0] MSR MSP, R1 LDR R0, =OSRunning; Set OSRunning = TRUE MOVS R1, #1 STRB R1, [R0] LDR R0, =NVIC_INT_CTRL ; Trigger PendSV exception (cause context switch) LDR R1, =NVIC_PENDSVSET STR R1, [R0] CPSIE I ; Enable interrupt, then enter PendSV exception OSStartHang B OSStartHang ; Normally, it should not run to this
The most core function should be OS_CPU_PendSVHandler, which handles the PendSV interrupt and completes the context switch.
OS_CPU_PendSVHandler CPSID I ; Disable interrupt MRS R0, PSP ; Get PSP CBZ R0, OS_CPU_PendSVHandler_nosave; If PSP is 0, jump to OS_CPU_PendSVHandler_nosave, that is, do not save the previous context and directly enter the next context. ; Why ask? Because it is the first call, there is no context. ; Save the previous text SUBS R0, R0, #0x20; Because the register is 32 bits, 4 bytes aligned, there are 8 registers pushed automatically, so the offset is 8*0x04=0x20 STM R0, {R4-R11}; In addition to the registers that are automatically pushed onto the stack, R4-R11 must be pushed onto the stack manually LDR R1, =OSTCBCur ; Save the SP pointer above OSTCBCur->OSTCBStkPtr = SP; LDR R1, [R1] STR R0, [R1] OS_CPU_PendSVHandler_nosave ; Switch to the following context PUSH {R14}; LR is pushed onto the stack, and the C function is called next LDR R0, =OSTaskSwHook ; Call OSTaskSwHook(); BLX R0 POP {R14} LDR R0, =OSPrioCur; Set OSPrioCur = OSPrioHighRdy; LDR R1, =OSPrioHighRdy LDRB R2, [R1] STRB R2, [R0] LDR R0, =OSTCBCur; Set OSTCBCur = OSTCBHighRdy; LDR R1, =OSTCBHighRdy LDR R2, [R1] STR R2, [R0] LDR R0, [R2]; The value in R0 is the SP of the new task; SP = OSTCBHighRdy->OSTCBStkPtr; LDM R0, {R4-R11} ; Manually pop R4-R11 ADDS R0, R0, #0x20 MSR PSP, R0; PSP = New mission SP ORR LR, LR, #0x04 ; Ensure that PSP CPSIE I is used after the exception return BX LR; Exit abnormally, pop up xPSR, PC, LR, R0-R3 from PSP, and enter new task running
Then we built our own OS on this basis. First, we completed the mutual calling of the two tasks, then added SysTick task scheduling, and finally added semaphores, mailboxes and other functions.
Previous article:S3C6410 naked LED
Next article:Write STM32 OS step by step [Part 2] Environment construction
Recommended ReadingLatest update time:2024-11-17 02:51
- Popular Resources
- Popular amplifiers
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!
- Rambus Launches Industry's First HBM 4 Controller IP: What Are the Technical Details Behind It?
- 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
- The MPU9250 magnetometer ID reading does not get the correct value for the following reasons:
- CircuitPython 6.0.0 Beta 1 released
- Controlling LED brightness under ZSTACK
- [Analog Electronics Course Selection Test] + Input and Output Limitation
- Core Experience | Free evaluation and trial of Lingdong MM32 eminiboard, waiting for you!
- The most efficient solution for receiving and sending indefinite length packets on DSP serial port SCI
- (There are still many prizes left in the prize pool!) Mentor downloads the white paper and answers questions for a prize draw
- Be sure to pay attention to safety when driving on the road! Stay away from large vehicles!
- LCD screen display effect is related to viewing angle and contrast
- Flip switch based on LIS2DW12 of PSOC6;