![](/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
xDotBridge/src/SerialTermMgr.cpp@94:8028c07f71fa, 2017-11-22 (annotated)
- Committer:
- mbriggs_vortex
- Date:
- Wed Nov 22 01:11:36 2017 +0000
- Revision:
- 94:8028c07f71fa
- Parent:
- 93:f3adc409c54e
- Child:
- 98:3609f600c2f5
Working blink and working key check.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Matt Briggs | 75:600cb3a9f126 | 1 | /* |
Matt Briggs | 75:600cb3a9f126 | 2 | * SerialTermMgr.cpp |
Matt Briggs | 75:600cb3a9f126 | 3 | * |
Matt Briggs | 75:600cb3a9f126 | 4 | * Created on: May 8, 2017 |
Matt Briggs | 75:600cb3a9f126 | 5 | * Author: mbriggs |
Matt Briggs | 75:600cb3a9f126 | 6 | */ |
Matt Briggs | 75:600cb3a9f126 | 7 | |
Matt Briggs | 75:600cb3a9f126 | 8 | #include "SerialTermMgr.h" |
Matt Briggs | 75:600cb3a9f126 | 9 | #include "UserInterface.h" |
Matt Briggs | 75:600cb3a9f126 | 10 | |
Matt Briggs | 76:d1b20a259d8f | 11 | extern Serial pc; |
Matt Briggs | 76:d1b20a259d8f | 12 | const char ACK = 0x06; |
Matt Briggs | 76:d1b20a259d8f | 13 | const char NAK = 0x15; |
Matt Briggs | 77:176e3eb8f712 | 14 | const char SOH = 0x01; |
Matt Briggs | 77:176e3eb8f712 | 15 | const char EOT = 0x04; |
Matt Briggs | 77:176e3eb8f712 | 16 | const char SUB = 0x1A; |
Matt Briggs | 76:d1b20a259d8f | 17 | |
Matt Briggs | 77:176e3eb8f712 | 18 | SerialTermMgr::SerialTermMgr(BaseboardIO *bbio, WinbondSPIFlash *flash, float fwVersion) |
Matt Briggs | 75:600cb3a9f126 | 19 | { |
Matt Briggs | 75:600cb3a9f126 | 20 | mPc = NULL; |
Matt Briggs | 75:600cb3a9f126 | 21 | mFwVersion = fwVersion; |
Matt Briggs | 75:600cb3a9f126 | 22 | mCurrScreen = mainScreenId; |
Matt Briggs | 75:600cb3a9f126 | 23 | mBbio = bbio; |
Matt Briggs | 77:176e3eb8f712 | 24 | mFlash = flash; |
mbriggs_vortex | 78:43f074baac34 | 25 | |
Matt Briggs | 75:600cb3a9f126 | 26 | } |
Matt Briggs | 75:600cb3a9f126 | 27 | void SerialTermMgr::printScreen() |
Matt Briggs | 75:600cb3a9f126 | 28 | { |
Matt Briggs | 75:600cb3a9f126 | 29 | switch (mCurrScreen) { |
Matt Briggs | 75:600cb3a9f126 | 30 | case mainScreenId: |
Matt Briggs | 75:600cb3a9f126 | 31 | printMainScreen(); |
Matt Briggs | 75:600cb3a9f126 | 32 | break; |
Matt Briggs | 75:600cb3a9f126 | 33 | case genInfoScreenId: |
Matt Briggs | 75:600cb3a9f126 | 34 | printGenInfo(); |
Matt Briggs | 75:600cb3a9f126 | 35 | break; |
Matt Briggs | 75:600cb3a9f126 | 36 | case settingsScreenId: |
Matt Briggs | 75:600cb3a9f126 | 37 | case statsScreenId: |
Matt Briggs | 75:600cb3a9f126 | 38 | case errorLogScreenId: |
Matt Briggs | 75:600cb3a9f126 | 39 | case liveLogScreenId: |
Matt Briggs | 75:600cb3a9f126 | 40 | case enterSerialBridgeScreenId: |
Matt Briggs | 76:d1b20a259d8f | 41 | printMainScreen(); |
Matt Briggs | 76:d1b20a259d8f | 42 | break; |
Matt Briggs | 75:600cb3a9f126 | 43 | case enterProgModeScreenId: |
Matt Briggs | 76:d1b20a259d8f | 44 | printEnterProgMode(); |
Matt Briggs | 75:600cb3a9f126 | 45 | } |
Matt Briggs | 75:600cb3a9f126 | 46 | } |
Matt Briggs | 75:600cb3a9f126 | 47 | bool SerialTermMgr::input() |
Matt Briggs | 75:600cb3a9f126 | 48 | { |
Matt Briggs | 75:600cb3a9f126 | 49 | if (mPc == NULL) { |
Matt Briggs | 75:600cb3a9f126 | 50 | return true; |
Matt Briggs | 75:600cb3a9f126 | 51 | } |
Matt Briggs | 75:600cb3a9f126 | 52 | bool quit = false; |
Matt Briggs | 75:600cb3a9f126 | 53 | char c = 10; |
Matt Briggs | 75:600cb3a9f126 | 54 | if (mPc->readable()) { |
Matt Briggs | 75:600cb3a9f126 | 55 | c = mPc->getc(); |
Matt Briggs | 75:600cb3a9f126 | 56 | } |
Matt Briggs | 75:600cb3a9f126 | 57 | else { |
Matt Briggs | 75:600cb3a9f126 | 58 | return false; // Do nothing if there is no input |
Matt Briggs | 75:600cb3a9f126 | 59 | } |
Matt Briggs | 75:600cb3a9f126 | 60 | switch (mCurrScreen) { |
Matt Briggs | 75:600cb3a9f126 | 61 | case mainScreenId: |
Matt Briggs | 75:600cb3a9f126 | 62 | quit = inputMainPage(c); |
Matt Briggs | 75:600cb3a9f126 | 63 | break; |
Matt Briggs | 75:600cb3a9f126 | 64 | case genInfoScreenId: |
Matt Briggs | 75:600cb3a9f126 | 65 | inputGenInfo(c); |
Matt Briggs | 75:600cb3a9f126 | 66 | break; |
Matt Briggs | 75:600cb3a9f126 | 67 | case settingsScreenId: |
Matt Briggs | 75:600cb3a9f126 | 68 | case statsScreenId: |
Matt Briggs | 75:600cb3a9f126 | 69 | case errorLogScreenId: |
Matt Briggs | 75:600cb3a9f126 | 70 | case liveLogScreenId: |
Matt Briggs | 75:600cb3a9f126 | 71 | case enterSerialBridgeScreenId: |
Matt Briggs | 76:d1b20a259d8f | 72 | inputMainPage(c); |
Matt Briggs | 76:d1b20a259d8f | 73 | break; |
Matt Briggs | 75:600cb3a9f126 | 74 | case enterProgModeScreenId: |
Matt Briggs | 76:d1b20a259d8f | 75 | inputEnterProgMode(c); |
Matt Briggs | 75:600cb3a9f126 | 76 | } |
Matt Briggs | 75:600cb3a9f126 | 77 | return quit; |
Matt Briggs | 75:600cb3a9f126 | 78 | } |
Matt Briggs | 75:600cb3a9f126 | 79 | |
Matt Briggs | 75:600cb3a9f126 | 80 | // private methods |
Matt Briggs | 75:600cb3a9f126 | 81 | bool SerialTermMgr::inputMainPage (char in) { |
Matt Briggs | 75:600cb3a9f126 | 82 | bool quit = false; |
Matt Briggs | 75:600cb3a9f126 | 83 | switch (in) { |
Matt Briggs | 75:600cb3a9f126 | 84 | case '1': |
Matt Briggs | 75:600cb3a9f126 | 85 | mCurrScreen = genInfoScreenId; |
Matt Briggs | 75:600cb3a9f126 | 86 | break; |
Matt Briggs | 75:600cb3a9f126 | 87 | case '2': |
Matt Briggs | 76:d1b20a259d8f | 88 | mCurrScreen = enterProgModeScreenId; |
Matt Briggs | 75:600cb3a9f126 | 89 | break; |
mbriggs_vortex | 92:1f86edb14cbe | 90 | case '3': |
mbriggs_vortex | 92:1f86edb14cbe | 91 | mPc->printf("\r\nNot implemented yet.\r\n"); |
mbriggs_vortex | 92:1f86edb14cbe | 92 | // mCurrScreen = settingsScreenId; |
mbriggs_vortex | 92:1f86edb14cbe | 93 | break; |
mbriggs_vortex | 91:8196900df6fe | 94 | // Future |
mbriggs_vortex | 91:8196900df6fe | 95 | // case '4': |
mbriggs_vortex | 91:8196900df6fe | 96 | // mPc->printf("\r\nNot implemented yet.\r\n"); |
mbriggs_vortex | 91:8196900df6fe | 97 | //// mCurrScreen = statsScreenId; |
mbriggs_vortex | 91:8196900df6fe | 98 | // break; |
mbriggs_vortex | 91:8196900df6fe | 99 | // case '5': |
mbriggs_vortex | 91:8196900df6fe | 100 | // mPc->printf("\r\nNot implemented yet.\r\n"); |
mbriggs_vortex | 91:8196900df6fe | 101 | //// mCurrScreen = errorLogScreenId; |
mbriggs_vortex | 91:8196900df6fe | 102 | // break; |
mbriggs_vortex | 91:8196900df6fe | 103 | // case '6': |
mbriggs_vortex | 91:8196900df6fe | 104 | // mPc->printf("\r\nNot implemented yet.\r\n"); |
mbriggs_vortex | 91:8196900df6fe | 105 | //// mCurrScreen = liveLogScreenId; |
mbriggs_vortex | 91:8196900df6fe | 106 | // break; |
mbriggs_vortex | 91:8196900df6fe | 107 | // case '7': |
mbriggs_vortex | 91:8196900df6fe | 108 | // mPc->printf("\r\nNot implemented yet.\r\n"); |
mbriggs_vortex | 91:8196900df6fe | 109 | //// mCurrScreen = enterSerialBridgeScreenId; |
mbriggs_vortex | 91:8196900df6fe | 110 | // break; |
Matt Briggs | 75:600cb3a9f126 | 111 | case 0x11: // ctrl-q |
Matt Briggs | 75:600cb3a9f126 | 112 | quit=true; |
Matt Briggs | 75:600cb3a9f126 | 113 | break; |
Matt Briggs | 75:600cb3a9f126 | 114 | default: |
Matt Briggs | 75:600cb3a9f126 | 115 | mCurrScreen = mainScreenId; |
Matt Briggs | 75:600cb3a9f126 | 116 | } |
Matt Briggs | 75:600cb3a9f126 | 117 | if (!quit) { |
Matt Briggs | 75:600cb3a9f126 | 118 | printScreen(); |
Matt Briggs | 75:600cb3a9f126 | 119 | } |
Matt Briggs | 75:600cb3a9f126 | 120 | return quit; |
Matt Briggs | 75:600cb3a9f126 | 121 | } |
Matt Briggs | 75:600cb3a9f126 | 122 | |
Matt Briggs | 75:600cb3a9f126 | 123 | void SerialTermMgr::printMainScreen() |
Matt Briggs | 75:600cb3a9f126 | 124 | { |
Matt Briggs | 75:600cb3a9f126 | 125 | if (mPc == NULL) { |
Matt Briggs | 75:600cb3a9f126 | 126 | return; |
Matt Briggs | 75:600cb3a9f126 | 127 | } |
Matt Briggs | 75:600cb3a9f126 | 128 | mPc->printf("\r\n\r\n"); |
Matt Briggs | 75:600cb3a9f126 | 129 | mPc->printf("===============================================\r\n"); |
Matt Briggs | 75:600cb3a9f126 | 130 | mPc->printf("= Wireless Bridge Terminal (Firmware: v%0.2f) =\r\n", mFwVersion); |
Matt Briggs | 75:600cb3a9f126 | 131 | mPc->printf("===============================================\r\n"); |
Matt Briggs | 75:600cb3a9f126 | 132 | mPc->printf("===============================================\r\n"); |
Matt Briggs | 75:600cb3a9f126 | 133 | mPc->printf("= Selection Options =\r\n"); |
Matt Briggs | 75:600cb3a9f126 | 134 | mPc->printf("===============================================\r\n"); |
Matt Briggs | 75:600cb3a9f126 | 135 | mPc->printf("= 0: Refresh (Enter and space also work) =\r\n"); |
Matt Briggs | 75:600cb3a9f126 | 136 | mPc->printf("= 1: General information =\r\n"); |
mbriggs_vortex | 91:8196900df6fe | 137 | mPc->printf("= 2: Enter programming mode =\r\n"); |
mbriggs_vortex | 92:1f86edb14cbe | 138 | mPc->printf("= 3: Settings =\r\n"); |
mbriggs_vortex | 91:8196900df6fe | 139 | // Future |
mbriggs_vortex | 91:8196900df6fe | 140 | // mPc->printf("= 4: Statistics =\r\n"); |
mbriggs_vortex | 91:8196900df6fe | 141 | // mPc->printf("= 5: Error log =\r\n"); |
mbriggs_vortex | 91:8196900df6fe | 142 | // mPc->printf("= 6: Live log =\r\n"); |
mbriggs_vortex | 91:8196900df6fe | 143 | // mPc->printf("= 7: Enter serial bridge mode =\r\n"); |
Matt Briggs | 75:600cb3a9f126 | 144 | mPc->printf("= <ctrl>-q: Exit terminal =\r\n"); |
Matt Briggs | 75:600cb3a9f126 | 145 | mPc->printf("===============================================\r\n"); |
Matt Briggs | 75:600cb3a9f126 | 146 | } |
Matt Briggs | 75:600cb3a9f126 | 147 | |
Matt Briggs | 75:600cb3a9f126 | 148 | void SerialTermMgr::inputGenInfo (char in) { |
Matt Briggs | 75:600cb3a9f126 | 149 | if (mPc == NULL) { |
Matt Briggs | 75:600cb3a9f126 | 150 | return; |
Matt Briggs | 75:600cb3a9f126 | 151 | } |
Matt Briggs | 75:600cb3a9f126 | 152 | switch (in) { |
mbriggs_vortex | 91:8196900df6fe | 153 | case 0x1B: // esc |
Matt Briggs | 75:600cb3a9f126 | 154 | mCurrScreen = mainScreenId; |
Matt Briggs | 75:600cb3a9f126 | 155 | break; |
Matt Briggs | 75:600cb3a9f126 | 156 | case 'r': |
Matt Briggs | 75:600cb3a9f126 | 157 | mBbio->sampleUserSwitches(); |
Matt Briggs | 75:600cb3a9f126 | 158 | mPc->printf("User switches sampled.\r\n"); |
Matt Briggs | 75:600cb3a9f126 | 159 | break; |
Matt Briggs | 75:600cb3a9f126 | 160 | default: |
Matt Briggs | 75:600cb3a9f126 | 161 | mPc->printf("Invalid key.\r\n"); |
Matt Briggs | 75:600cb3a9f126 | 162 | } |
Matt Briggs | 75:600cb3a9f126 | 163 | printScreen(); |
Matt Briggs | 75:600cb3a9f126 | 164 | } |
Matt Briggs | 75:600cb3a9f126 | 165 | |
Matt Briggs | 75:600cb3a9f126 | 166 | void SerialTermMgr::printGenInfo() |
Matt Briggs | 75:600cb3a9f126 | 167 | { |
Matt Briggs | 75:600cb3a9f126 | 168 | if (mPc == NULL) { |
Matt Briggs | 75:600cb3a9f126 | 169 | return; |
Matt Briggs | 75:600cb3a9f126 | 170 | } |
Matt Briggs | 75:600cb3a9f126 | 171 | mPc->printf("\r\n\r\n"); |
Matt Briggs | 75:600cb3a9f126 | 172 | mPc->printf("===============================================\r\n"); |
Matt Briggs | 75:600cb3a9f126 | 173 | mPc->printf("= General Info (ESC to return to main menu) =\r\n", mFwVersion); |
Matt Briggs | 75:600cb3a9f126 | 174 | mPc->printf("===============================================\r\n"); |
Matt Briggs | 75:600cb3a9f126 | 175 | mPc->printf("= Press r to sample user switch values =\r\n"); // FIXME |
Matt Briggs | 75:600cb3a9f126 | 176 | mPc->printf("===============================================\r\n"); |
Matt Briggs | 75:600cb3a9f126 | 177 | mPc->printf("===============================================\r\n"); |
Matt Briggs | 75:600cb3a9f126 | 178 | mPc->printf("= Firmware version: v%0.2f =\r\n", mFwVersion); |
Matt Briggs | 75:600cb3a9f126 | 179 | //TODO |
Matt Briggs | 75:600cb3a9f126 | 180 | // mPc->printf("= Radio EUI: %s =\r\n", mts::Text::bin2hexString(dot->getDeviceId()).c_str()); |
Matt Briggs | 75:600cb3a9f126 | 181 | mPc->printf("= Contact closure: %s =\r\n", mBbio->isCCNO() ? "Normally open " : |
Matt Briggs | 75:600cb3a9f126 | 182 | "Normally closed"); |
Matt Briggs | 75:600cb3a9f126 | 183 | mPc->printf("= WB Mode: %s =\r\n", mBbio->isTx() ? "Transmitter" : |
Matt Briggs | 75:600cb3a9f126 | 184 | "Receiver "); |
mbriggs_vortex | 91:8196900df6fe | 185 | // mPc->printf("= Comm Mode: %s =\r\n", mBbio->isLoRaWANMode() ? "LoRaWAN " : |
mbriggs_vortex | 91:8196900df6fe | 186 | // "Peer-to-peer"); |
mbriggs_vortex | 91:8196900df6fe | 187 | // mPc->printf("= Serial Mode: %s =\r\n", mBbio->isSerialEnabled() ? "Enabled " : |
mbriggs_vortex | 91:8196900df6fe | 188 | // "Disabled"); |
Matt Briggs | 75:600cb3a9f126 | 189 | if (mBbio->isTx()) { |
Matt Briggs | 75:600cb3a9f126 | 190 | mPc->printf("= Currently no rotary switches apply for TX =\r\n"); |
Matt Briggs | 75:600cb3a9f126 | 191 | } |
Matt Briggs | 75:600cb3a9f126 | 192 | else { |
Matt Briggs | 75:600cb3a9f126 | 193 | mPc->printf("= Rotary 1 hold setting is %0.1f seconds =\r\n", HoldTimeSetting::rotVal2Sec(mBbio->rotarySwitch1())); |
Matt Briggs | 75:600cb3a9f126 | 194 | mPc->printf("= Rotary 2 currently does not apply for RX =\r\n"); |
Matt Briggs | 75:600cb3a9f126 | 195 | } |
Matt Briggs | 75:600cb3a9f126 | 196 | mPc->printf("===============================================\r\n"); |
Matt Briggs | 75:600cb3a9f126 | 197 | } |
Matt Briggs | 76:d1b20a259d8f | 198 | |
Matt Briggs | 76:d1b20a259d8f | 199 | void SerialTermMgr::printEnterProgMode() |
Matt Briggs | 76:d1b20a259d8f | 200 | { |
Matt Briggs | 76:d1b20a259d8f | 201 | if (mPc == NULL) { |
Matt Briggs | 76:d1b20a259d8f | 202 | return; |
Matt Briggs | 76:d1b20a259d8f | 203 | } |
Matt Briggs | 76:d1b20a259d8f | 204 | mPc->printf("\r\n\r\n"); |
Matt Briggs | 76:d1b20a259d8f | 205 | mPc->printf("===============================================\r\n"); |
mbriggs_vortex | 91:8196900df6fe | 206 | mPc->printf("= Serial Firmware Prog Mode (Esc to return) =\r\n"); |
Matt Briggs | 76:d1b20a259d8f | 207 | mPc->printf("===============================================\r\n"); |
Matt Briggs | 76:d1b20a259d8f | 208 | mPc->printf("===============================================\r\n"); |
Matt Briggs | 76:d1b20a259d8f | 209 | mPc->printf("= To program firmware: \r\n"); |
Matt Briggs | 76:d1b20a259d8f | 210 | mPc->printf("= 1: Have bin file ready =\r\n"); |
Matt Briggs | 76:d1b20a259d8f | 211 | mPc->printf("= 2: Hit <ctrl>-d to enter XMODEM mode =\r\n"); |
Matt Briggs | 76:d1b20a259d8f | 212 | mPc->printf("= 3: In terminal program (such as teraterm) =\r\n"); |
Matt Briggs | 76:d1b20a259d8f | 213 | mPc->printf("= select transfer via XMODEM =\r\n"); |
Matt Briggs | 76:d1b20a259d8f | 214 | mPc->printf("= 4: Programming may take up to a minute =\r\n"); |
mbriggs_vortex | 91:8196900df6fe | 215 | mPc->printf("= 5: If successful terminal prints \"Success\" =\r\n"); |
Matt Briggs | 76:d1b20a259d8f | 216 | mPc->printf("= 6: Wireless Bridge will then automatically =\r\n"); |
Matt Briggs | 76:d1b20a259d8f | 217 | mPc->printf("= reboot with new firmware. =\r\n"); |
Matt Briggs | 76:d1b20a259d8f | 218 | mPc->printf("===============================================\r\n"); |
Matt Briggs | 76:d1b20a259d8f | 219 | } |
Matt Briggs | 76:d1b20a259d8f | 220 | |
Matt Briggs | 76:d1b20a259d8f | 221 | void SerialTermMgr::inputEnterProgMode (char in) { |
Matt Briggs | 76:d1b20a259d8f | 222 | if (mPc == NULL) { |
Matt Briggs | 76:d1b20a259d8f | 223 | return; |
Matt Briggs | 76:d1b20a259d8f | 224 | } |
Matt Briggs | 76:d1b20a259d8f | 225 | switch (in) { |
mbriggs_vortex | 91:8196900df6fe | 226 | case 0x1B: // esc: |
Matt Briggs | 76:d1b20a259d8f | 227 | mCurrScreen = mainScreenId; |
Matt Briggs | 76:d1b20a259d8f | 228 | break; |
Matt Briggs | 76:d1b20a259d8f | 229 | case 0x04: |
Matt Briggs | 76:d1b20a259d8f | 230 | xmodem2Flash(); |
Matt Briggs | 76:d1b20a259d8f | 231 | break; |
Matt Briggs | 76:d1b20a259d8f | 232 | default: |
Matt Briggs | 76:d1b20a259d8f | 233 | mPc->printf("Invalid key.\r\n"); |
Matt Briggs | 76:d1b20a259d8f | 234 | } |
Matt Briggs | 76:d1b20a259d8f | 235 | printScreen(); |
Matt Briggs | 76:d1b20a259d8f | 236 | } |
Matt Briggs | 76:d1b20a259d8f | 237 | |
Matt Briggs | 76:d1b20a259d8f | 238 | bool SerialTermMgr::xmodem2Flash () |
Matt Briggs | 76:d1b20a259d8f | 239 | { |
Matt Briggs | 77:176e3eb8f712 | 240 | mPc->printf("Ready to receive file\r\n"); |
Matt Briggs | 77:176e3eb8f712 | 241 | mFlash->releaseFromPowerDown(); |
Matt Briggs | 77:176e3eb8f712 | 242 | mFlash->chipErase(); |
Matt Briggs | 76:d1b20a259d8f | 243 | char packetBuf[132]; |
Matt Briggs | 77:176e3eb8f712 | 244 | uint16_t packetNum = 1; |
Matt Briggs | 77:176e3eb8f712 | 245 | uint8_t csum = 0; |
Matt Briggs | 76:d1b20a259d8f | 246 | unsigned char pktIdx = 0; |
Matt Briggs | 76:d1b20a259d8f | 247 | time_t lastValidInput = 0; |
mbriggs_vortex | 78:43f074baac34 | 248 | unsigned char nTimeouts = 0; |
Matt Briggs | 76:d1b20a259d8f | 249 | while (1) { |
Matt Briggs | 77:176e3eb8f712 | 250 | // Send a NAK if a packet is not received within timeout period |
Matt Briggs | 76:d1b20a259d8f | 251 | if ((time(NULL)-lastValidInput) > XMODEM_TIMEOUT){ |
Matt Briggs | 76:d1b20a259d8f | 252 | pc.printf("Total chars: %d\r\n", pktIdx); |
Matt Briggs | 76:d1b20a259d8f | 253 | pktIdx = 0; |
Matt Briggs | 77:176e3eb8f712 | 254 | mPc->printf("%c", NAK); // Send NAK |
Matt Briggs | 77:176e3eb8f712 | 255 | lastValidInput = time(NULL); |
mbriggs_vortex | 78:43f074baac34 | 256 | nTimeouts++; |
mbriggs_vortex | 78:43f074baac34 | 257 | if (nTimeouts >= MAX_TIMEOUTS) { |
mbriggs_vortex | 78:43f074baac34 | 258 | mPc->printf("Programming timed out.\r\n"); |
mbriggs_vortex | 78:43f074baac34 | 259 | pc.printf("Programming timed out.\r\n"); |
mbriggs_vortex | 78:43f074baac34 | 260 | break; |
mbriggs_vortex | 78:43f074baac34 | 261 | } |
Matt Briggs | 76:d1b20a259d8f | 262 | } |
Matt Briggs | 76:d1b20a259d8f | 263 | if (mPc->readable()) { |
Matt Briggs | 77:176e3eb8f712 | 264 | packetBuf[pktIdx++] = mPc->getc(); |
Matt Briggs | 77:176e3eb8f712 | 265 | if (pktIdx == 1 && packetBuf[0] == EOT) { |
Matt Briggs | 77:176e3eb8f712 | 266 | lastValidInput = time(NULL); |
Matt Briggs | 77:176e3eb8f712 | 267 | mPc->printf("%c", ACK); // Last ACK |
mbriggs_vortex | 92:1f86edb14cbe | 268 | |
mbriggs_vortex | 92:1f86edb14cbe | 269 | // Check for update string in last packet |
mbriggs_vortex | 92:1f86edb14cbe | 270 | bool foundKey; |
mbriggs_vortex | 94:8028c07f71fa | 271 | for (int packetIdx=3;packetIdx<sizeof(packetBuf)-sizeof(VORTEX_UPDATE_KEY);packetIdx++) { |
mbriggs_vortex | 92:1f86edb14cbe | 272 | foundKey = true; |
mbriggs_vortex | 93:f3adc409c54e | 273 | for (int i=0;i<sizeof(VORTEX_UPDATE_KEY)-1;i++) { |
mbriggs_vortex | 92:1f86edb14cbe | 274 | if (packetBuf[packetIdx+i] != VORTEX_UPDATE_KEY[i]) { |
mbriggs_vortex | 92:1f86edb14cbe | 275 | foundKey = false; |
mbriggs_vortex | 92:1f86edb14cbe | 276 | break; |
mbriggs_vortex | 92:1f86edb14cbe | 277 | } |
mbriggs_vortex | 92:1f86edb14cbe | 278 | } |
mbriggs_vortex | 92:1f86edb14cbe | 279 | if (foundKey) { |
mbriggs_vortex | 92:1f86edb14cbe | 280 | break; |
mbriggs_vortex | 92:1f86edb14cbe | 281 | } |
mbriggs_vortex | 92:1f86edb14cbe | 282 | } |
mbriggs_vortex | 92:1f86edb14cbe | 283 | if (foundKey) { |
mbriggs_vortex | 92:1f86edb14cbe | 284 | mPc->printf("Success.\r\n"); |
mbriggs_vortex | 92:1f86edb14cbe | 285 | pc.printf("Success on xmodem. Reset in progress.\r\n"); |
mbriggs_vortex | 92:1f86edb14cbe | 286 | writeBootloaderCtrlPage(packetNum*XMODEM_PACKET_SIZE); |
mbriggs_vortex | 92:1f86edb14cbe | 287 | } |
mbriggs_vortex | 92:1f86edb14cbe | 288 | else { |
mbriggs_vortex | 92:1f86edb14cbe | 289 | mPc->printf("Failed update key validation.\r\n"); |
mbriggs_vortex | 92:1f86edb14cbe | 290 | pc.printf("Failed update key validation\r\n"); |
mbriggs_vortex | 92:1f86edb14cbe | 291 | } |
mbriggs_vortex | 78:43f074baac34 | 292 | |
Matt Briggs | 77:176e3eb8f712 | 293 | wait(0.01); |
mbriggs_vortex | 78:43f074baac34 | 294 | mFlash->powerDown(); |
mbriggs_vortex | 92:1f86edb14cbe | 295 | if (foundKey) { |
mbriggs_vortex | 92:1f86edb14cbe | 296 | NVIC_SystemReset(); |
mbriggs_vortex | 92:1f86edb14cbe | 297 | } |
Matt Briggs | 77:176e3eb8f712 | 298 | break; |
Matt Briggs | 76:d1b20a259d8f | 299 | } |
Matt Briggs | 77:176e3eb8f712 | 300 | else if (pktIdx >= 132) { |
Matt Briggs | 77:176e3eb8f712 | 301 | lastValidInput = time(NULL); |
mbriggs_vortex | 78:43f074baac34 | 302 | nTimeouts = 0; // Clear n timeouts with new packet |
Matt Briggs | 77:176e3eb8f712 | 303 | csum = 0; |
Matt Briggs | 77:176e3eb8f712 | 304 | for (int i=3; i<131; i++) { |
Matt Briggs | 77:176e3eb8f712 | 305 | csum += packetBuf[i]; |
Matt Briggs | 77:176e3eb8f712 | 306 | } |
Matt Briggs | 77:176e3eb8f712 | 307 | pktIdx = 0; |
Matt Briggs | 77:176e3eb8f712 | 308 | if ((csum == packetBuf[131]) && (packetBuf[0] == SOH) && |
mbriggs_vortex | 92:1f86edb14cbe | 309 | (packetBuf[1] == ((uint8_t)packetNum)) && (((uint8_t)~packetBuf[2]) == ((uint8_t)packetNum))) { |
mbriggs_vortex | 92:1f86edb14cbe | 310 | mFlash->writeStream(FLASH_BIN_OFFSET+(packetNum-1)*XMODEM_PACKET_SIZE, packetBuf+3, |
mbriggs_vortex | 92:1f86edb14cbe | 311 | XMODEM_PACKET_SIZE); |
Matt Briggs | 77:176e3eb8f712 | 312 | mPc->printf("%c", ACK); |
Matt Briggs | 77:176e3eb8f712 | 313 | packetNum++; |
Matt Briggs | 77:176e3eb8f712 | 314 | } |
Matt Briggs | 77:176e3eb8f712 | 315 | else { |
Matt Briggs | 77:176e3eb8f712 | 316 | mPc->printf("%c", NAK); |
Matt Briggs | 77:176e3eb8f712 | 317 | } |
Matt Briggs | 77:176e3eb8f712 | 318 | } |
Matt Briggs | 76:d1b20a259d8f | 319 | } |
Matt Briggs | 76:d1b20a259d8f | 320 | } |
Matt Briggs | 77:176e3eb8f712 | 321 | mCurrScreen = mainScreenId; // Just return to main screen for now |
Matt Briggs | 77:176e3eb8f712 | 322 | mFlash->powerDown(); |
Matt Briggs | 77:176e3eb8f712 | 323 | |
Matt Briggs | 77:176e3eb8f712 | 324 | return true; |
Matt Briggs | 76:d1b20a259d8f | 325 | } |
mbriggs_vortex | 78:43f074baac34 | 326 | |
mbriggs_vortex | 78:43f074baac34 | 327 | void SerialTermMgr::writeBootloaderCtrlPage(uint32_t nBytes) |
mbriggs_vortex | 78:43f074baac34 | 328 | { |
mbriggs_vortex | 78:43f074baac34 | 329 | // This should always be less than 256 bytes |
mbriggs_vortex | 91:8196900df6fe | 330 | |
mbriggs_vortex | 91:8196900df6fe | 331 | // Zero updates the bootloader version so that is cleared until the bootloader adds its back |
mbriggs_vortex | 91:8196900df6fe | 332 | const uint32_t zeroBootloaderVersion = 0; |
mbriggs_vortex | 91:8196900df6fe | 333 | const unsigned int ctrlSize = sizeof(NEW_CODE)+sizeof(nBytes)+sizeof(zeroBootloaderVersion); |
mbriggs_vortex | 78:43f074baac34 | 334 | char buf[ctrlSize]; |
mbriggs_vortex | 78:43f074baac34 | 335 | std::memcpy(buf, NEW_CODE, sizeof(NEW_CODE)); |
mbriggs_vortex | 78:43f074baac34 | 336 | std::memcpy(buf+sizeof(NEW_CODE), &nBytes, sizeof(nBytes)); |
mbriggs_vortex | 91:8196900df6fe | 337 | std::memcpy(buf+sizeof(NEW_CODE)+sizeof(nBytes), |
mbriggs_vortex | 91:8196900df6fe | 338 | &zeroBootloaderVersion, sizeof(zeroBootloaderVersion)); |
mbriggs_vortex | 91:8196900df6fe | 339 | // TODO add reprogram counter or other functions |
mbriggs_vortex | 78:43f074baac34 | 340 | |
mbriggs_vortex | 78:43f074baac34 | 341 | // Copy to first page of flash |
mbriggs_vortex | 78:43f074baac34 | 342 | mFlash->writeStream(0, buf, sizeof(buf)); |
mbriggs_vortex | 78:43f074baac34 | 343 | } |