#include "xc_software_crc.h" #include "xc6xxx.h" #include uint8_t rf_whiten_buff[DATA_NUM] = {0}; /** * CRC16-CCITT-FALSE 多项式计算 */ __RAM_CODE uint16_t crc16_ccitt_false(uint8_t *crc_data, uint8_t data_len, uint16_t crc_seed, uint8_t packet_mode) { uint16_t crc_shift = crc_seed; uint16_t poly = 0x1021; uint8_t data_bit = 0; for (uint8_t i = 0; i < data_len - 1; 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; } } crc_shift ^= crc_data[data_len - 1] << 8; if (packet_mode == PACKET_ENHANCED) { data_bit = SURPLUS_UNIT; } else if (packet_mode == PACKET_NORMAL) { data_bit = 8; } for (uint8_t i = 0; i < data_bit; i++) { if (crc_shift & 0x8000) crc_shift = (crc_shift << 1) ^ poly; else crc_shift = crc_shift << 1; } return crc_shift; } /** * CRC8-ROHC 多项式计算 */ __RAM_CODE uint8_t crc8_rohc(const uint8_t *data, uint8_t length, uint16_t crc_seed, uint8_t packet_mode) { uint8_t crc = crc_seed; for (uint8_t i = 0; i < length; i++) { crc ^= (uint8_t)data[i]; for (int j = 0; j < 8; j++) { if (crc & 0x80) { crc = (crc << 1) ^ 0x07; } else { crc <<= 1; } if (packet_mode == PACKET_ENHANCED) { if (i >= length - 1) { break; } } } } return crc; } /** * 数据取反 */ __RAM_CODE uint16_t bit_negation(uint16_t data, uint8_t data_bit) { uint16_t temp = 0; for (uint8_t i = 0; i < data_bit; i++) { if ((data >> i) & 0x1) temp &= ~(0x1 << i); else temp |= (0x1 << i); } return temp; } /* 字节反序 */ __RAM_CODE uint8_t byte_bigandsmallend(uint8_t data) { uint8_t temp = 0x00; for (uint8_t i = 0; i < 8; i++) { temp = ((data >> i) & 0x01) | temp; if (i < 7) { temp = temp << 1; } } return temp; } /* 数组字节反序 */ __RAM_CODE void buff_bigandsmallend(uint8_t *buff, uint8_t buff_len) { for (uint8_t i = 0; i < buff_len; i++) { buff[i] = byte_bigandsmallend(buff[i]); } } /** * 数据白化 */ __RAM_CODE void whiten_shift(uint8_t *shifter) { *shifter = ((*shifter & 0x30) << 1) | (((*shifter & 0x08) << 1) ^ ((*shifter & 0x40) >> 2)) | (((*shifter) & 0x7) << 1) | ((*shifter & 0x40) >> 6); } /** * 磐启数据白化公式 */ __RAM_CODE uint8_t whiten_data(uint8_t *whiten_data_in, uint8_t data_in_len, uint8_t shifer_init) { uint8_t shift_buffer = shifer_init; 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; whiten_data_in[i] = whiten_data_out[i]; } return shift_buffer; }