Microchip® Advanced Software Framework

 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CRC hardware implementation

Low-level driver implementation for the AVR XMEGA AU CRC module.

The CRC computation is done by the hardware CRC module. Even if this hardware module computes the checksum, the CPU can still be required to read the data. In the case of 32-bit CRC computation for flash memory, the whole process can be run without support from the CPU. However, for the 16-bit computation the CPU would have to read from Flash memory and transfer data to the CRC module. For CRC computations of data in SRAM, EEPROM or peripherals, the CPU will have to transfer data to the CRC module unless a DMA transaction is setup. The CRC-CCITT polynomial is 0x1021 and the IEEE 802.3 polynomial is 0x04C11DB7. More details on the CRC implementation can be found in the XMEGA AU manual.

Note
  • The CRC module driver requires one of the new XMEGA AU devices.
  • The CPU is locked while the CRC module is set to compute a checksum from Flash.
  • CRC32 in Flash can only check an even number of bytes.
  • When specifying Flash or EEPROM addresses, 0x000000 denotes the start of physical memory.

Enumerations

enum  crc_16_32_t {
  CRC_16BIT,
  CRC_32BIT,
  CRC_16BIT,
  CRC_32BIT
}
 Enum for indicating whether to use 16- or 32-bits CRC. More...
 
uint32_t crc_initial_value = 0
 Initial CRC value to use on next CRC calculation. More...
 
static bool crc_is_busy (void)
 Check if the CRC module is busy. More...
 
static void crc_reset (void)
 Reset CRC module and set CHECKSUM to initial value. More...
 
static uint16_t crc16_checksum_read (void)
 Read CRC-16 checksum. More...
 
static uint32_t crc32_checksum_read (void)
 Read CRC-32 checksum. More...
 
static uint32_t crc_checksum_read (void)
 Read checksum. More...
 
static void crc_set_source (CRC_SOURCE_t source)
 Set data source for the CRC module after reset. More...
 
static void crc_disable (void)
 Disable the CRC module. More...
 
static uint32_t crc_checksum_complete (void)
 Complete CRC calculation by reading checksum and disabling module. More...
 
static void crc_32_enable (void)
 Enable CRC-32. More...
 

NVM Commands

NVM commands for 32-bit CRC of flash memory.

These are only used by CLASSB_CRC32_Flash_HW.

#define CRC_FLASH_RANGE   NVM_CMD_FLASH_RANGE_CRC_gc
 Compute a CRC checksum on an address range. More...
 
#define CRC_BOOT   NVM_CMD_BOOT_CRC_gc
 Compute a CRC checksum on the application section. More...
 
#define CRC_APP   NVM_CMD_APP_CRC_gc
 Compute a CRC checksum on the boot section. More...
 

CRC tests

Invariant memory tests based on CRC that are compliant with IEC60730 Class B.

uint16_t CLASSB_CRC16_EEPROM_HW (eepromptr_t dataptr, const crcbytenum_t numBytes, eeprom_uint16ptr_t pchecksum)
 Compute 16-bit CRC for EEPROM address range using hardware CRC module. More...
 
uint16_t CLASSB_CRC16_Flash_HW (flashptr_t dataptr, const crcbytenum_t numBytes, eeprom_uint16ptr_t pchecksum)
 Compute 16-bit CRC for Flash address range using hardware CRC module. More...
 
uint32_t CLASSB_CRC32_EEPROM_HW (eepromptr_t dataptr, const crcbytenum_t numBytes, eeprom_uint32ptr_t pchecksum)
 Compute 32-bit CRC for EEPROM address range using hardware CRC module. More...
 
uint32_t CLASSB_CRC32_Flash_HW (NVM_CMD_t crc_type, flashptr_t dataptr, const crcbytenum_t numBytes, eeprom_uint32ptr_t pchecksum)
 Compute 32-bit CRC for Flash address range using hardware CRC module. More...
 

Internal functions

These are functions from the CRC module driver.

void crc_set_initial_value (uint32_t value)
 Set initial CRC value for next CRC calculation. More...
 
void crc32_append_value (uint32_t value, void *ptr)
 Get and append data to generate checksum zero with CRC32. More...
 
void crc16_append_value (uint16_t value, void *ptr)
 Appends a CRC value to the given pointer. More...
 
uint32_t crc_flash_checksum (NVM_CMD_t crc_type, flash_addr_t flash_addr, uint32_t len)
 Perform a CRC-32 calculation on part of or entire flash. More...
 
uint32_t crc_io_checksum (void *data, uint16_t len, enum crc_16_32_t crc_16_32)
 Calculate the CRC checksum for data in a buffer. More...
 
