From c22975fffe758da27b1ab1191908903803f15efe Mon Sep 17 00:00:00 2001 From: Tomasz Duda Date: Thu, 15 Feb 2024 23:23:41 +0100 Subject: [PATCH] add missing immplementation --- esphome/components/nrf52/__init__.py | 2 + esphome/components/nrf52/core_zephyr.cpp | 49 ----------------- esphome/components/nrf52/gpio.h | 15 +++--- esphome/components/zephyr/__init__.py | 8 +-- esphome/components/zephyr/core.cpp | 53 +++++++++++++++++++ .../gpio_zephyr.cpp => zephyr/gpio.cpp} | 6 +-- esphome/components/zephyr/gpio.h | 37 +++++++++++++ esphome/core/automation.h | 6 +-- esphome/core/helpers.cpp | 22 +++++--- esphome/core/helpers.h | 11 ++-- 10 files changed, 127 insertions(+), 82 deletions(-) delete mode 100644 esphome/components/nrf52/core_zephyr.cpp create mode 100644 esphome/components/zephyr/core.cpp rename esphome/components/{nrf52/gpio_zephyr.cpp => zephyr/gpio.cpp} (97%) create mode 100644 esphome/components/zephyr/gpio.h diff --git a/esphome/components/nrf52/__init__.py b/esphome/components/nrf52/__init__.py index a674294987..14b44a1ca7 100644 --- a/esphome/components/nrf52/__init__.py +++ b/esphome/components/nrf52/__init__.py @@ -34,6 +34,8 @@ from .const import ( # force import gpio to register pin schema from .gpio import nrf52_pin_to_code # noqa +AUTO_LOAD = ["zephyr"] + def set_core_data(config): zephyr_set_core_data(config) diff --git a/esphome/components/nrf52/core_zephyr.cpp b/esphome/components/nrf52/core_zephyr.cpp deleted file mode 100644 index 6ed759199a..0000000000 --- a/esphome/components/nrf52/core_zephyr.cpp +++ /dev/null @@ -1,49 +0,0 @@ -#ifdef USE_NRF52 -#ifdef USE_ZEPHYR - -#include -#include - -namespace esphome { -void yield() { ::k_yield(); } -uint32_t millis() { return k_ticks_to_ms_floor32(k_uptime_ticks()); } -void delay(uint32_t ms) { ::k_msleep(ms); } -uint32_t micros() { return k_ticks_to_us_floor32(k_uptime_ticks()); } - -void arch_init() { - // TODO -} -void arch_feed_wdt() { - // TODO -} - -void arch_restart() { - // TODO -} - -void nrf52GetMacAddr(uint8_t *mac) { - const uint8_t *src = (const uint8_t *) NRF_FICR->DEVICEADDR; - mac[5] = src[0]; - mac[4] = src[1]; - mac[3] = src[2]; - mac[2] = src[3]; - mac[1] = src[4]; - mac[0] = src[5] | 0xc0; // MSB high two bits get set elsewhere in the bluetooth stack -} - -} // namespace esphome - -void setup(); -void loop(); - -int main() { - setup(); - while (1) { - loop(); - esphome::yield(); - } - return 0; -} - -#endif -#endif // USE_RP2040 diff --git a/esphome/components/nrf52/gpio.h b/esphome/components/nrf52/gpio.h index 7dc8a3ee57..4abaf62e50 100644 --- a/esphome/components/nrf52/gpio.h +++ b/esphome/components/nrf52/gpio.h @@ -1,13 +1,14 @@ #pragma once -#ifdef USE_NRF52 #include "esphome/core/hal.h" #ifdef USE_ZEPHYR -struct device; +#include "esphome/components/zephyr/gpio.h" #endif + namespace esphome { namespace nrf52 { +#ifdef USE_ARDUINO class NRF52GPIOPin : public InternalGPIOPin { public: void set_pin(uint8_t pin) { pin_ = pin; } @@ -29,13 +30,9 @@ class NRF52GPIOPin : public InternalGPIOPin { uint8_t pin_; bool inverted_; gpio::Flags flags_; -#ifdef USE_ZEPHYR - const device *gpio_ = nullptr; - bool value_ = false; -#endif }; - +#else +class NRF52GPIOPin : public zephyr::NRF52GPIOPin {}; +#endif // USE_ARDUINO } // namespace nrf52 } // namespace esphome - -#endif // USE_NRF52 diff --git a/esphome/components/zephyr/__init__.py b/esphome/components/zephyr/__init__.py index a2d1cbc5ec..9cfd617122 100644 --- a/esphome/components/zephyr/__init__.py +++ b/esphome/components/zephyr/__init__.py @@ -82,12 +82,12 @@ def zephyr_to_code(conf): zephyr_add_prj_conf("DEBUG_THREAD_INFO", True) # zephyr_add_prj_conf("DEBUG", True) ### - # zephyr_add_prj_conf("USE_SEGGER_RTT", True) - # zephyr_add_prj_conf("RTT_CONSOLE", True) - # zephyr_add_prj_conf("LOG", True) + zephyr_add_prj_conf("USE_SEGGER_RTT", True) + zephyr_add_prj_conf("RTT_CONSOLE", True) + zephyr_add_prj_conf("LOG", True) # zephyr_add_prj_conf("USB_DEVICE_LOG_LEVEL_ERR", True) - # zephyr_add_prj_conf("USB_CDC_ACM_LOG_LEVEL_DBG", True) + zephyr_add_prj_conf("USB_CDC_ACM_LOG_LEVEL_WRN", True) def _format_prj_conf_val(value: PrjConfValueType) -> str: diff --git a/esphome/components/zephyr/core.cpp b/esphome/components/zephyr/core.cpp new file mode 100644 index 0000000000..29b1684276 --- /dev/null +++ b/esphome/components/zephyr/core.cpp @@ -0,0 +1,53 @@ +#ifdef USE_ZEPHYR + +#include +#include +#include + +namespace esphome { + +static int wdt_channel_id = -EINVAL; +const device * wdt = nullptr; + +void yield() { ::k_yield(); } +uint32_t millis() { return k_ticks_to_ms_floor32(k_uptime_ticks()); } +void delay(uint32_t ms) { ::k_msleep(ms); } +uint32_t micros() { return k_ticks_to_us_floor32(k_uptime_ticks()); } + +void arch_init() { + wdt = DEVICE_DT_GET(DT_ALIAS(watchdog0)); + + if (device_is_ready(wdt)) { + static wdt_timeout_cfg wdt_config{}; + wdt_config.flags = WDT_FLAG_RESET_SOC; + wdt_config.window.max = 2000; + wdt_channel_id = wdt_install_timeout(wdt, &wdt_config); + if (wdt_channel_id >= 0) { + wdt_setup(wdt, WDT_OPT_PAUSE_HALTED_BY_DBG); + } + } +} + +void arch_feed_wdt() { + if (wdt_channel_id >= 0) { + wdt_feed(wdt, wdt_channel_id); + } +} + +void arch_restart() { sys_reboot(SYS_REBOOT_COLD); } + +} // namespace esphome + +void setup(); +void loop(); + +int main() { + setup(); + while (1) { + loop(); + esphome::yield(); + } + return 0; +} + +#endif diff --git a/esphome/components/nrf52/gpio_zephyr.cpp b/esphome/components/zephyr/gpio.cpp similarity index 97% rename from esphome/components/nrf52/gpio_zephyr.cpp rename to esphome/components/zephyr/gpio.cpp index 6814f27b7c..8b79c03937 100644 --- a/esphome/components/nrf52/gpio_zephyr.cpp +++ b/esphome/components/zephyr/gpio.cpp @@ -1,11 +1,10 @@ -#ifdef USE_NRF52 #ifdef USE_ZEPHYR #include "gpio.h" #include "esphome/core/log.h" #include namespace esphome { -namespace nrf52 { +namespace zephyr { static const char *const TAG = "nrf52"; @@ -109,7 +108,7 @@ void NRF52GPIOPin::detach_interrupt() const { // TODO } -} // namespace nrf52 +} // namespace zephyr bool IRAM_ATTR ISRInternalGPIOPin::digital_read() { // TODO @@ -119,4 +118,3 @@ bool IRAM_ATTR ISRInternalGPIOPin::digital_read() { } // namespace esphome #endif -#endif // USE_NRF52 diff --git a/esphome/components/zephyr/gpio.h b/esphome/components/zephyr/gpio.h new file mode 100644 index 0000000000..4ec930e0c4 --- /dev/null +++ b/esphome/components/zephyr/gpio.h @@ -0,0 +1,37 @@ +#pragma once + +#ifdef USE_ZEPHYR +#include "esphome/core/hal.h" +struct device; +namespace esphome { +namespace zephyr { + +class NRF52GPIOPin : public InternalGPIOPin { + public: + void set_pin(uint8_t pin) { pin_ = pin; } + void set_inverted(bool inverted) { inverted_ = inverted; } + void set_flags(gpio::Flags flags) { flags_ = flags; } + + void setup() override; + void pin_mode(gpio::Flags flags) override; + bool digital_read() override; + void digital_write(bool value) override; + std::string dump_summary() const override; + void detach_interrupt() const override; + ISRInternalGPIOPin to_isr() const override; + uint8_t get_pin() const override { return pin_; } + bool is_inverted() const override { return inverted_; } + + protected: + void attach_interrupt(void (*func)(void *), void *arg, gpio::InterruptType type) const override; + uint8_t pin_; + bool inverted_; + gpio::Flags flags_; + const device *gpio_ = nullptr; + bool value_ = false; +}; + +} // namespace zephyr +} // namespace esphome + +#endif // USE_ZEPHYR diff --git a/esphome/core/automation.h b/esphome/core/automation.h index 84c754e081..f9f1b1255e 100644 --- a/esphome/core/automation.h +++ b/esphome/core/automation.h @@ -24,7 +24,7 @@ template struct gens<0, S...> { using type = seq; }; // NOLINT template class TemplatableValue { public: - TemplatableValue() : type_(EMPTY) {} + TemplatableValue() : type_(ESPHOME_EMPTY) {} template::value, int> = 0> TemplatableValue(F value) : type_(VALUE), value_(value) {} @@ -32,7 +32,7 @@ template class TemplatableValue { template::value, int> = 0> TemplatableValue(F f) : type_(LAMBDA), f_(f) {} - bool has_value() { return this->type_ != EMPTY; } + bool has_value() { return this->type_ != ESPHOME_EMPTY; } T value(X... x) { if (this->type_ == LAMBDA) { @@ -58,7 +58,7 @@ template class TemplatableValue { protected: enum { - EMPTY, + ESPHOME_EMPTY, VALUE, LAMBDA, } type_; diff --git a/esphome/core/helpers.cpp b/esphome/core/helpers.cpp index f2580a0c84..7cca994499 100644 --- a/esphome/core/helpers.cpp +++ b/esphome/core/helpers.cpp @@ -54,6 +54,9 @@ #endif #include "esphome/components/nrf52/core.h" #endif +#ifdef USE_ZEPHYR +#include +#endif namespace esphome { @@ -248,12 +251,11 @@ bool random_bytes(uint8_t *data, size_t len) { } fclose(fp); return true; -#elif defined(USE_NRF52) -#ifdef USE_ARDUINO +#elif defined(USE_NRF52) && defined(USE_ARDUINO) return nRFCrypto.Random.generate(data, len); -#elif USE_ZEPHYR -// TODO -#endif +#elif defined(USE_ZEPHYR) + sys_rand_get(data, len); + return true; #else #error "No random source available for this configuration." #endif @@ -525,14 +527,18 @@ void hsv_to_rgb(int hue, float saturation, float value, float &red, float &green } // System APIs -#if defined(USE_ESP8266) || defined(USE_RP2040) || defined(USE_HOST) || defined(USE_NRF52) +#if defined(USE_ESP8266) || defined(USE_RP2040) || defined(USE_HOST) // ESP8266 doesn't have mutexes, but that shouldn't be an issue as it's single-core and non-preemptive OS. Mutex::Mutex() {} void Mutex::lock() {} bool Mutex::try_lock() { return true; } void Mutex::unlock() {} -// TODO -#elif defined(USE_ESP32) || defined(USE_LIBRETINY) /*|| defined(USE_NRF52)*/ +#elif defined(USE_ZEPHYR) +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) Mutex::Mutex() { handle_ = xSemaphoreCreateMutex(); } void Mutex::lock() { xSemaphoreTake(this->handle_, portMAX_DELAY); } bool Mutex::try_lock() { return xSemaphoreTake(this->handle_, 0) == pdTRUE; } diff --git a/esphome/core/helpers.h b/esphome/core/helpers.h index f8633801ae..e973dc040d 100644 --- a/esphome/core/helpers.h +++ b/esphome/core/helpers.h @@ -21,10 +21,10 @@ #elif defined(USE_LIBRETINY) #include #include -#elif defined(USE_NRF52) -#ifdef USE_ARDUINO +#elif defined(USE_NRF52) && defined(USE_ARDUINO) #include -#endif +#elif defined(USE_ZEPHYR) +#include #endif #define HOT __attribute__((hot)) @@ -551,8 +551,9 @@ class Mutex { Mutex &operator=(const Mutex &) = delete; private: -// TODO -#if defined(USE_ESP32) || defined(USE_LIBRETINY) /*|| defined(USE_NRF52)*/ +#if defined(USE_ZEPHYR) + k_mutex handle_; +#elif defined(USE_ESP32) || defined(USE_LIBRETINY) || defined(USE_NRF52) SemaphoreHandle_t handle_; #endif };