cancel
Showing results for 
Search instead for 
Did you mean: 

CRC32 Calculation STM32L071CBT

davidcodes
Associate

Hi all,
I know similar issues have been discussed before but I have not been able to find another post with the same issue. I am trying to use a CRC32 calculation to validate my flash memory, I am using KEIL IDE and the STM32L071CBT.

This has been my current approach, enabling the HEX file generation on KEIL IDE and using a batch file (used with my after-build option in KEIL)  with the following code:


.\bin\srec_cat .\Device\OutputHexFile.hex -intel -crop 0x08000000 0x0801FFFC -fill 0xFF 0x08000000 0x0801FFFC -crc32-l-e 0x0801FFFC -o .\Device\OutputHexFile_CRC.hex -intel

From my understanding this should calculate the CRC32 (Little-Endian) and place the value at the address 0x0801FFFC. 

I use the HAL peripheral, HAL_CRC_Calculate in my code to calculate the CRC-32 little-endian value:

#define ROM_START     (uint8_t*)(0x08000000)
#define ROM_END       (uint8_t*)(0x0801FFFB)
#define CHECKSUM      (uint8_t*)(0x0801FFFC)

#define ROM_LEN       (uint32_t)(ROM_END - ROM_START + 1u) 
#define ROM_LEN_WORDS ((ROM_LEN + 3u) / 4u)  


uint32_t crc, checksum;


crc = HAL_CRC_Calculate(&hcrc, (uint32_t*)ROM_START, ROM_LEN_WORDS);
checksum = *(uint32_t*)CHECKSUM;
 
Then I compare the checksum and crc variables to check for any issues with the flash memory.

Because my STM32L071CBT has a flash memory size of 128KB.

When I try to print the different variables I get different values, but I thought they should be the same. 

Can I have some actual code example or updates to how I should fix this? Or should I not trust the HAL_CRC_Calculate?
 
12 REPLIES 12
Saket_Om
ST Employee

Hello @davidcodes 

Please, places the CHECKSUM result out of the tested area.  

 

If your question is answered, please close this topic by clicking "Accept as Solution".

Thanks
Omar

Hi Omar,

Thanks for the reply, if you mean for the .bat file that uses srec_cat, my understanding was that using crop and fill for srec_cat, crops and fills the data up to the end address you specify, e.g. where I specified "0x0801FFFC", but does not actually touch any data at the address "0x0801FFFC" and therefore I am safe to save my CHECKSUM value at that location? 

If you mean in the C code, using HAL_Calculate_CRC, my understanding was that specifying the start address as "0x08000000" and the end address as "0x0801FFFB"", the address "0x0801FFFC" would not be touched by my HAL_Calculate_CRC function/calculation and therefore be safe to access and read the value.

Please tell me if my assumptions are incorrect or if you could more specific what part of my code (C code or batch file) is incorrect?

Thanks,
David

Hello @davidcodes 

In the scatter file you should set the correct placement of the CRC. 

It places the CRC at the end of the Flash to prevent any constant initialization data be placed out of the tested area in the Flash.

; Place after LR_IROM1 region
; Place CHECKSUM at the and of ROM after RW init
LR_IROM2 +0 {
  ER_ROM2 +0x0 {
	*.o (CHECKSUM, +Last)
  }
}

 

If your question is answered, please close this topic by clicking "Accept as Solution".

Thanks
Omar

Hi Omar,
This does not fix the issue after making the suggested change. Given that I have STM32L071CBT, which from what I understand is a Category 5 device? Is there any potential errors which the start and end flash memory addresses I have used? 

To refresh, this is my C code:

 

#define ROM_START     (uint8_t*)(0x08000000)
#define ROM_END       (uint8_t*)(0x0801FFFB)
#define CHECKSUM      (uint8_t*)(0x0801FFFC)

#define ROM_LEN       (uint32_t)(ROM_END - ROM_START + 1u) 
#define ROM_LEN_WORDS ((ROM_LEN + 3u) / 4u)  


uint32_t crc, checksum;


crc = HAL_CRC_Calculate(&hcrc, (uint32_t*)ROM_START, ROM_LEN_WORDS);
checksum = *(uint32_t*)CHECKSUM;

 

Make srec_cat generate the .BIN you're going to burn, and the CRC for that, to ensure you're generating what you expect.

Perhaps confirm PC, and STM32 side computations for the binary with this..

https://github.com/cturvey/RandomNinjaChef/blob/main/stm32crc.c

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Hi Tesla,
I have tried to separate the .bat file I mentioned before into 2 bat files if this is what you mean, but that still does not work. I have been trying to implement the HAL_CRC_Calculate peripheral and therefore the the manual implementation used in the link you provided is a little different. Is there any way I have used HAL_CRC_Calculate or the flash memory addresses incorrectly? 

My updated file layout (if this is the implementation you were suggesting?) :

file1.bat:
.\bin\srec_cat .\Device\OutputHexFile.hex-intel -crop 0x08000000 0x0801FFFC -fill 0xFF 0x08000000 0x0801FFFC 
-o .\Device\OutputFile_NoCRC.bin -intel

file2.bat
.\bin\srec_cat .\Device\OutputFile_NoCRC.bin -intel -crc32-l-e 0x0801FFFC -o .\Device\OutputHexFile_CRC.hex -intel




Hi Pavel,
In the thread you have linked the OP made a comment that was never answered where they asked for the code example of the implementation of the CRC32 calculation but that comment was never answered and I am not sure whether the suggestions can be implemented if they were never replied to, claimed the suggestions did not work (no match still) and therefore I do not know if they work.