Writing STM32 OS step by step [I] Preface

Publisher:数字冒险Latest update time:2017-01-04 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

  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.


Keywords:STM32 Reference address:Writing STM32 OS step by step [I] Preface

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

STM32 power management and low power mode
7.1 Power Management Overview Processors make extensive use of clock gating to disable inputs to unused functions and blocks, so that only the clocks that are being used are Dynamic power is consumed only by logic that is actively in use. The ARMv7-M architecture supports a system sleep mode that stops the Cortex-M3 a
[Microcontroller]
STM32 power management and low power mode
Solution to the STM32 program not running after adding printf
The default output device of the standard library function is the display. To output to the serial port or LCD, you must redefine the functions related to the output device called in the standard library function. For example: if printf is output to the serial port, the output in fputc needs to be directed to the seri
[Microcontroller]
Google officially starts pushing Fuchsia OS to users, adding new variables to the battle for operating systems
Recently, media reported that Google has begun pushing the Fuchsia OS system to all Nest Hub users, adding a new variable to the global operating system competition.   It is reported that unlike the Linux kernel at the bottom of Android and Hongmeng, Fuchsia uses Zircon, a new kernel developed by Google itself. It is
[Embedded]
Google officially starts pushing Fuchsia OS to users, adding new variables to the battle for operating systems
Program explanation of GPIO pin initialization in stm32 and related knowledge expansion introduction
The following is the GPIO initialization program segment that I want to explain, trying to explain every aspect that Xiaobai does not understand during the learning process. When explaining the code, I follow a program and jump into its declaration or definition as I explain. voidLED_GPIO_Config(void) {             /*
[Microcontroller]
STM32 Quadrature Encoder Programming
As shown in the figure, each TIMER of STM32 has an orthogonal encoder input interface. TI1 and TI2 are input filtered, and edge detection generates TI1FP1. TI2FP2 is connected to the encoder module. By configuring the working mode of the encoder, the encoder can be counted forward/reverse. As shown in the figure
[Microcontroller]
STM32 Quadrature Encoder Programming
UC_COS ported to STM32
1 Transplant the LCD screen based on the LCD screen lighting (17) in the peripherals section 1 Download the GUI and unzip it to the root directory of the project Picture 1 2 Add all the folders in the GUI to the project file. Picture 2 3 Add the files in each folder in the GUI to the project.     1 When adding Confi
[Microcontroller]
UC_COS ported to STM32
Surface Duo firmware contains Microsoft's canceled Andromeda OS code
       According to Windowslatest , the initial Surface Phone will ship with "Andromeda OS," a future version of the previously canceled Windows 10 (Mobile) that will feature significantly different features.   IT Home learned that Andromeda OS was originally designed to support multiple device launches, including dua
[Mobile phone portable]
Surface Duo firmware contains Microsoft's canceled Andromeda OS code
Realizing multi-task communication on μC/OS-Ⅱ by using interrupt mode based on S3C44BOX
Introduction Nowadays, various embedded electronic products have penetrated into every aspect of our lives, and embedded operating systems have become one of the hottest topics. Among them, the free real-time operating system μC/OS-Ⅱ has been successfully applied to many commercial products due to its portabil
[Microcontroller]
Realizing multi-task communication on μC/OS-Ⅱ by using interrupt mode based on S3C44BOX
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号