Skip to main content

IAR Embedded Workbench for RH850 3.20.x

Trap functions

In this section:

A trap is a kind of exception that can be activated when a specific event occurs or is called. A trap function is called by the TRAP assembler instruction and returns just like an interrupt function using the EIRET instruction. A fetrap function is called by the FETRAP instruction on the FE interrupt level and returns using the FERET instruction. In many respects, a trap function behaves as a normal function; it can accept parameters, return a value, and it uses the same calling convention as other functions.

The typical use for trap functions is for the client interface of an operating system. If this interface is implemented using trap functions, the operating system part of an application can be updated independently of the rest of the system.

The linker will automatically assign a vector number for the function. Vector numbers can be made explicit, but you cannot use both automatic and explicit vector number assignment in the same project.

The keywords __trap and __fetrapcan be used to define trap functions. For example, this piece of code defines a function doubling its argument:

__trap int Twice(int x)
{
  return x + x;
}

When a trap function is used, the compiler ensures that the application also will include the appropriate trap-handling code. See Assembler language interface for more information.

By default, trap functions are handled by included exception handlers defined in the file exception_vector.s. To override these default handlers with your own handlers, you must change the definitions in this file. (Remember to make a backup copy of the file first.)

Trap and C++ member functions

Virtual member functions cannot be trap functions.