void crc_io_checksum_byte_start (enum crc_16_32_t crc_16_32)
 Reset CRC module and set source to I/O interface. More...
 
void crc_io_checksum_byte_add (uint8_t data)
 Add one byte of data to CRC performed on I/O interface. More...
 
uint32_t crc_io_checksum_byte_stop (void)
 Disable CRC module and return checksum. More...
 
void nvm_issue_flash_range_crc (flash_addr_t start_addr, flash_addr_t end_addr)
 Issue flash range CRC command. More...
 

#define CRC_APP   NVM_CMD_APP_CRC_gc

Compute a CRC checksum on the boot section.

Referenced by crc_flash_checksum(), and main().

#define CRC_BOOT   NVM_CMD_BOOT_CRC_gc

Compute a CRC checksum on the application section.

Referenced by crc_flash_checksum(), and main().

#define CRC_FLASH_RANGE   NVM_CMD_FLASH_RANGE_CRC_gc

Compute a CRC checksum on an address range.

Referenced by crc_flash_checksum(), main(), and run_flash_test().

Enum for indicating whether to use 16- or 32-bits CRC.

Enumerator
CRC_16BIT 
CRC_32BIT 
CRC_16BIT 
CRC_32BIT 

uint16_t CLASSB_CRC16_EEPROM_HW ( eepromptr_t  origDataptr,
crcbytenum_t  numBytes,
eeprom_uint16ptr_t  pchecksum 
)

Compute 16-bit CRC for EEPROM address range using hardware CRC module.

This function returns the 16-bit CRC of the specified EEPROM address range.

Parameters
origDataptrAddress of EEPROM location to start CRC computation at.
numBytesNumber of bytes of the data.
pchecksumPointer to the checksum stored in EEPROM.
Note
No sanity checking of addresses is done.

References CLASSB_EEMAP_BEGIN, CLASSB_EEMAP_END, CLASSB_ERROR_HANDLER_CRC, CRC16_INITIAL_REMAINDER, CRC_16BIT, crc_io_checksum(), and crc_set_initial_value().

uint16_t CLASSB_CRC16_Flash_HW ( flashptr_t  origDataptr,
crcbytenum_t  numBytes,
eeprom_uint16ptr_t  pchecksum 
)

Compute 16-bit CRC for Flash address range using hardware CRC module.

This function returns the 16-bit CRC of the specified Flash address range.

Parameters
origDataptrAddress of Flash location to start CRC computation at.
numBytesNumber of bytes of the data.
pchecksumPointer to the checksum stored in EEPROM.
Note
16-bit flash CRC is much slower than 32-bit for flash checking.
No sanity checking of addresses is done.

References CLASSB_EEMAP_BEGIN, CLASSB_EEMAP_END, CLASSB_ERROR_HANDLER_CRC, CRC16_INITIAL_REMAINDER, CRC_16BIT, crc_io_checksum_byte_add(), crc_io_checksum_byte_start(), crc_io_checksum_byte_stop(), crc_set_initial_value(), and PROGMEM_READ_BYTE.

uint32_t CLASSB_CRC32_EEPROM_HW ( eepromptr_t  origDataptr,
crcbytenum_t  numBytes,
eeprom_uint32ptr_t  pchecksum 
)

Compute 32-bit CRC for EEPROM address range using hardware CRC module.

This function returns the 32-bit CRC of the specified EEPROM address range.

Parameters
origDataptrAddress of EEPROM location to start CRC computation at.
numBytesNumber of bytes of the data.
pchecksumPointer to the checksum stored in EEPROM.
Note
No sanity checking of addresses is done.

References CLASSB_EEMAP_BEGIN, CLASSB_EEMAP_END, CLASSB_ERROR_HANDLER_CRC, CRC32_INITIAL_REMAINDER, CRC_32BIT, crc_io_checksum(), and crc_set_initial_value().

uint32_t CLASSB_CRC32_Flash_HW ( NVM_CMD_t  crc_type,
flashptr_t  origDataptr,
crcbytenum_t  numBytes,
eeprom_uint32ptr_t  pchecksum 
)

Compute 32-bit CRC for Flash address range using hardware CRC module.

This function returns the 32-bit CRC of the specified Flash address range.

Parameters
crc_typeType of CRC computation: CRC_FLASH_RANGE, CRC_BOOT or CRC_APP.
origDataptrAddress of Flash location to start CRC computation at.
numBytesNumber of bytes of the data.
pchecksumPointer to the checksum stored in EEPROM.
Note
No sanity checking of addresses is done.

References CLASSB_EEMAP_BEGIN, CLASSB_EEMAP_END, CLASSB_ERROR_HANDLER_CRC, CRC32_INITIAL_REMAINDER, crc_flash_checksum(), and crc_set_initial_value().

