Improvement and optimization of IRQ interrupt response mechanism based on μC/OSII and ARM7 interrupt mechanism

Publisher:快乐航程Latest update time:2020-09-09 Source: elecfansKeywords:μC Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Nowadays, among embedded processor chips, the processor with ARM7 as the core is the most widely used one. It has multiple working modes and supports two different instruction sets (standard 32-bit ARM instruction set and 16-bit Thumb instruction set). μC/OSII is a preemptive, multi-tasking real-time operating system designed for embedded applications. It can be used for various 8-bit, 16-bit and 32-bit microcontrollers or DSPs. The transplantation of μC/OSII to ARM7 has unique advantages, so "μC/OSII+ARM7" has become a widely used platform.


Regardless of the type of ARM processor, or whether the embedded system has an operating system, interrupt technology is a key technology in the real-time interaction between the computer and the outside world. When an external event occurs, the CPU must respond to the interrupt in time to process the corresponding event. Therefore, whether interrupts can be nested is the main factor affecting the real-time performance of embedded systems.


1 ARM7 interrupt processing

There are two main types of interrupts in the ARM7 processor. This article mainly discusses the response mechanism of the IRQ interrupt exception. When the interrupt request IRQ arrives and the CPU enters the interrupt response, the CPU will automatically complete the following tasks: first, store the current values ​​of PC and CPSR in the LR and SPSR of the interrupt mode; then, operate the running status bit in the CPSR to make the CPU enter the interrupt mode and turn off the interrupt; finally, change the value of PC to 0x00000018, so that the CPU execution jumps to the IRQ interrupt entry 0x00000018. At 0x00000018 in the exception vector table, an "LDR PC, [PC, #0xff0]" instruction is used. This instruction used at the IRQ is different from other vectors. When the CPU executes this instruction but has not jumped yet, the value of PC is 0x00000020 (because the ARM7TDMI core is a three-stage pipeline structure), 0x00000020 minus 0x00000FF0 is 0xFFFFF030, which is the address unit of the special register VICVectAddr of VIC. This register stores the entry of the interrupt service program of the IRQ to be serviced, so read the value of the VICVectAddr register and then put it into the PC program pointer, that is, jump to the corresponding interrupt service program, so that the CPU starts to execute the interrupt service program.


2 Handler macro analysis

In the "μC/OSII+ARM7" system, only the ARM7 IRQ interrupt is used. Since the interrupt systems of different ARM chips are not exactly the same, it is impossible to write interrupt and clock beat porting code that is common to all processors using the ARM core. However, in order to allow users to write interrupt service programs in C language without being troubled by the hardware differences of the processor, here, according to the requirements of μC/OSII for interrupt service programs and the characteristics of the ARM7 architecture and ADS compiler, an assembly macro-Handler suitable for all ARM7 core processors is written. This macro implements the general interface between the assembly language code and the C language function code of the "μC/OSII+ARM7" interrupt service program. Its function is to package the user's C language interrupt handler. Only after this packaging can the system execute the user's interrupt handler.


The interrupt service program flow is shown in Figure 1. When entering the Handler macro, first save the values ​​of LR, SPSR and related registers in the stack in interrupt mode to facilitate breakpoint recovery. Then add 1 to the global variable OSIntNeSTIng that records the number of system interrupts, turn off the interrupt and switch to system mode, and call the C language interrupt handler. After executing the interrupt handler, call the interrupt function to obtain the task control block pointer and task priority of the highest priority ready task. After returning to the interrupt mode, by comparing the priority of the current task with the task to be switched, determine whether to switch tasks, and finally return to the breakpoint.

Analysis and Optimization of ARM7 Interrupt Process Based on μC/OSII

Figure 1 Interrupt service program flow

The assembly part of the IRQ exception handling code --Handler macro:

MACRO

$IRQ_Label HANDLER $IRQ_Excep TI ON_Func TI ON

EXPORT $IRQ_Label; Output label

IMPORT $IRQ_Excep TI on_Function; referenced external label

