Fix bug with manchester receive function. Using suggested algorithm from em4x70 datasheet

This commit is contained in:
Christian Molson 2020-12-07 11:18:00 -05:00
commit b0ff0ed526

View file

@ -488,12 +488,10 @@ static bool find_EM4X70_Tag(void) {
static int em4x70_receive(uint8_t *bits) { static int em4x70_receive(uint8_t *bits) {
bool bbitchange = false;
uint32_t pl; uint32_t pl;
int bit_pos = 0; int bit_pos = 0;
uint8_t edge = 0;
// Set first bit to a 1 for starting off corectly
bits[0] = 1;
bool foundheader = false; bool foundheader = false;
@ -529,48 +527,44 @@ static int em4x70_receive(uint8_t *bits) {
// between two listen windows only pulse lengths of 1, 1.5 and 2 are possible // between two listen windows only pulse lengths of 1, 1.5 and 2 are possible
while (true) { while (true) {
bit_pos++; if(edge)
pl = get_pulse_length(); pl = get_pulse_length();
else
pl = get_pulse_invert_length();
if (check_pulse_length(pl, EM4X70_T_TAG_FULL_PERIOD, EM4X70_T_TAG_QUARTER_PERIOD)) { if (check_pulse_length(pl, EM4X70_T_TAG_FULL_PERIOD, EM4X70_T_TAG_QUARTER_PERIOD)) {
// pulse length = 1 -> keep former bit value // pulse length = 1
bits[bit_pos] = bits[bit_pos - 1]; bits[bit_pos++] = edge;
} else if (check_pulse_length(pl, 3 * EM4X70_T_TAG_HALF_PERIOD, EM4X70_T_TAG_QUARTER_PERIOD)) { } else if (check_pulse_length(pl, 3 * EM4X70_T_TAG_HALF_PERIOD, EM4X70_T_TAG_QUARTER_PERIOD)) {
// pulse length = 1.5 -> decision on bit change // pulse length = 1.5 -> flip edge detection
if(edge) {
if (bbitchange) { bits[bit_pos++] = 0;
bits[bit_pos++] = 0;
// if number of pulse lengths with 1.5 periods is even -> add bit edge = 0;
bits[bit_pos] = (bits[bit_pos - 1] == 1) ? 1 : 0;
// pulse length of 1.5 changes bit value
bits[bit_pos + 1] = (bits[bit_pos] == 1) ? 0 : 1;
bit_pos++;
// next time add only one bit
bbitchange = false;
} else { } else {
bits[bit_pos++] = 1;
bits[bit_pos] = (bits[bit_pos - 1] == 1) ? 0 : 1; bits[bit_pos++] = 1;
edge = 1;
// next time two bits have to be added
bbitchange = true;
} }
} else if (check_pulse_length(pl, 2 * EM4X70_T_TAG_FULL_PERIOD, EM4X70_T_TAG_QUARTER_PERIOD)) { } else if (check_pulse_length(pl, 2 * EM4X70_T_TAG_FULL_PERIOD, EM4X70_T_TAG_QUARTER_PERIOD)) {
// pulse length of 2 means: adding 2 bits "01" // pulse length of 2
bits[bit_pos] = 0; if(edge) {
bits[bit_pos + 1] = 1; bits[bit_pos++] = 0;
bit_pos++; bits[bit_pos++] = 1;
} else {
bits[bit_pos++] = 1;
bits[bit_pos++] = 0;
}
} else if (check_pulse_length(pl, 3 * EM4X70_T_TAG_FULL_PERIOD, EM4X70_T_TAG_QUARTER_PERIOD)) { } else if ( (edge && check_pulse_length(pl, 3 * EM4X70_T_TAG_FULL_PERIOD, EM4X70_T_TAG_QUARTER_PERIOD)) ||
// pulse length of 3 indicates listen window -> clear last (!edge && check_pulse_length(pl, 80, EM4X70_T_TAG_QUARTER_PERIOD))) {
// bit (= 0) and return
// LIW detected (either invert or normal)
return --bit_pos; return --bit_pos;
} }
} }