Fork to see if I can get working

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

Fork of xDotBridge_update_test20180823 by Matt Briggs

Files at this revision

API Documentation at this revision

Comitter:
Matt Briggs
Date:
Wed May 17 16:41:56 2017 -0600
Parent:
76:d1b20a259d8f
Child:
78:43f074baac34
Commit message:
Added serial terminal and Buffered lib

Changed in this revision

BufferedSerial.lib Show annotated file Show diff for this revision Revisions of this file
xDotBridge/inc/SerialTermMgr.h Show annotated file Show diff for this revision Revisions of this file
xDotBridge/src/SerialTermMgr.cpp Show annotated file Show diff for this revision Revisions of this file
xDotBridge/src/main.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/BufferedSerial.lib	Wed May 17 16:41:56 2017 -0600
@@ -0,0 +1,1 @@
+https://developer.mbed.org/users/sam_grove/code/BufferedSerial/#a0d37088b405
--- a/xDotBridge/inc/SerialTermMgr.h	Tue May 16 15:15:58 2017 -0600
+++ b/xDotBridge/inc/SerialTermMgr.h	Wed May 17 16:41:56 2017 -0600
@@ -9,13 +9,16 @@
 #define XDOTBRIDGE_INC_SERIALTERMMGR_H_
 
 #include "mbed.h"
+#include "BufferedSerial.h"
+#include "WinbondSPIFlash.h"
 #include "BaseboardIO.h"
 
 const uint32_t TERM_BAUD = 115200;
 const uint8_t TERM_TIMEOUT = 30; // In seconds
 
 const float XMODEM_TIMEOUT = 10.0;
-const uint8_t FLASH_BIN_OFFSET = 0x20; // Skip first 32 bytes
+const uint16_t FLASH_BIN_OFFSET = 256; // Skip first flash page
+const uint16_t XMODEM_PACKET_SIZE = 128;
 
 enum ScreenId {
     mainScreenId,
@@ -35,7 +38,8 @@
     ScreenId mCurrScreen; // Current selection
     float mFwVersion;
     BaseboardIO *mBbio; // Handle for reading states
-    Serial *mPc;
+    BufferedSerial *mPc;
+    WinbondSPIFlash *mFlash;
 
     bool inputMainPage(char in);
     void printMainScreen();
@@ -45,8 +49,8 @@
     void printEnterProgMode();
     bool xmodem2Flash();
 public:
-    SerialTermMgr(BaseboardIO *bbio, float fwVersion);
-    void regSerial (Serial *pc) {
+    SerialTermMgr(BaseboardIO *bbio, WinbondSPIFlash *flash, float fwVersion);
+    void regSerial (BufferedSerial *pc) {
         mPc = pc;
     }
     ScreenId getCurrentScreenId() {
--- 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;
 }
--- a/xDotBridge/src/main.cpp	Tue May 16 15:15:58 2017 -0600
+++ b/xDotBridge/src/main.cpp	Wed May 17 16:41:56 2017 -0600
@@ -3,6 +3,7 @@
 #include "xdot_flash.h"
 #include "dot_util.h"
 #include "RadioEvent.h"
+#include "BufferedSerial.h"
 #include "WinbondSPIFlash.h"
 #include "UserInterface.h"
 //#include <xdot_low_power.h>
@@ -21,7 +22,8 @@
 #ifndef __TEST__ // Exclude code for tests
 Serial pc(USBTX, USBRX);
 //RawSerial outPc(UART_TX, UART_RX);
-Serial *outPc;
+//Serial *outPc;
+BufferedSerial *outPc;
 InterruptIn uart1RxIntIn (UART_RX);
 
 //// Serial interrupts / buffer code
@@ -87,7 +89,7 @@
     CommProtocolPeerBrute *protocol = new CommProtocolPeerBrute();
     BaseboardIO *bbio = new BaseboardIO();
     WinbondSPIFlash *flash = new WinbondSPIFlash(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_NSS);
-    SerialTermMgr serialTermMgr(bbio, BridgeVersion);
+    SerialTermMgr serialTermMgr(bbio, flash, BridgeVersion);
 
     pc.baud(115200);
     CmdResult result;
@@ -294,7 +296,7 @@
             uart1RxIntIn.disable_irq();
 //            pc.printf("Got uart flag!!!\r\n");
             uartRxFlag = false;
-            outPc = new Serial(UART1_TX, UART1_RX);
+            outPc = new BufferedSerial(UART1_TX, UART1_RX);
             outPc->baud(TERM_BAUD);
             serialTermMgr.regSerial(outPc);
             char c;
@@ -310,8 +312,8 @@
             while(!quit && (time(NULL) < (termLastAction + TERM_TIMEOUT))) {
                 if (outPc->readable()) {
 //                    pc.printf("Got %d\r\n", outPc->getc());
+                    quit = serialTermMgr.input();
                     termLastAction = time(NULL);
-                    quit = serialTermMgr.input();
                 }
             }
             if (quit) {
@@ -320,6 +322,7 @@
             else {
                 outPc->printf("Terminal timeout resuming operation <press any key to reactivate>...\r\n");
             }
+            wait(0.01); // Wait for end message to be shifted out
             serialTermMgr.regSerial(NULL);
             delete outPc;
             uart1RxIntIn.enable_irq();