- IAR Embedded Workbench for RISC-V 3.40
- 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 RISC-V provides the following primitives related to writing interrupt functions, concurrent functions, and OS-related functions:
The extended keywords:
__interrupt,__task,__monitorThe pragma directives:
#pragma enter_leave,#pragma no_epilogue,#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 core 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 RISC-V 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
By default (for supported devices), the vector table is populated with a default interrupt handler which calls the abort function. 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.
For a list of devices that support interrupt vectors and default interrupt handlers, see the release notes in the Information Center.
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 = 0x7
__interrupt void MyInterruptRoutine(void)
{
/* Do something */
}Note
An interrupt function must have the return type void, and it cannot specify any parameters.
Interrupt and C++ member functions
Only static member functions can be interrupt functions.
Special function types can be used for static member functions. For example, in the following example, the function handler is declared as an interrupt function:
class Device
{
static __interrupt void handler();
};