Calculate CRC 32 as in STM 32 hard ware(EWARM v.5.50 and later)

6494 ワード

http://supp.iar.com/Support/?note=64424&from=note+11927
Background The STM 32 devices from ST Miro have a built-in hardware CRC 32 calculator.(So using CRC 32 in appication does not use up code space for the algorithm.)ProblemThe CRC 32 algorithm used in STdevice 32
Solution for EWARM version 6.40 and later
Options for 6.40 and later
Optionsエリアadded to Project>Options>Linker>Checksum to enable ielftool to make CRC 32 calculation in the same way the hardware in the STM 32 devices.
The options shound 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 STM 32 F 10 x
This example shows source code、the linker configration file and the cores ponding 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 configration 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 configred to calculate CRC 32 in the same manner as the cacacaculation of CRC 32 using the STM 32 hadware.This ability is not documented and it can onlybe invoked as a command the opeat the contronit
0 x FFFF as initial value.
Utilize the two nelftool algorithm flags i and r.
The string to enter in Project>Options>Build Acts>Post-build commandライン 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 substrigs are plocholders that needs to changed for the project in use:
同前checksum
The name of the smbol where the checksum value shound be stored.Note that it must exist in the smbol table in the input ELF file.
同前checksum_begin
The first address on which the checksum Shuld be calculated.
同前checksum_end
The last address on which the checksum Shuld be calculated.
The preceding command line shound be commbined with all optionset in Project>Options>Linker>Checksum
Project Optionslinker Checksum(6.40) (32 KB)
Project Optionslinker Checksum(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