![](/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@91:8196900df6fe, 2017-11-18 (annotated)
- Committer:
- mbriggs_vortex
- Date:
- Sat Nov 18 18:01:43 2017 -0700
- Revision:
- 91:8196900df6fe
- Parent:
- 78:43f074baac34
- Child:
- 92:1f86edb14cbe
Added new 1.01 bootloader, removed unimplemented prints, removed secrets from being printed (using MACRO in dot_util.h)
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 | 91:8196900df6fe | 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 | 76:d1b20a259d8f | 89 | mCurrScreen = enterProgModeScreenId; |
Matt Briggs | 75:600cb3a9f126 | 90 | break; |
mbriggs_vortex | 91:8196900df6fe | 91 | // Future |
mbriggs_vortex | 91:8196900df6fe | 92 | // case '3': |
mbriggs_vortex | 91:8196900df6fe | 93 | // mPc->printf("\r\nNot implemented yet.\r\n"); |
mbriggs_vortex | 91:8196900df6fe | 94 | //// mCurrScreen = settingsScreenId; |
mbriggs_vortex | 91:8196900df6fe | 95 | // break; |
mbriggs_vortex | 91:8196900df6fe | 96 | // case '4': |
mbriggs_vortex | 91:8196900df6fe | 97 | // mPc->printf("\r\nNot implemented yet.\r\n"); |
mbriggs_vortex | 91:8196900df6fe | 98 | //// mCurrScreen = statsScreenId; |
mbriggs_vortex | 91:8196900df6fe | 99 | // break; |
mbriggs_vortex | 91:8196900df6fe | 100 | // case '5': |
mbriggs_vortex | 91:8196900df6fe | 101 | // mPc->printf("\r\nNot implemented yet.\r\n"); |
mbriggs_vortex | 91:8196900df6fe | 102 | //// mCurrScreen = errorLogScreenId; |
mbriggs_vortex | 91:8196900df6fe | 103 | // break; |
mbriggs_vortex | 91:8196900df6fe | 104 | // case '6': |
mbriggs_vortex | 91:8196900df6fe | 105 | // mPc->printf("\r\nNot implemented yet.\r\n"); |
mbriggs_vortex | 91:8196900df6fe | 106 | //// mCurrScreen = liveLogScreenId; |
mbriggs_vortex | 91:8196900df6fe | 107 | // break; |
mbriggs_vortex | 91:8196900df6fe | 108 | // case '7': |
mbriggs_vortex | 91:8196900df6fe | 109 | // mPc->printf("\r\nNot implemented yet.\r\n"); |
mbriggs_vortex | 91:8196900df6fe | 110 | //// mCurrScreen = enterSerialBridgeScreenId; |
mbriggs_vortex | 91:8196900df6fe | 111 | // break; |
Matt Briggs | 75:600cb3a9f126 | 112 | case 0x11: // ctrl-q |
Matt Briggs | 75:600cb3a9f126 | 113 | quit=true; |
Matt Briggs | 75:600cb3a9f126 | 114 | break; |
Matt Briggs | 75:600cb3a9f126 | 115 | default: |
Matt Briggs | 75:600cb3a9f126 | 116 | mCurrScreen = mainScreenId; |
Matt Briggs | 75:600cb3a9f126 | 117 | } |
Matt Briggs | 75:600cb3a9f126 | 118 | if (!quit) { |
Matt Briggs | 75:600cb3a9f126 | 119 | printScreen(); |
Matt Briggs | 75:600cb3a9f126 | 120 | } |
Matt Briggs | 75:600cb3a9f126 | 121 | return quit; |
Matt Briggs | 75:600cb3a9f126 | 122 | } |
Matt Briggs | 75:600cb3a9f126 | 123 | |
Matt Briggs | 75:600cb3a9f126 | 124 | void SerialTermMgr::printMainScreen() |
Matt Briggs | 75:600cb3a9f126 | 125 | { |
Matt Briggs | 75:600cb3a9f126 | 126 | if (mPc == NULL) { |
Matt Briggs | 75:600cb3a9f126 | 127 | return; |
Matt Briggs | 75:600cb3a9f126 | 128 | } |
Matt Briggs | 75:600cb3a9f126 | 129 | mPc->printf("\r\n\r\n"); |
Matt Briggs | 75:600cb3a9f126 | 130 | mPc->printf("===============================================\r\n"); |
Matt Briggs | 75:600cb3a9f126 | 131 | mPc->printf("= Wireless Bridge Terminal (Firmware: v%0.2f) =\r\n", mFwVersion); |
Matt Briggs | 75:600cb3a9f126 | 132 | mPc->printf("===============================================\r\n"); |
Matt Briggs | 75:600cb3a9f126 | 133 | mPc->printf("===============================================\r\n"); |
Matt Briggs | 75:600cb3a9f126 | 134 | mPc->printf("= Selection Options =\r\n"); |
Matt Briggs | 75:600cb3a9f126 | 135 | mPc->printf("===============================================\r\n"); |
Matt Briggs | 75:600cb3a9f126 | 136 | mPc->printf("= 0: Refresh (Enter and space also work) =\r\n"); |
Matt Briggs | 75:600cb3a9f126 | 137 | mPc->printf("= 1: General information =\r\n"); |
mbriggs_vortex | 91:8196900df6fe | 138 | mPc->printf("= 2: Enter programming mode =\r\n"); |
mbriggs_vortex | 91:8196900df6fe | 139 | // Future |
mbriggs_vortex | 91:8196900df6fe | 140 | // mPc->printf("= 3: Settings =\r\n"); |
mbriggs_vortex | 91:8196900df6fe | 141 | // mPc->printf("= 4: Statistics =\r\n"); |
mbriggs_vortex | 91:8196900df6fe | 142 | // mPc->printf("= 5: Error log =\r\n"); |
mbriggs_vortex | 91:8196900df6fe | 143 | // mPc->printf("= 6: Live log =\r\n"); |
mbriggs_vortex | 91:8196900df6fe | 144 | // mPc->printf("= 7: Enter serial bridge mode =\r\n"); |
Matt Briggs | 75:600cb3a9f126 | 145 | mPc->printf("= <ctrl>-q: Exit terminal =\r\n"); |
Matt Briggs | 75:600cb3a9f126 | 146 | mPc->printf("===============================================\r\n"); |
Matt Briggs | 75:600cb3a9f126 | 147 | } |
Matt Briggs | 75:600cb3a9f126 | 148 | |
Matt Briggs | 75:600cb3a9f126 | 149 | void SerialTermMgr::inputGenInfo (char in) { |
Matt Briggs | 75:600cb3a9f126 | 150 | if (mPc == NULL) { |
Matt Briggs | 75:600cb3a9f126 | 151 | return; |
Matt Briggs | 75:600cb3a9f126 | 152 | } |
Matt Briggs | 75:600cb3a9f126 | 153 | switch (in) { |
mbriggs_vortex | 91:8196900df6fe | 154 | case 0x1B: // esc |
Matt Briggs | 75:600cb3a9f126 | 155 | mCurrScreen = mainScreenId; |
Matt Briggs | 75:600cb3a9f126 | 156 | break; |
Matt Briggs | 75:600cb3a9f126 | 157 | case 'r': |
Matt Briggs | 75:600cb3a9f126 | 158 | mBbio->sampleUserSwitches(); |
Matt Briggs | 75:600cb3a9f126 | 159 | mPc->printf("User switches sampled.\r\n"); |
Matt Briggs | 75:600cb3a9f126 | 160 | break; |
Matt Briggs | 75:600cb3a9f126 | 161 | default: |
Matt Briggs | 75:600cb3a9f126 | 162 | mPc->printf("Invalid key.\r\n"); |
Matt Briggs | 75:600cb3a9f126 | 163 | } |
Matt Briggs | 75:600cb3a9f126 | 164 | printScreen(); |
Matt Briggs | 75:600cb3a9f126 | 165 | } |
Matt Briggs | 75:600cb3a9f126 | 166 | |
Matt Briggs | 75:600cb3a9f126 | 167 | void SerialTermMgr::printGenInfo() |
Matt Briggs | 75:600cb3a9f126 | 168 | { |
Matt Briggs | 75:600cb3a9f126 | 169 | if (mPc == NULL) { |
Matt Briggs | 75:600cb3a9f126 | 170 | return; |
Matt Briggs | 75:600cb3a9f126 | 171 | } |
Matt Briggs | 75:600cb3a9f126 | 172 | mPc->printf("\r\n\r\n"); |
Matt Briggs | 75:600cb3a9f126 | 173 | mPc->printf("===============================================\r\n"); |
Matt Briggs | 75:600cb3a9f126 | 174 | mPc->printf("= General Info (ESC to return to main menu) =\r\n", mFwVersion); |
Matt Briggs | 75:600cb3a9f126 | 175 | mPc->printf("===============================================\r\n"); |
Matt Briggs | 75:600cb3a9f126 | 176 | mPc->printf("= Press r to sample user switch values =\r\n"); // FIXME |
Matt Briggs | 75:600cb3a9f126 | 177 | mPc->printf("===============================================\r\n"); |
Matt Briggs | 75:600cb3a9f126 | 178 | mPc->printf("===============================================\r\n"); |
Matt Briggs | 75:600cb3a9f126 | 179 | mPc->printf("= Firmware version: v%0.2f =\r\n", mFwVersion); |
Matt Briggs | 75:600cb3a9f126 | 180 | //TODO |
Matt Briggs | 75:600cb3a9f126 | 181 | // mPc->printf("= Radio EUI: %s =\r\n", mts::Text::bin2hexString(dot->getDeviceId()).c_str()); |
Matt Briggs | 75:600cb3a9f126 | 182 | mPc->printf("= Contact closure: %s =\r\n", mBbio->isCCNO() ? "Normally open " : |
Matt Briggs | 75:600cb3a9f126 | 183 | "Normally closed"); |
Matt Briggs | 75:600cb3a9f126 | 184 | mPc->printf("= WB Mode: %s =\r\n", mBbio->isTx() ? "Transmitter" : |
Matt Briggs | 75:600cb3a9f126 | 185 | "Receiver "); |
mbriggs_vortex | 91:8196900df6fe | 186 | // mPc->printf("= Comm Mode: %s =\r\n", mBbio->isLoRaWANMode() ? "LoRaWAN " : |
mbriggs_vortex | 91:8196900df6fe | 187 | // "Peer-to-peer"); |
mbriggs_vortex | 91:8196900df6fe | 188 | // mPc->printf("= Serial Mode: %s =\r\n", mBbio->isSerialEnabled() ? "Enabled " : |
mbriggs_vortex | 91:8196900df6fe | 189 | // "Disabled"); |
Matt Briggs | 75:600cb3a9f126 | 190 | if (mBbio->isTx()) { |
Matt Briggs | 75:600cb3a9f126 | 191 | mPc->printf("= Currently no rotary switches apply for TX =\r\n"); |
Matt Briggs | 75:600cb3a9f126 | 192 | } |
Matt Briggs | 75:600cb3a9f126 | 193 | else { |
Matt Briggs | 75:600cb3a9f126 | 194 | mPc->printf("= Rotary 1 hold setting is %0.1f seconds =\r\n", HoldTimeSetting::rotVal2Sec(mBbio->rotarySwitch1())); |
Matt Briggs | 75:600cb3a9f126 | 195 | mPc->printf("= Rotary 2 currently does not apply for RX =\r\n"); |
Matt Briggs | 75:600cb3a9f126 | 196 | } |
Matt Briggs | 75:600cb3a9f126 | 197 | mPc->printf("===============================================\r\n"); |
Matt Briggs | 75:600cb3a9f126 | 198 | } |
Matt Briggs | 76:d1b20a259d8f | 199 | |
Matt Briggs | 76:d1b20a259d8f | 200 | void SerialTermMgr::printEnterProgMode() |
Matt Briggs | 76:d1b20a259d8f | 201 | { |
Matt Briggs | 76:d1b20a259d8f | 202 | if (mPc == NULL) { |
Matt Briggs | 76:d1b20a259d8f | 203 | return; |
Matt Briggs | 76:d1b20a259d8f | 204 | } |
Matt Briggs | 76:d1b20a259d8f | 205 | mPc->printf("\r\n\r\n"); |
Matt Briggs | 76:d1b20a259d8f | 206 | mPc->printf("===============================================\r\n"); |
mbriggs_vortex | 91:8196900df6fe | 207 | mPc->printf("= Serial Firmware Prog Mode (Esc to return) =\r\n"); |
Matt Briggs | 76:d1b20a259d8f | 208 | mPc->printf("===============================================\r\n"); |
Matt Briggs | 76:d1b20a259d8f | 209 | mPc->printf("===============================================\r\n"); |
Matt Briggs | 76:d1b20a259d8f | 210 | mPc->printf("= To program firmware: \r\n"); |
Matt Briggs | 76:d1b20a259d8f | 211 | mPc->printf("= 1: Have bin file ready =\r\n"); |
Matt Briggs | 76:d1b20a259d8f | 212 | mPc->printf("= 2: Hit <ctrl>-d to enter XMODEM mode =\r\n"); |
Matt Briggs | 76:d1b20a259d8f | 213 | mPc->printf("= 3: In terminal program (such as teraterm) =\r\n"); |
Matt Briggs | 76:d1b20a259d8f | 214 | mPc->printf("= select transfer via XMODEM =\r\n"); |
Matt Briggs | 76:d1b20a259d8f | 215 | mPc->printf("= 4: Programming may take up to a minute =\r\n"); |
mbriggs_vortex | 91:8196900df6fe | 216 | mPc->printf("= 5: If successful terminal prints \"Success\" =\r\n"); |
Matt Briggs | 76:d1b20a259d8f | 217 | mPc->printf("= 6: Wireless Bridge will then automatically =\r\n"); |
Matt Briggs | 76:d1b20a259d8f | 218 | mPc->printf("= reboot with new firmware. =\r\n"); |
Matt Briggs | 76:d1b20a259d8f | 219 | mPc->printf("===============================================\r\n"); |
Matt Briggs | 76:d1b20a259d8f | 220 | } |
Matt Briggs | 76:d1b20a259d8f | 221 | |
Matt Briggs | 76:d1b20a259d8f | 222 | void SerialTermMgr::inputEnterProgMode (char in) { |
Matt Briggs | 76:d1b20a259d8f | 223 | if (mPc == NULL) { |
Matt Briggs | 76:d1b20a259d8f | 224 | return; |
Matt Briggs | 76:d1b20a259d8f | 225 | } |
Matt Briggs | 76:d1b20a259d8f | 226 | switch (in) { |
mbriggs_vortex | 91:8196900df6fe | 227 | case 0x1B: // esc: |
Matt Briggs | 76:d1b20a259d8f | 228 | mCurrScreen = mainScreenId; |
Matt Briggs | 76:d1b20a259d8f | 229 | break; |
Matt Briggs | 76:d1b20a259d8f | 230 | case 0x04: |
Matt Briggs | 76:d1b20a259d8f | 231 | xmodem2Flash(); |
Matt Briggs | 76:d1b20a259d8f | 232 | break; |
Matt Briggs | 76:d1b20a259d8f | 233 | default: |
Matt Briggs | 76:d1b20a259d8f | 234 | mPc->printf("Invalid key.\r\n"); |
Matt Briggs | 76:d1b20a259d8f | 235 | } |
Matt Briggs | 76:d1b20a259d8f | 236 | printScreen(); |
Matt Briggs | 76:d1b20a259d8f | 237 | } |
Matt Briggs | 76:d1b20a259d8f | 238 | |
Matt Briggs | 76:d1b20a259d8f | 239 | bool SerialTermMgr::xmodem2Flash () |
Matt Briggs | 76:d1b20a259d8f | 240 | { |
Matt Briggs | 77:176e3eb8f712 | 241 | mPc->printf("Ready to receive file\r\n"); |
Matt Briggs | 77:176e3eb8f712 | 242 | mFlash->releaseFromPowerDown(); |
Matt Briggs | 77:176e3eb8f712 | 243 | mFlash->chipErase(); |
Matt Briggs | 76:d1b20a259d8f | 244 | char packetBuf[132]; |
Matt Briggs | 77:176e3eb8f712 | 245 | uint16_t packetNum = 1; |
Matt Briggs | 77:176e3eb8f712 | 246 | uint8_t csum = 0; |
Matt Briggs | 76:d1b20a259d8f | 247 | unsigned char pktIdx = 0; |
Matt Briggs | 76:d1b20a259d8f | 248 | time_t lastValidInput = 0; |
mbriggs_vortex | 78:43f074baac34 | 249 | unsigned char nTimeouts = 0; |
Matt Briggs | 76:d1b20a259d8f | 250 | while (1) { |
Matt Briggs | 77:176e3eb8f712 | 251 | // Send a NAK if a packet is not received within timeout period |
Matt Briggs | 76:d1b20a259d8f | 252 | if ((time(NULL)-lastValidInput) > XMODEM_TIMEOUT){ |
Matt Briggs | 76:d1b20a259d8f | 253 | pc.printf("Total chars: %d\r\n", pktIdx); |
Matt Briggs | 76:d1b20a259d8f | 254 | pktIdx = 0; |
Matt Briggs | 77:176e3eb8f712 | 255 | mPc->printf("%c", NAK); // Send NAK |
Matt Briggs | 77:176e3eb8f712 | 256 | lastValidInput = time(NULL); |
mbriggs_vortex | 78:43f074baac34 | 257 | nTimeouts++; |
mbriggs_vortex | 78:43f074baac34 | 258 | if (nTimeouts >= MAX_TIMEOUTS) { |
mbriggs_vortex | 78:43f074baac34 | 259 | mPc->printf("Programming timed out.\r\n"); |
mbriggs_vortex | 78:43f074baac34 | 260 | pc.printf("Programming timed out.\r\n"); |
mbriggs_vortex | 78:43f074baac34 | 261 | break; |
mbriggs_vortex | 78:43f074baac34 | 262 | } |
mbriggs_vortex | 78:43f074baac34 | 263 | |
Matt Briggs | 76:d1b20a259d8f | 264 | } |
Matt Briggs | 76:d1b20a259d8f | 265 | if (mPc->readable()) { |
Matt Briggs | 77:176e3eb8f712 | 266 | packetBuf[pktIdx++] = mPc->getc(); |
Matt Briggs | 77:176e3eb8f712 | 267 | if (pktIdx == 1 && packetBuf[0] == EOT) { |
Matt Briggs | 77:176e3eb8f712 | 268 | lastValidInput = time(NULL); |
Matt Briggs | 77:176e3eb8f712 | 269 | mPc->printf("%c", ACK); // Last ACK |
mbriggs_vortex | 78:43f074baac34 | 270 | mPc->printf("Success.\r\n"); |
mbriggs_vortex | 78:43f074baac34 | 271 | pc.printf("Success on xmodem. Reset in progress.\r\n"); |
mbriggs_vortex | 78:43f074baac34 | 272 | writeBootloaderCtrlPage(packetNum*XMODEM_PACKET_SIZE); |
mbriggs_vortex | 78:43f074baac34 | 273 | // copyNewProgram(); |
mbriggs_vortex | 78:43f074baac34 | 274 | |
Matt Briggs | 77:176e3eb8f712 | 275 | wait(0.01); |
mbriggs_vortex | 78:43f074baac34 | 276 | mFlash->powerDown(); |
mbriggs_vortex | 78:43f074baac34 | 277 | NVIC_SystemReset(); |
mbriggs_vortex | 78:43f074baac34 | 278 | // Should never get here because of reset. |
Matt Briggs | 77:176e3eb8f712 | 279 | break; |
Matt Briggs | 76:d1b20a259d8f | 280 | } |
Matt Briggs | 77:176e3eb8f712 | 281 | else if (pktIdx >= 132) { |
Matt Briggs | 77:176e3eb8f712 | 282 | lastValidInput = time(NULL); |
mbriggs_vortex | 78:43f074baac34 | 283 | nTimeouts = 0; // Clear n timeouts with new packet |
Matt Briggs | 77:176e3eb8f712 | 284 | csum = 0; |
Matt Briggs | 77:176e3eb8f712 | 285 | for (int i=3; i<131; i++) { |
Matt Briggs | 77:176e3eb8f712 | 286 | csum += packetBuf[i]; |
Matt Briggs | 77:176e3eb8f712 | 287 | } |
Matt Briggs | 77:176e3eb8f712 | 288 | pktIdx = 0; |
Matt Briggs | 77:176e3eb8f712 | 289 | if ((csum == packetBuf[131]) && (packetBuf[0] == SOH) && |
Matt Briggs | 77:176e3eb8f712 | 290 | (packetBuf[1] == ((uint8_t)packetNum)) && (((uint8_t)~packetBuf[2]) == ((uint8_t)packetNum))) { |
mbriggs_vortex | 78:43f074baac34 | 291 | mFlash->writeStream(FLASH_BIN_OFFSET+(packetNum-1)*XMODEM_PACKET_SIZE, packetBuf+3, XMODEM_PACKET_SIZE); |
Matt Briggs | 77:176e3eb8f712 | 292 | mPc->printf("%c", ACK); |
Matt Briggs | 77:176e3eb8f712 | 293 | packetNum++; |
Matt Briggs | 77:176e3eb8f712 | 294 | } |
Matt Briggs | 77:176e3eb8f712 | 295 | else { |
Matt Briggs | 77:176e3eb8f712 | 296 | mPc->printf("%c", NAK); |
Matt Briggs | 77:176e3eb8f712 | 297 | } |
Matt Briggs | 77:176e3eb8f712 | 298 | } |
Matt Briggs | 76:d1b20a259d8f | 299 | } |
Matt Briggs | 76:d1b20a259d8f | 300 | } |
Matt Briggs | 77:176e3eb8f712 | 301 | mCurrScreen = mainScreenId; // Just return to main screen for now |
Matt Briggs | 77:176e3eb8f712 | 302 | mFlash->powerDown(); |
Matt Briggs | 77:176e3eb8f712 | 303 | |
Matt Briggs | 77:176e3eb8f712 | 304 | return true; |
Matt Briggs | 76:d1b20a259d8f | 305 | } |
mbriggs_vortex | 78:43f074baac34 | 306 | |
mbriggs_vortex | 78:43f074baac34 | 307 | void SerialTermMgr::writeBootloaderCtrlPage(uint32_t nBytes) |
mbriggs_vortex | 78:43f074baac34 | 308 | { |
mbriggs_vortex | 78:43f074baac34 | 309 | // This should always be less than 256 bytes |
mbriggs_vortex | 91:8196900df6fe | 310 | |
mbriggs_vortex | 91:8196900df6fe | 311 | // Zero updates the bootloader version so that is cleared until the bootloader adds its back |
mbriggs_vortex | 91:8196900df6fe | 312 | const uint32_t zeroBootloaderVersion = 0; |
mbriggs_vortex | 91:8196900df6fe | 313 | const unsigned int ctrlSize = sizeof(NEW_CODE)+sizeof(nBytes)+sizeof(zeroBootloaderVersion); |
mbriggs_vortex | 78:43f074baac34 | 314 | char buf[ctrlSize]; |
mbriggs_vortex | 78:43f074baac34 | 315 | std::memcpy(buf, NEW_CODE, sizeof(NEW_CODE)); |
mbriggs_vortex | 78:43f074baac34 | 316 | std::memcpy(buf+sizeof(NEW_CODE), &nBytes, sizeof(nBytes)); |
mbriggs_vortex | 91:8196900df6fe | 317 | std::memcpy(buf+sizeof(NEW_CODE)+sizeof(nBytes), |
mbriggs_vortex | 91:8196900df6fe | 318 | &zeroBootloaderVersion, sizeof(zeroBootloaderVersion)); |
mbriggs_vortex | 91:8196900df6fe | 319 | // TODO add reprogram counter or other functions |
mbriggs_vortex | 78:43f074baac34 | 320 | |
mbriggs_vortex | 78:43f074baac34 | 321 | // Copy to first page of flash |
mbriggs_vortex | 78:43f074baac34 | 322 | mFlash->writeStream(0, buf, sizeof(buf)); |
mbriggs_vortex | 78:43f074baac34 | 323 | } |