Sync 12 and 34 working

This commit is contained in:
2026-04-04 16:27:52 +02:00
parent 0dc5d1ce79
commit b0842aadef
8 changed files with 93 additions and 41 deletions

View File

@@ -21,12 +21,12 @@ lib_deps =
;Upload protocol configuration
upload_protocol = esptool
upload_port = /dev/ttyACM1
upload_port = /dev/ttyACM2
upload_speed = 921600
;Monitor configuration
monitor_speed = 115200
monitor_port = /dev/ttyACM1
monitor_port = /dev/ttyACM2
; Build configuration
build_type = debug
@@ -39,12 +39,12 @@ lib_deps = ${env:esp32-s3-devkitc1-n16r8.lib_deps}
;Upload protocol configuration
upload_protocol = esptool
upload_port = /dev/ttyACM1
upload_port = /dev/ttyACM2
upload_speed = 921600
;Monitor configuration
monitor_speed = 115200
monitor_port = /dev/ttyACM1
monitor_port = /dev/ttyACM2
; Debug configuration
debug_tool = esp-builtin
@@ -58,4 +58,5 @@ build_flags =
-ggdb3
-DCORE_DEBUG_LEVEL=5
-DARDUINO_USB_CDC_ON_BOOT=0
-DARDUINO_USB_MODE=0
-fstack-protector-all

View File

@@ -7,12 +7,11 @@
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)
return;
// FOR TESTING ONLY
digitalWrite(POT_A_CS, HIGH);
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
isrParams *params = (isrParams *)arg;
@@ -23,37 +22,54 @@ void trig_isr(void *arg)
if (!task_handle)
return;
// reset spark flags, cannot be same time as trigger flags
box->coils12.spark_ok = false;
box->coils34.spark_ok = false;
switch (params->flag)
{
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);
}
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);
}
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
{
box->coils34.spark_ok = false;
box->coils12.spark_ok = true;
box->coils12.spark_time = time_us;
isr_firing_count = 0; // reset trigger timeout counter on spark event
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
{
box->coils12.spark_ok = false;
box->coils34.spark_ok = true;
box->coils34.spark_time = time_us;
isr_firing_count = 0; // reset trigger timeout counter on spark event
vTaskNotifyGiveFromISR(task_handle, &xHigherPriorityTaskWoken);
}
break;
default:
break;
}
// FOR TESTING ONLY
digitalWrite(POT_A_CS, LOW);
if (xHigherPriorityTaskWoken)
portYIELD_FROM_ISR();

View File

