- if (Uart.highCnt < 7) { // wait for a stable unmodulated signal
- if (Uart.twoBits == 0xffff) {
- Uart.highCnt++;
- } else {
- Uart.highCnt = 0;
- }
- } else {
- Uart.syncBit = 0xFFFF; // not set
- // look for 00xx1111 (the start bit)
- if ((Uart.twoBits & 0x6780) == 0x0780) Uart.syncBit = 7;
- else if ((Uart.twoBits & 0x33C0) == 0x03C0) Uart.syncBit = 6;
- else if ((Uart.twoBits & 0x19E0) == 0x01E0) Uart.syncBit = 5;
- else if ((Uart.twoBits & 0x0CF0) == 0x00F0) Uart.syncBit = 4;
- else if ((Uart.twoBits & 0x0678) == 0x0078) Uart.syncBit = 3;
- else if ((Uart.twoBits & 0x033C) == 0x003C) Uart.syncBit = 2;
- else if ((Uart.twoBits & 0x019E) == 0x001E) Uart.syncBit = 1;
- else if ((Uart.twoBits & 0x00CF) == 0x000F) Uart.syncBit = 0;
- if (Uart.syncBit != 0xFFFF) {
+ Uart.syncBit = 9999; // not set
+
+ // 00x11111 2|3 ticks pause followed by 6|5 ticks unmodulated Sequence Z (a "0" or "start of communication")
+ // 11111111 8 ticks unmodulation Sequence Y (a "0" or "end of communication" or "no information")
+ // 111100x1 4 ticks unmodulated followed by 2|3 ticks pause Sequence X (a "1")
+
+ // The start bit is one ore more Sequence Y followed by a Sequence Z (... 11111111 00x11111). We need to distinguish from
+ // Sequence X followed by Sequence Y followed by Sequence Z (111100x1 11111111 00x11111)
+ // we therefore look for a ...xx1111 11111111 00x11111xxxxxx... pattern
+ // (12 '1's followed by 2 '0's, eventually followed by another '0', followed by 5 '1's)
+ //
+#define ISO14443A_STARTBIT_MASK 0x07FFEF80 // mask is 00001111 11111111 1110 1111 10000000
+#define ISO14443A_STARTBIT_PATTERN 0x07FF8F80 // pattern is 00001111 11111111 1000 1111 10000000
+
+ if ((Uart.fourBits & (ISO14443A_STARTBIT_MASK >> 0)) == ISO14443A_STARTBIT_PATTERN >> 0) Uart.syncBit = 7;
+ else if ((Uart.fourBits & (ISO14443A_STARTBIT_MASK >> 1)) == ISO14443A_STARTBIT_PATTERN >> 1) Uart.syncBit = 6;
+ else if ((Uart.fourBits & (ISO14443A_STARTBIT_MASK >> 2)) == ISO14443A_STARTBIT_PATTERN >> 2) Uart.syncBit = 5;
+ else if ((Uart.fourBits & (ISO14443A_STARTBIT_MASK >> 3)) == ISO14443A_STARTBIT_PATTERN >> 3) Uart.syncBit = 4;
+ else if ((Uart.fourBits & (ISO14443A_STARTBIT_MASK >> 4)) == ISO14443A_STARTBIT_PATTERN >> 4) Uart.syncBit = 3;
+ else if ((Uart.fourBits & (ISO14443A_STARTBIT_MASK >> 5)) == ISO14443A_STARTBIT_PATTERN >> 5) Uart.syncBit = 2;
+ else if ((Uart.fourBits & (ISO14443A_STARTBIT_MASK >> 6)) == ISO14443A_STARTBIT_PATTERN >> 6) Uart.syncBit = 1;
+ else if ((Uart.fourBits & (ISO14443A_STARTBIT_MASK >> 7)) == ISO14443A_STARTBIT_PATTERN >> 7) Uart.syncBit = 0;
+
+ if (Uart.syncBit != 9999) { // found a sync bit