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