Skip to content

Commit

Permalink
added timeout on modbus
Browse files Browse the repository at this point in the history
  • Loading branch information
thpe committed Jun 23, 2024
1 parent 04d4431 commit 9e9d7d3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 16 deletions.
46 changes: 31 additions & 15 deletions include/scd30.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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;}
Expand All @@ -39,6 +56,9 @@ class SCD30 {
uart_set_fifo_enabled(uart_id<UART>(), false);
uart_set_hw_flow(uart_id<UART>(), false, false);

hardware_alarm_claim(alarm_scd30);
hardware_alarm_set_callback(alarm_scd30, &on_timeout);

// Set our data format
//uart_set_format(uart_id<UART>(), 8, 1, UART_PARITY_NONE);

Expand All @@ -53,24 +73,21 @@ class SCD30 {

void txReset() {
resp_len = sizeof(soft_reset);
wait_start = 0;
finished = 0;
clear_state();
tx_arr_crc(uart_id<UART>(), 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<UART>(), start_cont, sizeof(start_cont)-2);
}

/**
** Send measurement request.
*/
void txMeas() {
wait_start = 0;
finished = 0;
clear_state();
resp_len = readMultipleHoldingRegister(uart_id<UART>(), 0x61, 0x28, 0x06);
}

Expand Down Expand Up @@ -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<UART>())) {
tot_chars_rxed++;
Expand All @@ -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.
**
Expand All @@ -161,7 +185,7 @@ class SCD30 {
}

int check_resp() {
if (finished) {
if (finished && timeout) {
return check_crc();
} else {
return 0;
Expand Down Expand Up @@ -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;
};
4 changes: 3 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down

0 comments on commit 9e9d7d3

Please sign in to comment.