EEP fORK

Dependencies:   BLE_API mbed nRF51822

Fork of MCS_LRF by Farshad N

Revision:
5:207d4b6dface
Parent:
4:76bd50c41d39
Child:
6:09cdafc3ffeb
--- a/main.cpp	Sun Oct 11 23:38:53 2015 +0000
+++ b/main.cpp	Fri Oct 16 03:10:47 2015 +0000
@@ -15,6 +15,7 @@
  */
 
 #include "mbed.h"
+#include "Serial.h"
 #include "BLE.h"
 #include "DeviceInformationService.h"
 #include "UARTService.h"
@@ -33,17 +34,31 @@
 #define TRACE(...)
 #endif /* #if NEED_CONSOLE_OUTPUT */
 
-#if NEED_TRACE
+#if NEED_PARSE_TRACE
 #define TRACE(...) { pc.printf(__VA_ARGS__); }
 #else
 #define TRACE(...)
 #endif /* #if NEED_TRACE */
 
+
+
 BLEDevice  ble;
-//DigitalOut led(p19);
-DigitalOut led(p21);
+
 DigitalOut trigger(p18);
-Serial reader(p9, p17);  // tx, rx   === NOTE tx port pin needs to be wired and verified
+
+#undef NORDIC  // is board nordic DK?
+
+#if NORDIC
+DigitalOut connectionLed(p21);
+DigitalOut triggerLed(p22);
+DigitalOut shutdown(p20);  // for nordic DK
+Serial reader(p13, p17);  // tx, rx   === NOTE tx port pin needs to be wired and verified  (for nordic DK)
+#else
+DigitalOut connectionLed(p19);
+DigitalOut triggerLed(p18);
+DigitalOut shutdown(p10); 
+Serial reader(p9, p8);  // tx, rx   === for adafruit BLE UART board (small blue board with red BLE module)
+#endif
 
 const static char     DEVICE_NAME[]    = "MCS_RFID";
 const static char     MANUFACTURER[]   = "MCS";
@@ -57,8 +72,9 @@
 static uint8_t isConnected = 0;
 
 // these values must macth definitions in the XML file accompanying this device
-const static uint16_t tagIdCmd      = 0x0001;
-const static uint16_t triggerCmd   = 0x0002;
+const static uint16_t tagIdCmd             = 0x0001;
+const static uint16_t hardwareTriggerCmd   = 0x0002;
+const static uint16_t softwareTriggerCmd   = 0x0003;
 
 // definition of the RFID packet
 #define HEADER_FLAG             0xFF
@@ -70,14 +86,15 @@
 #define BIT_96_TAG_PC_WORD      0x30
 #define SKIP_TO_TAG_EPC         2
 
-     
+
 #define SET_PARAM_CMD_MASK      0x8000  // commands with MSB set to 0 are to get the parameter and MSB of 1 to set the parameter
-#define READER_BAUD_RATE        115200  
+#define READER_BAUD_RATE        115200
 #define READ_BUF_SIZE           255
 #define TAG_EPC_BUF_SIZE        12
-       
+
 
 typedef enum {
+    AWAITING_PREAMBLE,
     AWAITING_HEADER,
     AWAITING_DATA_LEN,
     AWAITING_READ_CMD,
@@ -90,11 +107,62 @@
 } packetState_e;
 
 static uint8_t tagEPC[TAG_EPC_BUF_SIZE] = {0};
-static packetState_e state = AWAITING_HEADER;
+static packetState_e state = AWAITING_PREAMBLE;
 static uint8_t tagBufIndex = 0;
-static uint8_t skip = 0;
+//static uint8_t skip = 0;
 static uint16_t tagLen = 0;
