Classe de gestion du HTRC110

Dependents:   RFID_test

Files at this revision

API Documentation at this revision

Comitter:
gr66
Date:
Mon Dec 04 16:32:15 2017 +0000
Parent:
0:ec877e8509d0
Commit message:
HTRC110

Changed in this revision

RFIDer.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r ec877e8509d0 -r 02db8b7c40fb RFIDer.cpp
--- a/RFIDer.cpp	Tue May 02 13:45:17 2017 +0000
+++ b/RFIDer.cpp	Mon Dec 04 16:32:15 2017 +0000
@@ -1,30 +1,36 @@
 #include "mbed.h"
 #include "RFIDer.h"
 
-RFIDer::RFIDer(PinName clk, PinName mosi, PinName miso) : clk(DigitalOut(clk)), mosi(DigitalOut(mosi)), miso(DigitalIn(miso)), data(InterruptIn(miso)){
+RFIDer::RFIDer(PinName clk, PinName mosi, PinName miso) : clk(DigitalOut(clk)), mosi(DigitalOut(mosi)), miso(DigitalIn(miso)), data(InterruptIn(miso))
+{
     Tserial = 40;
     Tdata = 250;
     lastBit = 2;
     tagAvailable = 0;
 }
 
-void RFIDer::setSamplingTime(char ts){
+void RFIDer::setSamplingTime(char ts)
+{
     sendCmd((0b10 << 6) | (ts & 0b00111111)); // no response
 }
 
-char RFIDer::getSamplingTime(){
+char RFIDer::getSamplingTime()
+{
     return sendCmd(0b00000010); // reponse : 0 0 D5-D0
 }
 
-void RFIDer::setComPeriod(short half_period){
+void RFIDer::setComPeriod(short half_period)
+{
     Tserial = half_period;
 }
 
-short RFIDer::getComPeriod(){
+short RFIDer::getComPeriod()
+{
     return Tserial;
 }
-    
-void RFIDer::setConfigPage(char mode, char data){
+
+void RFIDer::setConfigPage(char mode, char data)
+{
     /* Format : 0 1 P1 P0 D3 D2 D1 D0
      * P1,P0 - D3,D2,D1,D0
      * 0,0 - GAIN1,GAIN0,FILTERH,FILTERL
@@ -32,7 +38,7 @@
      * 1,0 - THRESET,ACQAMP,FREEZ1,FREEZ0
      * 1,1 - DIPSL1,DISSMARTCOMP,FSEL1,FSEL0
      */
-    switch(mode){
+    switch(mode) {
         case 0 :
             sendCmd((0b0100 << 4) | (0b00001111 & data)); // no response
             break;
@@ -49,9 +55,10 @@
     }
 }
 
-char RFIDer::getConfigPage(char mode){
+char RFIDer::getConfigPage(char mode)
+{
     char response;
-    switch(mode){
+    switch(mode) {
         case 0 :
             response = sendCmd(0b00000100); // no response
             break;
@@ -68,11 +75,12 @@
     return response;
 }
 
-void RFIDer::setClockFrequency(int frequency){
+void RFIDer::setClockFrequency(int frequency)
+{
     // Get value
     char data = getConfigPage(3);
     printf("Get initial config : %X\r\n", data);
-    switch(frequency){
+    switch(frequency) {
         case 4000000: //4Mhz => FSEL = 00
             setConfigPage(3, ((data & 0b1100) | 0b00) & 0b00001111);
             break;
@@ -91,9 +99,10 @@
     }
 }
 
-int RFIDer::getClockFrequency(){
+int RFIDer::getClockFrequency()
+{
     char page = getConfigPage(3);
-    switch(page & 0b00000011){
+    switch(page & 0b00000011) {
         case 0 :
             return 4000000;
         case 1 :
@@ -107,12 +116,14 @@
     }
 }
 
-int RFIDer::getAntennaStatus(){
+int RFIDer::getAntennaStatus()
+{
     char page = getConfigPage(3);
     return ((page & 0b00010000) >> 4);
 }
 
-char RFIDer::readPhase(){
+char RFIDer::readPhase()
+{
     return sendCmd(0b00001000); // response : 0 0 D5-D0
 }
 
@@ -129,7 +140,7 @@
     mosi = 1;
     wait_us(Tserial);
     //8 clock periods to send the commands
-    for (i=7;i>=0;i--){
+    for (i=7; i>=0; i--) {
         // Falling edge
         clk = 0;
         b = (cmd & ( 1 << i )) >> i;
@@ -138,9 +149,9 @@
         // Rising edge
         clk = 1;
         wait_us(Tserial);
-    }   
+    }
     // 8 clocks period for data
-    for (i=7;i>=0;i--){
+    for (i=7; i>=0; i--) {
         // Falling edge
         clk = 0;
         wait_us(Tserial);
@@ -155,7 +166,8 @@
     return response;
 }
 
-void RFIDer::startReadingTag(){
+void RFIDer::startReadingTag()
+{
     int i;
     // initialisation
     clk = 1; //CLK High
@@ -165,7 +177,7 @@
     mosi = 1;
     wait_us(Tserial);
     //8 clock periods to send the commands
-    for (i=2;i>=0;i--){
+    for (i=2; i>=0; i--) {
         // Falling edge
         clk = 0;
         mosi = 1;
@@ -182,9 +194,11 @@
     // Attach interrupt for tag reading
     data.rise(this, &RFIDer::ISR_tag_reading);
     data.fall(this, &RFIDer::ISR_tag_reading);
+    data.enable_irq(); // gr
 }
 
-void RFIDer::stopReadingTag(){
+void RFIDer::stopReadingTag()
+{
     // Disable isr
     data.disable_irq();
     clk = 0; //CLK low
@@ -192,127 +206,122 @@
     clk = 1; //CLK high
 }
 
-void RFIDer::ISR_tag_reading(){
+void RFIDer::ISR_tag_reading()
+{
     // Timer management
     timer.stop();
     int delay = timer.read_us(); // Save delay between the last 2 rises
     timer.reset();
     timer.start();
-    
+
     static char counter = 0;    // '1' header counter
     static char bit_i = 0;      // bit Array index
     static TagReaderState tagReaderState = IDLE;
-    
+
     // Decoding bit
     char bit = decodeBit(delay);
-    if(bit == -1){ // Not synchronized or an error occurred
+    if(bit == -1) { // Not synchronized or an error occurred
         tagReaderState = IDLE; // Reset the tag reader
         counter = 0;
         bit_i = 0;
         return;
-    }
-    else if(bit == 2) // Single short edge detected : bit value is unknown
+    } else if(bit == 2) // Single short edge detected : bit value is unknown
         return; // Nothing to do : wait for the next edge
-    
-    // FSM : TAG READER    
-    switch(tagReaderState){
-        /*
-         * Looking for the nine 1 header
-         */
+
+    // FSM : TAG READER
+    switch(tagReaderState) {
+            /*
+             * Looking for the nine 1 header
+             */
         case IDLE :
-            if(bit == 1){ // If '1' is read
+            if(bit == 1) { // If '1' is read
                 counter++;
-            }
-            else{ // If '0' is read
+            } else { // If '0' is read
                 counter = 0; // Start again
             }
-    
-            if(counter == 9){ // If 9 following '1' are read
+
+            if(counter == 9) { // If 9 following '1' are read
                 counter = 0;
                 bit_i = 0;
                 tagReaderState = READING;
             }
             break;
-        /*
-         * Header has been found : Reading stream
-         */
+            /*
+             * Header has been found : Reading stream
+             */
         case READING:
             array[bit_i] = bit;
             bit_i++;
-            
-            if(bit_i > 54){ // A complete transponder memory has been read
+
+            if(bit_i > 54) { // A complete transponder memory has been read
                 timer.stop();               // Stop timer
                 timer.reset();              // Reset timer
                 data.disable_irq();         // Disable interrupts
-                // Check data integrity (row even parity, column even parity and stop bit) 
-                if(checkDataIntegrity()){ // Data is ok
+                // Check data integrity (row even parity, column even parity and stop bit)
+                if(checkDataIntegrity()) { // Data is ok
                     decodeTag();            // Read tag
-                    tagAvailable = 1;       // Set flag   
+                    tagAvailable = 1;       // Set flag
                     tagReaderState = DONE;  // Change FSM state
-                }
-                else{ // Corrupted data : start again !
+                } else { // Corrupted data : start again !
                     tagReaderState = IDLE;
                 }
                 data.enable_irq();          // Reactivating interrupts
             }
             break;
-        /*
-         *  A tag reading has been done : immediately relaunching the FSM
-         */   
+            /*
+             *  A tag reading has been done : immediately relaunching the FSM
+             */
         case DONE :
             counter = 0;
             bit_i = 0;                // Reset array pointer
             data.enable_irq();
             tagReaderState = IDLE;
             break;
-        /*
-         * Default case
-         */   
+            /*
+             * Default case
+             */
         default :
             tagReaderState = IDLE;
             break;
     }
 }
 
-char RFIDer::decodeBit(int delay){
+char RFIDer::decodeBit(int delay)
+{
     static DecoderState decoderState = SYNC;
     static char lastBit = 0;
-    static char TdelayCounter = 0;   
+    static char TdelayCounter = 0;
     char currentBit = -1; // Error default value
-    
-    switch(decoderState){
-        /*  Synchronisation with the clock
-         *  The routine looks for a Tdatarate-delay between 2 edges
-         */        
+
+    switch(decoderState) {
+            /*  Synchronisation with the clock
+             *  The routine looks for a Tdatarate-delay between 2 edges
+             */
         case SYNC :
-            if(delay > 0.75*2*Tdata && delay < 1.25*2*Tdata){
+            if(delay > 0.75*2*Tdata && delay < 1.25*2*Tdata) {
                 lastBit = miso;
                 decoderState = READY;
             }
             break;
-            
-        /* Decoder is ready to decode stream
-         */        
+
+            /* Decoder is ready to decode stream
+             */
         case READY :
-            if(delay > 0.70*Tdata && delay < 1.30*Tdata){
+            if(delay > 0.70*Tdata && delay < 1.30*Tdata) {
                 TdelayCounter++;
-                if(TdelayCounter == 2){ // 2nd consecutive T-delay edge
+                if(TdelayCounter == 2) { // 2nd consecutive T-delay edge
                     TdelayCounter = 0;
                     currentBit = lastBit;
-                }
-                else currentBit = 2; // Undetermined value : waiting for a second edge
-            }
-            else if(delay > 0.70*2*Tdata && delay < 1.30*2*Tdata){
-                if(TdelayCounter == 1){ // Error
+                } else currentBit = 2; // Undetermined value : waiting for a second edge
+            } else if(delay > 0.70*2*Tdata && delay < 1.30*2*Tdata) {
+                if(TdelayCounter == 1) { // Error
                     TdelayCounter = 0;
                     decoderState = SYNC; // Try to resynchronize
-                }    
-                else{
+                } else {
                     currentBit = !lastBit;
                     lastBit = !lastBit;
                 }
-            }
-            else{ // Error (delay too small or too big)
+            } else { // Error (delay too small or too big)
                 TdelayCounter = 0;
                 decoderState = SYNC; // Try to resynchronize
             }
@@ -325,21 +334,22 @@
     return currentBit;
 }
 
-char RFIDer::checkDataIntegrity(void){
+char RFIDer::checkDataIntegrity(void)
+{
     char sum = 0;
-    
+
     // Check if last bit is not Stop bit
-    if(array[54] != 0){
+    if(array[54] != 0) {
         return 0;
     }
-    
+
     // Even parity row check
-    for (int row = 0 ; row <= 9 ; row++){
+    for (int row = 0 ; row <= 9 ; row++) {
         sum = 0;
-        for (int i = 0; i <=4 ; i++){
+        for (int i = 0; i <=4 ; i++) {
             //printf("%d",array[5*row+i]);
-            if(i == 4){ // End of row
-                if(sum%2 != array[5*row+i]){
+            if(i == 4) { // End of row
+                if(sum%2 != array[5*row+i]) {
                     //printf("Row parity error!\n\r");
                     return 0;
                 }
@@ -348,13 +358,13 @@
             sum+=array[5*row+i];
         }
     }
-    
+
     // Even parity column check
-    for (int col=0 ; col <= 3 ; col++){
+    for (int col=0 ; col <= 3 ; col++) {
         sum = 0;
-        for (int i = 0; i <=10 ; i++){
-            if(i == 10){ // End of column
-                if(sum%2 != array[col+5*i]){
+        for (int i = 0; i <=10 ; i++) {
+            if(i == 10) { // End of column
+                if(sum%2 != array[col+5*i]) {
                     //printf("Col parity error!\n\r");
                     return 0;
                 }
@@ -362,25 +372,44 @@
             sum+=array[col+5*i];
         }
     }
-    
+
     return 1;
 }
 
-void RFIDer::decodeTag(void){
-    // Get 8-hex word id
-    for (int row = 0 ; row <= 7 ; row++){
-        tag[row] = 0; // init
-        for (int i = 0; i <=3 ; i++){
-            tag[row]+=array[5*(row+2)+i] << (3-i);
+
+void RFIDer::decodeTag(void)
+{
+    /* Init tag */
+    for(char i = 0 ; i < 10 ; i++) {
+        tag[i] = 0; // init
+    }
+    /* Get 10-hex half bytes id */
+    for (char row = 0 ; row < 10 ; row++) {
+        for (char col = 0; col < 4 ; col++) {
+            tag[row/2]+=array[5*row+col] << ((3-col) + 4*((row+1)%2));
         }
     }
 }
+/*
+void RFIDer::decodeTag(void)
+{
 
-char* RFIDer::getTag(void){
+     // Get 8-hex word id
+     for (int row = 0 ; row <= 7 ; row++) {
+         tag[row] = 0; // init
+         for (int i = 0; i <=3 ; i++) {
+             tag[row]+=array[5*(row+2)+i] << (3-i);
+         }
+     }
+}
+*/
+char* RFIDer::getTag(void)
+{
     tagAvailable = 0; // Reset flag
     return tag;
 }
 
-bool RFIDer::isTagAvailable(void){
+bool RFIDer::isTagAvailable(void)
+{
     return tagAvailable;
 }
\ No newline at end of file