Skip to main content

Check Your Understanding 9.1.4 Check Your Understanding

1.

A program prints a counter, then calls delay_ms(1000), then reads a button pin and resets the counter if the button is down, and repeats. A user presses and releases the button quickly, entirely within one of the delays. What does the program do?
  • Nothing: the counter keeps counting. By the time the program reads the pin, the button is back up, so the press is never seen.
  • Correct. Polling reads the pin’s level at one instant per trip around the loop. A press that starts and ends between two of those instants leaves no trace for the program to find. This is the problem an interrupt solves: the hardware detects the change when it happens, not when the program next asks.
  • The counter resets on the next trip through the loop, because the pin remembers that it was pulled LOW.
  • The input data register (IDR) reports what the pin’s voltage is right now; it has no memory of what the level used to be. Once the button is released and the pull-up has brought the pin back HIGH, reading IDR gives HIGH. A pending flag that remembers an edge is exactly what the EXTI adds, and that is this chapter’s subject.
  • The counter resets, because delay_ms() checks for pin changes while it waits.
  • delay_ms() only counts time; it knows nothing about any pin. Nothing in the program is watching the button while the delay runs.
  • The program cannot compile: reading a pin after a delay is a race condition the compiler rejects.
  • The compiler has no idea what the pin means or how fast a person presses a button; this compiles and runs perfectly. The behavior is correct C and a disappointing user experience, which is a combination worth recognizing.

2.

Why do GPIO pins connect to the EXTI rather than each having its own line into the NVIC, the way a timer or the ADC does?
  • There are far too many pins. Giving each one its own NVIC line and vector-table entry would cost dozens of both, nearly all unused in any given program.
  • Correct. One peripheral gets one line because there are only a handful of peripherals. Five ports of up to sixteen pins is a different scale entirely, so the pins are condensed onto a few shared lines first. The price of that condensing shows up in class, in the handler you write.
  • GPIO pins are slower than peripherals, so their interrupts must be buffered before the NVIC can accept them.
  • Speed is not the issue: an edge on a pin is if anything faster than a peripheral’s status flag. The issue is how many pins there are relative to how many interrupt lines the NVIC has.
  • The NVIC cannot detect edges, only levels, so the EXTI converts a level into an interrupt.
  • The EXTI does detect edges, and that is genuinely one of its jobs, but it is not the reason pins are routed through it rather than being wired individually. Even if the NVIC could detect edges itself, there would still not be room for one line per pin.
  • Pins configured as outputs would otherwise interrupt the CPU every time software wrote to them.
  • Nothing about writing an output pin requests an interrupt. The routing question is about the number of inputs the interrupt controller would have to accommodate.

3.

Compared with the TIM14_IRQHandler you wrote last week, what changes about the rules for an ISR when the interrupt comes from a pin instead of a timer?
  • Nothing. The name is different and it clears a different flag, but it still takes no arguments, returns nothing, must be named exactly, gets in and out fast, and shares state with main() only through a volatile variable.
  • Correct, and it is the reason this chapter is short. The mechanism does not know or care what raised the request: the CPU saves state, jumps to the address in the vector table, runs your function, and restores. Only the source changes.
  • A pin ISR may take the pin number as an argument, so that one handler can serve several pins.
  • Hardware is the caller, and hardware has no way to pass arguments. That constraint is unchanged. One handler does serve several pins on this chip, which is a real complication, but it learns which pin fired by reading a register, not by receiving a parameter.
  • A pin ISR does not need to clear anything, because releasing the button clears the request automatically.
  • Every interrupt source in this course latches a flag that software must clear, and a pin is no exception: the flag records that an edge happened, so the pin’s present level cannot clear it. An ISR that returns without clearing is re-entered immediately.
  • Shared variables no longer need volatile, because a pin interrupt is asynchronous rather than periodic.
  • It is the other way around, if anything. volatile is needed because no call to the ISR appears anywhere in the program, so the compiler has no reason to believe the variable can change (Subsection 8.3.6). That is true of every ISR, whatever wakes it.