0% found this document useful (0 votes)
23 views28 pages

Lab 05

This document provides an introduction to interrupts in embedded programming, contrasting it with polling methods. It details the types of interrupts, the role of the Nested Vectored Interrupt Controller (NVIC), and how to program interrupts using registers. Additionally, it includes exercises for practical application using the NUCLEO-F401RE board.

Uploaded by

phanpham2k4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views28 pages

Lab 05

This document provides an introduction to interrupts in embedded programming, contrasting it with polling methods. It details the types of interrupts, the role of the Nested Vectored Interrupt Controller (NVIC), and how to program interrupts using registers. Additionally, it includes exercises for practical application using the NUCLEO-F401RE board.

Uploaded by

phanpham2k4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Lab 5: Introduction to interrupt

Introduction to embedded programming course


CONTENTS
1 Introduction to polling vs interrupt

2 Registers used when programming interrupt.

3 Practice interrupt programming using registers.


INTERRUPT VS POLLING
 Interrupt and polling

Polling is a method where the CPU An interrupt is a signal that pauses the
continuously checks or "polls" the status of normal execution of a program and
an input device or peripheral to see if an immediately directs the CPU to execute a
event has occurred. special piece of code performing a specific
task.
INTRODUCTION TO INTERRUPT

• Signals generated by the microcontroller's peripherals/ core of the microcontroller and


sent to the microprocessor. These signals are called Interrupt Request (IRQ).
• When an interrupt occurs, the microprocessor will temporarily suspend normal program
execution to execute a special program segment:
Interrupt Service Routine (ISR)
• Interrupt Request (IRQ) signals are sent to the NVIC block of the microprocessor.
TYPES OF INTERRUPTS

Internal interrupts External interrupts

Internal interrupts are generated by the core or External interrupts are generated by various
system-level functions of the microcontroller: on-chip peripherals: GPIO, ADC, UART, I2C,…
SysTick Timer, Faults and Exceptions,….

Software interrupts Hardware interrupts

A software interrupt is triggered by executing a A hardware interrupt is triggered by a hardware


specific instruction in software to intentionally event, such as the pressing of a button, the
generate an interrupt. Unlike hardware arrival of new data via a communication
interrupts, software interrupts are triggered by peripheral (like UART, I2C, SPI), based on
code rather than by an external hardware results of sensors,….
event.
NESTED VECTOR INTERRUPT CONTROLLERS
The NVIC (Nested Vectored Interrupt Controller) is a hardware component found in microcontrollers.
It manages the interrupts, allowing the CPU to respond to external and internal events in a prioritized
and efficient way.

Key features:
Vector Table: The NVIC uses a vector table to map interrupts to their respective Interrupt Service
Routines (ISRs). This table is stored in memory, and the NVIC uses it to determine which ISR to
execute when an interrupt occurs.
NESTED VECTOR INTERRUPT CONTROLLERS

Key features:
Interrupt Prioritization: The NVIC supports multiple priority levels, allowing you to assign
different priorities to different interrupts. Higher-priority interrupts can preempt (or interrupt)
lower-priority ones, which is known as nested interrupts.
Priorities are divided into two fields: preemption priority and subpriority, giving flexibility in
handling interrupts at various levels of urgency.
 The interrupt with the higher Preemption Priority will be executed first.
 Two interrupts with the same Preemption Priority level: The interrupt with the higher Sub
Priority will be executed first.
 Two interrupts with the same Preemption and Sub Priority levels:The interrupt that arrives
first is executed first.
NESTED VECTOR INTERRUPT CONTROLLERS

Key features:
 Enabling/Disabling Interrupts:
 The NVIC provides registers to enable or disable specific interrupts. This is
useful when you need to prevent certain interrupts from triggering temporarily.
 You can enable interrupts using NVIC_EnableIRQ(IRQn) and disable them with
NVIC_DisableIRQ(IRQn).
 Interrupt Masking:
 The NVIC can mask interrupts of lower priority levels to prevent them from
interrupting critical tasks.
 This is done using the priority mask register (PRIMASK), which blocks all
interrupts of lower priority than the current one.
VECTOR TABLE IN STM32F401RE

Type of priority:
• fixed: fixed
• seltable: can be set.
• Address: address of the interrupt in the
memory map
Note: The priority of the interrupt is arranged
from top to bottom.
VECTOR TABLE IN STM32F401RE
VECTOR TABLE IN STM32F401RE

Note: Names of Interrupt Service Routine must be the same as names defined here.
INTRODUCTION TO INTERRUPT

 How interrupts work?


• Execute the current instruction (Assembly command).
• Stop the main program
• Execute the instructions in the interrupt program
• Return to the next instruction in the program
INTRODUCTION TO INTERRUPT