-static uint16_t dataLen = 0;
+//static uint16_t dataLen = 0;
+
+// the preamble before 96 bit EPC tag data- we look for this before reading the EPC data 
+static uint8_t preamble[] = {0x00, 0x80, 0x30, 0x00};
+static uint8_t preambleIdx = 0;
+
+// commmands to initialize the Nano RFID reader- sniffied from coms going from the base edge device to the Nano reader
+// only use the get commands
+const uint8_t rfidInit[] = {
+//                            0xFF, 0x04, 0x06, 0x00, 0x01, 0xC2, 0x00, 0xA4,0x60,   // set baud rate to 115200
+//  0xFF, 0x00, 0x03, 0x1D, 0x0C,
+//  0xFF, 0x00, 0x03, 0x1D, 0x0C,
+//  0xF3, 0xE0, 0xE0, 0xE0, 0xE0, 0xFC, 0x1C, 0x00, 0x00, 0xFE, 0x80, 0xE6, 0xE0,
+//  0xFF, 0x00, 0x03, 0x1D, 0x0C,
+//  0xFF, 0x00, 0x0C, 0x1D, 0x03,                       //get current program
+//  0xFF, 0x00, 0x68, 0x1D, 0x67,                       // get power mode
+//  0xFF, 0x02, 0x6A, 0x01, 0x0E, 0x2E, 0x40,           // get reader configuration
+//  0xFF, 0x02, 0x6A, 0x01, 0x12, 0x2E, 0x5C,           // get reader configuration
+//  0xFF, 0x00, 0x63, 0x1D, 0x6C,                       // get current tag protocol
+    0xFF, 0x02, 0x93, 0x00, 0x05, 0x51, 0x7D,           // set current tag protocol to Gen2)
+//  0xFF, 0x01, 0x61, 0x05, 0xBD, 0xB8,                 // get antenna configuration
+//  0xFF, 0x02, 0x6A, 0x01, 0x0C, 0x2E, 0x42,           // get power mode
+//  0xFF, 0x02, 0x6A, 0x01, 0x0D, 0x2E, 0x43,           // get power mode
+//  0xFF, 0x02, 0x10, 0x00, 0x40, 0xF0, 0xD3,           // get hardware version
+    0xFF, 0x01, 0x97, 0x01, 0x4B, 0xBC,                 // set current region to NA
+    0xFF, 0x03, 0x9B, 0x05, 0x00, 0x00, 0xDC, 0xE8,     // set protocol configuration, Gen2, S0
+    0xFF, 0x04, 0x9B, 0x05, 0x01, 0x01, 0x00, 0xA2, 0xFD,  // set protocol configuration Gen2,
+    0xFF, 0x03, 0x9B, 0x05, 0x02, 0x01, 0xDE, 0xE9,       // set protocol configuration Gen2,
+//  0xFF, 0x01, 0x62, 0x00, 0xBE, 0xBD,                   // get read transmit power
+    0xFF, 0x02, 0x92, 0x0A, 0x8C, 0x4B, 0xD5,              // set read transmit power to 27dbm
+};
+
+// commmands to trigger a tag read- sniffied from coms going from the base edge device to the Nano reader
+uint8_t cmd_1[] = {0xFF, 0x00, 0x72, 0x1D, 0x7D};                               // get current temperature
+uint8_t cmd_2[] = {0xFF, 0x00, 0x2A, 0x1D, 0x25};                               // clear tag buffer
+uint8_t cmd_3[] = {0xFF, 0x03, 0x91, 0x02, 0x01, 0x01, 0x42, 0xC5};             // set antenna port
+uint8_t cmd_4[] = {0xFF, 0x05, 0x22, 0x00, 0x00, 0x13, 0x00, 0xFA, 0x2A, 0x17}; // read Tag Multiple
+uint8_t cmd_5[] = {0xFF, 0x03, 0x29, 0x01, 0xFF, 0x00, 0x1B, 0x03};             // get tag buffer
+
+typedef struct {
+    uint8_t* data;
+    uint8_t len;
+    uint16_t delayms;
+} command_t;
+
+command_t readCommands[] = {
+    {cmd_1, sizeof(cmd_1), 10},
+    {cmd_2, sizeof(cmd_2), 10},
+    {cmd_3, sizeof(cmd_3), 10},
+    {cmd_4, sizeof(cmd_4), 300},
+    {cmd_5, sizeof(cmd_5), 1}
+};
 
 
 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
