stm32 hardware crc in c

Author Avatar
aghost 02月 10,2020
  • 在其它设备中阅读本文章
uint32_t cal_crc(uint32_t *ptr, int len) {
    uint32_t dwPolynomial = 0x04c11db7;
    uint32_t xbit;
    uint32_t data;
    uint32_t CRC = 0xFFFFFFFF;    // init  0xFFFFFFFF
    while (len--) {
        xbit = 1 << 31;
        data = *ptr++;
        for (int bits = 0; bits < 32; bits++) {
            if (CRC & 0x80000000) {
                CRC <<= 1;
                CRC ^= dwPolynomial;
            } else
                CRC <<= 1;
            if (data & xbit)
                CRC ^= dwPolynomial;
            xbit >>= 1;
        }
    }
    return CRC;
}