Код в вопросе реализует CR C -10 / ATM , который включает 6 заполненных 0 бит после байтов данных и перед 10-битным CR C. Для данных примера в вопросе:
1f ff 30 04 05 34 a7
Должно быть получено 0x3b1, что и генерирует опубликованный вами код.
Альтернативная реализация также приводит к 0x3b1 для примера, если вы опубликовано, и код вопросов и эта альтернативная реализация соответствуют примерам из CR C -10 / ATM :
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
#define CRC10 (0x233<<6)
static uint16_t crctbl[256];
void gentbl(void)
{
uint16_t crc;
uint16_t c;
uint16_t i;
for(c = 0; c < 0x100; c++){
crc = c << 8;
for(i = 0; i < 8; i++)
/* assumes twos complement */
crc = (crc<<1)^((0-(crc>>15))&CRC10);
crctbl[c] = crc;
}
}
uint16_t crc10(uint8_t * bfr, int len)
{
uint16_t crc = 0x0000;
while(len--){
/* shifting 6 bits instead of 8 will leave crc cycled -2 bits when done */
crc ^= ((uint16_t)(*bfr++))<<6; /* xor byte<<6 into crc */
crc = (crc<<8)^crctbl[(crc>>8)]; /* cycle crc */
}
/* At this point, crc is cycled -2 bits, meaning that */
/* 2 bit of data have not been cycled yet, so cycle */
/* 8 bits to cycle the 2 bits of data and 6 padding bits of 0 */
crc = (crc<<8)^crctbl[(crc>>8)]; /* cycle crc +8 bits */
return(crc>>6); /* return 10 bit crc */
}
Тестовый код:
#include <stdio.h>
/* test messages including 10 bit CRC */
static uint8_t tst1[] =
{0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xf6};
static uint8_t tst2[] =
{0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x6B};
static uint8_t tst3[] =
{0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x03,0x0F};
static uint8_t tst4[] =
{0x12,0x34,0x56,0x78,0x90,0x12,0x34,0x56,0x78,0x90,0x12,0x34,0x56,0x78,0x90,0x12,
0x34,0x56,0x78,0x90,0x12,0x34,0x56,0x78,0x90,0x12,0x34,0x56,0x78,0x90,0x12,0x34,
0x56,0x78,0x90,0x12,0x34,0x56,0x78,0x90,0x12,0x34,0x56,0x78,0x90,0x12,0x02,0xED};
static uint8_t tst5[] =
{0x10,0x6A,0x6A,0x6A,0x6A,0x6A,0x6A,0x6A,0x6A,0x6A,0x6A,0x6A,0x6A,0x6A,0x6A,0x6A,
0x6A,0x6A,0x6A,0x6A,0x6A,0x6A,0x6A,0x6A,0x6A,0x6A,0x6A,0x6A,0x6A,0x6A,0x6A,0x6A,
0x6A,0x6A,0x6A,0x6A,0x6A,0x6A,0x6A,0x6A,0x6A,0x6A,0x6A,0x6A,0x6A,0x6A,0x03,0xB9};
static uint8_t tst6[] =
{0x18,0x01,0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x6A,0x6A,0x6A,0x6A,0x6A,0x6A,0x6A,0x6A,0x00,0x4A};
static uint8_t* ptst[6] = {tst1, tst2, tst3, tst4, tst5, tst6};
/* length of msgs including crc */
#define LEN (48)
int main(void)
{
uint16_t crc;
int i;
gentbl();
for(i = 0; i < (sizeof(ptst)/sizeof(ptst[0])); i++){
crc = crc10(ptst[i], LEN-2); /* generate a 10 bit CRC */
if((crc>>8) != ptst[i][LEN-2] || /* verify it matches test CRC */
(crc&0xff) != ptst[i][LEN-1])
break;
crc = crc10(ptst[i], LEN); /* verify CRC == 0 */
if(0 != crc)
break;
}
if(i == 6)
printf("passed\n");
else
printf("error\n");
return 0;
}