From a8bb3eb557d1006b3fda0ebbf3f43eb8397cdcbe Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Tue, 28 Jun 2022 12:33:35 -0700 Subject: [PATCH 1/3] Update to newer 4.4 branch --- .gitmodules | 4 ++-- ports/espressif/esp-idf | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.gitmodules b/.gitmodules index 0edcdec653..cf0d86f3ab 100644 --- a/.gitmodules +++ b/.gitmodules @@ -145,8 +145,8 @@ url = https://github.com/adafruit/Adafruit_CircuitPython_RFM69.git [submodule "ports/espressif/esp-idf"] path = ports/espressif/esp-idf - url = https://github.com/espressif/esp-idf.git - branch = release/v4.4 + url = https://github.com/adafruit/esp-idf.git + branch = circuitpython8 [submodule "ports/espressif/certificates/nina-fw"] path = ports/espressif/certificates/nina-fw url = https://github.com/adafruit/nina-fw.git diff --git a/ports/espressif/esp-idf b/ports/espressif/esp-idf index 944c01eef4..15dc9d9809 160000 --- a/ports/espressif/esp-idf +++ b/ports/espressif/esp-idf @@ -1 +1 @@ -Subproject commit 944c01eef4fbba693f991f9d033cda3f59ca82c9 +Subproject commit 15dc9d980946059cd5287207a7f1286c523216fb From 1f065a313c6174e00356742d6d257c99d2a96474 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Wed, 29 Jun 2022 11:12:29 -0700 Subject: [PATCH 2/3] Update with startup fix instead of revert --- ports/espressif/esp-idf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/espressif/esp-idf b/ports/espressif/esp-idf index 15dc9d9809..171e5af9c5 160000 --- a/ports/espressif/esp-idf +++ b/ports/espressif/esp-idf @@ -1 +1 @@ -Subproject commit 15dc9d980946059cd5287207a7f1286c523216fb +Subproject commit 171e5af9c5b9b126c5f81f217c7b1ff931d54863 From 8dc826527563c67d5f677cafe524eff434889d1d Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Wed, 29 Jun 2022 11:53:10 -0700 Subject: [PATCH 3/3] Fix auto-wifi created crash The wifi event_handler runs on the other core so we need to be careful when calling into CP APIs. Fixes #6503 --- .../common-hal/microcontroller/__init__.c | 6 ++--- ports/espressif/common-hal/wifi/__init__.c | 22 ++++++++++++++++++- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/ports/espressif/common-hal/microcontroller/__init__.c b/ports/espressif/common-hal/microcontroller/__init__.c index 3cf41ba7f0..81a512bdd6 100644 --- a/ports/espressif/common-hal/microcontroller/__init__.c +++ b/ports/espressif/common-hal/microcontroller/__init__.c @@ -65,6 +65,7 @@ volatile uint32_t nesting_count = 0; static portMUX_TYPE cp_mutex = portMUX_INITIALIZER_UNLOCKED; void common_hal_mcu_disable_interrupts(void) { + assert(xPortGetCoreID() == CONFIG_ESP_MAIN_TASK_AFFINITY); if (nesting_count == 0) { portENTER_CRITICAL(&cp_mutex); } @@ -72,9 +73,8 @@ void common_hal_mcu_disable_interrupts(void) { } void common_hal_mcu_enable_interrupts(void) { - if (nesting_count == 0) { - // Maybe log here because it's very bad. - } + assert(xPortGetCoreID() == CONFIG_ESP_MAIN_TASK_AFFINITY); + assert(nesting_count > 0); nesting_count--; if (nesting_count > 0) { return; diff --git a/ports/espressif/common-hal/wifi/__init__.c b/ports/espressif/common-hal/wifi/__init__.c index 3da0fc7297..80d01a253e 100644 --- a/ports/espressif/common-hal/wifi/__init__.c +++ b/ports/espressif/common-hal/wifi/__init__.c @@ -42,12 +42,25 @@ wifi_radio_obj_t common_hal_wifi_radio_obj; #include "components/log/include/esp_log.h" +#include "supervisor/port.h" #include "supervisor/workflow.h" +#include "esp_ipc.h" + static const char *TAG = "wifi"; +STATIC void schedule_background_on_cp_core(void *arg) { + supervisor_workflow_request_background(); + + // CircuitPython's VM is run in a separate FreeRTOS task from wifi callbacks. So, we have to + // notify the main task every time in case it's waiting for us. + port_wake_main_task(); +} + static void event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) { + // This runs on the PRO CORE! It cannot share CP interrupt enable/disable + // directly. wifi_radio_obj_t *radio = arg; if (event_base == WIFI_EVENT) { switch (event_id) { @@ -108,7 +121,14 @@ static void event_handler(void *arg, esp_event_base_t event_base, radio->retries_left = radio->starting_retries; xEventGroupSetBits(radio->event_group_handle, WIFI_CONNECTED_BIT); } - supervisor_workflow_request_background(); + // Use IPC to ensure we run schedule background on the same core as CircuitPython. + #if defined(CONFIG_FREERTOS_UNICORE) && CONFIG_FREERTOS_UNICORE + schedule_background_on_cp_core(NULL); + #else + // This only blocks until the start of the function. That's ok since the PRO + // core shouldn't care what we do. + esp_ipc_call(CONFIG_ESP_MAIN_TASK_AFFINITY, schedule_background_on_cp_core, NULL); + #endif } static bool wifi_inited;