This article is a continuation of a conversation on the concepts of concurrency and interrupts in microcontrollers.
In the previous article, we talked about GPIO interrupts. Here, we will briefly go over the role of GPIO in microcontrollers and cover the concept of interrupts in some familiar peripherals. This will set us up to conclude our series with a comparison of interrupts on three real-world MCUs.
The Role of GPIO for Microcontrollers
GPIO stands for general purpose input/output, sometimes called the digital I/O controller.
Microcontrollers have pins through which they interact with the outside world. These pins usually produce or take in electrical signals which the microcontroller processes.
The GPIO is a peripheral that is connected to some of the pins on the microcontroller.
GPIO for a Raspberry Pi. Image from SparkFun Electronics [CC-BY 2.0]
GPIO allows you to configure a pin as an input or an output. When the pin is configured as an input, the assumption is that something external is connected to the pin that determines what the electrical signal on the pin is. For example, if I connect the pin to a switch and connect that switch to a voltage source, I can use the switch to control whether the electrical signal on the pin is no voltage (0 V) or some voltage (whatever the voltage value of the source is). When the pin is configured as an input, the microcontroller is what determines what the signal on the pin is.
The pins are connected to registers in the microcontroller which allow you to read the value on or write the value you want to the pin in your program. The value on the pin is encoded as a bit (0 or 1). 0 usually means a very low voltage and 1 usually means a high voltage typically equivalent to the operating voltage on the microcontroller. For example, for the STM32L151C6 operating at 3.3 V, the input is read as 0 when the voltage is less than 0.99 V, and the input is read as 1 when the voltage is greater than 2.31 V.
Interrupts for Other Peripherals
Below, I will talk briefly about other common peripherals and the kinds of interrupts they might provide.
Most microcontrollers have at least one timer. Timers contain counters that work with the clock signal to provide a sense of elapsed time. The count of the timer, in addition to how faster the counter is counting, determines how much “real world” time has elapsed.
Most timers can be configured to generate an interrupt when they count to a particular value. There are usually interrupts for two cases.
The first case is overflow. If the counter is counting up, then this is when it reaches the maximum value. If it is counting down, then this is when it reaches the minimum value. Different microcontrollers are designed differently in the way the counter behaves after reaching overflow. Sometimes this behavior is configurable.
The second case is when it counts a user-specific value. Regardless of the event that caused the interrupt, there will usually be a flag that is set inside the timer, another that is set inside the interrupt controller, and an interrupt vector associated with interrupts from the timer. There may be more than one interrupt vector for the timer, each associated with a different event.
Most microcontrollers also have serial communication which allow them to send messages to other devices. Usually, there are data buffers (sometimes one for sending and a separate one for receiving).
There may be an interrupt to let the CPU know that data was just received in the receive buffer. There may be an interrupt to let the CPU know that the data, if put in the sending buffer, has been successfully sent so the buffer is ready to receive new data.
Some communication has signaling where a device must first determine who can send a message. In this case, there may be an interrupt to let the CPU know that the other device is ready to receive or that the other device intends to send data.
Some microcontrollers come equipped with an analog-to-digital converter that takes analog voltage that varies over a range and converts to a binary representation that the microcontroller can further process. The conversation process takes time, so there is typically an interrupt that tells the CPU that conversion is done after the CPU has initiated a conversion.
In the next article, we'll wrap up our discussion of interrupts for microcontrollers with a comparison of some familiar MCUs.