void crc16_append_value ( uint16_t  value,
void *  ptr 
)

Appends a CRC value to the given pointer.

Taking the checksum of the value and the appended data will result in zero.

Note
At least two bytes must be reserved at the given pointer location.
Parameters
valueCRC value to append
ptrPointer to 2 byte memory location to store value at.

Taking the checksum of the value and the appended data will result in zero.

Note
At least two bytes must be reserved at the given pointer location.
Parameters
valueCRC value to append
ptrpointer to 2 byte memory location to store value.
static uint16_t crc16_checksum_read ( void  )
inlinestatic

Read CRC-16 checksum.

This function returns the computed CRC-16 checksum.

Returns
checksum checksum value

Referenced by crc_checksum_read().

void crc32_append_value ( uint32_t  value,
void *  ptr 
)

Get and append data to generate checksum zero with CRC32.

This function calculates checksum value to append to the data after generating its checksum. The value is written to given pointer as little endian. Taking the checksum of the data appended will result in checksum zero.

Note
At least four bytes must be reserved at the given pointer location.
Parameters
valueThe value to convert to append value
ptrPointer to 4 byte memory location to store the value.

This function calculates checksum value to append to the data after generating its checksum. The value is written to given pointer as little endian. Taking the checksum of the data appended will result in checksum zero.

Note
At least four bytes must be reserved at the given pointer location.
Parameters
valuethe value to convert to append value
ptrpointer to 4 byte memory location to store the value.
static uint32_t crc32_checksum_read ( void  )
inlinestatic

Read CRC-32 checksum.

This function extracts the CRC-32 checksum from the registers.

Note
In IEEE 802.3 CRC-32 the checksum is bit reversed and complemented.
Returns
checksum checksum value

Referenced by crc_checksum_read().

static void crc_32_enable ( void  )
inlinestatic

Enable CRC-32.

This function will set the CRC-32 polynomial to be used to generate the checksum.

Note
This cannot be changed while the busy flag is set.

Referenced by crc_flash_checksum(), and crc_io_checksum_byte_start().

static uint32_t crc_checksum_complete ( void  )
inlinestatic

Complete CRC calculation by reading checksum and disabling module.

Returns
checksum checksum value

References crc_checksum_read(), and crc_disable().

Referenced by crc_flash_checksum(), and crc_io_checksum_byte_stop().

static uint32_t crc_checksum_read ( void  )
inlinestatic

Read checksum.

This function waits until the CRC conversion is complete and returns either the 32-bit or 16-bit checksum as a 32-bit value.

Note
This function must not be called before a IO CRC calculation is completed or a FLASH or DMA CRC calculation is started, as this will cause it to wait for the BUSY flag to be cleared forever.
Returns
checksum checksum value

References crc16_checksum_read(), crc32_checksum_read(), and crc_is_busy().

Referenced by crc_checksum_complete().

static void crc_disable ( void  )
inlinestatic

Disable the CRC module.

References crc_set_source().

Referenced by crc_checksum_complete().

uint32_t crc_flash_checksum ( NVM_CMD_t  crc_type,
flash_addr_t  flash_addr,
uint32_t  length 
)

Perform a CRC-32 calculation on part of or entire flash.

This function can perform a CRC-32 calculation on the entire flash memory, on only the application section, on only the boot section, or on a selectable range of the flash memory.

This function sets up the type of CRC to perform and configures the memory range to perform the CRC on, and starts the CRC via the NVM module. When the calculation is done, the function disables the CRC module and returns the checksum. For CRC on the boot or application section, start address and length are not needed.

Note
In the flash range mode, an even number of bytes is read. If the user selects a range with an odd number of bytes, an extra byte will be read, and the checksum will not correspond to the selected range.
Parameters
crc_typeWhat kind of CRC to do perform on the nvm module: CRC_FLASH_RANGE, CRC_BOOT or CRC_APP
flash_addrAddress of first byte in flash to perform CRC on
lengthNumber of bytes to perform CRC on
Returns
checksum CRC-32 checksum

Perform a CRC-32 calculation on part of or entire flash.

This function setups the type of CRC to perform and configures the memory range to perform the CRC on, and starts the CRC via the NVM module. When the calculation is done, the function disables the CRC module and returns the checksum. For CRC on the boot or application section, start address and length are not needed.

Note
In the flash range mode, an even number of bytes is read. If the user selects a range with an odd number of bytes, an extra byte will be read, and the checksum will not correspond to the selected range.
Parameters
crc_typewhat kind of CRC to do perform on the NVM module: CRC_FLASH_RANGE, CRC_BOOT or CRC_APP
flash_addraddress of first byte in flash to perform CRC on
lengthnumber of bytes to perform CRC on
Returns
checksum CRC-32 checksum
uint32_t crc_io_checksum ( void *  data,
uint16_t  len,
enum crc_16_32_t  crc_16_32 
)

