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