How It Works
Interrupt types
Switches, sensors and other external components can be connected to an MCU via tracks or cables. They are usually connected to I/O pins of the MCU. When a distance sensor triggers an interrupt, it changes the voltage at an I/O pin. When the level of an I/O pin changes from high to low or from low to high, this can trigger an interrupt. This type of interrupt is called an “attached interrupt” or pin interrupt, because the sensor is not part of the MCU but is connected (attached) to the MCU via an I/O pin.
The internal peripherals of an MCU, such as timers or communication units, can trigger “peripheral interrupts”. If, for example, a message comes in from the PC via the COM port, this triggers an interrupt. Because the communication interface is part of the MCU’s IC, this is an internal (peripheral) interrupt. With timers you can trigger interrupts at regular time intervals or after a specific time. The hardware of a timer is located in the IC of the MCU, so this is also a peripheral interrupt.
So a distance sensor can be read regularly with a peripheral timer interrupt. In an emergency, a collision sensor can trigger an attached interrupt at an I/O pin.
Interrupt sources
In an embedded system there are many events that can interrupt the main program. Each event is assigned to an interrupt source in the MCU. A source is, for example, an I/O pin, a timer or the COM port. An MCU generally has several I/O pins that can trigger an attached interrupt in the event of external events. There are often several timers and various potential communication channels. On the Arduino Uno, the following events trigger interrupts:
- Pin changes (changes low <-> high)
- Timer interrupts
- Watchdog timer
- Communication (SPI, I2C, USART)
- ADC conversion complete
- Memory ready for use (EEPROM or flash)
Only the top two interrupts are covered in the lecture. The same basic considerations apply to the others.
Activation
On the Arduino, interrupts are inactive by default. Interrupts must be enabled or activated in the source code before they take effect. If an interrupt is disabled or deactivated, the processor does not react to the corresponding event. Activating or deactivating individual interrupts is also called “masking”. Interrupts are masked via registers.
There is a global flag for activating or deactivating all interrupts. In addition, each interrupt has an individual flag via which it is activated. An interrupt is only active when its individual activation flag and the global interrupt enable flag are set.
All interrupts are activated globally with the command sei(). With the command cli() they are deactivated globally. A timer interrupt from timer 1, for example, is activated in the register TIMSK1. For such specific information we have to read the data sheet of the MCU; the names of the registers differ from MCU to MCU. More on this in the lecture.
Some interrupts cannot be deactivated. They are always active. These are called NMIs (non-maskable interrupts). They are intended for safety-critical functions. If, for example, we consider collision detection to be safety-critical, we should connect the distance sensor to an I/O pin with NMI. No matter what is happening in the code, this interrupt is always executed.
Interrupt service routine
The functions to be executed when an event occurs are located in the source code in “interrupt service routines” (ISRs). When an interrupt interrupts the main program, an ISR is executed. Each interrupt source is assigned an ISR that the programmer can fill with content.
The ISR for boiling eggs would say, for example, that you get up, take the eggs out of the pot and then sit down again. The ISR of the distance sensor for collision detection, for example, stops the robot’s drive. In the ISR of the timer, a distance measurement for optimising the direction of travel is carried out.
Interrupt request
When an interrupt is active and the corresponding event occurs, an interrupt request flag or interrupt flag is set in a register. Usually a bit assigned to the interrupt number then changes in a register from 0 to 1 or from 1 to 0. The processor then jumps to the corresponding ISR.
After the interrupt has been processed, the processor jumps back to the main program. In many processors, the interrupt request must be cleared explicitly in the source code. Otherwise the processor jumps straight back into the corresponding ISR. This is a classic programming error with interrupts. If an interrupt flag has to be cleared manually, you should do this in the ISR. The data sheet of an MCU states which interrupts this applies to.
Sequence of an interrupt
When an interrupt request is present, the MCU first saves the values of important processor registers. The MCU must ensure that the main program can continue without errors after the interrupt. Some processors also switch to other registers for processing an interrupt. The central processor registers then exist several times in parallel. The MCU freezes its state before processing the interrupt.
The MCU then executes the ISR. When it has finished, it restores the previous values of the processor registers. It continues processing the source code at the previous point. Here is the sequence again in brief:
1. Saving the current state: working registers and the source code line of the program are saved
2. Jump in the source code to the ISR
3. Processing the ISR
4. Restoring the state before the interrupt
5. Return to the program code
6. Continue processing the program code
Prioritisation
Several events can occur at the same time. Or a new event can occur while the ISR of another, older interrupt is being processed. Processors deal with this problem in different ways.
In many MCUs, interrupts can interrupt each other. This requires prioritising which interrupt may interrupt other interrupts and which interrupt has to wait until another ISR has finished executing. Higher-priority interrupts interrupt the ISRs of lower-priority interrupts. The priority of each interrupt is set in registers.
Saving and restoring processor registers becomes complicated when interrupts interrupt each other. Saving and restoring then have to work several times in a nested way. I do not go into this any further here.
Collision detection is the most important function of the robot. It should therefore get the highest priority. If the distance to an obstacle is measured a few microseconds later than planned, that does not matter. That is why the corresponding timer interrupt should get a lower priority. This ensures that switching off the drive in an emergency is never interrupted by a distance measurement for optimising the direction of travel.
With an ATMEGA328P in the Arduino Uno, only one interrupt is ever executed at a time. Interrupts cannot be interrupted. If a second interrupt occurs while a first interrupt is still running, the first interrupt is not interrupted. This method is inflexible but relatively safe. Prioritising interrupts is therefore not necessary with the ATMEGA328.
The processor in the ATMEGA328 automatically switches off interrupts while an ISR is being processed. On an ATMEGA328 you can also re-enable interrupts inside an ISR. Then you need prioritisation again. But especially for beginners, this is not a good idea. Better keep your hands off it.
Since we are working with an ATMEGA328, we have to live with the fact that, if in doubt, collision detection has to wait until the distance measurement interrupt has finished. For this reason we optimise the distance measurement interrupt for a short execution time.
With the ATMEGA328, note that no other interrupts are executed within an ISR. That is why none of the Arduino functions that themselves use interrupts work in ISRs. These include COM port communication such as serial.write() or serial.read() and wait commands such as delay().
Latency
The time between the occurrence of an event and the reaction of the MCU is called latency. Latency arises, among other things, from saving the current state. Complex arithmetic operations are often completed before an interrupt is executed. A good MCU has a low latency. The latency can generally be taken from the data sheet. On the Arduino Uno it amounts to a few microseconds. That is sufficient for our collision detection.
Variables in an ISR
Digression for advanced readers: imagine that the distance to the nearest obstacle is stored in the variable “Abstand” (distance). Your main program regularly reads the variable “Abstand” to determine the optimal direction of travel from it.
The main program has just loaded the distance value from the variable “Abstand” into a processor register. Before the processor can calculate the direction of travel with the distance value, a timer interrupt occurs. The distance value in the processor register is saved before the timer ISR is executed.
In the timer ISR, a new distance value is determined and written into the variable “Abstand”. After the ISR has been processed, the processor registers are restored. The processor register now contains the old distance value; the variable “Abstand” contains the new value. So the processor calculates the direction of travel with the wrong, old distance value.
What can we do? If variables are declared as “volatile”, the processor reads them anew from memory immediately before every use. So the processor updates the value of its processor register from memory, i.e. from the variable, immediately before every use. So let us declare the variable “Abstand” as volatile, because it is used in the interrupt and in the main program. The processor in our example then calculates the direction of travel with the new distance value determined in the timer interrupt.
Interrupt rules
The following rules should be followed when programming ISRs:
1. Little code: keep it short
2. Do not use (on the ATMEGA328) any functions that themselves need interrupts
3. Declare variables that are also used in ISRs as “volatile”
Further information: