Calculate CRC32 as in STM32 hardware (EWARM v.5.50 and later)
http://supp.iar.com/Support/?note=64424&from=note+11927
Background
The STM32 devices from ST Micro have a built-in hardware CRC32 calculator. (So using CRC32 in an application does not use up code space for the algorithm.)
Problem
The CRC32 algorithm used in STM32 devices could not be reproduced by the ielftool utility before EWARM version 5.50.
Solution for EWARM version 6.40 and later
Options for 6.40 and later
Options are added to 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.
#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 };
Solution for EWARM 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. |
__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
ProjectOptionsLinkerChecksum(6.40) (32 KB)
ProjectOptionsLinkerChecksum(6.10-6.30) (31 KB)
Example (6.40) ZIP, 654 KB)
Example (6.21) (ZIP, 645 KB)
Technical note 11927
Checksum calculation with IELFTOOL after linking with ILINK
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
2013-06-27 磁盘相关概念以及知识