Fork to see if I can get working

Dependencies:   BufferedSerial OneWire WinbondSPIFlash libxDot-dev-mbed5-deprecated

Fork of xDotBridge_update_test20180823 by Matt Briggs

Revision:
77:176e3eb8f712
Parent:
76:d1b20a259d8f
Child:
78:43f074baac34
--- a/xDotBridge/src/SerialTermMgr.cpp	Tue May 16 15:15:58 2017 -0600
+++ b/xDotBridge/src/SerialTermMgr.cpp	Wed May 17 16:41:56 2017 -0600
@@ -11,13 +11,17 @@
 extern Serial pc;
 const char ACK = 0x06;
 const char NAK = 0x15;
+const char SOH = 0x01;
+const char EOT = 0x04;
+const char SUB = 0x1A;
 
-SerialTermMgr::SerialTermMgr(BaseboardIO *bbio, float fwVersion)
+SerialTermMgr::SerialTermMgr(BaseboardIO *bbio, WinbondSPIFlash *flash, float fwVersion)
 {
     mPc = NULL;
     mFwVersion = fwVersion;
     mCurrScreen = mainScreenId;
     mBbio = bbio;
+    mFlash = flash;
 }
 void SerialTermMgr::printScreen()
 {
@@ -230,30 +234,65 @@
 
 bool SerialTermMgr::xmodem2Flash ()
 {
-    mPc->printf("Ready to receive file");
-    // TODO may need to buffer input
+    mPc->printf("Ready to receive file\r\n");
+    mFlash->releaseFromPowerDown();
+    mFlash->chipErase();
     char packetBuf[132];
+    uint16_t packetNum = 1;
+    uint8_t csum = 0;
     unsigned char pktIdx = 0;
     time_t lastValidInput = 0;
     while (1) {
+        // Send a NAK if a packet is not received within timeout period
         if ((time(NULL)-lastValidInput) > XMODEM_TIMEOUT){
-            mPc->printf("%c", 0x15); // Send NAK
-            lastValidInput = time(NULL);
             pc.printf("Total chars: %d\r\n", pktIdx);
             pktIdx = 0;
+            mPc->printf("%c", NAK); // Send NAK
+            lastValidInput = time(NULL);
+            // TODO need to timeout of XMODEM
         }
         if (mPc->readable()) {
-            mPc->gets(packetBuf, 132);
-            pc.printf("Got full packet.\r\n");
-            for (int i=0; i<132; i++) {
-                pc.printf("%02X", packetBuf[i]);
+            packetBuf[pktIdx++] = mPc->getc();
+            if (pktIdx == 1 && packetBuf[0] == EOT) {
+                lastValidInput = time(NULL);
+                mPc->printf("%c", ACK); // Last ACK
+                wait(0.01);
+                mPc->printf("Success.\r\n");
+                pc.printf("Success on xmodem.\r\n");
+                break;
             }
-//            if (pktIdx >= 132) {
+            else if (pktIdx >= 132) {
+                lastValidInput = time(NULL);
+                csum = 0;
 //                pc.printf("Got full packet.\r\n");
-//                for (int i=0; i<132; i++) {
+//                pc.printf("Header SOH: %02X, Packet #%02X, inv #%02x\r\n", packetBuf[0], packetBuf[1], packetBuf[2]);
+                for (int i=3; i<131; i++) {
 //                    pc.printf("%02X", packetBuf[i]);
-//                }
-//            }
+                    csum += packetBuf[i];
+                }
+                pktIdx = 0;
+//                pc.printf("\r\ncsum = %d (%02X). Got %d (%02X)\r\n", csum, csum, packetBuf[131], packetBuf[131]);
+                if ((csum == packetBuf[131]) && (packetBuf[0] == SOH) &&
+                    (packetBuf[1] == ((uint8_t)packetNum)) && (((uint8_t)~packetBuf[2]) == ((uint8_t)packetNum))) {
+//                    if (packetBuf[1] == packetNum) {
+//                        pc.printf("One works.\r\n");
+//                    }
+//                    if (((uint8_t)~packetBuf[2]) == packetNum) {
+//                        pc.printf("Two works.\r\n");
+//                    }
+//                    pc.printf("%02X == %02X\r\n", packetNum, (uint8_t)~packetBuf[2]);
+                    mFlash->writeStream(FLASH_BIN_OFFSET+packetNum*XMODEM_PACKET_SIZE, packetBuf+3, XMODEM_PACKET_SIZE);
+                    mPc->printf("%c", ACK);
+                    packetNum++;
+                }
+                else {
+                    mPc->printf("%c", NAK);
+                }
+            }
         }
     }
+    mCurrScreen = mainScreenId; // Just return to main screen for now
+    mFlash->powerDown();
+
+    return true;
 }