Fixed pin mappings and task logic
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
// Arduino Libraries
|
||||
#include <Arduino.h>
|
||||
#include <DebugLog.h>
|
||||
@@ -5,67 +7,91 @@
|
||||
// ISR
|
||||
#include "isr.h"
|
||||
|
||||
const uint16_t spark_delay_us = 500;
|
||||
const auto spark_timeout_max = 1;
|
||||
|
||||
void ignitionA_task(void *pvParameters) {
|
||||
uint32_t notifiedValue;
|
||||
uint32_t pickup_flag;
|
||||
uint32_t spark_flag;
|
||||
|
||||
while (true) {
|
||||
// attende eventi
|
||||
// WAIT FOR PICKUP SIGNAL
|
||||
xTaskNotifyWait(
|
||||
0x00, // non pulire all'ingresso
|
||||
ULONG_MAX, // pulisci tutti i bit all'uscita
|
||||
¬ifiedValue, // valore ricevuto
|
||||
&pickup_flag, // valore ricevuto
|
||||
portMAX_DELAY
|
||||
);
|
||||
|
||||
uint64_t wait_time=0;
|
||||
switch (notifiedValue) {
|
||||
case TRIG_FLAG_A12P:
|
||||
case TRIG_FLAG_A12N:
|
||||
bool spark12_timeout = false;
|
||||
if (ignA_status.trig12_complete) {
|
||||
// read peak adc values from sample and hold
|
||||
} else {
|
||||
while(!digitalRead(SPARK_A12)) {
|
||||
wait_time = ignA_status.trig12_start - esp_timer_get_time();
|
||||
if (wait_time >= spark_delay_us) {
|
||||
spark12_timeout = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (spark12_timeout) { // spark did not happen, timeout
|
||||
ignA_status.trig12_complete = false;
|
||||
} else { // spark did happen
|
||||
ignA_status.trig12_complete = true;
|
||||
ignA_status.trig12_time = wait_time;
|
||||
}
|
||||
// WAIT FOR SPARK TO HAPPEN
|
||||
auto spark_timeout = xTaskNotifyWait(
|
||||
0x00, // non pulire all'ingresso
|
||||
ULONG_MAX, // pulisci tutti i bit all'uscita
|
||||
&spark_flag, // valore ricevuto
|
||||
spark_timeout_max
|
||||
);
|
||||
// Save current time to compute delay from pickup to spark
|
||||
auto curr_time = esp_timer_get_time();
|
||||
|
||||
// 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_A12P || pickup_flag == TRIG_FLAG_A12N) && spark_flag != SPARK_A12) {
|
||||
ignA_status.trig12_start = ignA_status.trig34_start = -1;
|
||||
ignA_status.trig12_end = ignA_status.trig34_end = -1;
|
||||
ignA_status.spark12_delay = ignA_status.spark34_delay = -1;
|
||||
ignA_status.soft12_engaged = ignA_status.soft34_engaged = false;
|
||||
ignA_status.spark12_status = ignA_status.spark12_status = sparkStatus::SPARK_SYNC_FAIL;
|
||||
// Save error on circular buffer and skip to next cycle //
|
||||
// [TODO]
|
||||
continue;
|
||||
}
|
||||
|
||||
switch (pickup_flag) {
|
||||
case TRIG_FLAG_A12P: {
|
||||
// Timeout not occourred, expected POSITIVE edge spark OCCOURRED
|
||||
if (spark_timeout == pdPASS) {
|
||||
ignA_status.trig12_end = curr_time;
|
||||
ignA_status.spark12_delay = ignA_status.trig12_end - ignA_status.trig12_end;
|
||||
ignA_status.soft12_engaged = false; // because spark on positive edge
|
||||
ignA_status.spark12_status = sparkStatus::SPARK_POS_OK; // do not wait for spark on negative edge
|
||||
}
|
||||
// Timeout occourred, expected POSITIVE edge spark NOT OCCOURRED
|
||||
else if (spark_timeout == pdFAIL) {
|
||||
ignA_status.spark12_status = sparkStatus::SPARK_NEG_WAIT;
|
||||
ignA_status.soft12_engaged = false;
|
||||
}
|
||||
// Do nothing more on positive pulse
|
||||
break;
|
||||
}
|
||||
case TRIG_FLAG_A12N: {
|
||||
bool expected_negative12 = ignA_status.spark12_status == sparkStatus::SPARK_NEG_WAIT;
|
||||
// Timeout not occourred, expected NEGATIVE edge spark OCCOURRED
|
||||
if (spark_timeout == pdPASS && expected_negative12) {
|
||||
ignA_status.trig12_end = curr_time;
|
||||
ignA_status.spark12_delay = ignA_status.trig12_end - ignA_status.trig12_end;
|
||||
ignA_status.soft12_engaged = true;
|
||||
ignA_status.spark12_status == sparkStatus::SPARK_NEG_OK;
|
||||
}
|
||||
// Timeout occourred, expected POSITIVE edge spark NOT OCCOURRED
|
||||
else if (spark_timeout == pdFAIL && expected_negative12) {
|
||||
ignA_status.trig12_start = 0;
|
||||
ignA_status.trig12_end = 0;
|
||||
ignA_status.soft12_engaged = false;
|
||||
ignA_status.spark12_status = sparkStatus::SPARK_NEG_FAIL;
|
||||
}
|
||||
// Timeout not occouured, unexpected negative edge spark
|
||||
else if (spark_timeout == pdPASS && !expected_negative12) {
|
||||
ignA_status.soft12_engaged = true;
|
||||
ignA_status.spark12_status = sparkStatus::SPARK_NEG_UNEXPECTED;
|
||||
}
|
||||
|
||||
// Save status on circular buffer
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case TRIG_FLAG_A34P:
|
||||
case TRIG_FLAG_A34N:
|
||||
bool spark34_timeout = false;
|
||||
if (ignA_status.trig12_complete) {
|
||||
// read peak adc values from sample and hold
|
||||
} else {
|
||||
while(!digitalRead(SPARK_A34)) {
|
||||
wait_time = ignA_status.trig34_start - esp_timer_get_time();
|
||||
if (wait_time >= spark_delay_us) {
|
||||
spark12_timeout = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (spark34_timeout) { // spark did not happen, timeout
|
||||
ignA_status.trig34_complete = false;
|
||||
} else { // spark did happen
|
||||
ignA_status.trig34_complete = true;
|
||||
ignA_status.trig34_time = wait_time;
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
LOG_ERROR("Invalid A Interrupt: ", notifiedValue);
|
||||
LOG_ERROR("Invalid A Interrupt");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user