2020 CoE2DX4 W7
2020 CoE2DX4 W7
Engineering
2DX4
Event Based Programming &
Interrupts
Week 7
mcmaster.ca |
Course Organization
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 23-7-23 | 3
Event vs.
Procedural:
Click the picture icon to insert a background picture.
Make sure to include an image description by rightInterrupt vs
clicking on your image and selecting ‘Edit Alt Text…’
Polling
mcmaster.ca | 23-7-23 4
Recall: Embedded Program Structure
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
is invoked by interrupt, instead of the programmer like 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 07/23/2023 | 10
Click the picture icon to insert a background picture.
Make sure to include an image description by rightInterrupt
clicking on your image and selecting ‘Edit Alt Text…’
Concept
For an embedded system
mcmaster.ca | 23-7-2311
Interrupts
Technical description
• 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
mcmaster.ca 23-7-23 | 13
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 23-7-23 | 14
What happens when an Interrupt is triggered?
This is a fundamental difference between and ISR and a normal function
mcmaster.ca |
Click the picture icon to insert a background picture.
Make sure to include an image description by right
clicking on your image and selecting ‘Edit Alt Text…’
NVIC
ARM Cortex-M4 Nested
Vectored Interrupt Controller
mcmaster.ca | 23-7-2316
Interrupt Vector Table
What can generate an interrupt and how does the microcontroller know where to find the ISRs?
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
NVIC_ST_CTRL_R, bit 1
❑ Arm bit (also called enable)
❖ Separate arm bit for each source
❖ Software initializes arm bit to 1 to allow interrupts
❑ Trigger flag Count flag is in NVIC_ST_CTRL_R, bit 16
❖ hardware sets trigger when it wishes to request an interrupt
❖ software clears the trigger to signify processing done
❑ Interrupt priority (0=max to 7=lowest) NVIC_PRI3_R, bits 31-29
❖ Higher priority interrupt can suspend a lower ISR
❖ Lower/equal priority interrupt will wait until higher is done
❑ Interrupt enable (I bit)
❖ Global interrupt enable bit, I, in PRIMASK register
PRIMASK, bit 0
❖ To enable (I=0), execute CPSIE I EnableInterrupts();
❖ To disable (I=1), execute CPSID I DisableInterrupts();
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
❖ SysTick does not need this enable step
7-22
7-23
ARM Cortex-M Interrupts
7-24
7-25
Click the picture icon to insert a background picture.
Make sure to include an image description by rightTriggering
clicking on your image and selecting ‘Edit Alt Text…’
Interrupts
- Inputs
- SysTick
- Periodic Timers
mcmaster.ca | 23-7-2326
SysTick Interrupt
SysTick can be configured to
generate periodic interrupts
• If configuring each individual I/O device
for interrupt handling is too much, then
a hybrid approach may be taken.
• On the left is a typical polling busy/wait
approach.
• On the right, we can instead use
SysTick to trigger a simple periodic
ISR that polls the next I/O resource.
• This has the advantage that
processing can occur between periodic
I/O check, instead of at the end of all
I/O polling. Also, if a specific I/O
needed to occur every 3xPeriod then
this would accomplish that objective.
mcmaster.ca 23-7-23 | 27
SysTick Interrupt
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
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
mcmaster.ca 23-7-23 | 37
Choose/configure the edge for triggering
mcmaster.ca |
Interrupt
Click the picture icon to insert a background picture.
Make sure to include an image description by rightRequirements
clicking on your image and selecting ‘Edit Alt Text…’
mcmaster.ca | 23-7-2339
Events Required to Create An Interrupt
• 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 |
Looking Forward
• Studio
o Communications
o Interrupt
• Lab W8
mcmaster.ca | 43