Skip to content

Commit

Permalink
Moving CRC16 routine to common code (to be used by EEPROM code)
Browse files Browse the repository at this point in the history
  • Loading branch information
kh4 committed Feb 17, 2014
1 parent b554c98 commit d7bb54e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 34 deletions.
28 changes: 7 additions & 21 deletions TX.h
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,6 @@ uint8_t compositeRSSI(uint8_t rssi, uint8_t linkq)

uint8_t frameIndex=0;
uint32_t srxLast=0;
uint16_t srxCRC=0;
uint8_t srxFlags=0;
uint8_t srxChannels=0;

Expand Down Expand Up @@ -495,41 +494,28 @@ void processSBUS(uint8_t c)
}
}

void sumdCRC16(uint8_t c)
{
uint8_t i;
srxCRC ^= (uint16_t)c<<8;
for (i = 0; i < 8; i++) {
if (srxCRC & 0x8000) {
srxCRC = (srxCRC << 1) ^ 0x1021;
} else {
srxCRC = (srxCRC << 1);
}
}
}

void processSUMD(uint8_t c)
{
if ((frameIndex == 0) && (c == SUMD_HEAD)) {
srxCRC=0;
sumdCRC16(c);
CRC16_reset();
CRC16_add(c);
frameIndex=1;
} else {
if (frameIndex == 1) {
srxFlags = c;
sumdCRC16(c);
CRC16_add(c);
} else if (frameIndex == 2) {
srxChannels = c;
sumdCRC16(c);
CRC16_add(c);
} else if (frameIndex < (3 + (srxChannels << 1))) {
if (frameIndex < 35) {
ppmWork.bytes[frameIndex-3] = c;
}
sumdCRC16(c);
CRC16_add(c);
} else if (frameIndex == (3 + (srxChannels << 1))) {
srxCRC ^= (uint16_t)c << 8;
CRC16_value ^= (uint16_t)c << 8;
} else {
if ((srxCRC == c) && (srxFlags == 0x01)) {
if ((CRC16_value == c) && (srxFlags == 0x01)) {
uint8_t ch;
if (srxChannels > 16) {
srxChannels = 16;
Expand Down
20 changes: 20 additions & 0 deletions common.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,26 @@ uint8_t countSetBits(uint16_t x)
return (x * 0x0101) >> 8;
}

static uint16_t CRC16_value;

inline void CRC16_reset()
{
CRC16_value = 0;
}

void CRC16_add(uint8_t c) // CCITT polynome
{
uint8_t i;
CRC16_value ^= (uint16_t)c << 8;
for (i = 0; i < 8; i++) {
if (CRC16_value & 0x8000) {
CRC16_value = (CRC16_value << 1) ^ 0x1021;
} else {
CRC16_value = (CRC16_value << 1);
}
}
}

// Halt and blink failure code
void fatalBlink(uint8_t blinks)
{
Expand Down
17 changes: 4 additions & 13 deletions serialPPM.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,28 +78,19 @@ void sendSBUSFrame(uint8_t failsafe, uint8_t lostpack)
}

#define SUMD_HEAD 0xa8
uint16_t sumdCRC;

void sumdWriteCRC(uint8_t c)
{
uint8_t i;
Serial.write(c);
sumdCRC ^= (uint16_t)c<<8;
for (i = 0; i < 8; i++) {
if (sumdCRC & 0x8000) {
sumdCRC = (sumdCRC << 1) ^ 0x1021;
} else {
sumdCRC = (sumdCRC << 1);
}
}
CRC16_add(c);
}

void sendSUMDFrame(uint8_t failsafe)
{
uint32_t now = micros();
if ((now - sOutLast) > 10000) {
sOutLast = now;
sumdCRC = 0;
CRC16_reset();
sumdWriteCRC(SUMD_HEAD);
sumdWriteCRC(failsafe ? 0x81 : 0x01);
sumdWriteCRC(16);
Expand All @@ -108,7 +99,7 @@ void sendSUMDFrame(uint8_t failsafe)
sumdWriteCRC(val >> 8);
sumdWriteCRC(val & 0xff);
}
Serial.write(sumdCRC >> 8);
Serial.write(sumdCRC & 0xff);
Serial.write(CRC16_value >> 8);
Serial.write(CRC16_value & 0xff);
}
}

0 comments on commit d7bb54e

Please sign in to comment.