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

@@ -1,4 +1,11 @@
#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)
{
@@ -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);