Stacking: Save working context, register values ​into


SRAM, specifically stack memory area.

After the processor finishes executing the ISR


(Interrupt Service Routine)

Unstacking: Returns stored values ​to registers to Continue execution where it left
off in the main() function
restore context.
INTRODUCTION TO INTERRUPT

 Stacking
INTRODUCTION TO INTERRUPT

 Unstacking
INTRODUCTION TO INTERRUPT

 Nested interrupt

Note: When there are multiple interrupts, priority is important in deciding which interrupt will be serviced first.
INTRODUCTION TO INTERRUPT

When there is a logic level transition on the interrupt pin, an interrupt event will be generated.
Then the NVIC will check which line the interrupt is on to run into the corresponding interrupt program.

Rising: Enter an interrupt handler when encountering a rising edge


Falling: Enter an interrupt handler when encountering a falling edge
Rising + Falling: Enter an interrupt handler when encountering both rising and falling edges
PROGRAMING INTERRUPT USING REGISTERS

 External interrupt/event controller (EXTI):

 The external interrupt/event controller consists of up to 23 edge


detectors for generating event/interrupt requests. Each input line
can be independently configured to select the type (interrupt or
event) and the corresponding trigger event (rising or falling or
both).
 Line0 corresponding to EXTI0 contains interrupts for pins PA0,
PB0, PC0, PD0, PE0, PH0.
 Line3 corresponding to EXTI3 contains interrupts for pins PA3,
PB3, PC3, PD3, PE3, PH3
PROGRAMING INTERRUPT USING REGISTERS

 External interrupt/event controller:

 Each Line is only allowed to have 1 external interrupt pin.


For example: If PA0 is selected as an interrupt, other Ports are not
allowed to be selected as external interrupts.
 Configure the priority of the interrupts.
 Write the ISR program corresponding to the IRQ interrupt request.
PROGRAMING INTERRUPT USING REGISTERS

 EXTI_IMR – Interrupt mask register:

Set to allow interrupt requests on the corresponding Line.


PROGRAMING INTERRUPT USING REGISTERS

EXTI_RTSR – Rising trigger selection register:


Used to configure the rising edge as the interrupt trigger signal.
PROGRAMING INTERRUPT USING REGISTERS

EXTI_FTSR – Falling trigger selection register:


Used to configure the falling edge as the interrupt trigger signal.
PROGRAMING INTERRUPT USING REGISTERS
EXTI_PR – Pending register:
Interrupt Pending Register, when an interrupt request is issued on an interrupt line, the
corresponding bit of this register is turned on until the interrupt is processed.
PROGRAMING INTERRUPT USING REGISTERS

How to choose PAx/PBx/PCx/PDx/PEx/PHx pin for EXTIx line interrupt ?

Note: When using SYSCFG module, need to turn on clock for SYSCFG module
EXERCISES
Exercise: Write a program to toggle green led (on PA5) using user button (on PC13) on
NUCLEO – F401RE board
Structure of program:

#include "stm32f4xx_hal.h"

void main()
{
init_led();
init_button();
init_interrupt();

NVIC_EnableIRQ(EXTI15_10_IRQn); //Enable interrupt for EXTI Line from 10 to 15

while(1);
}

void init_led()
{
//Enable clock to port A
//Configure PA5 as output
//Configure pull up register for PA5
}
EXERCISES
Exercise: Write a program to toggle green led (on PA5) using user button (on PC13) on
NUCLEO – F401RE board
Structure of program:

void init_button()
{
//Enable clock to port C
//Configure PC13 as input

void init_interrupt()
{
//Enable System configuration controller clock (using RCC_APB2ENR register)
//Choose PC13 for EXTI13 line interrupt (using SYSCFG->EXTICR[3] register, equivalent to
SYSCFG_EXTICR4 in reference manual)
//Interrupt request from line 13 is not masked (using EXTI_IMR register)
//Falling edge trigger enable for line 13 (using EXTI_FTSR register)
}
EXERCISES
Exercise: Write a program to toggle green led (on PA5) using user button (on PC13) on
NUCLEO – F401RE board
Structure of program:

void EXTI15_10_IRQHandler()
{

uint32_t previous_state = (GPIOA->ODR >> 5) & 0x1; // Read previous of green led on PA5
if (previous_state == 0)
{
// Turn on led
}
else
{
// Turn off led
}
// Clear pending bit for EXTI13 (using EXTI_PR register)

}
DOCUMENTATIONS
- NUCLEO-F401RE_Schematic
- UM1724: User manual for STM32 Nucleo – 64 boards
- STM32F401RE datasheet
- RM0368: Reference manual for STM32F401RE

You might also like