[Country EMBED strategy] [050] [2440-key interrupt programming]

Source: Internet
Author: User

Program Structure optimization

1. Put different functions into different files

2. Normally do not use the MMU in bootloader, so switch off the MMU

Keystroke initialization

1. Open the schematic of the Development Board and locate the key

2. Configure the corresponding Gpio to interrupt to initialize the interrupt source

Initializing the interrupt Controller

1.SUBMASK and mask must ensure that there is no blocking interrupt, Gpio is not a sub-interrupt, so do not set Submask,mode and priority to save the default value can be set Intmsk (0x4a000008)

2.eint4_7 corresponds to 4 interrupts, set Eintmask (0X560000A4) separately. Note that setting the Eintmask register must be set before Intmask

3. During core preprocessing, interrupts are turned off, where interrupts need to be opened

Interrupt Handling

1. When the interrupt is generated, it jumps to the interrupt vector table, executes the LDR pc, _IRQ

2. Save the Environment

2.1. The current LR points to the position of the pc+8, where it is necessary to return to lr-4 when the interrupt is returned, to calculate the value of LR, sub LR, LR, #4

2.2. Save register to Stack, Stmfd sp!, {r0-r12, LR}

2.3. Call the interrupt handler function, Handle_int

2.4. From the stack recovery register, Ldmfd sp!, {r0-r12, pc}^

2.5.handle_int need to do: 1. Determine the interrupt source 2. Depending on the interrupt source, different interrupt processing is performed. Intoffset (0x4a000014) indicates that the interrupt source has been interrupted.

3. Clear the interrupt to clear the SRCPND (0x4a000000) and INTPND (0x4a000010) corresponding bits of the request before recovering the environment, to ensure that the next interrupt can be processed. If the next interrupt arrives, it is found that the corresponding bit of SRCPND and INTPND is 1, indicating that the last interrupt has not been processed, ignoring this interrupt request. If the use is a sub-interrupt need to clear 0 subsrcpnd, if the use of eint4-ent23 so to clear Eintpend (0X560000A8). Note that to clear the interrupt, write 1 to the corresponding bit

4. Note that the SP stack pointer set in IRQ mode is R13_IRQ, and the initialize stack SP pointer is r13_svc. So when initializing the stack pointer is to set the address for both R13_IRQ and R13_SVC

/********************************************************************* Name: irq* function: Interrupt handling ************************** /irq://Save Environment Sub LR, LR, #4            //three-level pipelining, PC pointing to the specified address with the address offset of the current execution instruction +8stmfd sp!, {r0-r12, LR}   Save the value of the R0-R12 and LR registers into the stack//processing interrupt BL process_int//Recovery Environment Ldmfd sp!, {r0-r12, pc}^   //Load R0-r12 and LR register values from the stack, ^ means restore SPSR to CPSR

/********************************************************************* Name: int* Author: d* Time: 2015.11.10* function: Interrupt controller ****** **************************************************************//*********************************************** Macro Definition ********************************************************************/#define SRCPND 0x4a000000//Interrupt Source Request Register # define INTPND 0x4a000010//Interrupt Request Register # define INTMSK 0x4a000008//Interrupt Mask Register # define Eintmask 0x 560000A4//External Interrupt Mask Register # define Eintpend 0X560000A8//external interrupt request register. text/************************************************* Name: init_int* function: Initialize interrupt *********************************************************************/ . Global init_intinit_int://Shield Interrupt Ldr r0, =INTMSKMVN R1, #0//Shield interrupt, set INTMSK[31-0]:0XFFFFFFFFSTR R1, [r0]//off Interrupt Mrs R0, CPSRO RR R0, R0, #0x000000C0//Off interrupt, set CPSR[7-6]:11MSR CPSR, R0mov pc, lr/*************************************************** Name: enable_int* function: Open Interrupt **************************************/.global enable_intenable_int://enable to interrupt Ldr R0, =eintmaskldr R1, [r0]bic R1, R1, # 0x00000900//Enable external interrupts, set external interrupt mask eint8:0 Eint11:0str R1, [R0]ldr r0, =intmskldr R1, [r0]bic R1, R1, #0x00000020//Enable interrupt, set interrupt mask eint8_23:0str R1, [r0]//Open interrupts Mrs R0, Cpsrbic r0, R0, #0x000000C0//Turn on interrupt, set CPSR[7-6]:00MSR CPSR, R0mov pc, lr/********************************************** Name: process_int* function: Processing interrupt ***************************************************************** /.global Process_intprocess_int:mov IP, LR//Save link Register, Nest call Save return address//Read interrupt source Ldr R0, =eintpend//Read external interrupt Request if interrupt from Intoffset Read in, sub-interrupts from Subsrcpndldr R1, [r0]//processing interrupt Source CMP r1, #0x00000100//If EINT8, call EINT8 interrupt handling beq eint8_processcmp R1, #0x00000800///AS The result is EINT11, call EINT11 interrupt processing beq EINT11_PROCESSB end_process_int//If none, end interrupt handling eint8_process:bl on_led//lit ledb end_process              _INTEINT11_PROCESS:BL off_led//off Ledb end_process_intend_process_int://clear interrupt Source Ldr R0, =eintpendldr R1, [r0]str R1, [R0] //Clear external Interrupt Request LDR R0, =srcpndldr R1, [R0]orr R1, R1, #0x00000020//clear Interrupt source request, set eint8_23:0, if a sub-interrupt also to clear subsrcpndstr R1, [R0]ldr r0,  =intpndldr R1, [R0]orr R1, R1, #0x00000020//Clear interrupt request, set EINT8_23:0STR r1, [r0]mov LR, IP//recover link Register, nested call recovery return address MOV pc, LR

Note: In a function nested call to save LR to the IP, in the function is called other functions will change, when the external function returns, you need to restore the LR from the IP, used to return to the external function is invoked

R0-R15 Register function

R0-r3

Used as an incoming function parameter, the outgoing function returns a value. The R0-R3 can be used for any purpose between subroutine calls. The called function does not have to recover R0-R3 before returning. If the calling function needs to use R0-R3 's content again, it must retain the content.

R4-r11

The local variable that is used to store the function. If the called function uses these registers, it must restore the values of these registers before returning.

R12

is an internal call to the temporary register IP. It is used for this role in the process link glue code (for example, interactively manipulating the glue code). Between procedure calls, it can be used for any purpose. The called function does not have to recover R12 before returning.

R13

is the stack pointer sp. It cannot be used for any other purpose. The value stored in the SP must be the same as the value entered when exiting the called function.

R14

is the link register LR. If you save the return address, you can use R14 between calls for other purposes, to restore when the program returns

R15

is the program counter PC. It cannot be used for any other purpose.

Note: In the interrupt program, all registers must be protected and the compiler will automatically protect the R4~r11

[Country EMBED strategy] [050] [2440-key interrupt programming]

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.