Compare commits
2 Commits
8c5b7d4a1c
...
f84fcfffbb
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f84fcfffbb | ||
|
|
bece80ad6c |
38
RotaxMonitor/src/devices.h
Normal file
38
RotaxMonitor/src/devices.h
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
// Device Libraries
|
||||||
|
#include <ADS1256.h>
|
||||||
|
#include <AD5292.h>
|
||||||
|
#include <Adafruit_SSD1306.h>
|
||||||
|
#include <PCA95x5.h>
|
||||||
|
|
||||||
|
// ADC Channel mapping
|
||||||
|
#define PICKUP_IN_A12 SING_0
|
||||||
|
#define PICKUP_IN_A34 SING_1
|
||||||
|
#define PICKUP_OUT_A12 SING_2
|
||||||
|
#define PICKUP_OUT_A34 SING_3
|
||||||
|
|
||||||
|
#define PICKUP_IN_B12 SING_4
|
||||||
|
#define PICKUP_IN_B34 SING_5
|
||||||
|
#define PICKUP_OUT_B12 SING_6
|
||||||
|
#define PICKUP_OUT_B34 SING_7
|
||||||
|
|
||||||
|
struct Devices {
|
||||||
|
AD5292* pot;
|
||||||
|
ADS1256* adc;
|
||||||
|
Adafruit_SSD1306* lcd;
|
||||||
|
PCA9555* io;
|
||||||
|
};
|
||||||
|
|
||||||
|
float adcReadChannel(ADS1256* adc, const uint32_t ch){
|
||||||
|
adc->setMUX(PICKUP_IN_A12);
|
||||||
|
// scarta 3 conversioni
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
while (digitalRead(ADC_DRDY));
|
||||||
|
adc->readSingle();
|
||||||
|
}
|
||||||
|
|
||||||
|
// ora lettura valida a 30kSPS → ~100 µs di settling
|
||||||
|
while (digitalRead(ADC_DRDY));
|
||||||
|
return adc->convertToVoltage(adc->readSingle());
|
||||||
|
}
|
||||||
@@ -1,13 +1,16 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include "soc/gpio_struct.h"
|
#include "soc/gpio_struct.h"
|
||||||
#include "pins.h"
|
#include "pins.h"
|
||||||
|
|
||||||
|
|
||||||
#define CORE_0 0
|
#define CORE_0 0
|
||||||
#define CORE_1 1
|
#define CORE_1 1
|
||||||
#define TASK_STACK 4096 // in words
|
#define TASK_STACK 4096 // in words
|
||||||
#define TASK_PRIORITY 2 // priorità leggermente più alta
|
#define TASK_PRIORITY 2 // priorità leggermente più alta
|
||||||
|
|
||||||
|
#define IGN_BUF_SIZE 128
|
||||||
|
|
||||||
// =====================
|
// =====================
|
||||||
// Event Flags (bitmask)
|
// Event Flags (bitmask)
|
||||||
// =====================
|
// =====================
|
||||||
@@ -20,41 +23,82 @@
|
|||||||
#define TRIG_FLAG_B34P (1 << 5)
|
#define TRIG_FLAG_B34P (1 << 5)
|
||||||
#define TRIG_FLAG_B34N (1 << 7)
|
#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
|
// Task handle
|
||||||
TaskHandle_t trigA_TaskHandle = NULL;
|
TaskHandle_t trigA_TaskHandle = NULL;
|
||||||
TaskHandle_t trigB_TaskHandle = NULL;
|
TaskHandle_t trigB_TaskHandle = NULL;
|
||||||
|
|
||||||
// Task internal Status
|
// Spark Status
|
||||||
struct taskStatus {
|
enum sparkStatus {
|
||||||
int64_t trig12_start;
|
SPARK_POS_OK,
|
||||||
int64_t trig34_start;
|
SPARK_NEG_OK,
|
||||||
|
SPARK_POS_SKIP,
|
||||||
int64_t trig12_time;
|
SPARK_NEG_SKIP,
|
||||||
int64_t trig34_time;
|
SPARK_POS_WAIT,
|
||||||
|
SPARK_NEG_WAIT,
|
||||||
bool trig12_complete = false;
|
SPARK_POS_FAIL,
|
||||||
bool trig34_complete = false;
|
SPARK_NEG_FAIL,
|
||||||
|
SPARK_POS_UNEXPECTED,
|
||||||
bool soft12 = false;
|
SPARK_NEG_UNEXPECTED,
|
||||||
bool soft34 = false;
|
SPARK_SYNC_FAIL,
|
||||||
|
|
||||||
bool error12 = false;
|
|
||||||
bool error34 = false;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
taskStatus ignA_status;
|
// Task internal Status
|
||||||
taskStatus ignB_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
|
// Pin to flag Map
|
||||||
static const uint32_t int2flag[] = {
|
static uint32_t pin2trig[49];
|
||||||
[TRIG_A12P] = TRIG_FLAG_A12P,
|
void initTriggerPinMapping() {
|
||||||
[TRIG_A12N] = TRIG_FLAG_A34P,
|
pin2trig[TRIG_A12P] = TRIG_FLAG_A12P;
|
||||||
[TRIG_A34P] = TRIG_FLAG_A12N,
|
pin2trig[TRIG_A12N] = TRIG_FLAG_A12N;
|
||||||
[TRIG_A34N] = TRIG_FLAG_A34N,
|
pin2trig[TRIG_A34P] = TRIG_FLAG_A34P;
|
||||||
[TRIG_B12P] = TRIG_FLAG_B12P,
|
pin2trig[TRIG_A34N] = TRIG_FLAG_A34N;
|
||||||
[TRIG_B12N] = TRIG_FLAG_B34P,
|
pin2trig[TRIG_B12P] = TRIG_FLAG_B12P;
|
||||||
[TRIG_B34P] = TRIG_FLAG_B12N,
|
pin2trig[TRIG_B12N] = TRIG_FLAG_B12N;
|
||||||
[TRIG_B34N] = TRIG_FLAG_B34N,
|
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
|
uint32_t pin = __builtin_ctz(status); // trova primo bit attivo
|
||||||
status &= ~(1 << pin); // clear bit
|
status &= ~(1 << pin); // clear bit
|
||||||
|
|
||||||
flags |= int2flag[pin];
|
flags |= pin2trig[pin];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (flags & (TRIG_FLAG_A12P | TRIG_FLAG_A12N))
|
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() {
|
void IRAM_ATTR trig_isr_b() {
|
||||||
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
||||||
|
|
||||||
uint32_t status = GPIO.status;
|
uint32_t status = GPIO.status1.val;
|
||||||
uint32_t flags = 0;
|
uint32_t flags = 0;
|
||||||
|
|
||||||
while (status) {
|
while (status) {
|
||||||
uint32_t pin = __builtin_ctz(status); // trova primo bit attivo
|
uint32_t pin = __builtin_ctz(status); // trova primo bit attivo
|
||||||
status &= ~(1 << pin); // clear bit
|
status &= ~(1 << pin); // clear bit
|
||||||
|
|
||||||
flags |= int2flag[pin];
|
flags |= pin2trig[pin];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (trigB_TaskHandle) {
|
if (trigB_TaskHandle) {
|
||||||
@@ -104,3 +157,12 @@ void IRAM_ATTR trig_isr_b() {
|
|||||||
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -7,14 +7,11 @@
|
|||||||
#include <SPI.h>
|
#include <SPI.h>
|
||||||
|
|
||||||
// Definitions
|
// Definitions
|
||||||
#include <isr.h>
|
|
||||||
#include <pins.h>
|
#include <pins.h>
|
||||||
#include <channels.h>
|
#include <channels.h>
|
||||||
#include <tasks.h>
|
#include <tasks.h>
|
||||||
|
#include <devices.h>
|
||||||
|
|
||||||
// Device Libraries
|
|
||||||
#include <ADS1256.h>
|
|
||||||
#include <AD5292.h>
|
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
delay(250);
|
delay(250);
|
||||||
@@ -31,7 +28,7 @@ void setup() {
|
|||||||
LOG_INFO("ESP32 Heap:", ESP.getHeapSize());
|
LOG_INFO("ESP32 Heap:", ESP.getHeapSize());
|
||||||
LOG_INFO("ESP32 Sketch:", ESP.getFreeSketchSpace());
|
LOG_INFO("ESP32 Sketch:", ESP.getFreeSketchSpace());
|
||||||
|
|
||||||
// Initialize Interrupt pins on peak detectors
|
// Initialize Interrupt pins on coil detectors
|
||||||
pinMode(TRIG_A12P, INPUT_PULLDOWN);
|
pinMode(TRIG_A12P, INPUT_PULLDOWN);
|
||||||
pinMode(TRIG_A12N, INPUT_PULLDOWN);
|
pinMode(TRIG_A12N, INPUT_PULLDOWN);
|
||||||
pinMode(TRIG_A34P, INPUT_PULLDOWN);
|
pinMode(TRIG_A34P, INPUT_PULLDOWN);
|
||||||
@@ -40,17 +37,29 @@ void setup() {
|
|||||||
pinMode(TRIG_B12N, INPUT_PULLDOWN);
|
pinMode(TRIG_B12N, INPUT_PULLDOWN);
|
||||||
pinMode(TRIG_B34P, INPUT_PULLDOWN);
|
pinMode(TRIG_B34P, INPUT_PULLDOWN);
|
||||||
pinMode(TRIG_B34N, INPUT_PULLDOWN);
|
pinMode(TRIG_B34N, INPUT_PULLDOWN);
|
||||||
|
initTriggerPinMapping();
|
||||||
|
|
||||||
|
// Initialize Interrupt pins on spark detectors
|
||||||
|
pinMode(SPARK_A12, INPUT_PULLDOWN);
|
||||||
|
pinMode(SPARK_A34, INPUT_PULLDOWN);
|
||||||
|
pinMode(SPARK_B12, INPUT_PULLDOWN);
|
||||||
|
pinMode(SPARK_B34, INPUT_PULLDOWN);
|
||||||
|
initSparkPinMapping();
|
||||||
|
|
||||||
// Ignition A Interrupts
|
// Ignition A Interrupts
|
||||||
attachInterrupt(TRIG_A12P, trig_isr_a, RISING);
|
attachInterrupt(TRIG_A12P, trig_isr_a, RISING);
|
||||||
attachInterrupt(TRIG_A34P, trig_isr_a, RISING);
|
attachInterrupt(TRIG_A34P, trig_isr_a, RISING);
|
||||||
attachInterrupt(TRIG_A12N, trig_isr_a, RISING);
|
attachInterrupt(TRIG_A12N, trig_isr_a, RISING);
|
||||||
attachInterrupt(TRIG_A34N, trig_isr_a, RISING);
|
attachInterrupt(TRIG_A34N, trig_isr_a, RISING);
|
||||||
|
attachInterrupt(SPARK_A12, spark_a, RISING);
|
||||||
|
attachInterrupt(SPARK_A34, spark_a, RISING);
|
||||||
// Ignition B Interrupts
|
// Ignition B Interrupts
|
||||||
attachInterrupt(TRIG_B12P, trig_isr_b, RISING);
|
attachInterrupt(TRIG_B12P, trig_isr_b, RISING);
|
||||||
attachInterrupt(TRIG_B34P, trig_isr_b, RISING);
|
attachInterrupt(TRIG_B34P, trig_isr_b, RISING);
|
||||||
attachInterrupt(TRIG_B12N, trig_isr_b, RISING);
|
attachInterrupt(TRIG_B12N, trig_isr_b, RISING);
|
||||||
attachInterrupt(TRIG_B34N, trig_isr_b, RISING);
|
attachInterrupt(TRIG_B34N, trig_isr_b, RISING);
|
||||||
|
attachInterrupt(SPARK_B12, spark_b, RISING);
|
||||||
|
attachInterrupt(SPARK_B34, spark_b, RISING);
|
||||||
|
|
||||||
// Init SPI interface
|
// Init SPI interface
|
||||||
SPI.begin();
|
SPI.begin();
|
||||||
@@ -59,13 +68,20 @@ void setup() {
|
|||||||
void loop() {
|
void loop() {
|
||||||
// global variables
|
// global variables
|
||||||
bool running = true;
|
bool running = true;
|
||||||
|
Devices dev;
|
||||||
|
|
||||||
|
// Init devices
|
||||||
|
dev.adc = new ADS1256(ADC_DRDY, ADC_RST, ADC_SYNC, ADC_CS, 2.5, &SPI);
|
||||||
|
dev.adc->InitializeADC();
|
||||||
|
dev.adc->setPGA(PGA_1);
|
||||||
|
dev.adc->setDRATE(DRATE_1000SPS);
|
||||||
|
|
||||||
// Ignition A on Core 0
|
// Ignition A on Core 0
|
||||||
auto ignA_task_success = xTaskCreatePinnedToCore(
|
auto ignA_task_success = xTaskCreatePinnedToCore(
|
||||||
ignitionA_task,
|
ignitionA_task,
|
||||||
"ignitionA_task",
|
"ignitionA_task",
|
||||||
TASK_STACK,
|
TASK_STACK,
|
||||||
NULL,
|
(void*) &dev,
|
||||||
TASK_PRIORITY,
|
TASK_PRIORITY,
|
||||||
&trigA_TaskHandle,
|
&trigA_TaskHandle,
|
||||||
CORE_0
|
CORE_0
|
||||||
@@ -76,7 +92,7 @@ void loop() {
|
|||||||
ignitionB_task,
|
ignitionB_task,
|
||||||
"ignitionB_task",
|
"ignitionB_task",
|
||||||
TASK_STACK,
|
TASK_STACK,
|
||||||
NULL,
|
(void*) &dev,
|
||||||
TASK_PRIORITY, // priorità leggermente più alta
|
TASK_PRIORITY, // priorità leggermente più alta
|
||||||
&trigA_TaskHandle,
|
&trigA_TaskHandle,
|
||||||
CORE_1
|
CORE_1
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
// =====================
|
// =====================
|
||||||
// SPI BUS
|
// SPI BUS
|
||||||
// =====================
|
// =====================
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
// Arduino Libraries
|
// Arduino Libraries
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include <DebugLog.h>
|
#include <DebugLog.h>
|
||||||
@@ -5,67 +7,112 @@
|
|||||||
// ISR
|
// ISR
|
||||||
#include "isr.h"
|
#include "isr.h"
|
||||||
|
|
||||||
const uint16_t spark_delay_us = 500;
|
// DEVICES
|
||||||
|
#include "devices.h"
|
||||||
|
|
||||||
|
const auto spark_timeout_max = 1;
|
||||||
|
|
||||||
void ignitionA_task(void *pvParameters) {
|
void ignitionA_task(void *pvParameters) {
|
||||||
uint32_t notifiedValue;
|
Devices* dev = (Devices*) pvParameters;
|
||||||
|
ADS1256* adc = dev->adc;
|
||||||
|
|
||||||
|
uint32_t pickup_flag;
|
||||||
|
uint32_t spark_flag;
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
// attende eventi
|
// WAIT FOR PICKUP SIGNAL
|
||||||
xTaskNotifyWait(
|
xTaskNotifyWait(
|
||||||
0x00, // non pulire all'ingresso
|
0x00, // non pulire all'ingresso
|
||||||
ULONG_MAX, // pulisci tutti i bit all'uscita
|
ULONG_MAX, // pulisci tutti i bit all'uscita
|
||||||
¬ifiedValue, // valore ricevuto
|
&pickup_flag, // valore ricevuto
|
||||||
portMAX_DELAY
|
portMAX_DELAY
|
||||||
);
|
);
|
||||||
|
// 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();
|
||||||
|
|
||||||
uint64_t wait_time=0;
|
// A trigger from pickup 12 is followed by a spark event on 34 or vice versa pickup 34 triggers spark on 12
|
||||||
switch (notifiedValue) {
|
if ((pickup_flag == TRIG_FLAG_A12P || pickup_flag == TRIG_FLAG_A12N) && spark_flag != SPARK_A12) {
|
||||||
case TRIG_FLAG_A12P:
|
ignA_status.trig12_start = ignA_status.trig34_start = -1;
|
||||||
case TRIG_FLAG_A12N:
|
ignA_status.trig12_end = ignA_status.trig34_end = -1;
|
||||||
bool spark12_timeout = false;
|
ignA_status.spark12_delay = ignA_status.spark34_delay = -1;
|
||||||
if (ignA_status.trig12_complete) {
|
ignA_status.soft12_engaged = ignA_status.soft34_engaged = false;
|
||||||
// read peak adc values from sample and hold
|
ignA_status.spark12_status = ignA_status.spark12_status = sparkStatus::SPARK_SYNC_FAIL;
|
||||||
} else {
|
// Save error on circular buffer and skip to next cycle //
|
||||||
while(!digitalRead(SPARK_A12)) {
|
// [TODO]
|
||||||
wait_time = ignA_status.trig12_start - esp_timer_get_time();
|
continue;
|
||||||
if (wait_time >= spark_delay_us) {
|
}
|
||||||
spark12_timeout = true;
|
|
||||||
break;
|
bool new_data12 = false;
|
||||||
}
|
bool new_data34 = false;
|
||||||
}
|
|
||||||
if (spark12_timeout) { // spark did not happen, timeout
|
switch (pickup_flag) {
|
||||||
ignA_status.trig12_complete = false;
|
case TRIG_FLAG_A12P: {
|
||||||
} else { // spark did happen
|
// Timeout not occourred, expected POSITIVE edge spark OCCOURRED
|
||||||
ignA_status.trig12_complete = true;
|
if (spark_timeout == pdPASS) {
|
||||||
ignA_status.trig12_time = wait_time;
|
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;
|
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.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
|
||||||
|
new_data12 = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
case TRIG_FLAG_A34P:
|
case TRIG_FLAG_A34P:
|
||||||
case TRIG_FLAG_A34N:
|
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;
|
break;
|
||||||
default:
|
default:
|
||||||
LOG_ERROR("Invalid A Interrupt: ", notifiedValue);
|
LOG_ERROR("Invalid A Interrupt");
|
||||||
|
}
|
||||||
|
|
||||||
|
vTaskDelay(pdMS_TO_TICKS(1)); // delay 1ms to allow peak detectors to charge
|
||||||
|
|
||||||
|
if (new_data12) {
|
||||||
|
// read adc channels: pickup12, out12 [ pos + neg ]
|
||||||
|
ignA_status.volts12_pickup = adcReadChannel(adc, PICKUP_IN_A12);
|
||||||
|
ignA_status.volts12_out = adcReadChannel(adc, PICKUP_OUT_A12);
|
||||||
|
// reset peak detectors
|
||||||
|
digitalWrite(RST_A12P, HIGH);
|
||||||
|
digitalWrite(RST_A12N, HIGH);
|
||||||
|
vTaskDelay(pdMS_TO_TICKS(1));
|
||||||
|
digitalWrite(RST_A12P, HIGH);
|
||||||
|
digitalWrite(RST_A12N, HIGH);
|
||||||
|
// save on circluar buffer 12
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user