cancel
Showing results for 
Search instead for 
Did you mean: 

How to calculate and generate post-build CRC

LauraCx
ST Employee

How to calculate and generate post-build CRC?

1.  Cyclic Redundancy Check

Cyclic Redundancy Check (CRC) is an error detection method for digital data based on binary division.
CRC algorithm generates a fixed checksum code length.
 

2. How to calculate and generate post-build CRC?

Post build CRC are mainly used for flash integrity.

This section describes how to generate it with mainly toolchains.

  • With IAR EWARM Toolchain

IAR EWARM provides a way to generate checksum in Project--> Options:

1541.png 

  • With STM32CubeIDE Toolchain

STM32CubeIDE doesn’t have this property as IAR EWARM, an external tool is mandatory to generate a post build CRC.

Srec_cat is a tool available in SRecord utility package which is a standalone utility for memory manipulation.

Features:

  • load one or multiple BINARY files
  • cut out specific address areas
  • move them to a new address
  • store them to a HEX file


Srec_cat can be used for example to generate a CRC at the end of binary file after build by calling it in your toolchain.
This utility and all information about it are available at Peter Miller’s webpage: http://srecord.sourceforge.net/.
In the download page in the “SourceForge Downloads” section, select the “1.64 release .zip file” link.
Click on it and download the SRecord 1.64 package.
The downloaded ZIP folder contains these items:
srec_cat.exe: The srec_cat program may be used to catenate (join) EPROM load files, or portions of EPROM load files, together.  Because it understands all the input and output formats, it can also be used to convert files from one format to another.
srec_cmp.exe: The srec_cmp program may be used to compare EPROM load files, or portions of EPROM load files, for equality.
srec_info.exe: The srec_info program may be used to print summary information about EPROM load files.
Reference manual srecord-1.63.pdf
Readme files
This online workshop gives the process to use srec_cat with STM32CubeIDE:

STM32CubeIDE-Workshop-2019/hands-on/06_F746-DISCO-CRC at master · ethanhuanginst/STM32CubeIDE-Workshop-2019 · GitHub
 

  • With Keil MDK Toolchain

It is possible to use srec_cat in the same way with Keil MDK.

  • Copy srec_cat.exe, srec_cmp.exe, and srec_info.exe files in a new “bin” folder to the root of a Keil project as shown below.

1542.png

  • Enable the option “Create HEX file” in tab “Output” of Options:


1543.png
 

  • Enable the post-build option


To add a CRC after build, it is necessary to enable option “After Build/Rebuild” in User tab and call Srecord commands:

  • Create an empty text file in a text editor
  • Copy this command in your file:

.\bin\srec_cat .\Debug\OEM_Dev.hex -intel -crop 0x08000000 0x08002F02 -fill 0xFF 0x08000000 0x08002F10 -CRC16_Little_Endian 0x08002F10 -o .\Debug\OEM_Dev_CRC16.hex -intel
This command isolates section data from address 0x08000000 to 0x08002F02, fill unused datas of this sector with 0xFF, calculate CRC of the isolate section and place it at address 0x08002F10.
These operations are saved in the output hex file: OEM_Dev_CRC16.hex
You can adapt this command to your own usage.
Refer to srecord-1.63.pdf for more information about the Srecord commands.
 

  • Save your file as “post-build.bat”.
  • Enable post-build option and call this file:


1544.png

  • The hex file to be used for debug and download must be filled in Debug tab.

The edited hex file generated in post-build step will be then used for programming and debugging.
Create a text file, rename it as “Initialize.ini”.
Save it in the root of the project.
Write this command to the file and save it: LOAD .\debug\ OEM_Dev_CRC16.hex INCREMENTAL
Enter this file in Debug tab:
1545.png
And in “Utilities” tab:
 1546.png
 

Comments
FShah.1
Associate III

@Tesla DeLorean 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?

Version history
Last update:
‎2024-01-03 09:17 AM
Updated by:
Contributors