refactored real time task to general with configuration structure
This commit is contained in:
@@ -1,50 +1,34 @@
|
||||
#pragma once
|
||||
|
||||
// Test device Flag
|
||||
#define TEST
|
||||
|
||||
// Arduino Libraries
|
||||
#include <Arduino.h>
|
||||
#include <map>
|
||||
#include "soc/gpio_struct.h"
|
||||
#ifndef TEST
|
||||
#include "pins.h"
|
||||
#include "pins.h"
|
||||
#else
|
||||
#include "pins_test.h"
|
||||
#include "pins_test.h"
|
||||
#endif
|
||||
|
||||
#define CORE_0 0
|
||||
#define CORE_1 1
|
||||
#define TASK_STACK 4096 // in words
|
||||
#define TASK_PRIORITY 2 // priorità leggermente più alta
|
||||
#define TASK_STACK 4096 // in words
|
||||
#define TASK_PRIORITY (configMAX_PRIORITIES - 4) // highest priority after wifi tasks
|
||||
|
||||
// =====================
|
||||
// Event Flags (bitmask)
|
||||
// =====================
|
||||
#define TRIG_FLAG_A12P (1 << 0)
|
||||
#define TRIG_FLAG_A12N (1 << 2)
|
||||
#define TRIG_FLAG_A34P (1 << 1)
|
||||
#define TRIG_FLAG_A34N (1 << 3)
|
||||
#ifndef TEST
|
||||
#define TRIG_FLAG_B12P (1 << 4)
|
||||
#define TRIG_FLAG_B12N (1 << 6)
|
||||
#define TRIG_FLAG_B34P (1 << 5)
|
||||
#define TRIG_FLAG_B34N (1 << 7)
|
||||
#endif
|
||||
#define TRIG_FLAG_12P (1 << 0)
|
||||
#define TRIG_FLAG_12N (1 << 2)
|
||||
#define TRIG_FLAG_34P (1 << 1)
|
||||
#define TRIG_FLAG_34N (1 << 3)
|
||||
|
||||
#define SPARK_FLAG_A12 (1 << 8)
|
||||
#define SPARK_FLAG_A34 (1 << 9)
|
||||
#ifndef TEST
|
||||
#define SPARK_FLAG_B12 (1 << 10)
|
||||
#define SPARK_FLAG_B34 (1 << 11)
|
||||
#endif
|
||||
|
||||
static const std::map<const uint32_t, const char*> names = {
|
||||
{TRIG_FLAG_A12P, "TRIG_FLAG_A12P"},
|
||||
{TRIG_FLAG_A12N, "TRIG_FLAG_A12N"},
|
||||
{TRIG_FLAG_A34P, "TRIG_FLAG_A34P"},
|
||||
{TRIG_FLAG_A34N, "TRIG_FLAG_A34N"},
|
||||
{SPARK_FLAG_A12, "SPARK_FLAG_A12"},
|
||||
{SPARK_FLAG_A34, "SPARK_FLAG_A34"},
|
||||
};
|
||||
#define SPARK_FLAG_NIL (1 << 8)
|
||||
#define SPARK_FLAG_12 (1 << 9)
|
||||
#define SPARK_FLAG_34 (1 << 10)
|
||||
|
||||
// Task handle
|
||||
TaskHandle_t trigA_TaskHandle = NULL;
|
||||
@@ -78,20 +62,22 @@ struct coilsStatus
|
||||
int64_t spark_time;
|
||||
int64_t spark_delay;
|
||||
sparkStatus spark_status;
|
||||
softStartStatus soft_start_status;
|
||||
float pickup_volts;
|
||||
float output_volts;
|
||||
softStartStatus sstart_status;
|
||||
float peak_p_in, peak_n_in;
|
||||
float peak_p_out, peak_n_out;
|
||||
float trigger_spark;
|
||||
};
|
||||
|
||||
// Task internal Status
|
||||
struct ignitionBoxStatus
|
||||
{
|
||||
int64_t timestamp;
|
||||
// coils pairs for each ignition
|
||||
coilsStatus coils12;
|
||||
coilsStatus coils34;
|
||||
// voltage from generator
|
||||
float volts_gen = 0.0;
|
||||
|
||||
// spark flags
|
||||
bool spark12 = false;
|
||||
bool spark34 = false;
|
||||
};
|
||||
@@ -103,126 +89,72 @@ ignitionBoxStatus ignB_status;
|
||||
static uint32_t pin2trig[49] = {0};
|
||||
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;
|
||||
#ifndef TEST
|
||||
pin2trig[TRIG_B12P] = TRIG_FLAG_B12P;
|
||||
pin2trig[TRIG_B12N] = TRIG_FLAG_B12N;
|
||||
pin2trig[TRIG_B34P] = TRIG_FLAG_B34P;
|
||||
pin2trig[TRIG_B34N] = TRIG_FLAG_B34N;
|
||||
#endif
|
||||
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
|
||||
};
|
||||
|
||||
static uint32_t pin2spark[49] = {0};
|
||||
void initSparkPinMapping()
|
||||
{
|
||||
pin2spark[SPARK_A12] = SPARK_FLAG_A12;
|
||||
pin2spark[SPARK_A34] = SPARK_FLAG_A34;
|
||||
#ifndef TEST
|
||||
pin2spark[SPARK_B12] = SPARK_FLAG_B12;
|
||||
pin2spark[SPARK_B34] = SPARK_FLAG_B34;
|
||||
#endif
|
||||
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)
|
||||
// one function for each wake up pin conncted to a trigger
|
||||
// =====================
|
||||
void IRAM_ATTR trig_isr_a(void* arg)
|
||||
void IRAM_ATTR trig_isr_a(void *arg)
|
||||
{
|
||||
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
||||
volatile const int64_t time_us = esp_timer_get_time();
|
||||
if (!trigA_TaskHandle)
|
||||
return; // exit if task is not running
|
||||
|
||||
uint32_t flag = (uint32_t)arg;
|
||||
// exit if task is not running
|
||||
if (!trigA_TaskHandle)
|
||||
return;
|
||||
|
||||
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
||||
volatile const uint32_t flag = (uint32_t)arg;
|
||||
|
||||
// reset spark flags, cannot be same time as trigger flags
|
||||
ignA_status.spark12 = false;
|
||||
ignA_status.spark34 = false;
|
||||
|
||||
if (flag & TRIG_FLAG_A12P) {
|
||||
switch (flag)
|
||||
{
|
||||
case TRIG_FLAG_12P:
|
||||
ignA_status.coils12.trig_time = time_us;
|
||||
xTaskNotifyFromISR(trigA_TaskHandle, flag, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
}
|
||||
if (flag & TRIG_FLAG_A34P) {
|
||||
break;
|
||||
case TRIG_FLAG_34P:
|
||||
ignA_status.coils34.trig_time = time_us;
|
||||
xTaskNotifyFromISR(trigA_TaskHandle, flag, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
}
|
||||
|
||||
if (flag & SPARK_FLAG_A12) {
|
||||
break;
|
||||
case SPARK_FLAG_12:
|
||||
ignA_status.spark12 = true;
|
||||
ignA_status.coils12.spark_time = time_us;
|
||||
xTaskNotifyGive(trigA_TaskHandle);
|
||||
}
|
||||
if (flag & SPARK_FLAG_A34) {
|
||||
ignA_status.spark12 = false;
|
||||
vTaskNotifyGiveFromISR(trigA_TaskHandle, &xHigherPriorityTaskWoken);
|
||||
break;
|
||||
case SPARK_FLAG_34:
|
||||
ignA_status.spark34 = true;
|
||||
ignA_status.coils34.spark_time = time_us;
|
||||
xTaskNotifyGive(trigA_TaskHandle);
|
||||
vTaskNotifyGiveFromISR(trigA_TaskHandle, &xHigherPriorityTaskWoken);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
//xTaskNotifyFromISR(trigA_TaskHandle, flag, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
if (xHigherPriorityTaskWoken) {
|
||||
if (xHigherPriorityTaskWoken)
|
||||
portYIELD_FROM_ISR();
|
||||
}
|
||||
}
|
||||
|
||||
// void IRAM_ATTR spark_a(void* arg)
|
||||
// {
|
||||
// BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
||||
// volatile const int64_t time_us = esp_timer_get_time();
|
||||
// if (!trigA_TaskHandle)
|
||||
// return;
|
||||
|
||||
// uint32_t flag = (uint32_t) arg;
|
||||
|
||||
// xTaskNotifyFromISR(trigA_TaskHandle, flag, eSetBits, &xHigherPriorityTaskWoken);
|
||||
// portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
|
||||
// }
|
||||
|
||||
#ifndef TEST
|
||||
void IRAM_ATTR trig_isr_b()
|
||||
{
|
||||
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
||||
volatile const int64_t time_us = esp_timer_get_time();
|
||||
if (!trigB_TaskHandle)
|
||||
return; // exit if task is not running
|
||||
|
||||
#ifndef TEST
|
||||
uint32_t status = GPIO.status;
|
||||
#else
|
||||
uint32_t status = GPIO.status1.val;
|
||||
#endif
|
||||
uint32_t pickup_flags = 0;
|
||||
|
||||
while (status)
|
||||
{
|
||||
uint32_t pin = __builtin_ctz(status); // trova primo bit attivo
|
||||
status &= ~(1 << pin); // clear bit
|
||||
pickup_flags |= pin2trig[pin];
|
||||
}
|
||||
|
||||
if (pickup_flags & TRIG_FLAG_B12P)
|
||||
ignB_status.coils12.trig_time = time_us;
|
||||
if (pickup_flags & TRIG_FLAG_B34P)
|
||||
ignB_status.coils34.trig_time = time_us;
|
||||
|
||||
xTaskNotifyFromISR(trigB_TaskHandle, pickup_flags, eSetBits, &xHigherPriorityTaskWoken);
|
||||
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
|
||||
}
|
||||
|
||||
void IRAM_ATTR spark_b()
|
||||
{
|
||||
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
||||
volatile const int64_t time_us = esp_timer_get_time();
|
||||
if (!trigB_TaskHandle)
|
||||
return;
|
||||
uint32_t spark_flag = GPIO.status1.val & SPARK_B12 ? SPARK_FLAG_B12 : SPARK_FLAG_B34;
|
||||
if (spark_flag & SPARK_FLAG_B12)
|
||||
ignB_status.coils12.spark_time = time_us;
|
||||
if (spark_flag & SPARK_FLAG_B34)
|
||||
ignB_status.coils34.spark_time = time_us;
|
||||
xTaskNotifyFromISR(trigB_TaskHandle, spark_flag, eSetBits, &xHigherPriorityTaskWoken);
|
||||
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
|
||||
}
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user