crc16 1.0
CRC calcualtion library
Loading...
Searching...
No Matches
crc16.h File Reference

Header file with declarations and macros for CRC16 checksum calculation. More...

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define CRC16_INITIAL_VALUE   0x0000
 Default initial value for CRC16 calculation.
#define CRC16_POLYNOM   0x8005
 Default polynomial used for CRC16 calculation.

Functions

void crc16_init (unsigned int value)
 Initializes the CRC16 calculation with a specified starting value.
void crc16_update (unsigned char data)
 Updates the CRC16 value with a single data byte.
void crc16_calculate (unsigned char initial, unsigned char *data, unsigned char length)
 Calculates a complete CRC16 checksum over a data buffer.
unsigned int crc16_result (void)
 Returns the current CRC16 result.
void crc16_result_array (unsigned char *crc)
 Stores the CRC16 result as a two-byte array.

Detailed Description

Header file with declarations and macros for CRC16 checksum calculation.

This file provides function prototypes, type definitions, and constants for calculating and handling 16-bit CRC checksums. It supports configurable polynomial and initial values to adapt to different CRC16 implementations.

Available functions include initialization, incremental updates with data bytes, and retrieval of the final CRC result as integer or byte array.

Author
g.raf
Date
2026-01-25
Version
1.0 Release
Note
This file is part of a larger utility library and subject to the license specified in the repository. For updates and the complete revision history, see the GitHub repository.
See also
https://github.com/0x007e/utils-crc "CRC GitHub Repository"

Macro Definition Documentation

◆ CRC16_INITIAL_VALUE

#define CRC16_INITIAL_VALUE   0x0000

Default initial value for CRC16 calculation.

This constant specifies the starting value for the CRC16 shift register before processing any data. The default value 0x0000 is commonly used in standard CRC16 implementations, but other initial values (e.g. 0xFFFF) may be required for specific protocols or checksum variants.

Note
You can override this value before including this header to use a different initial CRC value.
See also
CRC16_POLYNOM

◆ CRC16_POLYNOM

#define CRC16_POLYNOM   0x8005

Default polynomial used for CRC16 calculation.

This constant defines the generator polynomial for the CRC16 algorithm. The default value 0x8005 corresponds to the reversed polynomial (x^{16} + x^{15} + x^2 + 1), which is commonly used in standard CRC16 implementations.

You can override this value before including this header to use a custom polynomial.

Note
The polynomial is represented without the leading coefficient bit.
See also
CRC16_INITIAL_VALUE

Function Documentation

◆ crc16_calculate()

void crc16_calculate ( unsigned char initial,
unsigned char * data,
unsigned char length )

Calculates a complete CRC16 checksum over a data buffer.

This function initializes the CRC register with the given start value and processes each byte in the provided data buffer using the CRC16 update routine. It performs a full CRC16 computation over a contiguous block of data.

Parameters
initialThe initial CRC16 value (e.g. CRC16_INITIAL_VALUE or a protocol-specific seed).
dataPointer to the data buffer to compute the CRC for.
lengthNumber of bytes in the data buffer.
Note
This function combines crc16_init() and repeated calls to crc16_update() for each byte. After completion, the CRC result can be retrieved with crc16_result() or crc16_result_array().
See also
crc16_init(), crc16_update(), crc16_result(), crc16_result_array()
Here is the call graph for this function:

◆ crc16_init()

void crc16_init ( unsigned int value)

Initializes the CRC16 calculation with a specified starting value.

This function sets the internal CRC register to the given initial value. It is typically called before starting a new CRC calculation sequence.

Parameters
valueThe initial value to load into the CRC register (e.g. CRC16_INITIAL_VALUE or a custom seed).
Note
This function must be called before any calls to crc16_update().
See also
crc16_update(), crc16_result(), CRC16_INITIAL_VALUE
Here is the caller graph for this function:

◆ crc16_result()

unsigned int crc16_result ( void )

Returns the current CRC16 result.

This function returns the 16-bit value currently stored in the internal CRC register. It can be called after one or more calls to crc16_update() or after a complete block calculation using crc16_calculate().

Returns
The 16-bit CRC result as an unsigned integer.
Note
The result reflects the current CRC state and is not reset automatically. To start a new calculation, call crc16_init() again.
See also
crc16_calculate(), crc16_result_array(), crc16_init()
Here is the caller graph for this function:

◆ crc16_result_array()

void crc16_result_array ( unsigned char * crc)

Stores the CRC16 result as a two-byte array.

This function retrieves the current 16-bit CRC value (via crc16_result()) and writes it into a two-byte array in little-endian order — the least significant byte first, followed by the most significant byte.

Parameters
crcPointer to a two-byte buffer that receives the CRC16 result. The array must have space for at least two bytes.
Note
Useful when communicating CRC values as byte streams over serial interfaces or storing them in data packets. The byte order follows the convention used in common embedded CRC protocols.
See also
crc16_result(), crc16_calculate(), crc16_update()
Here is the call graph for this function:

◆ crc16_update()

void crc16_update ( unsigned char data)

Updates the CRC16 value with a single data byte.

This function processes one byte of data and updates the internal CRC register according to the CRC16 algorithm defined by CRC16_POLYNOM. The implementation follows the method described in the Microchip application note: Atmel-8936 – CryptoAuth Data Zone CRC Calculation.

Each bit of the input byte is processed sequentially. If the current data bit differs from the most significant bit of the CRC register, the polynomial is applied via XOR to update the remainder.

Parameters
dataThe 8-bit data value to include in the CRC16 computation.
Note
Call crc16_init() before using this function to ensure a valid initial state. Subsequent calls to this function extend the CRC over multi-byte data.
See also
crc16_init(), crc16_result(), CRC16_POLYNOM
Here is the caller graph for this function: