Microchip® Advanced Software Framework

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

CRC computations implemented in software.

The CPU reads the data and computes the CRC checksum. There are two available methods:

The CRC32-polynomial is in the reflected form (0xEDB88320) in the software implementation. The initial remainder is 0xFFFFFFFF, and the generated checksum is bit-reversed and complemented (in compliance with IEE802.3). The CCITT polynomial used is 0x1021, with 0x0000 as initial remainder. In this case the checksum is neither bit reversed nor complemented.

uint16_t PROGMEM_DECLARE (CLASSB_CRC16Table[256])
 Table for CCITT 16-bit CRC, stored in Flash. More...
 
uint32_t PROGMEM_DECLARE (CLASSB_CRC32Table[256])
 Table for IEE802.3 32-bit CRC, stored in Flash. More...
 

Configuration settings for CRC software implementation

Symbols used to choose between lookup table or direct computation.

They should be defined for lookup table implementation and commented out otherwise.

#define CRC_USE_16BIT_LOOKUP_TABLE
 Select the lookup table method for 16-bit CRC. More...
 
#define CRC_USE_32BIT_LOOKUP_TABLE
 Select the lookup table method for 32-bit CRC. More...
 

Constants for internal use

This options make sure that the CRC computation complies with the standard 16-bit CCITT CRC and IEE802.3 32-bit CRC.

Attention
These should not be modified by user!
#define CRC16_POLYNOMIAL   (uint16_t)0x1021
 Polynomial used for computation of CRC16 (software, not lookup table) More...
 
#define CRC32_POLYNOMIAL   (uint32_t)0xEDB88320UL
 Polynomial used for computation of CRC32 (software, not lookup table) More...
 
#define CRC32_FINAL_XOR_VALUE   (uint32_t)0xFFFFFFFF
 Final XOR Value for CRC32. More...
 

Internal macros used for CRC computation

#define CLASSB_CRC(data, crc, poly, crcbits)
 Update CRC value for one input byte. More...
 
#define CLASSB_CRC_REFL(data, crc, poly, crcbits)
 Update CRC value for one input byte (reflected polynomial). More...
 
#define CLASSB_CRC_REFL_TABLE_32(data, crc, table)
 Update 32-bit CRC value for one input byte (reflected polynomial). More...
 
#define CLASSB_CRC_TABLE_16(data, crc, table)
 Update 16-bit CRC value for one input byte. More...
 

CRC tests

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

uint16_t CLASSB_CRC16_EEPROM_SW (eepromptr_t dataptr, const crcbytenum_t numBytes, eeprom_uint16ptr_t pchecksum)
 Compute 16-bit CRC for EEPROM address range using table lookup. More...
 
uint16_t CLASSB_CRC16_Flash_SW (flashptr_t dataptr, const crcbytenum_t numBytes, eeprom_uint16ptr_t pchecksum)
 Compute 16-bit CRC for Flash address range using table lookup. More...
 
uint32_t CLASSB_CRC32_EEPROM_SW (eepromptr_t dataptr, const crcbytenum_t numBytes, eeprom_uint32ptr_t pchecksum)
 Compute 32-bit CRC for EEPROM address range using table lookup. More...
 
uint32_t CLASSB_CRC32_Flash_SW (flashptr_t dataptr, const crcbytenum_t numBytes, eeprom_uint32ptr_t pchecksum)
 Compute 32-bit CRC for Flash address range using table lookup. More...
 

#define CLASSB_CRC (   data,
  crc,
  poly,
  crcbits 
)
Value:
do { \
/* Bring the next byte into the checksum. */ \
crc ^= ((uint32_t)data << (crcbits - 8)); \
\
uint8_t bit; \
\
/* Perform modulo-2 division. */ \
for (bit = 8; bit > 0; --bit) { \
/* ..only if MSB is set. */ \
if (crc & ((uint32_t)1 << (crcbits - 1))) { \
crc = (crc << 1) ^ poly; \
} else { \
crc <<= 1; \
} \
} \
} while (0)
static uint8_t data[SIO_RX_BUF_SIZE]
This is the buffer to hold the frame received through serial interface.
Definition: perf_api_serial_handler.c:133
if((SERIAL_RX_BUF_SIZE_NCP-1)==serial_rx_buf_tail)
Definition: sio2ncp.c:249

Update CRC value for one input byte.

Note
This macro works for both 16 and 32 bit CRC.
Parameters
dataInput data byte.
crcVariable that holds the CRC value.
polyCRC polynomial.
crcbitsNumber of CRC bits.

Referenced by CLASSB_CRC16_EEPROM_SW(), and CLASSB_CRC16_Flash_SW().

#define CLASSB_CRC_REFL (   data,
  crc,
  poly,
  crcbits 
)
Value:
do { \
/* Bring next byte into the checksum. */ \
crc ^= (uint32_t)data; \
\
uint8_t bit; \
\
/* Perform modulo-2 division. */ \
for (bit = 8; bit > 0; --bit) { \
/* ..only if LSB is set. */ \
if (crc & 0x01) { \
crc = (crc >> 1) ^ poly; \
} else { \
crc >>= 1; \
} \
} \
} while (0)
static uint8_t data[SIO_RX_BUF_SIZE]
This is the buffer to hold the frame received through serial interface.
Definition: perf_api_serial_handler.c:133
if((SERIAL_RX_BUF_SIZE_NCP-1)==serial_rx_buf_tail)
Definition: sio2ncp.c:249

