cancel
Showing results for 
Search instead for 
Did you mean: 

What the CRC algorithm used in No-Stretch GetCheckSum command of STM32 I2C Bootloader

FrankYang-P&S
Associate III

Hello,

There is No-Stretch GetCheckSum command in I2C bootloader command, Does someone know what the CRC algorithm used in the bootloader?

Many thanks.

B.R.

Frank

3 REPLIES 3
akn
Associate

On the STM32U545, at least, the Bootloader seems to use the CRC-32 (Ethernet, poly = 0x4C11DB), except the input is not reflected, the output is not reflected, and the final XOR is skipped (or set to 0). Also, each 32-bit word is byte swapped.

So if you have written data to flash using the bootloader (No Stretch) Write Memory command, you can compute the same CRC on the data you sent using a function like this:

#define CRC_POLY 0x04C11DB7UL
#define CRC_INIT 0xFFFFFFFFUL

uint32_t Crc32(uint32_t crc, uint8_t *data, uint32_t len) {
  for (size_t i = 0; i < len; i+=4) {
    // Reverse bytes in word
    crc ^= data[i+3] << 24;
    crc ^= data[i+2] << 16;
    crc ^= data[i+1] <<  8;
    crc ^= data[i+0] <<  0;
    for (uint32_t k = 0; k < 32; k++) {
      crc = crc & 0x80000000UL ? (crc << 1) ^ CRC_POLY : crc << 1;
    }
  }
  return crc;
}

 Set crc = CRC_INIT for the first call, and feed the result back in for subsequent blocks of data. The final result does not need to be finalized.

CMYL
ST Employee

Hello @FrankYang-P&S 

 

The "No-Stretch GetCheckSum" command in I2C bootloader is explained in section "2.20 No-Stretch GetCheckSum command" of AN4221 "I2C protocol used in the STM32 bootloader".

This command is used to compute the CRC value (based on the CRC hardware IP) of a given flash memory range, defined by the memory offset and size.

The CRC algorithm implemented in the CRC hardware IP (see CRC chapter 20 of the Reference Manual) is CRC-32 (Ethernet) polynomial: 0x4C11DB7

CMYL_0-1708704239644.png

Note that this command is supported in the I2C bootloader protocol version 1.2 and later ( see table 3 of AN4221). I can list for example the STM32H5 series and the STM32U59/5A that support this command.

You can share with me the STM32 family you are using and I will tell you check if this command is supported or not.

 

With best regards,

Younes

CMYL
ST Employee

hello @akn 

Thanks, yes it is the algorithm used by all the STM32 Families, however it is a hardware accelerated algorithm. 

I confirm, we can emulate the CRC behavior using your shared code. 

For more information about the CRC peripheral can be found in AN4187 (Using the CRC peripheral on STM32 microcontrollers - Application note

 

Best Regards,

Younes