$IRQ_Label

SUB LR, LR, #4; Calculate return address

STMFD SP!, {R0R3, R12, LR}; save task environment

MRS R3, SPSR; save status

STMFD SP, {R3, SP, LR}^; R3, SP, LR that saves user status

;OSIntNesting++

LDR R2, =OSIntNesting

LDRB R1,[R2]

ADD R1,R1,#1

STRB R1, [R2]

SUB SP,SP,#4*3

MSR CPSR_c, #(NoInt | SYS32Mode)

;Switch to system mode to operate related registers

CMP R1, #1

LDREQ SP, =StackUsr

;When the first interrupt occurs, a new variable is created to store the variables used in the interrupt to avoid storage space conflicts

BL $IRQ_Exception_Function ;Call the C language interrupt handler

MSR CPSR_c, #(NoInt | SYS32Mode); Switch to system mode

LDR R2, =OsEnterSum

;OsEnterSum, disable interrupts when OSIntExit exits

MOV R1, #1

STR R1, [R2]

BL OSIntExit

LDR R2, =OsEnterSum

; The interrupt service routine needs to exit, so OsEnterSum=0

MOV R1, #0

STR R1, [R2]

MSR CPSR_c, #(NoInt | IRQ32Mode); switch back to interrupt mode

LDMFD SP, {R3, SP, LR}^; Restore user status R3, SP, LR

LDR R0, =OSTCBHighRdy

LDR R0, [R0]

LDR R1, =OSTCBCur

LDR R1, [R1]

CMP R0, R1

ADD SP,SP,#4*3

MSR SPSR_cxsf, R3

LDMEQFD SP! , {R0R3, R12, PC}^ ; Do not switch tasks

LDR PC, =OSINTCtxSw; perform task switching

MEND

END


Through the analysis of the Handler macro, we know that the user's C language interrupt handler runs in the privileged mode - system mode, and the interrupt is turned off when the CPU executes the interrupt service program, so this system adopts the simplest non-nested interrupt method. The advantage of this method is that the context data will not be destroyed by any order of interrupts; the disadvantage is that when the interrupt service program is executed, the interrupt cannot be nested according to the interrupt priority, the delay time is long, and the interrupt is only accepted again when an ISR is completely completed and exits the interrupt, which reduces the real-time characteristics of the system. In order to improve the real-time performance of the system, its interrupts need to be optimized.


3 Interrupt Optimization

Rewriting the HANDLER macro in the μC/OSII kernel can realize ARM interrupt nesting. Although this improves the real-time performance of the system, it damages the stability and portability of the system operation. Through the analysis of the interrupt process, a template for writing an interrupt service program is given below, which makes full use of the feature that ISR is executed in the privileged mode - system mode to realize the interrupt nesting conditions. The interrupt service program template is as follows:

void ISR(void) {

OS_ENTER_CRITICAL(); //Disable interrupt in interrupt service routine Clear interrupt flag; //Prevent interrupt from entering low priority multiple times without clearing interrupt flag; //Disable low priority interrupt

S_EXIT_CRITICAL(); //Open the user's C language code in the interrupt service routine; //Perform the work that the user needs to do in the interrupt

VICVectAddr=0; //Set the entry address of the interrupt service program to 0

}


Since the Handler macro has saved registers such as LR, SPSR, return address, and stack pointer before the interrupt, the only thing left to do is to turn on and off the interrupt. Since the interrupt-disabled system mode is entered before entering the C interrupt handler, the interrupt must be reopened in the C language, and the C language cannot perform register operations, so the soft interrupt OS_EXIT_CRITICAL() must be called to reopen the interrupt. Before turning on the interrupt, it is necessary to determine whether the global variable OsEnterSum is 0 after decrementing 1, so the soft interrupt OS_ENTER_CRITICAL() must be called before calling the interrupt to turn OsEnterSum to 1. Some processing can be performed in the critical section, such as clearing the interrupt flag, turning off low-priority interrupts, etc. After executing the C language interrupt service program, VICVectAddr must be set to 0. This is a requirement of the ARM7 processor core and must be written in this way, otherwise some errors will occur (such as failure to enter the interrupt for the second time, etc.).


