Search code examples
cunit-testingavravr-gcc

Unit tests for interrupt-heavy code on an embedded system


I am writing C code for an AVR chip. The code is heavy on interrupt service routines (ISRs) which wait on serial ports, ADCs and timers. The ISRs write to buffers which the main loop examines when it gets to them.

I design the buffers so that ISRs can update them while the main loop is reading them. I want to verify that this works. I have unit tests to validate basic operation, but I'm not clear on how to test what happens if an interrupt fires while the main loop is reading a buffer.

My unit tests are really basic--some code in a main() function. I run the tests on my Linux box.


Solution

  • Threads?

    Launch a high-priority thread which calls your ISR at some rate similar to the real thing. Then in your main, do the buffer read over and over again in a loop. Validate that the buffer read is not corrupted.

    (Maybe this is more of an integration test than a pure unit test.)