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

Configuring interrupts from the UART More...

Collaboration diagram for UART Interrupt control:

Functions

void uart_enable_interrupts (uint32_t uart, enum uart_interrupt_flag ints)
 Enable Specific UART Interrupts. More...
 
void uart_disable_interrupts (uint32_t uart, enum uart_interrupt_flag ints)
 Enable Specific UART Interrupts. More...
 
void uart_enable_rx_interrupt (uint32_t uart)
 Enable the UART Receive Interrupt. More...
 
void uart_disable_rx_interrupt (uint32_t uart)
 Disable the UART Receive Interrupt. More...
 
void uart_enable_tx_interrupt (uint32_t uart)
 Enable the UART Transmit Interrupt. More...
 
void uart_disable_tx_interrupt (uint32_t uart)
 Disable the UART Transmit Interrupt. More...
 
void uart_clear_interrupt_flag (uint32_t uart, enum uart_interrupt_flag ints)
 Mark interrupt as serviced. More...
 
static bool uart_is_interrupt_source (uint32_t uart, enum uart_interrupt_flag source)
 Determine if interrupt is generated by the given source. More...
 

Detailed Description

Configuring interrupts from the UART

To have an event generate an interrupt, its interrupt source must be unmasked. This can be achieved with uart_enable_interrupts(). Interrupts which are no longer needed can be disabled through uart_disable_interrupts().

In order for the interrupt to generate an IRQ and a call to the interrupt service routine, the interrupt for the target UART 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 unmasking the desired interrupt, and routing the desired UART's interrupt through the NVIC.

// Unmask receive interrupt
// Make sure the interrupt is routed through the NVIC
#define NVIC_UART0_IRQ
Definition: lm3s/nvic.h:21
void nvic_enable_irq(uint8_t irqn)
NVIC Enable Interrupt.
Definition: nvic.c:57
void uart_enable_rx_interrupt(uint32_t uart)
Enable the UART Receive Interrupt.
Definition: uart.c:459
#define UART0
Definition: uart.h:49

If a more than one interrupt is to be enabled at one time, the interrupts can be enabled by a single call to uart_enable_interrupts(). For example:

// Unmask receive, CTS, and RI, interrupts
void uart_enable_interrupts(uint32_t uart, enum uart_interrupt_flag ints)
Enable Specific UART Interrupts.
Definition: uart.c:430
@ UART_INT_RI
Definition: uart.h:401
@ UART_INT_RX
Definition: uart.h:397
@ UART_INT_CTS
Definition: uart.h:400

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 UART ISR. The ISR should query the IRQ flags to determine which event caused the interrupt. For this, use uart_is_interrupt_source(), with the desired UART_INT flag. After one or more interrupt sources are serviced, the IRQ flags must be cleared by the ISR. This can be done with uart_clear_interrupt_flag().

A typical UART ISR may look like the following:

void uart0_isr(void)
{
uint32_t serviced_irqs = 0;
// Process individual IRQs
process_rx_event();
serviced_irq |= UART_INT_RX;
}
process_cts_event();
serviced_irq |= UART_INT_CTS;
}
// Clear the interrupt flag for the processed IRQs
}
void uart0_isr(void)
void uart_clear_interrupt_flag(uint32_t uart, enum uart_interrupt_flag ints)
Mark interrupt as serviced.
Definition: uart.c:508
static bool uart_is_interrupt_source(uint32_t uart, enum uart_interrupt_flag source)
Determine if interrupt is generated by the given source.
Definition: uart.h:540

Function Documentation

◆ uart_clear_interrupt_flag()

void uart_clear_interrupt_flag ( uint32_t  uart,
enum uart_interrupt_flag  ints 
)

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]uartUART block register address base UART register base addresses
[in]intsInterrupts which to clear. Any combination of interrupts may be specified by OR'ing then together

Definition at line 508 of file uart.c.

References UART_ICR.

◆ uart_disable_interrupts()

void uart_disable_interrupts ( uint32_t  uart,
enum uart_interrupt_flag  ints 
)

Enable Specific UART Interrupts.

Disabe any combination of interrupts. Interrupts may be OR'ed together to disable them with one call. For example, to disable both the RX and CTS interrupts, pass (UART_INT_RX | UART_INT_CTS)

Parameters
[in]uartUART block register address base UART register base addresses
[in]intsInterrupts which to disable. Any combination of interrupts may be specified by OR'ing then together

Definition at line 446 of file uart.c.

References UART_IM.

Referenced by uart_disable_rx_interrupt(), and uart_disable_tx_interrupt().

Here is the caller graph for this function:

◆ uart_disable_rx_interrupt()

void uart_disable_rx_interrupt ( uint32_t  uart)

Disable the UART Receive Interrupt.

Parameters
[in]uartUART block register address base UART register base addresses

Definition at line 469 of file uart.c.

References uart_disable_interrupts(), and UART_INT_RX.

Here is the call graph for this function:

◆ uart_disable_tx_interrupt()

void uart_disable_tx_interrupt ( uint32_t  uart)

Disable the UART Transmit Interrupt.

Parameters
[in]uartUART block register address base UART register base addresses

Definition at line 492 of file uart.c.

References uart_disable_interrupts(), and UART_INT_TX.

Here is the call graph for this function:

◆ uart_enable_interrupts()

void uart_enable_interrupts ( uint32_t  uart,
enum uart_interrupt_flag  ints 
)

Enable Specific UART Interrupts.

Enable any combination of interrupts. Interrupts may be OR'ed together to enable them with one call. For example, to enable both the RX and CTS interrupts, pass (UART_INT_RX | UART_INT_CTS)

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

Parameters
[in]uartUART block register address base UART register base addresses
[in]intsInterrupts which to enable. Any combination of interrupts may be specified by OR'ing then together

Definition at line 430 of file uart.c.

References UART_IM.

Referenced by uart_enable_rx_interrupt(), and uart_enable_tx_interrupt().

Here is the caller graph for this function:

◆ uart_enable_rx_interrupt()

void uart_enable_rx_interrupt ( uint32_t  uart)

Enable the UART Receive Interrupt.

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

Parameters
[in]uartUART block register address base UART register base addresses

Definition at line 459 of file uart.c.

References uart_enable_interrupts(), and UART_INT_RX.

Here is the call graph for this function:

◆ uart_enable_tx_interrupt()

void uart_enable_tx_interrupt ( uint32_t  uart)

Enable the UART Transmit Interrupt.

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

Parameters
[in]uartUART block register address base UART register base addresses

Definition at line 482 of file uart.c.

References uart_enable_interrupts(), and UART_INT_TX.

Here is the call graph for this function:

◆ uart_is_interrupt_source()

static bool uart_is_interrupt_source ( uint32_t  uart,
enum uart_interrupt_flag  source 
)
inlinestatic

Determine if interrupt is generated by the given source.

Parameters
[in]uartUART block register address base UART register base addresses
[in]sourcesource to check.

Definition at line 540 of file uart.h.

References UART_MIS.