hardware_irq

Hardware interrupt handling. More...

Typedefs

typedef void(* irq_handler_t) (void)
 Interrupt handler function type. More...
 

Functions

void irq_set_priority (uint num, uint8_t hardware_priority)
 Set specified interrupt's priority. More...
 
uint irq_get_priority (uint num)
 Get specified interrupt's priority. More...
 
void irq_set_enabled (uint num, bool enabled)
 Enable or disable a specific interrupt on the executing core. More...
 
bool irq_is_enabled (uint num)
 Determine if a specific interrupt is enabled on the executing core. More...
 
void irq_set_mask_enabled (uint32_t mask, bool enabled)
 Enable/disable multiple interrupts on the executing core. More...
 
void irq_set_exclusive_handler (uint num, irq_handler_t handler)
 Set an exclusive interrupt handler for an interrupt on the executing core. More...
 
irq_handler_t irq_get_exclusive_handler (uint num)
 Get the exclusive interrupt handler for an interrupt on the executing core. More...
 
void irq_add_shared_handler (uint num, irq_handler_t handler, uint8_t order_priority)
 Add a shared interrupt handler for an interrupt on the executing core. More...
 
void irq_remove_handler (uint num, irq_handler_t handler)
 Remove a specific interrupt handler for the given irq number on the executing core. More...
 
bool irq_has_shared_handler (uint num)
 Determine if the current handler for the given number is shared. More...
 
irq_handler_t irq_get_vtable_handler (uint num)
 Get the current IRQ handler for the specified IRQ from the currently installed hardware vector table (VTOR) of the execution core. More...
 
static void irq_clear (uint int_num)
 Clear a specific interrupt on the executing core. More...
 
void irq_set_pending (uint num)
 Force an interrupt to be pending on the executing core. More...
 
void user_irq_claim (uint irq_num)
 Claim ownership of a user IRQ on the calling core. More...
 
void user_irq_unclaim (uint irq_num)
 Mark a user IRQ as no longer used on the calling core. More...
 
int user_irq_claim_unused (bool required)
 Claim ownership of a free user IRQ on the calling core. More...
 

Detailed Description

Hardware interrupt handling.

The RP2040 uses the standard ARM nested vectored interrupt controller (NVIC).

Interrupts are identified by a number from 0 to 31.

On the RP2040, only the lower 26 IRQ signals are connected on the NVIC; IRQs 26 to 31 are tied to zero (never firing).

There is one NVIC per core, and each core's NVIC has the same hardware interrupt lines routed to it, with the exception of the IO interrupts where there is one IO interrupt per bank, per core. These are completely independent, so, for example, processor 0 can be interrupted by GPIO 0 in bank 0, and processor 1 by GPIO 1 in the same bank.

Note
That all IRQ APIs affect the executing core only (i.e. the core calling the function).
You should not enable the same (shared) IRQ number on both cores, as this will lead to race conditions or starvation of one of the cores. Additionally, don't forget that disabling interrupts on one core does not disable interrupts on the other core.

There are three different ways to set handlers for an IRQ:

Note
If an IRQ is enabled and fires with no handler installed, a breakpoint will be hit and the IRQ number will be in register r0.

Interrupt Numbers

Interrupts are numbered as follows, a set of defines is available (intctrl.h) with these names to avoid using the numbers directly.

IRQ Interrupt Source
0 TIMER_IRQ_0
1 TIMER_IRQ_1
2 TIMER_IRQ_2
3 TIMER_IRQ_3
4 PWM_IRQ_WRAP
5 USBCTRL_IRQ
6 XIP_IRQ
7 PIO0_IRQ_0
8 PIO0_IRQ_1
9 PIO1_IRQ_0
10 PIO1_IRQ_1
11 DMA_IRQ_0
12 DMA_IRQ_1
13 IO_IRQ_BANK0
14 IO_IRQ_QSPI
15 SIO_IRQ_PROC0
16 SIO_IRQ_PROC1
17 CLOCKS_IRQ
18 SPI0_IRQ
19 SPI1_IRQ
20 UART0_IRQ
21 UART1_IRQ
22 ADC0_IRQ_FIFO
23 I2C0_IRQ
24 I2C1_IRQ
25 RTC_IRQ

