Skip to content

Commit 1408921

Browse files
committed
sensors/ev3_uart_sensor_ld: improve resync of data messages
It is common to get UART hardware buffer overruns and miss data after switching to the higher baud rate for receiving data. This adds some extra checks to try to get back into sync after this happens without having to wait for the sensor to timeout and go through the connection sequence again. It doesn't completely fix the problem but is an improvement over the current situation.
1 parent 45e0cee commit 1408921

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

sensors/ev3_uart_sensor_ld.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,7 @@ static int ev3_uart_receive_buf2(struct tty_struct *tty,
571571

572572
while (pos < count) {
573573
if (port->partial_msg_size) {
574+
msg_type = port->msg[0] & EV3_UART_MSG_TYPE_MASK;
574575
msg_size = ev3_uart_msg_size(port->msg[0]);
575576
} else if (cp[pos] == 0xFF) {
576577
/*
@@ -580,9 +581,29 @@ static int ev3_uart_receive_buf2(struct tty_struct *tty,
580581
pos++;
581582
continue;
582583
} else {
584+
msg_type = cp[pos] & EV3_UART_MSG_TYPE_MASK;
583585
msg_size = ev3_uart_msg_size(cp[pos]);
584586
}
585587

588+
if (port->info_done && (msg_type != EV3_UART_MSG_TYPE_DATA ||
589+
msg_size < 3 ||
590+
msg_size > EV3_UART_MAX_MESSAGE_SIZE)) {
591+
/**
592+
* If we are receiving data, sometimes there can
593+
* be a hardware buffer overflow and we get out
594+
* of sync. So keep trying to get back in sync.
595+
*/
596+
if (port->partial_msg_size) {
597+
port->partial_msg_size--;
598+
for (i = 0; i < port->partial_msg_size; i++) {
599+
port->msg[i] = port->msg[i + 1];
600+
}
601+
} else {
602+
pos++;
603+
}
604+
continue;
605+
}
606+
586607
if (msg_size > EV3_UART_MAX_MESSAGE_SIZE) {
587608
debug_pr("header: 0x%02x\n", cp[pos]);
588609
port->last_err = "Bad message size";
@@ -615,7 +636,6 @@ static int ev3_uart_receive_buf2(struct tty_struct *tty,
615636
pos += msg_size;
616637
}
617638

618-
msg_type = message[0] & EV3_UART_MSG_TYPE_MASK;
619639
cmd = message[0] & EV3_UART_MSG_CMD_MASK;
620640
mode = cmd;
621641
cmd2 = message[1];

0 commit comments

Comments
 (0)