Tasks, ISR, Pin refactoring and renaming
This commit is contained in:
@@ -6,26 +6,17 @@
|
||||
#include <Adafruit_SSD1306.h>
|
||||
#include <PCA95x5.h>
|
||||
|
||||
// ADC Channel mapping Ignition A
|
||||
#define IN_A12_P SING_0
|
||||
#define IN_A12_N SING_1
|
||||
#define IN_A34_P SING_2
|
||||
#define IN_A34_N SING_3
|
||||
#define OUT_A12_P SING_4
|
||||
#define OUT_A12_N SING_5
|
||||
#define OUT_A34_P SING_6
|
||||
#define OUT_A34_N SING_7
|
||||
|
||||
// ADC Channel mapping Ignition B
|
||||
#define IN_A12_P SING_0
|
||||
#define IN_A12_N SING_1
|
||||
#define IN_A34_P SING_2
|
||||
#define IN_A34_N SING_3
|
||||
#define OUT_A12_P SING_4
|
||||
#define OUT_A12_N SING_5
|
||||
#define OUT_A34_P SING_6
|
||||
#define OUT_A34_N SING_7
|
||||
// ADC Channel mapping
|
||||
#define ADC_CH_PEAK_12P_IN SING_0
|
||||
#define ADC_CH_PEAK_12N_IN SING_1
|
||||
#define ADC_CH_PEAK_34P_IN SING_2
|
||||
#define ADC_CH_PEAK_34N_IN SING_3
|
||||
#define ADC_CH_PEAK_12P_OUT SING_4
|
||||
#define ADC_CH_PEAK_12N_OUT SING_5
|
||||
#define ADC_CH_PEAK_34P_OUT SING_6
|
||||
#define ADC_CH_PEAK_34N_OUT SING_7
|
||||
|
||||
// Device Pointer structs for tasks
|
||||
struct Devices {
|
||||
AD5292 *pot_a = NULL, *pot_b = NULL;
|
||||
ADS1256 *adc_a = NULL, *adc_b = NULL;
|
||||
@@ -33,6 +24,7 @@ struct Devices {
|
||||
PCA9555* io = NULL;
|
||||
};
|
||||
|
||||
// Adc read channel wrapper to selet mux before reading
|
||||
inline float adcReadChannel(ADS1256* adc, const uint8_t ch){
|
||||
adc->setMUX(ch);
|
||||
// scarta 3 conversioni
|
||||
|
||||
@@ -66,6 +66,7 @@ struct coilsStatus
|
||||
float peak_p_in, peak_n_in;
|
||||
float peak_p_out, peak_n_out;
|
||||
float trigger_spark;
|
||||
bool spark_ok;
|
||||
};
|
||||
|
||||
// Task internal Status
|
||||
@@ -77,40 +78,13 @@ struct ignitionBoxStatus
|
||||
coilsStatus coils34;
|
||||
// voltage from generator
|
||||
float volts_gen = 0.0;
|
||||
// spark flags
|
||||
bool spark12 = false;
|
||||
bool spark34 = false;
|
||||
};
|
||||
|
||||
ignitionBoxStatus ignA_status;
|
||||
ignitionBoxStatus ignB_status;
|
||||
|
||||
// Pin to flag Map
|
||||
static uint32_t pin2trig[49] = {0};
|
||||
void initTriggerPinMapping()
|
||||
{
|
||||
pin2trig[TRIG_A12P] = TRIG_FLAG_12P;
|
||||
pin2trig[TRIG_A12N] = TRIG_FLAG_12N;
|
||||
pin2trig[TRIG_A34P] = TRIG_FLAG_34P;
|
||||
pin2trig[TRIG_A34N] = TRIG_FLAG_34N;
|
||||
#ifndef TEST
|
||||
pin2trig[TRIG_B12P] = TRIG_FLAG_12P;
|
||||
pin2trig[TRIG_B12N] = TRIG_FLAG_12N;
|
||||
pin2trig[TRIG_B34P] = TRIG_FLAG_34P;
|
||||
pin2trig[TRIG_B34N] = TRIG_FLAG_34N;
|
||||
#endif
|
||||
struct isrParams {
|
||||
const uint32_t flag;
|
||||
ignitionBoxStatus* ign_stat;
|
||||
};
|
||||
|
||||
static uint32_t pin2spark[49] = {0};
|
||||
void initSparkPinMapping()
|
||||
{
|
||||
pin2spark[SPARK_A12] = SPARK_FLAG_12;
|
||||
pin2spark[SPARK_A34] = SPARK_FLAG_34;
|
||||
#ifndef TEST
|
||||
pin2spark[SPARK_B12] = SPARK_FLAG_12;
|
||||
pin2spark[SPARK_B34] = SPARK_FLAG_34;
|
||||
#endif
|
||||
};
|
||||
|
||||
// =====================
|
||||
// ISR (Pass return bitmask to ISR management function)
|
||||
@@ -118,37 +92,38 @@ void initSparkPinMapping()
|
||||
// =====================
|
||||
void IRAM_ATTR trig_isr_a(void *arg)
|
||||
{
|
||||
volatile const int64_t time_us = esp_timer_get_time();
|
||||
const int64_t time_us = esp_timer_get_time();
|
||||
|
||||
// exit if task is not running
|
||||
if (!trigA_TaskHandle)
|
||||
if (!trigA_TaskHandle || !arg)
|
||||
return;
|
||||
|
||||
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
||||
volatile const uint32_t flag = (uint32_t)arg;
|
||||
isrParams *params = (isrParams*)arg;
|
||||
ignitionBoxStatus *box = box;
|
||||
|
||||
// reset spark flags, cannot be same time as trigger flags
|
||||
ignA_status.spark12 = false;
|
||||
ignA_status.spark34 = false;
|
||||
box->coils12.spark_ok = false;
|
||||
box->coils34.spark_ok = false;
|
||||
|
||||
switch (flag)
|
||||
switch (params->flag)
|
||||
{
|
||||
case TRIG_FLAG_12P:
|
||||
ignA_status.coils12.trig_time = time_us;
|
||||
xTaskNotifyFromISR(trigA_TaskHandle, flag, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
box->coils12.trig_time = time_us;
|
||||
xTaskNotifyFromISR(trigA_TaskHandle, params->flag, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
break;
|
||||
case TRIG_FLAG_34P:
|
||||
ignA_status.coils34.trig_time = time_us;
|
||||
xTaskNotifyFromISR(trigA_TaskHandle, flag, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
box->coils34.trig_time = time_us;
|
||||
xTaskNotifyFromISR(trigA_TaskHandle, params->flag, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
break;
|
||||
case SPARK_FLAG_12:
|
||||
ignA_status.spark12 = true;
|
||||
ignA_status.coils12.spark_time = time_us;
|
||||
box->coils12.spark_ok = true;
|
||||
box->coils12.spark_time = time_us;
|
||||
vTaskNotifyGiveFromISR(trigA_TaskHandle, &xHigherPriorityTaskWoken);
|
||||
break;
|
||||
case SPARK_FLAG_34:
|
||||
ignA_status.spark34 = true;
|
||||
ignA_status.coils34.spark_time = time_us;
|
||||
box->coils34.spark_ok = true;
|
||||
box->coils34.spark_time = time_us;
|
||||
vTaskNotifyGiveFromISR(trigA_TaskHandle, &xHigherPriorityTaskWoken);
|
||||
break;
|
||||
default:
|
||||
|
||||
@@ -11,13 +11,11 @@
|
||||
#include <channels.h>
|
||||
#include <devices.h>
|
||||
|
||||
void printTaskList() {
|
||||
void printTaskList()
|
||||
{
|
||||
char buffer[1024];
|
||||
|
||||
Serial.println("Task Name\tState\tPrio\tStack\tNum");
|
||||
|
||||
vTaskList(buffer);
|
||||
|
||||
Serial.println(buffer);
|
||||
}
|
||||
|
||||
@@ -32,7 +30,8 @@ void setup()
|
||||
|
||||
// Print Processor Info
|
||||
LOG_INFO("ESP32 Chip:", ESP.getChipModel());
|
||||
if (psramFound()){
|
||||
if (psramFound())
|
||||
{
|
||||
LOG_INFO("ESP32 PSram Found");
|
||||
LOG_INFO("ESP32 PSram:", ESP.getPsramSize());
|
||||
psramInit();
|
||||
@@ -43,51 +42,96 @@ void setup()
|
||||
|
||||
// Initialize Interrupt pins on PICKUP detectors
|
||||
initTriggerPinsInputs();
|
||||
initTriggerPinMapping();
|
||||
// Initialize Interrupt pins on SPARK detectors
|
||||
initSparkPinInputs();
|
||||
initSparkPinMapping();
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
// global variables
|
||||
bool running = true;
|
||||
rtTaskParams taskA_params, taskB_params;
|
||||
Devices dev;
|
||||
QueueHandle_t rt_taskA_queue = xQueueCreate(10, sizeof(ignitionBoxStatus));
|
||||
rtTaskParams taskA_params {
|
||||
.rt_running = true,
|
||||
.dev = &dev,
|
||||
.rt_queue = rt_taskA_queue,
|
||||
.rt_int = rtTaskInterrupts{
|
||||
.isr_ptr = trig_isr_a,
|
||||
.trig_pin_12p = TRIG_PIN_A12P,
|
||||
.trig_pin_12n = TRIG_PIN_A12N,
|
||||
.trig_pin_34p = TRIG_PIN_A34P,
|
||||
.trig_pin_34n = TRIG_PIN_A34N,
|
||||
.spark_pin_12 = SPARK_PIN_A12,
|
||||
.spark_pin_34 = SPARK_PIN_A34},
|
||||
.rt_resets = rtTaskResets{
|
||||
.rst_io_12p = RST_EXT_A12P,
|
||||
.rst_io_12n = RST_EXT_A12N,
|
||||
.rst_io_34p = RST_EXT_A34P,
|
||||
.rst_io_34n = RST_EXT_A34N}
|
||||
};
|
||||
#ifndef TEST
|
||||
QueueHandle_t rt_taskB_queue = xQueueCreate(10, sizeof(ignitionBoxStatus));
|
||||
rtTaskParams taskB_params {
|
||||
.rt_running = true,
|
||||
.dev = &dev,
|
||||
.rt_queue = rt_taskB_queue,
|
||||
.rt_int = rtTaskInterrupts{
|
||||
.isr_ptr = trig_isr_a,
|
||||
.trig_pin_12p = TRIG_PIN_B12P,
|
||||
.trig_pin_12n = TRIG_PIN_B12N,
|
||||
.trig_pin_34p = TRIG_PIN_B34P,
|
||||
.trig_pin_34n = TRIG_PIN_B34N,
|
||||
.spark_pin_12 = SPARK_PIN_B12,
|
||||
.spark_pin_34 = SPARK_PIN_B34},
|
||||
.rt_resets = rtTaskResets{
|
||||
.rst_io_12p = RST_EXT_B12P,
|
||||
.rst_io_12n = RST_EXT_B12N,
|
||||
.rst_io_34p = RST_EXT_B34P,
|
||||
.rst_io_34n = RST_EXT_B34N}
|
||||
};
|
||||
#endif
|
||||
|
||||
#ifndef TEST
|
||||
// Init 2 SPI interfaces
|
||||
// Init 2 SPI interfaces
|
||||
bool spiA_ok = true;
|
||||
SPIClass SPI_A(FSPI);
|
||||
spiA_ok = SPI_A.begin(SPI_A_SCK, SPI_A_MISO, SPI_A_MOSI);
|
||||
|
||||
bool spiB_ok = true;
|
||||
#ifndef TEST
|
||||
SPIClass SPI_B(HSPI);
|
||||
if (!SPI_A.begin(SPI_A_SCK, SPI_A_MISO, SPI_A_MOSI) || !SPI_B.begin(SPI_A_SCK, SPI_A_MISO, SPI_A_MOSI)) {
|
||||
spiB_ok = SPI_B.begin(SPI_B_SCK, SPI_B_MISO, SPI_B_MOSI);
|
||||
#endif
|
||||
if (!spiA_ok || !spiB_ok)
|
||||
{
|
||||
LOG_ERROR("Unable to Initialize SPI Busses");
|
||||
LOG_ERROR("5 seconds to restart...");
|
||||
vTaskDelay(pdMS_TO_TICKS(5000));
|
||||
esp_restart();
|
||||
}
|
||||
#endif
|
||||
|
||||
pinMode(POT_A_CS, OUTPUT); // Temporary!
|
||||
pinMode(POT_B_CS, OUTPUT); // Temporary!
|
||||
LOG_INFO("Init SPI [OK]");
|
||||
|
||||
// Init ADC_A
|
||||
// dev.adc_a = new ADS1256(ADC_A_DRDY, ADC_A_RST, ADC_A_SYNC, ADC_A_CS, 2.5, &SPI_A);
|
||||
// dev.adc_a->InitializeADC();
|
||||
// dev.adc_a->setPGA(PGA_1);
|
||||
// dev.adc_a->setDRATE(DRATE_1000SPS);
|
||||
|
||||
dev.adc_a = new ADS1256(ADC_A_DRDY, ADC_A_RST, ADC_A_SYNC, ADC_A_CS, 2.5, &SPI_A);
|
||||
dev.adc_a->InitializeADC();
|
||||
dev.adc_a->setPGA(PGA_1);
|
||||
dev.adc_a->setDRATE(DRATE_1000SPS);
|
||||
|
||||
#ifndef TEST
|
||||
// Init ADC_B
|
||||
// dev.adc_a = new ADS1256(ADC_B_DRDY, ADC_B_RST, ADC_B_SYNC, ADC_B_CS, 2.5, &SPI_B);
|
||||
// dev.adc_a->InitializeADC();
|
||||
// dev.adc_a->setPGA(PGA_1);
|
||||
// dev.adc_a->setDRATE(DRATE_1000SPS);
|
||||
dev.adc_a = new ADS1256(ADC_B_DRDY, ADC_B_RST, ADC_B_SYNC, ADC_B_CS, 2.5, &SPI_B);
|
||||
dev.adc_a->InitializeADC();
|
||||
dev.adc_a->setPGA(PGA_1);
|
||||
dev.adc_a->setDRATE(DRATE_1000SPS);
|
||||
#endif
|
||||
|
||||
LOG_INFO("Init ADC [OK]");
|
||||
|
||||
// Ignition A on Core 0
|
||||
auto ignA_task_success = xTaskCreatePinnedToCore(
|
||||
ignitionA_task,
|
||||
"ignitionA_task",
|
||||
auto ignA_task_success = pdPASS;
|
||||
ignA_task_success = xTaskCreatePinnedToCore(
|
||||
rtIgnitionTask,
|
||||
"rtIgnitionTask_boxA",
|
||||
TASK_STACK,
|
||||
(void *)&taskA_params,
|
||||
TASK_PRIORITY,
|
||||
@@ -96,14 +140,16 @@ void loop()
|
||||
|
||||
// Ignition B on Core 1
|
||||
auto ignB_task_success = pdPASS;
|
||||
// auto ignB_task_success = xTaskCreatePinnedToCore(
|
||||
// ignitionB_task,
|
||||
// "ignitionB_task",
|
||||
// TASK_STACK,
|
||||
// (void *)&taskB_params,
|
||||
// TASK_PRIORITY, // priorità leggermente più alta
|
||||
// &trigA_TaskHandle,
|
||||
// CORE_1);
|
||||
#ifndef TEST
|
||||
ignB_task_success = xTaskCreatePinnedToCore(
|
||||
rtIgnitionTask,
|
||||
"rtIgnitionTask_boxA",
|
||||
TASK_STACK,
|
||||
(void *)&taskB_params,
|
||||
TASK_PRIORITY, // priorità leggermente più alta
|
||||
&trigB_TaskHandle,
|
||||
CORE_1);
|
||||
#endif
|
||||
|
||||
if ((ignA_task_success && ignB_task_success) != pdPASS)
|
||||
{
|
||||
@@ -111,14 +157,15 @@ void loop()
|
||||
LOG_ERROR("5 seconds to restart...");
|
||||
vTaskDelay(pdMS_TO_TICKS(5000));
|
||||
esp_restart();
|
||||
}
|
||||
LOG_INFO("Real Time Tasks A&B initialized");
|
||||
}
|
||||
|
||||
LOG_INFO("Real Time Tasks A & B initialized");
|
||||
|
||||
////////////////////// MAIN LOOP //////////////////////
|
||||
uint32_t count(0);
|
||||
while (running)
|
||||
{
|
||||
//printTaskList();
|
||||
// printTaskList();
|
||||
delay(10000);
|
||||
}
|
||||
|
||||
|
||||
@@ -65,40 +65,40 @@
|
||||
// =====================
|
||||
// TRIGGER INPUT INTERRUPTS
|
||||
// =====================
|
||||
#define TRIG_A12P 18
|
||||
#define TRIG_A12N 21
|
||||
#define TRIG_A34P 33
|
||||
#define TRIG_A34N 34
|
||||
#define TRIG_B12P 38
|
||||
#define TRIG_B12N 39
|
||||
#define TRIG_B34P 40
|
||||
#define TRIG_B34N 41
|
||||
#define TRIG_PIN_A12P 18
|
||||
#define TRIG_PIN_A12N 21
|
||||
#define TRIG_PIN_A34P 33
|
||||
#define TRIG_PIN_A34N 34
|
||||
#define TRIG_PIN_B12P 38
|
||||
#define TRIG_PIN_B12N 39
|
||||
#define TRIG_PIN_B34P 40
|
||||
#define TRIG_PIN_B34N 41
|
||||
|
||||
// =====================
|
||||
// SPARK DETECT INPUTS
|
||||
// =====================
|
||||
#define SPARK_A12 42
|
||||
#define SPARK_A34 45 // OK (strapping ma consentito)
|
||||
#define SPARK_B12 46 // OK (strapping ma consentito)
|
||||
#define SPARK_B34 47
|
||||
#define SPARK_PIN_A12 42
|
||||
#define SPARK_PIN_A34 45 // OK (strapping ma consentito)
|
||||
#define SPARK_PIN_B12 46 // OK (strapping ma consentito)
|
||||
#define SPARK_PIN_B34 47
|
||||
|
||||
// =====================
|
||||
// PCA9555 (I2C EXPANDER)
|
||||
// =====================
|
||||
|
||||
// --- RESET LINES ---
|
||||
#define RST_A12P 0
|
||||
#define RST_A12N 1
|
||||
#define RST_A34P 2
|
||||
#define RST_A34N 3
|
||||
#define RST_B12P 4
|
||||
#define RST_B12N 5
|
||||
#define RST_B34P 6
|
||||
#define RST_B34N 7
|
||||
#define RST_EXT_A12P 0
|
||||
#define RST_EXT_A12N 1
|
||||
#define RST_EXT_A34P 2
|
||||
#define RST_EXT_A34N 3
|
||||
#define RST_EXT_B12P 4
|
||||
#define RST_EXT_B12N 5
|
||||
#define RST_EXT_B34P 6
|
||||
#define RST_EXT_B34N 7
|
||||
|
||||
// --- RELAY ---
|
||||
#define A_RELAY 8
|
||||
#define B_RELAY 9
|
||||
#define A_EXT_RELAY 8
|
||||
#define B_EXT_RELAY 9
|
||||
|
||||
// --- STATUS / BUTTON ---
|
||||
#define BTN_3 10
|
||||
@@ -111,20 +111,20 @@
|
||||
// Init Pin Functions
|
||||
inline void initTriggerPinsInputs()
|
||||
{
|
||||
pinMode(TRIG_A12P, INPUT);
|
||||
pinMode(TRIG_A12N, INPUT);
|
||||
pinMode(TRIG_A34P, INPUT);
|
||||
pinMode(TRIG_A34N, INPUT);
|
||||
pinMode(TRIG_B12P, INPUT);
|
||||
pinMode(TRIG_B12N, INPUT);
|
||||
pinMode(TRIG_B34P, INPUT);
|
||||
pinMode(TRIG_B34N, INPUT);
|
||||
pinMode(TRIG_PIN_A12P, INPUT_PULLDOWN);
|
||||
pinMode(TRIG_PIN_A12N, INPUT_PULLDOWN);
|
||||
pinMode(TRIG_PIN_A34P, INPUT_PULLDOWN);
|
||||
pinMode(TRIG_PIN_A34N, INPUT_PULLDOWN);
|
||||
pinMode(TRIG_PIN_B12P, INPUT_PULLDOWN);
|
||||
pinMode(TRIG_PIN_B12N, INPUT_PULLDOWN);
|
||||
pinMode(TRIG_PIN_B34P, INPUT_PULLDOWN);
|
||||
pinMode(TRIG_PIN_B34N, INPUT_PULLDOWN);
|
||||
}
|
||||
|
||||
inline void initSparkPinInputs()
|
||||
{
|
||||
pinMode(SPARK_A12, INPUT);
|
||||
pinMode(SPARK_A34, INPUT);
|
||||
pinMode(SPARK_B12, INPUT);
|
||||
pinMode(SPARK_B34, INPUT);
|
||||
pinMode(SPARK_PIN_A12, INPUT_PULLDOWN);
|
||||
pinMode(SPARK_PIN_A34, INPUT_PULLDOWN);
|
||||
pinMode(SPARK_PIN_B12, INPUT_PULLDOWN);
|
||||
pinMode(SPARK_PIN_B34, INPUT_PULLDOWN);
|
||||
}
|
||||
|
||||
@@ -42,30 +42,43 @@
|
||||
// =====================
|
||||
// TRIGGER INPUT INTERRUPTS
|
||||
// =====================
|
||||
#define TRIG_A12P 35
|
||||
#define TRIG_A12N 32
|
||||
#define TRIG_A34P 39
|
||||
#define TRIG_A34N 36
|
||||
#define TRIG_PIN_A12P 35
|
||||
#define TRIG_PIN_A12N 32
|
||||
#define TRIG_PIN_A34P 39
|
||||
#define TRIG_PIN_A34N 36
|
||||
|
||||
|
||||
// =====================
|
||||
// SPARK DETECT INTERRUPTS
|
||||
// =====================
|
||||
#define SPARK_A12 4
|
||||
#define SPARK_A34 2
|
||||
#define SPARK_PIN_A12 4
|
||||
#define SPARK_PIN_A34 2
|
||||
|
||||
// =====================
|
||||
// PCA9555 (I2C EXPANDER)
|
||||
// =====================
|
||||
|
||||
// --- RESET LINES ---
|
||||
#define RST_EXT_A12P 0
|
||||
#define RST_EXT_A12N 1
|
||||
#define RST_EXT_A34P 2
|
||||
#define RST_EXT_A34N 3
|
||||
|
||||
// --- RELAY ---
|
||||
#define A_EXT_RELAY 8
|
||||
|
||||
|
||||
// Init Pin Functions
|
||||
inline void initTriggerPinsInputs()
|
||||
{
|
||||
pinMode(TRIG_A12P, INPUT_PULLDOWN);
|
||||
pinMode(TRIG_A12N, INPUT_PULLDOWN);
|
||||
pinMode(TRIG_A34P, INPUT_PULLDOWN);
|
||||
pinMode(TRIG_A34N, INPUT_PULLDOWN);
|
||||
pinMode(TRIG_PIN_A12P, INPUT_PULLDOWN);
|
||||
pinMode(TRIG_PIN_A12N, INPUT_PULLDOWN);
|
||||
pinMode(TRIG_PIN_A34P, INPUT_PULLDOWN);
|
||||
pinMode(TRIG_PIN_A34N, INPUT_PULLDOWN);
|
||||
}
|
||||
|
||||
inline void initSparkPinInputs()
|
||||
{
|
||||
pinMode(SPARK_A12, INPUT_PULLDOWN);
|
||||
pinMode(SPARK_A34, INPUT_PULLDOWN);
|
||||
pinMode(SPARK_PIN_A12, INPUT_PULLDOWN);
|
||||
pinMode(SPARK_PIN_A34, INPUT_PULLDOWN);
|
||||
}
|
||||
@@ -15,8 +15,7 @@
|
||||
#include "devices.h"
|
||||
|
||||
// Global Variables and Flags
|
||||
static bool rt_task_running = true;
|
||||
const auto spark_timeout_max = 2; // in milliseconds
|
||||
const uint8_t spark_timeout_max = 2; // in milliseconds
|
||||
|
||||
// Debug Variables
|
||||
#ifdef DEBUG
|
||||
@@ -51,36 +50,20 @@ struct rtTaskResets
|
||||
const uint8_t rst_io_34n;
|
||||
};
|
||||
|
||||
// RT Task Adc channels
|
||||
struct rtTaskAdChannels
|
||||
{
|
||||
const uint8_t adc_gen;
|
||||
const uint8_t adc_spark_12;
|
||||
const uint8_t adc_spark_34;
|
||||
const uint8_t adc_peak_12p_in;
|
||||
const uint8_t adc_peak_12n_in;
|
||||
const uint8_t adc_peak_34p_in;
|
||||
const uint8_t adc_peak_34n_in;
|
||||
const uint8_t adc_peak_12p_out;
|
||||
const uint8_t adc_peak_12n_out;
|
||||
const uint8_t adc_peak_34p_out;
|
||||
const uint8_t adc_peak_34n_out;
|
||||
};
|
||||
|
||||
// RT task parameters
|
||||
struct rtTaskParams
|
||||
{
|
||||
bool rt_running; // run flag, false to terminate
|
||||
Devices *dev;
|
||||
QueueHandle_t rt_queue;
|
||||
const rtTaskInterrupts rt_int; // interrupt pins to attach
|
||||
const rtTaskResets rt_resets; // reset ping for peak detectors
|
||||
const rtTaskAdChannels rt_adcch; // adc channels
|
||||
const QueueHandle_t rt_queue;
|
||||
const rtTaskInterrupts rt_int; // interrupt pins to attach
|
||||
const rtTaskResets rt_resets; // reset ping for peak detectors
|
||||
};
|
||||
|
||||
void ignitionA_task(void *pvParameters)
|
||||
void rtIgnitionTask(void *pvParameters)
|
||||
{
|
||||
|
||||
// Invalid real time task parameters, exit immediate
|
||||
if (!pvParameters)
|
||||
{
|
||||
LOG_ERROR("Null task parameters");
|
||||
@@ -88,22 +71,42 @@ void ignitionA_task(void *pvParameters)
|
||||
}
|
||||
|
||||
// Task Parameters and Devices
|
||||
const rtTaskParams *params = (const rtTaskParams *)pvParameters;
|
||||
const rtTaskInterrupts rt_int = params->rt_int; // copy to avoid external override
|
||||
const rtTaskResets rt_rst = params->rt_resets; // copy to avoid external override
|
||||
const rtTaskAdChannels rt_adcch = params->rt_adcch; // copy to avoid external override
|
||||
rtTaskParams *params = (rtTaskParams *)pvParameters;
|
||||
const rtTaskInterrupts rt_int = params->rt_int; // copy to avoid external override
|
||||
const rtTaskResets rt_rst = params->rt_resets; // copy to avoid external override
|
||||
QueueHandle_t queue = params->rt_queue;
|
||||
Devices *dev = params->dev;
|
||||
ADS1256 *adc = dev->adc_a;
|
||||
PCA9555 *io = dev->io;
|
||||
|
||||
// Variables for ISR, static to be fixed in memory locations
|
||||
static ignitionBoxStatus ign_box_sts; // common for all ISR calls
|
||||
static isrParams isr_params_t12p{ // only call flag changes
|
||||
.flag = TRIG_FLAG_12P,
|
||||
.ign_stat = &ign_box_sts};
|
||||
static isrParams isr_params_t12n{
|
||||
.flag = TRIG_FLAG_12N,
|
||||
.ign_stat = &ign_box_sts};
|
||||
static isrParams isr_params_t34p{
|
||||
.flag = TRIG_FLAG_34P,
|
||||
.ign_stat = &ign_box_sts};
|
||||
static isrParams isr_params_t34n{
|
||||
.flag = TRIG_FLAG_34N,
|
||||
.ign_stat = &ign_box_sts};
|
||||
static isrParams isr_params_sp12{
|
||||
.flag = SPARK_FLAG_12,
|
||||
.ign_stat = &ign_box_sts};
|
||||
static isrParams isr_params_sp34{
|
||||
.flag = SPARK_FLAG_34,
|
||||
.ign_stat = &ign_box_sts};
|
||||
|
||||
// Attach Pin Interrupts
|
||||
attachInterruptArg(rt_int.trig_pin_12p, rt_int.isr_ptr, (void *)TRIG_FLAG_12P, RISING);
|
||||
attachInterruptArg(rt_int.trig_pin_12n, rt_int.isr_ptr, (void *)TRIG_FLAG_12N, RISING);
|
||||
attachInterruptArg(rt_int.trig_pin_34p, rt_int.isr_ptr, (void *)TRIG_FLAG_34P, RISING);
|
||||
attachInterruptArg(rt_int.trig_pin_34n, rt_int.isr_ptr, (void *)TRIG_FLAG_34N, RISING);
|
||||
attachInterruptArg(rt_int.spark_pin_12, rt_int.isr_ptr, (void *)SPARK_FLAG_12, RISING);
|
||||
attachInterruptArg(rt_int.spark_pin_34, rt_int.isr_ptr, (void *)SPARK_FLAG_34, RISING);
|
||||
attachInterruptArg(rt_int.trig_pin_12p, rt_int.isr_ptr, (void *)&isr_params_t12p, RISING);
|
||||
attachInterruptArg(rt_int.trig_pin_12n, rt_int.isr_ptr, (void *)&isr_params_t12n, RISING);
|
||||
attachInterruptArg(rt_int.trig_pin_34p, rt_int.isr_ptr, (void *)&isr_params_t34p, RISING);
|
||||
attachInterruptArg(rt_int.trig_pin_34n, rt_int.isr_ptr, (void *)&isr_params_t34n, RISING);
|
||||
attachInterruptArg(rt_int.spark_pin_12, rt_int.isr_ptr, (void *)&isr_params_sp12, RISING);
|
||||
attachInterruptArg(rt_int.spark_pin_34, rt_int.isr_ptr, (void *)&isr_params_sp34, RISING);
|
||||
|
||||
// Compute Reset Pin Bitmask
|
||||
const uint16_t rst_bitmask = (1 << rt_rst.rst_io_12p) |
|
||||
@@ -113,7 +116,7 @@ void ignitionA_task(void *pvParameters)
|
||||
|
||||
uint32_t it = 0;
|
||||
uint32_t q_fail_count = 0;
|
||||
while (rt_task_running)
|
||||
while (params->rt_running)
|
||||
{
|
||||
// Global task variables
|
||||
uint32_t pickup_flag = 0;
|
||||
@@ -147,8 +150,8 @@ void ignitionA_task(void *pvParameters)
|
||||
|
||||
// WAIT FOR SPARK TO HAPPEN
|
||||
auto spark_timeout = ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(spark_timeout_max));
|
||||
if (ignA_status.spark12 || ignA_status.spark34) // otherwise timeout if none is set in the ISR
|
||||
spark_flag = ignA_status.spark12 ? SPARK_FLAG_12 : SPARK_FLAG_34;
|
||||
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;
|
||||
|
||||
xTaskNotifyStateClear(NULL);
|
||||
ulTaskNotifyValueClear(NULL, 0xFFFFFFFF);
|
||||
@@ -163,22 +166,22 @@ void ignitionA_task(void *pvParameters)
|
||||
// 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)
|
||||
{
|
||||
ignA_status.coils12.spark_status = ignA_status.coils34.spark_status = sparkStatus::SPARK_SYNC_FAIL;
|
||||
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 //
|
||||
LOG_ERROR("Spark Mismatch");
|
||||
continue;
|
||||
}
|
||||
|
||||
coilsStatus *c;
|
||||
coilsStatus *coils;
|
||||
switch (pickup_flag)
|
||||
{
|
||||
case TRIG_FLAG_12P:
|
||||
case TRIG_FLAG_12N:
|
||||
c = &ignA_status.coils12;
|
||||
coils = &ign_box_sts.coils12;
|
||||
break;
|
||||
case TRIG_FLAG_34P:
|
||||
case TRIG_FLAG_34N:
|
||||
c = &ignA_status.coils34;
|
||||
coils = &ign_box_sts.coils34;
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -192,19 +195,19 @@ void ignitionA_task(void *pvParameters)
|
||||
// Timeout not occourred, expected POSITIVE edge spark OCCOURRED
|
||||
if (spark_timeout == pdPASS)
|
||||
{
|
||||
c->spark_delay = c->spark_time - c->trig_time;
|
||||
c->sstart_status = softStartStatus::NORMAL; // because spark on positive edge
|
||||
c->spark_status = sparkStatus::SPARK_POS_OK; // do not wait for spark on negative edge
|
||||
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("Trigger Spark POSITIVE");
|
||||
LOG_INFO("Spark12 Delay Timer: ", (int)c->spark_delay);
|
||||
LOG_INFO("Spark12 Delay Timer: ", (int)coils->spark_delay);
|
||||
#endif
|
||||
}
|
||||
// Timeout occourred, expected POSITIVE edge spark NOT OCCOURRED
|
||||
else if (spark_timeout == pdFAIL)
|
||||
{
|
||||
c->spark_status = sparkStatus::SPARK_NEG_WAIT;
|
||||
c->sstart_status = softStartStatus::NORMAL;
|
||||
coils->spark_status = sparkStatus::SPARK_NEG_WAIT;
|
||||
coils->sstart_status = softStartStatus::NORMAL;
|
||||
}
|
||||
new_data = false;
|
||||
break; // Do nothing more on positive pulse
|
||||
@@ -213,29 +216,29 @@ void ignitionA_task(void *pvParameters)
|
||||
case TRIG_FLAG_12N:
|
||||
case TRIG_FLAG_34N:
|
||||
{
|
||||
const bool expected_negative12 = c->spark_status == sparkStatus::SPARK_NEG_WAIT;
|
||||
const bool expected_negative12 = coils->spark_status == sparkStatus::SPARK_NEG_WAIT;
|
||||
// Timeout not occourred, expected NEGATIVE edge spark OCCOURRED
|
||||
if (spark_timeout == pdPASS && expected_negative12)
|
||||
{
|
||||
c->spark_delay = c->spark_time - c->trig_time;
|
||||
c->sstart_status = softStartStatus::SOFT_START;
|
||||
c->spark_status == sparkStatus::SPARK_NEG_OK;
|
||||
coils->spark_delay = coils->spark_time - coils->trig_time;
|
||||
coils->sstart_status = softStartStatus::SOFT_START;
|
||||
coils->spark_status == sparkStatus::SPARK_NEG_OK;
|
||||
#ifdef DEBUG
|
||||
LOG_INFO("Trigger Spark NEGATIVE");
|
||||
LOG_INFO("Spark12 Delay Timer: ", (int)ignA_status.coils12.spark_delay);
|
||||
LOG_INFO("Spark12 Delay Timer: ", (int)ign_box_sts.coils12.spark_delay);
|
||||
#endif
|
||||
}
|
||||
// Timeout occourred, expected POSITIVE edge spark NOT OCCOURRED
|
||||
else if (spark_timeout == pdFAIL && expected_negative12)
|
||||
{
|
||||
c->sstart_status = softStartStatus::NORMAL;
|
||||
c->spark_status = sparkStatus::SPARK_NEG_FAIL;
|
||||
coils->sstart_status = softStartStatus::NORMAL;
|
||||
coils->spark_status = sparkStatus::SPARK_NEG_FAIL;
|
||||
}
|
||||
// Timeout not occouured, unexpected negative edge spark
|
||||
else if (spark_timeout == pdPASS && !expected_negative12)
|
||||
{
|
||||
c->sstart_status = softStartStatus::SOFT_START;
|
||||
c->spark_status = sparkStatus::SPARK_NEG_UNEXPECTED;
|
||||
coils->sstart_status = softStartStatus::SOFT_START;
|
||||
coils->spark_status = sparkStatus::SPARK_NEG_UNEXPECTED;
|
||||
}
|
||||
// Wait for finish of negative pulse to save data to buffer
|
||||
new_data = true;
|
||||
@@ -252,19 +255,15 @@ void ignitionA_task(void *pvParameters)
|
||||
// read adc channels: pickup12, out12 [ pos + neg ]
|
||||
if (adc) // read only if adc initialized
|
||||
{
|
||||
ignA_status.volts_gen = adcReadChannel(adc, rt_adcch.adc_gen);
|
||||
// from peak detector circuits
|
||||
ignA_status.coils12.peak_p_in = adcReadChannel(adc, rt_adcch.adc_peak_12p_in);
|
||||
ignA_status.coils12.peak_n_in = adcReadChannel(adc, rt_adcch.adc_peak_12n_in);
|
||||
ignA_status.coils34.peak_p_in = adcReadChannel(adc, rt_adcch.adc_peak_34p_in);
|
||||
ignA_status.coils34.peak_n_in = adcReadChannel(adc, rt_adcch.adc_peak_34n_in);
|
||||
ignA_status.coils12.peak_p_out = adcReadChannel(adc, rt_adcch.adc_peak_12p_out);
|
||||
ignA_status.coils12.peak_n_out = adcReadChannel(adc, rt_adcch.adc_peak_12n_out);
|
||||
ignA_status.coils34.peak_p_out = adcReadChannel(adc, rt_adcch.adc_peak_34p_out);
|
||||
ignA_status.coils34.peak_n_out = adcReadChannel(adc, rt_adcch.adc_peak_34n_out);
|
||||
// from sample and hold triggered from spark interrupt
|
||||
ignA_status.coils12.trigger_spark = adcReadChannel(adc, rt_adcch.adc_spark_12);
|
||||
ignA_status.coils34.trigger_spark = adcReadChannel(adc, rt_adcch.adc_spark_34);
|
||||
ign_box_sts.coils12.peak_p_in = adcReadChannel(adc, ADC_CH_PEAK_12P_IN);
|
||||
ign_box_sts.coils12.peak_n_in = adcReadChannel(adc, ADC_CH_PEAK_12N_IN);
|
||||
ign_box_sts.coils34.peak_p_in = adcReadChannel(adc, ADC_CH_PEAK_34P_IN);
|
||||
ign_box_sts.coils34.peak_n_in = adcReadChannel(adc, ADC_CH_PEAK_34N_IN);
|
||||
ign_box_sts.coils12.peak_p_out = adcReadChannel(adc, ADC_CH_PEAK_12P_OUT);
|
||||
ign_box_sts.coils12.peak_n_out = adcReadChannel(adc, ADC_CH_PEAK_12N_OUT);
|
||||
ign_box_sts.coils34.peak_p_out = adcReadChannel(adc, ADC_CH_PEAK_34P_OUT);
|
||||
ign_box_sts.coils34.peak_n_out = adcReadChannel(adc, ADC_CH_PEAK_34N_OUT);
|
||||
}
|
||||
else // simulate adc read timig
|
||||
vTaskDelay(pdMS_TO_TICKS(6));
|
||||
@@ -281,13 +280,14 @@ void ignitionA_task(void *pvParameters)
|
||||
else
|
||||
vTaskDelay(pdMS_TO_TICKS(2));
|
||||
|
||||
// send essage to main loop with ignition info
|
||||
// send essage to main loop with ignition info, by copy so local static variable is ok
|
||||
if (queue)
|
||||
ignA_status.timestamp = esp_timer_get_time(); // update data timestamp
|
||||
if (xQueueSendToBack(queue, (void*)&ignA_status, pdMS_TO_TICKS(1)) != pdPASS) {
|
||||
q_fail_count++;
|
||||
LOG_ERROR("Failed to send to queue");
|
||||
}
|
||||
ign_box_sts.timestamp = esp_timer_get_time(); // update data timestamp
|
||||
if (xQueueSendToBack(queue, (void *)&ign_box_sts, pdMS_TO_TICKS(1)) != pdPASS)
|
||||
{
|
||||
q_fail_count++;
|
||||
LOG_ERROR("Failed to send to queue");
|
||||
}
|
||||
}
|
||||
}
|
||||
LOG_WARN("Ending realTime Task");
|
||||
|
||||
Reference in New Issue
Block a user