Typedef Documentation

◆ irq_handler_t

typedef void(* irq_handler_t) (void)

Interrupt handler function type.

All interrupts handlers should be of this type, and follow normal ARM EABI register saving conventions

Function Documentation

◆ irq_add_shared_handler()

void irq_add_shared_handler ( uint  num,
irq_handler_t  handler,
uint8_t  order_priority 
)

Add a shared interrupt handler for an interrupt on the executing core.

Use this method to add a handler on an irq number shared between multiple distinct hardware sources (e.g. GPIO, DMA or PIO IRQs). Handlers added by this method will all be called in sequence from highest order_priority to lowest. The irq_set_exclusive_handler() method should be used instead if you know there will or should only ever be one handler for the interrupt.

This method will assert if there is an exclusive interrupt handler set for this irq number on this core, or if the (total across all IRQs on both cores) maximum (configurable via PICO_MAX_SHARED_IRQ_HANDLERS) number of shared handlers would be exceeded.

Parameters
numInterrupt number Interrupt Numbers
handlerThe handler to set. See irq_handler_t
order_priorityThe order priority controls the order that handlers for the same IRQ number on the core are called. The shared irq handlers for an interrupt are all called when an IRQ fires, however the order of the calls is based on the order_priority (higher priorities are called first, identical priorities are called in undefined order). A good rule of thumb is to use PICO_SHARED_IRQ_HANDLER_DEFAULT_ORDER_PRIORITY if you don't much care, as it is in the middle of the priority range by default.
Note
The order_priority uses higher values for higher priorities which is the opposite of the CPU interrupt priorities passed to irq_set_priority() which use lower values for higher priorities.
See also
irq_set_exclusive_handler()

◆ irq_clear()

static void irq_clear ( uint  int_num)
inlinestatic

Clear a specific interrupt on the executing core.

This method is only useful for "software" IRQs that are not connected to hardware (i.e. IRQs 26-31) as the the NVIC always reflects the current state of the IRQ state of the hardware for hardware IRQs, and clearing of the IRQ state of the hardware is performed via the hardware's registers instead.

Parameters
int_numInterrupt number Interrupt Numbers

◆ irq_get_exclusive_handler()

irq_handler_t irq_get_exclusive_handler ( uint  num)

Get the exclusive interrupt handler for an interrupt on the executing core.

This method will return an exclusive IRQ handler set on this core by irq_set_exclusive_handler if there is one.

Parameters
numInterrupt number Interrupt Numbers
See also
irq_set_exclusive_handler()
Returns
handler The handler if an exclusive handler is set for the IRQ, NULL if no handler is set or shared/shareable handlers are installed

◆ irq_get_priority()

uint irq_get_priority ( uint  num)

Get specified interrupt's priority.

Numerically-lower values indicate a higher priority. Hardware priorities range from 0 (highest priority) to 255 (lowest priority) though only the top 2 bits are significant on ARM Cortex-M0+. To make it easier to specify higher or lower priorities than the default, all IRQ priorities are initialized to PICO_DEFAULT_IRQ_PRIORITY by the SDK runtime at startup. PICO_DEFAULT_IRQ_PRIORITY defaults to 0x80

Parameters
numInterrupt number Interrupt Numbers
Returns
the IRQ priority

◆ irq_get_vtable_handler()

irq_handler_t irq_get_vtable_handler ( uint  num)

Get the current IRQ handler for the specified IRQ from the currently installed hardware vector table (VTOR) of the execution core.

Parameters
numInterrupt number Interrupt Numbers
Returns
the address stored in the VTABLE for the given irq number

◆ irq_has_shared_handler()

bool irq_has_shared_handler ( uint  num)

Determine if the current handler for the given number is shared.

Parameters
numInterrupt number Interrupt Numbers
Returns
true if the specified IRQ has a shared handler

◆ irq_is_enabled()

bool irq_is_enabled ( uint  num)

Determine if a specific interrupt is enabled on the executing core.

Parameters
numInterrupt number Interrupt Numbers
Returns
true if the interrupt is enabled

◆ irq_remove_handler()

void irq_remove_handler ( uint  num,
irq_handler_t  handler 
)

Remove a specific interrupt handler for the given irq number on the executing core.