@@ -103,14 +171,14 @@
     DEBUG("Restarting the advertising process\n\r");
     ble.startAdvertising();
     isConnected = 0;
-    led = isConnected;
+    connectionLed = isConnected;
 }
 
 void connectionCallback(const Gap::ConnectionCallbackParams_t *params)
 {
     DEBUG("Connected!\n\r");
     isConnected = 1;
-    led = isConnected;
+    connectionLed = isConnected;
 }
 
 static void sendBLENotification()
@@ -144,13 +212,32 @@
                 }
                 break;
 
-            case triggerCmd:
+            case hardwareTriggerCmd:
                 if(isSetCmd && params->len == 3) {
-                    // TODO also need to send this information to the LED controller
-                    DEBUG("CMD is SET triggerCmd\n\r");
+                    DEBUG("CMD is SET hardwareTriggerCmd\n\r");
                     trigger = 0;
+                    triggerLed = 1;
                     wait_ms(100);
                     trigger = 1;
+                    triggerLed = 0;
+                }
+                break;
+
+            case softwareTriggerCmd:
+                if(isSetCmd && params->len == 3) {      // TODO this command does not need a value
+                    DEBUG("CMD is SET softwareTriggerCmd\n\r");
+                    triggerLed = 1;
+                    for(int i = 0; i < sizeof(readCommands) / sizeof(command_t); i++) {
+                        uint8_t* d = readCommands[i].data;
+                        uint8_t len = readCommands[i].len;
+                        for(int j = 0; j < len; j++) {
+                            // DEBUG("%02X ", d[j]);
+                            reader.putc(d[j]);
+                        }
+                        wait_ms(readCommands[i].delayms);                        
+                        DEBUG("\r\n ");
+                    }
+                    triggerLed = 0;
                 }
                 break;
 
@@ -169,7 +256,7 @@
             DEBUG("0x%X ", params->data[i]);
         }
         DEBUG("\n\r", bytesRead);
-        
+
         // echo?
         // ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead);
 
@@ -177,131 +264,176 @@
     }
 }
 