Calculate the CRC checksum for data in a buffer.

This function initializes the CRC module, calculates the checksum for the selected number of bytes in a data buffer, disables the module and returns the calculated checksum.

Parameters
dataData buffer to perform CRC on
lenThe number of bytes to perform CRC on
crc_16_32Indicator for whether CRC-32 or CRC-16 shall be used
Returns
checksum checksum

This function initializes the CRC module, calculates the checksum for the selected number of bytes in a data buffer, disables the module and returns the calculated checksum.

Parameters
datadata buffer to perform CRC on
lenthe number of bytes to perform CRC on
crc_16_32enum to indicate whether CRC-32 or CRC-16 shall be used
Returns
checksum checksum
void crc_io_checksum_byte_add ( uint8_t  data)

Add one byte of data to CRC performed on I/O interface.

This function writes a byte to the DATAIN register. Each time this function is called, the checksum is calculated for the new data appended to all previous data written since the crc_io_checksum_byte_start was called. A new checksum is ready one clock cycle after the DATAIN register is written.

Parameters
dataByte to perform CRC on

This function writes a byte to the DATAIN register. Each time this function is called, the checksum is calculated for the new data appended to all previous data written since the crc_io_checksum_byte_start was called. A new checksum is ready one clock cycle after the DATAIN register is written.

Parameters
datadata to perform CRC on
void crc_io_checksum_byte_start ( enum crc_16_32_t  crc_16_32)

Reset CRC module and set source to I/O interface.

This function initializes the CRC module, and crc_io_checksum_byte_add can be used to add bytes to calculate checksum for. When any number of bytes have been added, the crc_io_checksum_byte_stop can be called to disable the module and get the calculated checksum.

Parameters
crc_16_32Indicator for whether CRC-32 or CRC-16 shall be used

This function initializes the CRC module, and crc_io_checksum_byte_add can be used to add bytes to calculate checksum for. When any number of bytes have been added, the crc_io_checksum_byte_stop can be called to disable the module and get the calculated checksum.

Parameters
crc_16_32enum to indicate whether CRC-32 or CRC-16 shall be used
uint32_t crc_io_checksum_byte_stop ( void  )

Disable CRC module and return checksum.

This function stops the CRC calculation for bytes added with the crc_io_checksum_byte_add and returns the calculated checksum.

Return values
checksumCalculated checksum

This function stops the CRC calculation for bytes added with the crc_io_checksum_byte_add and returns the calculated checksum.

Return values
checksumchecksum
static bool crc_is_busy ( void  )
inlinestatic

Check if the CRC module is busy.

This function can be used to see if a CRC checksum generation is completed.

Return values
trueif module is busy
falseif module is not busy

Referenced by crc_checksum_read().

static void crc_reset ( void  )
inlinestatic

Reset CRC module and set CHECKSUM to initial value.

The CRC registers will be reset one peripheral clock cycle after the RESET[1] bit is set. The initial value is reset to 0 after loading it into the CHECKSUM registers.

References crc_initial_value.

Referenced by crc_flash_checksum(), and crc_io_checksum_byte_start().

void crc_set_initial_value ( uint32_t  value)

Set initial CRC value for next CRC calculation.

Note
Value is reset to 0 when CRC calculation completes.
Parameters
valueInitial CRC value
static void crc_set_source ( CRC_SOURCE_t  source)
inlinestatic

Set data source for the CRC module after reset.

The selected source is locked until either the CRC generation is completed or the CRC module is reset. CRC generation complete is generated and signalled from the selected source when used with the DMA controller or flash memory. To disable the CRC module, use CRC_SOURCE_DISABLE_gc as source.

Parameters
sourceThe data source for the CRC module

References source.

Referenced by crc_disable(), crc_flash_checksum(), and crc_io_checksum_byte_start().

void nvm_issue_flash_range_crc ( flash_addr_t  start_addr,
flash_addr_t  end_addr 
)

Issue flash range CRC command.

This function sets the FLASH range CRC command in the NVM.CMD register. It then loads the start and end byte address of the part of FLASH to generate a CRC-32 for into the ADDR and DATA registers and finally performs the execute command.

Note
Should only be called from the CRC module. The function saves and restores the NVM.CMD register, but if this function is called from an interrupt, interrupts must be disabled before this function is called.
Parameters
start_addrend byte address
end_addrstart byte address

References ccp_write_io().

Referenced by crc_flash_checksum().

uint32_t crc_initial_value = 0

Initial CRC value to use on next CRC calculation.

Referenced by crc_reset(), and crc_set_initial_value().