【简介】
我们在之前的帖子中使用了S32K146 的硬件CRC 对整个1MB的flash 镜像进行了验证,我们将继续在此基础上对比下CRC 模块在硬件在计算性能及资源使用两方面比较相对软件CRC算法的优劣势。
本地使用的CRC 软件算法如下:
#include "crc32.h"
// 生成CRC32查表法使用的查找表
void crc32_init(uint32_t *table) {
uint32_t polynomial = 0xEDB88320;
for (uint32_t i = 0; i < 256; i++) {
uint32_t c = i;
for (int j = 0; j < 8; j++) {
c = (c >> 1) ^ ((c & 1) ? polynomial : 0);
}
table[i] = c;
}
}
// 计算CRC32校验值
uint32_t crc32_calculate(const uint8_t *data, size_t length, const uint32_t *table) {
uint32_t crc = 0xFFFFFFFF;
for (size_t i = 0; i < length; i++) {
uint8_t byte = data[i];
// 取当前crc高8位与数据异或作为查表索引
uint32_t index = (crc ^ byte) & 0xFF;
crc = (crc >> 8) ^ table[index];
}
return crc ^ 0xFFFFFFFF; // 最终异或
}
unsigned int image_check(char argc,char *argv[])
{
#if (IMAGE_CHECK_USED_CRC_HW)
test_io(1);
CRC_DRV_Deinit(INST_CRC_1);
CRC_DRV_Init(INST_CRC_1,&crc_1_Cfg0);
CRC_DRV_WriteData(INST_CRC_1,(uint8_t *)0,0xffffc);
test_io(0);
printf("crc32 %08x,IAR %08x \r\n",CRC_DRV_GetCrcResult(INST_CRC_1),*(((unsigned long *)0xffffc)));
#else
test_io(1);
// 初始化CRC表
static uint32_t crc_table[256];
crc32_init(crc_table);
// 计算CRC32
uint32_t result = crc32_calculate((const uint8_t *)0, 0xffffc, crc_table);
test_io(0);
printf("CRC32 %x \n",result);
#endif
return 0;
}【计算耗时对比】
CRC 模块校验1MB数据耗时289.392ms

软件CRC 校验1MB数据耗时316.3175ms

【资源使用对比】
硬件 CRC 检验算法对应map 使用资源如下:
54'532 bytes of readonly code memory 2'404 bytes of readonly data memory 18'373 bytes of readwrite data memor
软件CRC 校验算法使用资源如下:
54'600 bytes of readonly code memory 2'404 bytes of readonly data memory 19'397 bytes of readwrite data memory
相对硬件CRC 软件CRC 需要1K的RAM/ROM 存储快速计算的CRC表,相对会多耗费1K 的 RAM或者ROM 取决于表存在在哪里,本地使用的RAM存储相对readwrite 的数据会大一点。
总体来说CRC 模块相对软件CRC 速度会稍微快一点,但也不是很明显,耗费的资源要少一些,这对资源紧张的芯片来说算是一大优势。
我要赚赏金