-
 // State machine to parse the tag data received from serial port. It only looks for EPC data in the packet
 static void parsePacket(uint8_t d)
 {
-    TRACE("%02X    ", d);
+    //  TRACE("%02X ", d);
+    tagLen = 12;
     switch (state) {
-        case AWAITING_HEADER:
-            if(d == HEADER_FLAG) {
-                TRACE("AWAITING_DATA_LEN \r\n");
-                state = AWAITING_DATA_LEN;
-                skip = 0;
-            }
-            break;
-
-        case AWAITING_DATA_LEN:
-            if(d > 0) {
-                dataLen = d;
-                state = AWAITING_READ_CMD;
-                TRACE("AWAITING_READ_CMD \r\n");
-            } else {
-                state = AWAITING_HEADER;
-            }
-            break;
-
-        case AWAITING_READ_CMD:
-            if(d == EPC_READ_CMD) {
-                state = AWAITING_STATUS_1;
-                TRACE("AWAITING_STATUS_1 \r\n");
-            } else {
-                state = AWAITING_HEADER;
-            }
-            break;
-
-        case AWAITING_STATUS_1:
-            if(d == 0) {
-                state = AWAITING_STATUS_2;
-                TRACE("AWAITING_STATUS_2 \r\n");
+        case AWAITING_PREAMBLE:
+            if(d == preamble[preambleIdx]) {
+                preambleIdx++;
+                if(preambleIdx == sizeof(preamble)) {
+                    state = READING_TAG_EPC;
+                }
             } else {
-                state = AWAITING_HEADER;
-            }
-            break;
-
-        case AWAITING_STATUS_2:
-            if(d == 0) {
-                state = AWAITING_EPC_LENGTH;
-                TRACE("AWAITING_EPC_LENGTH_1 \r\n");
-            } else {
-                state = AWAITING_HEADER;
-            }
-            break;
-
-        case AWAITING_EPC_LENGTH:
-         if(++skip >= SKIP_TO_BIT_FLAG) {
-            if(d == BIT_64_TAG_FLAG) {
-                state = AWAITING_PC_WORD;
-                tagLen = 8;
-                TRACE("AWAITING_PC_WORD \r\n");
-            } else if (d == BIT_96_TAG_FLAG) {
-                state = AWAITING_PC_WORD;
-                tagLen = 12;
-                TRACE("AWAITING_PC_WORD \r\n");
-            } else {
-                state = AWAITING_HEADER;
-            }
-            }
-            break;
-
-        case AWAITING_PC_WORD:
-            if((tagLen == 8 && d == BIT_64_TAG_PC_WORD) || (tagLen == 12 && d == BIT_96_TAG_PC_WORD)) {
-                state = READING_TAG_EPC;
-                tagBufIndex = 0;
-                TRACE("READING_TAG_EPC \r\n");
-                skip = 0;
-            } else {
-                state = AWAITING_HEADER;
+                preambleIdx = 0;
             }
             break;
 
         case READING_TAG_EPC:
-            if(++skip >= SKIP_TO_TAG_EPC) {
-                if(tagBufIndex < tagLen) {
-                    tagEPC[tagBufIndex++] = d;
-                    TRACE("%02X ", d);
-                } else {
-                    // don't worry about CRC for now
-                    state = AWAITING_HEADER;
-                    TRACE("AWAITING_HEADER \r\n");
+            if(tagBufIndex < tagLen) {
+                tagEPC[tagBufIndex++] = d;
+                TRACE("%02X ", d);
+            } else {
+                // don't worry about CRC for now
+                state = AWAITING_PREAMBLE;
+                preambleIdx = 0;
+                tagBufIndex = 0;
+                TRACE("AWAITING_PREAMBLE \r\n");
 
-                    TRACE("TAG EPC: ============\r\n");
-                    for(int i = 0; i < tagLen; i++) {
-                        TRACE("%02X ", tagEPC[i]);
-                        pc.printf("%02X ", tagEPC[i]);
-                    }
-                    pc.printf("\r\n");
-                    
-                    sendBLENotification();
+                TRACE("TAG EPC: ============\r\n");
+                for(int i = 0; i < tagLen; i++) {
+                    TRACE("%02X ", tagEPC[i]);
+                    //      pc.printf("%02X ", tagEPC[i]);
                 }
+                // pc.printf("\r\n");
+
+                sendBLENotification();
+                triggerLed = 0;
             }
+
             break;
     }
 }
 
+
+//// State machine to parse the tag data received from serial port. It only looks for EPC data in the packet
+//static void parsePacket_2(uint8_t d)
+//{
+//  //  TRACE("%02X ", d);
+//    switch (state) {
+//        case AWAITING_HEADER:
+//            if(d == HEADER_FLAG) {
+//                TRACE("AWAITING_DATA_LEN \r\n");
+//                state = AWAITING_DATA_LEN;
+//                skip = 0;
+//            }
+//            break;
+//
+//        case AWAITING_DATA_LEN:
+//            if(d > 0) {
+//                dataLen = d;
+//                state = AWAITING_READ_CMD;
+//                TRACE("AWAITING_READ_CMD \r\n");
+//            } else {
+//                state = AWAITING_HEADER;
+//            }
+//            break;
+//
+//        case AWAITING_READ_CMD:
+//            if(d == EPC_READ_CMD) {
+//                state = AWAITING_STATUS_1;
+//                TRACE("AWAITING_STATUS_1 \r\n");
+//            } else {
+//                state = AWAITING_HEADER;
+//            }
+//            break;
+//
+//        case AWAITING_STATUS_1:
+//            if(d == 0) {
+//                state = AWAITING_STATUS_2;
+//                TRACE("AWAITING_STATUS_2 \r\n");
+//            } else {
+//                state = AWAITING_HEADER;
+//            }
+//            break;
+//
+//        case AWAITING_STATUS_2:
+//            if(d == 0) {
+//                state = AWAITING_EPC_LENGTH;
+//                TRACE("AWAITING_EPC_LENGTH_1 \r\n");
+//            } else {
+//                state = AWAITING_HEADER;
+//            }
+//            break;
+//
+//        case AWAITING_EPC_LENGTH:
+//         if(++skip >= SKIP_TO_BIT_FLAG) {
+//            if(d == BIT_64_TAG_FLAG) {
+//                state = AWAITING_PC_WORD;
+//                tagLen = 8;
+//                TRACE("AWAITING_PC_WORD \r\n");
+//            } else if (d == BIT_96_TAG_FLAG) {
+//                state = AWAITING_PC_WORD;
+//                tagLen = 12;
+//                TRACE("AWAITING_PC_WORD \r\n");
+//            } else {
+//                state = AWAITING_HEADER;
+//            }
+//            }
+//            break;
+//
+//        case AWAITING_PC_WORD:
+//            if((tagLen == 8 && d == BIT_64_TAG_PC_WORD) || (tagLen == 12 && d == BIT_96_TAG_PC_WORD)) {
+//                state = READING_TAG_EPC;
+//                tagBufIndex = 0;
+//                TRACE("READING_TAG_EPC \r\n");
+//                skip = 0;
+//            } else {
+//                state = AWAITING_HEADER;
+//            }
+//            break;
+//
+//        case READING_TAG_EPC:
+//            if(++skip >= SKIP_TO_TAG_EPC) {
+//                if(tagBufIndex < tagLen) {
+//                    tagEPC[tagBufIndex++] = d;
+//                    TRACE("%02X ", d);
+//                } else {
+//                    // don't worry about CRC for now
+//                    state = AWAITING_HEADER;
+//                    TRACE("AWAITING_HEADER \r\n");
+//
+//                    TRACE("TAG EPC: ============\r\n");
+//                    for(int i = 0; i < tagLen; i++) {
+//                        TRACE("%02X ", tagEPC[i]);
+//                  //      pc.printf("%02X ", tagEPC[i]);
+//                    }
+//                   // pc.printf("\r\n");
+//
+//                    sendBLENotification();
+//                }
+//            }
+//            break;
+//    }
+//}
+
+
 void periodicCallback(void)
 {
-    //  led1 = !led1;
+    triggerLed = 0;
 }
 
 // this is an ISR, so do not spend too much time here and be careful with printing debug info
 void readerCallback()
 {
     // Note: Need to actually read from the serial to clear the RX interrupt
-    while(reader.readable()) {           
+    while(reader.readable()) {
         parsePacket(reader.getc());
     }
-    
-    // make sure LED stays on
-    led = isConnected;
 }
 
 int main(void)
 {
     // default state is unknown
-    led = 0;
-    
+    connectionLed = 0;
+
     trigger = 1;
+    triggerLed = 0;
+
+    // make sure Reader is not shutdown
+    shutdown = 1;
 
     Ticker ticker;
     ticker.attach(periodicCallback, 5);
@@ -334,6 +466,16 @@
     reader.baud(READER_BAUD_RATE);
     reader.attach(&readerCallback);
 
+    // initialise reader
+    //reader.write(rfidInit, sizeof(rfidInit)/sizeof(uint8_t), &writeCallback);
+    wait_ms(100);
+    for(int i = 0; i < sizeof(rfidInit)/sizeof(rfidInit[0]); i++) {
+        if(rfidInit[i] == 0xFF) {
+            wait_ms(50);
+        }
+        reader.putc(rfidInit[i]);
+    }
+
     while (true) {
         ble.waitForEvent();
     }