Skip to content

Commit

Permalink
fix #17 yield(), increase length, minor edits (#18)
Browse files Browse the repository at this point in the history
* fix #17 yield(), 
* increase length field to uint16_t
* add CRC_polynomes.h
* minor edits
  • Loading branch information
RobTillaart authored Feb 7, 2022
1 parent 3ffaed7 commit 836672c
Show file tree
Hide file tree
Showing 16 changed files with 145 additions and 63 deletions.
25 changes: 16 additions & 9 deletions CRC.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@
//
// FILE: CRC.h
// AUTHOR: Rob Tillaart
// VERSION: 0.2.0
// PURPOSE: Arduino library fir CRC8, CRC12, CRC16, CRC16-CCITT, CRC32
// VERSION: 0.2.1
// PURPOSE: Arduino library for CRC8, CRC12, CRC16, CRC16-CCITT, CRC32, CRC64
// URL: https://github.com/RobTillaart/CRC
//


#include "Arduino.h"

#include "CRC_polynomes.h"

#define CRC_LIB_VERSION (F("0.2.0"))
#define CRC_LIB_VERSION (F("0.2.1"))


////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -74,11 +75,12 @@ uint64_t reverse64(uint64_t in)
///////////////////////////////////////////////////////////////////////////////////

// CRC POLYNOME = x8 + x5 + x4 + 1 = 1001 1000 = 0x8C
uint8_t crc8(const uint8_t *array, uint8_t length, const uint8_t polynome = 0xD5, const uint8_t startmask = 0x00, const uint8_t endmask = 0x00, const bool reverseIn = false, const bool reverseOut = false)
uint8_t crc8(const uint8_t *array, uint16_t length, const uint8_t polynome = 0xD5, const uint8_t startmask = 0x00, const uint8_t endmask = 0x00, const bool reverseIn = false, const bool reverseOut = false)
{
uint8_t crc = startmask;
while (length--)
{
if ((length & 0xFF) == 0) yield(); // RTOS
uint8_t data = *array++;
if (reverseIn) data = reverse8(data);
crc ^= data;
Expand All @@ -102,11 +104,12 @@ uint8_t crc8(const uint8_t *array, uint8_t length, const uint8_t polynome = 0xD5


// CRC POLYNOME = x12 + x3 + x2 + 1 = 0000 1000 0000 1101 = 0x80D
uint16_t crc12(const uint8_t *array, uint8_t length, const uint16_t polynome = 0x80D, const uint16_t startmask = 0x0000, const uint16_t endmask = 0x0000, const bool reverseIn = false, const bool reverseOut = false)
uint16_t crc12(const uint8_t *array, uint16_t length, const uint16_t polynome = 0x80D, const uint16_t startmask = 0x0000, const uint16_t endmask = 0x0000, const bool reverseIn = false, const bool reverseOut = false)
{
uint16_t crc = startmask;
while (length--)
{
if ((length & 0xFF) == 0) yield(); // RTOS
uint8_t data = *array++;
if (reverseIn) data = reverse8(data);

Expand All @@ -132,11 +135,12 @@ uint16_t crc12(const uint8_t *array, uint8_t length, const uint16_t polynome = 0


// CRC POLYNOME = x15 + 1 = 1000 0000 0000 0001 = 0x8001
uint16_t crc16(const uint8_t *array, uint8_t length, const uint16_t polynome = 0x8001, const uint16_t startmask = 0x0000, const uint16_t endmask = 0x0000, const bool reverseIn = false, const bool reverseOut = false)
uint16_t crc16(const uint8_t *array, uint16_t length, const uint16_t polynome = 0x8001, const uint16_t startmask = 0x0000, const uint16_t endmask = 0x0000, const bool reverseIn = false, const bool reverseOut = false)
{
uint16_t crc = startmask;
while (length--)
{
if ((length & 0xFF) == 0) yield(); // RTOS
uint8_t data = *array++;
if (reverseIn) data = reverse8(data);
crc ^= ((uint16_t)data) << 8;
Expand All @@ -160,18 +164,19 @@ uint16_t crc16(const uint8_t *array, uint8_t length, const uint16_t polynome = 0


// CRC-CCITT POLYNOME = x13 + X5 + 1 = 0001 0000 0010 0001 = 0x1021
uint16_t crc16_CCITT(uint8_t *array, uint8_t length)
uint16_t crc16_CCITT(uint8_t *array, uint16_t length)
{
return crc16(array, length, 0x1021, 0xFFFF);
}


// CRC-32 POLYNOME = x32 + ..... + 1
uint32_t crc32(const uint8_t *array, uint8_t length, const uint32_t polynome = 0x04C11DB7, const uint32_t startmask = 0, const uint32_t endmask = 0, const bool reverseIn = false, const bool reverseOut = false)
uint32_t crc32(const uint8_t *array, uint16_t length, const uint32_t polynome = 0x04C11DB7, const uint32_t startmask = 0, const uint32_t endmask = 0, const bool reverseIn = false, const bool reverseOut = false)
{
uint32_t crc = startmask;
while (length--)
{
if ((length & 0xFF) == 0) yield(); // RTOS
uint8_t data = *array++;
if (reverseIn) data = reverse8(data);
crc ^= ((uint32_t) data) << 24;
Expand All @@ -195,11 +200,13 @@ uint32_t crc32(const uint8_t *array, uint8_t length, const uint32_t polynome = 0


// CRC-CCITT POLYNOME = x64 + ..... + 1
uint64_t crc64(const uint8_t *array, uint8_t length, const uint64_t polynome, const uint64_t startmask, const uint64_t endmask, const bool reverseIn, const bool reverseOut)
// CRC_ECMA64 = 0x42F0E1EBA9EA3693
uint64_t crc64(const uint8_t *array, uint16_t length, const uint64_t polynome = 0x42F0E1EBA9EA3693, const uint64_t startmask = 0, const uint64_t endmask = 0, const bool reverseIn = false, const bool reverseOut = false)
{
uint64_t crc = startmask;
while (length--)
{
if ((length & 0xFF) == 0) yield(); // RTOS
uint8_t data = *array++;
if (reverseIn) data = reverse8(data);
crc ^= ((uint64_t) data) << 56;
Expand Down
8 changes: 3 additions & 5 deletions CRC12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,16 @@ void CRC12::restart()
void CRC12::add(uint8_t value)
{
_count++;
if ((_count & 0xFF) == 0) yield();
_update(value);
}


void CRC12::add(const uint8_t * array, uint8_t length)
void CRC12::add(const uint8_t * array, uint16_t length)
{
_count += length;
while (length--)
{
// reduce yield() calls
if ((_count & 0xFF) == 0xFF) yield();
_update(*array++);
add(*array++);
}
}

Expand Down
4 changes: 2 additions & 2 deletions CRC12.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

#include "Arduino.h"

#define CRC12_DEFAULT_POLYNOME 0x080D
#include "CRC_polynomes.h"


class CRC12
Expand All @@ -36,7 +36,7 @@ class CRC12
bool getReverseOut() { return _reverseOut; };

void add(uint8_t value);
void add(const uint8_t * array, uint8_t length);
void add(const uint8_t * array, uint16_t length);

uint16_t getCRC(); // returns CRC
uint32_t count() { return _count; };
Expand Down
10 changes: 4 additions & 6 deletions CRC16.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,16 @@ void CRC16::restart()
void CRC16::add(uint8_t value)
{
_count++;
if ((_count & 0xFF) == 0) yield();
_update(value);
}


void CRC16::add(const uint8_t * array, uint8_t length)
void CRC16::add(const uint8_t * array, uint16_t length)
{
_count += length;
while (length--)
{
// reduce yield() calls
if ((_count & 0xFF) == 0xFF) yield();
_update(*array++);
add(*array++);
}
}

Expand All @@ -67,7 +65,7 @@ void CRC16::_update(uint8_t value)
{
if (!_started) restart();
if (_reverseIn) value = _reverse8(value);
_crc ^= ((uint16_t)value) << 8;;
_crc ^= ((uint16_t)value) << 8;
for (uint8_t i = 8; i; i--)
{
if (_crc & (1UL << 15))
Expand Down
4 changes: 2 additions & 2 deletions CRC16.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include "Arduino.h"

#define CRC16_DEFAULT_POLYNOME 0x1021
#include "CRC_polynomes.h"


class CRC16
Expand All @@ -35,7 +35,7 @@ class CRC16
bool getReverseOut() { return _reverseOut; };

void add(uint8_t value);
void add(const uint8_t * array, uint8_t length);
void add(const uint8_t * array, uint16_t length);

uint16_t getCRC(); // returns CRC
uint32_t count() { return _count; };
Expand Down
10 changes: 4 additions & 6 deletions CRC32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,16 @@ void CRC32::restart()
void CRC32::add(uint8_t value)
{
_count++;
if ((_count & 0xFF) == 0) yield();
_update(value);
}


void CRC32::add(const uint8_t * array, uint8_t length)
void CRC32::add(const uint8_t * array, uint16_t length)
{
_count += length;
while (length--)
{
// reduce yield() calls
if ((_count & 0xFF) == 0xFF) yield();
_update(*array++);
add(*array++);
}
}

Expand All @@ -67,7 +65,7 @@ void CRC32::_update(uint8_t value)
{
if (!_started) restart();
if (_reverseIn) value = _reverse8(value);
_crc ^= ((uint32_t)value) << 24;;
_crc ^= ((uint32_t)value) << 24;
for (uint8_t i = 8; i; i--)
{
if (_crc & (1UL << 31))
Expand Down
4 changes: 2 additions & 2 deletions CRC32.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include "Arduino.h"

#define CRC32_DEFAULT_POLYNOME 0x04C11DB7
#include "CRC_polynomes.h"


class CRC32
Expand All @@ -35,7 +35,7 @@ class CRC32
bool getReverseOut() { return _reverseOut; };

void add(uint8_t value);
void add(const uint8_t * array, uint8_t length);
void add(const uint8_t * array, uint16_t length);

uint32_t getCRC(); // returns CRC
uint32_t count() { return _count; };
Expand Down
10 changes: 4 additions & 6 deletions CRC64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,16 @@ void CRC64::restart()
void CRC64::add(uint8_t value)
{
_count++;
if ((_count & 0xFF) == 0) yield();
_update(value);
}


void CRC64::add(const uint8_t * array, uint8_t length)
void CRC64::add(const uint8_t * array, uint16_t length)
{
_count += length;
while (length--)
{
// reduce yield() calls
if ((_count & 0xFF) == 0xFF) yield();
_update(*array++);
add(*array++);
}
}

Expand All @@ -67,7 +65,7 @@ void CRC64::_update(uint8_t value)
{
if (!_started) restart();
if (_reverseIn) value = _reverse8(value);
_crc ^= ((uint64_t)value) << 56;;
_crc ^= ((uint64_t)value) << 56;
for (uint8_t i = 8; i; i--)
{
if (_crc & (1ULL << 63))
Expand Down
5 changes: 3 additions & 2 deletions CRC64.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

#include "Arduino.h"

#define CRC64_DEFAULT_POLYNOME 0x814141AB
#include "CRC_polynomes.h"



class CRC64
Expand All @@ -35,7 +36,7 @@ class CRC64
bool getReverseOut() { return _reverseOut; };

void add(uint8_t value);
void add(const uint8_t * array, uint8_t length);
void add(const uint8_t * array, uint16_t length);

uint64_t getCRC(); // returns CRC
uint64_t count() { return _count; };
Expand Down
8 changes: 3 additions & 5 deletions CRC8.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,16 @@ void CRC8::restart()
void CRC8::add(uint8_t value)
{
_count++;
if ((_count & 0xFF) == 0) yield();
_update(value);
}


void CRC8::add(const uint8_t * array, uint8_t length)
void CRC8::add(const uint8_t * array, uint16_t length)
{
_count += length;
while (length--)
{
// reduce yield() calls
if ((_count & 0xFF) == 0xFF) yield();
_update(*array++);
add(*array++);
}
}

Expand Down
4 changes: 2 additions & 2 deletions CRC8.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include "Arduino.h"

#define CRC8_DEFAULT_POLYNOME 0x07
#include "CRC_polynomes.h"


class CRC8
Expand All @@ -35,7 +35,7 @@ class CRC8
bool getReverseOut() { return _reverseOut; };

void add(uint8_t value);
void add(const uint8_t * array, uint8_t length);
void add(const uint8_t * array, uint16_t length);

uint8_t getCRC(); // returns CRC
uint32_t count() { return _count; };
Expand Down
68 changes: 68 additions & 0 deletions CRC_polynomes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#pragma once
//
// FILE: polynomes.h
// AUTHOR: Rob Tillaart
// PURPOSE: defines for standard CRC polynomes
// URL: https://github.com/RobTillaart/CRC
//
// Polynomes from
// https://en.wikipedia.org/wiki/Cyclic_redundancy_check#Polynomial_representations_of_cyclic_redundancy_checks


// CRC 4
#define CRC4_DEFAULT_POLYNOME 0x03
#define CRC4_ITU 0x03


// CRC 8
#define CRC8_DEFAULT_POLYNOME 0x07
#define CRC8_DVB_S2 0xD5
#define CRC8_AUTOSAR 0x2F
#define CRC8_BLUETOOTH 0xA7
#define CRC8_CCITT 0x07
#define CRC8_DALLAS_MAXIM 0x31 // oneWire
#define CRC8_DARC 0x39
#define CRC8_GSM_B 0x49
#define CRC8_SAEJ1850 0x1D
#define CRC8_WCDMA 0x9B


// CRC 12
#define CRC12_DEFAULT_POLYNOME 0x080D
#define CRC12_CCITT 0x080F
#define CRC12_CDMA2000 0x0F13
#define CRC12_GSM 0x0D31


// CRC 16
#define CRC16_DEFAULT_POLYNOME 0x1021
#define CRC16_CHAKRAVARTY 0x2F15
#define CRC16_ARINC 0xA02B
#define CRC16_CCITT 0x1021
#define CRC16_CDMA2000 0xC867
#define CRC16_DECT 0x0589
#define CRC16_T10_DIF 0x8BB7
#define CRC16_DNP 0x3D65
#define CRC16_IBM 0x8005
#define CRC16_OPENSAFETY_A 0x5935
#define CRC16_OPENSAFETY_B 0x755B
#define CRC16_PROFIBUS 0x1DCF


// CRC 32
#define CRC32_DEFAULT_POLYNOME 0x04C11DB7
#define CRC32_ISO3309 0x04C11DB7
#define CRC32_CASTAGNOLI 0x1EDC6F41
#define CRC32_KOOPMAN 0x741B8CD7
#define CRC32_KOOPMAN_2 0x32583499
#define CRC32_Q 0x814141AB


// CRC 64
#define CRC64_DEFAULT_POLYNOME 0x42F0E1EBA9EA3693
#define CRC64_ECMA64 0x42F0E1EBA9EA3693
#define CRC64_ISO64 0x000000000000001B


// -- END OF FILE --

Loading

0 comments on commit 836672c

Please sign in to comment.