- IAR Embedded Workbench for RH850 3.20.x
- IAR C/C++ Development
- Functions
- Primitives for interrupts, concurrency, and OS-related programming
Primitives for interrupts, concurrency, and OS-related programming
The IAR C/C++ Compiler for RH850 provides the following primitives related to writing interrupt functions, concurrent functions, and OS-related functions:
The extended keywords:
__interrupt,__task,__trap,__callt,__syscall,__fetrap,__fast_interrupt,__monitorThe pragma directive,
#pragma vectorThe intrinsic functions:
__enable_interrupt,__disable_interrupt, __get_interrupt_state,__set_interrupt_state.
Interrupt functions
In embedded systems, using interrupts is a method for handling external events immediately; for example, detecting that a button was pressed.
Interrupt service routines
In general, when an interrupt occurs in the code, the microcontroller immediately stops executing the code it runs, and starts executing an interrupt routine instead. It is important that the environment of the interrupted function is restored after the interrupt is handled—this includes the values of processor registers and the processor status register. This makes it possible to continue the execution of the original code after the code that handled the interrupt was executed.
Note that the RH850 microcontroller supports many interrupt sources. For each interrupt source, an interrupt routine can be written. Each interrupt routine is associated with a vector number, which is specified in the documentation from the chip manufacturer. If you want to handle several different interrupts using the same interrupt routine, you can specify several interrupt vectors.
Interrupt vectors and the interrupt vector table
For the RH850 microcontroller, the reset vector always starts at address 0x0, which is the base for the exception vectors pointed to by the RBASE system register. The interrupt vector base is pointed to by the system register INTBP.
The exception vector can be moved and is then pointed to by the system register EBASE.
By default, the vector table is populated with a default interrupt handler which returns immediately. For each interrupt source that has no explicit interrupt service routine, the default interrupt handler will be called. If you write your own service routine for a specific vector, that routine will override the default interrupt handler.
The header file iodevice.h, where device corresponds to the selected device, contains predefined names for the existing interrupt vectors.
Defining an interrupt function—an example
To define an interrupt function, the __interrupt keyword and the #pragma vector directive can be used. For example:
#pragma vector = 0x13 /* or a symbol defined in I/O header file
*/
__interrupt void MyInterruptRoutine(void)
{
/* Do something */
}Note
An interrupt function must have the return type void, and it cannot specify any parameters.
Interrupt function return methods
There are two available return methods for exceptions and interrupts: The EI return method and the FE return method. Which registers that the compiler uses for storing the return address of an interrupt handler function depends on the return method and on the exact source of the exception:
The EI return method is used for normal interrupts, for example peripheral interrupts such as timers or UARTs. These interrupts are on the EI level and use the system registers
EIPCandEIPSWto save the return address and status word contents. They must execute anEIRETinstruction to restore these resources correctly. To specify that an interrupt should use this return method, use the__ei_intkeyword.The FE return method is used for higher-level exceptions on the FE level. They store their return information in the system registers
FEPCandFEPSW, and return using theFERETinstruction. To specify that an exception should use this return method, use the__fe_intkeyword.
When a function is declared only with __interrupt, it will by default use the EI return method. To override an FE level exception handler, use the __fe_int keyword together with __interrupt, like this:
__fe_int __interrupt void __syserr_exception_handler(void)
{
exception_source = 4711;
abort();
}When you overload an exception handler, look in the file default_handler.c where all exception handlers are declared in C.
Interrupt and C++ member functions
Only static member functions can be interrupt functions. When a non-static member function is called, it must be applied to an object. When an interrupt occurs and the interrupt function is called, there is no object available to apply the member function to.