MCUXpresso SDK API Reference Manual  Rev. 0
NXP Semiconductors
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
CRC: Cyclic Redundancy Check Driver

Overview

The MCUXpresso SDK provides a Peripheral driver for the Cyclic Redundancy Check (CRC) module of MCUXpresso SDK devices.

The cyclic redundancy check (CRC) module generates 16/32-bit CRC code for error detection. The CRC module also provides a programmable polynomial, seed, and other parameters required to implement a 16-bit or 32-bit CRC standard.

CRC Driver Initialization and Configuration

CRC_Init() function enables the clock gate for the CRC module in the SIM module and fully (re-)configures the CRC module according to the configuration structure. The seed member of the configuration structure is the initial checksum for which new data can be added to. When starting a new checksum computation, the seed is set to the initial checksum per the CRC protocol specification. For continued checksum operation, the seed is set to the intermediate checksum value as obtained from previous calls to CRC_Get16bitResult() or CRC_Get32bitResult() function. After calling the CRC_Init(), one or multiple CRC_WriteData() calls follow to update the checksum with data and CRC_Get16bitResult() or CRC_Get32bitResult() follow to read the result. The crcResult member of the configuration structure determines whether the CRC_Get16bitResult() or CRC_Get32bitResult() return value is a final checksum or an intermediate checksum. The CRC_Init() function can be called as many times as required allowing for runtime changes of the CRC protocol.

CRC_GetDefaultConfig() function can be used to set the module configuration structure with parameters for CRC-16/CCIT-FALSE protocol.

CRC Write Data

The CRC_WriteData() function adds data to the CRC. Internally, it tries to use 32-bit reads and writes for all aligned data in the user buffer and 8-bit reads and writes for all unaligned data in the user buffer. This function can update the CRC with user-supplied data chunks of an arbitrary size, so one can update the CRC byte by byte or with all bytes at once. Prior to calling the CRC configuration function CRC_Init() fully specifies the CRC module configuration for the CRC_WriteData() call.

CRC Get Checksum

The CRC_Get16bitResult() or CRC_Get32bitResult() function reads the CRC module data register. Depending on the prior CRC module usage, the return value is either an intermediate checksum or the final checksum. For example, for 16-bit CRCs the following call sequences can be used.

CRC_Init() / CRC_WriteData() / CRC_Get16bitResult() to get the final checksum.

CRC_Init() / CRC_WriteData() / ... / CRC_WriteData() / CRC_Get16bitResult() to get the final checksum.

CRC_Init() / CRC_WriteData() / CRC_Get16bitResult() to get an intermediate checksum.

CRC_Init() / CRC_WriteData() / ... / CRC_WriteData() / CRC_Get16bitResult() to get an intermediate checksum.

Comments about API usage in RTOS

If multiple RTOS tasks share the CRC module to compute checksums with different data and/or protocols, the following needs to be implemented by the user.

The triplets

CRC_Init() / CRC_WriteData() / CRC_Get16bitResult() or CRC_Get32bitResult()

The triplets are protected by the RTOS mutex to protect the CRC module against concurrent accesses from different tasks. This is an example.

CRC_Module_RTOS_Mutex_Lock;
CRC_Module_RTOS_Mutex_Unlock;

Comments about API usage in interrupt handler

All APIs can be used from an interrupt handler although an interrupt latency of equal and lower priority interrupts increases. The user must protect against concurrent accesses from different interrupt handlers and/or tasks.

CRC Driver Examples

Simple examples

This is an example with the default CRC-16/CCIT-FALSE protocol.

crc_config_t config;
CRC_Type *base;
uint8_t data[] = {0x00, 0x01, 0x02, 0x03, 0x04};
uint16_t checksum;
base = CRC0;
CRC_GetDefaultConfig(base, &config); /* default gives CRC-16/CCIT-FALSE */
CRC_Init(base, &config);
CRC_WriteData(base, data, sizeof(data));
checksum = CRC_Get16bitResult(base);

This is an example with the CRC-32 protocol configuration.

crc_config_t config;
uint32_t checksum;
config.polynomial = 0x04C11DB7u;
config.seed = 0xFFFFFFFFu;
config.reflectIn = true;
config.reflectOut = true;
config.complementChecksum = true;
CRC_Init(base, &config);
/* example: update by 1 byte at time */
while (dataSize)
{
uint8_t c = GetCharacter();
CRC_WriteData(base, &c, 1);
dataSize--;
}
checksum = CRC_Get32bitResult(base);

Advanced examples

