現在在做一款32位單片機軟件自檢程序,在做ROM的CRC校驗自檢時遇到問題,認證公司要求先把程序的CRC校驗值寫到FLASH固定地址里面(該地址不做CRC校驗),進入自檢的時候程序進行CRC計算,然后與放在FLASH固定地址的CRC比較來檢驗ROM區域的程序是否有異常變化,自己寫了程序應該是有BUG,有大佬提供一下例程不?
// 引用外部函數extern uint32_t iec60730_ram_march_bist_test(uint32_t addr);extern void iec60730_reg_r1_r4_bist_test(void);extern void iec60730_reg_r0_bist_test(void);extern void iec60730_reg_r8_r12_bist_test(void);extern void iec60730_reg_sp_bist_test(void);extern void iec60730_reg_spec_bist_test(void);extern void iec60730_reg_lr_bist_test(void);extern void iec60730_pc_test(void);extern CPUStatus STL_RunTimeCPUTest(void);void PC_Test_Func1(void)__attribute__((section(".ARM.__at_0x00003ff0"))); //地址以4為單位遞增,不然編譯不通過,函數聲明需要放在文件最上面聲明void PC_Test_Func2(void)__attribute__((section(".ARM.__at_0x0000400c"))); const uint32_t CRC_Result __attribute__((at(0x00007F8C))) = 0x00006EFF;uint32_t RamTestResult = 0;uint32_t CpuTestResult = 0;uint32_t crcResult = 0;/*flash self test*/static uint32_t _FLASH_CRC;// 棧底數據__IO uint32_t StackOverFlowPtrn[4] __attribute__((section("STACK_BOTTOM"), zero_init));// 每次校驗的數據量#define ROM_BIST_SIZE 8 /* 8 bytes every time */#define ROM_START (0x0)#define FLASH_ROM_START_ADDR (0x0)#define FLASH_ROM_SIZE (20480)//(32*1024)//#define FLASH_ROM_SIZE (32*1024)#define ROM_END ((uint32_t)(FLASH_ROM_SIZE - 1))#define ROM_SIZE ((uint32_t)ROM_END - (uint32_t)ROM_START + 1)static volatile uint32_t hw_crc,sw_crc;// bref: 測試flash// para:// note:static void ROM_Test(void){ stl_uint8_t* p_flash_start_addr = (stl_uint8_t*)FLASH_ROM_START_ADDR; iec_test_result.rom_test_result = IEC_TEST_SUCCESS; CRC_Init(); /*open crc apb clock*/ SYS_EnablePeripheralClk(SYS_CLK_CRC_MSK); /* use hardware CRC16 to calculate expected crc first, then verify if the CRC code calculated by software is same with expected crc */ hw_crc = IEC60730_HardwareCRC16Gen(p_flash_start_addr, ROM_SIZE); if(IEC60730_TEST_NORMAL != IEC60730_SoftwareCRC16Test(p_flash_start_addr, ROM_SIZE, hw_crc)){ iec_test_result.rom_test_result = IEC_TEST_FAIL; } // 使用過硬件CRC 完成一個完整的校驗 選喲重新初始化 CRC_Init(); /* use software CRC16 to calculate expected crc first, then verify if the CRC code calculated by hardware is same with expected crc */ sw_crc = IEC60730_SoftwareCRC16Gen(p_flash_start_addr, ROM_SIZE); if(IEC60730_TEST_NORMAL != IEC60730_HardwareCRC16Test(p_flash_start_addr,ROM_SIZE, sw_crc)){ iec_test_result.rom_test_result = IEC_TEST_FAIL; } // 記錄整個flash 區域的校驗值 _FLASH_CRC = hw_crc; crcResult = CRC_Result; if(crcResult == sw_crc)//_FLASH_CRC CRC_Result { iec_test_result.rom_test_result = IEC_TEST_SUCCESS; } else { iec_test_result.rom_test_result = IEC_TEST_FAIL; } }
|