![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Fork to see if I can get working
Dependencies: BufferedSerial OneWire WinbondSPIFlash libxDot-dev-mbed5-deprecated
Fork of xDotBridge_update_test20180823 by
Diff: xDotBridge/src/SerialTermMgr.cpp
- Revision:
- 78:43f074baac34
- Parent:
- 77:176e3eb8f712
- Child:
- 91:8196900df6fe
--- a/xDotBridge/src/SerialTermMgr.cpp Wed May 17 16:41:56 2017 -0600 +++ b/xDotBridge/src/SerialTermMgr.cpp Mon Nov 13 09:50:49 2017 -0700 @@ -7,6 +7,7 @@ #include "SerialTermMgr.h" #include "UserInterface.h" +#include "bootloader.h" extern Serial pc; const char ACK = 0x06; @@ -22,6 +23,7 @@ mCurrScreen = mainScreenId; mBbio = bbio; mFlash = flash; + } void SerialTermMgr::printScreen() { @@ -242,6 +244,7 @@ uint8_t csum = 0; unsigned char pktIdx = 0; time_t lastValidInput = 0; + unsigned char nTimeouts = 0; while (1) { // Send a NAK if a packet is not received within timeout period if ((time(NULL)-lastValidInput) > XMODEM_TIMEOUT){ @@ -249,39 +252,41 @@ pktIdx = 0; mPc->printf("%c", NAK); // Send NAK lastValidInput = time(NULL); - // TODO need to timeout of XMODEM + nTimeouts++; + if (nTimeouts >= MAX_TIMEOUTS) { + mPc->printf("Programming timed out.\r\n"); + pc.printf("Programming timed out.\r\n"); + break; + } + } if (mPc->readable()) { packetBuf[pktIdx++] = mPc->getc(); if (pktIdx == 1 && packetBuf[0] == EOT) { lastValidInput = time(NULL); mPc->printf("%c", ACK); // Last ACK + mPc->printf("Success.\r\n"); + pc.printf("Success on xmodem. Reset in progress.\r\n"); + writeBootloaderCtrlPage(packetNum*XMODEM_PACKET_SIZE); +// copyNewProgram(); + wait(0.01); - mPc->printf("Success.\r\n"); - pc.printf("Success on xmodem.\r\n"); + mFlash->powerDown(); + NVIC_SystemReset(); + // Should never get here because of reset. break; } else if (pktIdx >= 132) { lastValidInput = time(NULL); + nTimeouts = 0; // Clear n timeouts with new packet csum = 0; -// pc.printf("Got full packet.\r\n"); -// 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); + mFlash->writeStream(FLASH_BIN_OFFSET+(packetNum-1)*XMODEM_PACKET_SIZE, packetBuf+3, XMODEM_PACKET_SIZE); mPc->printf("%c", ACK); packetNum++; } @@ -296,3 +301,16 @@ return true; } + +void SerialTermMgr::writeBootloaderCtrlPage(uint32_t nBytes) +{ + // This should always be less than 256 bytes + const unsigned int ctrlSize = sizeof(NEW_CODE)+sizeof(nBytes); + char buf[ctrlSize]; + std::memcpy(buf, NEW_CODE, sizeof(NEW_CODE)); + std::memcpy(buf+sizeof(NEW_CODE), &nBytes, sizeof(nBytes)); + // TODO add reprogram counter just for fun + + // Copy to first page of flash + mFlash->writeStream(0, buf, sizeof(buf)); +}