///* // * Copyright (c) 2006-2021, RT-Thread Development Team // * // * SPDX-License-Identifier: Apache-2.0 // * // * Change Logs: // * Date Author Notes // * 2026-03-12 RuoFeng the first version // */ //#include //#include // ///* 测试函数声明 */ //static void test_hardfault_div0(void); //static void test_hardfault_jump0(void); //static void test_hardfault_stack_overflow(void); //static void test_hardfault_mem_access(void); // ///*============================================================================= // * 硬错误触发测试命令 // *============================================================================*/ // ///** // * @brief 触发除零错误 // * // * 通过除零操作引发 UsageFault // */ //static void test_hardfault_div0(void) //{ // rt_kprintf("\n=== 触发除零错误 ===\n"); // rt_kprintf("正在执行除零操作...\n"); // // volatile int a = 1; // volatile int b = 0; // volatile int c; // // __asm volatile( // "MOV R0, %0\n" // "MOV R1, %1\n" // "UDIV R2, R0, R1\n" /* R0 / R1,除零操作 */ // : : "r"(a), "r"(b) : "r0", "r1", "r2" // ); // // c = a / b; /* 后备的C语言除零,如果汇编没触发的话 */ // rt_kprintf("结果: %d\n", c); //} //MSH_CMD_EXPORT(test_hardfault_div0, Trigger divide by zero hard fault); // ///** // * @brief 跳转到零地址执行 // * // * 通过跳转到0地址引发 MemManageFault 或 BusFault // */ //static void test_hardfault_jump0(void) //{ // rt_kprintf("\n=== 跳转到零地址 ===\n"); // rt_kprintf("正在跳转到0x00000000地址...\n"); // // void (*func)(void) = (void(*)(void))0; // func(); /* 执行空指针函数 */ //} //MSH_CMD_EXPORT(test_hardfault_jump0, Jump to address 0 to trigger hard fault); // ///** // * @brief 触发栈溢出 // * // * 通过无限递归导致栈溢出 // */ //static void recursive_func(int depth) //{ // char buffer[256]; /* 在栈上分配内存 */ // rt_kprintf("递归深度: %d, 栈地址: %p\n", depth, buffer); // // /* 无限递归 */ // recursive_func(depth + 1); //} // //static void test_hardfault_stack_overflow(void) //{ // rt_kprintf("\n=== 触发栈溢出 ===\n"); // rt_kprintf("开始无限递归...\n"); // // recursive_func(1); //} //MSH_CMD_EXPORT(recursive_func, Trigger stack overflow hard fault); // ///** // * @brief 非法内存访问 // * // * 访问未映射的内存地址 // */ //static void test_hardfault_mem_access(void) //{ // rt_kprintf("\n=== 触发非法内存访问 ===\n"); // // /* 尝试写入一个通常不可访问的地址 */ // volatile uint32_t *bad_addr = (uint32_t*)0xE0000000; /* Cortex-M 系统控制空间外区域 */ // rt_kprintf("正在向地址 0xE0000000 写入数据...\n"); // // *bad_addr = 0xDEADBEEF; // // /* 如果上面没触发,尝试另一个地址 */ // bad_addr = (uint32_t*)0xFFFFFFFF; // rt_kprintf("正在向地址 0xFFFFFFFF 写入数据...\n"); // // *bad_addr = 0xDEADBEEF; //} //MSH_CMD_EXPORT(test_hardfault_mem_access, Trigger invalid memory access hard fault); // ///** // * @brief 触发硬错误综合测试 // * // * 依次执行所有硬错误测试,但每个测试都会导致系统复位 // * 所以这个函数实际上只会执行第一个测试 // */ //static void test_hardfault_all(int argc, char **argv) //{ // rt_kprintf("\n========================================\n"); // rt_kprintf(" 硬错误综合测试 - 将依次触发多种错误\n"); // rt_kprintf("========================================\n"); // // if (argc > 1) { // int type = atoi(argv[1]); // switch (type) { // case 1: // test_hardfault_div0(); // break; // case 2: // test_hardfault_jump0(); // break; // case 3: // test_hardfault_stack_overflow(); // break; // case 4: // test_hardfault_mem_access(); // break; // default: // test_hardfault_div0(); // break; // } // } else { // /* 默认触发除零错误 */ // test_hardfault_div0(); // } //} //MSH_CMD_EXPORT(test_hardfault_all, Test hard fault: test_hf [1-4] (1=div0,2=jump0,3=stack,4=mem)); // //