From 9e9d7d343e29454c835ccfd1a3392e8ad4b91d76 Mon Sep 17 00:00:00 2001 From: Thomas Petig Date: Mon, 24 Jun 2024 01:28:57 +0200 Subject: [PATCH] added timeout on modbus --- include/scd30.hpp | 46 +++++++++++++++++++++++++++++++--------------- src/main.cpp | 4 +++- 2 files changed, 34 insertions(+), 16 deletions(-) diff --git a/include/scd30.hpp b/include/scd30.hpp index 27dcddd..cd93e56 100644 --- a/include/scd30.hpp +++ b/include/scd30.hpp @@ -4,6 +4,10 @@ #include "crc.h" #include "measurement.hpp" +/** Inter-character timeout in us */ +#define MODBUS_INTERCHAR_TIMEOUT 750 +/** Inter-frame delay in us */ +#define MODBUS_INTERFRAME_DELAY 1750 const uint8_t start_cont[] = {0x61, 0x06, 0x00, 0x36, 0x00, 0x00, 0x60, 0x64}; const uint8_t stop_cont[] = {0x61, 0x06, 0x00, 0x37, 0x00, 0x01, 0xF0, 0x64}; @@ -18,6 +22,19 @@ int chars_rxed = 0; int tot_chars_rxed = 0; int finished = 0; int wait_start = 0; +absolute_time_t last_rx; +int timeout = 0; +void clear_state() +{ + chars_rxed = 0; + finished = 0; + wait_start = 0; + timeout = 0; +} +void on_timeout(unsigned int alarm_num) { + timeout = 1; +} +constexpr int alarm_scd30 = 0; template< int ID > uart_inst_t* uart_id(); template<> uart_inst_t* uart_id<0> () {return uart0;} @@ -39,6 +56,9 @@ class SCD30 { uart_set_fifo_enabled(uart_id(), false); uart_set_hw_flow(uart_id(), false, false); + hardware_alarm_claim(alarm_scd30); + hardware_alarm_set_callback(alarm_scd30, &on_timeout); + // Set our data format //uart_set_format(uart_id(), 8, 1, UART_PARITY_NONE); @@ -53,15 +73,13 @@ class SCD30 { void txReset() { resp_len = sizeof(soft_reset); - wait_start = 0; - finished = 0; + clear_state(); tx_arr_crc(uart_id(), soft_reset, sizeof(soft_reset)-2); } void txContStart() { resp_len = sizeof(start_cont); - wait_start = 0; - finished = 0; + clear_state(); tx_arr_crc(uart_id(), start_cont, sizeof(start_cont)-2); } @@ -69,8 +87,7 @@ class SCD30 { ** Send measurement request. */ void txMeas() { - wait_start = 0; - finished = 0; + clear_state(); resp_len = readMultipleHoldingRegister(uart_id(), 0x61, 0x28, 0x06); } @@ -121,6 +138,9 @@ class SCD30 { printf("SCD30 status: rx chars %d (%d), finished %d \r\n", chars_rxed, tot_chars_rxed, finished); } + /** + ** Callback for one byte. + */ static void on_uart_rx() { while (uart_is_readable(uart_id())) { tot_chars_rxed++; @@ -146,8 +166,12 @@ class SCD30 { finished=1; } } + last_rx = get_absolute_time(); + absolute_time_t to = delayed_by_us(last_rx, MODBUS_INTERFRAME_DELAY); + hardware_alarm_set_target(alarm_scd30, to); } + /** ** Check the CRC of the response. ** @@ -161,7 +185,7 @@ class SCD30 { } int check_resp() { - if (finished) { + if (finished && timeout) { return check_crc(); } else { return 0; @@ -193,13 +217,5 @@ class SCD30 { private: - uint8_t msb(uint16_t data) - { - return (data >> 8) & 0xFF; - } - uint8_t lsb(uint16_t data) - { - return data & 0xFF; - } static constexpr int baud = 19200; }; diff --git a/src/main.cpp b/src/main.cpp index 2c41366..456d30e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -142,8 +142,10 @@ int main() { measurement.v_cell = adc_read_V(ADC_CELL); measurement.v_sys = adc_read_V(ADC_SYS); queue_try_add(&res_queue, &measurement); + } else { + printf("ERROR\r\n"); + scd.printStatus(); } -// printf("\r\n"); // scd.check_resp(); } }