Conclusion

"μC/OSII+ ARM7" is a platform widely used in current embedded systems, suitable for small and medium-sized embedded systems with low complexity. Based on an in-depth analysis of the "μC/OSII+ ARM7" interrupt mechanism, this paper improves the IRQ interrupt response mechanism and proposes an optimization scheme. Experiments have shown that this method can achieve interrupt nesting and improve system real-time performance, and has certain application value.

Keywords:μC Reference address:Improvement and optimization of IRQ interrupt response mechanism based on μC/OSII and ARM7 interrupt mechanism

Previous article:Analysis of abnormal interrupt mechanism of ARM S3C4510B system
Next article:How to use the combination of ASIC and ARM to achieve powerful functions

Recommended ReadingLatest update time:2024-11-16 13:45

ADC touch screen operation of s3c2440
principle: 8-channel AD input, converted into 10-bit binary data. At the maximum A/D conversion clock of 2.5MHZ, it can reach 500KSPS. The XP, XM, YP and YM of 2440 are directly connected to the touch screen. The ADC and touch screen interface share the same A/D converter. Touch screen interface mode: 1. Norma
[Microcontroller]
Storage of C51 variables
1. Global variables and local variables The difference between global variables and local variables is the difference in scope. In addition, there are static global variables and static local variables. Global variables have a global scope. Once defined in one source file, they can also be used in other source files.
[Microcontroller]
Design of clothing production station machine based on LPC11C14 microcontroller that can read RFID tags
    The clothing manufacturing industry is a typical labor-intensive industry, and information-based production management is usually lacking, especially workshop management. Modern enterprises expect to use high-tech to enhance the competitiveness of products and reduce costs, and maximize production capacity by effe
[Microcontroller]
Design of clothing production station machine based on LPC11C14 microcontroller that can read RFID tags
Design of serial bus chip testing experimental platform based on AT89C51 microcontroller
When applying serial interface chips to expand the system, after initially selecting the serial interface chip, in order to better understand the chip's resources, developers usually build a simple hardware circuit and compile corresponding software before system design. Test, and then determine the final design plan
[Microcontroller]
Design of serial bus chip testing experimental platform based on AT89C51 microcontroller
C51 interrupt counter
#include reg51.h #define uchar unsigned char #define uint  unsigned int  #define long unsigned long uint tc; static uint i; fly ledbuff ; fly ledchar ={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; void init_en() { EA = 1; ET1=1; ET0=1; TMOD = 0X51; TH0 = 0XFC; TL0 = 0X67; TR0=1; TR1=1; } void clac(u
[Microcontroller]
C51 interrupt counter
Fingerprint recognition management system using S3C2410 and Linux system
Fingerprint recognition technology is increasingly being used in finance, transportation, security, and other fields, as well as in daily work and life. Fingerprint recognition algorithms usually involve a large number of matrix operations, domain transformations, trigonometric functions, etc., which are typical compu
[Microcontroller]
Fingerprint recognition management system using S3C2410 and Linux system
STC89C52MCU -- DS18B20 temperature sensor
DS18B20 temperature sensor uses "single bus" serial transmission mode At present, the serial buses for microcontroller data transmission are mainly Inter IC Bus, SPI and SCI. Among them, the IIC bus communicates in a synchronous serial two-wire mode (one data line and one clock line), the SPI bus communicates in a syn
[Microcontroller]
Temperature display circuit based on DS18B20 and 89C2051
An economical household temperature measurement system composed of a single bus digital temperature sensor DS18B20 and 89C2051. Its temperature display is "-XXC", with an accuracy of ±0.5℃ and a temperature measurement range of -55℃ to +125℃. The following is a detailed analysis of the system composition and the imp
[Industrial Control]
Temperature display circuit based on DS18B20 and 89C2051
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号