diff --git a/esphome/components/nrf52/__init__.py b/esphome/components/nrf52/__init__.py index 91870ba5c4..24d316b9b1 100644 --- a/esphome/components/nrf52/__init__.py +++ b/esphome/components/nrf52/__init__.py @@ -145,12 +145,9 @@ CONFIG_SCHEMA = cv.All( _detect_bootloader, ) -nrf52_ns = cg.esphome_ns.namespace("nrf52") - @coroutine_with_priority(1000) async def to_code(config): - cg.add(nrf52_ns.setup_preferences()) cg.add_platformio_option("board", config[CONF_BOARD]) cg.add_build_flag("-DUSE_NRF52") cg.add_define("ESPHOME_BOARD", config[CONF_BOARD]) diff --git a/esphome/components/nrf52/core.h b/esphome/components/nrf52/core.h deleted file mode 100644 index 77a6c09b26..0000000000 --- a/esphome/components/nrf52/core.h +++ /dev/null @@ -1,8 +0,0 @@ -#pragma once -#include - -namespace esphome { - -void nrf52GetMacAddr(uint8_t *mac); - -} diff --git a/esphome/components/nrf52/gpio.h b/esphome/components/nrf52/gpio.h deleted file mode 100644 index 65de0c01fe..0000000000 --- a/esphome/components/nrf52/gpio.h +++ /dev/null @@ -1,11 +0,0 @@ -#pragma once - -#include "esphome/components/zephyr/gpio.h" - -namespace esphome { -namespace nrf52 { - -class NRF52GPIOPin : public zephyr::NRF52GPIOPin {}; - -} // namespace nrf52 -} // namespace esphome diff --git a/esphome/components/nrf52/gpio.py b/esphome/components/nrf52/gpio.py index 8aa311fb6b..0357edfe89 100644 --- a/esphome/components/nrf52/gpio.py +++ b/esphome/components/nrf52/gpio.py @@ -8,9 +8,11 @@ from esphome.const import ( CONF_INVERTED, CONF_NUMBER, ) +from esphome.components.zephyr.const import ( + zephyr_ns, +) -nrf52_ns = cg.esphome_ns.namespace("nrf52") -NRF52GPIOPin = nrf52_ns.class_("NRF52GPIOPin", cg.InternalGPIOPin) +GPIOPin = zephyr_ns.class_("GPIOPin", cg.InternalGPIOPin) def _translate_pin(value): @@ -41,9 +43,7 @@ def validate_gpio_pin(value): NRF52_PIN_SCHEMA = cv.All( - pins.gpio_base_schema( - NRF52GPIOPin, validate_gpio_pin, modes=pins.GPIO_STANDARD_MODES - ), + pins.gpio_base_schema(GPIOPin, validate_gpio_pin, modes=pins.GPIO_STANDARD_MODES), ) diff --git a/esphome/components/zephyr/__init__.py b/esphome/components/zephyr/__init__.py index 9cfd617122..e802e8b680 100644 --- a/esphome/components/zephyr/__init__.py +++ b/esphome/components/zephyr/__init__.py @@ -10,6 +10,7 @@ from .const import ( KEY_ZEPHYR, KEY_PRJ_CONF, KEY_OVERLAY, + zephyr_ns, ) from esphome.const import ( CONF_VARIANT, @@ -48,6 +49,7 @@ def zephyr_add_overlay(content): def zephyr_to_code(conf): + cg.add(zephyr_ns.setup_preferences()) cg.add_build_flag("-DUSE_ZEPHYR") if conf[CONF_VARIANT] == ZEPHYR_VARIANT_GENERIC: cg.add_platformio_option( diff --git a/esphome/components/zephyr/const.py b/esphome/components/zephyr/const.py index 886fea022e..acbf83d4f3 100644 --- a/esphome/components/zephyr/const.py +++ b/esphome/components/zephyr/const.py @@ -1,5 +1,9 @@ +import esphome.codegen as cg + ZEPHYR_VARIANT_GENERIC = "generic" ZEPHYR_VARIANT_NRF_SDK = "nrf-sdk" KEY_ZEPHYR = "zephyr" KEY_PRJ_CONF = "prj_conf" KEY_OVERLAY = "overlay" + +zephyr_ns = cg.esphome_ns.namespace("zephyr") diff --git a/esphome/components/zephyr/gpio.cpp b/esphome/components/zephyr/gpio.cpp index 8b79c03937..e2e55c045b 100644 --- a/esphome/components/zephyr/gpio.cpp +++ b/esphome/components/zephyr/gpio.cpp @@ -38,18 +38,18 @@ struct ISRPinArg { bool inverted; }; -ISRInternalGPIOPin NRF52GPIOPin::to_isr() const { +ISRInternalGPIOPin GPIOPin::to_isr() const { auto *arg = new ISRPinArg{}; arg->pin = pin_; arg->inverted = inverted_; return ISRInternalGPIOPin((void *) arg); } -void NRF52GPIOPin::attach_interrupt(void (*func)(void *), void *arg, gpio::InterruptType type) const { +void GPIOPin::attach_interrupt(void (*func)(void *), void *arg, gpio::InterruptType type) const { // TODO } -void NRF52GPIOPin::setup() { +void GPIOPin::setup() { const struct device *gpio = nullptr; if (pin_ < 32) { #define GPIO0 DT_NODELABEL(gpio0) @@ -75,27 +75,27 @@ void NRF52GPIOPin::setup() { pin_mode(flags_); } -void NRF52GPIOPin::pin_mode(gpio::Flags flags) { +void GPIOPin::pin_mode(gpio::Flags flags) { if (nullptr == gpio_) { return; } gpio_pin_configure(gpio_, pin_, flags_to_mode(flags, pin_, inverted_, value_)); } -std::string NRF52GPIOPin::dump_summary() const { +std::string GPIOPin::dump_summary() const { char buffer[32]; snprintf(buffer, sizeof(buffer), "GPIO%u", pin_); return buffer; } -bool NRF52GPIOPin::digital_read() { +bool GPIOPin::digital_read() { if (nullptr == gpio_) { return false; } return bool(gpio_pin_get(gpio_, pin_) != inverted_); } -void NRF52GPIOPin::digital_write(bool value) { +void GPIOPin::digital_write(bool value) { // make sure that value is not ignored since it can be inverted e.g. on switch side // that way init state should be correct value_ = value; @@ -104,7 +104,7 @@ void NRF52GPIOPin::digital_write(bool value) { } gpio_pin_set(gpio_, pin_, value != inverted_ ? 1 : 0); } -void NRF52GPIOPin::detach_interrupt() const { +void GPIOPin::detach_interrupt() const { // TODO } diff --git a/esphome/components/zephyr/gpio.h b/esphome/components/zephyr/gpio.h index 4ec930e0c4..1054e568e0 100644 --- a/esphome/components/zephyr/gpio.h +++ b/esphome/components/zephyr/gpio.h @@ -6,7 +6,7 @@ struct device; namespace esphome { namespace zephyr { -class NRF52GPIOPin : public InternalGPIOPin { +class GPIOPin : public InternalGPIOPin { public: void set_pin(uint8_t pin) { pin_ = pin; } void set_inverted(bool inverted) { inverted_ = inverted; } diff --git a/esphome/components/nrf52/preferences.cpp b/esphome/components/zephyr/preferences.cpp similarity index 82% rename from esphome/components/nrf52/preferences.cpp rename to esphome/components/zephyr/preferences.cpp index 5b3030011e..bbb4d762de 100644 --- a/esphome/components/nrf52/preferences.cpp +++ b/esphome/components/zephyr/preferences.cpp @@ -1,11 +1,11 @@ -#ifdef USE_NRF52 +#ifdef USE_ZEPHYR #include "esphome/core/preferences.h" namespace esphome { -namespace nrf52 { +namespace zephyr { -class NRF52Preferences : public ESPPreferences { +class Preferences : public ESPPreferences { public: ESPPreferenceObject make_preference(size_t length, uint32_t type, bool in_flash) override { return make_preference(length, type); @@ -31,7 +31,7 @@ class NRF52Preferences : public ESPPreferences { }; void setup_preferences() { - auto *prefs = new NRF52Preferences(); // NOLINT(cppcoreguidelines-owning-memory) + auto *prefs = new Preferences(); // NOLINT(cppcoreguidelines-owning-memory) global_preferences = prefs; } // TODO diff --git a/esphome/components/nrf52/preferences.h b/esphome/components/zephyr/preferences.h similarity index 73% rename from esphome/components/nrf52/preferences.h rename to esphome/components/zephyr/preferences.h index 4eb42d93e8..a85a704acf 100644 --- a/esphome/components/nrf52/preferences.h +++ b/esphome/components/zephyr/preferences.h @@ -1,15 +1,15 @@ #pragma once -#ifdef USE_NRF52 +#ifdef USE_ZEPHYR namespace esphome { -namespace nrf52 { +namespace zephyr { void setup_preferences(); // TODO // void preferences_prevent_write(bool prevent); -} // namespace nrf52 +} // namespace zephyr } // namespace esphome #endif // USE_RP2040 diff --git a/esphome/core/helpers.cpp b/esphome/core/helpers.cpp index 7cca994499..7b8ea9419b 100644 --- a/esphome/core/helpers.cpp +++ b/esphome/core/helpers.cpp @@ -48,12 +48,6 @@ #include // for macAddress() #endif -#ifdef USE_NRF52 -#ifdef USE_ARDUINO -#include "Adafruit_nRFCrypto.h" -#endif -#include "esphome/components/nrf52/core.h" -#endif #ifdef USE_ZEPHYR #include #endif @@ -212,7 +206,7 @@ uint32_t random_uint32() { std::mt19937 rng(dev()); std::uniform_int_distribution dist(0, std::numeric_limits::max()); return dist(rng); -#elif defined(USE_NRF52) +#elif defined(USE_ZEPHYR) return rand(); #else #error "No random source available for this configuration." @@ -251,8 +245,6 @@ bool random_bytes(uint8_t *data, size_t len) { } fclose(fp); return true; -#elif defined(USE_NRF52) && defined(USE_ARDUINO) - return nRFCrypto.Random.generate(data, len); #elif defined(USE_ZEPHYR) sys_rand_get(data, len); return true; @@ -538,7 +530,7 @@ Mutex::Mutex() { k_mutex_init(&handle_); } void Mutex::lock() { k_mutex_lock(&this->handle_, K_FOREVER); } bool Mutex::try_lock() { return k_mutex_lock(&this->handle_, K_NO_WAIT) == 0; } void Mutex::unlock() { k_mutex_unlock(&this->handle_); } -#elif defined(USE_ESP32) || defined(USE_LIBRETINY) || defined(USE_NRF52) +#elif defined(USE_ESP32) || defined(USE_LIBRETINY) Mutex::Mutex() { handle_ = xSemaphoreCreateMutex(); } void Mutex::lock() { xSemaphoreTake(this->handle_, portMAX_DELAY); } bool Mutex::try_lock() { return xSemaphoreTake(this->handle_, 0) == pdTRUE; } @@ -592,8 +584,6 @@ void get_mac_address_raw(uint8_t *mac) { // NOLINT(readability-non-const-parame WiFi.macAddress(mac); #elif defined(USE_LIBRETINY) WiFi.macAddress(mac); -#elif defined(USE_NRF52) - nrf52GetMacAddr(mac); #endif } std::string get_mac_address() { diff --git a/esphome/core/helpers.h b/esphome/core/helpers.h index e973dc040d..a3a71db73d 100644 --- a/esphome/core/helpers.h +++ b/esphome/core/helpers.h @@ -21,8 +21,6 @@ #elif defined(USE_LIBRETINY) #include #include -#elif defined(USE_NRF52) && defined(USE_ARDUINO) -#include #elif defined(USE_ZEPHYR) #include #endif @@ -553,7 +551,7 @@ class Mutex { private: #if defined(USE_ZEPHYR) k_mutex handle_; -#elif defined(USE_ESP32) || defined(USE_LIBRETINY) || defined(USE_NRF52) +#elif defined(USE_ESP32) || defined(USE_LIBRETINY) SemaphoreHandle_t handle_; #endif };