2024-03-11 07:13 AM
Hi , have somebody link to exe or online or linux utility with same result as HAL CRC calculate?
2024-03-11 07:19 AM
srecord
JW
2024-03-11 07:22 AM
Thanks srec line example for bin file ?
2024-03-11 07:35 AM
https://community.st.com/t5/stm32-mcus-products/stm32f4-crc32-algorithm-headache/m-p/371428
https://community.st.com/t5/stm32-mcus-security/crc-computation/m-p/536190/page/2
// STM32 CRC32 Test App - sourcer32@gmail.com
// Copyright (C) 2014
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
//****************************************************************************
DWORD Crc32(DWORD Crc, DWORD 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);
}
//****************************************************************************
DWORD Crc32Block(DWORD Crc, DWORD Size, DWORD *Buffer) // 32-bit units
{
while(Size--)
Crc = Crc32(Crc, *Buffer++);
return(Crc);
}
//****************************************************************************
DWORD Crc32Fast(DWORD Crc, DWORD Data)
{
static const DWORD 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);
}
//****************************************************************************
DWORD Crc32FastBlock(DWORD Crc, DWORD Size, DWORD *Buffer) // 32-bit units
{
while(Size--)
Crc = Crc32Fast(Crc, *Buffer++);
return(Crc);
}
//****************************************************************************
void test(void)
{
BYTE vector[12] = { 0x02,0x07,0x02,0x00,0x18,0x8A,0xD0,0x23,0x25,0x2B,0x09,0x00 }; // ACD7E298
DWORD Crc;
int i;
for(i=0; i<sizeof(vector); i++)
printf("%02X ",vector[i]);
putchar('\n');
Crc = 0xFFFFFFFF; // Initial state
for(i=0; i<sizeof(vector); i+=4)
{
Crc = Crc32Fast(Crc, *((DWORD *)&vector[i]) ); // 4-bytes at a time
}
printf("%08X %08X test",Crc, Crc32FastBlock(0xFFFFFFFF, sizeof(vector)/4, (void *)vector));
}
//****************************************************************************
void TestFile(char *Filename)
{
FILE *f;
DWORD Size;
BYTE *Buffer;
f = fopen (Filename, "rb");
if (f)
{
fseek(f, 0, SEEK_END);
Size = ftell (f);
fseek(f, 0, SEEK_SET);
if (Size & 3)
printf("WARNING: File must be multiple of 4 bytes (32-bit) for valid results\n");
Buffer = malloc(Size);
fread(Buffer, Size, 1, f);
fclose(f);
printf("crc=%08X Slow\n", Crc32Block(0xFFFFFFFF, Size >> 2, (void *)Buffer));
printf("crc=%08X Fast", Crc32FastBlock(0xFFFFFFFF, Size >> 2, (void *)Buffer));
free(Buffer);
}
else
printf("ERROR: Unable to open file '%s'\n", Filename);
}
//****************************************************************************
int main(int argc, char **argv)
{
printf("STM32CRC Test\n Usage: STM32CRC [<file>]\n");
if ((Crc32(0xFFFFFFFF, 0x12345678) != 0xDF8A8A2B) ||
(Crc32Fast(0xFFFFFFFF, 0x12345678) != 0xDF8A8A2B))
{
printf("ERROR: Internal Sanity Check Failed\n");
}
if (argc > 1)
TestFile(argv[1]);
else
test();
return(1);
}
//****************************************************************************
2024-03-11 08:56 AM
Thanks i compile it on linux after little complication with DWORD. Now it work and result is same thx.
But for other is better have utility ready...
2024-03-11 10:28 AM
Not exactly rocket sciencery.. but I'll bite.
windows.h contained DWORD
https://github.com/cturvey/RandomNinjaChef/blob/main/stm32crc.c
2024-03-11 11:35 AM
I looked at RandomNinjaChef's menu first, but couldn't find the right flavour of CRC there.
Oh, it's a fresh dish.
JW