Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions platformio/stima_v3/i2c-rain/src/i2c-rain-config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
12 changes: 12 additions & 0 deletions platformio/stima_v3/i2c-rain/src/i2c-rain.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
28 changes: 24 additions & 4 deletions platformio/stima_v3/i2c-rain/src/i2c-rain.ino
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/**@file i2c-rain.ino */

/*********************************************************************
Copyright (C) 2022 Marco Baldinetti <m.baldinetti@digiteco.it>
authors:
Expand Down Expand Up @@ -242,6 +240,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() {
Expand Down Expand Up @@ -297,6 +298,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

Expand Down Expand Up @@ -386,6 +397,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) {
Expand Down Expand Up @@ -555,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");
}
Expand All @@ -565,6 +579,8 @@ 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");
}
Expand All @@ -575,6 +591,8 @@ 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");
}
Expand All @@ -585,6 +603,7 @@ void command_task() {
is_start = false;
is_stop = true;
is_test = true;
commands();
break;

case I2C_RAIN_COMMAND_SAVE:
Expand All @@ -596,8 +615,6 @@ void command_task() {
default:
LOGE(F("Command UNKNOWN"));
}

commands();

noInterrupts();
is_event_command_task = false;
Expand All @@ -608,6 +625,9 @@ void command_task() {


void commands() {

if (inside_transaction) return;

noInterrupts();

if (configuration.is_oneshot){
Expand Down
7 changes: 7 additions & 0 deletions platformio/stima_v3/i2c-th/src/i2c-th-config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 13 additions & 0 deletions platformio/stima_v3/i2c-th/src/i2c-th.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
21 changes: 20 additions & 1 deletion platformio/stima_v3/i2c-th/src/i2c-th.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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();
Expand Down