5 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.
Initialization
In this application, all the module configurations exist in the main.c file.
- 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).
- In
- 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
- The STATUS register is monitored in
an endless
while(1)
loop.- If the OK bit in the STATUS register is set, LED0 toggles at the configured OK_DELAY interval (the default value is 500 ms) indicating that the CRC check has successfully completed.
- If the CRCSCAN fails, LED0 will toggle at the configured ERROR_DELAY (the default value is 50 ms). This may indicate that the NMI handler is corrupted.
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
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.