diff --git a/RotaxMonitor/platformio.ini b/RotaxMonitor/platformio.ini index 88ee606..4803271 100644 --- a/RotaxMonitor/platformio.ini +++ b/RotaxMonitor/platformio.ini @@ -29,7 +29,13 @@ monitor_speed = 115200 monitor_port = /dev/ttyACM2 ; Build configuration -build_type = debug +build_type = release +build_flags = + -DARDUINO_USB_CDC_ON_BOOT=0 + -DARDUINO_USB_MODE=0 + -fstack-protector-all + -DCONFIG_FREERTOS_GENERATE_RUN_TIME_STATS=1 + -DCONFIG_FREERTOS_USE_TRACE_FACILITY=1 [env:esp32-s3-devkitc1-n16r8-debug] board = ${env:esp32-s3-devkitc1-n16r8.board} diff --git a/RotaxMonitor/src/isr.cpp b/RotaxMonitor/src/isr.cpp index 622b339..a0c84c9 100644 --- a/RotaxMonitor/src/isr.cpp +++ b/RotaxMonitor/src/isr.cpp @@ -7,7 +7,6 @@ void trig_isr(void *arg) { const int64_t time_us = esp_timer_get_time(); - static uint8_t isr_firing_count = 0; // exit if invalid args if (!arg) @@ -26,47 +25,29 @@ void trig_isr(void *arg) { case TRIG_FLAG_12P: case TRIG_FLAG_12N: - // if (isr_firing_count == 0) - { - // only on first trigger to avoid multiple firing due to noise, to be fixed with hardware debounce - isr_firing_count++; - box->coils12.spark_ok = false; // reset spark ok flag on new trigger event - box->coils12.trig_time = time_us; - xTaskNotifyFromISR(task_handle, params->flag, eSetValueWithOverwrite, &xHigherPriorityTaskWoken); - } + // only on first trigger to avoid multiple firing due to noise, to be fixed with hardware debounce + box->coils12.trig_time = time_us; + xTaskNotifyFromISR(task_handle, params->flag, eSetValueWithOverwrite, &xHigherPriorityTaskWoken); break; case TRIG_FLAG_34P: case TRIG_FLAG_34N: - // if (isr_firing_count == 0) - { - // only on first trigger to avoid multiple firing due to noise, to be fixed with hardware debounce - isr_firing_count++; - box->coils34.spark_ok = false; // reset spark ok flag on new trigger event - box->coils34.trig_time = time_us; - xTaskNotifyFromISR(task_handle, params->flag, eSetValueWithOverwrite, &xHigherPriorityTaskWoken); - } + // only on first trigger to avoid multiple firing due to noise, to be fixed with hardware debounce + box->coils34.trig_time = time_us; + xTaskNotifyFromISR(task_handle, params->flag, eSetValueWithOverwrite, &xHigherPriorityTaskWoken); break; case SPARK_FLAG_12: - // if (isr_firing_count > 0) // only consider spark if a trigger has been detected, otherwise noise on spark pin can cause false positives - { - isr_firing_count = 0; // reset trigger timeout counter on spark event - box->coils34.spark_ok = false; - box->coils12.spark_ok = true; - box->coils12.spark_time = time_us; - xTaskNotifyFromISR(task_handle, params->flag, eSetValueWithOverwrite, &xHigherPriorityTaskWoken); - // vTaskNotifyGiveFromISR(task_handle, &xHigherPriorityTaskWoken); - } + box->coils34.spark_ok = false; + box->coils12.spark_ok = true; + box->coils12.spark_time = time_us; + xTaskNotifyFromISR(task_handle, params->flag, eSetValueWithOverwrite, &xHigherPriorityTaskWoken); + // vTaskNotifyGiveFromISR(task_handle, &xHigherPriorityTaskWoken); break; case SPARK_FLAG_34: - // if (isr_firing_count > 0) // only consider spark if a trigger has been detected, otherwise noise on spark pin can cause false positives - { - isr_firing_count = 0; // reset trigger timeout counter on spark event - box->coils12.spark_ok = false; - box->coils34.spark_ok = true; - box->coils34.spark_time = time_us; - xTaskNotifyFromISR(task_handle, params->flag, eSetValueWithOverwrite, &xHigherPriorityTaskWoken); - // vTaskNotifyGiveFromISR(task_handle, &xHigherPriorityTaskWoken); - } + box->coils12.spark_ok = false; + box->coils34.spark_ok = true; + box->coils34.spark_time = time_us; + xTaskNotifyFromISR(task_handle, params->flag, eSetValueWithOverwrite, &xHigherPriorityTaskWoken); + // vTaskNotifyGiveFromISR(task_handle, &xHigherPriorityTaskWoken); break; default: break; diff --git a/RotaxMonitor/src/isr.h b/RotaxMonitor/src/isr.h index f3c38c6..0893b2c 100644 --- a/RotaxMonitor/src/isr.h +++ b/RotaxMonitor/src/isr.h @@ -29,6 +29,7 @@ static const uint32_t TRIG_FLAG_34N = (1 << 3); static const uint32_t SPARK_FLAG_NIL = (1 << 8); static const uint32_t SPARK_FLAG_12 = (1 << 9); static const uint32_t SPARK_FLAG_34 = (1 << 10); +static const uint32_t SPARK_FLAG_TIMEOUT = (1 << 11); // Spark Status enum sparkStatus diff --git a/RotaxMonitor/src/main.cpp b/RotaxMonitor/src/main.cpp index 714f204..70977b6 100644 --- a/RotaxMonitor/src/main.cpp +++ b/RotaxMonitor/src/main.cpp @@ -12,14 +12,16 @@ #include #include +// FreeRTOS directives +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" + // #define CH_B_ENABLE #define TEST -void printTaskList() -{ +void printTaskStats() { char buffer[1024]; - Serial.println("Task Name\tState\tPrio\tStack\tNum"); - vTaskList(buffer); + vTaskGetRunTimeStats(buffer); Serial.println(buffer); } @@ -174,6 +176,7 @@ void loop() int64_t last = esp_timer_get_time(); uint32_t missed_firings12 = 0; uint32_t missed_firings34 = 0; + uint32_t counter = 0; while (running) { @@ -217,9 +220,7 @@ void loop() last = esp_timer_get_time(); } else { - setCursor(0, 22); - Serial.print("Waiting for data... ");; - Serial.flush(); + Serial.println("Waiting for data... ");; } } diff --git a/RotaxMonitor/src/tasks.cpp b/RotaxMonitor/src/tasks.cpp index ba55408..6b5606a 100644 --- a/RotaxMonitor/src/tasks.cpp +++ b/RotaxMonitor/src/tasks.cpp @@ -1,4 +1,11 @@ #include "tasks.h" +#include + +// Timeout callback for microsecond precision +void spark_timeout_callback(void* arg) { + TaskHandle_t handle = (TaskHandle_t)arg; + xTaskNotify(handle, SPARK_FLAG_TIMEOUT, eSetValueWithOverwrite); +} void rtIgnitionTask(void *pvParameters) { @@ -51,6 +58,16 @@ void rtIgnitionTask(void *pvParameters) LOG_INFO("rtTask ISR Params OK"); + // Create esp_timer for microsecond precision timeout + esp_timer_handle_t timeout_timer; + esp_timer_create_args_t timer_args = { + .callback = spark_timeout_callback, + .arg = (void*)rt_handle_ptr, + .dispatch_method = ESP_TIMER_TASK, + .name = "spark_timeout" + }; + esp_timer_create(&timer_args, &timeout_timer); + // Attach Pin Interrupts attachInterruptArg(digitalPinToInterrupt(rt_int.trig_pin_12p), rt_int.isr_ptr, (void *)&isr_params_t12p, RISING); attachInterruptArg(digitalPinToInterrupt(rt_int.trig_pin_12n), rt_int.isr_ptr, (void *)&isr_params_t12n, RISING); @@ -107,25 +124,25 @@ void rtIgnitionTask(void *pvParameters) } #endif - // WAIT FOR SPARK TO HAPPEN OR TIMEOUT - // auto spark_timeout = ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(spark_timeout_max)); - // if (ign_box_sts.coils12.spark_ok || ign_box_sts.coils34.spark_ok) // otherwise timeout if none is set in the ISR - // spark_flag = ign_box_sts.coils12.spark_ok ? SPARK_FLAG_12 : SPARK_FLAG_34; - // else - // spark_flag = SPARK_FLAG_NIL; + // Start microsecond precision timeout timer + esp_timer_stop(timeout_timer); // stop timer in case it was running from previous cycle + esp_timer_start_once(timeout_timer, spark_timeout_max); spark_flag = SPARK_FLAG_NIL; // default value in case of timeout, to be set by ISR if spark event occours // WAIT FOR SPARK TO HAPPEN OR TIMEOUT - auto sp = xTaskNotifyWait( + BaseType_t sp = pdFALSE; + sp = xTaskNotifyWait( 0x00, // non pulire all'ingresso ULONG_MAX, // pulisci i primi 8 bit &spark_flag, // valore ricevuto - pdMS_TO_TICKS(spark_timeout_max)); // wait for spark event or timeout + portMAX_DELAY); // wait indefinitely, timeout handled by esp_timer - // timeout occurred, set spark flag to nil - if (sp == pdFALSE) - { + // Handle timeout or spark event + if (spark_flag == SPARK_FLAG_TIMEOUT) { spark_flag = SPARK_FLAG_NIL; + } else { + // Spark occurred, stop the timer + esp_timer_stop(timeout_timer); } #ifdef DEBUG @@ -271,6 +288,8 @@ void rtIgnitionTask(void *pvParameters) } } } + // Delete the timeout timer + esp_timer_delete(timeout_timer); LOG_WARN("Ending realTime Task"); // Ignition A Interrupts DETACH detachInterrupt(rt_int.trig_pin_12p); diff --git a/RotaxMonitor/src/tasks.h b/RotaxMonitor/src/tasks.h index 1fde989..c1196eb 100644 --- a/RotaxMonitor/src/tasks.h +++ b/RotaxMonitor/src/tasks.h @@ -16,7 +16,7 @@ #include "devices.h" // Global Variables and Flags -const uint8_t spark_timeout_max = 1; // in milliseconds +const uint32_t spark_timeout_max = 500; // in microseconds // Debug Variables #ifdef DEBUG @@ -27,6 +27,7 @@ static const std::map names = { {TRIG_FLAG_34N, "TRIG_FLAG_34N"}, {SPARK_FLAG_12, "SPARK_FLAG_12"}, {SPARK_FLAG_34, "SPARK_FLAG_34"}, + {SPARK_FLAG_TIMEOUT, "SPARK_FLAG_TIMEOUT"}, }; #endif