Calculate CRC32 as in STM32 hardware

Technical Note 64424

Targets:
ARM

Component:
General

Updated:
5/14/2013 9:10 AM

Introduction

STM32 devices from STMicroelectronics have a built-in hardware CRC32 calculator. (This means that using CRC32 in an application can be done without using code space for the algorithm).

Discussion

The CRC32 algorithm used in STM32 devices can be reproduced by the IELFTOOL utility in IAR Embedded Workbench for ARM version 5.50 and newer.

Solution for IAR Embedded Workbench for ARM version 6.40 and later

Options for 6.40 and later

Configure options in Project > Options > Linker > Checksum to enable IELFTOOL to make CRC32 calculation in the same way as the hardware in the STM32 devices.

The options should be set as follows:

Size: 4 bytes
Alignment: 4
Algorithm: CRC32(0x4C11DB7)
Complement: As is
Bit order: MSB first
[ ]Reverse byte order within word [unchecked]
Initial Value: 0xFFFFFFFF
[ ]Use as input [unchecked]
Checksum unit size: 32-bit

Example for STM32F10x

This example shows source code, the linker configuration file and the corresponding settings in the linker options dialog.

  • Source code (snippet)
#include "stm32f10x_crc.h"

extern uint32_t __checksum;

uint32_t calcCrc32(uint8_t* data, uint32_t len)
{
uint32_t* pBuffer = (uint32_t*) data;
uint32_t BufferLength = len/4;
uint32_t index = 0;

RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC, ENABLE);

CRC_ResetDR();

for(index = 0; index < BufferLength; index++)
{
CRC->DR = pBuffer[index];
}

return CRC->DR;
}

void main(void)
{
...

uint32_t valCrc32 =
calcCrc32((uint8_t*)0x08000000, 0x040000 - 4);

if (valCrc32 == __checksum)
{
// TBD
}
else
{
// TBD
}
...
}
  • Linker configuration file (snippet)
define symbol __ICFEDIT_region_ROM_start__ = 0x08000000;
define symbol __ICFEDIT_region_ROM_end__ = 0x0803FFFB;

place at address mem:0x0803FFFC { readonly section .checksum };

Solutions for older IAR Embedded Workbench for ARM versions

Options for 6.10 to 6.30

Configure options in Project > Options > Linker > Checksum to enable IELFTOOL to make CRC32 calculation in the same way as the hardware in the STM32 devices.

The options should be set as follows:

Size: 4 bytes
Alignment: 4
Algorithm: CRC32(0x4C11DB7)
Complement: As is
Bit order: MSB first
[*]Reverse byte order within word [CHECKED]
Initial Value: 0xFFFFFFFF
[ ]Use as input [unchecked]

Solution for IAR Embedded Workbench for ARM version 5.50

In version 5.50, IELFTOOL can be configured to calculate CRC32 in the same manner as the calculation of CRC32 using the STM32 hardware. This ability is not documented and it can only be invoked as a command line option.

The details that concern the STM32 CRC32 algorithm are

  • 0xFFFFFFFF as initial value.
  • Utilize the two new ielftool algorithm flags i and r.

The string to enter in Project > Options > Build Actions > Post-build command line is...

ielftool --fill 0xFF;__checksum_begin-__checksum_end
--checksum __checksum:4,crc32:ir,0xFFFFFFFF;__checksum_begin-__checksum_end
--verbose $TARGET_PATH$ $TARGET_PATH$

...where these substrings are placeholders that needs to changed for the project in use:

__checksum
The name of the symbol where the checksum value should be stored. Note that it must exist in the symbol table in the input ELF file.

__checksum_begin
The first address on which the checksum should be calculated.

__checksum_end
The last address on which the checksum should be calculated.

The preceding command line should be combined with all options reset (unchecked) in Project > Options > Linker > Checksum

 

All Product names are trademarks or registered trademarks of their respective owners.

Related Tech Notes

© IAR Systems 1995-2016 - All rights reserved.