Bridge: even more reliable startup

This commit is contained in:
Cristian Maglie 2013-07-10 20:24:16 +02:00
parent 3c39763ced
commit dec517181e
2 changed files with 53 additions and 27 deletions

View File

@ -18,7 +18,8 @@
#include "Bridge.h" #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 // Empty
} }
@ -27,35 +28,52 @@ void BridgeClass::begin() {
return; return;
started = true; started = true;
// Wait for Atheros bootloader to finish startup // Wait for U-boot to finish startup
do { do {
dropAll(); dropAll();
delay(1100); delay(1000);
} while (stream.available()>0); } 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: // Reset the brigde to check if it is running
// - If the bridge is not running starts it safely uint8_t cmd[] = {'X','X', '1','0','0'};
stream.print(CTRL_C); uint8_t res[1];
delay(250); max_retries = 20;
stream.print(F("\n")); uint16_t l = transfer(cmd, 5, res, 1);
delay(500); if (l == TRANSFER_TIMEOUT) {
stream.print(F("\n")); // Bridge didn't start...
delay(750); // Maybe the board is starting-up?
// Wait for OpenWRT message
// "Press enter to activate console" // Wait and retry
stream.print(F("run-bridge\n")); delay(1000);
delay(500); continue;
dropAll(); }
if (res[0] != 0)
// - If the bridge was already running previous commands while (true);
// are ignored as "invalid packets".
max_retries = 50;
// Reset the brigde return;
uint8_t cmd[] = {'X','X', '1','0','0'}; }
uint8_t res[1];
transfer(cmd, 5, res, 1);
if (res[0] != 0)
while (true);
} }
void BridgeClass::put(const char *key, const char *value) { 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) uint8_t *rxbuff, uint16_t rxlen)
{ {
uint16_t len = len1 + len2 + len3; 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 // Send packet
crcReset(); crcReset();
stream.write((char)0xFF); // Start of packet (0xFF) 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 rxlen;
return l; return l;
} }
// Max retries exceeded
return TRANSFER_TIMEOUT;
} }
int BridgeClass::timedRead(unsigned int timeout) { int BridgeClass::timedRead(unsigned int timeout) {

View File

@ -50,6 +50,9 @@ public:
const uint8_t *buff2, uint16_t len2, const uint8_t *buff2, uint16_t len2,
uint8_t *rxbuff, uint16_t rxlen) uint8_t *rxbuff, uint16_t rxlen)
{ return transfer(buff1, len1, buff2, len2, NULL, 0, rxbuff, rxlen); } { return transfer(buff1, len1, buff2, len2, NULL, 0, rxbuff, rxlen); }
static const int TRANSFER_TIMEOUT = 0xFFFF;
private: private:
uint8_t index; uint8_t index;
int timedRead(unsigned int timeout); int timedRead(unsigned int timeout);
@ -66,6 +69,7 @@ private:
static const char CTRL_C = 3; static const char CTRL_C = 3;
Stream &stream; Stream &stream;
bool started; bool started;
uint8_t max_retries;
}; };
// This subclass uses a serial port Stream // This subclass uses a serial port Stream