Thans to copilot, microsecond resolution to wait for spark.

no missing firings detected
This commit is contained in:
2026-04-04 22:12:53 +02:00
parent 38c595fd7b
commit c5d80052e5
6 changed files with 64 additions and 55 deletions

View File

@@ -29,7 +29,13 @@ monitor_speed = 115200
monitor_port = /dev/ttyACM2 monitor_port = /dev/ttyACM2
; Build configuration ; 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] [env:esp32-s3-devkitc1-n16r8-debug]
board = ${env:esp32-s3-devkitc1-n16r8.board} board = ${env:esp32-s3-devkitc1-n16r8.board}

View File

@@ -7,7 +7,6 @@
void trig_isr(void *arg) void trig_isr(void *arg)
{ {
const int64_t time_us = esp_timer_get_time(); const int64_t time_us = esp_timer_get_time();
static uint8_t isr_firing_count = 0;
// exit if invalid args // exit if invalid args
if (!arg) if (!arg)
@@ -26,47 +25,29 @@ void trig_isr(void *arg)
{ {
case TRIG_FLAG_12P: case TRIG_FLAG_12P:
case TRIG_FLAG_12N: 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
{ box->coils12.trig_time = time_us;
// only on first trigger to avoid multiple firing due to noise, to be fixed with hardware debounce xTaskNotifyFromISR(task_handle, params->flag, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
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);
}
break; break;
case TRIG_FLAG_34P: case TRIG_FLAG_34P:
case TRIG_FLAG_34N: 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
{ box->coils34.trig_time = time_us;
// only on first trigger to avoid multiple firing due to noise, to be fixed with hardware debounce xTaskNotifyFromISR(task_handle, params->flag, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
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);
}
break; break;
case SPARK_FLAG_12: 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 box->coils34.spark_ok = false;
{ box->coils12.spark_ok = true;
isr_firing_count = 0; // reset trigger timeout counter on spark event box->coils12.spark_time = time_us;
box->coils34.spark_ok = false; xTaskNotifyFromISR(task_handle, params->flag, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
box->coils12.spark_ok = true; // vTaskNotifyGiveFromISR(task_handle, &xHigherPriorityTaskWoken);
box->coils12.spark_time = time_us;
xTaskNotifyFromISR(task_handle, params->flag, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
// vTaskNotifyGiveFromISR(task_handle, &xHigherPriorityTaskWoken);
}
break; break;
case SPARK_FLAG_34: 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 box->coils12.spark_ok = false;
{ box->coils34.spark_ok = true;
isr_firing_count = 0; // reset trigger timeout counter on spark event box->coils34.spark_time = time_us;
box->coils12.spark_ok = false; xTaskNotifyFromISR(task_handle, params->flag, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
box->coils34.spark_ok = true; // vTaskNotifyGiveFromISR(task_handle, &xHigherPriorityTaskWoken);
box->coils34.spark_time = time_us;
xTaskNotifyFromISR(task_handle, params->flag, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
// vTaskNotifyGiveFromISR(task_handle, &xHigherPriorityTaskWoken);
}
break; break;
default: default:
break; break;

View File

@@ -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_NIL = (1 << 8);
static const uint32_t SPARK_FLAG_12 = (1 << 9); static const uint32_t SPARK_FLAG_12 = (1 << 9);
static const uint32_t SPARK_FLAG_34 = (1 << 10); static const uint32_t SPARK_FLAG_34 = (1 << 10);
static const uint32_t SPARK_FLAG_TIMEOUT = (1 << 11);
// Spark Status // Spark Status
enum sparkStatus enum sparkStatus

View File

@@ -12,14 +12,16 @@
#include <devices.h> #include <devices.h>
#include <ui.h> #include <ui.h>
// FreeRTOS directives
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
// #define CH_B_ENABLE // #define CH_B_ENABLE
#define TEST #define TEST
void printTaskList() void printTaskStats() {
{
char buffer[1024]; char buffer[1024];
Serial.println("Task Name\tState\tPrio\tStack\tNum"); vTaskGetRunTimeStats(buffer);
vTaskList(buffer);
Serial.println(buffer); Serial.println(buffer);
} }
@@ -174,6 +176,7 @@ void loop()
int64_t last = esp_timer_get_time(); int64_t last = esp_timer_get_time();
uint32_t missed_firings12 = 0; uint32_t missed_firings12 = 0;
uint32_t missed_firings34 = 0; uint32_t missed_firings34 = 0;
uint32_t counter = 0;
while (running) while (running)
{ {
@@ -217,9 +220,7 @@ void loop()
last = esp_timer_get_time(); last = esp_timer_get_time();
} else } else
{ {
setCursor(0, 22); Serial.println("Waiting for data... ");;
Serial.print("Waiting for data... ");;
Serial.flush();
} }
} }

View File

@@ -1,4 +1,11 @@
#include "tasks.h" #include "tasks.h"
#include <esp_timer.h>
// 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) void rtIgnitionTask(void *pvParameters)
{ {
@@ -51,6 +58,16 @@ void rtIgnitionTask(void *pvParameters)
LOG_INFO("rtTask ISR Params OK"); 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 // 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_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); 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 #endif
// WAIT FOR SPARK TO HAPPEN OR TIMEOUT // Start microsecond precision timeout timer
// auto spark_timeout = ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(spark_timeout_max)); esp_timer_stop(timeout_timer); // stop timer in case it was running from previous cycle
// if (ign_box_sts.coils12.spark_ok || ign_box_sts.coils34.spark_ok) // otherwise timeout if none is set in the ISR esp_timer_start_once(timeout_timer, spark_timeout_max);
// spark_flag = ign_box_sts.coils12.spark_ok ? SPARK_FLAG_12 : SPARK_FLAG_34;
// else
// spark_flag = SPARK_FLAG_NIL;
spark_flag = SPARK_FLAG_NIL; // default value in case of timeout, to be set by ISR if spark event occours 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 // WAIT FOR SPARK TO HAPPEN OR TIMEOUT
auto sp = xTaskNotifyWait( BaseType_t sp = pdFALSE;
sp = xTaskNotifyWait(
0x00, // non pulire all'ingresso 0x00, // non pulire all'ingresso
ULONG_MAX, // pulisci i primi 8 bit ULONG_MAX, // pulisci i primi 8 bit
&spark_flag, // valore ricevuto &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 // Handle timeout or spark event
if (sp == pdFALSE) if (spark_flag == SPARK_FLAG_TIMEOUT) {
{
spark_flag = SPARK_FLAG_NIL; spark_flag = SPARK_FLAG_NIL;
} else {
// Spark occurred, stop the timer
esp_timer_stop(timeout_timer);
} }
#ifdef DEBUG #ifdef DEBUG
@@ -271,6 +288,8 @@ void rtIgnitionTask(void *pvParameters)
} }
} }
} }
// Delete the timeout timer
esp_timer_delete(timeout_timer);
LOG_WARN("Ending realTime Task"); LOG_WARN("Ending realTime Task");
// Ignition A Interrupts DETACH // Ignition A Interrupts DETACH
detachInterrupt(rt_int.trig_pin_12p); detachInterrupt(rt_int.trig_pin_12p);

View File

@@ -16,7 +16,7 @@
#include "devices.h" #include "devices.h"
// Global Variables and Flags // Global Variables and Flags
const uint8_t spark_timeout_max = 1; // in milliseconds const uint32_t spark_timeout_max = 500; // in microseconds
// Debug Variables // Debug Variables
#ifdef DEBUG #ifdef DEBUG
@@ -27,6 +27,7 @@ static const std::map<const uint32_t, const char *> names = {
{TRIG_FLAG_34N, "TRIG_FLAG_34N"}, {TRIG_FLAG_34N, "TRIG_FLAG_34N"},
{SPARK_FLAG_12, "SPARK_FLAG_12"}, {SPARK_FLAG_12, "SPARK_FLAG_12"},
{SPARK_FLAG_34, "SPARK_FLAG_34"}, {SPARK_FLAG_34, "SPARK_FLAG_34"},
{SPARK_FLAG_TIMEOUT, "SPARK_FLAG_TIMEOUT"},
}; };
#endif #endif