cancel
Showing results for 
Search instead for 
Did you mean: 

A solution to adding CRC32 to the end of the Binary file after build

MStra.3
Associate III

I spend a lot of time looking and never could find an easy way to do this. I came up with a solution and thought I would post it.

I am using the STM32F407. The F4 series only has one way of calculating the CRC in hardware and that is what I chose to use in the Binary file. This way, the F4 can easily verify a file when it is uploaded to it for a firmware update.

I have attached a Zip file with an EXE in it called CRC32. It requires one parameter - the name of the file to add a CRC32 to. It assumes the file size will be a multiple of 4 bytes. It then reads the file and calculates the CRC. When it reaches the EOF it inverts the CRC and appends it to the end of the file. This way when the whole file with added CRC32 is CRC'd again on upload, the result should be 0xFFFFFFFF. If CRC32 is run on the same file multiple times, it will continue to add CRC32's to it - probably not what you want, but if you make this automatically run each time you build, that should never happen.

You can make this run automatically after your build by adding a command in the Post-build steps: Project Properies >> C/C++ Build >> Settings >> Build Steps:

"<path to crc32>\crc32" "${PWD}\${ProjName}.bin"

The quotes are necessary because of possible blanks in the path and to correctly get the filename to add the CRC to.

Mike.

5 REPLIES 5
HHa.1
Associate

hi @MStra.3​ , may you share the source code of crc32 tool?

MStra.3
Associate III

I found the code in the public domain and modified the File I/O slightly to add the CRC to the file.

Mike.

FShah.1
Associate III

@MStra.3 

Hi, I am following a video to write a bootloader that transfers the application code serially into the STM32F446 controller. The video originally presents the STM32F7xx series controller, but I am applying it to the F4xxx series. I have modified the code as needed, such as assigning the proper flash areas for the bootloader and application code. See the GitHub link for this tutorial.

https://github.com/Embetronicx/STM32-Bootloader/tree/ETX_Bootloader_3.0/Bootloader_Example

This link also provides a PC tool that transfers the code from the host to the controller once the handshaking is complete.

What are the recommendations if I want to calculate the CRC32 on a PC that matches the CRC32 calculated on the STM32F446 using HAL_CRC_Calculate()?

My code gives 0x08b329c8 and host calculated CRC is 0x4e08bfb4?

The STM32 32-bit CRC operates on 32-bit words, left shifting, and isn't directly compatible with the CRC32 polynomial used for PKZIP, etc.

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..