2019-02-22 11:05 AM
I'd like to use the CRC peripheral to check the validity of a code image in memory. I'm planning on generating a CRC during the build process, and adding the value to the end of the image when it's built. I'm using the Python function crcmod.mkCrcFun with the polynomial set to 0x14D11CDB7 and the initial value set to 0xFFFFFFFF. I understand that some descriptions of the poly value leave off the starting '1'. I'm assuming this is the case for theCRC peripheral description. mkCrcFun doesn't like the value 0x4D11CDB7.
So far the values generated by each method don't match. Can anyone tell me:
Thanks.
2019-02-22 11:20 AM
I could swear there were Python examples posted, the last forum transition garbled a lot of source postings. Here a C example. The ST solution processes 32-bit wide, the endian order is backward at a byte level, and one of the things that breaks web calculators
//****************************************************************************
//
// STM32TST - CRC32 Computation on the ST Micro STM32 Test Module
// Copyright (C) 2014-2018, All rights reserved
//
// sourcer32@gmail.com
//
//****************************************************************************
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned long uint32_t;
//****************************************************************************
uint32_t Crc32(uint32_t Crc, uint32_t Data)
{
int i;
Crc = Crc ^ Data;
for(i=0; i<32; i++)
if (Crc & 0x80000000)
Crc = (Crc << 1) ^ 0x04C11DB7; // Polynomial used in STM32
else
Crc = (Crc << 1);
return(Crc);
}
//****************************************************************************
uint32_t Crc32Fast(uint32_t Crc, uint32_t Data)
{
static const uint32_t CrcTable[16] = { // Nibble lookup table for 0x04C11DB7 polynomial
0x00000000,0x04C11DB7,0x09823B6E,0x0D4326D9,0x130476DC,0x17C56B6B,0x1A864DB2,0x1E475005,
0x2608EDB8,0x22C9F00F,0x2F8AD6D6,0x2B4BCB61,0x350C9B64,0x31CD86D3,0x3C8EA00A,0x384FBDBD };
Crc = Crc ^ Data; // Apply all 32-bits
// Process 32-bits, 4 at a time, or 8 rounds
Crc = (Crc << 4) ^ CrcTable[Crc >> 28]; // Assumes 32-bit reg, masking index to 4-bits
Crc = (Crc << 4) ^ CrcTable[Crc >> 28]; // 0x04C11DB7 Polynomial used in STM32
Crc = (Crc << 4) ^ CrcTable[Crc >> 28];
Crc = (Crc << 4) ^ CrcTable[Crc >> 28];
Crc = (Crc << 4) ^ CrcTable[Crc >> 28];
Crc = (Crc << 4) ^ CrcTable[Crc >> 28];
Crc = (Crc << 4) ^ CrcTable[Crc >> 28];
Crc = (Crc << 4) ^ CrcTable[Crc >> 28];
return(Crc);
}
//****************************************************************************
uint32_t Crc32FastBlock(uint32_t Crc, uint32_t Size, uint32_t *Data)
{
while(Size--)
Crc = Crc32Fast(Crc, *Data++); // 32-bit at a time
return(Crc);
}
//****************************************************************************
int main(int argc, char **argv)
{
uint32_t test[] = { 0x11223344, 0xB14257CC };
printf("%08X (0xB14257CC) Slow\n",Crc32(0xFFFFFFFF, 0x11223344)); // 0xB14257CC
printf("%08X (0xB14257CC) Fast\n",Crc32Fast(0xFFFFFFFF, 0x11223344)); // 0xB14257CC
printf("%08X (0x00000000) Block\n", Crc32FastBlock(0xFFFFFFFF, sizeof(test)/sizeof(uint32_t), test) );
// B14257CC (0xB14257CC) Slow
// B14257CC (0xB14257CC) Fast
// 00000000 (0x00000000) Block
return(0);
}
//****************************************************************************
2019-02-22 11:22 AM
>>Is the polynomial implementation for the CRC peripheral actually 0x14D11CDB7?
No