Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
Crc.hxx File Reference
#include <stdint.h>
#include <stddef.h>

Go to the source code of this file.

Classes

class  Crc8DallasMaxim
 Helper class for computing CRC-8 according to Dallas/Maxim specification for 1-wire protocol. More...
 
class  Crc16CCITT
 Helper class for computing CRC-16 according to the CCITT specification (polynomial = 0x1021, x^16 + x^12 + x^5 + 1, initial value = 0xFFFF). More...
 

Macros

#define CRC8DALLAS_TABLE_SIZE   16
 Use the smaller (slower) table by default.
 
#define CRC16CCITT_TABLE_SIZE   256
 Use the larger (faster) table by default.
 

Functions

uint16_t crc_16_ibm (const void *data, size_t length_bytes)
 Computes the 16-bit CRC value over data using the CRC16-ANSI (aka CRC16-IBM) settings.
 
void crc3_crc16_ibm (const void *data, size_t length_bytes, uint16_t *checksum)
 Computes the triple-CRC value over a chunk of data.
 
static void crc3_crc16_ccitt (const void *data, size_t length_bytes, uint16_t checksum[3])
 Computes the triple-CRC value over a chunk of data.
 

Detailed Description

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Helper functions for computing checksums.

Author
Balazs Racz
Date
16 Dec 2014

Definition in file Crc.hxx.

Macro Definition Documentation

◆ CRC16CCITT_TABLE_SIZE

#define CRC16CCITT_TABLE_SIZE   256

Use the larger (faster) table by default.

Definition at line 47 of file Crc.hxx.

◆ CRC8DALLAS_TABLE_SIZE

#define CRC8DALLAS_TABLE_SIZE   16

Use the smaller (slower) table by default.

Definition at line 43 of file Crc.hxx.

Function Documentation

◆ crc3_crc16_ccitt()

static void crc3_crc16_ccitt ( const void *  data,
size_t  length_bytes,
uint16_t  checksum[3] 
)
inlinestatic

Computes the triple-CRC value over a chunk of data.

checksum is an array of 3 halfwords. The first halfword will get the CRC of the data array, the second halfword the CRC of all even index bytes (starting with the first byte, index 0), the third halfword will get the CRC of all odd index bytes (starting with the the second byte, index 1).

This implementation has no requirements for starting alignment or size modulus. It will work on any number of bytes.

Parameters
datawhat to compute the checksum over
length_byteshow long data is
checksumis the output buffer where to store the 48-bit checksum.

Definition at line 325 of file Crc.hxx.

◆ crc3_crc16_ibm()

void crc3_crc16_ibm ( const void *  data,
size_t  length_bytes,
uint16_t *  checksum 
)

Computes the triple-CRC value over a chunk of data.

checksum is an array of 3 halfwords. The first halfword will get the CRC of the data array, the second halfword the CRC of all odd bytes (starting with the first byte), the third halfword will get the CRC of all even bytes.

This routine is helpful, because a similar routine is part of the TIVA microcontroller ROM, thus needs no implementation on an actual part. It is important to note that the TIVA version takes the length in 4-byte words and allows only using data length divisible by 4.

Parameters
datawhat to compute the checksum over
length_byteshow long data is
checksumis the output buffer where to store the 48-bit checksum.

Definition at line 167 of file Crc.cxx.

◆ crc_16_ibm()

uint16_t crc_16_ibm ( const void *  data,
size_t  length_bytes 
)

Computes the 16-bit CRC value over data using the CRC16-ANSI (aka CRC16-IBM) settings.

This involves zero init value, zero terminating value, reversed polynomial 0xA001, reversing input bits and reversing output bits. The example CRC value of "123456789" is 0xbb3d.

Parameters
datawhat to compute the checksum over
length_byteshow long data is
Returns
the CRC-16-IBM value of the checksummed data.

Definition at line 156 of file Crc.cxx.