Skip to content

Commit

Permalink
sensors/ev3_uart_sensor_ld: improve resync of data messages
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dlech committed Mar 7, 2020
1 parent 45e0cee commit 1408921
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion sensors/ev3_uart_sensor_ld.c
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,7 @@ static int ev3_uart_receive_buf2(struct tty_struct *tty,

while (pos < count) {
if (port->partial_msg_size) {
msg_type = port->msg[0] & EV3_UART_MSG_TYPE_MASK;
msg_size = ev3_uart_msg_size(port->msg[0]);
} else if (cp[pos] == 0xFF) {
/*
Expand All @@ -580,9 +581,29 @@ static int ev3_uart_receive_buf2(struct tty_struct *tty,
pos++;
continue;
} else {
msg_type = cp[pos] & EV3_UART_MSG_TYPE_MASK;
msg_size = ev3_uart_msg_size(cp[pos]);
}

if (port->info_done && (msg_type != EV3_UART_MSG_TYPE_DATA ||
msg_size < 3 ||
msg_size > EV3_UART_MAX_MESSAGE_SIZE)) {
/**
* If we are receiving data, sometimes there can
* be a hardware buffer overflow and we get out
* of sync. So keep trying to get back in sync.
*/
if (port->partial_msg_size) {
port->partial_msg_size--;
for (i = 0; i < port->partial_msg_size; i++) {
port->msg[i] = port->msg[i + 1];
}
} else {
pos++;
}
continue;
}

if (msg_size > EV3_UART_MAX_MESSAGE_SIZE) {
debug_pr("header: 0x%02x\n", cp[pos]);
port->last_err = "Bad message size";
Expand Down Expand Up @@ -615,7 +636,6 @@ static int ev3_uart_receive_buf2(struct tty_struct *tty,
pos += msg_size;
}

msg_type = message[0] & EV3_UART_MSG_TYPE_MASK;
cmd = message[0] & EV3_UART_MSG_CMD_MASK;
mode = cmd;
cmd2 = message[1];
Expand Down

0 comments on commit 1408921

Please sign in to comment.