Framing the Context
I'm zeroing in on the importance of the embedded medical device domain. The IEC 62304 and FDA Class II/III regulations are the key here, and they're driving my focus on safety, reliability, and the need for deterministic behavior. Logging is also a high priority.
Outlining the Structure
Now, I'm defining the architecture of the boot-time diagnostics. I'll need a central RunDiagnostics function, a status code structure to manage results, and separate functions to test each hardware component. The emphasis is on deterministic execution within a defined timeframe, and logging capabilities.
Designing the Routine
I'm now structuring the diagnostics routine. The order of execution is crucial, so I've created a diagnostic flow. I'll test CPU registers first, followed by a RAM test, with a walking 1's pattern to strike a balance between speed and reliability. I'll then move on to flash memory verification with a CRC check.
Detailing the Steps
I'm now delving into the step-by-step implementation. My focus is on the order of execution and, of course, the diagnostic routines themselves. First, defining my error codes with an enum for readability. After that, I'll mock up a CRC function and create a walking-1s RAM test with original value preservation.
Developing the Code
I'm now integrating the testing routines into a cohesive boot-time diagnostic flow. I'm focusing on determinism and safety, ensuring the watchdog is active and refreshed at appropriate points. I'm adding __disable_irq() to prevent issues during memory tests, and reviewing against the IEC 62304 standards.
Synthesizing the Details
I'm now integrating all these aspects to refine the final diagnostic code structure. I'm focusing on providing more context regarding the standards, which is necessary. I'll include the mock CRC, RAM test logic, and the "Safe State" handler. I also need to make sure I note that this framework requires hardware-specific register tests.
This request requires adherence to high-integrity coding standards suitable for IEC 62304 (Medical Device Software) and IEC 61508 (Functional Safety).
In a medical context, a boot-time diagnostic (Power-On Self-Test or POST) must be deterministic, fail-safe, and isolated. If a hardware fault is detected, the device must enter a "Safe State" immediately and must not allow the main application to run.
Key Diagnostics Implemented
- ROM/Flash Integrity: Ensures bit-rot or corruption hasn't altered the program code (CRC check).
- RAM Integrity: Checks for stuck-at faults (bits stuck at 0 or 1) and addressing errors.
- Stack Check: Ensures the stack pointer is within valid bounds.
- Watchdog Logic: Ensures the diagnostic routine itself completes within a specific timeframe.
The C Implementation
Implementation Details & Safety Considerations
1. The "Safe State"
Note the System_Fatal_Error function. In a medical device (e.g., an infusion pump or ventilator), if RAM is bad, the device must effectively brick itself until serviced. It must never attempt to run the main loop with compromised memory. Logic often involves verifying that hardware relays (e.g., motor power) are de-energized here.
2. Volatile Keyword
In POST_Test_RAM_Integrity, pointers are declared volatile. This prevents the compiler from optimizing the code.
- Without volatile: The compiler sees you writing
0xAAAA then immediately checking if it equals 0xAAAA. It assumes this is always true and deletes the code check.
- With volatile: The compiler is forced to perform the actual read/write bus cycles.
3. Destructive vs. Non-Destructive Testing
The specific RAM test provided above is destructive. It wipes memory.
- Placement: This function must be called before the C Runtime (
crt0) initializes the .data and .bss sections. If called after, it will corrupt your global variables.
- Alternative: If run periodically during operation (Runtime Diagnostics), you must use a "Save-Test-Restore" pattern with interrupts strictly disabled.
4. Vendor Self-Test Libraries (STL)
For FDA Class II/III devices, hand-written C code for CPU register testing (checking the Program Counter, ALU, etc.) is often insufficient.
You should integrate Safety Test Libraries (STL) provided by silicon vendors (e.g., STMicroelectronics Class B STL or TI Hercules SafeTI). These libraries provide certified Assembly routines to check the silicon sanity that are difficult to write in C.
5. Watchdog Integration
The Watchdog Timer (WDT) is critical. If the CRC check takes 500ms but the generic bootloader timeout is 200ms, the device will enter a reset loop. The WDT is refreshed inside the Flash calculation loop, but carefully—ensuring that if the loop hangs, the WDT still fires.