Keil Logo

__swi: Software Interrupt

Software interrupt functions (SWI) are defined using the __swi function attribute:

<[>type<]> funcname (<[>args<]>) __swi (<[>num<]>)

Where:

type is the type of the value returned from the function. If no type is specified, int is assumed.
funcname is the name of the function.
args is the argument list for the function.
__swi indicates the function is a software interrupt function.
num is the number encoded in the SWI instruction.

The __swi attribute affects the generated code of the function as follows:

  • Calls to the function are generated using the SWI instruction.
  • SWI functions are entered into the SWI table in the SWI_VEC.S file.
  • SWI functions are coded with standard parameter passing conventions.
  • SWI functions execute in Supervisor Mode and are protected from other interrupts.

The following example program shows how to use the __swi attribute along with the SWI_VEC.S file. A complete interrupt example project is provided in the \KEIL\ARM\EXAMPLES\INTERRUPT\ folder.

stmt  level    source

    1          volatile long x;
    2
    3          void func (void) __swi(3)
    4          {
    5   1      x++;
    6   1      }
    7
    8          void main (void)
    9          {
   10   1      while (1)
   11   1        func ();
   12   1      }
.
.
.
*** CODE SEGMENT '?PR?func?T?main':
    5: x++;
 00000000  4800      LDR         R0,=x ; x
 00000002  6801      LDR         R1,[R0,#0x0] ; x
 00000004  3101      ADD         R1,#0x1
 00000006  6001      STR         R1,[R0,#0x0] ; x
    6: }
 00000008  4770      BX          R14
 0000000A          ENDP ; 'func?T'


*** CODE SEGMENT '?PR?main?main':
    8: void main (void)
 00000000  B500      PUSH        {LR}
   10: while (1)
 00000002          L_3:
 00000002  DF03      SWI         0x3 ; func?T
 00000004  E7FD      B           L_3  ; T=0x00000002
 00000006  BC08      POP         {R3}
 00000008  4718      BX          R3
 0000000A          ENDP ; 'main'

Note

  • Function with __swi attribute are using the current selected CPU mode (ARM or Thumb).
  • Software interrupts 0-7 are used by the Advanced Real-Time Kernel (ARARM) and may not be used in your application.
  • The __swi attribute must be used when declaring or prototyping external functions. For example:
    extern long func (int i) __swi (8);
    
  • Parameters may be passed only in registers. This restriction reduces the maximum number of parameters to four int values.
  • The CARM run-time library contains a default SWI_Handler (in SWI.S) that generates an endless loop using the B $ instruction. To use automatic SWI table generation, the SWI_VEC.S file must be added to your project.
  • You may implement your own SWI handler by adapting the SWI.S file to your requirements. This file is a part of the Keil CARM run-time library and is linked into each project by default.
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.