Checksum calculation with IAR XLINK Linker

Technical Note 91733






2023/10/6 15:15


This Technical Note shows a few examples of how a CRC16 checksum can be added in applications made with IAR Embedded Workbench.

Applies to

This Technical Note applies to IAR Embedded Workbench products that use the IAR XLINK Linker. For products using the IAR ILINK Linker, see Technical Note 11927.


  • You need to turn on checksum calculation in the IAR XLINK Linker.
  • You must inform yourself about the memory ranges that IAR XLINK Linker will calculate the checksum for.
  • You need to include the checksum calculation C source in the source code of the application.

What will be included in the checksum calculation?

Generally speaking: "All bytes in the application that must be downloaded to the target, will be part of the checksum calculation."

The simplest way of get knowledge about this is to turn on "checksum calculation" + "map file" in the options for IAR XLINK Linker. Then look at the end of the .map file to see which ranges in which memory that will be included in the checksum calculation.

Please note that DATA will normaly not be included in the checksum calculation, but that XDATA (and some other segment types) can be included besides the CODE segment type. If other segment types (than CODE) are included, then you can exclude those segment types. (The example for IAR Embedded Workbench for ARM has the -J option set up in such a way that XDATA is not included in the checksum calculation.)

Checksum calculation in the application

The supplied examples show how to use the CRC16 checksum calculation. In the C source of the application you need to add the actual generic C source to calculate the same checksum value as IAR XLINK Linker produces. There are two variants of the calculation:

  • Fast -- Short execution time, but it uses up more ROM/flash for a constant table.
  • Slow -- It uses less ROM/flash but more execution time.

Please note:

  • The checksum calculation can be made over several ranges. If several ranges are used, then take care to calculate the ranges with the lowest address range first and so on.
  • The checksum calculation must not (in order to work) be made over the bytes where the checksum is placed in memory. (One solution is to place checksum at the end of memory, as otherwise you must make two ranges in the application.) So the bytes (1, 2 or 4) where the checksum is saved must be excluded from the checksum calculation.
  • If the slow function is used, you must make a final call to the checksum calculation with as many bytes with value 0x00 as you have bytes in the checksum.

Checksum calculation made by XLINK

You can use these options either in your linker command file (filename .XCL) or in the IAR Embedded Workbench IDE.

In IAR Embedded Workbench, you set up calculation of the checksum in the Project > Options > Linker (XLINK) > Processing. The options that corresponds to the examples are Fill unused code memory (command line option H); Fill pattern (any pattern); Generate checksum (command line option J); size = 2 bytes; CRC16; Complement = as is; Bit order = MSB first.

From command line (or in the .xcl file) you use the -J and -H linker options for calculating the checksum. Note that you must use the -H filler byte option to be able to use -J.

There are some requirements that you have to fulfil to make this work.

  • You must ensure that the segment in which the checksum itself is stored has a proper address and address range.
    -Z(CODE)CHECKSUM=1000-1001 /* In this example a two byte checksum */

  • You should have both a start and stop address defined for all code segments.
    -Z(CODE)CODE_SEGMENT /* Wrong */
    -Z(CODE)CODE_SEGMENT=2000 /* Wrong */
    -Z(CODE)CODE_SEGMENT=2000-4FFF /* Correct */

  • As long as you have both start and stop addresses you can have more than one CODE memory range.

Examples for download

These examples include the actual generic C source to calculate the same checksum value as IAR XLINK Linker produces in its CRC calculation. The examples also contain IAR Embedded Workbench settings for IAR XLINK Linker and a modified .xcl file for placing the CHECKSUM segment at a proper place in memory.

More information

There is more information about the checksums in XMAN.HTM and XLINK.PDF which can be found in your installation. You can also download these files in the related support link "Latest version of IAR XLINK Linker".

Mr. Ross N. Williams at Rocksoft in Australia have written, what he calls: A PAINLESS GUIDE TO CRC ERROR DETECTION ALGORITHMS and this document can be down loaded from the link CRC explained by Mr. Ross N. Williams.

Finally, there is also the tecnical note '26457 Checksum generation' in which our Senior linker developer goes into the details of checksums.


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



We do no longer support Internet Explorer. To get the best experience of, we recommend upgrading to a modern browser such as Chrome or Edge.