返回提示词目录第 1421 页

固件主机端测试改造与用例编写

拆分固件逻辑与硬件访问,构建主机测试和仿真实现,编写关键用例并明确实机验证范围。

完整提示词

<context>
The user is an embedded engineer whose firmware is only tested by flashing a board and watching it. Most firmware logic (state machines, protocol parsing, scaling, filtering, retry and timeout rules) does not need hardware at all, and can run as fast unit tests on the build machine, compiled with the host compiler. What blocks that is code that reads and writes registers directly, calls the vendor HAL from inside business logic, uses compiler-specific keywords, or depends on a real tick counter.

Common failures this prompt avoids: mocking every HAL call so tests only restate the implementation; trying to emulate the whole MCU; ignoring host-versus-target differences (int width, endianness, struct packing, `volatile`, alignment) so tests pass on the laptop but not on the chip; and claiming host tests prove timing, interrupts or electrical behaviour.

Toolchain: {{toolchain}}. Test framework: {{framework}}.
</context>

<task>
<firmware_code>
{{firmware_code}}
</firmware_code>

1. Sort the code into three layers: pure logic (no hardware access), hardware-facing glue (calls into the HAL, drivers, RTOS), and direct register access. Name the functions in each.
2. Propose seams with the least churn: a thin interface (struct of function pointers, link-time substitution of a `.c` file, or a small C++ interface) between logic and hardware. Prefer link-time substitution for C when the code must not change shape; prefer passing a dependency when the module is being touched anyway.
3. Design fakes, not just mocks: a fake GPIO or UART that records writes and lets the test inject reads, a fake register block as a plain struct the code points at in tests, a controllable fake clock or tick source, and a fake for any RTOS call the logic uses (queue, semaphore, delay). Use generated mocks (for example CMock) only to check that a call happened at the boundary.
4. Set up the host build: a separate target compiled with the host compiler, compile flags such as `-Wall -Wextra -Werror` and sanitizers (`-fsanitize=address,undefined`) on host, fixed-width types, a guard for compiler-specific attributes, and the one command to run the tests locally and in CI. Note host-versus-target differences to check for this code.
5. Write the first 4-8 tests for the most valuable logic: boundaries, invalid input, wraparound of counters and ticks, timeouts and error paths. Each test names the behaviour it proves.
6. List what still needs hardware-in-the-loop or on-target tests: interrupt timing and races, DMA, peripheral configuration, power modes, real sensor noise, watchdog and boot behaviour. Suggest the cheapest way to cover each (on-target test runner, logic analyser capture, HIL rig).

If the code is too partial to see where the hardware calls happen, ask for the header or HAL wrapper it uses and stop.
</task>

<constraints>
- Do not change the firmware's behaviour while adding seams; keep refactors minimal and show them as diffs or before-and-after snippets.
- No dynamic allocation added to target code for the sake of tests.
- Do not invent register names, addresses or HAL function signatures; use the ones in the code or mark them [CHECK DATASHEET].
- Never claim a host test proves timing, interrupt safety or electrical behaviour.
{{> guardrails/scope-discipline}}
</constraints>

<output_format>
## Testability assessment
Table: function or module | layer (logic, glue, register) | testable on host now? | blocker.
## Seams and fakes
For each seam: technique, the interface or substitution, and the fake's code.
## Test setup
Directory layout, build target, flags and the commands to run locally and in CI.
## First tests
The test file code, with one comment line per test on what it proves.
## Needs real hardware
Table: behaviour | why host tests cannot prove it | cheapest on-target check.
## Next steps
Up to five, in order.
</output_format>

可替换内容

  • {{toolchain}}
  • {{framework}}
  • {{firmware_code}}
  • {{> guardrails/scope-discipline}}
  • [CHECK DATASHEET]

分类与标签

编程开发、嵌入式固件、主机端测试、单元测试、硬件抽象、持续集成

前往 AI 图片创作