From 6c42cbfb4b9f0c8b86db614c3f46b6f1f10fbed5 Mon Sep 17 00:00:00 2001 From: MichaelDvP Date: Sat, 28 Jun 2025 13:00:53 +0200 Subject: [PATCH] make task cores configurable in platformio.ini --- lib/espMqttClient/src/MqttClient.cpp | 2 +- platformio.ini | 9 ++++++--- src/ESP32React/MqttSettingsService.cpp | 4 ++-- src/ESP32React/MqttSettingsService.h | 13 +++++++++++++ src/uart/emsuart_esp32.cpp | 15 +++++++++++---- src/uart/emsuart_esp32.h | 21 ++++++++++++++++----- src/web/WebSchedulerService.cpp | 7 ++++++- src/web/WebSchedulerService.h | 12 ++++++++++++ 8 files changed, 67 insertions(+), 16 deletions(-) diff --git a/lib/espMqttClient/src/MqttClient.cpp b/lib/espMqttClient/src/MqttClient.cpp index dc21f7456..a5d498e2c 100644 --- a/lib/espMqttClient/src/MqttClient.cpp +++ b/lib/espMqttClient/src/MqttClient.cpp @@ -74,7 +74,7 @@ MqttClient::MqttClient(espMqttClientTypes::UseInternalTask useInternalTask, uint MqttClient::~MqttClient() { disconnect(true); - _clearQueue(2); + clearQueue(true); #if defined(ARDUINO_ARCH_ESP32) vSemaphoreDelete(_xSemaphore); if (_useInternalTask == espMqttClientTypes::UseInternalTask::YES) { diff --git a/platformio.ini b/platformio.ini index 940ff7f94..f36a1f520 100644 --- a/platformio.ini +++ b/platformio.ini @@ -39,11 +39,14 @@ build_flags = -D CONFIG_ASYNC_TCP_MAX_ACK_TIME=5000 ; default -D CONFIG_ASYNC_TCP_PRIORITY=10 ; default -D CONFIG_ASYNC_TCP_QUEUE_SIZE=64 ; default - -D CONFIG_ASYNC_TCP_RUNNING_CORE=0 ; force async_tcp task to be on other core as Arduino app (default is any core) + -D CONFIG_ASYNC_TCP_RUNNING_CORE=1 ; force async_tcp task to be on same core as Arduino app (default is any core) -D CONFIG_ASYNC_TCP_STACK_SIZE=6144 ; stack usage measured: ESP32: ~2.3K, ESP32S3: ~3.5k - (default is 16K) ; ESPAsyncWebServer -D WS_MAX_QUEUED_MESSAGES=0 ; log messages are already queued in ems-esp -D CORE_DEBUG_LEVEL=0 + -D EMSESP_SCHEDULER_RUNNING_CORE=1 + ; -D EMSESP_SCHEDULER_STACKSIZE=6144 + -D EMSESP_MQTT_RUNNING_CORE=1 unbuild_flags = @@ -101,10 +104,10 @@ monitor_filters = direct build_type = release board_build.filesystem = littlefs lib_deps = - bblanchon/ArduinoJson @ 7.4.1 + bblanchon/ArduinoJson @ 7.4.2 ESP32Async/AsyncTCP @ 3.4.2 ESP32Async/ESPAsyncWebServer @ 3.7.8 - https://github.com/emsesp/EMS-ESP-Modules.git @ 1.0.7 + https://github.com/MichaelDvP/EMS-ESP-Modules.git @ 1.0.8 ; ; Builds for different board types diff --git a/src/ESP32React/MqttSettingsService.cpp b/src/ESP32React/MqttSettingsService.cpp index f6fb78872..5514af3b4 100644 --- a/src/ESP32React/MqttSettingsService.cpp +++ b/src/ESP32React/MqttSettingsService.cpp @@ -44,7 +44,7 @@ void MqttSettingsService::startClient() { if (_state.enableTLS) { isSecure = true; if (emsesp::EMSESP::system_.PSram() > 0) { - _mqttClient = new espMqttClientSecure(espMqttClientTypes::UseInternalTask::YES); + _mqttClient = new espMqttClientSecure(EMSESP_MQTT_PRIORITY, EMSESP_MQTT_RUNNING_CORE); } else { _mqttClient = new espMqttClientSecure(espMqttClientTypes::UseInternalTask::NO); } @@ -64,7 +64,7 @@ void MqttSettingsService::startClient() { #endif isSecure = false; if (emsesp::EMSESP::system_.PSram() > 0) { - _mqttClient = new espMqttClient(espMqttClientTypes::UseInternalTask::YES); + _mqttClient = new espMqttClient(EMSESP_MQTT_PRIORITY, EMSESP_MQTT_RUNNING_CORE); } else { _mqttClient = new espMqttClient(espMqttClientTypes::UseInternalTask::NO); } diff --git a/src/ESP32React/MqttSettingsService.h b/src/ESP32React/MqttSettingsService.h index ef448af0d..2c4871a50 100644 --- a/src/ESP32React/MqttSettingsService.h +++ b/src/ESP32React/MqttSettingsService.h @@ -50,6 +50,19 @@ #define FACTORY_MQTT_MAX_TOPIC_LENGTH 128 #endif +#ifndef EMSESP_MQTT_RUNNING_CORE +#define EMSESP_MQTT_RUNNING_CORE 1 +#endif + +#ifdef EMSESP_MQTT_STACKSIZE +#undef EMC_TASK_STACK_SIZE +#define EMC_TASK_STACK_SIZE EMSESP_MQTT_STACKSIZE +#endif + +#ifndef EMSESP_MQTT_PRIORITY +#define EMSESP_MQTT_PRIORITY 1 +#endif + class MqttSettings { public: bool enabled; diff --git a/src/uart/emsuart_esp32.cpp b/src/uart/emsuart_esp32.cpp index bf13dfd4a..82fafb78a 100644 --- a/src/uart/emsuart_esp32.cpp +++ b/src/uart/emsuart_esp32.cpp @@ -33,6 +33,7 @@ namespace emsesp { uint8_t EMSuart::last_tx_src_ = 0; +static TaskHandle_t xHandle; static QueueHandle_t uart_queue; uint8_t tx_mode_ = 0xFF; uint32_t inverse_mask = 0; @@ -94,12 +95,18 @@ void EMSuart::start(const uint8_t tx_mode, const uint8_t rx_gpio, const uint8_t uart_param_config(EMSUART_NUM, &uart_config); uart_set_pin(EMSUART_NUM, tx_gpio, rx_gpio, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE); uart_set_line_inverse(EMSUART_NUM, inverse_mask); - uart_driver_install(EMSUART_NUM, 129, 0, (EMS_MAXBUFFERSIZE + 1) * 2, &uart_queue, 0); // buffer must be > fifo + uart_driver_install(EMSUART_NUM, UART_FIFO_LEN + 1, 0, (EMS_MAXBUFFERSIZE + 1) * 2, &uart_queue, 0); // buffer must be > fifo uart_set_rx_full_threshold(EMSUART_NUM, 1); uart_set_rx_timeout(EMSUART_NUM, 0); // disable // note esp32s3 crashes with 2k stacksize, stack overflow here sometimes wipes settingsfiles. - xTaskCreate(uart_event_task, "uart_event_task", 2560, NULL, configMAX_PRIORITIES - 1, NULL); +#if defined(CONFIG_FREERTOS_UNICORE) || (EMSESP_UART_RUNNING_CORE < 0) + xTaskCreate(uart_event_task, "uart_event_task", EMSESP_UART_STACKSIZE, NULL, EMSESP_UART_PRIORITY, &xHandle); +#else + xTaskCreatePinnedToCore(uart_event_task, "uart_event_task", EMSESP_UART_STACKSIZE, NULL, EMSESP_UART_PRIORITY, &xHandle, EMSESP_UART_RUNNING_CORE); +#endif + } else { + vTaskResume(xHandle); } tx_mode_ = tx_mode; uart_enable_intr_mask(EMSUART_NUM, UART_BRK_DET_INT_ENA | UART_RXFIFO_FULL_INT_ENA); @@ -111,7 +118,7 @@ void EMSuart::start(const uint8_t tx_mode, const uint8_t rx_gpio, const uint8_t void EMSuart::stop() { if (tx_mode_ != 0xFF) { // only call after driver initialisation uart_disable_intr_mask(EMSUART_NUM, UART_BRK_DET_INT_ENA | UART_RXFIFO_FULL_INT_ENA); - // TODO should we xTaskSuspend() the event task here? + vTaskSuspend(xHandle); } }; @@ -139,7 +146,7 @@ void EMSuart::send_poll(const uint8_t data) { * buf contains the CRC and len is #bytes including the CRC * returns code, 1=success */ -uint16_t EMSuart::transmit(const uint8_t * buf, const uint8_t len) { +uint8_t EMSuart::transmit(const uint8_t * buf, const uint8_t len) { if (len == 0 || len >= EMS_MAXBUFFERSIZE) { return EMS_TX_STATUS_ERR; } diff --git a/src/uart/emsuart_esp32.h b/src/uart/emsuart_esp32.h index e2b5cd643..620139e66 100644 --- a/src/uart/emsuart_esp32.h +++ b/src/uart/emsuart_esp32.h @@ -24,6 +24,17 @@ #ifndef EMSESP_EMSUART_H #define EMSESP_EMSUART_H +#ifndef EMSESP_UART_RUNNING_CORE +#define EMSESP_UART_RUNNING_CORE -1 +#endif + +#ifndef EMSESP_UART_STACKSIZE +#define EMSESP_UART_STACKSIZE 2560 +#endif + +#ifndef EMSESP_UART_PRIORITY +#define EMSESP_UART_PRIORITY configMAX_PRIORITIES - 1 +#endif #define EMS_MAXBUFFERSIZE 33 // max size of the buffer. EMS packets are max 32 bytes, plus extra for BRK #define EMSUART_NUM UART_NUM_1 // on C3 and S2 there is no UART2, use UART1 for all @@ -61,11 +72,11 @@ class EMSuart { EMSuart() = default; ~EMSuart() = default; - static void start(const uint8_t tx_mode, const uint8_t rx_gpio, const uint8_t tx_gpio); - static void send_poll(const uint8_t data); - static void stop(); - static uint16_t transmit(const uint8_t * buf, const uint8_t len); - static uint8_t last_tx_src() { + static void start(const uint8_t tx_mode, const uint8_t rx_gpio, const uint8_t tx_gpio); + static void send_poll(const uint8_t data); + static void stop(); + static uint8_t transmit(const uint8_t * buf, const uint8_t len); + static uint8_t last_tx_src() { return last_tx_src_; } diff --git a/src/web/WebSchedulerService.cpp b/src/web/WebSchedulerService.cpp index ef519cc31..d2fa168bd 100644 --- a/src/web/WebSchedulerService.cpp +++ b/src/web/WebSchedulerService.cpp @@ -40,7 +40,12 @@ void WebSchedulerService::begin() { Mqtt::subscribe(EMSdevice::DeviceType::SCHEDULER, topic, nullptr); // use empty function callback #ifndef EMSESP_STANDALONE if (EMSESP::system_.PSram()) { - xTaskCreate((TaskFunction_t)scheduler_task, "scheduler_task", 5120, NULL, 1, NULL); +#if defined(CONFIG_FREERTOS_UNICORE) || (EMSESP_SCHEDULER_RUNNING_CORE < 0) + xTaskCreate((TaskFunction_t)scheduler_task, "scheduler_task", EMSESP_SCHEDULER_STACKSIZE, NULL, EMSESP_SCHEDULER_PRIORITY, NULL); +#else + xTaskCreatePinnedToCore( + (TaskFunction_t)scheduler_task, "scheduler_task", EMSESP_SCHEDULER_STACKSIZE, NULL, EMSESP_SCHEDULER_PRIORITY, NULL, EMSESP_SCHEDULER_RUNNING_CORE); +#endif } #endif } diff --git a/src/web/WebSchedulerService.h b/src/web/WebSchedulerService.h index 3cd1b8c88..8b40063b5 100644 --- a/src/web/WebSchedulerService.h +++ b/src/web/WebSchedulerService.h @@ -22,6 +22,18 @@ #define EMSESP_SCHEDULER_FILE "/config/emsespScheduler.json" #define EMSESP_SCHEDULER_SERVICE_PATH "/rest/schedule" // GET and POST +#ifndef EMSESP_SCHEDULER_RUNNING_CORE +#define EMSESP_SCHEDULER_RUNNING_CORE 1 +#endif + +#ifndef EMSESP_SCHEDULER_STACKSIZE +#define EMSESP_SCHEDULER_STACKSIZE 5120 +#endif + +#ifndef EMSESP_SCHEDULER_PRIORITY +#define EMSESP_SCHEDULER_PRIORITY 1 +#endif + // bit flags for the schedule items. Matches those in interface/src/app/main/SchedulerDialog.tsx // 0-127 (0->0x7F) is day schedule // 128/0x80 is timer