From 53d2d190c32a1cc4cfca56a3eff4f0a24c0fb10a Mon Sep 17 00:00:00 2001 From: Tomasz Duda Date: Thu, 16 May 2024 15:21:40 +0200 Subject: [PATCH] add deep sleep --- esphome/components/deep_sleep/__init__.py | 7 +- .../deep_sleep/deep_sleep_zephyr.cpp | 67 +++++++++++++++++++ esphome/zephyr_tools.py | 11 ++- 3 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 esphome/components/deep_sleep/deep_sleep_zephyr.cpp diff --git a/esphome/components/deep_sleep/__init__.py b/esphome/components/deep_sleep/__init__.py index fd7ef6fcce..9ef3667b2a 100644 --- a/esphome/components/deep_sleep/__init__.py +++ b/esphome/components/deep_sleep/__init__.py @@ -16,6 +16,7 @@ from esphome.const import ( CONF_WAKEUP_PIN, PLATFORM_ESP32, PLATFORM_ESP8266, + PLATFORM_NRF52, ) from esphome.components.esp32 import get_esp32_variant @@ -28,6 +29,8 @@ from esphome.components.esp32.const import ( VARIANT_ESP32C6, VARIANT_ESP32H2, ) +from esphome.components.zephyr import zephyr_add_prj_conf +from esphome.core import CORE WAKEUP_PINS = { VARIANT_ESP32: [ @@ -198,7 +201,7 @@ CONFIG_SCHEMA = cv.All( cv.Optional(CONF_TOUCH_WAKEUP): cv.All(cv.only_on_esp32, cv.boolean), } ).extend(cv.COMPONENT_SCHEMA), - cv.only_on([PLATFORM_ESP32, PLATFORM_ESP8266]), + cv.only_on([PLATFORM_ESP32, PLATFORM_ESP8266, PLATFORM_NRF52]), ) @@ -249,6 +252,8 @@ async def to_code(config): if CONF_TOUCH_WAKEUP in config: cg.add(var.set_touch_wakeup(config[CONF_TOUCH_WAKEUP])) + if CORE.using_zephyr: + zephyr_add_prj_conf("POWEROFF", True) cg.add_define("USE_DEEP_SLEEP") diff --git a/esphome/components/deep_sleep/deep_sleep_zephyr.cpp b/esphome/components/deep_sleep/deep_sleep_zephyr.cpp new file mode 100644 index 0000000000..05fa3b35cc --- /dev/null +++ b/esphome/components/deep_sleep/deep_sleep_zephyr.cpp @@ -0,0 +1,67 @@ +#ifdef USE_ZEPHYR +#include "deep_sleep_component.h" +#include "esphome/core/log.h" +#include + +namespace esphome { +namespace deep_sleep { + +static const char *const TAG = "deep_sleep"; + +optional DeepSleepComponent::get_run_duration_() const { return this->run_duration_; } + +void DeepSleepComponent::dump_config_platform_() { + ESP_LOGCONFIG(TAG, "Enabled peripherals:"); + ESP_LOGCONFIG(TAG, " USBD: %-3s| UARTE0: %-3s| UARTE1: %-3s| UART0 %-3s", + YESNO((NRF_USBD->ENABLE & USBD_ENABLE_ENABLE_Msk) == USBD_ENABLE_ENABLE_Enabled), + YESNO((NRF_UARTE0->ENABLE & UARTE_ENABLE_ENABLE_Msk) == UARTE_ENABLE_ENABLE_Enabled), + YESNO((NRF_UARTE1->ENABLE & UARTE_ENABLE_ENABLE_Msk) == UARTE_ENABLE_ENABLE_Enabled), + YESNO((NRF_UART0->ENABLE & UART_ENABLE_ENABLE_Msk) == UART_ENABLE_ENABLE_Enabled)); + ESP_LOGCONFIG(TAG, " TWIS0: %-3s| TWIS1: %-3s| TWIM0: %-3s| TWIM1: %-3s", + YESNO((NRF_TWIS0->ENABLE & TWIS_ENABLE_ENABLE_Msk) == TWIS_ENABLE_ENABLE_Enabled), + YESNO((NRF_TWIS1->ENABLE & TWIS_ENABLE_ENABLE_Msk) == TWIS_ENABLE_ENABLE_Enabled), + YESNO((NRF_TWIM0->ENABLE & TWIM_ENABLE_ENABLE_Msk) == TWIM_ENABLE_ENABLE_Enabled), + YESNO((NRF_TWIM1->ENABLE & TWIM_ENABLE_ENABLE_Msk) == TWIM_ENABLE_ENABLE_Enabled)); + ESP_LOGCONFIG(TAG, " TWI0: %-3s| TWI1: %-3s| COMP: %-3s| CCM: %-3s", + YESNO((NRF_TWI0->ENABLE & TWI_ENABLE_ENABLE_Msk) == TWI_ENABLE_ENABLE_Enabled), + YESNO((NRF_TWI1->ENABLE & TWI_ENABLE_ENABLE_Msk) == TWI_ENABLE_ENABLE_Enabled), + YESNO((NRF_COMP->ENABLE & COMP_ENABLE_ENABLE_Msk) == COMP_ENABLE_ENABLE_Enabled), + YESNO((NRF_CCM->ENABLE & CCM_ENABLE_ENABLE_Msk) == CCM_ENABLE_ENABLE_Enabled)); + ESP_LOGCONFIG(TAG, " PDM: %-3s| SPIS0: %-3s| SPIS1: %-3s| SPIS2: %-3s", + YESNO((NRF_PDM->ENABLE & PDM_ENABLE_ENABLE_Msk) == PDM_ENABLE_ENABLE_Enabled), + YESNO((NRF_SPIS0->ENABLE & SPIS_ENABLE_ENABLE_Msk) == SPIS_ENABLE_ENABLE_Enabled), + YESNO((NRF_SPIS1->ENABLE & SPIS_ENABLE_ENABLE_Msk) == SPIS_ENABLE_ENABLE_Enabled), + YESNO((NRF_SPIS2->ENABLE & SPIS_ENABLE_ENABLE_Msk) == SPIS_ENABLE_ENABLE_Enabled)); + ESP_LOGCONFIG(TAG, " SPIM0: %-3s| SPIM1: %-3s| SPIM2: %-3s| SPIM3: %-3s", + YESNO((NRF_SPIM0->ENABLE & SPIM_ENABLE_ENABLE_Msk) == SPIM_ENABLE_ENABLE_Enabled), + YESNO((NRF_SPIM1->ENABLE & SPIM_ENABLE_ENABLE_Msk) == SPIM_ENABLE_ENABLE_Enabled), + YESNO((NRF_SPIM2->ENABLE & SPIM_ENABLE_ENABLE_Msk) == SPIM_ENABLE_ENABLE_Enabled), + YESNO((NRF_SPIM3->ENABLE & SPIM_ENABLE_ENABLE_Msk) == SPIM_ENABLE_ENABLE_Enabled)); + ESP_LOGCONFIG(TAG, " SPI0: %-3s| SPI1: %-3s| SPI2: %-3s| SAADC: %-3s", + YESNO((NRF_SPI0->ENABLE & SPI_ENABLE_ENABLE_Msk) == SPI_ENABLE_ENABLE_Enabled), + YESNO((NRF_SPI1->ENABLE & SPI_ENABLE_ENABLE_Msk) == SPI_ENABLE_ENABLE_Enabled), + YESNO((NRF_SPI2->ENABLE & SPI_ENABLE_ENABLE_Msk) == SPI_ENABLE_ENABLE_Enabled), + YESNO((NRF_SAADC->ENABLE & SAADC_ENABLE_ENABLE_Msk) == SAADC_ENABLE_ENABLE_Enabled)); + ESP_LOGCONFIG(TAG, " QSPI: %-3s| QDEC: %-3s| LPCOMP: %-3s| I2S: %-3s", + YESNO((NRF_QSPI->ENABLE & QSPI_ENABLE_ENABLE_Msk) == QSPI_ENABLE_ENABLE_Enabled), + YESNO((NRF_QDEC->ENABLE & QDEC_ENABLE_ENABLE_Msk) == QDEC_ENABLE_ENABLE_Enabled), + YESNO((NRF_LPCOMP->ENABLE & LPCOMP_ENABLE_ENABLE_Msk) == LPCOMP_ENABLE_ENABLE_Enabled), + YESNO((NRF_I2S->ENABLE & I2S_ENABLE_ENABLE_Msk) == I2S_ENABLE_ENABLE_Enabled)); + ESP_LOGCONFIG(TAG, " PWM0: %-3s| PWM1: %-3s| PWM2: %-3s| PWM3: %-3s", + YESNO((NRF_PWM0->ENABLE & PWM_ENABLE_ENABLE_Msk) == PWM_ENABLE_ENABLE_Enabled), + YESNO((NRF_PWM1->ENABLE & PWM_ENABLE_ENABLE_Msk) == PWM_ENABLE_ENABLE_Enabled), + YESNO((NRF_PWM2->ENABLE & PWM_ENABLE_ENABLE_Msk) == PWM_ENABLE_ENABLE_Enabled), + YESNO((NRF_PWM3->ENABLE & PWM_ENABLE_ENABLE_Msk) == PWM_ENABLE_ENABLE_Enabled)); + ESP_LOGCONFIG(TAG, " AAR: %-3s| QSPI deep power-down:%-3s| CRYPTOCELL: %-3s", + YESNO((NRF_AAR->ENABLE & AAR_ENABLE_ENABLE_Msk) == AAR_ENABLE_ENABLE_Enabled), + YESNO((NRF_QSPI->IFCONFIG0 & QSPI_IFCONFIG0_DPMENABLE_Msk) == QSPI_IFCONFIG0_DPMENABLE_Enable), + YESNO((NRF_CRYPTOCELL->ENABLE & CRYPTOCELL_ENABLE_ENABLE_Msk) == CRYPTOCELL_ENABLE_ENABLE_Enabled)); +} + +bool DeepSleepComponent::prepare_to_sleep_() { return true; } + +void DeepSleepComponent::deep_sleep_() { sys_poweroff(); } + +} // namespace deep_sleep +} // namespace esphome +#endif diff --git a/esphome/zephyr_tools.py b/esphome/zephyr_tools.py index 8f11f9b9a1..62f278a917 100644 --- a/esphome/zephyr_tools.py +++ b/esphome/zephyr_tools.py @@ -9,7 +9,7 @@ from bleak.exc import BleakDeviceNotFoundError, BleakDBusError from esphome.espota2 import ProgressBar try: - from smpclient.transport.ble import SMPBLETransport + from smpclient.transport.ble import SMPBLETransport, SMPBLETransportDeviceNotFound from smpclient.transport.serial import SMPSerialTransport from smpclient import SMPClient from smpclient.mcuboot import IMAGE_TLV, ImageInfo, TLVNotFound, MCUBootImageError @@ -90,6 +90,15 @@ def get_image_tlv_sha256(file): async def smpmgr_upload(config, host, firmware): + for attempt in range(3): + try: + return await smpmgr_upload_(config, host, firmware) + except SMPBLETransportDeviceNotFound: + if attempt == 2: + raise + + +async def smpmgr_upload_(config, host, firmware): if sys.version_info < (3, 10): _LOGGER.error("BLE OTA requires at least python 3.10") return 1