In this lesson, you will learn how to quantify the delay between a hardware event and the execution of your Interrupt Service Routine (ISR) using GPIO profiling and hardware timers.
The Problem: Latency and Jitter
In an ideal world, the moment an interrupt signal hits the processor, your ISR starts executing. In reality, you face two hurdles: latency (the fixed delay between the hardware signal and the first instruction of your ISR) and jitter (the variation in that delay over time). These are caused by factors like high-priority interrupts blocking your task, critical sections disabling interrupts, or wait states in the memory bus.
Measuring with "Instrumentation Toggling"
To visualize this, we use a technique called "pin-banging." You designate a spare GPIO pin to act as a diagnostic probe.
- The Signal Point: When the external hardware event occurs (e.g., a button press or sensor pulse), it triggers an interrupt.
- The Observation Point: Inside the very first line of your ISR, set a GPIO pin to high.
- Analysis: Using an oscilloscope or logic analyzer, measure the time elapsed between the external trigger pin and your diagnostic GPIO pin.
void EXTI0_IRQHandler(void) {
// 1. First line of ISR: Toggle debug pin
GPIO_PORT_DEBUG->BSRR = PIN_DEBUG;
// 2. Perform ISR logic
Handle_Sensor_Data();
// 3. Last line of ISR: Clear debug pin
GPIO_PORT_DEBUG->BSRR = (PIN_DEBUG << 16);
}
Analyzing the Results
If you connect your logic analyzer across the trigger pin and your debug pin, you can measure the "time-to-first-instruction." If this duration varies between different triggers, you have jitter.
- Fixed delay: Indicates inherent hardware architecture overhead (like context saving).
- Variable delay (Jitter): Suggests that your code is occasionally hitting long-running
__disable_irq()blocks or being preempted by higher-priority interrupts.
By comparing the captured pulse width to your known ISR code path, you can determine if your latency is caused by the processor's initial response time or by code sitting inside your ISR.
Key Takeaway: Use a GPIO toggle as the first line of your ISR and compare it against the external trigger pin on an oscilloscope to isolate and measure interrupt latency and jitter.