Hi,
Why this CRC code is not working correctly with Teensy 3.6. CRC calculation is wrong...
It works fine with 3.2...
Thanks for you help.
Why this CRC code is not working correctly with Teensy 3.6. CRC calculation is wrong...
It works fine with 3.2...
Thanks for you help.
Code:
/* CRC8 library
*/
#ifndef CRC8_H
#define CRC8_H
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
typedef uint8_t crc;
#define POLYNOMIAL 0x07 /* CRC8_CCITT -- this polynomial needs to match choice on javascript end */
#define WIDTH0 (8 * sizeof(crc))
#define TOPBIT (1 << (WIDTH0 - 1))
class CRC8 {
public:
CRC8();
void begin();
crc get_crc8(uint8_t const message[], int nBytes);
private:
uint8_t crcTable[256];
};
#endif
Code:
#include "CRC8.h"
// renamed WIDTH with WIDTH0 due to conflict with AdafruitNeomatrix
CRC8::CRC8(void)
{
}
void CRC8::begin(void)
{
crc remainder;
for (int dividend = 0; dividend < 256; ++dividend)
{
remainder = dividend << (WIDTH0 - 8);
for (uint8_t bit = 8; bit > 0; --bit)
{
if (remainder & TOPBIT)
{
remainder = (remainder << 1) ^ POLYNOMIAL;
}
else
{
remainder = (remainder << 1);
}
}
crcTable[dividend] = remainder;
}
}
crc CRC8::get_crc8(uint8_t const message[], int nBytes) {
uint8_t data;
crc remainder = 0;
for (int byte = 0; byte < nBytes; ++byte)
{
data = message[byte] ^ (remainder >> (WIDTH0 - 8));
remainder = crcTable[data] ^ (remainder << 8);
}
return (remainder);
}
Last edited: