diff options
author | Cristian Maglie <c.maglie@bug.st> | 2013-07-10 20:24:16 +0200 |
---|---|---|
committer | Cristian Maglie <c.maglie@bug.st> | 2013-07-10 20:24:16 +0200 |
commit | dec517181e596da42420807a50d61f4208cf4c49 (patch) | |
tree | b700981dca9edcf17f980117c628005205bd1276 | |
parent | 3c39763ced9b42222ce54e35dee04ae4a9a0afe4 (diff) |
Bridge: even more reliable startup
-rw-r--r-- | libraries/Bridge/Bridge.cpp | 76 | ||||
-rw-r--r-- | libraries/Bridge/Bridge.h | 4 |
2 files changed, 53 insertions, 27 deletions
diff --git a/libraries/Bridge/Bridge.cpp b/libraries/Bridge/Bridge.cpp index c929ed8..363dcd1 100644 --- a/libraries/Bridge/Bridge.cpp +++ b/libraries/Bridge/Bridge.cpp @@ -18,7 +18,8 @@ #include "Bridge.h" -BridgeClass::BridgeClass(Stream &_stream) : index(0), stream(_stream), started(false) { +BridgeClass::BridgeClass(Stream &_stream) : + index(0), stream(_stream), started(false), max_retries(0) { // Empty } @@ -27,35 +28,52 @@ void BridgeClass::begin() { return; started = true; - // Wait for Atheros bootloader to finish startup + // Wait for U-boot to finish startup do { dropAll(); - delay(1100); + delay(1000); } while (stream.available()>0); + + while (true) { + // Bridge interrupt: + // - Ask the bridge to close itself + uint8_t quit_cmd[] = {'X','X','X','X','X'}; + max_retries = 1; + transfer(quit_cmd, 5); + + // Bridge startup: + // - If the bridge is not running starts it safely + stream.print(CTRL_C); + delay(250); + stream.print(F("\n")); + delay(250); + stream.print(F("\n")); + delay(500); + // Wait for OpenWRT message + // "Press enter to activate console" + stream.print(F("run-bridge\n")); + delay(500); + dropAll(); - // Bridge startup: - // - If the bridge is not running starts it safely - stream.print(CTRL_C); - delay(250); - stream.print(F("\n")); - delay(500); - stream.print(F("\n")); - delay(750); - // Wait for OpenWRT message - // "Press enter to activate console" - stream.print(F("run-bridge\n")); - delay(500); - dropAll(); - - // - If the bridge was already running previous commands - // are ignored as "invalid packets". - - // Reset the brigde - uint8_t cmd[] = {'X','X', '1','0','0'}; - uint8_t res[1]; - transfer(cmd, 5, res, 1); - if (res[0] != 0) - while (true); + // Reset the brigde to check if it is running + uint8_t cmd[] = {'X','X', '1','0','0'}; + uint8_t res[1]; + max_retries = 20; + uint16_t l = transfer(cmd, 5, res, 1); + if (l == TRANSFER_TIMEOUT) { + // Bridge didn't start... + // Maybe the board is starting-up? + + // Wait and retry + delay(1000); + continue; + } + if (res[0] != 0) + while (true); + + max_retries = 50; + return; + } } void BridgeClass::put(const char *key, const char *value) { @@ -99,7 +117,8 @@ uint16_t BridgeClass::transfer(const uint8_t *buff1, uint16_t len1, uint8_t *rxbuff, uint16_t rxlen) { uint16_t len = len1 + len2 + len3; - for ( ; ; delay(100), dropAll() /* Delay for retransmission */) { + uint8_t retries = 0; + for ( ; retries<max_retries; retries++, delay(100), dropAll() /* Delay for retransmission */) { // Send packet crcReset(); stream.write((char)0xFF); // Start of packet (0xFF) @@ -177,6 +196,9 @@ uint16_t BridgeClass::transfer(const uint8_t *buff1, uint16_t len1, return rxlen; return l; } + + // Max retries exceeded + return TRANSFER_TIMEOUT; } int BridgeClass::timedRead(unsigned int timeout) { diff --git a/libraries/Bridge/Bridge.h b/libraries/Bridge/Bridge.h index cfbbfa4..e4ed9f8 100644 --- a/libraries/Bridge/Bridge.h +++ b/libraries/Bridge/Bridge.h @@ -50,6 +50,9 @@ public: const uint8_t *buff2, uint16_t len2, uint8_t *rxbuff, uint16_t rxlen) { return transfer(buff1, len1, buff2, len2, NULL, 0, rxbuff, rxlen); } + + static const int TRANSFER_TIMEOUT = 0xFFFF; + private: uint8_t index; int timedRead(unsigned int timeout); @@ -66,6 +69,7 @@ private: static const char CTRL_C = 3; Stream &stream; bool started; + uint8_t max_retries; }; // This subclass uses a serial port Stream |