Hello,
I am working with a sensor that generates a simple crc8 XOR on based on this code from the nrf sdk 17 and sends it to a central:
#include <stdio.h>
#include <string.h>
#include <math.h>
static unsigned int L2_calculate_crc(unsigned char *buffer_ptr, unsigned int count)
{
register unsigned int crc_reg;
register unsigned char bit_count;
#define CRC_GEN_POLY (0x11021) /* CRC-CCITT (X^^16 + X^^12 + X^^5 + 1): */
#define CRC_WORK_POLY (0x08408) /* Bit order reversed to match Tx order */
/*
* Initialize the working register to all one bits.
* This is equivalent to initializing to zero and then
* inverting the first sixteen message bits.
*/
crc_reg = 0xFFFF;
/* Loop for each byte in the message */
do
{
crc_reg ^= (unsigned int)*buffer_ptr;
++buffer_ptr;
for (bit_count=8; bit_count; --bit_count)
{
if (crc_reg & 0x01)
{
/*
* Bit ready to be shifted off the right is a one.
* A division is possible. This bit is logically
* XORed with the implied "testing" bit below
* bit zero of the working polynomial.
*/
crc_reg >>= 1;
crc_reg ^= CRC_WORK_POLY;
}
else
{
/*
* Bit ready to be shifted off the right is a zero.
* No division is possible. Just shift right one bit.
*/
crc_reg >>= 1;
}
}/*end for each bit*/
} while (--count);
/* Return the one's complement of the calculated remainder. */
return (~crc_reg);
}
char CalculateSum8 (__uint8_t *Buffer, __uint16_t Length)
{
__uint8_t Sum;
__uint16_t Count;
for (Sum = 0, Count = 0; Count < Length; Count++) {
Sum = (__uint8_t) (Sum + *(Buffer + Count));
}
return Sum;
}
char CalculateCheckSum8 ( __uint8_t *Buffer, __uint16_t Length)
{
__uint8_t CheckSum;
CheckSum = CalculateSum8 (Buffer, Length);
return (__uint8_t) (0x100 - CheckSum);
}
int main()
{ unsigned char crc[2] ={0xff,0xff};
printf("Test CRC8 generation\n");
/*66 11 01 1650861179 'e'*/
unsigned char array1[] ={ 0x42 , 0x0B , 0x01 , 0x62,0x66,0x24,0x7B , 0x65};
unsigned char crc_value;
unsigned char *buffer_ptr = array1;
unsigned int count = sizeof(array1);
crc_value = CalculateCheckSum8(buffer_ptr,count); // test function
printf("crc_value: 0X%X\n",crc_value);
return 0;
}
I need to check this CRC8 on the central and noticed that zephyr had a crc8 function: https://docs.zephyrproject.org/apidoc/latest/group__crc.html#ga9a925de71cd0255a22c769dc4b130da5
but that it required a polynomial and initial value. Can this be configured to be compatible with the crc8 check done from the nrf sdk 17 so that I can check the crc8 code sent from the sensor?