CRC (Cyclic Redundancy Check)¶
-
group
group_hal_crc
High level interface for interacting with the CRC, which provides hardware accelerated CRC computations.
The CRC APIs are structured to enable usage in situations where the entire input data set is not available in memory at the same time. Therefore, each conversion consists of three steps:
A single call to cyhal_crc_start, to initialize data structures used to compute CRC
One or more calls to cyhal_crc_compute, to provide chunks of data
A single call to cyhal_crc_finish, to finalize the computation and retrieve the result
The table below provides CRC parameters for some common CRC algorithms.
note
The Expected CRC column shows the computed CRC when the value "123456789" is passed to cyhal_crc_compute.
CRC algorithm Name
Len
Polynomial
Initial seed
Data REV
Data XOR
Rem REV
Remainder XOR
Expected CRC
CRC-6 / CDMA2000-A
6
0x27
0x3F
false
0
false
0x00
0x0D
CRC-6 / CDMA2000-B
6
0x07
0x3F
false
0
false
0x00
0x3B
CRC-6 / DARC
6
0x19
0x00
true
0
true
0x00
0x26
CRC-6 / ITU
6
0x03
0x00
true
0
true
0x00
0x06
CRC-8 / ITU
8
0x07
0x00
false
0
false
0x55
0xA1
CRC-8 / MAXIM
8
0x31
0x00
true
0
true
0x00
0xA1
CRC-8 / ROHC
8
0x07
0xFF
true
0
true
0x00
0xD0
CRC-8 / WCDMA
8
0x9B
0x00
true
0
true
0x00
0x25
CRC-16 / CCITT-0
16
0x1021
0xFFFF
false
0
false
0x0000
0x29B1
CRC-16 / CDMA2000
16
0xC867
0xFFFF
false
0
false
0x0000
0x4C06
CRC-32
32
0x04C11DB7
0xFFFFFFFF
true
0
true
0xFFFFFFFF
0xCBF43926
CRC-32 / BZIP2
32
0x04C11DB7
0xFFFFFFFF
false
0
false
0xFFFFFFFF
0xFC891918
note
Algorithms that have less than 8 bits, like CRC-6, populate the lower bits and leave the high order bits 0.
See crc_algorithm_t and Snippet1: CRC Generation for more details.note
Many of the algorithm parameters can be customized.
Quick Start
cyhal_crc_init initializes the CRC generator and passes the pointer to the CRC block through the obj object of type cyhal_crc_t.
Snippet1: CRC Generation
The following snippet initializes a CRC generator and computes the CRC for a sample message.
// Using CRC-16/CCITT-0 Algorithm #define CRC_BITLEN (16u) #define CRC_POLYNOMIAL (0x1021u) #define CRC_LFSR_SEED (0xffffu) #define CRC_DATA_REVERSE (0u) #define CRC_DATA_XOR (0u) #define CRC_REM_REVERSE (0u) #define CRC_REM_XOR (0x0000u) #define MSG_LENGTH 9 CY_ALIGN(4) uint8_t message[MSG_LENGTH] = "123456789"; CY_ALIGN(4) uint32_t calc_crc = 0; // Set CRC algorithm parameters crc_algorithm_t my_algorithm = { .width = CRC_BITLEN, .polynomial = CRC_POLYNOMIAL, .lfsrInitState = CRC_LFSR_SEED, .dataReverse = CRC_DATA_REVERSE, .dataXor = CRC_DATA_XOR, .remReverse = CRC_REM_REVERSE, .remXor = CRC_REM_XOR }; cy_rslt_t rslt; cyhal_crc_t crc_obj; // Initialize the CRC Generator rslt = cyhal_crc_init(&crc_obj); // Initialize the CRC algorithm parameters rslt = cyhal_crc_start(&crc_obj, &my_algorithm); // Compute CRC rslt = cyhal_crc_compute(&crc_obj, message, MSG_LENGTH); // Finish computation rslt = cyhal_crc_finish(&crc_obj, &calc_crc); // Additional computations can be done here if needed // Release the CRC generator // Note: Free only if not required anymore cyhal_crc_free(&crc_obj);
Functions
-
cy_rslt_t
cyhal_crc_init
(cyhal_crc_t *obj)¶ Initialize the CRC generator.
This function reserves the CRYPTO block for CRC calculations.
Returns
CY_RSLT_SUCCESS if the operation was successful. Refer Snippet1: CRC Generation for more information.- Return
The status of the init request.
- Parameters
[out] obj
: Pointer to a CRC generator object. The caller must allocate the memory for this object but the init function will initialize its contents.
-
void
cyhal_crc_free
(cyhal_crc_t *obj)¶ Release the CRC generator.
- Parameters
[inout] obj
: The CRC generator object
-
cy_rslt_t
cyhal_crc_start
(cyhal_crc_t *obj, const crc_algorithm_t *algorithm)¶ The CRC block is setup to perform CRC computation.
Returns CY_RSLT_SUCCESS if the operation was successful.note
The state of the CRC Block will be reset to the state provided by in the argument algorithm.
- Return
The status of the compute request
- Parameters
[inout] obj
: The CRC generator object[in] algorithm
: The CRC algorithm to use for computations Refer crc_algorithm_t.
-
cy_rslt_t
cyhal_crc_compute
(const cyhal_crc_t *obj, const uint8_t *data, size_t length)¶ Computes the CRC for the given data and accumulates the CRC with the CRC generated from previous calls.
This function can be called multiple times to provide additional data.
note
Input data must be 4-byte aligned. Refer Snippet1: CRC Generation for more details.
Returns
CY_RSLT_SUCCESS if the operation was successful.- Return
The status of the compute request
- Parameters
[in] obj
: The CRC generator object[in] data
: The input data[in] length
: The number of bytes in the data
-
cy_rslt_t
cyhal_crc_finish
(const cyhal_crc_t *obj, uint32_t *crc)¶ Finalizes the CRC computation and returns the CRC for the complete set of data passed through a single call or multiple calls to cyhal_crc_compute.
note
The computed CRC pointer provided must be 4 byte aligned. Refer to Snippet1: CRC Generation for more details.
note
If the length of the CRC is less than a full word, the result will be in the lower bits allowing the result to be downcast if desired.
Returns
CY_RSLT_SUCCESS if the operation was successful.- Return
The status of the compute request
- Parameters
[in] obj
: The CRC generator object[out] crc
: The computed CRC
-
struct
crc_algorithm_t
¶ - #include <cyhal_crc.h>
CRC algorithm parameters.
Public Members
-
uint32_t
width
¶ Bit width of the CRC.
-
uint32_t
polynomial
¶ The polynomial to use.
-
uint32_t
lfsrInitState
¶ Initial state of the LFSR.
-
uint32_t
dataXor
¶ Byte mask to xor with the data.
-
uint32_t
remXor
¶ Mask to xor with the remainder.
-
bool
dataReverse
¶ The order in which data should be processed.
If 0, data is processed MSB first. If 1, data is processed LSB first.
-
bool
remReverse
¶ If 1, the remainder is reversed. If 0, it is not.
-
uint32_t