Check Entire Flash in Priority Mode

In priority mode, CRCSCAN will run a single check of Flash, and the CPU will be stalled until CRCSCAN has completed. In this example, CRCSCAN is configured to run in priority mode on the entire Flash. When CRCSCAN has completed its check, the application code will check the CRC calculation status, which will be reflected by the frequency of LED0 on ATtiny817 Xplained Mini.


In this application, all the module configurations exist in the main.c file.

  1. 1.Configure CRCSCAN
    • In main(), set the MODE bit fields (MODE<1:0>) in CRCSCAN CTRLB register equal to 0x0 in order to run a single CRC check where CRCSCAN has priority to Flash. This will stall the CPU until the scan has finished.
    • Set the SRC bit fields (SRC<1:0>) in CRCSCAN CTRLB register equal to 0x0 to perform a CRC check on the entire Flash (the boot, application code, and application data sections).
  2. 2.Write the ENABLE bit (ENABLE) to ‘1’ in the CRCSCAN CTRLA register to enable the CRCSCAN peripheral with the current configurations to start a scan.

Program Flow

Post-Build Command

Calculating the CRC checksum and storing it in Flash can be done by using a post-build command in Atmel Studio. Follow the instructions in CRC Checksum Setup with the following post-build command:

srec_cat "$(OutputDirectory)\$(OutputFileName).hex" -intel -crop 0 0x1FFE -fill 0xFF 0 0x1FFE -CRC16_Big_Endian 0x1FFE -broken -o "$(OutputFileName)_crc.hex" -intel -line-length=44
Info: The post-build command will take the generated <project_name>.hex file from the build output, fill the remaining space of the 8 KB Flash with 0xFF, calculate the CRC, and put the CRC checksum at the last two bytes of the 8 KB block, address 0x1FFE. The resulting file will be named <project_name>_crc.hex. A complete command line description and options can be found at
Tip: The post-build command might fail due to improper break-line interpretation during copy-paste. Fix the improper break-line or type in the command manually.

Testing the Application

Program the ATtiny817 by clicking Tools→Device programming→(Tool=mEDBG, Device=ATtiny817, Interface=UPDI, Apply)→ Memories →Program.

If the device is programmed with the <program_name>.hex file, the CRC check will fail since there is no CRC checksum added at the end of the Flash, and LED0 will toggle at FAIL_DELAY interval.

If the device is programmed with a <program_name>_crc.hex file, the pre-calculated checksum will be stored at the end of the 8 KB Flash address. The CRCSCAN should run through with no failure and LED0 will toggle at the OK_DELAY interval.