From: Jan "Yenya" Kasprzak Date: Sat, 23 May 2015 19:02:37 +0000 (+0200) Subject: Do more RX checks in the IRQ handler. X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?a=commitdiff_plain;h=63da2000b15179f9c6c42965f9cc0115ed95c5f7;p=openparking.git Do more RX checks in the IRQ handler. --- diff --git a/firmware/modbus.c b/firmware/modbus.c index 00a9370..e45f5f0 100644 --- a/firmware/modbus.c +++ b/firmware/modbus.c @@ -169,18 +169,10 @@ uint8_t modbus_poll() if (transmitting) return 0; - if (buf_len == 0) // nothing received yet - return 0; - if (get_clock() - last_rx < REQ_TIMEOUT) // still receiving return 0; - if (buf_len < 4) { // too short - buf_len = 0; - return 0; - } - - if (buffer[0] != mb_unit_id) { // not for myself + if (buf_len < 4) { // too short (or not for us) buf_len = 0; return 0; } @@ -241,16 +233,28 @@ uint8_t modbus_poll() ISR(USART_RX_vect) { uint8_t rx_byte = UDR0; + clock_t now = get_clock(); + + if (transmitting) // how did we get here? discard it + goto out; - if (transmitting) // discard it - return; + if (buf_len && buffer[0] != mb_unit_id) // not for us + goto out; - buffer[buf_len] = rx_byte; + if (buf_len == BUFSIZE) { // overrun - discard the packet + buffer[0] = 0xFF; + buf_len = 1; + goto out; + } - if (buf_len + 1 < BUFSIZE) // ignore overruns - buf_len++; + if (now - last_rx >= REQ_TIMEOUT) { // new packet; start over + buf_len = 0; + } - last_rx = get_clock(); + // TODO: we can probably calculate the CRC here as well + buffer[buf_len++] = rx_byte; +out: + last_rx = now; } ISR(USART_TX_vect)