Classe de gestion du HTRC110
RFIDer.cpp@0:ec877e8509d0, 2017-05-02 (annotated)
- Committer:
- garivetm
- Date:
- Tue May 02 13:45:17 2017 +0000
- Revision:
- 0:ec877e8509d0
- Child:
- 1:02db8b7c40fb
First commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
garivetm | 0:ec877e8509d0 | 1 | #include "mbed.h" |
garivetm | 0:ec877e8509d0 | 2 | #include "RFIDer.h" |
garivetm | 0:ec877e8509d0 | 3 | |
garivetm | 0:ec877e8509d0 | 4 | RFIDer::RFIDer(PinName clk, PinName mosi, PinName miso) : clk(DigitalOut(clk)), mosi(DigitalOut(mosi)), miso(DigitalIn(miso)), data(InterruptIn(miso)){ |
garivetm | 0:ec877e8509d0 | 5 | Tserial = 40; |
garivetm | 0:ec877e8509d0 | 6 | Tdata = 250; |
garivetm | 0:ec877e8509d0 | 7 | lastBit = 2; |
garivetm | 0:ec877e8509d0 | 8 | tagAvailable = 0; |
garivetm | 0:ec877e8509d0 | 9 | } |
garivetm | 0:ec877e8509d0 | 10 | |
garivetm | 0:ec877e8509d0 | 11 | void RFIDer::setSamplingTime(char ts){ |
garivetm | 0:ec877e8509d0 | 12 | sendCmd((0b10 << 6) | (ts & 0b00111111)); // no response |
garivetm | 0:ec877e8509d0 | 13 | } |
garivetm | 0:ec877e8509d0 | 14 | |
garivetm | 0:ec877e8509d0 | 15 | char RFIDer::getSamplingTime(){ |
garivetm | 0:ec877e8509d0 | 16 | return sendCmd(0b00000010); // reponse : 0 0 D5-D0 |
garivetm | 0:ec877e8509d0 | 17 | } |
garivetm | 0:ec877e8509d0 | 18 | |
garivetm | 0:ec877e8509d0 | 19 | void RFIDer::setComPeriod(short half_period){ |
garivetm | 0:ec877e8509d0 | 20 | Tserial = half_period; |
garivetm | 0:ec877e8509d0 | 21 | } |
garivetm | 0:ec877e8509d0 | 22 | |
garivetm | 0:ec877e8509d0 | 23 | short RFIDer::getComPeriod(){ |
garivetm | 0:ec877e8509d0 | 24 | return Tserial; |
garivetm | 0:ec877e8509d0 | 25 | } |
garivetm | 0:ec877e8509d0 | 26 | |
garivetm | 0:ec877e8509d0 | 27 | void RFIDer::setConfigPage(char mode, char data){ |
garivetm | 0:ec877e8509d0 | 28 | /* Format : 0 1 P1 P0 D3 D2 D1 D0 |
garivetm | 0:ec877e8509d0 | 29 | * P1,P0 - D3,D2,D1,D0 |
garivetm | 0:ec877e8509d0 | 30 | * 0,0 - GAIN1,GAIN0,FILTERH,FILTERL |
garivetm | 0:ec877e8509d0 | 31 | * 0,1 - PD_MODE,PD_HYSTERESIS,TXDIS |
garivetm | 0:ec877e8509d0 | 32 | * 1,0 - THRESET,ACQAMP,FREEZ1,FREEZ0 |
garivetm | 0:ec877e8509d0 | 33 | * 1,1 - DIPSL1,DISSMARTCOMP,FSEL1,FSEL0 |
garivetm | 0:ec877e8509d0 | 34 | */ |
garivetm | 0:ec877e8509d0 | 35 | switch(mode){ |
garivetm | 0:ec877e8509d0 | 36 | case 0 : |
garivetm | 0:ec877e8509d0 | 37 | sendCmd((0b0100 << 4) | (0b00001111 & data)); // no response |
garivetm | 0:ec877e8509d0 | 38 | break; |
garivetm | 0:ec877e8509d0 | 39 | case 1 : |
garivetm | 0:ec877e8509d0 | 40 | sendCmd((0b0101 << 4) | (0b00001111 & data)); // no response |
garivetm | 0:ec877e8509d0 | 41 | break; |
garivetm | 0:ec877e8509d0 | 42 | case 2 : |
garivetm | 0:ec877e8509d0 | 43 | sendCmd((0b0110 << 4) | (0b00001111 & data)); // no response |
garivetm | 0:ec877e8509d0 | 44 | break; |
garivetm | 0:ec877e8509d0 | 45 | case 3 : |
garivetm | 0:ec877e8509d0 | 46 | sendCmd((0b0111 << 4) | (0b00001111 & data)); // no response |
garivetm | 0:ec877e8509d0 | 47 | printf("Config sent : %X\r\n", (0b0111 << 4) | (0b00001111 & data)); |
garivetm | 0:ec877e8509d0 | 48 | break; |
garivetm | 0:ec877e8509d0 | 49 | } |
garivetm | 0:ec877e8509d0 | 50 | } |
garivetm | 0:ec877e8509d0 | 51 | |
garivetm | 0:ec877e8509d0 | 52 | char RFIDer::getConfigPage(char mode){ |
garivetm | 0:ec877e8509d0 | 53 | char response; |
garivetm | 0:ec877e8509d0 | 54 | switch(mode){ |
garivetm | 0:ec877e8509d0 | 55 | case 0 : |
garivetm | 0:ec877e8509d0 | 56 | response = sendCmd(0b00000100); // no response |
garivetm | 0:ec877e8509d0 | 57 | break; |
garivetm | 0:ec877e8509d0 | 58 | case 1 : |
garivetm | 0:ec877e8509d0 | 59 | response = sendCmd(0b00000101); // no response |
garivetm | 0:ec877e8509d0 | 60 | break; |
garivetm | 0:ec877e8509d0 | 61 | case 2 : |
garivetm | 0:ec877e8509d0 | 62 | response = sendCmd(0b00000110); // no response |
garivetm | 0:ec877e8509d0 | 63 | break; |
garivetm | 0:ec877e8509d0 | 64 | case 3 : |
garivetm | 0:ec877e8509d0 | 65 | response = sendCmd(0b00000111); // no response |
garivetm | 0:ec877e8509d0 | 66 | break; |
garivetm | 0:ec877e8509d0 | 67 | } |
garivetm | 0:ec877e8509d0 | 68 | return response; |
garivetm | 0:ec877e8509d0 | 69 | } |
garivetm | 0:ec877e8509d0 | 70 | |
garivetm | 0:ec877e8509d0 | 71 | void RFIDer::setClockFrequency(int frequency){ |
garivetm | 0:ec877e8509d0 | 72 | // Get value |
garivetm | 0:ec877e8509d0 | 73 | char data = getConfigPage(3); |
garivetm | 0:ec877e8509d0 | 74 | printf("Get initial config : %X\r\n", data); |
garivetm | 0:ec877e8509d0 | 75 | switch(frequency){ |
garivetm | 0:ec877e8509d0 | 76 | case 4000000: //4Mhz => FSEL = 00 |
garivetm | 0:ec877e8509d0 | 77 | setConfigPage(3, ((data & 0b1100) | 0b00) & 0b00001111); |
garivetm | 0:ec877e8509d0 | 78 | break; |
garivetm | 0:ec877e8509d0 | 79 | case 8000000: //8Mhz => FSEL = 01 |
garivetm | 0:ec877e8509d0 | 80 | setConfigPage(3, ((data & 0b1100) | 0b01) & 0b00001111); |
garivetm | 0:ec877e8509d0 | 81 | break; |
garivetm | 0:ec877e8509d0 | 82 | case 12000000: //12Mhz => FSEL = 10 |
garivetm | 0:ec877e8509d0 | 83 | setConfigPage(3, ((data & 0b1100) | 0b10) & 0b00001111); |
garivetm | 0:ec877e8509d0 | 84 | break; |
garivetm | 0:ec877e8509d0 | 85 | case 16000000: //16Mhz => FSEL = 11 |
garivetm | 0:ec877e8509d0 | 86 | setConfigPage(3, ((data & 0b1100) | 0b11) & 0b00001111); |
garivetm | 0:ec877e8509d0 | 87 | break; |
garivetm | 0:ec877e8509d0 | 88 | default: // default 8Mhz |
garivetm | 0:ec877e8509d0 | 89 | setConfigPage(3, ((data & 0b1100) | 0b00) & 0b00001111); |
garivetm | 0:ec877e8509d0 | 90 | break; |
garivetm | 0:ec877e8509d0 | 91 | } |
garivetm | 0:ec877e8509d0 | 92 | } |
garivetm | 0:ec877e8509d0 | 93 | |
garivetm | 0:ec877e8509d0 | 94 | int RFIDer::getClockFrequency(){ |
garivetm | 0:ec877e8509d0 | 95 | char page = getConfigPage(3); |
garivetm | 0:ec877e8509d0 | 96 | switch(page & 0b00000011){ |
garivetm | 0:ec877e8509d0 | 97 | case 0 : |
garivetm | 0:ec877e8509d0 | 98 | return 4000000; |
garivetm | 0:ec877e8509d0 | 99 | case 1 : |
garivetm | 0:ec877e8509d0 | 100 | return 8000000; |
garivetm | 0:ec877e8509d0 | 101 | case 2 : |
garivetm | 0:ec877e8509d0 | 102 | return 12000000; |
garivetm | 0:ec877e8509d0 | 103 | case 3 : |
garivetm | 0:ec877e8509d0 | 104 | return 16000000; |
garivetm | 0:ec877e8509d0 | 105 | default : |
garivetm | 0:ec877e8509d0 | 106 | return 0; |
garivetm | 0:ec877e8509d0 | 107 | } |
garivetm | 0:ec877e8509d0 | 108 | } |
garivetm | 0:ec877e8509d0 | 109 | |
garivetm | 0:ec877e8509d0 | 110 | int RFIDer::getAntennaStatus(){ |
garivetm | 0:ec877e8509d0 | 111 | char page = getConfigPage(3); |
garivetm | 0:ec877e8509d0 | 112 | return ((page & 0b00010000) >> 4); |
garivetm | 0:ec877e8509d0 | 113 | } |
garivetm | 0:ec877e8509d0 | 114 | |
garivetm | 0:ec877e8509d0 | 115 | char RFIDer::readPhase(){ |
garivetm | 0:ec877e8509d0 | 116 | return sendCmd(0b00001000); // response : 0 0 D5-D0 |
garivetm | 0:ec877e8509d0 | 117 | } |
garivetm | 0:ec877e8509d0 | 118 | |
garivetm | 0:ec877e8509d0 | 119 | char RFIDer::sendCmd(char cmd) |
garivetm | 0:ec877e8509d0 | 120 | { |
garivetm | 0:ec877e8509d0 | 121 | int i; |
garivetm | 0:ec877e8509d0 | 122 | bool b; |
garivetm | 0:ec877e8509d0 | 123 | char response = 0; |
garivetm | 0:ec877e8509d0 | 124 | // initialisation |
garivetm | 0:ec877e8509d0 | 125 | clk = 1; //CLK High |
garivetm | 0:ec877e8509d0 | 126 | wait_us(Tserial); |
garivetm | 0:ec877e8509d0 | 127 | mosi = 0; |
garivetm | 0:ec877e8509d0 | 128 | wait_us(Tserial); |
garivetm | 0:ec877e8509d0 | 129 | mosi = 1; |
garivetm | 0:ec877e8509d0 | 130 | wait_us(Tserial); |
garivetm | 0:ec877e8509d0 | 131 | //8 clock periods to send the commands |
garivetm | 0:ec877e8509d0 | 132 | for (i=7;i>=0;i--){ |
garivetm | 0:ec877e8509d0 | 133 | // Falling edge |
garivetm | 0:ec877e8509d0 | 134 | clk = 0; |
garivetm | 0:ec877e8509d0 | 135 | b = (cmd & ( 1 << i )) >> i; |
garivetm | 0:ec877e8509d0 | 136 | mosi = b; |
garivetm | 0:ec877e8509d0 | 137 | wait_us(Tserial); |
garivetm | 0:ec877e8509d0 | 138 | // Rising edge |
garivetm | 0:ec877e8509d0 | 139 | clk = 1; |
garivetm | 0:ec877e8509d0 | 140 | wait_us(Tserial); |
garivetm | 0:ec877e8509d0 | 141 | } |
garivetm | 0:ec877e8509d0 | 142 | // 8 clocks period for data |
garivetm | 0:ec877e8509d0 | 143 | for (i=7;i>=0;i--){ |
garivetm | 0:ec877e8509d0 | 144 | // Falling edge |
garivetm | 0:ec877e8509d0 | 145 | clk = 0; |
garivetm | 0:ec877e8509d0 | 146 | wait_us(Tserial); |
garivetm | 0:ec877e8509d0 | 147 | clk = 1; |
garivetm | 0:ec877e8509d0 | 148 | //Rising edge |
garivetm | 0:ec877e8509d0 | 149 | b = miso; |
garivetm | 0:ec877e8509d0 | 150 | response = response | (b << i); |
garivetm | 0:ec877e8509d0 | 151 | wait_us(Tserial); |
garivetm | 0:ec877e8509d0 | 152 | } |
garivetm | 0:ec877e8509d0 | 153 | clk = 0; |
garivetm | 0:ec877e8509d0 | 154 | mosi = 1; |
garivetm | 0:ec877e8509d0 | 155 | return response; |
garivetm | 0:ec877e8509d0 | 156 | } |
garivetm | 0:ec877e8509d0 | 157 | |
garivetm | 0:ec877e8509d0 | 158 | void RFIDer::startReadingTag(){ |
garivetm | 0:ec877e8509d0 | 159 | int i; |
garivetm | 0:ec877e8509d0 | 160 | // initialisation |
garivetm | 0:ec877e8509d0 | 161 | clk = 1; //CLK High |
garivetm | 0:ec877e8509d0 | 162 | wait_us(Tserial); |
garivetm | 0:ec877e8509d0 | 163 | mosi = 0; |
garivetm | 0:ec877e8509d0 | 164 | wait_us(Tserial); |
garivetm | 0:ec877e8509d0 | 165 | mosi = 1; |
garivetm | 0:ec877e8509d0 | 166 | wait_us(Tserial); |
garivetm | 0:ec877e8509d0 | 167 | //8 clock periods to send the commands |
garivetm | 0:ec877e8509d0 | 168 | for (i=2;i>=0;i--){ |
garivetm | 0:ec877e8509d0 | 169 | // Falling edge |
garivetm | 0:ec877e8509d0 | 170 | clk = 0; |
garivetm | 0:ec877e8509d0 | 171 | mosi = 1; |
garivetm | 0:ec877e8509d0 | 172 | wait_us(Tserial); |
garivetm | 0:ec877e8509d0 | 173 | // Rising edge |
garivetm | 0:ec877e8509d0 | 174 | clk = 1; |
garivetm | 0:ec877e8509d0 | 175 | wait_us(Tserial); |
garivetm | 0:ec877e8509d0 | 176 | } |
garivetm | 0:ec877e8509d0 | 177 | clk = 0; |
garivetm | 0:ec877e8509d0 | 178 | mosi = 0; |
garivetm | 0:ec877e8509d0 | 179 | // Preparing for decoding |
garivetm | 0:ec877e8509d0 | 180 | timer.reset(); |
garivetm | 0:ec877e8509d0 | 181 | tagAvailable = 0; |
garivetm | 0:ec877e8509d0 | 182 | // Attach interrupt for tag reading |
garivetm | 0:ec877e8509d0 | 183 | data.rise(this, &RFIDer::ISR_tag_reading); |
garivetm | 0:ec877e8509d0 | 184 | data.fall(this, &RFIDer::ISR_tag_reading); |
garivetm | 0:ec877e8509d0 | 185 | } |
garivetm | 0:ec877e8509d0 | 186 | |
garivetm | 0:ec877e8509d0 | 187 | void RFIDer::stopReadingTag(){ |
garivetm | 0:ec877e8509d0 | 188 | // Disable isr |
garivetm | 0:ec877e8509d0 | 189 | data.disable_irq(); |
garivetm | 0:ec877e8509d0 | 190 | clk = 0; //CLK low |
garivetm | 0:ec877e8509d0 | 191 | wait_us(Tserial); |
garivetm | 0:ec877e8509d0 | 192 | clk = 1; //CLK high |
garivetm | 0:ec877e8509d0 | 193 | } |
garivetm | 0:ec877e8509d0 | 194 | |
garivetm | 0:ec877e8509d0 | 195 | void RFIDer::ISR_tag_reading(){ |
garivetm | 0:ec877e8509d0 | 196 | // Timer management |
garivetm | 0:ec877e8509d0 | 197 | timer.stop(); |
garivetm | 0:ec877e8509d0 | 198 | int delay = timer.read_us(); // Save delay between the last 2 rises |
garivetm | 0:ec877e8509d0 | 199 | timer.reset(); |
garivetm | 0:ec877e8509d0 | 200 | timer.start(); |
garivetm | 0:ec877e8509d0 | 201 | |
garivetm | 0:ec877e8509d0 | 202 | static char counter = 0; // '1' header counter |
garivetm | 0:ec877e8509d0 | 203 | static char bit_i = 0; // bit Array index |
garivetm | 0:ec877e8509d0 | 204 | static TagReaderState tagReaderState = IDLE; |
garivetm | 0:ec877e8509d0 | 205 | |
garivetm | 0:ec877e8509d0 | 206 | // Decoding bit |
garivetm | 0:ec877e8509d0 | 207 | char bit = decodeBit(delay); |
garivetm | 0:ec877e8509d0 | 208 | if(bit == -1){ // Not synchronized or an error occurred |
garivetm | 0:ec877e8509d0 | 209 | tagReaderState = IDLE; // Reset the tag reader |
garivetm | 0:ec877e8509d0 | 210 | counter = 0; |
garivetm | 0:ec877e8509d0 | 211 | bit_i = 0; |
garivetm | 0:ec877e8509d0 | 212 | return; |
garivetm | 0:ec877e8509d0 | 213 | } |
garivetm | 0:ec877e8509d0 | 214 | else if(bit == 2) // Single short edge detected : bit value is unknown |
garivetm | 0:ec877e8509d0 | 215 | return; // Nothing to do : wait for the next edge |
garivetm | 0:ec877e8509d0 | 216 | |
garivetm | 0:ec877e8509d0 | 217 | // FSM : TAG READER |
garivetm | 0:ec877e8509d0 | 218 | switch(tagReaderState){ |
garivetm | 0:ec877e8509d0 | 219 | /* |
garivetm | 0:ec877e8509d0 | 220 | * Looking for the nine 1 header |
garivetm | 0:ec877e8509d0 | 221 | */ |
garivetm | 0:ec877e8509d0 | 222 | case IDLE : |
garivetm | 0:ec877e8509d0 | 223 | if(bit == 1){ // If '1' is read |
garivetm | 0:ec877e8509d0 | 224 | counter++; |
garivetm | 0:ec877e8509d0 | 225 | } |
garivetm | 0:ec877e8509d0 | 226 | else{ // If '0' is read |
garivetm | 0:ec877e8509d0 | 227 | counter = 0; // Start again |
garivetm | 0:ec877e8509d0 | 228 | } |
garivetm | 0:ec877e8509d0 | 229 | |
garivetm | 0:ec877e8509d0 | 230 | if(counter == 9){ // If 9 following '1' are read |
garivetm | 0:ec877e8509d0 | 231 | counter = 0; |
garivetm | 0:ec877e8509d0 | 232 | bit_i = 0; |
garivetm | 0:ec877e8509d0 | 233 | tagReaderState = READING; |
garivetm | 0:ec877e8509d0 | 234 | } |
garivetm | 0:ec877e8509d0 | 235 | break; |
garivetm | 0:ec877e8509d0 | 236 | /* |
garivetm | 0:ec877e8509d0 | 237 | * Header has been found : Reading stream |
garivetm | 0:ec877e8509d0 | 238 | */ |
garivetm | 0:ec877e8509d0 | 239 | case READING: |
garivetm | 0:ec877e8509d0 | 240 | array[bit_i] = bit; |
garivetm | 0:ec877e8509d0 | 241 | bit_i++; |
garivetm | 0:ec877e8509d0 | 242 | |
garivetm | 0:ec877e8509d0 | 243 | if(bit_i > 54){ // A complete transponder memory has been read |
garivetm | 0:ec877e8509d0 | 244 | timer.stop(); // Stop timer |
garivetm | 0:ec877e8509d0 | 245 | timer.reset(); // Reset timer |
garivetm | 0:ec877e8509d0 | 246 | data.disable_irq(); // Disable interrupts |
garivetm | 0:ec877e8509d0 | 247 | // Check data integrity (row even parity, column even parity and stop bit) |
garivetm | 0:ec877e8509d0 | 248 | if(checkDataIntegrity()){ // Data is ok |
garivetm | 0:ec877e8509d0 | 249 | decodeTag(); // Read tag |
garivetm | 0:ec877e8509d0 | 250 | tagAvailable = 1; // Set flag |
garivetm | 0:ec877e8509d0 | 251 | tagReaderState = DONE; // Change FSM state |
garivetm | 0:ec877e8509d0 | 252 | } |
garivetm | 0:ec877e8509d0 | 253 | else{ // Corrupted data : start again ! |
garivetm | 0:ec877e8509d0 | 254 | tagReaderState = IDLE; |
garivetm | 0:ec877e8509d0 | 255 | } |
garivetm | 0:ec877e8509d0 | 256 | data.enable_irq(); // Reactivating interrupts |
garivetm | 0:ec877e8509d0 | 257 | } |
garivetm | 0:ec877e8509d0 | 258 | break; |
garivetm | 0:ec877e8509d0 | 259 | /* |
garivetm | 0:ec877e8509d0 | 260 | * A tag reading has been done : immediately relaunching the FSM |
garivetm | 0:ec877e8509d0 | 261 | */ |
garivetm | 0:ec877e8509d0 | 262 | case DONE : |
garivetm | 0:ec877e8509d0 | 263 | counter = 0; |
garivetm | 0:ec877e8509d0 | 264 | bit_i = 0; // Reset array pointer |
garivetm | 0:ec877e8509d0 | 265 | data.enable_irq(); |
garivetm | 0:ec877e8509d0 | 266 | tagReaderState = IDLE; |
garivetm | 0:ec877e8509d0 | 267 | break; |
garivetm | 0:ec877e8509d0 | 268 | /* |
garivetm | 0:ec877e8509d0 | 269 | * Default case |
garivetm | 0:ec877e8509d0 | 270 | */ |
garivetm | 0:ec877e8509d0 | 271 | default : |
garivetm | 0:ec877e8509d0 | 272 | tagReaderState = IDLE; |
garivetm | 0:ec877e8509d0 | 273 | break; |
garivetm | 0:ec877e8509d0 | 274 | } |
garivetm | 0:ec877e8509d0 | 275 | } |
garivetm | 0:ec877e8509d0 | 276 | |
garivetm | 0:ec877e8509d0 | 277 | char RFIDer::decodeBit(int delay){ |
garivetm | 0:ec877e8509d0 | 278 | static DecoderState decoderState = SYNC; |
garivetm | 0:ec877e8509d0 | 279 | static char lastBit = 0; |
garivetm | 0:ec877e8509d0 | 280 | static char TdelayCounter = 0; |
garivetm | 0:ec877e8509d0 | 281 | char currentBit = -1; // Error default value |
garivetm | 0:ec877e8509d0 | 282 | |
garivetm | 0:ec877e8509d0 | 283 | switch(decoderState){ |
garivetm | 0:ec877e8509d0 | 284 | /* Synchronisation with the clock |
garivetm | 0:ec877e8509d0 | 285 | * The routine looks for a Tdatarate-delay between 2 edges |
garivetm | 0:ec877e8509d0 | 286 | */ |
garivetm | 0:ec877e8509d0 | 287 | case SYNC : |
garivetm | 0:ec877e8509d0 | 288 | if(delay > 0.75*2*Tdata && delay < 1.25*2*Tdata){ |
garivetm | 0:ec877e8509d0 | 289 | lastBit = miso; |
garivetm | 0:ec877e8509d0 | 290 | decoderState = READY; |
garivetm | 0:ec877e8509d0 | 291 | } |
garivetm | 0:ec877e8509d0 | 292 | break; |
garivetm | 0:ec877e8509d0 | 293 | |
garivetm | 0:ec877e8509d0 | 294 | /* Decoder is ready to decode stream |
garivetm | 0:ec877e8509d0 | 295 | */ |
garivetm | 0:ec877e8509d0 | 296 | case READY : |
garivetm | 0:ec877e8509d0 | 297 | if(delay > 0.70*Tdata && delay < 1.30*Tdata){ |
garivetm | 0:ec877e8509d0 | 298 | TdelayCounter++; |
garivetm | 0:ec877e8509d0 | 299 | if(TdelayCounter == 2){ // 2nd consecutive T-delay edge |
garivetm | 0:ec877e8509d0 | 300 | TdelayCounter = 0; |
garivetm | 0:ec877e8509d0 | 301 | currentBit = lastBit; |
garivetm | 0:ec877e8509d0 | 302 | } |
garivetm | 0:ec877e8509d0 | 303 | else currentBit = 2; // Undetermined value : waiting for a second edge |
garivetm | 0:ec877e8509d0 | 304 | } |
garivetm | 0:ec877e8509d0 | 305 | else if(delay > 0.70*2*Tdata && delay < 1.30*2*Tdata){ |
garivetm | 0:ec877e8509d0 | 306 | if(TdelayCounter == 1){ // Error |
garivetm | 0:ec877e8509d0 | 307 | TdelayCounter = 0; |
garivetm | 0:ec877e8509d0 | 308 | decoderState = SYNC; // Try to resynchronize |
garivetm | 0:ec877e8509d0 | 309 | } |
garivetm | 0:ec877e8509d0 | 310 | else{ |
garivetm | 0:ec877e8509d0 | 311 | currentBit = !lastBit; |
garivetm | 0:ec877e8509d0 | 312 | lastBit = !lastBit; |
garivetm | 0:ec877e8509d0 | 313 | } |
garivetm | 0:ec877e8509d0 | 314 | } |
garivetm | 0:ec877e8509d0 | 315 | else{ // Error (delay too small or too big) |
garivetm | 0:ec877e8509d0 | 316 | TdelayCounter = 0; |
garivetm | 0:ec877e8509d0 | 317 | decoderState = SYNC; // Try to resynchronize |
garivetm | 0:ec877e8509d0 | 318 | } |
garivetm | 0:ec877e8509d0 | 319 | break; |
garivetm | 0:ec877e8509d0 | 320 | default : // Error |
garivetm | 0:ec877e8509d0 | 321 | TdelayCounter = 0; |
garivetm | 0:ec877e8509d0 | 322 | decoderState = SYNC; // Try to resynchronize |
garivetm | 0:ec877e8509d0 | 323 | break; |
garivetm | 0:ec877e8509d0 | 324 | } |
garivetm | 0:ec877e8509d0 | 325 | return currentBit; |
garivetm | 0:ec877e8509d0 | 326 | } |
garivetm | 0:ec877e8509d0 | 327 | |
garivetm | 0:ec877e8509d0 | 328 | char RFIDer::checkDataIntegrity(void){ |
garivetm | 0:ec877e8509d0 | 329 | char sum = 0; |
garivetm | 0:ec877e8509d0 | 330 | |
garivetm | 0:ec877e8509d0 | 331 | // Check if last bit is not Stop bit |
garivetm | 0:ec877e8509d0 | 332 | if(array[54] != 0){ |
garivetm | 0:ec877e8509d0 | 333 | return 0; |
garivetm | 0:ec877e8509d0 | 334 | } |
garivetm | 0:ec877e8509d0 | 335 | |
garivetm | 0:ec877e8509d0 | 336 | // Even parity row check |
garivetm | 0:ec877e8509d0 | 337 | for (int row = 0 ; row <= 9 ; row++){ |
garivetm | 0:ec877e8509d0 | 338 | sum = 0; |
garivetm | 0:ec877e8509d0 | 339 | for (int i = 0; i <=4 ; i++){ |
garivetm | 0:ec877e8509d0 | 340 | //printf("%d",array[5*row+i]); |
garivetm | 0:ec877e8509d0 | 341 | if(i == 4){ // End of row |
garivetm | 0:ec877e8509d0 | 342 | if(sum%2 != array[5*row+i]){ |
garivetm | 0:ec877e8509d0 | 343 | //printf("Row parity error!\n\r"); |
garivetm | 0:ec877e8509d0 | 344 | return 0; |
garivetm | 0:ec877e8509d0 | 345 | } |
garivetm | 0:ec877e8509d0 | 346 | //else printf("\n\r"); |
garivetm | 0:ec877e8509d0 | 347 | } |
garivetm | 0:ec877e8509d0 | 348 | sum+=array[5*row+i]; |
garivetm | 0:ec877e8509d0 | 349 | } |
garivetm | 0:ec877e8509d0 | 350 | } |
garivetm | 0:ec877e8509d0 | 351 | |
garivetm | 0:ec877e8509d0 | 352 | // Even parity column check |
garivetm | 0:ec877e8509d0 | 353 | for (int col=0 ; col <= 3 ; col++){ |
garivetm | 0:ec877e8509d0 | 354 | sum = 0; |
garivetm | 0:ec877e8509d0 | 355 | for (int i = 0; i <=10 ; i++){ |
garivetm | 0:ec877e8509d0 | 356 | if(i == 10){ // End of column |
garivetm | 0:ec877e8509d0 | 357 | if(sum%2 != array[col+5*i]){ |
garivetm | 0:ec877e8509d0 | 358 | //printf("Col parity error!\n\r"); |
garivetm | 0:ec877e8509d0 | 359 | return 0; |
garivetm | 0:ec877e8509d0 | 360 | } |
garivetm | 0:ec877e8509d0 | 361 | } |
garivetm | 0:ec877e8509d0 | 362 | sum+=array[col+5*i]; |
garivetm | 0:ec877e8509d0 | 363 | } |
garivetm | 0:ec877e8509d0 | 364 | } |
garivetm | 0:ec877e8509d0 | 365 | |
garivetm | 0:ec877e8509d0 | 366 | return 1; |
garivetm | 0:ec877e8509d0 | 367 | } |
garivetm | 0:ec877e8509d0 | 368 | |
garivetm | 0:ec877e8509d0 | 369 | void RFIDer::decodeTag(void){ |
garivetm | 0:ec877e8509d0 | 370 | // Get 8-hex word id |
garivetm | 0:ec877e8509d0 | 371 | for (int row = 0 ; row <= 7 ; row++){ |
garivetm | 0:ec877e8509d0 | 372 | tag[row] = 0; // init |
garivetm | 0:ec877e8509d0 | 373 | for (int i = 0; i <=3 ; i++){ |
garivetm | 0:ec877e8509d0 | 374 | tag[row]+=array[5*(row+2)+i] << (3-i); |
garivetm | 0:ec877e8509d0 | 375 | } |
garivetm | 0:ec877e8509d0 | 376 | } |
garivetm | 0:ec877e8509d0 | 377 | } |
garivetm | 0:ec877e8509d0 | 378 | |
garivetm | 0:ec877e8509d0 | 379 | char* RFIDer::getTag(void){ |
garivetm | 0:ec877e8509d0 | 380 | tagAvailable = 0; // Reset flag |
garivetm | 0:ec877e8509d0 | 381 | return tag; |
garivetm | 0:ec877e8509d0 | 382 | } |
garivetm | 0:ec877e8509d0 | 383 | |
garivetm | 0:ec877e8509d0 | 384 | bool RFIDer::isTagAvailable(void){ |
garivetm | 0:ec877e8509d0 | 385 | return tagAvailable; |
garivetm | 0:ec877e8509d0 | 386 | } |