From: Jan "Yenya" Kasprzak Date: Sat, 23 May 2015 15:48:50 +0000 (+0200) Subject: modbus: make modbus_poll return value X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?a=commitdiff_plain;h=6146a1caafd449cae4ce08134d763137424300c6;p=openparking.git modbus: make modbus_poll return value - the return value is usable for determining whether a potentially lengthy packet processing went on. --- diff --git a/firmware/modbus.c b/firmware/modbus.c index 4e84563..00a9370 100644 --- a/firmware/modbus.c +++ b/firmware/modbus.c @@ -159,7 +159,7 @@ static mb_exception write_single_reg(uint16_t reg, uint16_t val) return MB_OK; } -void modbus_poll() +uint8_t modbus_poll() { bufptr_t packet_len; uint16_t crc; @@ -167,22 +167,22 @@ void modbus_poll() ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { if (transmitting) - return; + return 0; if (buf_len == 0) // nothing received yet - return; + return 0; if (get_clock() - last_rx < REQ_TIMEOUT) // still receiving - return; + return 0; if (buf_len < 4) { // too short buf_len = 0; - return; + return 0; } if (buffer[0] != mb_unit_id) { // not for myself buf_len = 0; - return; + return 0; } transmitting = 1; // disable further reads @@ -197,7 +197,7 @@ void modbus_poll() transmitting = 0; buf_len = 0; } - return; + return 1; } packet_len -= 2; // strip the CRC @@ -234,6 +234,8 @@ void modbus_poll() tx_ptr = 0; ctl_pin_on(); UCSR0B |= _BV(UDRIE0); + + return 1; } ISR(USART_RX_vect) diff --git a/firmware/modbus.h b/firmware/modbus.h index 9f5afa6..e762b70 100644 --- a/firmware/modbus.h +++ b/firmware/modbus.h @@ -8,8 +8,17 @@ * All bugs by Jan "Yenya" Kasprzak :-) */ +/* + * Call this at the beginning of the program. + */ void modbus_init(uint8_t unit_id); -void modbus_poll(); + +/* + * Call this frequently. Returns 0 when no data was received + * and the processing went on quickly. Returns 1 after potentially lengthy + * packet processing. + */ +uint8_t modbus_poll(); typedef enum { MB_OK = 0,