@@ -6,6 +6,7 @@
// Arduino Libraries
#include <Arduino.h>
#include "soc/gpio_struct.h"
#include <map>
#ifndef TEST
#include "pins.h"
#else
@@ -45,12 +46,31 @@ enum sparkStatus
SPARK_SYNC_FAIL,
};
static const std::map<const sparkStatus, const char *> sparkStatusNames = {
{SPARK_POS_OK, "SPARK_POS_OK"},
{SPARK_NEG_OK, "SPARK_NEG_OK"},
{SPARK_POS_SKIP, "SPARK_POS_SKIP"},
{SPARK_NEG_SKIP, "SPARK_NEG_SKIP"},
{SPARK_POS_WAIT, "SPARK_POS_WAIT"},
{SPARK_NEG_WAIT, "SPARK_NEG_WAIT"},
{SPARK_POS_FAIL, "SPARK_POS_FAIL"},
{SPARK_NEG_FAIL, "SPARK_NEG_FAIL"},
{SPARK_POS_UNEXPECTED, "SPARK_POS_UNEXPECTED"},
{SPARK_NEG_UNEXPECTED, "SPARK_NEG_UNEXPECTED"},
{SPARK_SYNC_FAIL, "SPARK_SYNC_FAIL"},
};
enum softStartStatus
{
NORMAL,
SOFT_START
};
const std::map<const softStartStatus, const char *> softStartStatusNames = {
{NORMAL, "NORMAL"},
{SOFT_START, "SOFT_START"},
};
struct coilsStatus
{
int64_t trig_time;

View File

@@ -25,7 +25,7 @@ void printTaskList()
void setup()
{
Serial.begin(115200);
Serial.begin(921600);
delay(250);
// Setup Logger
@@ -187,23 +187,23 @@ void loop()
printField("Pickup Tim", (uint32_t)ignA.coils12.trig_time, 0, 1);
printField("Spark Tim", (uint32_t)ignA.coils12.spark_time, 0, 2);
printField("Spark Dly", (uint32_t)ignA.coils12.spark_delay, 0, 3);
printField("Spark Sts", (uint32_t)ignA.coils12.spark_status, 0, 4);
printField("Spark Sts", sparkStatusNames.at(ignA.coils12.spark_status), 0, 4);
printField("Peak P_IN", ignA.coils12.peak_p_in, 0, 5);
printField("Peak P_OUT", ignA.coils12.peak_p_out, 0, 6);
printField("Peak N_IN", ignA.coils12.peak_n_in, 0, 7);
printField("Peak N_OUT", ignA.coils12.peak_n_out, 0, 8);
printField("SoftStart ", (uint32_t)ignA.coils12.sstart_status, 0, 9);
printField("SoftStart ", softStartStatusNames.at(ignA.coils12.sstart_status), 0, 9);
Serial.println("========== Coils 34 =============");
printField("Pickup Tim", (uint32_t)ignA.coils34.trig_time, 0, 11);
printField("Spark Tim", (uint32_t)ignA.coils34.spark_time, 0, 12);
printField("Spark Dly", (uint32_t)ignA.coils34.spark_delay, 0, 13);
printField("Spark Sts", (uint32_t)ignA.coils34.spark_delay, 0, 14);
printField("Spark Sts", sparkStatusNames.at(ignA.coils34.spark_status), 0, 14);
printField("Peak P_IN", ignA.coils34.peak_p_in, 0, 15);
printField("Peak P_OUT", ignA.coils34.peak_p_out, 0, 16);
printField("Peak N_IN", ignA.coils34.peak_n_in, 0, 17);
printField("Peak N_OUT", ignA.coils34.peak_n_out, 0, 18);
printField("SoftStart ", (uint32_t)ignA.coils34.sstart_status, 0, 19);
printField("SoftStart ", softStartStatusNames.at(ignA.coils34.sstart_status), 0, 19);
Serial.println("========== END =============");
count = 0;

View File

@@ -59,16 +59,16 @@
// =====================
// DIGITAL POT
// =====================
#define POT_A_CS 1
#define POT_B_CS 2
//#define POT_A_CS 1
//#define POT_B_CS 2
// =====================
// TRIGGER INPUT INTERRUPTS
// =====================
#define TRIG_PIN_A12P 18
#define TRIG_PIN_A12N 21
#define TRIG_PIN_A34P 38 // ATTENZIONEEEEEEEEEEE
#define TRIG_PIN_A34N 39 // ATTENZIONEEEEEEEEEEE
#define TRIG_PIN_A34P 1
#define TRIG_PIN_A34N 2
#define TRIG_PIN_B12P 38
#define TRIG_PIN_B12N 39
#define TRIG_PIN_B34P 40
@@ -78,9 +78,9 @@
// SPARK DETECT INPUTS
// =====================
#define SPARK_PIN_A12 42
#define SPARK_PIN_A34 42 // OK (strapping ma consentito) 45
#define SPARK_PIN_B12 42 // OK (strapping ma consentito) 46
#define SPARK_PIN_B34 42
#define SPARK_PIN_A34 45 // OK (strapping ma consentito) 45
#define SPARK_PIN_B12 46 // OK (strapping ma consentito) 46
#define SPARK_PIN_B34 47
// =====================
// PCA9555 (I2C EXPANDER)

View File

@@ -51,8 +51,6 @@ void rtIgnitionTask(void *pvParameters)
LOG_INFO("rtTask ISR Params OK");
pinMode(POT_A_CS, OUTPUT);
// 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);
@@ -73,6 +71,7 @@ void rtIgnitionTask(void *pvParameters)
// Global rt_task_ptr variables
uint32_t it = 0;
uint32_t q_fail_count = 0;
bool first_cycle = true;
while (params->rt_running)
{
@@ -86,6 +85,11 @@ void rtIgnitionTask(void *pvParameters)
&pickup_flag, // valore ricevuto
portMAX_DELAY);
if (first_cycle && pickup_flag != TRIG_FLAG_12P) // skip first cycle because of possible initial noise on pickup signals at startu
{
continue;
}
#ifdef DEBUG
Serial.print("\033[2J"); // clear screen
Serial.print("\033[H"); // cursor home
@@ -105,6 +109,7 @@ void rtIgnitionTask(void *pvParameters)
// WAIT FOR SPARK TO HAPPEN
auto spark_timeout = ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(spark_timeout_max));
//auto spark_timeout = ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
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
@@ -121,7 +126,7 @@ void rtIgnitionTask(void *pvParameters)
ulTaskNotifyValueClear(NULL, 0xFFFFFFFF);
// A trigger from pickup 12 is followed by a spark event on 34 or vice versa pickup 34 triggers spark on 12
if ((pickup_flag == TRIG_FLAG_12P || pickup_flag == TRIG_FLAG_12N) && spark_flag != SPARK_FLAG_12)
if ((pickup_flag == TRIG_FLAG_12P || pickup_flag == TRIG_FLAG_12N) && (spark_flag != SPARK_FLAG_12 && spark_flag != SPARK_FLAG_NIL))
{
ign_box_sts.coils12.spark_status = ign_box_sts.coils34.spark_status = sparkStatus::SPARK_SYNC_FAIL;
// Save error on circular buffer and skip to next cycle //
@@ -133,6 +138,7 @@ void rtIgnitionTask(void *pvParameters)
switch (pickup_flag)
{
case TRIG_FLAG_12P:
first_cycle = false;
case TRIG_FLAG_12N:
coils = &ign_box_sts.coils12;
break;
@@ -148,16 +154,15 @@ void rtIgnitionTask(void *pvParameters)
case TRIG_FLAG_12P:
case TRIG_FLAG_34P:
{
LOG_INFO("POSITIVE Edge");
// Timeout not occourred, expected POSITIVE edge spark OCCOURRED
if (spark_flag != SPARK_FLAG_NIL)
{
coils->spark_delay = coils->trig_time - coils->spark_time;
coils->spark_delay = coils->spark_time - coils->trig_time;
coils->sstart_status = softStartStatus::NORMAL; // because spark on positive edge
coils->spark_status = sparkStatus::SPARK_POS_OK; // do not wait for spark on negative edge
#ifdef DEBUG
LOG_INFO("Spark on POSITIVE pulse");
LOG_INFO("Spark Delay Timer: ", (int32_t)coils->spark_delay);
LOG_INFO("Spark Delay Time: ", (int32_t)coils->spark_delay);
#endif
}
// Timeout occourred, expected POSITIVE edge spark NOT OCCOURRED
@@ -173,17 +178,16 @@ void rtIgnitionTask(void *pvParameters)
case TRIG_FLAG_12N:
case TRIG_FLAG_34N:
{
LOG_INFO("NEGATIVE Edge");
const bool expected_negative = coils->spark_status == sparkStatus::SPARK_NEG_WAIT;
// Timeout not occourred, expected NEGATIVE edge spark OCCOURRED
if (spark_flag != SPARK_FLAG_NIL && expected_negative)
{
coils->spark_delay = coils->trig_time - coils->spark_time;
coils->spark_delay = coils->spark_time - coils->trig_time;
coils->sstart_status = softStartStatus::SOFT_START;
coils->spark_status == sparkStatus::SPARK_NEG_OK;
coils->spark_status = sparkStatus::SPARK_NEG_OK;
#ifdef DEBUG
LOG_INFO("Spark on NEGATIVE pulse");
LOG_INFO("Spark Delay Timer: ", (int32_t)coils->spark_delay);
LOG_INFO("Spark Delay Time: ", (int32_t)coils->spark_delay);
#endif
}
// Timeout occourred, expected POSITIVE edge spark NOT OCCOURRED
@@ -248,7 +252,7 @@ void rtIgnitionTask(void *pvParameters)
if (xQueueSendToBack(rt_queue, (void *)&ign_box_sts, pdMS_TO_TICKS(1)) != pdPASS)
{
q_fail_count++;
// LOG_ERROR("Failed to send to rt_queue");
LOG_ERROR("Failed to send to rt_queue");
}
}
}

View File

@@ -2,7 +2,7 @@
#define DEBUGLOG_DEFAULT_LOG_LEVEL_DEBUG
// Serial debug flag
#define DEBUG
//#define DEBUG
// Arduino Libraries
#include <Arduino.h>
@@ -16,7 +16,7 @@
#include "devices.h"
// Global Variables and Flags
const uint8_t spark_timeout_max = 2; // in milliseconds
const uint8_t spark_timeout_max = 1; // in milliseconds
// Debug Variables
#ifdef DEBUG

View File

@@ -46,3 +46,14 @@ void printField(const char name[], const float val, const uint8_t x, const uint8
Serial.print(val);
Serial.flush();
}
void printField(const char name[], const char *val, const uint8_t x, const uint8_t y) {
if (firstRun) {
setCursor(x,y);
Serial.printf("%15s: %s\n", name, val);
return;
}
setCursor(x+16, y);
Serial.print(val);
Serial.flush();
}