2020 CoE2DX4 W7
2020 CoE2DX4 W7
Engineering
2DX4
Event Based Programming &
Interrupts
Week 7
mcmaster.ca |
Introduction Observe
Week 3:
Week 0: Week 1: Week 2:
Timing, I/O, &
Embedded Systems & Signals Around Us Transduction & Analog
Structured Program
Architecture (Digital & Analog) Signal Acquisition
Organization
Reason
Course Organization Week 4:
Week 5: Week 6:
Act Overview
Week 7:
Week 8: Week A & B:
Week 9:
Event Based
Communication Embedded Systems &
Programming & Data Visualization
Protocols Architecture + Overview
Interrupts
mcmaster.ca | 2
o Event vs. Procedural: Interrupt vs Polling
• Interrupt Concept
• ARM Cortex-M4 Nested Vectored Interrupt
Controller (NVIC)
• Triggering Interrupts
Lecture Objectives
o Inputs
o SysTick
mcmaster.ca |
Embedded Program Structure
Is this a polling
method or interrupt
driven? How do I tell?
mcmaster.ca |
Embedded Program Structure
mcmaster.ca |
Embedded Program Structure
• An interrupt driven program typically has a very different
structure.
• Often the main body of the program seems to be
missing. However, by looking at the environment set up
we can determine that interrupts have been enabled.
• All logic has been implemented as event driven and so it
is common to have an infinite loop when the processor is
waiting for an event (or even better to be in a low power
“wait” state).
• When an interrupt occurs, it is handled by an Interrupt
Service Routine (ISR). This will look like a function, but
An ISR looks like
is invoked by interrupt, instead of the programmer a function.
explicitly calling the function.
This is an event-based
programming paradigm that
allows the programmer to service
mcmaster.ca I/O resources only when needed. |
What is an interrupt?
• A special event that requires the CPU to stop normal program execution and perform some service related to
the event.
• Examples of interrupts include I/O completion, timer time-out, illegal opcodes, arithmetic overflow, divide-by-0,
etc.
Classroom analogy:
Interrupt – Instructor continues to teach and student raises hand when they
need attention
mcmaster.ca |
What’s Wrong With Procedural/Polling?
• Nothing is “wrong” with procedural programming or polling. However, it can present a challenge when timing
is critical in the embedded application.
• For example, when using ADC you must meet Nyquist restrictions.
o Polling can introduce I/O time delays that are variable, causing failed synchronization.
o Interrupts can be prioritized to ensure timing is met.
• Generally I/O is slower than software
mcmaster.ca 3/11/21 | 10
Interrupt
Concept
For an embedded system
• An interrupt is the automatic transfer of software execution in response to a hardware event that is
asynchronous with the current software execution. This occurrence is called a trigger or an event.
• The hardware event can either be a busy to ready transition in an external I/O device (like the UART
input/output) or an internal event (like bus fault, memory fault, or a periodic timer).
• When the hardware needs service, signified by a busy to ready state transition, it will request an interrupt by
setting its trigger flag.
• A thread is defined as the path of action of software as it executes. The execution of the interrupt service
routine (ISR) is called a background thread. This thread is created by the hardware interrupt request and is
killed when the interrupt service routine returns from interrupt
• A new thread is created for each interrupt request. It is important to consider each individual request as a
separate thread because (just like functions) local variables and registers used in the interrupt service routine
are unique and separate from one interrupt event to the next interrupt.
• In a multi-threaded system, we consider the threads as cooperating to perform an overall task.
mcmaster.ca |
Interrupts (cont’d)
Technical description
• Event Queuing
o If a trigger flag is set, but the interrupts are disabled (I=1), the interrupt level is not high enough, or the flag
is disarmed, the request is not dismissed. Rather the request is queued, postponed until a later time, when
the system deems it convenient to handle the requests.
o We will pay special attention to these enable/disable software actions. In other words, once the trigger flag
is set, under most cases it remains set until the software clears it.
• Completing an Interrupt
o Clear the interrupt trigger/event flag. This is called an acknowledgement.
• ISR
o The software module that is executed when the hardware requests an interrupt.
o Except for the SysTick interrupt, the ISR must clear the trigger flag that caused the interrupt
(acknowledge).
mcmaster.ca |
NVIC
ARM Cortex-M4 Nested
Vectored Interrupt Controller
mcmaster.ca |
mcmaster.ca |
Interrupt Vector Table (cont’d)
What can generate an interrupt and how does the microcontroller know where to find the ISRs?
mcmaster.ca |
mcmaster.ca |
ARM Cortex-M Interrupts
7-21
NVIC Interrupt Enable Registers
❑Enable interrupt
❖A single enable bit for each device
❖NVIC_EN0_R for IRQ numbers 0 to 31
❖NVIC_EN1_R for IRQ numbers 32 to 47
❖ For SysTick the interrupt enable is in
NVIC_ST_CTRL_R
7-22
7-23
ARM Cortex-M Interrupts
7-24
7-25
Triggering
Interrupts
- Inputs
- SysTick
- Periodic Timers
mcmaster.ca |
SysTick Interrupt
• SysTick has a 24-bit counter that decrements at the bus clock frequency.
• Let fBUS be the frequency of the bus clock, and let n be the value of the RELOAD register.
• The frequency of the periodic interrupt will be fBUS/(n+1).
mcmaster.ca |
SysTick Interrupt
• First, we clear the ENABLE bit to turn off SysTick during initialization.
• Second, we set the RELOAD register.
• Third, we write any value to NVIC_ST_CURRENT_R to clear the counter.
• Lastly, we write the desired mode to the control register, NVIC_ST_CTRL_R. We must set CLK_SRC=1.
mcmaster.ca |
SysTick Interrupt
mcmaster.ca |
SysTick Interrupt
mcmaster.ca |
SysTick Interrupt
mcmaster.ca |
mcmaster.ca |
GPIO Interrupt
• Synchronizing software to hardware events requires the software to recognize when the hardware changes
states from busy to done.
• Many times the busy to done state transition is signified by a rising (or falling) edge on a status signal in the
hardware.
mcmaster.ca |
GPIO Interrupt
• For these situations, we connect this status signal to an input of the microcontroller, and we use edge-
triggered interfacing to configure the interface to set a flag on the rising (or falling) edge of the input.
• Using edge-triggered interfacing allows the software to respond quickly to changes in the external world.
mcmaster.ca |
GPIO Interrupt
As shown earlier, GPIO can trigger an interrupt. Triggers can be edge sensitive or level sensitive.
When configuring the port for GPIO, we can also set the Port Mode.
Micro
PC1
PC2
or
mcmaster.ca | 39
Timer
• Eight General-Purpose Timer Modules (GPTM), each of which
provides two 16-bit timers/counters.
o As a single 32-bit timer
mcmaster.ca | 40
mcmaster.ca | 41
Interrupt
Requirements
• Configuring an Interrupt
o Arm the I/O device
o Enable NVIC
o Enable Global interrupts
o Set interrupt level
o Write the ISR to handle the interrupt Trigger
mcmaster.ca |
Discuss:
SoftBank Group (Boston Dynamics) Do you think a system like this is
procedural or even-based?
Why?
mcmaster.ca |
Agility Robotics (Cassie)
http://spectrum.ieee.org/automaton/robotics/industrial-robots/agility-robotics-
introduces-cassie-a-dynamic-and-talented-robot-delivery-ostrich
mcmaster.ca |