Download as pdf or txt
Download as pdf or txt
You are on page 1of 26

Microprocessor

(for Embedded Systems)


07. Interrupt Management
Huỳnh Việt Thắng
Faculty of Electronic & Telecommunication Engineering
Danang University of Science and Technology
thanghv@dut.udn.vn
<Danang, Feb 2019>
Course contents
• Introduction
• Number systems and data formats (review)
• Microcomputer organization
• Introduction to ARM based processors
• Introduction to STM32 MCUs
• Interfacing with STM32 MCU peripherals
– GPIO Management
– Interrupt Management
• Applications
Main reference
1. Carmine Noviello, Mastering STM32, 2018
2. RM0383 Reference manualSTM32F411xC/E
advanced Arm®-based 32-bit MCUs
3. Datasheet STM32F411xC STM32F411xE
4. STM32F411VE Website:
https://www.st.com/content/st_com/en/products/microcontrollers/st
m32-32-bit-arm-cortex-mcus/stm32-high-performance-
mcus/stm32f4-series/stm32f411/stm32f411ve.html
Lecture Outline
• STM32 Interrupt Management overview
• Enabling Interrupts
• Examples/ Exercises
Interrupt Management Overview
(Tổng quan về quản lý ngắt)

• Các vi điều khiển cung cấp một tính năng gọi là ngắt
(Interrupt).

• Ngắt là một sự kiện không đồng bộ gây ra việc dừng


thực thi mã lệnh hiện tại trên cơ sở ưu tiên:
– ngắt càng quan trọng thì mức độ ưu tiên càng cao;
– ngắt có mức ưu tiên thấp hơn bị tạm dừng.

• Mã lệnh phục vụ ngắt được gọi là Chương trình con


phục vụ ngắt Interrupt Service Routine (ISR).
Interrupts & Multi-Programming
• Ngắt là một nguồn của lập trình đa nhiệm: phần cứng
biết về các ngắt và có trách nhiệm lưu bối cảnh thực thi
hiện tại (ngăn xếp, Bộ đếm chương trình hiện tại và một
số thứ khác) trước khi chuyển sang ISR.

• Ngắt được khai thác bởi Hệ điều hành thời gian thực
(Real Time Operating Systems - RTOS) để giới thiệu
khái niệm về các tác vụ (Task).

• Với sự trợ giúp của phần cứng có thể thực hiện một hệ
thống xử lý ưu tiên (pre-emptive system), cho phép
chuyển đổi giữa một số bối cảnh thực hiện mà không
làm mất đi luồng thực thi hiện tại.
Interrupts in ARM
• Interrupts can originate both by the hardware and the
software itself.
→ ngắt có thể được phát sinh bởi phần cứng và phần mềm

• ARM architecture distinguishes between the two types:


– interrupts (ngắt) originate by the hardware,
– exceptions (ngoại lệ) by the software (e.g., an access to invalid
memory location).

• In ARM terminology, an interrupt is a type of exception.


NVIC Controller
Vector Table in STM32
Enabling Interrupts
• When an STM32 MCU boots up, only Reset, NMI and Hard Fault
exceptions are enabled by default.

• The rest of exceptions and peripheral interrupts are disabled, and


they have to be enabled on request.

• To enable an IRQ, the CubeHAL provides the following function:

• The corresponding function to disable an IRQ:


STM32F411: EXTI main features
• The main features of the EXTI controller are the
following:
– independent trigger and mask on each interrupt/event
line (kích hoạt độc lập và cấm ngắt trên mỗi nguồn ngắt/sự
kiện)
– dedicated status bit for each interrupt line (có bit trạng
thái riêng biệt cho mỗi đường ngắt)
– generation of up to 23 software event/interrupt
requests (có đến 23 sự kiện/ngắt)
– detection of external signals with a pulse width lower
than the APB2 clock period (phát hiện tín hiệu ngoài với độ
rộng xung nhỏ hơn chu kỳ xung của bus APB2)

Tham khảo: Reference Manual STM32F411 (Chapter 10)


STM32F411 EXTI Block Diagram
• Tham khảo: Reference Manual STM32F411 (Chapter 10)
STM32F103: EXTI main features
• The main features of the EXTI controller are the
following:
– independent trigger and mask on each interrupt/event
line (kích hoạt độc lập và cấm ngắt trên mỗi nguồn ngắt/sự
kiện)
– dedicated status bit for each interrupt line (có bit trạng
thái riêng biệt cho mỗi đường ngắt)
– generation of up to 20 software event/interrupt
requests (có đến 20 sự kiện/ngắt)
– detection of external signals with a pulse width lower
than the APB2 clock period (phát hiện tín hiệu ngoài với độ
rộng xung nhỏ hơn chu kỳ xung của bus APB2)

Tham khảo: Reference Manual STM32F411 (Chapter 10)


STM32F103 EXTI Block Diagram
• Tham khảo: Reference Manual STM32F103 (Chapter 10)
STM32F411 EXTI Lines and NVIC
The relation between GPIO, EXTI lines and
corresponding ISR in an STM32F4 MCU
• Figure shows EXTI lines 0, 10 and 15 in an STM32F4 MCU.
• All Px0 pins are connected to EXTI0, all Px10 pins are connected
to EXTI10 and all Px15 pins are connected to EXTI15.
• However, EXTI lines 10 and 15 share the same IRQ inside the
NVIC (and hence are serviced by the same ISR).

• Only one PxY pin can be a


source of interrupt. For example,
we cannot define both PA0 and
PB0 as input interrupt pins.

• For EXTI lines sharing the same


IRQ inside the NVIC controller,
we have to code the
corresponding ISR so that we
must be able to discriminate
which lines generated the
interrupt.
STM32F103 EXTI Lines
• Tham khảo: Reference Manual STM32F103 (Chapter 10, Figure 21)
Stacking & Unstacking
Exercise 1 – Bài tập 1
• Use interrupts to toggle one LED (PD12 – PD15) every
time we press the user-programmable button (blue
button), which is connected to the PA0 pin in the
EDiscovery KIT STM32F411.
Cấu hình PA0 hoạt động như nguồn ngắt ngoài
Cấu hình GPIO mode: Rising edge trigger detection
Cấu hình cho phép ngắt EXTI0
Thuật toán
• Chương trình chính: liên tục đảo trạng thái của 2 LED PD14 và
PD15 (nháy đèn)
• Chương trình con phục vụ ngắt ngoài 0: (phím bấm tích cực
sườn lên): Khi có ngắt (tức người dùng bấm phím)
1. Tạo delay chống nẩy phím
2. Nếu có bấm phím tiếp tục bước 3, nếu không bấm phím đến bước 4.
3. Gọi CTC my_func() đảo trạng thái 2 LED PD12 và PD13
4. Kết thúc CTC phục vụ ngắt, quay về chương trình chính
Hướng dẫn học ở nhà
• Xem lại slide, xem lại video bài học và video demo, cũng
như các video hướng dẫn khác ở “HOCARM.ORG” và
“Học Điện tử IT” (truy cập ở hệ thống LMS Tuần 9)

• Tham khảo code chương trình đọc trạng thái phím bấm
(HAL_GPIO_ReadPin) và thử thực hành trên board
mạch F103 hoăc F411 (download ở LMS Tuần 9)

• Thử thực hiện chương trình đọc phím bấm bên ngoài
theo kiểu ngắt ngoài và thay đổi trạng thái chớp tắt của
đèn LED (ví dụ thay đổi thời gian delay)
Summary

You might also like