Update CRC value for one input byte (reflected polynomial).

Note
This macro works for both 16 and 32 bit CRC.
Parameters
dataInput data byte.
crcVariable that holds the CRC value.
polyCRC polynomial.
crcbitsNumber of CRC bits.

Referenced by CLASSB_CRC32_EEPROM_SW(), and CLASSB_CRC32_Flash_SW().

#define CLASSB_CRC_REFL_TABLE_32 (   data,
  crc,
  table 
)
Value:
do { \
data ^= crc & 0xFF; \
crc = PROGMEM_READ_DWORD(&table[data]) ^ (crc >> 8); \
} while (0)
static uint8_t data[SIO_RX_BUF_SIZE]
This is the buffer to hold the frame received through serial interface.
Definition: perf_api_serial_handler.c:133

Update 32-bit CRC value for one input byte (reflected polynomial).

Note
This macro assumes that the CRC lookup table is located in the lower 64 kB address range of Flash.
Parameters
dataInput data byte.
crcVariable that holds the CRC value.
tableTable that contains pre-calculated CRC values.

Referenced by CLASSB_CRC32_EEPROM_SW(), and CLASSB_CRC32_Flash_SW().

#define CLASSB_CRC_TABLE_16 (   data,
  crc,
  table 
)
Value:
do { \
data ^= crc >> (16 - 8); \
crc = PROGMEM_READ_WORD(&table[data]) ^ (crc << 8); \
} while (0)
static uint8_t data[SIO_RX_BUF_SIZE]
This is the buffer to hold the frame received through serial interface.
Definition: perf_api_serial_handler.c:133
#define PROGMEM_READ_WORD(x)
Definition: progmem.h:66

Update 16-bit CRC value for one input byte.

Note
This macro assumes that the CRC lookup table is located in the lower 64 kB address range of Flash.
Parameters
dataInput data byte.
crcVariable that holds the CRC value.
tableTable that contains pre-calculated CRC values.

Referenced by CLASSB_CRC16_EEPROM_SW(), and CLASSB_CRC16_Flash_SW().

#define CRC16_POLYNOMIAL   (uint16_t)0x1021

Polynomial used for computation of CRC16 (software, not lookup table)

Referenced by CLASSB_CRC16_EEPROM_SW(), and CLASSB_CRC16_Flash_SW().

#define CRC32_FINAL_XOR_VALUE   (uint32_t)0xFFFFFFFF

Final XOR Value for CRC32.

Referenced by CLASSB_CRC32_EEPROM_SW(), and CLASSB_CRC32_Flash_SW().

#define CRC32_POLYNOMIAL   (uint32_t)0xEDB88320UL

Polynomial used for computation of CRC32 (software, not lookup table)

Referenced by CLASSB_CRC32_EEPROM_SW(), and CLASSB_CRC32_Flash_SW().

#define CRC_USE_16BIT_LOOKUP_TABLE

Select the lookup table method for 16-bit CRC.

#define CRC_USE_32BIT_LOOKUP_TABLE

Select the lookup table method for 32-bit CRC.

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

Compute 16-bit CRC for EEPROM address range using table lookup.

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_CRC, CLASSB_CRC_TABLE_16, CLASSB_EEMAP_BEGIN, CLASSB_EEMAP_END, CLASSB_ERROR_HANDLER_CRC, CRC16_INITIAL_REMAINDER, and CRC16_POLYNOMIAL.

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

Compute 16-bit CRC for Flash address range using table lookup.

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
No sanity checking of addresses is done.

References CLASSB_CRC, CLASSB_CRC_TABLE_16, CLASSB_EEMAP_BEGIN, CLASSB_EEMAP_END, CLASSB_ERROR_HANDLER_CRC, CRC16_INITIAL_REMAINDER, CRC16_POLYNOMIAL, and PROGMEM_READ_BYTE.

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

Compute 32-bit CRC for EEPROM address range using table lookup.

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_CRC_REFL, CLASSB_CRC_REFL_TABLE_32, CLASSB_EEMAP_BEGIN, CLASSB_EEMAP_END, CLASSB_ERROR_HANDLER_CRC, CRC32_FINAL_XOR_VALUE, CRC32_INITIAL_REMAINDER, and CRC32_POLYNOMIAL.

uint32_t CLASSB_CRC32_Flash_SW ( flashptr_t  origDataptr,
crcbytenum_t  numBytes,
eeprom_uint32ptr_t  pchecksum 
)

Compute 32-bit CRC for Flash address range using table lookup.

This function returns the 32-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
No sanity checking of addresses is done.

References CLASSB_CRC_REFL, CLASSB_CRC_REFL_TABLE_32, CLASSB_EEMAP_BEGIN, CLASSB_EEMAP_END, CLASSB_ERROR_HANDLER_CRC, CRC32_FINAL_XOR_VALUE, CRC32_INITIAL_REMAINDER, CRC32_POLYNOMIAL, and PROGMEM_READ_BYTE.

uint16_t PROGMEM_DECLARE ( CLASSB_CRC16Table  [256])

Table for CCITT 16-bit CRC, stored in Flash.

uint32_t PROGMEM_DECLARE ( CLASSB_CRC32Table  [256])

Table for IEE802.3 32-bit CRC, stored in Flash.