Assuming there are three tasks/threads, each using the CRC module to compute checksums of a different protocol, with context switches.

First, prepare the three CRC module initialization functions for three different protocols CRC-16 (ARC), CRC-16/CCIT-FALSE, and CRC-32. The table below lists the individual protocol specifications. See also http://reveng.sourceforge.net/crc-catalogue/.

CRC-16/CCIT-FALSE CRC-16 CRC-32
Width 16 bits 16 bits 32 bits
Polynomial 0x1021 0x8005 0x04C11DB7
Initial seed 0xFFFF 0x0000 0xFFFFFFFF
Complement checksum No No Yes
Reflect In No Yes Yes
Reflect Out No Yes Yes

These are the corresponding initialization functions.

void InitCrc16_CCIT(CRC_Type *base, uint32_t seed, bool isLast)
{
crc_config_t config;
config.polynomial = 0x1021;
config.seed = seed;
config.reflectIn = false;
config.reflectOut = false;
config.complementChecksum = false;
config.crcBits = kCrcBits16;
CRC_Init(base, &config);
}
void InitCrc16(CRC_Type *base, uint32_t seed, bool isLast)
{
crc_config_t config;
config.polynomial = 0x8005;
config.seed = seed;
config.reflectIn = true;
config.reflectOut = true;
config.complementChecksum = false;
config.crcBits = kCrcBits16;
CRC_Init(base, &config);
}
void InitCrc32(CRC_Type *base, uint32_t seed, bool isLast)
{
crc_config_t config;
config.polynomial = 0x04C11DB7U;
config.seed = seed;
config.reflectIn = true;
config.reflectOut = true;
config.complementChecksum = true;
config.crcBits = kCrcBits32;
CRC_Init(base, &config);
}

The following context switches show a possible API usage.

uint16_t checksumCrc16;
uint32_t checksumCrc32;
uint16_t checksumCrc16Ccit;
checksumCrc16 = 0x0;
checksumCrc32 = 0xFFFFFFFFU;
checksumCrc16Ccit = 0xFFFFU;
/* Task A bytes[0-3] */
InitCrc16(base, checksumCrc16, false);
CRC_WriteData(base, &data[0], 4);
checksumCrc16 = CRC_Get16bitResult(base);
/* Task B bytes[0-3] */
InitCrc16_CCIT(base, checksumCrc16Ccit, false);
CRC_WriteData(base, &data[0], 4);
checksumCrc16Ccit = CRC_Get16bitResult(base);
/* Task C 4 bytes[0-3] */
InitCrc32(base, checksumCrc32, false);
CRC_WriteData(base, &data[0], 4);
checksumCrc32 = CRC_Get32bitResult(base);
/* Task B add final 5 bytes[4-8] */
InitCrc16_CCIT(base, checksumCrc16Ccit, true);
CRC_WriteData(base, &data[4], 5);
checksumCrc16Ccit = CRC_Get16bitResult(base);
/* Task C 3 bytes[4-6] */
InitCrc32(base, checksumCrc32, false);
CRC_WriteData(base, &data[4], 3);
checksumCrc32 = CRC_Get32bitResult(base);
/* Task A 3 bytes[4-6] */
InitCrc16(base, checksumCrc16, false);
CRC_WriteData(base, &data[4], 3);
checksumCrc16 = CRC_Get16bitResult(base);
/* Task C add final 2 bytes[7-8] */
InitCrc32(base, checksumCrc32, true);
CRC_WriteData(base, &data[7], 2);
checksumCrc32 = CRC_Get32bitResult(base);
/* Task A add final 2 bytes[7-8] */
InitCrc16(base, checksumCrc16, true);
CRC_WriteData(base, &data[7], 2);
checksumCrc16 = CRC_Get16bitResult(base);

Data Structures

struct  crc_config_t
 CRC protocol configuration. More...
 

Macros

#define CRC_DRIVER_USE_CRC16_CCIT_FALSE_AS_DEFAULT   1
 Default configuration structure filled by CRC_GetDefaultConfig(). More...
 

Enumerations

enum  crc_bits_t {
  kCrcBits16 = 0U,
  kCrcBits32 = 1U
}
 CRC bit width. More...
 
enum  crc_result_t {
  kCrcFinalChecksum = 0U,
  kCrcIntermediateChecksum = 1U
}
 CRC result type. More...
 

Functions

void CRC_Init (CRC_Type *base, const crc_config_t *config)
 Enables and configures the CRC peripheral module. More...
 
