From 7e7d0a1c59e2024cc0aa89240915181aab7756dc Mon Sep 17 00:00:00 2001 From: Emanuele Trabattoni Date: Tue, 21 Apr 2026 16:11:07 +0200 Subject: [PATCH] Second ADC debugging in process --- RotaxMonitor/lib/ADS1256/ADS1256.cpp | 28 ++-- RotaxMonitor/platformio.ini | 12 +- RotaxMonitor/src/devices.h | 16 +-- RotaxMonitor/src/main.cpp | 207 +++++++++++++++------------ RotaxMonitor/src/tasks.cpp | 54 ++++--- RotaxMonitor/src/tasks.h | 8 +- RotaxMonitor/src/utils.cpp | 30 ++-- 7 files changed, 199 insertions(+), 156 deletions(-) diff --git a/RotaxMonitor/lib/ADS1256/ADS1256.cpp b/RotaxMonitor/lib/ADS1256/ADS1256.cpp index 13ab8e1..8580685 100644 --- a/RotaxMonitor/lib/ADS1256/ADS1256.cpp +++ b/RotaxMonitor/lib/ADS1256/ADS1256.cpp @@ -14,10 +14,11 @@ #include "Arduino.h" #include "ADS1256.h" #include "SPI.h" +#include #define convertSigned24BitToLong(value) ((value) & (1l << 23) ? (value) - 0x1000000 : value) -void drdyCallback(void *arg) +void IRAM_ATTR drdyCallback(void *arg) { auto cls = (ADS1256 *)arg; if (!arg) @@ -61,11 +62,16 @@ ADS1256::ADS1256(const int8_t DRDY_pin, const int8_t RESET_pin, const int8_t SYN updateConversionParameter(); - m_drdyHigh = xSemaphoreCreateBinary(); - m_drdyLow = xSemaphoreCreateBinary(); - xSemaphoreGive(m_drdyHigh); - xSemaphoreGive(m_drdyLow); - attachInterruptArg(DRDY_pin, drdyCallback, (void *)this, CHANGE); + // m_drdyHigh = xSemaphoreCreateBinary(); + // m_drdyLow = xSemaphoreCreateBinary(); + // if (!m_drdyHigh || !m_drdyLow) { + // LOG_ERROR("ADC Unable to create interrupt semaphores"); + // return; + // } + + // xSemaphoreGive(m_drdyHigh); + // xSemaphoreGive(m_drdyLow); + //attachInterruptArg(DRDY_pin, drdyCallback, (void *)this, CHANGE); } // Initialization @@ -119,14 +125,16 @@ void ADS1256::InitializeADC() void ADS1256::waitForLowDRDY() { - xSemaphoreTake(m_drdyLow, pdMS_TO_TICKS(10)); - xSemaphoreGive(m_drdyLow); + while(digitalRead(m_DRDY_pin) == HIGH) {vTaskDelay(1);}; + // xSemaphoreTake(m_drdyLow, pdMS_TO_TICKS(10)); + // xSemaphoreGive(m_drdyLow); } void ADS1256::waitForHighDRDY() { - xSemaphoreTake(m_drdyHigh, pdMS_TO_TICKS(10)); - xSemaphoreGive(m_drdyHigh); + while(digitalRead(m_DRDY_pin) == LOW) {vTaskDelay(1);}; + // xSemaphoreTake(m_drdyHigh, pdMS_TO_TICKS(10)); + // xSemaphoreGive(m_drdyHigh); } void ADS1256::stopConversion() // Sending SDATAC to stop the continuous conversion diff --git a/RotaxMonitor/platformio.ini b/RotaxMonitor/platformio.ini index b43c9e2..c07cdb2 100644 --- a/RotaxMonitor/platformio.ini +++ b/RotaxMonitor/platformio.ini @@ -20,7 +20,6 @@ lib_deps = hideakitai/PCA95x5@^0.1.3 me-no-dev/AsyncTCP@^3.3.2 me-no-dev/ESPAsyncWebServer@^3.6.0 - adafruit/Adafruit NeoPixel@^1.15.4 upload_protocol = esptool upload_port = /dev/ttyACM1 upload_speed = 921600 @@ -28,15 +27,14 @@ monitor_port = /dev/ttyACM0 monitor_speed = 921600 build_type = release build_flags = - -DCORE_DEBUG_LEVEL=5 + -DCORE_DEBUG_LEVEL=3 -DARDUINO_USB_CDC_ON_BOOT=0 -DARDUINO_USB_MODE=0 -DCONFIG_ASYNC_TCP_MAX_ACK_TIME=5000 -DCONFIG_ASYNC_TCP_PRIORITY=21 - -DCONFIG_ASYNC_TCP_QUEUE_SIZE=64 + -DCONFIG_ASYNC_TCP_QUEUE_SIZE=128 -DCONFIG_ASYNC_TCP_RUNNING_CORE=1 - -DCONFIG_ASYNC_TCP_STACK_SIZE=4096 - -fstack-protector-all + -DCONFIG_ASYNC_TCP_STACK_SIZE=8192 [env:esp32-s3-devkitc1-n16r8-debug] board = ${env:esp32-s3-devkitc1-n16r8.board} @@ -46,7 +44,6 @@ platform = ${env:esp32-s3-devkitc1-n16r8.platform} framework = ${env:esp32-s3-devkitc1-n16r8.framework} lib_deps = ${env:esp32-s3-devkitc1-n16r8.lib_deps} - adafruit/Adafruit NeoPixel@^1.15.4 upload_protocol = esptool upload_port = /dev/ttyACM1 upload_speed = 921600 @@ -59,7 +56,7 @@ build_flags = -O0 -g3 -ggdb3 - -DCORE_DEBUG_LEVEL=5 + -DCORE_DEBUG_LEVEL=3 -DARDUINO_USB_CDC_ON_BOOT=0 -DARDUINO_USB_MODE=0 -DCONFIG_ASYNC_TCP_MAX_ACK_TIME=5000 @@ -67,4 +64,3 @@ build_flags = -DCONFIG_ASYNC_TCP_QUEUE_SIZE=128 -DCONFIG_ASYNC_TCP_RUNNING_CORE=1 -DCONFIG_ASYNC_TCP_STACK_SIZE=8192 - -fstack-protector-all diff --git a/RotaxMonitor/src/devices.h b/RotaxMonitor/src/devices.h index 5ee2125..4a2c52a 100644 --- a/RotaxMonitor/src/devices.h +++ b/RotaxMonitor/src/devices.h @@ -26,9 +26,9 @@ struct Devices { // Busses - std::unique_ptr m_i2c = nullptr; - std::unique_ptr m_spi_a = nullptr; - std::unique_ptr m_spi_b = nullptr; + TwoWire *m_i2c = NULL; + SPIClass *m_spi_a = NULL; + SPIClass *m_spi_b = NULL; // Bus Mutextes std::mutex m_spi_a_mutex; @@ -36,13 +36,13 @@ struct Devices std::mutex m_i2c_mutex; // Device Pointers - std::unique_ptr m_pot_a = nullptr; - std::unique_ptr m_pot_b = nullptr; + AD5292 *m_pot_a = NULL; + AD5292 *m_pot_b = NULL; - std::unique_ptr m_adc_a = nullptr; - std::unique_ptr m_adc_b = nullptr; + ADS1256 *m_adc_a = NULL; + ADS1256 *m_adc_b = NULL; - std::unique_ptr m_ext_io = nullptr; + ExternalIO *m_ext_io = NULL; }; // Adc read channel wrapper to selet mux before reading diff --git a/RotaxMonitor/src/main.cpp b/RotaxMonitor/src/main.cpp index 0b04eeb..565bec9 100644 --- a/RotaxMonitor/src/main.cpp +++ b/RotaxMonitor/src/main.cpp @@ -16,20 +16,24 @@ #include #include -// Defines to enable channel B +#define CH_A_ENABLE #define CH_B_ENABLE +#define CH_A_RT_ENABLE +#define CH_B_RT_ENABLE +// #define I2C_ENABLE +// #define WEB_ENABLE // Debug Defines #define WIFI_SSID "AstroRotaxMonitor" #define WIFI_PASSWORD "maledettirotax" -#define PSRAM_MAX 4096 -#define QUEUE_MAX 256 +#define PSRAM_MAX 1024 +#define QUEUE_MAX 32 void setup() { Serial.begin(115200); delay(250); - Serial.setTimeout(30000); + Serial.setTimeout(5000); // Setup Logger LOG_ATTACH_SERIAL(Serial); @@ -47,28 +51,30 @@ void setup() LOG_DEBUG("ESP32 Heap:", ESP.getHeapSize()); LOG_DEBUG("ESP32 Sketch:", ESP.getFreeSketchSpace()); - // Init Wifi station - // LOG_INFO("Initializing WiFi..."); - // WiFi.mode(WIFI_AP); - // IPAddress local_IP(10, 11, 12, 1); - // IPAddress gateway(10, 11, 12, 1); - // IPAddress subnet(255, 255, 255, 0); - // WiFi.softAPConfig(local_IP, gateway, subnet); - // WiFi.setTxPower(WIFI_POWER_5dBm); // reduce wifi power - // if (WiFi.softAP(WIFI_SSID, WIFI_PASSWORD)) - // { - // LOG_INFO("WiFi AP Mode Started"); - // LOG_INFO("Wifi SSID:", WIFI_SSID); - // LOG_INFO("Wifi Password:", WIFI_PASSWORD); - // LOG_INFO("WiFi IP:" + WiFi.softAPIP().toString()); - // } - // else - // { - // LOG_ERROR("Failed to start WiFi AP Mode"); - // LOG_ERROR("5 seconds to restart..."); - // vTaskDelay(pdMS_TO_TICKS(5000)); - // esp_restart(); - // } +// Init Wifi station +#ifdef WEB_ENABLE + LOG_INFO("Initializing WiFi..."); + WiFi.mode(WIFI_AP); + IPAddress local_IP(10, 11, 12, 1); + IPAddress gateway(10, 11, 12, 1); + IPAddress subnet(255, 255, 255, 0); + WiFi.softAPConfig(local_IP, gateway, subnet); + WiFi.setTxPower(WIFI_POWER_5dBm); // reduce wifi power + if (WiFi.softAP(WIFI_SSID, WIFI_PASSWORD)) + { + LOG_INFO("WiFi AP Mode Started"); + LOG_INFO("Wifi SSID:", WIFI_SSID); + LOG_INFO("Wifi Password:", WIFI_PASSWORD); + LOG_INFO("WiFi IP:" + WiFi.softAPIP().toString()); + } + else + { + LOG_ERROR("Failed to start WiFi AP Mode"); + LOG_ERROR("5 seconds to restart..."); + vTaskDelay(pdMS_TO_TICKS(5000)); + esp_restart(); + } +#endif // Initialize Interrupt pins on PICKUP detectors initTriggerPinsInputs(); @@ -84,7 +90,7 @@ void loop() led.setBrightness(0.025f); led.setStatus(RGBled::LedStatus::INIT); - std::shared_ptr dev = std::make_shared(); + Devices dev; bool running = true; std::mutex fs_mutex; LITTLEFSGuard fsGuard; @@ -94,34 +100,40 @@ void loop() bool spiB_ok = true; //////// INIT SPI INTERFACES //////// LOG_DEBUG("Init SPI Interfaces"); - SPIClass SPI_A(FSPI); +#ifdef CH_A_ENABLE + LOG_DEBUG("Begin Init SPI_A"); + SPIClass SPI_A(HSPI); spiA_ok = SPI_A.begin(SPI_A_SCK, SPI_A_MISO, SPI_A_MOSI); SPI_A.setDataMode(SPI_MODE1); // ADS1256 requires SPI mode 1 - LOG_DEBUG("Init SPI A ok"); - Serial.readStringUntil('\n'); - dev->m_spi_a.reset(&SPI_A); - dev->m_adc_a = std::make_unique(ADC_A_DRDY, ADS1256::PIN_UNUSED, ADS1256::PIN_UNUSED, ADC_A_CS, 2.5, &SPI_A); - dev->m_adc_a->InitializeADC(); - dev->m_adc_a->setPGA(PGA_1); - dev->m_adc_a->setDRATE(DRATE_7500SPS); - LOG_DEBUG("Init ADC A ok"); - Serial.readStringUntil('\n'); - - delay(250); - - #ifdef CH_B_ENABLE - SPIClass SPI_B(HSPI); + LOG_DEBUG("Init SPI_A -> OK"); + delay(500); + LOG_DEBUG("Begin Init ADC_A"); + ADS1256 ADC_A(ADC_A_DRDY, ADS1256::PIN_UNUSED, ADS1256::PIN_UNUSED, ADC_A_CS, 2.5, &SPI_A); + ADC_A.InitializeADC(); + ADC_A.setPGA(PGA_1); + ADC_A.setDRATE(DRATE_7500SPS); + dev.m_adc_a = &ADC_A; + dev.m_spi_a = &SPI_A; + LOG_DEBUG("Init ADC_A -> OK"); + delay(1000); +#endif + +#ifdef CH_B_ENABLE + LOG_DEBUG("Begin Init SPI_B"); + SPIClass SPI_B(FSPI); spiB_ok = SPI_B.begin(SPI_B_SCK, SPI_B_MISO, SPI_B_MOSI); SPI_B.setDataMode(SPI_MODE1); // ADS1256 requires SPI mode 1 - LOG_DEBUG("Init SPI B ok"); - Serial.readStringUntil('\n'); - dev->m_spi_b.reset(&SPI_B); - dev->m_adc_b = std::make_unique(ADC_B_DRDY, ADS1256::PIN_UNUSED, ADS1256::PIN_UNUSED, ADC_B_CS, 2.5, &SPI_B); - dev->m_adc_b->InitializeADC(); - dev->m_adc_b->setPGA(PGA_1); - dev->m_adc_b->setDRATE(DRATE_7500SPS); - LOG_DEBUG("Init ADC B ok"); - Serial.readStringUntil('\n'); + LOG_DEBUG("Init SPI_B -> OK"); + delay(500); + LOG_DEBUG("Begin Init ADC_B"); + ADS1256 ADC_B(ADC_B_DRDY, ADS1256::PIN_UNUSED, ADS1256::PIN_UNUSED, ADC_B_CS, 2.5, &SPI_B); + ADC_B.InitializeADC(); + ADC_B.setPGA(PGA_1); + ADC_B.setDRATE(DRATE_7500SPS); + dev.m_adc_b = &ADC_B; + dev.m_spi_b = &SPI_B; + LOG_DEBUG("Init ADC_B -> OK"); + delay(1000); #endif if (!spiA_ok || !spiB_ok) @@ -132,27 +144,29 @@ void loop() esp_restart(); } - LOG_DEBUG("Init SPI OK"); + LOG_DEBUG("Init SPI -> OK"); + +//////// INIT I2C INTERFACES //////// +#ifdef I2C_ENABLE + LOG_DEBUG("Init I2C Interfaces"); + bool i2c_ok = true; + i2c_ok = Wire.begin(SDA, SCL, 100000); + if (!i2c_ok) + { + LOG_ERROR("Unable to Initialize I2C Bus"); + LOG_ERROR("5 seconds to restart..."); + vTaskDelay(pdMS_TO_TICKS(5000)); + esp_restart(); + } + LOG_DEBUG("Init I2c ok"); Serial.readStringUntil('\n'); - //////// INIT I2C INTERFACES //////// - // LOG_DEBUG("Init I2C Interfaces"); - // bool i2c_ok = true; - // i2c_ok = Wire.begin(SDA, SCL, 100000); - // if (!i2c_ok) - // { - // LOG_ERROR("Unable to Initialize I2C Bus"); - // LOG_ERROR("5 seconds to restart..."); - // vTaskDelay(pdMS_TO_TICKS(5000)); - // esp_restart(); - // } - // LOG_DEBUG("Init I2c ok"); - // Serial.readStringUntil('\n'); - // Init IO Expanders - // dev->m_ext_io = std::make_unique(Wire, dev->m_i2c_mutex, EXPANDER_ALL_INTERRUPT); + dev->m_ext_io = std::make_unique(Wire, dev->m_i2c_mutex, EXPANDER_ALL_INTERRUPT); +#endif - //////// INIT REALTIME TASKS PARAMETERS //////// +//////// INIT REALTIME TASKS PARAMETERS //////// +#ifdef CH_A_RT_ENABLE const rtIgnitionTask::rtTaskParams taskA_params{ .rt_running = true, .name = "rtIgnTask_A", @@ -182,8 +196,9 @@ void loop() .relay_out_34 = RELAY_OUT_A34, }, .rt_queue = nullptr, - .dev = dev}; - + .dev = &dev}; +#endif +#ifdef CH_B_RT_ENABLE const rtIgnitionTask::rtTaskParams taskB_params{ .rt_running = true, .name = "rtIgnTask_B", @@ -213,18 +228,30 @@ void loop() .relay_out_34 = RELAY_OUT_B34, }, .rt_queue = nullptr, - .dev = dev}; + .dev = &dev}; +#endif //////// SPAWN REALTIME TASKS //////// - auto task_A = rtIgnitionTask(taskA_params, PSRAM_MAX, QUEUE_MAX, CORE_0, fs_mutex); - delay(50); - Serial.readStringUntil('\n'); + bool tasK_A_rt = true; + bool task_B_rt = true; + BaseType_t ignA_task_success = pdPASS; + BaseType_t ignB_task_success = pdPASS; + +#ifdef CH_A_RT_ENABLE + auto task_A = rtIgnitionTask(taskA_params, PSRAM_MAX, QUEUE_MAX, CORE_1, fs_mutex); + ignA_task_success = task_A.getStatus() == rtIgnitionTask::OK ? pdPASS : pdFAIL; + //tasK_A_rt = task_A.start(); + delay(1000); +#endif + +#ifdef CH_B_RT_ENABLE auto task_B = rtIgnitionTask(taskB_params, PSRAM_MAX, QUEUE_MAX, CORE_1, fs_mutex); - Serial.readStringUntil('\n'); + ignB_task_success = task_B.getStatus() == rtIgnitionTask::OK ? pdPASS : pdFAIL; + //task_B_rt = task_B.start(); + delay(1000); +#endif // Ignition A on Core 0 - auto ignA_task_success = task_A.getStatus() == rtIgnitionTask::OK ? pdPASS : pdFAIL; - auto ignB_task_success = task_B.getStatus() == rtIgnitionTask::OK ? pdPASS : pdFAIL; if (ignA_task_success != pdPASS || ignB_task_success != pdPASS) { LOG_ERROR("Unable to initialize ISR task"); @@ -232,10 +259,6 @@ void loop() vTaskDelay(pdMS_TO_TICKS(5000)); esp_restart(); } - - const bool tasK_A_rt = task_A.start(); - delay(50); - const bool task_B_rt = task_B.start(); if (tasK_A_rt != true || task_B_rt != true) { led.setStatus(RGBled::LedStatus::ERROR); @@ -248,18 +271,23 @@ void loop() } //////// SPAWN WEBSERVER and WEBSOCKET //////// - AstroWebServer webPage(80, LittleFS); ArduinoJson::JsonDocument json_data; - bool data_a, data_b; + bool data_a = false, data_b = false; +#ifdef WEB_ENABLE + AstroWebServer webPage(80, LittleFS); + delay(1000); task_A.onMessage([&webPage, &json_data, &data_a](ignitionBoxStatusFiltered sts) { - json_data["box_a"] = sts.toJson(); - data_a = true; }); + json_data["box_a"] = sts.toJson(); + data_a = true; }); + #ifdef CH_B_RT_ENABLE task_B.onMessage([&webPage, &json_data, &data_b](ignitionBoxStatusFiltered sts) { - json_data["box_b"] = sts.toJson(); - data_b = true; }); + json_data["box_b"] = sts.toJson(); + data_b = true; }); + #endif +#endif // task_A.enableSave(true, "ignitionA_test.csv"); // task_B.enableSave(true, "ignitionB_test.csv"); @@ -270,12 +298,14 @@ void loop() while (running) { uint32_t this_loop = millis(); - if (this_loop - monitor_loop > 2000) + if (this_loop - monitor_loop > 5000) { clearScreen(); printRunningTasksMod(Serial); monitor_loop = millis(); } + vTaskDelay(pdMS_TO_TICKS(10)); +#ifdef WEB_ENABLE if ((data_a && data_b) || (this_loop - data_loop > 500)) { webPage.sendWsData(json_data.as()); @@ -283,6 +313,7 @@ void loop() data_a = data_b = false; data_loop = millis(); } +#endif } //////////////// INNER LOOP ///////////////////// } ////////////////////// MAIN LOOP ////////////////////// diff --git a/RotaxMonitor/src/tasks.cpp b/RotaxMonitor/src/tasks.cpp index e23df0c..a5b6b92 100644 --- a/RotaxMonitor/src/tasks.cpp +++ b/RotaxMonitor/src/tasks.cpp @@ -16,9 +16,16 @@ void spark_timeout_callback(void *arg) void rtIgnitionTask::rtIgnitionTask_manager(void *pvParameters) { rtIgnitionTask *cls = (rtIgnitionTask *)pvParameters; + auto last_loop = millis(); + uint32_t count(0); while (cls->m_running) { cls->run(); + // if (millis() - last_loop > 2000) { + // LOG_DEBUG("TASK [", cls->m_name.c_str(), "] Alive -", count++); + // last_loop = millis(); + // } + vTaskDelay(pdMS_TO_TICKS(1)); } } @@ -38,10 +45,12 @@ void rtIgnitionTask::rtIgnitionTask_realtime(void *pvParameters) const rtTaskInterruptParams rt_int = params->rt_int; // copy to avoid external override const rtTaskIOParams rt_rst = params->rt_io; // copy to avoid external override QueueHandle_t rt_queue = params->rt_queue; - Devices *dev = params->dev.get(); - ADS1256 *adc = params->name == "rtIgnTask_A" ? dev->m_adc_a.get() : dev->m_adc_b.get(); - std::mutex &spi_mutex = params->name == "rtIgnTask_A" ? dev->m_spi_a_mutex : dev->m_spi_b_mutex; - ExternalIO *io = dev->m_ext_io.get(); + Devices *dev = params->dev; + ExternalIO *io = dev->m_ext_io; + // ADS1256 *adc = params->name == "rtIgnTask_A" ? dev->m_adc_a : dev->m_adc_b; + ADS1256 *adc = NULL; + // std::mutex &spi_mutex = params->name == "rtIgnTask_A" ? dev->m_spi_a_mutex : dev->m_spi_b_mutex; + std::mutex spi_mutex; TaskStatus_t rt_task_info; vTaskGetInfo(NULL, &rt_task_info, pdFALSE, eInvalid); @@ -76,10 +85,6 @@ void rtIgnitionTask::rtIgnitionTask_realtime(void *pvParameters) .ign_stat = &ign_box_sts, .rt_handle_ptr = rt_task_info.xHandle}; - LOG_DEBUG("rtTask HDL Params OK, HDL* [", (uint32_t)rt_task_info.xHandle, "]"); - LOG_DEBUG("rtTask ISR Params OK, ISR* [", (uint32_t)rt_int.isr_ptr, "]"); - LOG_DEBUG("rtTask QUE Params OK, QUE* [", (uint32_t)rt_queue, "]"); - // Create esp_timer for microsecond precision timeout esp_timer_handle_t timeout_timer; esp_timer_create_args_t timer_args = { @@ -87,7 +92,11 @@ void rtIgnitionTask::rtIgnitionTask_realtime(void *pvParameters) .arg = (void *)rt_task_info.xHandle, .dispatch_method = ESP_TIMER_TASK, .name = "spark_timeout"}; - esp_timer_create(&timer_args, &timeout_timer); + if (esp_timer_create(&timer_args, &timeout_timer) != ESP_OK) + { + LOG_INFO("rtTask [", params->name.c_str(), "] Fail to allocate timeoutTimer"); + vTaskDelete(NULL); + } // Attach Pin Interrupts attachInterruptArg(digitalPinToInterrupt(rt_int.trig_pin_12p), rt_int.isr_ptr, (void *)&isr_params_t12p, RISING); @@ -242,10 +251,10 @@ void rtIgnitionTask::rtIgnitionTask_realtime(void *pvParameters) ign_box_sts.coils12.peak_n_in = adc->convertToVoltage(adc->cycleSingle()); ign_box_sts.coils34.peak_p_in = adc->convertToVoltage(adc->cycleSingle()); ign_box_sts.coils34.peak_n_in = adc->convertToVoltage(adc->cycleSingle()); - ign_box_sts.coils12.peak_p_out =adc->convertToVoltage(adc->cycleSingle()); - ign_box_sts.coils12.peak_n_out =adc->convertToVoltage(adc->cycleSingle()); - ign_box_sts.coils34.peak_p_out =adc->convertToVoltage(adc->cycleSingle()); - ign_box_sts.coils34.peak_n_out =adc->convertToVoltage(adc->cycleSingle()); + ign_box_sts.coils12.peak_p_out = adc->convertToVoltage(adc->cycleSingle()); + ign_box_sts.coils12.peak_n_out = adc->convertToVoltage(adc->cycleSingle()); + ign_box_sts.coils34.peak_p_out = adc->convertToVoltage(adc->cycleSingle()); + ign_box_sts.coils34.peak_n_out = adc->convertToVoltage(adc->cycleSingle()); ign_box_sts.adc_read_time = (int32_t)(esp_timer_get_time() - start_adc_read); adc->stopConversion(); } @@ -300,6 +309,7 @@ void rtIgnitionTask::rtIgnitionTask_realtime(void *pvParameters) ///////////// CLASS MEMBER DEFINITIONS ///////////// rtIgnitionTask::rtIgnitionTask(const rtTaskParams params, const uint32_t history_size, const uint32_t queue_size, const uint8_t core, std::mutex &fs_mutex, fs::FS &filesystem) : m_params(params), m_filesystem(filesystem), m_fs_mutex(fs_mutex), m_core(core), m_max_history(history_size) { + LOG_WARN("Starting Manager for [", m_params.name.c_str(), "]"); // create queue buffers m_queue = xQueueCreate(queue_size, sizeof(ignitionBoxStatus)); if (!m_queue) @@ -318,12 +328,12 @@ rtIgnitionTask::rtIgnitionTask(const rtTaskParams params, const uint32_t history m_active_history = std::unique_ptr(&m_history_0); m_save_history = std::unique_ptr(&m_history_1); - LOG_WARN("Starting Manager for [", m_params.name.c_str(), "]"); + m_name = (std::string("man_") + m_params.name).c_str(); // auto task_success = pdPASS; auto task_success = xTaskCreatePinnedToCore( rtIgnitionTask_manager, - (std::string("man_") + m_params.name).c_str(), - 8192, + m_name.c_str(), + RT_TASK_STACK, (void *)this, m_params.rt_priority >> 2, &m_manager_handle, @@ -362,14 +372,15 @@ void rtIgnitionTask::run() m_last_data = millis(); m_manager_status = rtTaskStatus::RUNNING; // if history buffer is full swap buffers and if enabled save history buffer - if (m_counter_status >= m_active_history->size()) + if (m_counter_status >= m_max_history) { LOG_DEBUG("Save for Buffer Full: ", m_counter_status); m_counter_status = 0; m_partial_save = false; // reset partial save flag on new data cycle std::swap(m_active_history, m_save_history); if (m_enable_save) - saveHistory(*m_save_history, m_history_path); // directly call the save task function to save without delay + // saveHistory(m_save_history, m_history_path); // directly call the save task function to save without delay + LOG_INFO("Save History"); } // update filtered data @@ -391,15 +402,14 @@ void rtIgnitionTask::run() if (m_counter_status > 0 && !m_partial_save) { LOG_DEBUG("Save Partial: ", m_counter_status); - m_active_history->resize(m_counter_status); - saveHistory(*m_active_history, m_history_path); - m_active_history->resize(m_max_history); + // m_active_history->resize(m_counter_status); + // saveHistory(m_active_history, m_history_path); + // m_active_history->resize(m_max_history); m_counter_status = 0; m_partial_save = true; } m_manager_status = rtTaskStatus::IDLE; } - delay(5); // yeld to another task } } diff --git a/RotaxMonitor/src/tasks.h b/RotaxMonitor/src/tasks.h index e5feb9e..04f0af8 100644 --- a/RotaxMonitor/src/tasks.h +++ b/RotaxMonitor/src/tasks.h @@ -41,6 +41,7 @@ static const std::map names = { class rtIgnitionTask { using PSHistory = PSRAMVector; + // using PSHistory = std::vector; public: // RT task Interrupt parameters @@ -84,7 +85,7 @@ public: const rtTaskInterruptParams rt_int; // interrupt pins to attach const rtTaskIOParams rt_io; // reset ping for peak detectors QueueHandle_t rt_queue; // queue for task io - const std::shared_ptr dev; + Devices *dev; }; enum rtTaskStatus @@ -124,6 +125,7 @@ private: // static functions for FreeRTOS private: bool m_running = true; rtTaskStatus m_manager_status = INIT; + std::string m_name; rtTaskParams m_params; const uint8_t m_core; @@ -154,6 +156,6 @@ private: static const uint32_t c_idle_time = 10000; // in mS static const uint32_t c_spark_timeout_max = 500; // uS - static const uint8_t c_adc_time = 4; // in mS - static const uint8_t c_io_time = 2; // in mS + static const uint8_t c_adc_time = 4; // in mS + static const uint8_t c_io_time = 2; // in mS }; diff --git a/RotaxMonitor/src/utils.cpp b/RotaxMonitor/src/utils.cpp index 46951fd..0474abc 100644 --- a/RotaxMonitor/src/utils.cpp +++ b/RotaxMonitor/src/utils.cpp @@ -7,7 +7,7 @@ #include "esp_heap_caps.h" #include "esp_system.h" -#include "esp_spi_flash.h" +#include "spi_flash_mmap.h" #include "esp_partition.h" #include "LittleFS.h" @@ -49,23 +49,27 @@ void printBar(Print &printer, const char *label, size_t used, size_t total, cons { float perc = total > 0 ? ((float)used / total) : 0; int filled = perc * BAR_WIDTH; + char str[256] = {0}; + uint16_t k(0); - printer.printf("%s%-12s [" COLOR_RESET, color, label); + k += sprintf(str, "%s%-12s [" COLOR_RESET, color, label); for (int i = 0; i < BAR_WIDTH; i++) { if (i < filled) - printer.printf("%s#%s", color, COLOR_RESET); + k += sprintf(&str[k], "%s#%s", color, COLOR_RESET); else - printer.printf("-"); + k += sprintf(&str[k], "-"); } - printer.printf("] %s%6.2f%%%s (%5.3f/%5.3f)MB\n", + sprintf(&str[k], "] %s%6.2f%%%s (%5.3f/%5.3f)MB\n", color, perc * 100.0, COLOR_RESET, (used / 1024.0f / 1024.0f), (total / 1024.0f / 1024.0f)); + + printer.println(str); } void printRunningTasksMod(Print &printer, std::function orderBy) @@ -95,6 +99,7 @@ void printRunningTasksMod(Print &printer, std::function 0 ? ulCurrentRunTime : 1; ulLastRunTime = ulTotalRunTime; // PRINT MEMORY INFO @@ -134,17 +139,6 @@ void printRunningTasksMod(Print &printer, std::functionsize; // dimensione reale partizione - size_t sketchSize = ESP.getSketchSize(); - printBar(printer, "FLASH APP", sketchSize, totalAPP, COLOR_CYAN); - } - else - { - printer.printf(COLOR_YELLOW "%-12s [NOT FOUND]\n" COLOR_RESET, "FLASH APP"); - } - // ===== LITTLEFS (corretto con partition table) ===== const esp_partition_t *fs_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, @@ -164,7 +158,9 @@ void printRunningTasksMod(Print &printer, std::function