Day13 - GPIO IRQ Debounce with Workqueue¶
🎯 Objective¶
Implement a GPIO driver with:
- Interrupt handling
- Software debounce
- Event queue (ring buffer)
- Blocking read + poll support
🧩 Step 1 - Add Delayed Work¶
Init:
🧩 Step 2 - Modify ISR¶
🧩 Step 3 - Implement Debounce Handler¶
- Read GPIO
- Compare with last state
- Generate event
🧩 Step 4 - Add Ring Buffer¶
🧩 Step 5 - Wake Up Reader¶
🧩 Step 6 - Implement read()¶
Behavior:
- Blocking: wait event
- Non-blocking: return -EAGAIN
🧩 Step 7 - Implement poll()¶
🧪 Testing¶
1. Blocking read¶
Press button → should output event
2. Non-blocking¶
python3 -c "
import os
fd = os.open('/dev/mygpio', os.O_RDONLY | os.O_NONBLOCK)
try:
print(os.read(fd, 64))
except BlockingIOError:
print('No data')
"
3. poll()¶
import select, os
fd = os.open("/dev/mygpio", os.O_RDONLY)
p = select.poll()
p.register(fd, select.POLLIN)
while True:
events = p.poll()
print("EVENT:", os.read(fd, 64))
4. Stress test¶
- Rapid button press
- Ensure:
- no crash
- no stuck read
- events reasonable
✅ Expected Result¶
- No duplicate bounce events
- Stable press/release detection
- poll() wakes immediately
- read() blocks correctly