146 lines
3.7 KiB
C
146 lines
3.7 KiB
C
#include "xc_software_crc.h"
|
|
#include <stdio.h>
|
|
/**
|
|
* CRC16-CCITT-FALSE 多项式计算
|
|
*/
|
|
uint16_t crc16_ccitt_false(uint8_t *crc_data, uint8_t data_len)
|
|
{
|
|
uint16_t crc_shift = 0xFFFF;
|
|
uint16_t poly = 0x1021;
|
|
for (uint8_t i = 0; i < data_len; i++) {
|
|
crc_shift ^= crc_data[i] << 8;
|
|
for (uint8_t j = 0; j < 8; j++) {
|
|
if (crc_shift & 0x8000)
|
|
crc_shift = (crc_shift << 1) ^ poly;
|
|
else
|
|
crc_shift = crc_shift << 1;
|
|
}
|
|
}
|
|
return crc_shift;
|
|
}
|
|
|
|
/**
|
|
* CRC16-CCITT-FALSE 多项式计算(动态包)
|
|
*/
|
|
|
|
uint16_t crc16_ccitt_false_d(uint8_t *crc_data, uint8_t data_len)
|
|
{
|
|
uint16_t crc_shift = 0xFFFF;
|
|
uint16_t poly = 0x1021;
|
|
for (uint8_t i = 0; i < data_len - 1; i++) {
|
|
printf("%02x ", crc_data[i]);
|
|
crc_shift ^= crc_data[i] << 8;
|
|
for (uint8_t j = 0; j < 8; j++) {
|
|
if (crc_shift & 0x8000)
|
|
crc_shift = (crc_shift << 1) ^ poly;
|
|
else
|
|
crc_shift = crc_shift << 1;
|
|
// printf("%04x ", crc_shift);
|
|
}
|
|
// printf("\r\n");
|
|
}
|
|
uint8_t i;
|
|
crc_shift ^= crc_data[data_len - 1] << 8;
|
|
for (i = 0; i < SURPLUS_UNIT; i++) {
|
|
if (crc_shift & 0x8000)
|
|
crc_shift = (crc_shift << 1) ^ poly;
|
|
else
|
|
crc_shift = crc_shift << 1;
|
|
// printf("%04x\r\n", crc_shift);
|
|
}
|
|
printf("\r\n");
|
|
return crc_shift;
|
|
}
|
|
/**
|
|
CRC 8/ ROHC 多项式计算
|
|
*/
|
|
uint8_t crc8_rohc(uint8_t *crc_data, uint8_t data_len)
|
|
{
|
|
uint8_t crc_shift = 0xFF;
|
|
|
|
for (uint8_t i = 0; i < data_len; i++) {
|
|
crc_shift ^= crc_data[i];
|
|
for (uint8_t j = 0; j < 8; j++) {
|
|
if (crc_shift & 0x01)
|
|
crc_shift = (crc_shift >> 1) ^ 0xE0;
|
|
else
|
|
crc_shift = (crc_shift >> 1);
|
|
}
|
|
}
|
|
return crc_shift;
|
|
}
|
|
|
|
/**
|
|
* 数据字节取反
|
|
*/
|
|
uint8_t bit_negation(uint8_t data)
|
|
{
|
|
|
|
uint8_t temp = 0;
|
|
for (uint8_t i = 0; i < 8; i++) {
|
|
if ((data >> i) & 0x1)
|
|
temp &= ~(0x1 << i);
|
|
else
|
|
temp |= (0x1 << i);
|
|
}
|
|
return temp;
|
|
}
|
|
|
|
/* 字节大小端 */
|
|
void byte_bigandsmallend(uint8_t data)
|
|
{
|
|
uint8_t temp_num = data;
|
|
data = 0;
|
|
for (uint8_t i = 0; i < 8; i++) {
|
|
if (temp_num & 0x80)
|
|
data |= 1;
|
|
data <<= 1;
|
|
}
|
|
}
|
|
|
|
/* 数组每字节大小端 */
|
|
void buff_bigandsmallend(uint8_t *buff, uint8_t buff_len)
|
|
{
|
|
for (uint8_t i = 0; i < buff_len; i++) {
|
|
byte_bigandsmallend(buff[i]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 数据白化
|
|
*/
|
|
void whiten_shift(uint8_t *shifter)
|
|
{
|
|
*shifter = ((*shifter & 0x30) << 1) | (((*shifter & 0x08) << 1) ^ ((*shifter & 0x40) >> 2)) |
|
|
(((*shifter) & 0x7) << 1) | ((*shifter & 0x40) >> 6);
|
|
}
|
|
|
|
/**
|
|
* 磐启数据白化公式
|
|
*/
|
|
uint8_t whiten_data(uint8_t *whiten_data_in, uint8_t data_in_len)
|
|
{
|
|
uint8_t shift_buffer = 0x7F;
|
|
uint8_t whiten_data_out[DATA_NUM];
|
|
uint8_t whiten_data_code;
|
|
|
|
for (uint8_t i = 0; i < data_in_len; i++) {
|
|
whiten_data_code = 0x00;
|
|
for (uint8_t j = 0; j < 8; j++) {
|
|
if (j == 0) {
|
|
whiten_data_code = ((shift_buffer & 0x40) << 1) | whiten_data_code;
|
|
} else if (j == 1) {
|
|
whiten_data_code = (shift_buffer & 0x40) | whiten_data_code;
|
|
} else {
|
|
whiten_data_code = ((shift_buffer & 0x40) >> (j - 1)) | whiten_data_code;
|
|
}
|
|
whiten_shift(&shift_buffer);
|
|
}
|
|
whiten_data_out[i] = whiten_data_in[i] ^ whiten_data_code;
|
|
printf("%02x ", whiten_data_out[i]);
|
|
whiten_data_in[i] = whiten_data_out[i];
|
|
}
|
|
printf("\r\n");
|
|
return shift_buffer;
|
|
}
|