Fixed pin mappings and task logic
This commit is contained in:
@@ -1,13 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
#include "soc/gpio_struct.h"
|
||||
#include "pins.h"
|
||||
|
||||
|
||||
#define CORE_0 0
|
||||
#define CORE_1 1
|
||||
#define TASK_STACK 4096 // in words
|
||||
#define TASK_PRIORITY 2 // priorità leggermente più alta
|
||||
|
||||
#define IGN_BUF_SIZE 128
|
||||
|
||||
// =====================
|
||||
// Event Flags (bitmask)
|
||||
// =====================
|
||||
@@ -20,41 +23,82 @@
|
||||
#define TRIG_FLAG_B34P (1 << 5)
|
||||
#define TRIG_FLAG_B34N (1 << 7)
|
||||
|
||||
#define SPARK_FLAG_A12 (1 << 0)
|
||||
#define SPARK_FLAG_A34 (1 << 2)
|
||||
#define SPARK_FLAG_B12 (1 << 1)
|
||||
#define SPARK_FLAG_B34 (1 << 3)
|
||||
|
||||
// Task handle
|
||||
TaskHandle_t trigA_TaskHandle = NULL;
|
||||
TaskHandle_t trigB_TaskHandle = NULL;
|
||||
|
||||
// Task internal Status
|
||||
struct taskStatus {
|
||||
int64_t trig12_start;
|
||||
int64_t trig34_start;
|
||||
|
||||
int64_t trig12_time;
|
||||
int64_t trig34_time;
|
||||
|
||||
bool trig12_complete = false;
|
||||
bool trig34_complete = false;
|
||||
|
||||
bool soft12 = false;
|
||||
bool soft34 = false;
|
||||
|
||||
bool error12 = false;
|
||||
bool error34 = false;
|
||||
// Spark Status
|
||||
enum sparkStatus {
|
||||
SPARK_POS_OK,
|
||||
SPARK_NEG_OK,
|
||||
SPARK_POS_SKIP,
|
||||
SPARK_NEG_SKIP,
|
||||
SPARK_POS_WAIT,
|
||||
SPARK_NEG_WAIT,
|
||||
SPARK_POS_FAIL,
|
||||
SPARK_NEG_FAIL,
|
||||
SPARK_POS_UNEXPECTED,
|
||||
SPARK_NEG_UNEXPECTED,
|
||||
SPARK_SYNC_FAIL,
|
||||
};
|
||||
|
||||
taskStatus ignA_status;
|
||||
taskStatus ignB_status;
|
||||
// Task internal Status
|
||||
struct ignitionBoxStatus {
|
||||
// start time from ISR
|
||||
int64_t trig12_start;
|
||||
int64_t trig34_start;
|
||||
// time at which spark occours
|
||||
int64_t trig12_end;
|
||||
int64_t trig34_end;
|
||||
// computed delay from pickup to spark
|
||||
int64_t spark12_delay;
|
||||
int64_t spark34_delay;
|
||||
// spark status
|
||||
sparkStatus spark12_status = sparkStatus::SPARK_POS_OK;
|
||||
sparkStatus spark34_status = sparkStatus::SPARK_POS_OK;
|
||||
// soft start status for circuits 12 and 34
|
||||
bool soft12_engaged = false;
|
||||
bool soft34_engaged = false;
|
||||
// peak voltage from circuits 12 and 34
|
||||
float volts12_pickup;
|
||||
float volts34_pickup;
|
||||
// peak voltage from conditioned output 12 and 34
|
||||
float volts12_out;
|
||||
float volts34_out;
|
||||
// voltage from generator
|
||||
float volts_gen;
|
||||
};
|
||||
|
||||
ignitionBoxStatus ignA_status;
|
||||
ignitionBoxStatus ignB_status;
|
||||
|
||||
ignitionBoxStatus ingA_statusBuffer[IGN_BUF_SIZE];
|
||||
ignitionBoxStatus ingB_statusBuffer[IGN_BUF_SIZE];
|
||||
|
||||
// Pin to flag Map
|
||||
static const uint32_t int2flag[] = {
|
||||
[TRIG_A12P] = TRIG_FLAG_A12P,
|
||||
[TRIG_A12N] = TRIG_FLAG_A34P,
|
||||
[TRIG_A34P] = TRIG_FLAG_A12N,
|
||||
[TRIG_A34N] = TRIG_FLAG_A34N,
|
||||
[TRIG_B12P] = TRIG_FLAG_B12P,
|
||||
[TRIG_B12N] = TRIG_FLAG_B34P,
|
||||
[TRIG_B34P] = TRIG_FLAG_B12N,
|
||||
[TRIG_B34N] = TRIG_FLAG_B34N,
|
||||
static uint32_t pin2trig[49];
|
||||
void initTriggerPinMapping() {
|
||||
pin2trig[TRIG_A12P] = TRIG_FLAG_A12P;
|
||||
pin2trig[TRIG_A12N] = TRIG_FLAG_A12N;
|
||||
pin2trig[TRIG_A34P] = TRIG_FLAG_A34P;
|
||||
pin2trig[TRIG_A34N] = TRIG_FLAG_A34N;
|
||||
pin2trig[TRIG_B12P] = TRIG_FLAG_B12P;
|
||||
pin2trig[TRIG_B12N] = TRIG_FLAG_B12N;
|
||||
pin2trig[TRIG_B34P] = TRIG_FLAG_B34P;
|
||||
pin2trig[TRIG_B34N] = TRIG_FLAG_B34N;
|
||||
};
|
||||
|
||||
static uint32_t pin2spark[49];
|
||||
void initSparkPinMapping() {
|
||||
pin2spark[SPARK_A12] = SPARK_FLAG_A12;
|
||||
pin2spark[SPARK_A34] = SPARK_FLAG_A34;
|
||||
pin2spark[SPARK_B12] = SPARK_FLAG_B12;
|
||||
pin2spark[SPARK_B34] = SPARK_FLAG_B34;
|
||||
};
|
||||
|
||||
// =====================
|
||||
@@ -72,7 +116,7 @@ void IRAM_ATTR trig_isr_a() {
|
||||
uint32_t pin = __builtin_ctz(status); // trova primo bit attivo
|
||||
status &= ~(1 << pin); // clear bit
|
||||
|
||||
flags |= int2flag[pin];
|
||||
flags |= pin2trig[pin];
|
||||
}
|
||||
|
||||
if (flags & (TRIG_FLAG_A12P | TRIG_FLAG_A12N))
|
||||
@@ -86,17 +130,26 @@ void IRAM_ATTR trig_isr_a() {
|
||||
}
|
||||
}
|
||||
|
||||
void IRAM_ATTR spark_a() {
|
||||
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
||||
uint32_t spark_flag = GPIO.status1.val & SPARK_A12 ? SPARK_FLAG_A12 : SPARK_FLAG_A34 ;
|
||||
if (trigA_TaskHandle) {
|
||||
xTaskNotifyFromISR(trigA_TaskHandle, spark_flag, eSetBits, &xHigherPriorityTaskWoken);
|
||||
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
|
||||
}
|
||||
}
|
||||
|
||||
void IRAM_ATTR trig_isr_b() {
|
||||
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
||||
|
||||
uint32_t status = GPIO.status;
|
||||
uint32_t status = GPIO.status1.val;
|
||||
uint32_t flags = 0;
|
||||
|
||||
while (status) {
|
||||
uint32_t pin = __builtin_ctz(status); // trova primo bit attivo
|
||||
status &= ~(1 << pin); // clear bit
|
||||
|
||||
flags |= int2flag[pin];
|
||||
flags |= pin2trig[pin];
|
||||
}
|
||||
|
||||
if (trigB_TaskHandle) {
|
||||
@@ -104,3 +157,12 @@ void IRAM_ATTR trig_isr_b() {
|
||||
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
|
||||
}
|
||||
}
|
||||
|
||||
void IRAM_ATTR spark_b() {
|
||||
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
||||
uint32_t spark_flag = GPIO.status1.val & SPARK_B12 ? SPARK_FLAG_B12 : SPARK_FLAG_B34 ;
|
||||
if (trigB_TaskHandle) {
|
||||
xTaskNotifyFromISR(trigB_TaskHandle, spark_flag, eSetBits, &xHigherPriorityTaskWoken);
|
||||
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user