From ff6ec8c6a809104b693b147c538e19aef6258e58 Mon Sep 17 00:00:00 2001 From: Paolo Patruno Date: Tue, 15 Mar 2022 17:39:19 +0100 Subject: [PATCH 1/2] close #417 --- .../stima_v3/i2c-rain/src/i2c-rain-config.h | 10 +++++++++ platformio/stima_v3/i2c-rain/src/i2c-rain.h | 12 +++++++++++ platformio/stima_v3/i2c-rain/src/i2c-rain.ino | 20 ++++++++++++++++++ .../stima_v3/i2c-th/src/i2c-th-config.h | 7 +++++++ platformio/stima_v3/i2c-th/src/i2c-th.h | 13 ++++++++++++ platformio/stima_v3/i2c-th/src/i2c-th.ino | 21 ++++++++++++++++++- 6 files changed, 82 insertions(+), 1 deletion(-) diff --git a/platformio/stima_v3/i2c-rain/src/i2c-rain-config.h b/platformio/stima_v3/i2c-rain/src/i2c-rain-config.h index e202e2e10..49e1807ab 100644 --- a/platformio/stima_v3/i2c-rain/src/i2c-rain-config.h +++ b/platformio/stima_v3/i2c-rain/src/i2c-rain-config.h @@ -169,3 +169,13 @@ WDTO_1S, WDTO_2S, WDTO_4S, WDTO_8S #define TIMER1_TCNT1_VALUE (0xFFFFUL - (TIMER1_INTERRUPT_TIME_MS*1000UL/(1024 / (F_CPU/1000000)))+1) #endif + +/********************************************************************* +* TASKS +*********************************************************************/ + +/*! +\def TRANSACTION_TIMEOUT_MS +\brief Timeout for command transaction. +*/ +#define TRANSACTION_TIMEOUT_MS (12000) diff --git a/platformio/stima_v3/i2c-rain/src/i2c-rain.h b/platformio/stima_v3/i2c-rain/src/i2c-rain.h index e9d483e20..b3516c120 100644 --- a/platformio/stima_v3/i2c-rain/src/i2c-rain.h +++ b/platformio/stima_v3/i2c-rain/src/i2c-rain.h @@ -209,6 +209,18 @@ volatile uint8_t ready_tasks_count; */ uint32_t awakened_event_occurred_time_ms; +/*! +\var inside_transaction +\brief Status of command transaction. +*/ +volatile bool inside_transaction; + +/*! +\var transaction_time +\brief Timer counter variable for compute command transaction timeout. +*/ +volatile uint16_t transaction_time; + /*! \var is_start \brief Execute start command. diff --git a/platformio/stima_v3/i2c-rain/src/i2c-rain.ino b/platformio/stima_v3/i2c-rain/src/i2c-rain.ino index c9726e073..596cbea5b 100644 --- a/platformio/stima_v3/i2c-rain/src/i2c-rain.ino +++ b/platformio/stima_v3/i2c-rain/src/i2c-rain.ino @@ -242,6 +242,9 @@ void init_tasks() { // reset tipping bucket debounce value rain_tips_event_occurred_time_ms = -configuration.tipping_bucket_time_ms; interrupts(); + + transaction_time = 0; + inside_transaction = false; } void init_pins() { @@ -297,6 +300,16 @@ ISR(TIMER1_OVF_vect) { //! Pre-load timer counter register TCNT1 = TIMER1_TCNT1_VALUE; i2c_time+=TIMER1_INTERRUPT_TIME_MS/1000; + + if (inside_transaction) { + //! increment transaction_time by TIMER1_INTERRUPT_TIME_MS + transaction_time += TIMER1_INTERRUPT_TIME_MS; + + if (transaction_time >= TRANSACTION_TIMEOUT_MS) { + transaction_time = 0; + inside_transaction = false; + } + } } #endif @@ -386,6 +399,8 @@ void i2c_request_interrupt_handler() { // write readable_data_length bytes of data stored in readable_data_read_ptr (base) + readable_data_address (offset) on i2c bus Wire.write((uint8_t *)readable_data_read_ptr+readable_data_address, readable_data_length); Wire.write(crc8((uint8_t *)readable_data_read_ptr+readable_data_address, readable_data_length)); + + inside_transaction = false; } void i2c_receive_interrupt_handler(int rx_data_length) { @@ -565,6 +580,7 @@ void command_task() { LOGN(F("Execute [ %s ]"), "ONESHOT STOP"); is_start = false; is_stop = true; + inside_transaction = true; } else { LOGE(F("Skip command [ %s ] in continous mode"), "ONESHOT STOP"); } @@ -575,6 +591,7 @@ void command_task() { LOGN(F("Execute [ %s ]"), "ONESHOT START-STOP"); is_start = true; is_stop = true; + inside_transaction = true; } else { LOGE(F("Skip command [ %s ] in continous mode"), "ONESHOT START-STOP"); } @@ -608,6 +625,9 @@ void command_task() { void commands() { + + if (inside_transaction) return; + noInterrupts(); if (configuration.is_oneshot){ diff --git a/platformio/stima_v3/i2c-th/src/i2c-th-config.h b/platformio/stima_v3/i2c-th/src/i2c-th-config.h index b7ade5dd2..193c6b8a6 100644 --- a/platformio/stima_v3/i2c-th/src/i2c-th-config.h +++ b/platformio/stima_v3/i2c-th/src/i2c-th-config.h @@ -227,4 +227,11 @@ setting it to 3980 ms we gain 20 ms every sample, 300 ms every observation, 4500 */ #define SENSORS_RETRY_DELAY_MS (100) +/*! +\def TRANSACTION_TIMEOUT_MS +\brief Timeout for command transaction. +*/ +#define TRANSACTION_TIMEOUT_MS (12000) + + #endif diff --git a/platformio/stima_v3/i2c-th/src/i2c-th.h b/platformio/stima_v3/i2c-th/src/i2c-th.h index fa6ad6cec..4cb09857f 100644 --- a/platformio/stima_v3/i2c-th/src/i2c-th.h +++ b/platformio/stima_v3/i2c-th/src/i2c-th.h @@ -246,6 +246,19 @@ volatile uint8_t ready_tasks_count; */ uint32_t awakened_event_occurred_time_ms; +/*! +\var inside_transaction +\brief Status of command transaction. +*/ +volatile bool inside_transaction; + +/*! +\var transaction_time +\brief Timer counter variable for compute command transaction timeout. +*/ +volatile uint16_t transaction_time; + + /*! \var is_start \brief Execute start command. diff --git a/platformio/stima_v3/i2c-th/src/i2c-th.ino b/platformio/stima_v3/i2c-th/src/i2c-th.ino index f26439264..dc1b1057a 100644 --- a/platformio/stima_v3/i2c-th/src/i2c-th.ino +++ b/platformio/stima_v3/i2c-th/src/i2c-th.ino @@ -228,7 +228,8 @@ void init_tasks() { is_start = false; is_stop = false; is_test_read = false; - + transaction_time = 0; + inside_transaction = false; } void init_pins() { @@ -399,6 +400,16 @@ ISR(TIMER1_OVF_vect) { if (timer_counter >= TIMER1_VALUE_MAX_MS) { timer_counter = 0; } + + if (inside_transaction) { + //! increment transaction_time by TIMER1_INTERRUPT_TIME_MS + transaction_time += TIMER1_INTERRUPT_TIME_MS; + + if (transaction_time >= TRANSACTION_TIMEOUT_MS) { + transaction_time = 0; + inside_transaction = false; + } + } } void i2c_request_interrupt_handler() { @@ -427,6 +438,8 @@ void i2c_request_interrupt_handler() { readable_data_address=0xFF; readable_data_length=0; + inside_transaction = false; + } void i2c_receive_interrupt_handler(int rx_data_length) { @@ -1026,6 +1039,7 @@ void command_task() { is_stop = true; is_test_read = false; commands(); + inside_transaction = true; break; case I2C_TH_COMMAND_ONESHOT_START_STOP: @@ -1034,6 +1048,7 @@ void command_task() { is_stop = true; is_test_read = false; commands(); + inside_transaction = true; break; case I2C_TH_COMMAND_CONTINUOUS_START: @@ -1050,6 +1065,7 @@ void command_task() { is_stop = true; is_test_read = false; commands(); + inside_transaction = true; break; case I2C_TH_COMMAND_CONTINUOUS_START_STOP: @@ -1058,6 +1074,7 @@ void command_task() { is_stop = true; is_test_read = false; commands(); + inside_transaction = true; break; case I2C_TH_COMMAND_TEST_READ: @@ -1097,6 +1114,8 @@ void copy_buffers() { void commands() { + if (inside_transaction) return; + //! CONTINUOUS TEST if (!configuration.is_oneshot && is_start && !is_stop && is_test_read) { copy_buffers(); From c7fc6d2e4e2af77db970916ff4c2b5914e8fdbed Mon Sep 17 00:00:00 2001 From: Paolo Patruno Date: Tue, 15 Mar 2022 21:54:42 +0100 Subject: [PATCH 2/2] bugs in transaction management --- platformio/stima_v3/i2c-rain/src/i2c-rain.ino | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/platformio/stima_v3/i2c-rain/src/i2c-rain.ino b/platformio/stima_v3/i2c-rain/src/i2c-rain.ino index 596cbea5b..eab2ec7de 100644 --- a/platformio/stima_v3/i2c-rain/src/i2c-rain.ino +++ b/platformio/stima_v3/i2c-rain/src/i2c-rain.ino @@ -1,5 +1,3 @@ -/**@file i2c-rain.ino */ - /********************************************************************* Copyright (C) 2022 Marco Baldinetti authors: @@ -570,6 +568,7 @@ void command_task() { LOGN(F("Execute [ %s ]"), "ONESHOT START"); is_start = true; is_stop = false; + commands(); } else { LOGE(F("Skip command [ %s ] in continous mode"), "ONESHOT START"); } @@ -580,6 +579,7 @@ void command_task() { LOGN(F("Execute [ %s ]"), "ONESHOT STOP"); is_start = false; is_stop = true; + commands(); inside_transaction = true; } else { LOGE(F("Skip command [ %s ] in continous mode"), "ONESHOT STOP"); @@ -591,6 +591,7 @@ void command_task() { LOGN(F("Execute [ %s ]"), "ONESHOT START-STOP"); is_start = true; is_stop = true; + commands(); inside_transaction = true; } else { LOGE(F("Skip command [ %s ] in continous mode"), "ONESHOT START-STOP"); @@ -602,6 +603,7 @@ void command_task() { is_start = false; is_stop = true; is_test = true; + commands(); break; case I2C_RAIN_COMMAND_SAVE: @@ -613,8 +615,6 @@ void command_task() { default: LOGE(F("Command UNKNOWN")); } - - commands(); noInterrupts(); is_event_command_task = false;