STM32 Interrupts.
On STM32, interrupts are managed by the NVIC (Nested Vectored Interrupt Controller).
Let's look at its capabilities:
I'd like to point out right away that every interrupt is based on an event: a byte received over UART is an event, a timer overflow is an event. When an event occurs and the interrupt-enable flags are set, the program jumps to the interrupt handler.
CMSIS provides arrays of 3 elements for configuring the NVIC.

Let's look at how to enable/disable the usart1 interrupt in the NVIC; its number is 37 in the interrupt vector table of RM0008.
Where did the number 5 come from? The sixth interrupt will have number 5 because counting starts from zero. Since the zeroth element of the array contains interrupts numbered 0 to 31, the sixth interrupt of the first element of the array will have number 37: 6 + 31 = 37.
But that's not enough for the program to jump to the interrupt handler; on STM32 the interrupt must be enabled in 3 places:
Interrupts are controlled globally with the following commands:
You can enable the usart1 transmission complete and frame received interrupts as follows:
The names of the interrupt handlers can be found in the startup_stm32f10x_hd.s file
It should be noted that the same interrupt occurs both when a transmission completes and when a new frame is received over uart1, and to determine which event caused it, you need to use the flags. If the interrupt occurred on reception, the USART_SR_RXNE flag will be set; on transmission complete, USART_SR_TC. By the way, the flags must be cleared manually.
Each interrupt, with bits AIRCR[10:8] cleared, can be assigned a priority from 0 to 15; after reset, all interrupts have the highest priority, 0.
They are serviced according to the following rules:
The upper 4 bits of the IPRx[7:4] registers are used to set priorities; in CMSIS they are represented by the IP[x] array.
To create priority groups, you need to set the bits in the AIRCR[10:8] register according to the table.

x determines the number of priority groups, y the number of priority subgroups.
That is, if we want 16 priority groups, we need to write 3 to AIRCR[10:8], and the number written to the IPRx[7:4] register will determine the group; if we want 8 groups and 2 subgroups, we write 4 to AIRCR[10:8]; then the upper 3 bits of the IPRx[7:4] register will determine the group and the lowest bit of the upper nibble the subgroup, and so on.
But while enabling/disabling interrupts and setting priorities is convenient to do by setting the corresponding bits, it's more convenient to use the CMSIS functions for creating priority groups, although some may find it convenient to always use the CMSIS functions.
Let's look at its capabilities:
- handling up to 81 interrupts, depending on the MCU model
- a programmable priority level from 0 to 15 for each interrupt, with level 0 being the highest priority
- dynamic priority changes
- dividing interrupts into groups, and within groups into subgroups with different priority levels
I'd like to point out right away that every interrupt is based on an event: a byte received over UART is an event, a timer overflow is an event. When an event occurs and the interrupt-enable flags are set, the program jumps to the interrupt handler.
CMSIS provides arrays of 3 elements for configuring the NVIC.

Let's look at how to enable/disable the usart1 interrupt in the NVIC; its number is 37 in the interrupt vector table of RM0008.
//enable the usart1 interrupt
NVIC->ISER[1] = NVIC_ISER_SETENA_5;
//disable the uart1 interrupt
NVIC->ICER[1] = NVIC_ICER_CLRENA_5;
Where did the number 5 come from? The sixth interrupt will have number 5 because counting starts from zero. Since the zeroth element of the array contains interrupts numbered 0 to 31, the sixth interrupt of the first element of the array will have number 37: 6 + 31 = 37.
But that's not enough for the program to jump to the interrupt handler; on STM32 the interrupt must be enabled in 3 places:
- enable interrupts globally
- enable the interrupt in the NVIC
- enable the interrupt in the peripheral itself
Interrupts are controlled globally with the following commands:
// Disable IRQ interrupts
__disable_irq ();
// Enable IRQ interrupts
__enable_irq ();
You can enable the usart1 transmission complete and frame received interrupts as follows:
USART1->CR1 |=USART_CR1_TCIE | USART_CR1_RXNEIE;
The names of the interrupt handlers can be found in the startup_stm32f10x_hd.s file
It should be noted that the same interrupt occurs both when a transmission completes and when a new frame is received over uart1, and to determine which event caused it, you need to use the flags. If the interrupt occurred on reception, the USART_SR_RXNE flag will be set; on transmission complete, USART_SR_TC. By the way, the flags must be cleared manually.
void USART1_IRQHandler(void)
{
//the interrupt occurred on reception
if (USART1->SR & USART_SR_RXNE)
{
Code....
// Clear the interrupt flag
USART1->SR &= ~USART_SR_RXNE;
}
//the interrupt occurred on transmission complete
if (USART1->SR & USART_SR_TC)
{
Code....
// Clear the interrupt flag
USART1->SR &= ~USART_SR_TC;
}
}
Each interrupt, with bits AIRCR[10:8] cleared, can be assigned a priority from 0 to 15; after reset, all interrupts have the highest priority, 0.
They are serviced according to the following rules:
- the current interrupt can be preempted by a higher-priority one
- if 2 interrupts occur simultaneously, the one with the lower number in the table is executed first.
The upper 4 bits of the IPRx[7:4] registers are used to set priorities; in CMSIS they are represented by the IP[x] array.
//we want to set the usart1 priority to 5, but in Keil it will be 80
//since it takes the four lower bits into account
NVIC->IP[37] = 0x50;
To create priority groups, you need to set the bits in the AIRCR[10:8] register according to the table.

x determines the number of priority groups, y the number of priority subgroups.
That is, if we want 16 priority groups, we need to write 3 to AIRCR[10:8], and the number written to the IPRx[7:4] register will determine the group; if we want 8 groups and 2 subgroups, we write 4 to AIRCR[10:8]; then the upper 3 bits of the IPRx[7:4] register will determine the group and the lowest bit of the upper nibble the subgroup, and so on.
But while enabling/disabling interrupts and setting priorities is convenient to do by setting the corresponding bits, it's more convenient to use the CMSIS functions for creating priority groups, although some may find it convenient to always use the CMSIS functions.
void NVIC_SetPriorityGrouping(uint32_t priority_grouping) // Set the priority groups/subgroups
void NVIC_EnableIRQ(IRQn_t IRQn) // Enable IRQn
void NVIC_DisableIRQ(IRQn_t IRQn) // Disable IRQn
uint32_t NVIC_GetPendingIRQ (IRQn_t IRQn) // Return the IRQn number if it is pending
void NVIC_SetPendingIRQ (IRQn_t IRQn) // Set IRQn pending
void NVIC_ClearPendingIRQ (IRQn_tIRQn) // Remove IRQn from the pending queue
uint32_t NVIC_GetActive (IRQn_t IRQn) // Returns the number of the currently active interrupt
void NVIC_SetPriority (IRQn_t IRQn, uint32_t priority) // Set the IRQn priority
uint32_t NVIC_GetPriority (IRQn_t IRQn) // Read the IRQn priority
void NVIC_SystemReset (void) //Reset
- the group priority determines whether one interrupt can preempt another, i.e. a group with priority 1 will easily preempt any interrupt from a group with priority 5
- the subgroup number determines which interrupt is executed first if two interrupts from the same group occur simultaneously; it should also be said that interrupts from the same group cannot preempt each other
- if two interrupts from the same group with the same subgroups occur simultaneously, the interrupt with the lower number is executed first

