nRF51 SDK - S130 SoftDevice
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
SWI

The SWI driver provides functions to manage software interrupts (SWI). It allows users to allocate SWIs and pass extra flags to interrupt handler functions.

Usage

Software interrupts are allocated from the available SWI pool. By default, there are six interrupts available: SWI0 to SWI5. Note that this number is decreased if you use a SoftDevice or the Gazell or ESB protocol. For more information about resource usage for a specific protocol library, see the appropriate specification.

Initialization

You must initialize the driver by calling the nrf_drv_swi_init function. If initialization is successful, the function returns NRF_SUCCESS.

Allocation

To allocate an interrupt, call the nrf_drv_swi_alloc function. As input, you must provide a pointer to the nrf_swi_t variable, an event handler function, and a value that indicates the interrupt priority. If allocation is successful, the function returns NRF_SUCCESS, and the provided pointer variable is set.

Event handler and flags

When allocating the driver, you must provide a callback function (see nrf_swi_handler_t). The arguments for this function are the SWI and some user flags. The user flags are represented as bit mask.

To trigger the interrupt, call nrf_drv_swi_trigger with a specific flag number. When an SWI is triggered, the corresponding callback function is called with all specified flags (represented as a bit mask). The flags can be used, for example, for interrupt source tracking, or they can be ignored if not needed. Note that the callback function might be invoked with more than one flag set if the SWI is triggered more than once with different flag numbers from a same or higher priority interrupt handler.

Special options

To disable specific SWIs from the pool, add a global define during compilation (SWI_DISABLE0 to SWI_DISABLE5). Any disabled SWI will not be allocated and will therefore not be available to the user.

Example

See the following code for a usage example. Note that this example is not power optimized.

static nrf_swi_t my_swi;
void swi_event_handler(nrf_swi_t swi, nrf_swi_flags_t flags)
{
/* If "my_swi" was triggered and flag #3 is present... */
if ((swi == my_swi) && (flags & (1 << 3)))
{
/* Do something. */
}
return;
}
/*
....
*/
ret_code_t err_code;
nrf_swi_t my_swi;
/* Initialize the library. */
err_code = nrf_drv_swi_init();
APP_ERROR_CHECK(err_code);
/* Allocate and setup one SWI. */
err_code = nrf_drv_swi_alloc(&my_swi, swi_event_handler, APP_IRQ_PRIORITY_LOW);
APP_ERROR_CHECK(err_code);
while (true)
{
/* Trigger SWI with flag #3. */
nrf_drv_swi_trigger(my_swi, 3);
/* Wait for a second. */
nrf_delay_ms(1000);
}

Usage with a SoftDevice

The library can be used with a SoftDevice. However, the amount of interrupts that are available to the user will be limited in this case. See the SoftDevice specification for more information about the resource usage.

Be careful when specifying APP_IRQ_PRIORITY_HIGH as SWI priority. Long interrupt routines with high priority might affect the proper operation of the SoftDevice.