static void CRC_Deinit (CRC_Type *base)
 Disables the CRC peripheral module. More...
 
void CRC_GetDefaultConfig (crc_config_t *config)
 Loads default values to the CRC protocol configuration structure. More...
 
void CRC_WriteData (CRC_Type *base, const uint8_t *data, size_t dataSize)
 Writes data to the CRC module. More...
 
uint32_t CRC_Get32bitResult (CRC_Type *base)
 Reads the 32-bit checksum from the CRC module. More...
 
uint16_t CRC_Get16bitResult (CRC_Type *base)
 Reads a 16-bit checksum from the CRC module. More...
 

Driver version

#define FSL_CRC_DRIVER_VERSION   (MAKE_VERSION(2, 0, 1))
 CRC driver version. More...
 

Data Structure Documentation

struct crc_config_t

This structure holds the configuration for the CRC protocol.

Data Fields

uint32_t polynomial
 CRC Polynomial, MSBit first. More...
 
uint32_t seed
 Starting checksum value.
 
bool reflectIn
 Reflect bits on input. More...
 
bool reflectOut
 Reflect bits on output. More...
 
bool complementChecksum
 True if the result shall be complement of the actual checksum. More...
 
crc_bits_t crcBits
 Selects 16- or 32- bit CRC protocol. More...
 
crc_result_t crcResult
 Selects final or intermediate checksum return from CRC_Get16bitResult() or CRC_Get32bitResult()
 

Field Documentation

uint32_t crc_config_t::polynomial

Example polynomial: 0x1021 = 1_0000_0010_0001 = x^12+x^5+1

bool crc_config_t::reflectIn
bool crc_config_t::reflectOut
bool crc_config_t::complementChecksum
crc_bits_t crc_config_t::crcBits

Macro Definition Documentation

#define FSL_CRC_DRIVER_VERSION   (MAKE_VERSION(2, 0, 1))

Version 2.0.1.

Current version: 2.0.1

Change log:

  • Version 2.0.1
    • move DATA and DATALL macro definition from header file to source file
#define CRC_DRIVER_USE_CRC16_CCIT_FALSE_AS_DEFAULT   1

Use CRC16-CCIT-FALSE as defeault.

Enumeration Type Documentation

enum crc_bits_t
Enumerator
kCrcBits16 

Generate 16-bit CRC code.

kCrcBits32 

Generate 32-bit CRC code.

Enumerator
kCrcFinalChecksum 

CRC data register read value is the final checksum.

Reflect out and final xor protocol features are applied.

kCrcIntermediateChecksum 

CRC data register read value is intermediate checksum (raw value).

Reflect out and final xor protocol feature are not applied. Intermediate checksum can be used as a seed for CRC_Init() to continue adding data to this checksum.

Function Documentation

void CRC_Init ( CRC_Type *  base,
const crc_config_t config 
)

This function enables the clock gate in the SIM module for the CRC peripheral. It also configures the CRC module and starts a checksum computation by writing the seed.

Parameters
baseCRC peripheral address.
configCRC module configuration structure.
static void CRC_Deinit ( CRC_Type *  base)
inlinestatic

This function disables the clock gate in the SIM module for the CRC peripheral.

Parameters
baseCRC peripheral address.
void CRC_GetDefaultConfig ( crc_config_t config)

Loads default values to the CRC protocol configuration structure. The default values are as follows.

* config->polynomial = 0x1021;
* config->seed = 0xFFFF;
* config->reflectIn = false;
* config->reflectOut = false;
* config->complementChecksum = false;
* config->crcBits = kCrcBits16;
*
Parameters
configCRC protocol configuration structure.
void CRC_WriteData ( CRC_Type *  base,
const uint8_t *  data,
size_t  dataSize 
)

Writes input data buffer bytes to the CRC data register. The configured type of transpose is applied.

Parameters
baseCRC peripheral address.
dataInput data stream, MSByte in data[0].
dataSizeSize in bytes of the input data buffer.
uint32_t CRC_Get32bitResult ( CRC_Type *  base)

Reads the CRC data register (either an intermediate or the final checksum). The configured type of transpose and complement is applied.

Parameters
baseCRC peripheral address.
Returns
An intermediate or the final 32-bit checksum, after configured transpose and complement operations.
uint16_t CRC_Get16bitResult ( CRC_Type *  base)

Reads the CRC data register (either an intermediate or the final checksum). The configured type of transpose and complement is applied.

Parameters
baseCRC peripheral address.
Returns
An intermediate or the final 16-bit checksum, after configured transpose and complement operations.