首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 系统运维 >

STM32F10x 学习札记4(CRC计算单元 续)

2013-04-05 
STM32F10x 学习笔记4(CRC计算单元 续)上篇博客给出了 STM32F10X 系列单片机中CRC 单元的用法。还指出了这个

STM32F10x 学习笔记4(CRC计算单元 续)

上篇博客给出了 STM32F10X 系列单片机中CRC 单元的用法。还指出了这个CRC 单元计算的结果与常见的CRC32 算法得到的结果不相同。但是为什么不相同,是什么原因造成的却没有写出来。这里再补一篇,把这些都说清楚。

下面先给个crc32的计算函数,这个函数计算的结果与STM32F 单片机上硬件单元的计算结果相同。

uint32_t stm32crc32(uint32_t * message, unsigned int nWords, uint32_t remainder){    unsigned int offset;    unsigned char byte;    unsigned char *p = (unsigned char *)message;    //width_t remainder = INITIAL_REMAINDER;    /* Divide the message by the polynomial, a byte at a time. */    for( offset = 0; offset < nWords; offset++)    {        byte = (remainder >> (WIDTH - 8)) ^ p[3];        remainder = crcTable[byte] ^ (remainder << 8);        byte = (remainder >> (WIDTH - 8)) ^ p[2];        remainder = crcTable[byte] ^ (remainder << 8);        byte = (remainder >> (WIDTH - 8)) ^ p[1];        remainder = crcTable[byte] ^ (remainder << 8);        byte = (remainder >> (WIDTH - 8)) ^ p[0];        remainder = crcTable[byte] ^ (remainder << 8);        p += 4;    }    /* The final remainder is the CRC result. */    return (remainder);} /* crcCompute() */

大家可以验证这个函数的计算结果与STM32上的结果完全一样。

写到这里本该就结束了,不过我要多说一句,之所以要这么麻烦的调换字节序,都是小端(little endian)惹的祸。要是都采用大端格式就没这些麻烦的转换了。








热点排行