MotorControlModuleSDFM_TMS3.../Projects/epwm_test/src/crc.c

133 lines
3.1 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* crc.c
*
* Created on: 7 ìàð. 2024 ã.
* Author: seklyuts
*/
#include "f28x_project.h"
const int order = 6;
const unsigned long polynom = 0x43;
const int direct = 1;
const unsigned long crcinit = 0x0000;
const unsigned long crcxor = 0xffff;
const int refin = 0;
const int refout = 0;
// internal global values:
unsigned long crcmask;
unsigned long crchighbit;
unsigned long crcinit_direct;
unsigned long crcinit_nondirect;
unsigned long crctab[256];
unsigned long reflect (unsigned long crc, int bitnum) {
// reflects the lower bitnum bits of crc
unsigned long i, j=1, crcout=0;
for (i=(unsigned long)1<<(bitnum-1); i; i>>=1) {
if (crc & i) crcout|=j;
j<<= 1;
}
return (crcout);
}
unsigned long crcbitbybit(unsigned char* p, unsigned long len) {
// bit by bit algorithm with augmented zero bytes.
// does not use lookup table, suited for polynom orders between 1...32.
unsigned long i, j, c, bit;
unsigned long crc = crcinit_nondirect;
for (i=0; i<len; i++) {
c = (unsigned long)*p++;
if (refin) c = reflect(c, 8);
for (j=0x80; j; j>>=1) {
bit = crc & crchighbit;
crc<<= 1;
if (c & j) crc|= 1;
if (bit) crc^= polynom;
}
}
for (i=0; i<order; i++) {
bit = crc & crchighbit;
crc<<= 1;
if (bit) crc^= polynom;
}
if (refout) crc=reflect(crc, order);
crc^= crcxor;
crc&= crcmask;
return(crc);
}
//unsigned long crcbitbybitfast(unsigned char* p, unsigned long len) {
uint16_t crcbitbybitfast(uint64_t data)
{
// fast bit by bit algorithm without augmented zero bytes.
// does not use lookup table, suited for polynom orders between 1...32.
unsigned long i, j, c, bit;
unsigned long crc = crcinit_direct;
crcmask = ((((uint64_t)1<<(order-1))-1)<<1)|1;
crchighbit = (uint64_t)1<<(order-1);
uint16_t p[5];
p[4] = data & 0xFF;
p[3] = (data >> 8) & 0xFF;
p[2] = (data >> 16) & 0xFF;
p[1] = (data >> 24) & 0xFF;
p[0] = (data >> 32) & 0xFF;
for (i=0; i<5; i++) {
c = p[i];
// if (refin) c = reflect(c, 8);
for (j=0x80; j; j>>=1) {
bit = crc & crchighbit;
crc<<= 1;
if (c & j) bit^= crchighbit;
if (bit) crc^= polynom;
}
}
// if (refout) crc=reflect(crc, order);
crc^= crcxor;
crc&= crcmask;
return(crc);
}
#define ALL_BITS 38
#define CS_BITS 6
uint16_t calcCRC(uint64_t data) // 38 bit -> 6 bit // x6 + x1 + 1/// poly = 0b1000011 = 0x86;
{
uint64_t temp1, temp2, crc = 0;
uint64_t poly = (uint64_t)0b1000011 << (ALL_BITS - CS_BITS - 1);
uint16_t i = 0;
crc = poly;
temp1 = data;
crc ^= temp1;
for(i=0; i < (ALL_BITS - CS_BITS); i++)
{
// crc = (crc & ((uint64_t)1 << (ALL_BITS-1))) ? ((crc ^ poly) << 1) : (crc << 1);
temp2 = ((uint64_t)1 << (ALL_BITS-1));
if(crc & temp2)
{
crc = ((crc ^ poly) << 1);
}
else
{
crc = (crc << 1);
}
}
return (crc >> (ALL_BITS - CS_BITS));
}