]> www.fi.muni.cz Git - openparking.git/commitdiff
modbus: make modbus_poll return value
authorJan "Yenya" Kasprzak <kas@fi.muni.cz>
Sat, 23 May 2015 15:48:50 +0000 (17:48 +0200)
committerJan "Yenya" Kasprzak <kas@fi.muni.cz>
Sat, 23 May 2015 15:50:27 +0000 (17:50 +0200)
- the return value is usable for determining whether a potentially
lengthy packet processing went on.

firmware/modbus.c
firmware/modbus.h

index 4e8456354e1a5385d630eb7f4cd59cff0f6606a3..00a9370a05bdaa872ce14b38f95de35d194d275b 100644 (file)
@@ -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)
index 9f5afa6f7329d0c9971cbcb215bbc55500b5b025..e762b706a5173542f795e15eb8cdd16674d21eb8 100644 (file)
@@ -8,8 +8,17 @@
  * All bugs by Jan "Yenya" Kasprzak <kas@fi.muni.cz> :-)
  */
 
+/*
+ * 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,