]> www.fi.muni.cz Git - openparking.git/commitdiff
error counters to modbus
authorJan "Yenya" Kasprzak <kas@fi.muni.cz>
Mon, 25 May 2015 18:21:33 +0000 (20:21 +0200)
committerJan "Yenya" Kasprzak <kas@fi.muni.cz>
Mon, 25 May 2015 18:21:33 +0000 (20:21 +0200)
firmware/firmware.c
firmware/modbus.c
firmware/modbus.h

index 8e973510ae55d1577ecc28b2ec2b413d85d59f43..c2dd944e54f12566a8bee73473076fe99f7b03e4 100644 (file)
@@ -25,6 +25,7 @@
 #define free_bitmap    (hold_regs[MB_N_HOLD_REGS_EEPROM+13])
 #define err_bitmap     (hold_regs[MB_N_HOLD_REGS_EEPROM+14])
 #define max_distances  (hold_regs+MB_N_HOLD_REGS_EEPROM+21)
+#define err_counts     (hold_regs+MB_N_HOLD_REGS_EEPROM+41)
 
 static void pull_trigger(uint8_t trig)
 {
@@ -104,16 +105,32 @@ static void do_measurement(unsigned char trig)
                }
        }
 
-       for (i = 0; i < N_TRIG_SENSORS; i++)
-               if (to_start & (1 << i))
-                       distances[trig*N_TRIG_SENSORS + i] = -1;
-               else if (to_measure & (1 << i))
+       for (i = 0; i < N_TRIG_SENSORS; i++) {
+               uint8_t off = trig*N_TRIG_SENSORS + i;
+
+               if (to_start & (1 << i)) { // echo not received
+                       uint16_t err_count = err_counts[off] & 0xFF;
+                       if (err_count < 255) {
+                               err_count++;
+                               err_counts[off] = (err_counts[off] & 0xFF00)
+                                       | err_count;
+                       }
+                       distances[off] = -1;
+               } else if (to_measure & (1 << i)) { // echo pulse too long
+                       uint16_t err_count = err_counts[off] >> 8;
+
+                       if (err_count < 255) {
+                               err_count++;
+                               err_counts[off] = (err_counts[off] & 0x00FF)
+                                       | (err_count << 8);
+                       }
                        /*
                         * If the echo pulse is too long, do not treat it
                         * as error, just count it as maximum length.
                         */
-                       distances[trig*N_TRIG_SENSORS + i]
-                               = now - starttimes[i];
+                       distances[off] = now - starttimes[i];
+               }
+       }
 }
 
 static void led_set(uint8_t led, uint8_t state)
index b726280f9922f9de4abea11a702e0a69ba58af0f..c895c5f30ec71336e08a4820b059b9cfec22f827 100644 (file)
@@ -14,7 +14,7 @@
 #include "clock.h"
 #include "modbus.h"
 
-#define BUFSIZE 128 // maximum request size
+#define BUFSIZE 180 // maximum request size
 
 // configure the control pin
 #define ctl_pin_setup()        do { DDRD  |=  _BV(PD2); } while (0)
index e762b706a5173542f795e15eb8cdd16674d21eb8..3a3fcdf28e6fed2db6f6e929049b68b33d64a8ee 100644 (file)
@@ -27,11 +27,11 @@ typedef enum {
        MB_ILLEGAL_VAL  = 3,
 } mb_exception;
 
-#define MB_HOLD_REGS_BASE      1000
-#define MB_N_HOLD_REGS         60
-#define MB_N_HOLD_REGS_EEPROM  20
+#define MB_HOLD_REGS_BASE      0       // first register
+#define MB_N_HOLD_REGS         80      // total # of registers
+#define MB_N_HOLD_REGS_EEPROM  20      // the first N registers eeprom-backed
 extern uint16_t hold_regs[MB_N_HOLD_REGS];
-#define mb_unit_id     (hold_regs[0])
+#define mb_unit_id     (hold_regs[0])  // MODBUS id in the first register
 
 uint8_t hold_reg_is_valid(uint16_t reg, uint16_t val);