Control Code with I/O and ADC working

Dependencies:   MODSERIAL mbed

Revision:
15:74a01aaeb60e
Parent:
14:69cd53434783
Child:
16:82d941b1ef21
--- a/main.cpp	Wed May 16 19:31:12 2018 +0000
+++ b/main.cpp	Mon Jun 11 21:10:14 2018 +0000
@@ -1,6 +1,7 @@
 // MBED SCRIPT FOR CONTROLLING THE TEMPERATURE CONTROLLED TEST FIXTURE (TCTF)
 // DATE: SEPTEMBER 2017
 
+#define FW_VERSION      "V.010"
 #include "mbed.h"
 #include "MODSERIAL.h"
 #include "MCP23008.h"
@@ -8,30 +9,30 @@
 #include <string>
 
 //DEFINITIVE VARIABLES
-#define DEBUG                0
-#define DEBUG1               0
-#define DEBUG2               0
-#define DEBUG3               1
-#define DEBUG5               0
-#define CHN_COUNT            8
-#define MIN_TEMP             10
-#define MAX_TEMP             65
-#define TEMP_MARGIN          20
-#define HYST_LOW             0.3
-#define HYST_HIGH            1
-#define SAMPLES              5
-#define I2C_Freq             2000
-#define VALVE                1
-#define HEATER               2
-#define STATUS_GOOD          3
-#define STATUS_BAD           0
-#define sizeLUT              34
-#define BACK_THERM           0
-#define FRONT_THERM          1
-#define HEAT_FET_AMP         2
-#define VALVE_FET_AMP        3
-#define FET_ON_CURRENT       1.12
-#define ROOM_TEMP            22
+#define DEBUG            0
+#define DEBUG1           0
+#define DEBUG2           0
+#define DEBUG3           1
+#define DEBUG5           0
+#define CHN_COUNT        8
+#define MIN_TEMP         10
+#define MAX_TEMP         65
+#define TEMP_MARGIN      20
+#define HYST_LOW         0.3
+#define HYST_HIGH        1
+#define SAMPLES          5
+#define I2C_Freq         2000
+#define VALVE            1
+#define HEATER           2
+#define STATUS_GOOD      3
+#define STATUS_BAD       0
+#define sizeLUT          34
+#define BACK_THERM       0
+#define FRONT_THERM      1
+#define HEAT_FET_AMP     2
+#define VALVE_FET_AMP    3
+#define FET_ON_CURRENT   1.12
+#define ROOM_TEMP        22
 #define MAX_HEATER_ON_TIME   25
 #define MAX_CHILL_TIME       10 //10sec
 
@@ -44,9 +45,11 @@
 #define DELIMETER               0x2E
 #define SET_TEMPERATURE         0xB0
 #define SELECT_CHANNEL          0xB1
-#define CMD_RESPONSE            0xD0
-#define CMD_DATA                0xD1
-#define ERROR_DATA              0xD2
+#define READ_UID                0xB2
+#define RESPONSE_DATA           0xD0
+#define TEMP_DATA               0xD1
+#define UID_DATA                0xD2
+#define ERROR_DATA              0xDF
 
 
 unsigned int chanSel_SendTemp = 0;
@@ -91,6 +94,24 @@
     unsigned char EOF_flag;
 } RESPONSE_CMD;
 
+typedef struct {
+    HOST_CMD_HEADER cmd_header;
+    unsigned char chanIDSel;
+    unsigned char chanDelim;
+    unsigned char chanStat;
+} READ_UID_CMD;
+
+typedef struct {
+    unsigned char SOF_flag;
+    unsigned char cmd_type;
+    unsigned char len;
+    unsigned char Delim;
+    uint32_t UIDMH;
+    uint32_t UIDML;
+    uint32_t UIDL;
+    unsigned char EOF_flag;
+} UID_RESPONSE;
+
 //TCTF CHANNEL DATA
 struct CHNL_DATA{
    bool status;
@@ -294,6 +315,8 @@
 DigitalOut rLed(LED1);
 DigitalOut gLed(LED2);
 
+void sendUID ();
+
 //***********************************************
 // Rx Interrupt from serial Host interface
 //***********************************************
@@ -331,8 +354,7 @@
     pRxPktHdr = (HOST_CMD_HEADER *)rxBuf;
 
 #ifdef DEBUG
-    pc.printf("DBG: fl = %02x\n", pRxPktHdr->SOF_flag);
-    pc.printf("DBG: len = %02x\n", pRxPktHdr->len);
+    pc.printf("DBG: fl=%02x, len=%02x\n", pRxPktHdr->SOF_flag, pRxPktHdr->len);
 #endif
 
     // Exit if the packet does not contain correct header
@@ -346,10 +368,9 @@
             // Process set temp for specified channel
             {
                 SET_TEMPERATURE_CMD *pRxPkt = (SET_TEMPERATURE_CMD *)(rxBuf);
-#if 1 //def DEBUG
-                pc.printf("DBG: ch = %02x\n", pRxPkt->chanID);
-                pc.printf("DBG: tempSet = %f\n", pRxPkt->setTemp);
-                pc.printf("DBG: chanStat = %02x\n", pRxPkt->chanStat);
+#ifdef DEBUG
+                pc.printf("DBG: SETTEMP: ch = %02x, tempSet = %f, chanStat = %02x\n",
+                            pRxPkt->chanID, pRxPkt->setTemp, pRxPkt->chanStat);
 #endif
                 if ((pRxPkt->tempDelim != DELIMETER) || (pRxPkt->chanStatDelim != DELIMETER)) {
                     // Send NAK back
@@ -372,19 +393,30 @@
                 SELECT_CHANNEL_CMD *pRxPkt = (SELECT_CHANNEL_CMD *)(rxBuf);
 
                 chanSel_SendTemp = pRxPkt->chanIDSel;
-#if 1 //def DEBUG
-                pc.printf("DBG: chanSel = %02x\n", pRxPkt->chanIDSel);
-                pc.printf("DBG: chanStat = %02x\n", pRxPkt->chanStat);
+#ifdef DEBUG
+                pc.printf("DBG: CHAN_SEL: chan=%02x, chanStat = %02x\n", pRxPkt->chanIDSel, pRxPkt->chanStat);
 #endif
             }
             break;
 
+        case READ_UID:
+            {
+                READ_UID_CMD *pRxPkt = (READ_UID_CMD *)(rxBuf);
+#ifdef DEBUG
+                pc.printf("DBG: Read UID: chan%02x\n", pRxPkt->chanIDSel);
+#endif
+                sendUID();
+            }
+            break;
+
         default:
             // Error
             break;
     }
 }
 
