One way to disable/enable interrupt in ARM 7 user mode-use Soft Interrupt for Keil MDK

Source: Internet
Author: User

Recently, a program needs to be disconnected in user mode, but the architecture of ARM 7 determines that the interruption can only be changed in privileged mode, therefore, we want to use the Soft Interrupt of arm to achieve Guanzhong disconnection and open interruption.

 

To use soft interrupt, you must first support hardware commands. The arm command is SWI.

The SWI command format is:

SWI {condition} 24-bit instant count

SWI commands are used to generate software interruptions so that user programs can call the operating system's system routines. The operating system provides corresponding system services in the SWI exception handling program. The 24-bit immediate number in the command specifies the type of the system routine that the user program calls. The relevant parameters are transmitted through the General Register, when the 24-bit immediate number in the instruction is ignored, the type of the system routine called by the user program is determined by the content of the General Register R0. At the same time, the parameter is passed through other General registers.

Command example:

SWI 0x02; this command calls the system routine of the operating system number 02.

 

In the Keil MDK, the keyword _ SVC can generate the hardware SWI command so that the processor can respond to the software interrupt. The keyword _ SVC, which is described in the Keil MDK help file as follows:

The _ SVC keyword declares that the Super User calls the (SVC) function. This function uses up to four parameters similar to Integers and returns up to four results through the value_in_regs structure.
_ SVC is a function qualifier. It affects the function type.

Syntax
_ SVC (INT svc_num) Return-type function-Name ([argument-list]);
Where:
Svc_num is the immediate value used in the SVC command.
It is an expression and its calculation result is an integer in the following range:
• 0 to 224-1 in the arm command (24-bit value)
• The value in the 16-bit thumb command is 0-255 (8-bit value ).

To use software interruptions in the Keil MDK, do two things: first, change the Startup File and compile the Assembly entry for software interruptions. in this Assembly entry, function redirection is performed based on the software interrupt command number. second, write the C language service function for the corresponding command number. the following is an example of how to use a software interrupt to enable or disable the gateway.

 

Step 1: Change the startup code

The startup code of the Keil MDK is similar to the following statement:

Vectors ldr pc, reset_addr
Ldr pc, undef_addr
Ldr pc, swi_addr
Ldr pc, pabt_addr
Ldr pc, dabt_addr

The blue statement is the first command to be executed after the program is reset, that is, the entry to the reset exception. The red statement is the entry to the software interruption exception that will be jumped to after a software interruption command is executed. when the "swi_addr DCD swi_handler" clause is used for intercommunication, the software interruption jumps to the statement marked as swi_handler, which is the command number used to process the software interruption. the source code is as follows:

Export swi_handler
Extern enableirqfunc; Enable interrupt function name, implemented in C Language
Extern disableirqfunc; disable the interrupt function name, which is implemented in C Language
Swi_handler
Stmfd SP !, {R0, R12, LR}; stack entry
LDR r0, [LR, #-4]; obtain the command
Bic r0, R0, #0xff000000; obtain the software interruption command number
CMP r0, #0; compared with 0, because I used the software interrupt command 0 to interrupt the use of the software interrupt command 1
Bleq enableirqfunc; zero call enable interrupt function
Blne disableirqfunc; non-zero call prohibition of interrupt functions
Ldmfd SP !, {R0, R12, PC} ^; outbound Stack

 

Part 2: Compile the C language service function for the corresponding command number.

Declare software interruption:

_ SVC (0x00) void enableirq (void); // enable interruption
_ SVC (0x01) void disableirq (void); // disable interruption.

 

Compile service functions:

Void disableirqfunc (void) <br/>{< br/> int temp; <br/> _ ASM <br/>{< br/> Mrs temp, spsr <br/> orrtemp, temp, #0x80 <br/> MSR spsr_c, temp <br/>}< br/> void enableirqfunc (void) <br/>{< br/> int temp; <br/>__ ASM <br/> {<br/> Mrs temp, spsr <br/> bictemp, temp, #0x80 <br/> MSR spsr_c, temp <br/>}< br/>}

 

At this point, the soft interrupt that can enable and disable interruption ends. Let's take a look at the execution process.

 

In the program, if you want to disconnect, you only need to use: disableirq ();

When executing this function, the MDK compiler automatically uses Soft Interrupt commands, that is, SWI 0x01.

After arm executes this software interrupt command, a software interrupt exception occurs, and the program jumps to the software interrupt exception service function, that is, the assembly code is marked as swi_handler, the command number for software interruption is 0x01, and then the blne disableirqfunc statement is executed to call the function to disable interruption.

 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.