This method may be used to remove an irq set via either irq_set_exclusive_handler() or irq_add_shared_handler(), and will assert if the handler is not currently installed for the given IRQ number

Note
This method may only be called from user (non IRQ code) or from within the handler itself (i.e. an IRQ handler may remove itself as part of handling the IRQ). Attempts to call from another IRQ will cause an assertion.
Parameters
numInterrupt number Interrupt Numbers
handlerThe handler to removed.
See also
irq_set_exclusive_handler()
irq_add_shared_handler()

◆ irq_set_enabled()

void irq_set_enabled ( uint  num,
bool  enabled 
)

Enable or disable a specific interrupt on the executing core.

Parameters
numInterrupt number Interrupt Numbers
enabledtrue to enable the interrupt, false to disable

◆ irq_set_exclusive_handler()

void irq_set_exclusive_handler ( uint  num,
irq_handler_t  handler 
)

Set an exclusive interrupt handler for an interrupt on the executing core.

Use this method to set a handler for single IRQ source interrupts, or when your code, use case or performance requirements dictate that there should no other handlers for the interrupt.

This method will assert if there is already any sort of interrupt handler installed for the specified irq number.

Parameters
numInterrupt number Interrupt Numbers
handlerThe handler to set. See irq_handler_t
See also
irq_add_shared_handler()

◆ irq_set_mask_enabled()

void irq_set_mask_enabled ( uint32_t  mask,
bool  enabled 
)

Enable/disable multiple interrupts on the executing core.

Parameters
mask32-bit mask with one bits set for the interrupts to enable/disable Interrupt Numbers
enabledtrue to enable the interrupts, false to disable them.

◆ irq_set_pending()

void irq_set_pending ( uint  num)

Force an interrupt to be pending on the executing core.

This should generally not be used for IRQs connected to hardware.

Parameters
numInterrupt number Interrupt Numbers

◆ irq_set_priority()

void irq_set_priority ( uint  num,
uint8_t  hardware_priority 
)

Set specified interrupt's priority.

Parameters
numInterrupt number Interrupt Numbers
hardware_priorityPriority to set. Numerically-lower values indicate a higher priority. Hardware priorities range from 0 (highest priority) to 255 (lowest priority) though only the top 2 bits are significant on ARM Cortex-M0+. To make it easier to specify higher or lower priorities than the default, all IRQ priorities are initialized to PICO_DEFAULT_IRQ_PRIORITY by the SDK runtime at startup. PICO_DEFAULT_IRQ_PRIORITY defaults to 0x80

◆ user_irq_claim()

void user_irq_claim ( uint  irq_num)

Claim ownership of a user IRQ on the calling core.

User IRQs are numbered 26-31 and are not connected to any hardware, but can be triggered by irq_set_pending.

Note
User IRQs are a core local feature; they cannot be used to communicate between cores. Therfore all functions dealing with Uer IRQs affect only the calling core

This method explicitly claims ownership of a user IRQ, so other code can know it is being used.

Parameters
irq_numthe user IRQ to claim

◆ user_irq_claim_unused()

int user_irq_claim_unused ( bool  required)

Claim ownership of a free user IRQ on the calling core.

User IRQs are numbered 26-31 and are not connected to any hardware, but can be triggered by irq_set_pending.

Note
User IRQs are a core local feature; they cannot be used to communicate between cores. Therfore all functions dealing with Uer IRQs affect only the calling core

This method explicitly claims ownership of an unused user IRQ if there is one, so other code can know it is being used.

Parameters
requiredif true the function will panic if none are available
Returns
the user IRQ number or -1 if required was false, and none were free

◆ user_irq_unclaim()

void user_irq_unclaim ( uint  irq_num)

Mark a user IRQ as no longer used on the calling core.

User IRQs are numbered 26-31 and are not connected to any hardware, but can be triggered by irq_set_pending.

Note
User IRQs are a core local feature; they cannot be used to communicate between cores. Therfore all functions dealing with Uer IRQs affect only the calling core

This method explicitly releases ownership of a user IRQ, so other code can know it is free to use.

Note
it is customary to have disabled the irq and removed the handler prior to calling this method.
Parameters
irq_numthe irq irq_num to unclaim