9.1 Invariable Memory

A cyclic redundancy check (CRC) has been implemented to test the invariable memory. This is an error detection technique used to find accidental errors in data. This method is commonly used to determine the correctness of data transmissions and data present in data and program memories.

The CRC algorithm processes an input data stream or block of data and generates an output checksum, which can be used to detect errors later. Two common methods to do this consist of the following:

  • Compute a checksum of the data and store it. In order to detect errors, a new checksum is computed on the same data and compared with the previous one. If they are different, there is an error.
  • Compute a checksum of the data and append it to the data section. The checksum computed of the data plus the included CRC checksum should result in a constant CRC value. If the new checksum is not the correct value, the data has changed, and there is an error.

An n-bit CRC applied to a data block of arbitrary length will typically detect any single error burst not longer than n bits and will detect the fraction 1/(1-2-n) of all longer error bursts. If there is an error in the data, the application should take some corrective action.

Self-diagnostic modules based on hardware and software are available. Two commonly used CRC standards are supported:

  • 16-bit CRC CCITT
  • 32-bit CRC IEEE® 802.3

The software implementation can be used by all tinyAVR® 1-series devices. In this case, the CPU reads the data and computes the CRC checksum. It is possible to choose between two software implementations:

  • Lookup table: This uses a CRC lookup table to speed up the computations. The lookup table requires 512 (for 16-bit) or 1024 (for 32-bit) bytes of Flash memory.
  • Direct computation: This calculates the checksum for each byte using a polynomial division. This version occupies no space in the Flash memory but is slower than the lookup table method.

In the software implementations, the 32-bit CRC polynomial used is 0xEDB88320, the initial remainder is 0xFFFFFFFF, and the generated checksum is bit-reversed and complemented. The CCITT 16-bit CRC polynomial used is 0x1021, with 0x0000 as initial remainder. In this case, the checksum is neither bit reversed nor complemented.

The hardware implementation in the tinyAVR® 1-series can only check the content of Flash, not the EEPROM or SRAM. The CRC computed is 16 bits wide, and the CRC mechanism relies on the CRC checksum to be present at the last part of Flash being checked. Failure can either be signaled through an ISR, or a status flag needs to be checked after the CRC is complete. The CRC module has full access to Flash, and the CPU does not run at the same time. The CRC can also be made to run before the CPU is started at start-up through a fuse setting, it is recommended to do so.

The CRC scan time depends on how much Flash is scanned and the main clock frequency. The scan will process 16 bits of data in three main clock cycles. In addition, there is a small overhead for starting and stopping roughly 20 main clock cycles. If testing Flash during the operation of the application is needed it has to be done when the application can handle the microcontroller not responding for however long the Flash takes to scan.

The following functions are available to compute checksums for data stored in the EEPROM:
  • CLASSB_CRC16_EEPROM_SW
  • CLASSB_CRC32_EEPROM_SW

To compute CRC of Flash content the HW module is used. The driver for this can be found in classb_crc_hw.h.

Note: The hardware and software implementations have been configured, so that equivalent CRC algorithms produce the same checksum. There are, however, significant differences in processing time.
The example application uses the CRCSCAN module on the device to check the integrity of Flash. The CPU does not run while the test is being performed. A button is configured so that when pressed it writes to a page in Flash. When this is done the CRC scan will fail, and the activated non-maskable interrupt (NMI) will execute and turn OFF an LED.
Note: The NMI cannot be deactivated. Once an error is found, it will stay active constantly, and the code of the ISR will be reentered right after the ISR is exited until the WDT triggers and resets the device.
Note: In order for the CRC not to fail the CRC checksum needs to be calculated and appended to Flash. This can be accomplished by adding a post-build command in Studio. This command and how to add it can be found in AN2521 'CRCSCAN on Devices in the tinyAVR® 1-Series'.

Pressing the button once more will have no effect. In order to check that all equivalent CRC computations lead to the same checksum, it is possible to call them and compare the results. Note that the algorithm for the software implementation (lookup or direct computation) is chosen by a setting in the corresponding header file and only one algorithm can be called during one execution. Since choosing one algorithm or the other will modify the content in Flash, the checksums across different executions for EEPROM should be compared instead.