Unit testing patterns for microcontroller C code

后端 未结 4 1259
隐瞒了意图╮
隐瞒了意图╮ 2020-12-24 08:03

Although there are plenty of unit test frameworks that support C, I\'m a little stumped on how to write unit tests for micro controller code (PIC in my case, but I think the

4条回答
  •  无人及你
    2020-12-24 08:37

    Write mock versions of your register access functions/macros. Note that this assumes that your code uses a common set of register access functions, and not ad-hoc stuff like *(volatile int*)0xDEADBEEF = 0xBADF00D everywhere.

    Call your interrupt handlers directly from your test code (may be problematic on some architectures¹), a "software interrupt" if available, or from a timer interrupt handler if you need them to execute asynchronously. This may require wrapping your interrupt enable/disable code in functions/macros that you can mock up.

    ¹ 8051 comes to mind: at least with the Keil 8051 compiler, you can't call interrupt functions directly. This could be worked out with the C preprocessor though.

提交回复
热议问题