|
@@ -44,11 +44,15 @@ void TTY::reset()
|
|
|
rx.b64_bits = 0;
|
|
|
}
|
|
|
|
|
|
+static void null_flush(void)
|
|
|
+{
|
|
|
+}
|
|
|
|
|
|
TTY::TTY(Stream &port)
|
|
|
{
|
|
|
_port = &port;
|
|
|
rx_sbuf = true ? xStreamBufferCreate(STREAMBUF_SIZE, 1) : NULL;
|
|
|
+ _flush = null_flush;
|
|
|
reset();
|
|
|
}
|
|
|
|
|
@@ -67,21 +71,31 @@ int TTY::rxdata(void *buf, size_t len)
|
|
|
|
|
|
int rcv = 0;
|
|
|
while (!rcv) {
|
|
|
- if (tx_credits_reset) {
|
|
|
- // Drain input before WRST
|
|
|
- rcv = xStreamBufferReceive(rx_sbuf, buf, len, 0);
|
|
|
+ rcv = xStreamBufferReceive(rx_sbuf, buf, len, 0);
|
|
|
+ if (rcv)
|
|
|
+ break;
|
|
|
|
|
|
- if (rcv)
|
|
|
- break;
|
|
|
+ uint32_t now = millis();
|
|
|
+ if (now - last_rx > 500) {
|
|
|
+ last_rx = now;
|
|
|
+ tx_credits_reset = true;
|
|
|
+ }
|
|
|
|
|
|
+ if (tx_credits_reset) {
|
|
|
+ // Drain input before WRST
|
|
|
+ flush();
|
|
|
if (port().write(WRST)) {
|
|
|
- printf("[UPLD] Resetting window\n");
|
|
|
+ printf("[UPLD] Resetting window, last_ack = %u\n", rx.last_ack);
|
|
|
tx_credits_reset = false;
|
|
|
tx_credits = STREAMBUF_SIZE - BUF_SLACK;
|
|
|
} else {
|
|
|
// Uhm... wait a tiny bit and then try again to sent WRST?
|
|
|
- printf("[UPLD] Failed to reset window?!\n");
|
|
|
- rcv = xStreamBufferReceive(rx_sbuf, buf, len, 10);
|
|
|
+ static bool failed_once = false;
|
|
|
+ if (!failed_once) {
|
|
|
+ printf("[UPLD] Failed to reset window?!\n");
|
|
|
+ failed_once = true;
|
|
|
+ }
|
|
|
+ rcv = xStreamBufferReceive(rx_sbuf, buf, len, 1);
|
|
|
}
|
|
|
} else {
|
|
|
while (tx_credits >= WGO_CHUNK && port().write(WGO))
|
|
@@ -181,6 +195,8 @@ void TTY::_onrx()
|
|
|
int byte, data;
|
|
|
|
|
|
while ((byte = port().read()) >= 0) {
|
|
|
+ last_rx = millis();
|
|
|
+
|
|
|
switch (rx.state) {
|
|
|
case rx_state::normal:
|
|
|
if (rx.rlen < sizeof fwupload_start &&
|
|
@@ -398,17 +414,30 @@ void TTY::uart_onerr(hardwareSerial_error_t err)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+static void uart_flush()
|
|
|
+{
|
|
|
+ Serial0.flush(true);
|
|
|
+}
|
|
|
+
|
|
|
+static void usb_flush()
|
|
|
+{
|
|
|
+ Serial.flush();
|
|
|
+}
|
|
|
+
|
|
|
void TTY::init()
|
|
|
{
|
|
|
enq_str[sizeof(enq_str)-5] += max80_board_version;
|
|
|
|
|
|
uart_tty = new TTY(Serial0);
|
|
|
+ uart_tty->_flush = uart_flush;
|
|
|
Serial0.begin(BAUD_RATE);
|
|
|
Serial0.onReceive(uart_onrx, false);
|
|
|
Serial0.onReceiveError(uart_onerr);
|
|
|
|
|
|
usb_tty = new TTY(Serial);
|
|
|
+ usb_tty->_flush = usb_flush;
|
|
|
Serial.onEvent(usb_onevent);
|
|
|
+ Serial.enableReboot(true);
|
|
|
}
|
|
|
|
|
|
void TTY::ping()
|