The user wants to develop a boot-time diagnostic routine in C for an embedded medical device. It’s crucial to think about safety and regulatory aspects since this is for medical usage. They didn’t specify a particular MCU or RTOS, so I should design a generic pattern. This could involve things like power-on self-tests, memory and CPU tests, error logging, and safe state considerations. The final output should balance technical detail with brevity while avoiding any false regulatory claims.Proposing diagnostic pattern
I'm thinking about a boot-time diagnostic routine for an embedded system. I can propose a pattern involving a diagnostic manager that runs at boot and tests various modules sequentially, incorporating timeouts and static test descriptors. I should outline the overall architecture in diag.h and diag.c, including tests for the CPU, RAM, EEPROM, and communication loops. It's crucial to implement clear fail handling to prevent proceeding to normal operations if a critical failure occurs. Additionally, I'll need to consider memory usage constraints and watchdog functionality during testing.Linking to configuration
I need to include a way to link the diagnostic routine to device-specific configurations. It’s essential to keep the code generic and avoid OS-specific includes, while also mentioning MISRA without strictly adhering to it. The aim is to create understandable code without complex macros but we can use some for illustration. The user might expect an example of the boot-time diagnostic routine with tests and results, so I'll emphasize a modular design for adding tests easily. I could provide a pseudo pipeline example to illustrate usage without overstepping on the details they specifically requested.Designing with detail
The user hasn't specified the level of detail, but given the domain, I should be thorough yet concise in the design. I'll include an enum for error codes as a bitmask for each failure, and mention logging to nonvolatile storage, at least as a stub. I need to implement diag_run_boot_tests, with sample tests like test_cpu_registers and test_flash_crc. Since we don't have specific hardware details, I'll create stub functions that return PASS for now. For a RAM test, I could suggest writing and verifying a simple destructive pattern since nothing else is running at boot. This strikes a balance between development and practicality.Designing the diagnostic code
I’m aiming to create a boot-time diagnostic routine in C, ensuring we maintain clarity and simplicity. I'll define a small memory region using macros like RAM_START_ADDR and RAM_END_ADDR in platform.h for illustration. The code should include a range of error codes as an enum and a structure to handle test results. Each test function will be stubs initially, returning a PASS state. I'll ensure the design accommodates both critical and non-critical tests while structuring the output to keep it straightforward and maintainable. We will also include a brief intro outlining the routine's design goals.Creating RAM and test patterns
I’m outlining a simple memory test that uses a walking pattern from RAM_TEST_START to RAM_TEST_END. Each address will be tested in 32-bit increments, writing 0x55555555 and 0xAAAAAAAA for verification, even destructively since it’s at boot time. For the flash CRC test, I’ll assume a function to compare against stored CRC. We'll also include stubs for ADC and communications tests. These will be minimal code snippets indicating where the developer can add their implementations. I’ll be sure to document key elements without over-explaining, ensuring clarity and conciseness in the code examples.Implementing diagnostic tests
I'm crafting a CPU test function to check basic operations with volatile variables, returning errors if computations don’t match expectations. For the RAM test, I’ll write patterns to the memory and verify them sequentially, also incorporating watchdog_kick() calls to prevent resets during long loops. The flash CRC test will compare calculated values with stored ones, while the ADC and communications tests will check the respective functionalities. Each test serves as a demonstration for expected behavior, and I will ensure all relevant headers and macros are defined appropriately in platform.h and platform.c.Implementing RAM test procedures
I'm writing a RAM test function that initializes a pattern in memory and verifies its correctness while incorporating watchdog_kick() regularly to prevent resets. It’s crucial to avoid writing to the stack during tests, so I’ll ensure the defined test region excludes it. I need to clarify in the comments that developers should adapt the test region to ensure safety. Additionally, I’ll assemble the overall diagnostic structure, including initializing tests and tracking summaries, while keeping the code concise and functional. This should ensure clarity and usability in the context of embedded applications.Implementing diagnostic tests
I'm working on a series of diagnostic tests for RAM, flash, ADC, and communication components. The RAM test fills a specified region with a pattern, checks it, and repeats the process with a different pattern. For flash, I compute and compare CRCs, while ADC and communication tests ensure functionality through self-tests. I’m careful to avoid circular dependencies in header files and note the importance of error handling for critical components. The goal is a robust system capable of handling faults effectively and ensuring safe operation.Refactoring diagnostic tests
I'm aiming to make my diagnostic tests clearer and more efficient. Currently, I see that error handling can be improved. Instead of using an error bit to track failures, I’m thinking about simplifying the structure by focusing on critical tests. If a critical test fails, I’ll break out of the loop early, which makes sense for safety.
I’ll refactor the code to remove unnecessary components and ensure proper handling of volatile pointers to avoid optimization issues. Finally, I’ll check for address alignment in my macros. Below is a modular example of a boot‑time diagnostic routine in C for an embedded medical device. It’s hardware‑agnostic but structured so you can plug in MCU‑specific code in platform.c.
This covers, at boot:
CPU sanity test (basic ALU/register check)
RAM test (pattern test over a defined safe region)
Flash CRC check (integrity of application image)
ADC self‑test (via HAL hook)
Communications self‑test (loopback via HAL hook)
Safe‑state entry if critical tests fail
Important: This is a template. For a real medical device you must:
Tailor the tests to your actual hardware.
Ensure RAM test region does not overlap stack/.data/.bss.
Validate, review, and verify per your safety and regulatory process.
diag.h – Diagnostics API
c
#ifndef DIAG_H
#define DIAG_H
#include <stdint.h>
#include <stdbool.h>
/* Error bitmask definitions */
#define DIAG_ERR_NONE 0u
#define DIAG_ERR_CPU (1u << 0)
#define DIAG_ERR_RAM (1u << 1)
#define DIAG_ERR_FLASH_CRC (1u << 2)
#define DIAG_ERR_ADC (1u << 3)
#define DIAG_ERR_COMMS (1u << 4)
/* Summary of diagnostics run at boot */
typedef struct
{
uint32_t error_mask; /* OR of DIAG_ERR_* bits */
uint8_t tests_run; /* How many tests executed */
uint8_t tests_failed; /* Count of failed tests */
} diag_result_summary_t;
/* Initialize diagnostic subsystem (clear state, etc.) */
void diag_init(void);
/* Run all boot-time diagnostics; may take milliseconds to seconds */
diag_result_summary_t diag_run_boot_tests(void);
#endif /* DIAG_H */
platform.h – Hardware Abstraction for Diagnostics
Adapt this to your MCU/memory map.
c
#ifndef PLATFORM_H
#define PLATFORM_H
#include <stdint.h>
#include <stdbool.h>
/*
* RAM test region:
* - Must NOT overlap stack, .data, .bss, or any memory used during the test.
* - Typically a reserved region defined in linker script.
* - Addresses below are EXAMPLES only.
*/
#define RAM_TEST_START 0x20000000u /* Example RAM start address */
#define RAM_TEST_END 0x20001000u /* Example: test first 4 KB RAM */
/*
* Flash test region:
* - Application image address and length for CRC check.
* - APP_FLASH_CRC_ADDRESS holds the expected CRC (stored at build time).
*/
#define APP_FLASH_START 0x08004000u /* Example app start in flash */
#define APP_FLASH_LENGTH 0x0003C000u /* Example app size */
#define APP_FLASH_CRC_ADDRESS 0x08003FFCu /* Example CRC storage address */
/* Minimal hardware init needed before diagnostics (clocks, basic IO, watchdog) */
void platform_minimal_hw_init(void);
/* Visually/audibly/logically indicate an error (LED, buzzer, log, etc.) */
void platform_indicate_error(uint32_t error_mask);
/* Put the system into a safe state (disable outputs/actuators, etc.) */
void platform_enter_safe_state(uint32_t error_mask);
/* Compute CRC of flash region [start_addr, start_addr + length) */
uint32_t platform_flash_crc32(uint32_t start_addr, uint32_t length);
/* ADC self-test: internal reference, short to ground, etc. */
bool platform_adc_selftest(void);
/* Communications self-test: loopback or known-pattern check */
bool platform_comms_loopback_selftest(void);
/* Kick/refresh the watchdog timer */
void watchdog_kick(void);
#endif /* PLATFORM_H */
diag.c – Boot-Time Diagnostic Implementation
c
platform.c – Example Stubs (Fill in for Your Hardware)
c
#include "platform.h"
/* Example stubs: replace with real hardware code */
void platform_minimal_hw_init(void)
{
/* Configure clocks, GPIO for LEDs/UART, enable watchdog, etc. */
}
void platform_indicate_error(uint32_t error_mask)
{
(void)error_mask;
/* Example:
* - Turn on a red LED
* - Output error code over UART
* - Log into non-volatile memory
*/
}
void platform_enter_safe_state(uint32_t error_mask)
{
(void)error_mask;
/* Example safe state:
* - Disable all actuators, PWM outputs
* - Stop any therapy delivery
* - Optionally enter low-power mode and wait for reset
*/
for (;;)
{
/* In a real device, WDT or external supervisor will reset */
}
}
uint32_t platform_flash_crc32(uint32_t start_addr, uint32_t length)
{
(void)start_addr;
(void)length;
/* Placeholder implementation:
* In a real system:
* - Use HW CRC peripheral or software CRC32
* - Avoid reading outside valid flash region
*/
/* Return the stored CRC so diag_test_flash_crc passes for now. */
return *(volatile const uint32_t *)APP_FLASH_CRC_ADDRESS;
}
bool platform_adc_selftest(void)
{
/* Example:
* - Configure ADC to measure internal reference voltage
* - Check result within expected range
*/
return true; /* Stub: always pass */
}
bool platform_comms_loopback_selftest(void)
{
/* Example:
* - Configure UART/SPI with internal/external loopback
* - Send pattern, read back, compare
*/
return true; /* Stub: always pass */
}
void watchdog_kick(void)
{
/* Example:
* - Write key to watchdog refresh register
*/
}
main.c – Using the Boot-Time Diagnostics
c
#include "platform.h"
#include "diag.h"
int main(void)
{
diag_result_summary_t summary;
/* Initialize just enough hardware for diagnostics */
platform_minimal_hw_init();
/* Run boot-time diagnostics */
diag_init();
summary = diag_run_boot_tests();
/* Treat CPU, RAM, Flash CRC, ADC as critical */
if ((summary.error_mask &
(DIAG_ERR_CPU | DIAG_ERR_RAM | DIAG_ERR_FLASH_CRC | DIAG_ERR_ADC)) != 0u)
{
/* Signal error (LED, buzzer, log, etc.) and go to safe state */
platform_indicate_error(summary.error_mask);
platform_enter_safe_state(summary.error_mask);
/* Should not return; just in case, trap here */
for (;;)
{
}
}
/* Non-critical diagnostics (e.g., communications) may be handled separately */
if ((summary.error_mask & DIAG_ERR_COMMS) != 0u)
{
/* Example: flag degraded mode, log for maintenance, etc. */
}
/* Continue to normal application start-up */
// app_init();
// app_main_loop();
for (;;)
{
}
}
How to Adapt This for Your Device
RAM test region: Adjust RAM_TEST_START / RAM_TEST_END so they point to a reserved RAM area not used for stack or globals. For a full‑RAM test, you generally need a linker‑script and stack relocation strategy.
Flash CRC:
Ensure your build process computes a CRC of the application image and writes it to APP_FLASH_CRC_ADDRESS.
Implement platform_flash_crc32() using either a hardware CRC unit or a software CRC32 routine.