Skip to content

Commit 567af70

Browse files
committed
Bridge: even more reliable startup
1 parent 0e8c5a6 commit 567af70

File tree

2 files changed

+53
-27
lines changed

2 files changed

+53
-27
lines changed

hardware/arduino/avr/libraries/Bridge/Bridge.cpp

Lines changed: 49 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818

1919
#include "Bridge.h"
2020

21-
BridgeClass::BridgeClass(Stream &_stream) : index(0), stream(_stream), started(false) {
21+
BridgeClass::BridgeClass(Stream &_stream) :
22+
index(0), stream(_stream), started(false), max_retries(0) {
2223
// Empty
2324
}
2425

@@ -27,35 +28,52 @@ void BridgeClass::begin() {
2728
return;
2829
started = true;
2930

30-
// Wait for Atheros bootloader to finish startup
31+
// Wait for U-boot to finish startup
3132
do {
3233
dropAll();
33-
delay(1100);
34+
delay(1000);
3435
} while (stream.available()>0);
36+
37+
while (true) {
38+
// Bridge interrupt:
39+
// - Ask the bridge to close itself
40+
uint8_t quit_cmd[] = {'X','X','X','X','X'};
41+
max_retries = 1;
42+
transfer(quit_cmd, 5);
43+
44+
// Bridge startup:
45+
// - If the bridge is not running starts it safely
46+
stream.print(CTRL_C);
47+
delay(250);
48+
stream.print(F("\n"));
49+
delay(250);
50+
stream.print(F("\n"));
51+
delay(500);
52+
// Wait for OpenWRT message
53+
// "Press enter to activate console"
54+
stream.print(F("run-bridge\n"));
55+
delay(500);
56+
dropAll();
3557

36-
// Bridge startup:
37-
// - If the bridge is not running starts it safely
38-
stream.print(CTRL_C);
39-
delay(250);
40-
stream.print(F("\n"));
41-
delay(500);
42-
stream.print(F("\n"));
43-
delay(750);
44-
// Wait for OpenWRT message
45-
// "Press enter to activate console"
46-
stream.print(F("run-bridge\n"));
47-
delay(500);
48-
dropAll();
49-
50-
// - If the bridge was already running previous commands
51-
// are ignored as "invalid packets".
52-
53-
// Reset the brigde
54-
uint8_t cmd[] = {'X','X', '1','0','0'};
55-
uint8_t res[1];
56-
transfer(cmd, 5, res, 1);
57-
if (res[0] != 0)
58-
while (true);
58+
// Reset the brigde to check if it is running
59+
uint8_t cmd[] = {'X','X', '1','0','0'};
60+
uint8_t res[1];
61+
max_retries = 20;
62+
uint16_t l = transfer(cmd, 5, res, 1);
63+
if (l == TRANSFER_TIMEOUT) {
64+
// Bridge didn't start...
65+
// Maybe the board is starting-up?
66+
67+
// Wait and retry
68+
delay(1000);
69+
continue;
70+
}
71+
if (res[0] != 0)
72+
while (true);
73+
74+
max_retries = 50;
75+
return;
76+
}
5977
}
6078

6179
void BridgeClass::put(const char *key, const char *value) {
@@ -99,7 +117,8 @@ uint16_t BridgeClass::transfer(const uint8_t *buff1, uint16_t len1,
99117
uint8_t *rxbuff, uint16_t rxlen)
100118
{
101119
uint16_t len = len1 + len2 + len3;
102-
for ( ; ; delay(100), dropAll() /* Delay for retransmission */) {
120+
uint8_t retries = 0;
121+
for ( ; retries<max_retries; retries++, delay(100), dropAll() /* Delay for retransmission */) {
103122
// Send packet
104123
crcReset();
105124
stream.write((char)0xFF); // Start of packet (0xFF)
@@ -177,6 +196,9 @@ uint16_t BridgeClass::transfer(const uint8_t *buff1, uint16_t len1,
177196
return rxlen;
178197
return l;
179198
}
199+
200+
// Max retries exceeded
201+
return TRANSFER_TIMEOUT;
180202
}
181203

182204
int BridgeClass::timedRead(unsigned int timeout) {

hardware/arduino/avr/libraries/Bridge/Bridge.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ class BridgeClass {
5050
const uint8_t *buff2, uint16_t len2,
5151
uint8_t *rxbuff, uint16_t rxlen)
5252
{ return transfer(buff1, len1, buff2, len2, NULL, 0, rxbuff, rxlen); }
53+
54+
static const int TRANSFER_TIMEOUT = 0xFFFF;
55+
5356
private:
5457
uint8_t index;
5558
int timedRead(unsigned int timeout);
@@ -66,6 +69,7 @@ class BridgeClass {
6669
static const char CTRL_C = 3;
6770
Stream &stream;
6871
bool started;
72+
uint8_t max_retries;
6973
};
7074

7175
// This subclass uses a serial port Stream

0 commit comments

Comments
 (0)