2019-11-27 08:23 PM
Hello All,
We have designed a custom board with STM32F777IITx MCU on it, where i am using ST Micro's CubeMX with Atollic True Studio for developing the code.
Now I am also using a USB host peripheral and reading data from USB device now this data read from USB needs to checked for CRC.
I tried to build a basic code using ST Micro's CubeMX and Atollic True Studio which would calculate CRC of an array,
However when build the Code i am getting the given below warnings and also if i ignore these warnings my code gets stuck after it executes "HAL_CRC_Calculate" api.
de-referencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
de-referencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
de-referencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
The given above warnings also come when in blank code generated using Cube MX.
Also i referred the example code "CRC_Example" from the "STM32Cube_FW_F7_V1.9.0" Firmware package.(This example was for boards "STM32F769I_EVAL" and "STM32F769I-Discovery").
After referring example code from firmware package and incorporating the changes as per the example code in blank code yet i am getting same warnings.
2019-11-27 10:26 PM
This is consequence of a "hardware hack", where the data register of CRC behaves differently when written as byte, halfword or word. The offending line(s) are:
pReg = (__IO uint16_t *)(__IO void *)(&hcrc->Instance->DR); /* Derogation MisraC2012 R.11.5 */
Unfortunately, the C programming language has no "correct" way to tell the compiler that "this is a very special register, it's OK to access it in several different ways", this is why the compiler rightfully warns.
So, you either ignore this warning (suppress it selectively when compiling given file), or reimplement the function so that there are no such offending type punnings (punning to byte appears to be OK from gcc's point of view).
JW
2019-12-03 03:23 AM
Hello All,
Thank You For Your Response and Suggestion,
Currently, i am ignoring the warnings and continuing with further code.
My aim is to store a file (File would be always less then 2MB) from USB to flash. With this i also want to calculate the CRC of the same file stored on the flash.
I have used default settings (i.e., CRC-32/MPEG2) which calculates 32-bit CRC and takes 32bit data as input,
Now when i pass a long Buffer of 32 bit (i.e., length of buffer is 64) as input to the CRC function i am able to calculate the CRC and i am able to verify the calculated CRC using the online CRC calculator. Link given below
I have stored a file in the flash by using St Micro's FATFS library, and this file is 1.8 MB long.
Now using same CRC-32 calculation setting i wanted to calculate the CRC of the File stored in flash, Where i am also able to calculate the CRC of the whole file stored in flash,
However i am not able to verify whether my calculated CRC is correct or not.
As by searching on internet i am not able to find any link or site where i can directly upload the same file and verify the CRC,
I got some sites where i can upload the file and check CRC but when i verify CRC on that site and my calculated CRC doesn't match, after some research i found that the CRC calculated on these sites are based CRC-32 algorithm whereas our CRC is based on CRC-32/MPEG2 algorithm
After this i made few changes in code and changed the CRC algorithm to CRC-32 However my issue still remains the same when i calculate the CRC for small data my CRC matches whereas when i calculate it for whole file the CRC doesn't matches.
So i request to please help me with any tool/site where i can verify my calculated CRC (for both the algorithms).
The links which i used for verify CRC of whole file are as given below
https://simplycalc.com/crc32-file.php
Please help me with this at the earlies
2019-12-03 05:42 AM
If you can program in C, just write a simple application on the PC.
I've posted examples of the STM32 default algorithm in the past.
For a more standard CRC computation look at ZIP or RAR tools.
2019-12-03 02:36 PM
> when i calculate the CRC for small data my CRC matches whereas when i calculate it for whole file the CRC doesn't matches.
Is the size of the file multiple of 4?
To exclude problems with handling the file itself, try something simpler instead of CRC, e.g. simple XOR, or a simple sum.
JW
2019-12-03 03:46 PM
FatFS based File CRC example here, although forum migration has changed double quotes to single ones
https://community.st.com/s/question/0D50X00009XkgXzSAJ/bug-in-stm32adafruitsdc-spi-sd-card
2019-12-03 03:50 PM
//******************************************************************************
// CRC File Integrity Check - sourcer32@gmail.com
//******************************************************************************
uint32_t CRC32(uint32_t Crc, uint32_t Size, uint8_t *Buffer)
{
while(Size--)
{
static const uint32_t CrcTable[] = { // Nibble table for 0xEDB88320 polynomial
0x00000000,0x1DB71064,0x3B6E20C8,0x26D930AC,0x76DC4190,0x6B6B51F4,0x4DB26158,0x5005713C,
0xEDB88320,0xF00F9344,0xD6D6A3E8,0xCB61B38C,0x9B64C2B0,0x86D3D2D4,0xA00AE278,0xBDBDF21C };
Crc = Crc ^ (uint32_t)*Buffer++; // Apply byte
Crc = (Crc >> 4) ^ CrcTable[Crc & 0x0F]; // Two rounds of 4-bits
Crc = (Crc >> 4) ^ CrcTable[Crc & 0x0F];
}
return(Crc);
}
//******************************************************************************
uint32_t CrcFile(char *Filename, uint32_t BufferSize)
{
FRESULT res;
FIL fil;
uint32_t Crc = 0xFFFFFFFF;
uint32_t Align = 32;
uint8_t *p = malloc(BufferSize + Align);
while((p == NULL) && (BufferSize > Align))
{
BufferSize >>= 1;
p = malloc(BufferSize + Align);
}
if (p)
{ // Align IO Buffer
uint8_t *Buffer = (uint8_t *)(((uint32_t)p + (Align - 1)) & ~(Align - 1));
res = f_open(&fil, Filename, FA_READ);
if (res != FR_OK)
printf("res = %d f_open %s\n", res, Filename);
if (res == FR_OK)
{
uint32_t Size = f_size(&fil);
while(Size) // Process until all consumed
{
UINT BytesRead;
res = f_read(&fil, Buffer, BufferSize, &BytesRead);
if (res != FR_OK)
{
printf("res = %d f_read %s\n", res, Filename);
break;
}
Crc = CRC32(Crc, BytesRead, Buffer);
Size -= BytesRead;
}
res = f_close(&fil);
if (res != FR_OK)
printf("res = %d f_close %s\n", res, Filename);
}
printf("CRC32 %08X PKZIP %08X %s\n", Crc, ~Crc, Filename);
free(p);
}
return(~Crc);
}
//******************************************************************************
void TestFile(char *Filename)
{
uint32_t Crc;
Crc = CrcFile(Filename, 512);
if (CrcFile(FileName, 1024) != Crc)
puts("!!FAIL!!");
if (CrcFile(FileName, 4096) != Crc)
puts("!!FAIL!!");
if (CrcFile(FileName, 8192) != Crc)
puts("!!FAIL!!");
}
//******************************************************************************
�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?
Formatting addressed somewhat, can't edit original thread as closed.
2019-12-03 11:34 PM
Hello waclawek.jan,
Thank you for Your response
Is the size of the file multiple of 4?
Yes
Thanks and regards
Utkarsh
2019-12-03 11:36 PM
Hello User 6.02214076×10²³,
Thank you for your Suggestions and response
I will Check with this and let you know if any issues
Thanks and regards
Utkarash