Skip to content

Commit

Permalink
Move gpio_install_isr_service to esp32_shared
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidSchinazi committed Dec 19, 2024
1 parent 0a341ff commit 7dd8bb8
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
20 changes: 16 additions & 4 deletions src/jazzlights/esp32_shared.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,30 @@

#ifdef ESP32

#include <driver/gpio.h>
#include <esp_event.h>
#include <esp_netif.h>

#define JL_RUN_ONCE(_commands) \
do { \
static const bool _run_once = []() { \
{ _commands } \
return true; \
}(); \
(void)_run_once; \
} while (false)

namespace jazzlights {

void InitializeNetStack() {
static const bool run_once = []() {
JL_RUN_ONCE({
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
return true;
}();
(void)run_once;
});
}

void InstallGpioIsrService() {
JL_RUN_ONCE({ ESP_ERROR_CHECK(gpio_install_isr_service(/*intr_alloc_flags=*/0)); });
}

} // namespace jazzlights
Expand Down
2 changes: 2 additions & 0 deletions src/jazzlights/esp32_shared.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ namespace jazzlights {

void InitializeNetStack();

void InstallGpioIsrService();

} // namespace jazzlights

#endif // ESP32
Expand Down
2 changes: 1 addition & 1 deletion src/jazzlights/networks/esp32_ethernet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ Esp32EthernetNetwork::Esp32EthernetNetwork()
phy_config_spi.autonego_timeout_ms = 0;
phy_config_spi.reset_gpio_num = -1;

gpio_install_isr_service(0);
InstallGpioIsrService();

// Init SPI bus
spi_bus_config_t buscfg = {
Expand Down
5 changes: 2 additions & 3 deletions src/jazzlights/ui/gpio_button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>

#include "jazzlights/esp32_shared.h"
#include "jazzlights/util/log.h"

#define JL_GPIO_DEBUG_ENABLED 0
Expand Down Expand Up @@ -48,9 +49,7 @@ GpioPin::GpioPin(uint8_t pin, PinInterface& pinInterface, int64_t debounceDurati
pin_(pin),
debounceDuration_(debounceDuration) {
if (queue_ == nullptr) { jll_fatal("Failed to create GpioPin queue"); }
// Ensure this is only ever called once by saving the result in a static variable.
static esp_err_t err = gpio_install_isr_service(/*intr_alloc_flags=*/0);
ESP_ERROR_CHECK(err);
InstallGpioIsrService();
// GPIO interrupt handlers are run on the core where the config calls were made, so we use esp_ipc_call to
// ensure that all that happens on core 0. This prevents it from interfering with LED writes on core 1.
ESP_ERROR_CHECK(esp_ipc_call(/*coreID=*/0, ConfigurePin, /*arg=*/this));
Expand Down

0 comments on commit 7dd8bb8

Please sign in to comment.