Skip to content

Commit

Permalink
better timer1 timing management plus some minor interrupt protection …
Browse files Browse the repository at this point in the history
…of some variables in stima
  • Loading branch information
pat1 committed Mar 14, 2022
1 parent 12df597 commit 5eaea4e
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 78 deletions.
17 changes: 2 additions & 15 deletions platformio/stima_v3/i2c-rain/src/i2c-rain-config.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,23 +162,10 @@ WDTO_1S, WDTO_2S, WDTO_4S, WDTO_8S
*/
#define TIMER1_INTERRUPT_TIME_MS (4000)

/*!
\def TIMER1_OVERFLOW_TIME_MS
\brief Timer1 timer overflow with 1024 prescaler at 8 or 16 MHz.
*/
#if (F_CPU == 8000000L)
#define TIMER1_OVERFLOW_TIME_MS (8388)
#elif (F_CPU == 16000000L)
#define TIMER1_OVERFLOW_TIME_MS (4194) // IMPOSSIBILE CON SENSORS_SAMPLE_TIME_MS a 5000
#if (SENSORS_SAMPLE_TIME_MS > 4000)
#error "compilare a 3.3V"
#endif
#endif

/*!
\def TIMER1_TCNT1_VALUE
\brief Timer1 timer overflow with 1024 prescaler at 8 MHz.
\brief Timer1 timer overflow with 1024 prescaler.
*/
#define TIMER1_TCNT1_VALUE ((uint16_t)(0xFFFF - (float)(1.0 * 0xFFFF * TIMER1_INTERRUPT_TIME_MS / TIMER1_OVERFLOW_TIME_MS)))
#define TIMER1_TCNT1_VALUE (0xFFFFUL - (TIMER1_INTERRUPT_TIME_MS*1000UL/(1024 / (F_CPU/1000000)))+1)

