+ if (DecodeTag->SOF_corr >= DecodeTag->SOF_corr_prev) { // we are looking for the maximum correlation
+ DecodeTag->SOF_corr_prev = DecodeTag->SOF_corr;
+ } else {
+ DecodeTag->lastBit = SOF_PART1; // detected 1st part of SOF
+ DecodeTag->sum1 = DecodeTag->SOF_correlator[DecodeTag->SOF_last];
+ DecodeTag->sum2 = 0;
+ DecodeTag->posCount = 2;
+ DecodeTag->state = STATE_TAG_RECEIVING_DATA;
+ LED_C_ON();
+ }
+
+ break;
+
+ case STATE_TAG_RECEIVING_DATA:
+ if (DecodeTag->posCount == 1) {
+ DecodeTag->sum1 = 0;
+ DecodeTag->sum2 = 0;
+ }
+
+ if (DecodeTag->posCount <= 4) {
+ DecodeTag->sum1 += AMPLITUDE(ci, cq);
+ } else {
+ DecodeTag->sum2 += AMPLITUDE(ci, cq);
+ }
+
+ if (DecodeTag->posCount == 8) {
+ int16_t corr_1 = (DecodeTag->sum2 - DecodeTag->sum1) / 4;
+ int16_t corr_0 = (DecodeTag->sum1 - DecodeTag->sum2) / 4;
+ int16_t corr_EOF = (DecodeTag->sum1 + DecodeTag->sum2) / 8;
+ if (corr_EOF > corr_0 && corr_EOF > corr_1) {
+ DecodeTag->state = STATE_TAG_AWAIT_EOF;
+ } else if (corr_1 > corr_0) {
+ // logic 1
+ if (DecodeTag->lastBit == SOF_PART1) { // still part of SOF
+ DecodeTag->lastBit = SOF_PART2;
+ } else {
+ DecodeTag->lastBit = LOGIC1;
+ DecodeTag->shiftReg >>= 1;
+ DecodeTag->shiftReg |= 0x80;
+ DecodeTag->bitCount++;
+ if (DecodeTag->bitCount == 8) {
+ DecodeTag->output[DecodeTag->len] = DecodeTag->shiftReg;
+ DecodeTag->len++;
+ DecodeTag->bitCount = 0;
+ DecodeTag->shiftReg = 0;
+ }
+ }
+ } else {
+ // logic 0
+ if (DecodeTag->lastBit == SOF_PART1) { // incomplete SOF
+ DecodeTag->state = STATE_TAG_UNSYNCD;
+ LED_C_OFF();
+ } else {
+ DecodeTag->lastBit = LOGIC0;
+ DecodeTag->shiftReg >>= 1;
+ DecodeTag->bitCount++;
+ if (DecodeTag->bitCount == 8) {
+ DecodeTag->output[DecodeTag->len] = DecodeTag->shiftReg;
+ DecodeTag->len++;
+ DecodeTag->bitCount = 0;
+ DecodeTag->shiftReg = 0;
+ }
+ }