Other Features Interrupt 1. Introduction This cheat sheet explores interrupt processing in NC30. It describes the method to write an interrupt subroutine for software and hardware interrupts. 2. Software interrupts Software interrupts are generated by instructions that generate an interrupt request when executed. Software interrupts are non-maskable interrupts. The various types of software interrupts are described below. 2.1 Undefined instruction interrupt The UND instruction causes this interrupt. The vector address for this interrupt is located in a fixed vector table from FFFDC to FFFFDF. As shown in the example below, the user must define the interrupt sub routine for UND in the startup file sect30.inc, use .glb directive, in order to define the interrupt subroutine “UND_Instruction” and write the subroutine function in C file. Sect30.inc file: ;-----------------------------------------------------------------; fixed vector section ;-----------------------------------------------------------------.section fvector,ROMDATA .org 0fffdcH UDI: .glb UND_Instruction .lword UND_Instruction Example 1 Defining the Interrupt Subroutine for UND Interrupt in Startup File sect30.inc Note: If the interrupt routine is not defined in startup file, then a dummy_int routine is executed. 2.2 Overflow interrupt This interrupt is generated by the overflow flag. If the overflow bit is set to 0 and the INTO instruction is executed, then this interrupt is generated. The vector address for this interrupt is located in the fixed vector table between addresses FFFE0 to FFFE3. The overflow flag is changed by the following instructions: ABS, ADC, ADCF, ADD, CMP, DIV, DIVU, DIVX, NEG, RMPA, SBB, SHA and SUB. 2.3 Break interrupt This interrupt is generated by BRK instruction. The vector address for this interrupt is located in the fixed vector table between addresses FFFE4 to FFFE7. 2.4 Interrupt generated by INT instruction This interrupt is generated by INT instruction followed by an interrupt number. 64 software interrupts (0 to 63) are available. Software interrupts 0 to 31 are assigned to peripheral I/O interrupts. The software interrupt can be defined using #pragma INTCALL preprocessor directive. Below is the syntax for defining software interrupt. Syntax: #pragma INTCALL INT-No. C-function-name() – call the function using INT instruction. As shown in the below example, the Func() is defined as a software interrupt with an interrupt number of 20. Therefore, the compiler uses the INT 20 instruction to call the interrupt routine. 1 V1.03 char Func(char); #pragma INTCALL 20 Func(); void main(void){ Func(1); F0010 D812 _main MOV.B #1H,R1L F0012 EBD4 INT #20 } F0014 F3 RTS F0015 04 NOP char Func(char cFlag){ F0016 7CF201 _Func ENTER #01H F0019 722BFF MOV.B R1L,-1H[FB] return (cFlag); F001C 0AFF MOV.B -1H[FB],R0L F001E 7BF5 STC FB,A1 F0020 7AD5 LDC A1,SP F0022 ED80 POPM FB F0024 FB REIT } Example 2 Defining Software Interrupts Note: The INT-no in #pragma INTCALL INT-no function_name cannot be greater than 63. If the software interrupt number in the above example is greater than 63, then the compiler will generate the flowing error message when compiling: [Error(ccom)] Invalid #pragma INTCALL interrupt number. 3. Hardware interrupts The following are the two categories of hardware interrupts supported by M16C. 3.1 Special interrupts Special interrupts are non-maskable interrupts. Below is the list of the special interrupts: Reset: A reset interrupt occurs when reset pin on the microcontroller is pulled low. NMI interrupt: This interrupt occurs when NMI pin is pulled low. DBC: This interrupt is only used by the debugger. Watchdog timer interrupt: This interrupt occurs when the watchdog timer overflows. This interrupt resets the system. Single step interrupt: This interrupt is used exclusively for debugger purposes. The user does not normally need to use this interrupt. A single-step interrupt occurs when the debug flag (D flag) is set to 1. In this case, an interrupt is generated each time an instruction is executed. Address match interrupt: This interrupt occurs when the program's execution address matches the content of the address match register while the address match interrupt enable bit is set to 1.This interrupt does not occur if any address other than the start address of an instruction is set in the address match register. 3.2 Peripheral Interrupts These interrupts are generated by the peripheral functions built into the microcomputer (MCU) system. The types of built-in peripheral functions vary with each M16C model, as do the types of interrupt causes. The interrupt vector table uses the same software interrupt numbers (0 to 31) that are used by the INT instruction. Peripheral I/O interrupts are maskable interrupts. The following is the process for writing and defining a peripheral interrupt routine: Step 1: Writing an interrupt routine The #pragma INTERRUPT preprocessor directive is used to write the interrupt subroutine in the application code. The following is the syntax of #pragma INTERRUPT. Syntax: #pragma INTERRUPT interrupt_handler_name(vect=Vector_no) – define the interrupt handler with a vector number. 2 V1.03 #pragma INTERRUPT /B interrupt_handler_name (vect=Vect_no) - define the interrupt handler with a vector number, and do not push the register onto the stack. Use a different set of registers in the ISR. This is known as bank-switching and allows for lower interrupt latency, but does not allow interrupt-nesting. #pragma INTERRUPT /E interrupt_handler_name (vect=Vect_no) - define the interrupt handler with a vector number, and enable multiple interrupts within the ISR. As shown in the example below, the interrupt routine for a timer is defined using #pragma INTERRUPT. As the interrupt routine timerA() is defined with /B option, the compiler uses a different set of registers to process the interrupt routine. int iCounter; #pragma INTERRUPT /B timerA() void timerA(void){ F0010 EB44 _timerA FSET B iCounter += 1; F0012 C91F1004 ADD.W #1H,0410H } F0016 FB REIT F0017 04 NOP void main(){ iCounter = 0; F0018 D90F1004 _main MOV.W #0H,0410H } F001C F3 RTS Example 3 Declaring an ISR Using #pragma INTERRUPT /B Note: If the interrupt subroutine function is called from the program, then the compiler will output the error message [Error(ccom)] interrupt function, called when compiling. Step 2: Set the interrupt vector table The user must set the interrupt subroutine in the variable vector table. The variable vector table is located in the startup file sect30.inc. Given below is the process for setting the vector table. Open the startup file sect30.inc. At the end of the file, the interrupt vector is declared as shown in Example 4. Define the interrupt function name in the variable vector table. Define the function at the appropriate vector location, i.e. define the timer routine for timer B0 at comments ;timer B0 (vector 26). As shown in Example 4, define the interrupt function name in place of dummy_int preceded by an underscore ( _ ) using .glb and .lword directives. As shown in the example below, the interrupt function timerA() is defined for peripheral timer A3 at vector address 24 using .glb and .lword directives. ;--------------------------------------------------------------; variable vector section ;--------------------------------------------------------------.section vector ;variable vector table .org VECTOR_AD .lword dummy_int ;BRK (vector 0) .org (VECTOR_ADR+16) .glb .lword .lword .lword _timerA ;timer A3 (vector 24) _timerA dummy_int ;timer A4 (vector 25) dummy_int ;timer B0 (vector 26) 3 V1.03 .lword .lword .lword .lword .lword . . . .lword .lword dummy_int ;timer B1 (vector 27) dummy_int ;timer B2 (vector 28) _dummy_int ;int0 dummy_int ;int1 (vector 30) dummy_int ;int2 (vector 31) dummy_int ;vector 46 (software interrupt vector) dummy_int ;vector 47 (software interrupt vector) Example 4 Adding the Vector Address in the Interrupt Vector Table in Startup File sect30.inc 4. References For more details, please refer to the Compiler User Manual (nc30ue.pdf). 4 V1.03 Revision History Ver. No. Date 1.01 2007/08/0 6 1.02 1.03 2007/08/3 0 2008/01/10 Section No. 1,2,3,4 2,3 Changes Format and grammar Change in variable name and function name 1,2,3,4 Format and grammar 2 3.2 Format and grammar Additional line for #pragma INTERRUPT/B Changed “HEW” to “NC30” 1 5 Reason for Changes RTA and RSO comments KPIT review comments KPIT review comments RTA review V1.03