libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
GPIO Interrupt control

Configuring interrupts from GPIO pins More...

Collaboration diagram for GPIO Interrupt control:

Functions

void gpio_configure_trigger (uint32_t gpioport, enum gpio_trigger trigger, uint8_t gpios)
 Configure the interrupt trigger on the given GPIO pins. More...
 
void gpio_enable_interrupts (uint32_t gpioport, uint8_t gpios)
 Enable interrupts on specified GPIO pins. More...
 
void gpio_disable_interrupts (uint32_t gpioport, uint8_t gpios)
 Disable interrupts on specified GPIO pins. More...
 
static bool gpio_is_interrupt_source (uint32_t gpioport, uint8_t srcpins)
 Determine if interrupt is generated by the given pin. More...
 
static void gpio_clear_interrupt_flag (uint32_t gpioport, uint8_t gpios)
 Mark interrupt as serviced. More...
 

Detailed Description

Configuring interrupts from GPIO pins

GPIO pins can trigger interrupts on either edges or levels. The type of trigger can be configured with gpio_configure_int_trigger(). To have an event on the given pin generate an interrupt, its interrupt source must be unmasked. This can be achieved with gpio_enable_interrupts(). Interrupts which are no longer needed can be disabled through gpio_disable_interrupts().

In order for the interrupt to generate an IRQ and a call to the interrupt service routine, the interrupt for the GPIO port must be routed through the NVIC with nvic_enable_irq(). For this last step, the nvic.h header is needed:

Enabling an interrupt is as simple as configuring the desired trigger, unmasking the desired interrupt, and routing the desired GPIO port's interrupt through the NVIC.

// Trigger interrupt on each rising edge
// Unmask the interrupt on those pins
// Enable the interrupt in the NVIC as well
#define NVIC_GPIOF_IRQ
Definition: lm3s/nvic.h:46
void nvic_enable_irq(uint8_t irqn)
NVIC Enable Interrupt.
Definition: nvic.c:57
void gpio_enable_interrupts(uint32_t gpioport, uint8_t gpios)
Enable interrupts on specified GPIO pins.
Definition: gpio.c:572
void gpio_configure_trigger(uint32_t gpioport, enum gpio_trigger trigger, uint8_t gpios)
Configure the interrupt trigger on the given GPIO pins.
Definition: gpio.c:527
@ GPIO_TRIG_EDGE_RISE
Rising edge trigger.
Definition: gpio.h:211
#define GPIO0
Definition: gpio.h:77
#define GPIO4
Definition: gpio.h:81
#define GPIOF
Definition: gpio.h:57

After interrupts are properly enabled and routed through the NVIC, when an event occurs, the appropriate IRQ flag is set by hardware, and execution jumps to the GPIO ISR. The ISR should query the IRQ flags to determine which event caused the interrupt. For this, use gpio_is_interrupt_source(), with the desired GPIO flag. After one or more interrupt sources are serviced, the IRQ flags must be cleared by the ISR. This can be done with gpio_clear_interrupt_flag().

A typical GPIO ISR may look like the following:

void gpiof_isr(void)
{
uint8_t serviced_irqs = 0;
// Process individual IRQs
process_gpio0_event();
serviced_irq |= GPIO0;
}
process_gpio4_event();
serviced_irq |= GPIO4;
}
// Clear the interrupt flag for the processed IRQs
}
void gpiof_isr(void)
static void gpio_clear_interrupt_flag(uint32_t gpioport, uint8_t gpios)
Mark interrupt as serviced.
Definition: gpio.h:369
static bool gpio_is_interrupt_source(uint32_t gpioport, uint8_t srcpins)
Determine if interrupt is generated by the given pin.
Definition: gpio.h:353

Function Documentation

◆ gpio_clear_interrupt_flag()

static void gpio_clear_interrupt_flag ( uint32_t  gpioport,
uint8_t  gpios 
)
inlinestatic

Mark interrupt as serviced.

After an interrupt is services, its flag must be cleared. If the flag is not cleared, then execution will jump back to the start of the ISR after the ISR returns.

Parameters
[in]gpioportGPIO block register address base GPIO register base addresses
[in]gpiosGPIO pin identifiers. Any combination of pins may be specified by OR'ing then together.

Definition at line 369 of file gpio.h.

References GPIO_ICR.

◆ gpio_configure_trigger()

void gpio_configure_trigger ( uint32_t  gpioport,
enum gpio_trigger  trigger,
uint8_t  gpios 
)

Configure the interrupt trigger on the given GPIO pins.

Sets the Pin direction, analog/digital mode, and pull-up configuration of or a set of GPIO pins on a given GPIO port.

Parameters
[in]gpioportGPIO block register address base GPIO register base addresses
[in]triggerTrigger configuration (gpio_trigger)
  • GPIO_TRIG_LVL_LOW – Trigger on low level
  • GPIO_TRIG_LVL_HIGH – Trigger on high level
  • GPIO_TRIG_EDGE_FALL – Trigger on falling edges
  • GPIO_TRIG_EDGE_RISE – Trigger on rising edges
  • GPIO_TRIG_EDGE_BOTH – Trigger on all edges
[in]gpiosGPIO pin identifiers. Any combination of pins may be specified by OR'ing then together

Definition at line 527 of file gpio.c.

References GPIO_IBE, GPIO_IEV, GPIO_IS, GPIO_TRIG_EDGE_BOTH, GPIO_TRIG_EDGE_FALL, GPIO_TRIG_EDGE_RISE, GPIO_TRIG_LVL_HIGH, and GPIO_TRIG_LVL_LOW.

◆ gpio_disable_interrupts()

void gpio_disable_interrupts ( uint32_t  gpioport,
uint8_t  gpios 
)

Disable interrupts on specified GPIO pins.

Disable interrupts on the specified GPIO pins

Note that the NVIC must be enabled and properly configured for the interrupt to be routed to the CPU.

Parameters
[in]gpioportGPIO block register address base GPIO register base addresses
[in]gpiosGPIO pin identifiers. Pins whose interrupts to disable. Any combination of pins may be specified by OR'ing them together.

Definition at line 590 of file gpio.c.

References GPIO_IM.

◆ gpio_enable_interrupts()

void gpio_enable_interrupts ( uint32_t  gpioport,
uint8_t  gpios 
)

Enable interrupts on specified GPIO pins.

Enable interrupts on the specified GPIO pins

Note that the NVIC must be enabled and properly configured for the interrupt to be routed to the CPU.

Parameters
[in]gpioportGPIO block register address base GPIO register base addresses
[in]gpiosGPIO pin identifiers. Pins whose interrupts to enable. Any combination of pins may be specified by OR'ing them together.

Definition at line 572 of file gpio.c.

References GPIO_IM.

◆ gpio_is_interrupt_source()

static bool gpio_is_interrupt_source ( uint32_t  gpioport,
uint8_t  srcpins 
)
inlinestatic

Determine if interrupt is generated by the given pin.

Parameters
[in]gpioportGPIO block register address base GPIO register base addresses
[in]srcpinssource pin or group of pins to check.

Definition at line 353 of file gpio.h.

References GPIO_MIS.