Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Fork of CRIC_RFID by
main.cpp@4:449ff38844d3, 2014-12-18 (annotated)
- Committer:
- toni
- Date:
- Thu Dec 18 11:28:13 2014 +0000
- Revision:
- 4:449ff38844d3
- Parent:
- 3:1a298994f6ba
Avec interruptions
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| toni | 0:a6addbfe44b4 | 1 | #include "mbed.h" |
| toni | 4:449ff38844d3 | 2 | #include "main.hpp" |
| toni | 3:1a298994f6ba | 3 | |
| toni | 4:449ff38844d3 | 4 | //------------------------ MAIN ------------------------ |
| toni | 4:449ff38844d3 | 5 | bool infosDebug = false; |
| toni | 4:449ff38844d3 | 6 | bool readOrWriteMode = READ_MODE; |
| toni | 4:449ff38844d3 | 7 | DigitalOut myled(LED1); |
| toni | 4:449ff38844d3 | 8 | Serial debugPC(USBTX, USBRX); // tx, rx //Communication pour le debug entre le PC et le mbed |
| toni | 3:1a298994f6ba | 9 | |
| toni | 4:449ff38844d3 | 10 | //------------------------ RFID ------------------------ |
| toni | 3:1a298994f6ba | 11 | //Variables globales |
| toni | 3:1a298994f6ba | 12 | struct DonneesTag structDonneesTag; |
| toni | 4:449ff38844d3 | 13 | struct DataTrame strucTrameRx; |
| toni | 0:a6addbfe44b4 | 14 | Serial comRFID(p13, p14); // tx, rx //Communication entre le mbed et le RFID SM130 |
| toni | 4:449ff38844d3 | 15 | volatile bool frameRxComplete = false; |
| toni | 2:41df6e176f9a | 16 | |
| toni | 0:a6addbfe44b4 | 17 | |
| toni | 3:1a298994f6ba | 18 | //Main |
| toni | 0:a6addbfe44b4 | 19 | int main() { |
| toni | 3:1a298994f6ba | 20 | //Initialisation |
| toni | 3:1a298994f6ba | 21 | //debugPC.baud(19200); |
| toni | 2:41df6e176f9a | 22 | comRFID.baud(19200); |
| toni | 4:449ff38844d3 | 23 | comRFID.attach(&RxFrameInterrupt); |
| toni | 2:41df6e176f9a | 24 | |
| toni | 2:41df6e176f9a | 25 | debugPC.printf("\n\n\n\rAttente 2 secondes..."); |
| toni | 2:41df6e176f9a | 26 | wait(2); //Attente démarrage du mbed |
| toni | 1:6713f9d95bad | 27 | debugPC.printf("\n\rHello World!\n"); |
| toni | 0:a6addbfe44b4 | 28 | |
| toni | 0:a6addbfe44b4 | 29 | while(1) { |
| toni | 4:449ff38844d3 | 30 | RFID_Module(); |
| toni | 4:449ff38844d3 | 31 | //flashingLED(); |
| toni | 4:449ff38844d3 | 32 | } |
| toni | 4:449ff38844d3 | 33 | } |
| toni | 4:449ff38844d3 | 34 | //------------------------ MAIN ------------------------ |
| toni | 4:449ff38844d3 | 35 | void flashingLED() { |
| toni | 4:449ff38844d3 | 36 | myled = 1; |
| toni | 4:449ff38844d3 | 37 | wait(0.2); |
| toni | 4:449ff38844d3 | 38 | myled = 0; |
| toni | 4:449ff38844d3 | 39 | wait(0.2); |
| toni | 4:449ff38844d3 | 40 | } |
| toni | 4:449ff38844d3 | 41 | |
| toni | 4:449ff38844d3 | 42 | //------------------------ RFID ------------------------ |
| toni | 4:449ff38844d3 | 43 | void RFID_Module(){ |
| toni | 4:449ff38844d3 | 44 | unsigned char static state = SET_RESET, stateTAG = SEEK; |
| toni | 4:449ff38844d3 | 45 | unsigned char tabDataTx[25]; |
| toni | 4:449ff38844d3 | 46 | unsigned int valRetour; |
| toni | 4:449ff38844d3 | 47 | |
| toni | 4:449ff38844d3 | 48 | struct InfosTag structInfosTag; |
| toni | 4:449ff38844d3 | 49 | |
| toni | 4:449ff38844d3 | 50 | switch (state){ |
| toni | 4:449ff38844d3 | 51 | case SET_RESET : |
| toni | 4:449ff38844d3 | 52 | if(infosDebug == true) |
| toni | 4:449ff38844d3 | 53 | debugPC.printf("\n\rCase SET_RESET"); |
| toni | 4:449ff38844d3 | 54 | TxFrame(RESET, 0x00, tabDataTx); |
| toni | 4:449ff38844d3 | 55 | //debugPC.printf("\n\r\t\t\tframeRxComplete0 = %02x", frameRxComplete); |
| toni | 4:449ff38844d3 | 56 | while(frameRxComplete == false); |
| toni | 4:449ff38844d3 | 57 | frameRxComplete = false; |
| toni | 4:449ff38844d3 | 58 | //debugPC.printf("\n\r\t\t\tframeRxComplete1 = %02x \tcmd = %02x", frameRxComplete, strucTrameRx.command); |
| toni | 4:449ff38844d3 | 59 | if(strucTrameRx.command == FIRMWARE) //Response for reset = command response FIRMWARE |
| toni | 4:449ff38844d3 | 60 | state = SET_ANTENNA_POWER; |
| toni | 4:449ff38844d3 | 61 | break; |
| toni | 4:449ff38844d3 | 62 | |
| toni | 4:449ff38844d3 | 63 | case SET_ANTENNA_POWER : |
| toni | 4:449ff38844d3 | 64 | if(infosDebug == true) |
| toni | 4:449ff38844d3 | 65 | debugPC.printf("\n\rCase SET_ANTENNA_POWER"); |
| toni | 4:449ff38844d3 | 66 | tabDataTx[0] = ANTENNA_SWITCH_ON; |
| toni | 4:449ff38844d3 | 67 | TxFrame(ANTENNA_POWER, 0x01, tabDataTx); |
| toni | 4:449ff38844d3 | 68 | while(frameRxComplete == false); |
| toni | 4:449ff38844d3 | 69 | frameRxComplete = false; |
| toni | 4:449ff38844d3 | 70 | if((strucTrameRx.command == ANTENNA_POWER) & (strucTrameRx.data[0] == ANTENNA_SWITCH_ON)){ |
| toni | 4:449ff38844d3 | 71 | state = READ_TAG; |
| toni | 4:449ff38844d3 | 72 | stateTAG = SEEK; |
| toni | 4:449ff38844d3 | 73 | } |
| toni | 4:449ff38844d3 | 74 | break; |
| toni | 4:449ff38844d3 | 75 | |
| toni | 4:449ff38844d3 | 76 | case READ_TAG : |
| toni | 4:449ff38844d3 | 77 | if(infosDebug == true) |
| toni | 4:449ff38844d3 | 78 | debugPC.printf("\n\rCase READ_TAG"); |
| toni | 4:449ff38844d3 | 79 | |
| toni | 4:449ff38844d3 | 80 | switch (stateTAG){ |
| toni | 4:449ff38844d3 | 81 | case SEEK : |
| toni | 4:449ff38844d3 | 82 | if(infosDebug == true) |
| toni | 4:449ff38844d3 | 83 | debugPC.printf("\tSEEK"); |
| toni | 4:449ff38844d3 | 84 | TxFrame(SEEK_FOR_TAG, 0x00, tabDataTx); |
| toni | 4:449ff38844d3 | 85 | while(frameRxComplete == false); |
| toni | 4:449ff38844d3 | 86 | frameRxComplete = false; |
| toni | 4:449ff38844d3 | 87 | if(strucTrameRx.command == SEEK_FOR_TAG){ |
| toni | 4:449ff38844d3 | 88 | if((strucTrameRx.length == 0x02) & (strucTrameRx.data[0] == 0x55)){ |
| toni | 4:449ff38844d3 | 89 | state = SET_ANTENNA_POWER; |
| toni | 4:449ff38844d3 | 90 | stateTAG = SEEK; |
| toni | 4:449ff38844d3 | 91 | } |
| toni | 4:449ff38844d3 | 92 | else if(strucTrameRx.length == 0x06){ |
| toni | 4:449ff38844d3 | 93 | structInfosTag.tagType = strucTrameRx.data[0]; |
| toni | 4:449ff38844d3 | 94 | for(unsigned int cpt = 1; cpt < strucTrameRx.length - 1; cpt++) |
| toni | 4:449ff38844d3 | 95 | structInfosTag.serialNumber[cpt-1] = strucTrameRx.data[cpt]; |
| toni | 4:449ff38844d3 | 96 | stateTAG = AUTHENTI; |
| toni | 4:449ff38844d3 | 97 | } |
| toni | 3:1a298994f6ba | 98 | } |
| toni | 4:449ff38844d3 | 99 | break; |
| toni | 3:1a298994f6ba | 100 | |
| toni | 4:449ff38844d3 | 101 | case AUTHENTI : |
| toni | 4:449ff38844d3 | 102 | if(infosDebug == true) |
| toni | 4:449ff38844d3 | 103 | debugPC.printf("\tAUTHENTI"); |
| toni | 4:449ff38844d3 | 104 | tabDataTx[0] = 0x01; |
| toni | 4:449ff38844d3 | 105 | tabDataTx[1] = 0xFF; |
| toni | 4:449ff38844d3 | 106 | TxFrame(AUTHENTICATE, 0x02, tabDataTx); |
| toni | 4:449ff38844d3 | 107 | while(frameRxComplete == false); |
| toni | 4:449ff38844d3 | 108 | frameRxComplete = false; |
| toni | 4:449ff38844d3 | 109 | if(strucTrameRx.command == AUTHENTICATE){ |
| toni | 4:449ff38844d3 | 110 | if(strucTrameRx.data[0] == 0x4C){ //Login Successful |
| toni | 4:449ff38844d3 | 111 | if(readOrWriteMode == READ_MODE) |
| toni | 4:449ff38844d3 | 112 | stateTAG = READ; |
| toni | 4:449ff38844d3 | 113 | else if (readOrWriteMode == WRITE_MODE) |
| toni | 4:449ff38844d3 | 114 | stateTAG = WRITE; |
| toni | 4:449ff38844d3 | 115 | } |
| toni | 4:449ff38844d3 | 116 | else |
| toni | 4:449ff38844d3 | 117 | stateTAG = SEEK; |
| toni | 4:449ff38844d3 | 118 | } |
| toni | 4:449ff38844d3 | 119 | else |
| toni | 4:449ff38844d3 | 120 | stateTAG = SEEK; |
| toni | 4:449ff38844d3 | 121 | break; |
| toni | 4:449ff38844d3 | 122 | |
| toni | 4:449ff38844d3 | 123 | case READ : |
| toni | 4:449ff38844d3 | 124 | if(infosDebug == true) |
| toni | 4:449ff38844d3 | 125 | debugPC.printf("\tREAD"); |
| toni | 4:449ff38844d3 | 126 | tabDataTx[0] = 0x00; |
| toni | 4:449ff38844d3 | 127 | TxFrame(READ_BLOCK, 0x01, tabDataTx); |
| toni | 4:449ff38844d3 | 128 | while(frameRxComplete == false); |
| toni | 4:449ff38844d3 | 129 | frameRxComplete = false; |
| toni | 4:449ff38844d3 | 130 | if(strucTrameRx.command == READ_BLOCK){ |
| toni | 4:449ff38844d3 | 131 | if(strucTrameRx.length == 0x12){ //Read SUCCESS |
| toni | 4:449ff38844d3 | 132 | if(infosDebug == true) |
| toni | 4:449ff38844d3 | 133 | debugPC.printf("\tSUCCESS"); |
| toni | 4:449ff38844d3 | 134 | //on recupere les données du tag |
| toni | 4:449ff38844d3 | 135 | structDonneesTag.typeTrajet = strucTrameRx.data[0]; |
| toni | 4:449ff38844d3 | 136 | structDonneesTag.direction = strucTrameRx.data[1]; |
| toni | 4:449ff38844d3 | 137 | structDonneesTag.distance = strucTrameRx.data[2]; |
| toni | 4:449ff38844d3 | 138 | structDonneesTag.position = strucTrameRx.data[4]; |
| toni | 4:449ff38844d3 | 139 | if(infosDebug == true){ |
| toni | 4:449ff38844d3 | 140 | debugPC.printf("\r\nTypeTrajet :\t%02x", strucTrameRx.data[0]); |
| toni | 4:449ff38844d3 | 141 | debugPC.printf("\r\nDirection :\t%02x", strucTrameRx.data[1]); |
| toni | 4:449ff38844d3 | 142 | debugPC.printf("\r\nDistance :\t%02x", strucTrameRx.data[2]); |
| toni | 4:449ff38844d3 | 143 | debugPC.printf("\r\nPosition :\t%02x", strucTrameRx.data[4]); |
| toni | 3:1a298994f6ba | 144 | } |
| toni | 3:1a298994f6ba | 145 | } |
| toni | 4:449ff38844d3 | 146 | else { |
| toni | 4:449ff38844d3 | 147 | if(infosDebug == true) |
| toni | 4:449ff38844d3 | 148 | debugPC.printf("\tFAIL"); |
| toni | 3:1a298994f6ba | 149 | } |
| toni | 4:449ff38844d3 | 150 | } |
| toni | 4:449ff38844d3 | 151 | stateTAG = SEEK; |
| toni | 4:449ff38844d3 | 152 | break; |
| toni | 3:1a298994f6ba | 153 | |
| toni | 4:449ff38844d3 | 154 | case WRITE : |
| toni | 4:449ff38844d3 | 155 | if(infosDebug == true) |
| toni | 4:449ff38844d3 | 156 | debugPC.printf("\tWRITE"); |
| toni | 4:449ff38844d3 | 157 | |
| toni | 4:449ff38844d3 | 158 | tabDataTx[0] = 0x01; //TYPE DE TRAJET |
| toni | 4:449ff38844d3 | 159 | tabDataTx[1] = 0x00; //Direction |
| toni | 4:449ff38844d3 | 160 | tabDataTx[2] = 0x02; //Distance |
| toni | 4:449ff38844d3 | 161 | tabDataTx[3] = 0x00; //Reserved |
| toni | 4:449ff38844d3 | 162 | tabDataTx[4] = 0x05; //Position |
| toni | 4:449ff38844d3 | 163 | tabDataTx[5] = 0x00; |
| toni | 4:449ff38844d3 | 164 | tabDataTx[6] = 0x00; |
| toni | 4:449ff38844d3 | 165 | tabDataTx[7] = 0x00; |
| toni | 4:449ff38844d3 | 166 | tabDataTx[8] = 0x00; |
| toni | 4:449ff38844d3 | 167 | tabDataTx[9] = 0x00; |
| toni | 4:449ff38844d3 | 168 | tabDataTx[10] = 0x00; //Version |
| toni | 4:449ff38844d3 | 169 | tabDataTx[11] = 0x02; //Type carte |
| toni | 4:449ff38844d3 | 170 | tabDataTx[12] = 'C'; |
| toni | 4:449ff38844d3 | 171 | tabDataTx[13] = 'R'; |
| toni | 4:449ff38844d3 | 172 | tabDataTx[14] = 'I'; |
| toni | 4:449ff38844d3 | 173 | tabDataTx[15] = 'C'; |
| toni | 4:449ff38844d3 | 174 | TxFrame(WRITE_BLOCK, 0x0F, tabDataTx); |
| toni | 4:449ff38844d3 | 175 | while(frameRxComplete == false); |
| toni | 4:449ff38844d3 | 176 | frameRxComplete = false; |
| toni | 4:449ff38844d3 | 177 | if(strucTrameRx.command == WRITE_BLOCK){ |
| toni | 4:449ff38844d3 | 178 | if(strucTrameRx.length == 0x12){ //Read SUCCESS |
| toni | 4:449ff38844d3 | 179 | if(infosDebug == true) |
| toni | 4:449ff38844d3 | 180 | debugPC.printf("\tSUCCESS"); |
| toni | 4:449ff38844d3 | 181 | //on recupere les données du tag |
| toni | 4:449ff38844d3 | 182 | structDonneesTag.typeTrajet = strucTrameRx.data[0]; |
| toni | 4:449ff38844d3 | 183 | structDonneesTag.direction = strucTrameRx.data[1]; |
| toni | 4:449ff38844d3 | 184 | structDonneesTag.distance = strucTrameRx.data[2]; |
| toni | 4:449ff38844d3 | 185 | structDonneesTag.position = strucTrameRx.data[4]; |
| toni | 4:449ff38844d3 | 186 | if(infosDebug == true){ |
| toni | 4:449ff38844d3 | 187 | debugPC.printf("\r\nTypeTrajet :\t%02x", strucTrameRx.data[0]); |
| toni | 4:449ff38844d3 | 188 | debugPC.printf("\r\nDirection :\t%02x", strucTrameRx.data[1]); |
| toni | 4:449ff38844d3 | 189 | debugPC.printf("\r\nDistance :\t%02x", strucTrameRx.data[2]); |
| toni | 4:449ff38844d3 | 190 | debugPC.printf("\r\nPosition :\t%02x", strucTrameRx.data[4]); |
| toni | 3:1a298994f6ba | 191 | } |
| toni | 3:1a298994f6ba | 192 | } |
| toni | 4:449ff38844d3 | 193 | else { |
| toni | 4:449ff38844d3 | 194 | if(infosDebug == true) |
| toni | 4:449ff38844d3 | 195 | debugPC.printf("\tFAIL"); |
| toni | 3:1a298994f6ba | 196 | } |
| toni | 4:449ff38844d3 | 197 | } |
| toni | 4:449ff38844d3 | 198 | stateTAG = SEEK; |
| toni | 4:449ff38844d3 | 199 | break; |
| toni | 4:449ff38844d3 | 200 | } |
| toni | 4:449ff38844d3 | 201 | break; |
| toni | 0:a6addbfe44b4 | 202 | } |
| toni | 0:a6addbfe44b4 | 203 | } |
| toni | 0:a6addbfe44b4 | 204 | |
| toni | 2:41df6e176f9a | 205 | void TxFrame(unsigned char command, unsigned char lengthData, unsigned char *dataTx){ |
| toni | 3:1a298994f6ba | 206 | unsigned int cpt = 0; |
| toni | 0:a6addbfe44b4 | 207 | unsigned char csum = 0; |
| toni | 0:a6addbfe44b4 | 208 | |
| toni | 0:a6addbfe44b4 | 209 | lengthData += 1; // +1 pour ajout taille commande |
| toni | 3:1a298994f6ba | 210 | csum = calcCSUM(command, lengthData, dataTx); |
| toni | 0:a6addbfe44b4 | 211 | |
| toni | 4:449ff38844d3 | 212 | //Affiche debug __________________________________________ |
| toni | 4:449ff38844d3 | 213 | debugPC.printf("\n\r\tTx : "); |
| toni | 4:449ff38844d3 | 214 | debugPC.printf("FF "); |
| toni | 4:449ff38844d3 | 215 | debugPC.printf("00 "); |
| toni | 4:449ff38844d3 | 216 | debugPC.printf("%02x ", lengthData ); |
| toni | 4:449ff38844d3 | 217 | debugPC.printf("%02x ", command); |
| toni | 4:449ff38844d3 | 218 | for(cpt = 0; cpt<lengthData-1; cpt++) |
| toni | 4:449ff38844d3 | 219 | debugPC.printf("%02x ", *(dataTx+cpt)); |
| toni | 4:449ff38844d3 | 220 | debugPC.printf("%02x ", csum); |
| toni | 4:449ff38844d3 | 221 | //Affiche debug __________________________________________ |
| toni | 4:449ff38844d3 | 222 | |
| toni | 3:1a298994f6ba | 223 | comRFID.putc(0xFF); //HEADER |
| toni | 3:1a298994f6ba | 224 | comRFID.putc(0x00); //RESERVED |
| toni | 3:1a298994f6ba | 225 | comRFID.putc(lengthData ); //LENGTH |
| toni | 3:1a298994f6ba | 226 | comRFID.putc(command); //COMMAND |
| toni | 3:1a298994f6ba | 227 | for(cpt = 0; cpt<lengthData-1; cpt++){ //DATA |
| toni | 2:41df6e176f9a | 228 | comRFID.putc(*(dataTx+cpt)); |
| toni | 0:a6addbfe44b4 | 229 | } |
| toni | 4:449ff38844d3 | 230 | comRFID.putc(csum); //CSUM |
| toni | 3:1a298994f6ba | 231 | } |
| toni | 3:1a298994f6ba | 232 | |
| toni | 3:1a298994f6ba | 233 | unsigned int RxFrame(unsigned char *dataRx){ |
| toni | 3:1a298994f6ba | 234 | unsigned char command, length, csumRx, csumCalc; |
| toni | 3:1a298994f6ba | 235 | |
| toni | 3:1a298994f6ba | 236 | if(infosDebug == true) |
| toni | 3:1a298994f6ba | 237 | debugPC.printf("\n\r\tRx : "); |
| toni | 3:1a298994f6ba | 238 | |
| toni | 3:1a298994f6ba | 239 | for(unsigned int cpt = 0; comRFID.readable() == 1; cpt++){ //Ajouter un TIMEOUT |
| toni | 3:1a298994f6ba | 240 | dataRx[cpt] = comRFID.getc(); |
| toni | 3:1a298994f6ba | 241 | } |
| toni | 0:a6addbfe44b4 | 242 | |
| toni | 3:1a298994f6ba | 243 | command = *(dataRx + 3); |
| toni | 3:1a298994f6ba | 244 | length = *(dataRx + 2); |
| toni | 3:1a298994f6ba | 245 | csumRx = *(dataRx + 3 + length); |
| toni | 3:1a298994f6ba | 246 | |
| toni | 3:1a298994f6ba | 247 | //Verification HEADER |
| toni | 3:1a298994f6ba | 248 | if(*(dataRx + 0) != HEADER) |
| toni | 3:1a298994f6ba | 249 | return 1; |
| toni | 2:41df6e176f9a | 250 | |
| toni | 3:1a298994f6ba | 251 | //Verification RESERVED |
| toni | 3:1a298994f6ba | 252 | if(*(dataRx + 1) != RESERVED) |
| toni | 3:1a298994f6ba | 253 | return 1; |
| toni | 3:1a298994f6ba | 254 | |
| toni | 3:1a298994f6ba | 255 | //Verification csum |
| toni | 3:1a298994f6ba | 256 | csumCalc = calcCSUM(command, length, (dataRx + 4)); |
| toni | 3:1a298994f6ba | 257 | if(csumCalc != csumRx) |
| toni | 3:1a298994f6ba | 258 | return 1; |
| toni | 3:1a298994f6ba | 259 | |
| toni | 3:1a298994f6ba | 260 | if(infosDebug == true){ |
| toni | 3:1a298994f6ba | 261 | for(unsigned int cpt = 0; cpt < length + 4; cpt++) |
| toni | 3:1a298994f6ba | 262 | debugPC.printf("%02x ", dataRx[cpt]); |
| toni | 3:1a298994f6ba | 263 | } |
| toni | 3:1a298994f6ba | 264 | return 0; |
| toni | 0:a6addbfe44b4 | 265 | } |
| toni | 0:a6addbfe44b4 | 266 | |
| toni | 3:1a298994f6ba | 267 | unsigned char calcCSUM(unsigned char command, unsigned char lengthData, unsigned char *valData){ |
| toni | 3:1a298994f6ba | 268 | unsigned char csum = 0; |
| toni | 3:1a298994f6ba | 269 | |
| toni | 3:1a298994f6ba | 270 | csum += command; |
| toni | 3:1a298994f6ba | 271 | csum += lengthData; |
| toni | 3:1a298994f6ba | 272 | for(unsigned int cpt = 0; cpt<lengthData-1; cpt++) |
| toni | 3:1a298994f6ba | 273 | csum += *(valData + cpt); |
| toni | 3:1a298994f6ba | 274 | |
| toni | 3:1a298994f6ba | 275 | return csum; |
| toni | 4:449ff38844d3 | 276 | } |
| toni | 3:1a298994f6ba | 277 | |
| toni | 4:449ff38844d3 | 278 | void RxFrameInterrupt() { |
| toni | 4:449ff38844d3 | 279 | static unsigned char cptDataRxFrame = 0; //Compteur du nombres octets recu sur la liaison |
| toni | 4:449ff38844d3 | 280 | static unsigned char stateRxFrame = 1; //Variable etat permetant de valider chaque octet d'une trame recue |
| toni | 4:449ff38844d3 | 281 | unsigned char byteRx; |
| toni | 4:449ff38844d3 | 282 | |
| toni | 4:449ff38844d3 | 283 | byteRx = comRFID.getc(); //Variable qui stocke chaque octet recu |
| toni | 4:449ff38844d3 | 284 | //debugPC.printf("\n\rINTERRUPT"); |
| toni | 4:449ff38844d3 | 285 | |
| toni | 4:449ff38844d3 | 286 | switch (stateRxFrame) { |
| toni | 4:449ff38844d3 | 287 | case 0 : |
| toni | 4:449ff38844d3 | 288 | //debugPC.printf("\n\rSTATE 0 : Attente frameRxComplete = false"); |
| toni | 4:449ff38844d3 | 289 | if(frameRxComplete == false) |
| toni | 4:449ff38844d3 | 290 | stateRxFrame = 1; |
| toni | 4:449ff38844d3 | 291 | else |
| toni | 4:449ff38844d3 | 292 | break; |
| toni | 4:449ff38844d3 | 293 | |
| toni | 4:449ff38844d3 | 294 | case 1 : |
| toni | 4:449ff38844d3 | 295 | //debugPC.printf("\n\rSTATE 1 : Attente header"); |
| toni | 4:449ff38844d3 | 296 | if (byteRx == HEADER){ //Attente et vérification de la réception du HEADER |
| toni | 4:449ff38844d3 | 297 | strucTrameRx.header = byteRx; //Reception HEADER |
| toni | 4:449ff38844d3 | 298 | stateRxFrame = 2; |
| toni | 4:449ff38844d3 | 299 | } |
| toni | 4:449ff38844d3 | 300 | break; |
| toni | 4:449ff38844d3 | 301 | |
| toni | 4:449ff38844d3 | 302 | case 2 : |
| toni | 4:449ff38844d3 | 303 | //debugPC.printf("\n\rSTATE 2 : Attente reserved"); |
| toni | 4:449ff38844d3 | 304 | if (byteRx == RESERVED){ |
| toni | 4:449ff38844d3 | 305 | strucTrameRx.reserved = byteRx; //Reception RESERVED |
| toni | 4:449ff38844d3 | 306 | stateRxFrame = 3; //Réception et vérification du RESERVED |
| toni | 4:449ff38844d3 | 307 | } |
| toni | 4:449ff38844d3 | 308 | break; |
| toni | 4:449ff38844d3 | 309 | |
| toni | 4:449ff38844d3 | 310 | case 3 : |
| toni | 4:449ff38844d3 | 311 | strucTrameRx.length = byteRx; //Reception LENGTH |
| toni | 4:449ff38844d3 | 312 | //debugPC.printf("\n\rSTATE 3 : Reception length : %02x", strucTrameRx.length); |
| toni | 4:449ff38844d3 | 313 | stateRxFrame = 4; |
| toni | 4:449ff38844d3 | 314 | break; |
| toni | 4:449ff38844d3 | 315 | |
| toni | 4:449ff38844d3 | 316 | case 4 : |
| toni | 4:449ff38844d3 | 317 | strucTrameRx.command = byteRx; //Reception COMMAND |
| toni | 4:449ff38844d3 | 318 | //debugPC.printf("\n\rSTATE 4 : Reception command : %02x", strucTrameRx.command); |
| toni | 4:449ff38844d3 | 319 | stateRxFrame = 5; |
| toni | 4:449ff38844d3 | 320 | break; |
| toni | 4:449ff38844d3 | 321 | |
| toni | 4:449ff38844d3 | 322 | case 5 : |
| toni | 4:449ff38844d3 | 323 | if(cptDataRxFrame < strucTrameRx.length - 1){ //On teste si il y a des données à recuperer, on retire 1 pour la COMMAND |
| toni | 4:449ff38844d3 | 324 | strucTrameRx.data[cptDataRxFrame] = byteRx; //Reception DATA |
| toni | 4:449ff38844d3 | 325 | //debugPC.printf("\n\rSTATE 5 : Reception data : %02x", strucTrameRx.data[cptDataRxFrame]); |
| toni | 4:449ff38844d3 | 326 | cptDataRxFrame++; |
| toni | 4:449ff38844d3 | 327 | break; |
| toni | 4:449ff38844d3 | 328 | } |
| toni | 4:449ff38844d3 | 329 | else{ |
| toni | 4:449ff38844d3 | 330 | cptDataRxFrame = 0; //RAZ du compteur pour le prochain passage |
| toni | 4:449ff38844d3 | 331 | stateRxFrame = 6; |
| toni | 4:449ff38844d3 | 332 | //debugPC.printf("\n\rSTATE 5 : Reception data OK"); |
| toni | 4:449ff38844d3 | 333 | } |
| toni | 4:449ff38844d3 | 334 | // Pas de break, c'est normal car il faut récuperer le csum dans l'etat 6 |
| toni | 4:449ff38844d3 | 335 | |
| toni | 4:449ff38844d3 | 336 | case 6 : |
| toni | 4:449ff38844d3 | 337 | //debugPC.printf("\n\rSTATE 6 : Attente csum"); |
| toni | 4:449ff38844d3 | 338 | strucTrameRx.csum = byteRx; //Reception checksum |
| toni | 4:449ff38844d3 | 339 | |
| toni | 4:449ff38844d3 | 340 | unsigned char csumCalc = calcCSUM(strucTrameRx.command, strucTrameRx.length, (unsigned char*) strucTrameRx.data); //Verification csum |
| toni | 4:449ff38844d3 | 341 | //debugPC.printf("\n\rSTATE 6 : csum = %02x et csumCalc = %02x", strucTrameRx.csum, csumCalc); |
| toni | 4:449ff38844d3 | 342 | if(csumCalc == strucTrameRx.csum){ |
| toni | 4:449ff38844d3 | 343 | //debugPC.printf("\n\rSTATE 6 : csum OK"); |
| toni | 4:449ff38844d3 | 344 | frameRxComplete = true; |
| toni | 4:449ff38844d3 | 345 | |
| toni | 4:449ff38844d3 | 346 | debugPC.printf("\n\r\tRx : "); |
| toni | 4:449ff38844d3 | 347 | debugPC.printf("%02x ", strucTrameRx.header); |
| toni | 4:449ff38844d3 | 348 | debugPC.printf("%02x ", strucTrameRx.reserved); |
| toni | 4:449ff38844d3 | 349 | debugPC.printf("%02x ", strucTrameRx.length); |
| toni | 4:449ff38844d3 | 350 | debugPC.printf("%02x ", strucTrameRx.command); |
| toni | 4:449ff38844d3 | 351 | for(unsigned int cpt = 0; cpt < strucTrameRx.length-1; cpt++) |
| toni | 4:449ff38844d3 | 352 | debugPC.printf("%02x ", strucTrameRx.data[cpt]); |
| toni | 4:449ff38844d3 | 353 | debugPC.printf("%02x ", strucTrameRx.csum); |
| toni | 4:449ff38844d3 | 354 | |
| toni | 4:449ff38844d3 | 355 | } |
| toni | 4:449ff38844d3 | 356 | //debugPC.printf("\n\rSTATE 6 : OUT"); |
| toni | 4:449ff38844d3 | 357 | stateRxFrame = 1; |
| toni | 4:449ff38844d3 | 358 | break; |
| toni | 4:449ff38844d3 | 359 | } |
| toni | 4:449ff38844d3 | 360 | } |
| toni | 3:1a298994f6ba | 361 | |
| toni | 3:1a298994f6ba | 362 | /* |
| toni | 2:41df6e176f9a | 363 | unsigned int RxFrame(unsigned char *dataRx){ |
| toni | 2:41df6e176f9a | 364 | unsigned char csum; |
| toni | 2:41df6e176f9a | 365 | |
| toni | 2:41df6e176f9a | 366 | //Reception HEADER |
| toni | 2:41df6e176f9a | 367 | if(comRFID.readable() == 1) |
| toni | 2:41df6e176f9a | 368 | *(dataRx + 0) = comRFID.getc(); |
| toni | 3:1a298994f6ba | 369 | else |
| toni | 3:1a298994f6ba | 370 | return 1; |
| toni | 2:41df6e176f9a | 371 | if(*(dataRx + 0) != 0xFF) |
| toni | 2:41df6e176f9a | 372 | return 1; |
| toni | 2:41df6e176f9a | 373 | debugPC.printf("\n\r\tRx HEADER %02x", *(dataRx + 0)); |
| toni | 2:41df6e176f9a | 374 | |
| toni | 2:41df6e176f9a | 375 | //Reception RESERVED |
| toni | 2:41df6e176f9a | 376 | if(comRFID.readable() == 1) |
| toni | 2:41df6e176f9a | 377 | *(dataRx + 1) = comRFID.getc(); |
| toni | 3:1a298994f6ba | 378 | else |
| toni | 3:1a298994f6ba | 379 | return 1; |
| toni | 2:41df6e176f9a | 380 | if(*(dataRx + 1) != 0x00) |
| toni | 2:41df6e176f9a | 381 | return 1; |
| toni | 2:41df6e176f9a | 382 | debugPC.printf("\n\r\tRx RESERVED %02x", *(dataRx + 1)); |
| toni | 2:41df6e176f9a | 383 | |
| toni | 2:41df6e176f9a | 384 | //Reception LENGTH |
| toni | 2:41df6e176f9a | 385 | if(comRFID.readable() == 1) |
| toni | 2:41df6e176f9a | 386 | *(dataRx + 2) = comRFID.getc(); |
| toni | 3:1a298994f6ba | 387 | debugPC.printf("\n\r\tRx LENGTH %02x", *(dataRx + 2)); |
| toni | 2:41df6e176f9a | 388 | |
| toni | 3:1a298994f6ba | 389 | //Reception COMMAND |
| toni | 3:1a298994f6ba | 390 | if(comRFID.readable() == 1) |
| toni | 3:1a298994f6ba | 391 | *(dataRx + 3) = comRFID.getc(); |
| toni | 3:1a298994f6ba | 392 | debugPC.printf("\n\r\tRx COMMAND %02x\n\r\tRx BOUCLE", *(dataRx + 3)); |
| toni | 3:1a298994f6ba | 393 | |
| toni | 3:1a298994f6ba | 394 | //Reception DATA |
| toni | 3:1a298994f6ba | 395 | for(unsigned char cpt = 4; cpt<*(dataRx + 2) - 1; cpt++){ |
| toni | 2:41df6e176f9a | 396 | if(comRFID.readable() == 1){ |
| toni | 2:41df6e176f9a | 397 | debugPC.printf("."); |
| toni | 2:41df6e176f9a | 398 | *(dataRx + cpt) = comRFID.getc(); |
| toni | 2:41df6e176f9a | 399 | debugPC.printf(" (cpt : %02x) %02x .", cpt, *(dataRx + cpt)); |
| toni | 2:41df6e176f9a | 400 | } |
| toni | 2:41df6e176f9a | 401 | else { |
| toni | 2:41df6e176f9a | 402 | debugPC.printf("\n\r\tRx BOUCLE OUT"); |
| toni | 2:41df6e176f9a | 403 | return 1; |
| toni | 2:41df6e176f9a | 404 | } |
| toni | 2:41df6e176f9a | 405 | } |
| toni | 2:41df6e176f9a | 406 | |
| toni | 3:1a298994f6ba | 407 | //Reception CSUM |
| toni | 3:1a298994f6ba | 408 | if(comRFID.readable() == 1) |
| toni | 3:1a298994f6ba | 409 | *(dataRx + 2 + *(dataRx + 2)) = comRFID.getc(); |
| toni | 3:1a298994f6ba | 410 | debugPC.printf("\n\r\tRx CSUM %02x", *(dataRx + 2 + *(dataRx + 2))); |
| toni | 3:1a298994f6ba | 411 | |
| toni | 3:1a298994f6ba | 412 | |
| toni | 2:41df6e176f9a | 413 | csum = calcCSUM(*(dataRx+0), *(dataRx+1), (dataRx + 2)); |
| toni | 2:41df6e176f9a | 414 | debugPC.printf("\n\r\tRx VAL CALC CSUM %02x\n\r\t", csum); |
| toni | 2:41df6e176f9a | 415 | if(csum != *(dataRx + *(dataRx+2) + 3)) |
| toni | 2:41df6e176f9a | 416 | return 1; |
| toni | 2:41df6e176f9a | 417 | |
| toni | 2:41df6e176f9a | 418 | //Affiche debug __________________________________________ |
| toni | 2:41df6e176f9a | 419 | debugPC.printf("\n\rRx :\t"); |
| toni | 2:41df6e176f9a | 420 | for(unsigned char cpt = 0; cpt<*(dataRx + 2) + 2; cpt++) |
| toni | 2:41df6e176f9a | 421 | debugPC.printf("%02x ", *(dataRx+cpt)); |
| toni | 2:41df6e176f9a | 422 | |
| toni | 2:41df6e176f9a | 423 | return 0; |
| toni | 2:41df6e176f9a | 424 | } |
| toni | 3:1a298994f6ba | 425 | */ |
