Strange works with Teensy 3.2 but not with 3.6

Status
Not open for further replies.

2n3055

Well-known member
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.

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:
:confused: you failed to provide an example sketch that demonstrates the problem ??

I pasted your class definitions into the following test and it works for me on T3.2 and T.36 prints 106
Code:
CRC8 x;

void setup() {
  while (!Serial);
  uint8_t msg[200];

  x.begin();
  for (int i = 0; i < sizeof(msg); i++) msg[i] = i;
  crc c = x.get_crc8(msg, sizeof(msg));
  Serial.println(c);
}

void loop() {
}

Validation test with {'1','2','3','4','5','6','7','8','9'} gives 0xF4 as expected on both T3.2 and T3.6
SMBUS (poly=0x07 init=0x00 refin=false refout=false xorout=0x00 check=0xf4)
 
Last edited:
You have right manitou. Thanks for your help.
It seems that the problem is coming from elsewhere not from calculation !
 
Status
Not open for further replies.
Back
Top