+
+
 /* Function: get_temp
    **************************************************************
    Description: Retrieve data from thermistor
@@ -561,7 +593,7 @@
     int i;
 
     response.SOF_flag = TX_SOF;
-    response.cmd_type = CMD_DATA;
+    response.cmd_type = TEMP_DATA;
     response.len = 9;
     response.Delim = DELIMETER;
     response.data = (float)currentTemp;
@@ -574,6 +606,30 @@
 }
 
 //***************************************************************
+// Build packet with Board UID (Unique Identification)
+//***************************************************************
+void sendUID ()
+{
+    UID_RESPONSE response;
+    unsigned char *ptr = (unsigned char *)&response;
+    int i;
+
+    response.SOF_flag = TX_SOF;
+    response.cmd_type = UID_DATA;
+    response.len = 17;
+    response.Delim = DELIMETER;
+    response.UIDMH = (uint32_t)SIM->UIDMH;
+    response.UIDML = (uint32_t)SIM->UIDML;
+    response.UIDL = (uint32_t)SIM->UIDL;
+    response.EOF_flag = TX_EOF;
+
+    // Send response to GUI
+    for (i=0; i < response.len; i++, ptr++)
+        pc.printf("%02x", *ptr);
+    pc.printf("\n");
+}
+
+//***************************************************************
 // Build packet with errors to send to GUI
 //***************************************************************
 void sendError (int chan, float error)
@@ -631,7 +687,7 @@
     }
     if(chnlStatus[chnl].heater_init_time != 0){
         int init_time = chnlStatus[chnl].heater_init_time;
-        int on_time_heater = currTimeMin - init_time;   
+        int on_time_heater = currTimeMin - init_time;
         //account for 0 crossover
         if(init_time > currTimeMin){
             on_time_heater = (60 - init_time)+currTimeMin;
@@ -662,6 +718,7 @@
     Timer t_cool;
     int time_min = 0;
     int init_heat_time = 0;
+    uint32_t UIDMH, UIDML, UIDL;
 
     // Setup serial port
     // Look for RX_EOF
@@ -675,10 +732,17 @@
 
     t.start();
 
+    sendUID();
+
+    UIDMH = (uint32_t)SIM->UIDMH;
+    UIDML = (uint32_t)SIM->UIDML;
+    UIDL = (uint32_t)SIM->UIDL;
+
     while(1) {
 
-        if(DEBUG3)
-            pc.printf("DBG: PROGRAM STARTED \r\n");
+        if(DEBUG3) {
+            pc.printf("DBG: <%f>[UID: %04X%08X%08X] PSTARTED\n", t.read(), UIDMH, UIDML, UIDL);
+        }
 
         //setup timer used to track how long the heater is on
         float time_sec = t.read();
@@ -701,7 +765,9 @@
             //check if we received data/need to update TCTF data
             if(dataReceived){
                 dataReceived = false;
+#ifdef DEBUG
                 pc.printf("DBG: %d bytes Rx'd\n", pc.rxBufferGetCount());
+#endif
                 pc.move(rxBuf, 50);
                 parseRXData();
                 pc.rxBufferFlush();
@@ -720,7 +786,7 @@
 
             //CONTROL LOOP:
             if(chnlStatus[chnl].status == 1){
-                if(DEBUG3) pc.printf("DBG: [%d] Temp: F: %f B: %f\r\n", chnl, currentTempFront, currentTempBack);
+                if(DEBUG) pc.printf("DBG: [%d] Temp: F: %f B: %f\r\n", chnl, currentTempFront, currentTempBack);
                 //main loop for channels
                 if(chnlStatus[chnl].error == 0){
                     if(currentTemp > ((chnlStatus[chnl].setTemp)+HYST_HIGH)){
@@ -780,13 +846,13 @@
             }
             else{
                 if(chnlStatus[chnl].error == 0){
-                    //turn off chiller
-                    turn_valve_off(chnl);
-                    //turn off heater
-                    turn_heater_off(chnl);
-                    //turn on green LED status light
-                    status_led(chnl, 1);
-                }
+                //turn off chiller
+                turn_valve_off(chnl);
+                //turn off heater
+                turn_heater_off(chnl);
+                //turn on green LED status light
+                status_led(chnl, 1);
+            }
                 else{
                     status_led(chnl, STATUS_BAD);
                 }