#endif
2 changes: 1 addition & 1 deletion platformio/stima_v3/i2c-rain/src/i2c-rain.ino
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ void init_timer1() {

void start_timer() {
TCCR1A = 0x00; //!< Normal timer operation
TCCR1B = 0x05; //!< 1:1024 prescaler
TCCR1B = (1<<CS10) | (1<<CS12); //!< 1:1024 prescaler
TCNT1 = TIMER1_TCNT1_VALUE; //!< Pre-load timer counter register
TIFR1 |= (1 << TOV1); //!< Clear interrupt overflow flag register
TIMSK1 |= (1 << TOIE1); //!< Enable overflow interrupt
Expand Down
12 changes: 3 additions & 9 deletions platformio/stima_v3/i2c-th/src/i2c-th-config.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,23 +200,17 @@ setting it to 3980 ms we gain 20 ms every sample, 300 ms every observation, 4500
*/
#define TIMER1_INTERRUPT_TIME_MS (SENSORS_SAMPLE_TIME_MS)

/*!
\def TIMER1_OVERFLOW_TIME_MS
\brief Timer1 timer overflow with 1024 prescaler at 8 MHz.
*/
#define TIMER1_OVERFLOW_TIME_MS (8388)

/*!
\def TIMER1_TCNT1_VALUE
\brief Timer1 timer overflow with 1024 prescaler at 8 MHz.
\brief Timer1 timer overflow with 1024 prescaler.
*/
#define TIMER1_TCNT1_VALUE ((uint16_t)(0xFFFF - (float)(1.0 * 0xFFFF * TIMER1_INTERRUPT_TIME_MS / TIMER1_OVERFLOW_TIME_MS)))
#define TIMER1_TCNT1_VALUE (0xFFFFUL - (TIMER1_INTERRUPT_TIME_MS*1000UL/(1024 / (F_CPU/1000000)))+1)

/*!
\def TIMER1_VALUE_MAX_MS
\brief Maximum timer1 counter value for timed tasks.
*/
#define TIMER1_VALUE_MAX_MS (12000)
#define TIMER1_VALUE_MAX_MS (TIMER1_INTERRUPT_TIME_MS * 3)

/*********************************************************************
* TASKS
Expand Down
64 changes: 28 additions & 36 deletions platformio/stima_v3/i2c-th/src/i2c-th.ino
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,12 @@ void loop() {
wdt_reset();
}

noInterrupts();
if (ready_tasks_count == 0) {
wdt_reset();
state = END;
}
interrupts();
break;

case END:
Expand Down Expand Up @@ -202,11 +204,11 @@ void init_buffers() {
memset((void *) &readable_data_write_ptr->humidity, UINT8_MAX, sizeof(value_t));
memset((void *) &readable_data_write_ptr->temperature, UINT8_MAX, sizeof(value_t));

//! copy readable_data_2 in readable_data_1
memcpy((void *) readable_data_read_ptr, (const void*) readable_data_write_ptr, sizeof(readable_data_t));
//! copy readable_data_write in readable_data_read
copy_buffers();

reset_samples_buffer();
reset_report_buffer();
reset_data(readable_data_write_ptr);

readable_data_address=0xFF;
readable_data_length=0;
Expand Down Expand Up @@ -259,9 +261,10 @@ void init_timer1() {

void start_timer() {
TCCR1A = 0x00; //!< Normal timer operation
TCCR1B = 0x05; //!< 1:1024 prescaler
TCCR1B = (1<<CS10) | (1<<CS12); //!< 1:1024 prescaler
TCNT1 = TIMER1_TCNT1_VALUE; //!< Pre-load timer counter register
TIFR1 |= (1 << TOV1); //!< Clear interrupt overflow flag register
timer_counter = 0;
TIMSK1 |= (1 << TOIE1); //!< Enable overflow interrupt
}

Expand Down Expand Up @@ -458,7 +461,7 @@ void i2c_receive_interrupt_handler(int rx_data_length) {
//noInterrupts();
// enable Command task
if (!is_event_command_task) {
reset_readable_data_read(); // make shure read old data wil be impossible
reset_data(readable_data_read_ptr); // make shure read old data wil be impossible
lastcommand=i2c_rx_data[1]; // record command to be executed
is_event_command_task = true; // activate command task
ready_tasks_count++;
Expand Down Expand Up @@ -974,7 +977,7 @@ void make_report (bool init=false) {

void samples_processing() {
LOGN(F("SAMPLE PROCESSING"));
//reset_report_buffer();
//reset_data(readable_data_write_ptr);
make_report();
}

Expand All @@ -991,34 +994,19 @@ void reset_samples_buffer() {
bufferReset<sample_t, uint16_t, int32_t>(&humidity_samples, SAMPLES_COUNT_MAX);
}

void reset_report_buffer () {
readable_data_write_ptr->temperature.sample = UINT16_MAX;
readable_data_write_ptr->temperature.med60 = UINT16_MAX;
readable_data_write_ptr->temperature.med = UINT16_MAX;
readable_data_write_ptr->temperature.max = UINT16_MAX;
readable_data_write_ptr->temperature.min = UINT16_MAX;
readable_data_write_ptr->temperature.sigma = UINT16_MAX;
readable_data_write_ptr->humidity.sample = UINT16_MAX;
readable_data_write_ptr->humidity.med60 = UINT16_MAX;
readable_data_write_ptr->humidity.med = UINT16_MAX;
readable_data_write_ptr->humidity.max = UINT16_MAX;
readable_data_write_ptr->humidity.min = UINT16_MAX;
readable_data_write_ptr->humidity.sigma = UINT16_MAX;
}

void reset_readable_data_read() {
readable_data_read_ptr->temperature.sample = UINT16_MAX;
readable_data_read_ptr->temperature.med60 = UINT16_MAX;
readable_data_read_ptr->temperature.med = UINT16_MAX;
readable_data_read_ptr->temperature.max = UINT16_MAX;
readable_data_read_ptr->temperature.min = UINT16_MAX;
readable_data_read_ptr->temperature.sigma = UINT16_MAX;
readable_data_read_ptr->humidity.sample = UINT16_MAX;
readable_data_read_ptr->humidity.med60 = UINT16_MAX;
readable_data_read_ptr->humidity.med = UINT16_MAX;
readable_data_read_ptr->humidity.max = UINT16_MAX;
readable_data_read_ptr->humidity.min = UINT16_MAX;
readable_data_read_ptr->humidity.sigma = UINT16_MAX;
void reset_data(volatile readable_data_t *ptr) {
ptr->temperature.sample = UINT16_MAX;
ptr->temperature.med60 = UINT16_MAX;
ptr->temperature.med = UINT16_MAX;
ptr->temperature.max = UINT16_MAX;
ptr->temperature.min = UINT16_MAX;
ptr->temperature.sigma = UINT16_MAX;
ptr->humidity.sample = UINT16_MAX;
ptr->humidity.med60 = UINT16_MAX;
ptr->humidity.med = UINT16_MAX;
ptr->humidity.max = UINT16_MAX;
ptr->humidity.min = UINT16_MAX;
ptr->humidity.sigma = UINT16_MAX;
}

void command_task() {
Expand Down Expand Up @@ -1119,7 +1107,7 @@ void commands() {

stop_timer();
reset_samples_buffer();
reset_report_buffer();
reset_data(readable_data_write_ptr);
make_report(true);
start_timer();
}
Expand All @@ -1133,18 +1121,20 @@ void commands() {
stop_timer();
exchange_buffers();
reset_samples_buffer();
reset_report_buffer();
reset_data(readable_data_write_ptr);
make_report(true);
start_timer();
}
//! ONESHOT START
else if (configuration.is_oneshot && is_start && !is_stop) {
reset_samples_buffer();

noInterrupts();
if (!is_event_sensors_reading) {
is_event_sensors_reading = true;
ready_tasks_count++;
}
interrupts();
}
//! ONESHOT STOP
else if (configuration.is_oneshot && !is_start && is_stop) {
Expand All @@ -1169,9 +1159,11 @@ void commands() {
readable_data_write_ptr->humidity.sample = humidity;
exchange_buffers();

noInterrupts();
if (!is_event_sensors_reading) {
is_event_sensors_reading = true;
ready_tasks_count++;
}
interrupts();
}
}
10 changes: 2 additions & 8 deletions platformio/stima_v3/stima/src/stima-config.h
Original file line number Diff line number Diff line change
Expand Up @@ -387,17 +387,11 @@ WDTO_1S, WDTO_2S, WDTO_4S, WDTO_8S
*/
#define TIMER1_INTERRUPT_TIME_MS (1000)

/*!
\def TIMER1_OVERFLOW_TIME_MS
\brief Timer1 timer overflow with 1024 prescaler at 16 MHz.
*/
#define TIMER1_OVERFLOW_TIME_MS (4194)

/*!
\def TIMER1_TCNT1_VALUE
\brief Timer1 timer overflow with 1024 prescaler at 8 MHz.
\brief Timer1 timer overflow with 1024 prescaler.
*/
#define TIMER1_TCNT1_VALUE ((uint16_t)(0xFFFF - (float)(1.0 * 0xFFFF * TIMER1_INTERRUPT_TIME_MS / TIMER1_OVERFLOW_TIME_MS)))
#define TIMER1_TCNT1_VALUE (0xFFFFUL - (TIMER1_INTERRUPT_TIME_MS*1000UL/(1024 / (F_CPU/1000000)))+1)


/*********************************************************************
Expand Down
18 changes: 9 additions & 9 deletions platformio/stima_v3/stima/src/stima.ino
Original file line number Diff line number Diff line change
Expand Up @@ -536,18 +536,18 @@ void init_timer1() {
}

void start_timer() {
TCCR1A = 0x00; //!< Normal timer operation
TCCR1B = 0x05; //!< 1:1024 prescaler
TCNT1 = TIMER1_TCNT1_VALUE; //!< Pre-load timer counter register
TIFR1 |= (1 << TOV1); //!< Clear interrupt overflow flag register
TIMSK1 |= (1 << TOIE1); //!< Enable overflow interrupt
TCCR1A = 0x00; //!< Normal timer operation
TCCR1B = (1<<CS10) | (1<<CS12); //!< 1:1024 prescaler
TCNT1 = TIMER1_TCNT1_VALUE; //!< Pre-load timer counter register
TIFR1 |= (1 << TOV1); //!< Clear interrupt overflow flag register
TIMSK1 |= (1 << TOIE1); //!< Enable overflow interrupt
}

void stop_timer() {
TCCR1B = 0x00; //!< Stop
TIMSK1 &= ~(1 << TOIE1); //!< Disable overflow interrupt
TIFR1 |= (1 << TOV1); //!< Clear interrupt overflow flag register
TCNT1 = TIMER1_TCNT1_VALUE; //!< Pre-load timer counter register
TCCR1B = 0x00; //!< Stop
TIMSK1 &= ~(1 << TOIE1); //!< Disable overflow interrupt
TIFR1 |= (1 << TOV1); //!< Clear interrupt overflow flag register
TCNT1 = TIMER1_TCNT1_VALUE; //!< Pre-load timer counter register
}

/*!
Expand Down

0 comments on commit 5eaea4e

Please sign in to comment.