mirror of
https://github.com/esphome/esphome.git
synced 2025-10-27 05:03:48 +00:00
[nrf52] add dfu (#9319)
Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
@@ -2,10 +2,13 @@ from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
from esphome import pins
|
||||
import esphome.codegen as cg
|
||||
from esphome.components.zephyr import (
|
||||
copy_files as zephyr_copy_files,
|
||||
zephyr_add_pm_static,
|
||||
zephyr_add_prj_conf,
|
||||
zephyr_data,
|
||||
zephyr_set_core_data,
|
||||
zephyr_to_code,
|
||||
)
|
||||
@@ -18,6 +21,8 @@ import esphome.config_validation as cv
|
||||
from esphome.const import (
|
||||
CONF_BOARD,
|
||||
CONF_FRAMEWORK,
|
||||
CONF_ID,
|
||||
CONF_RESET_PIN,
|
||||
KEY_CORE,
|
||||
KEY_FRAMEWORK_VERSION,
|
||||
KEY_TARGET_FRAMEWORK,
|
||||
@@ -90,18 +95,43 @@ def _detect_bootloader(config: ConfigType) -> ConfigType:
|
||||
return config
|
||||
|
||||
|
||||
nrf52_ns = cg.esphome_ns.namespace("nrf52")
|
||||
DeviceFirmwareUpdate = nrf52_ns.class_("DeviceFirmwareUpdate", cg.Component)
|
||||
|
||||
CONF_DFU = "dfu"
|
||||
|
||||
CONFIG_SCHEMA = cv.All(
|
||||
set_core_data,
|
||||
cv.Schema(
|
||||
{
|
||||
cv.Required(CONF_BOARD): cv.string_strict,
|
||||
cv.Optional(KEY_BOOTLOADER): cv.one_of(*BOOTLOADERS, lower=True),
|
||||
cv.Optional(CONF_DFU): cv.Schema(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(DeviceFirmwareUpdate),
|
||||
cv.Required(CONF_RESET_PIN): pins.gpio_output_pin_schema,
|
||||
}
|
||||
),
|
||||
}
|
||||
),
|
||||
_detect_bootloader,
|
||||
set_core_data,
|
||||
)
|
||||
|
||||
|
||||
def _validate_mcumgr(config):
|
||||
bootloader = zephyr_data()[KEY_BOOTLOADER]
|
||||
if bootloader == BOOTLOADER_MCUBOOT:
|
||||
raise cv.Invalid(f"'{bootloader}' bootloader does not support DFU")
|
||||
|
||||
|
||||
def _final_validate(config):
|
||||
if CONF_DFU in config:
|
||||
_validate_mcumgr(config)
|
||||
|
||||
|
||||
FINAL_VALIDATE_SCHEMA = _final_validate
|
||||
|
||||
|
||||
@coroutine_with_priority(1000)
|
||||
async def to_code(config: ConfigType) -> None:
|
||||
"""Convert the configuration to code."""
|
||||
@@ -136,6 +166,19 @@ async def to_code(config: ConfigType) -> None:
|
||||
|
||||
zephyr_to_code(config)
|
||||
|
||||
if dfu_config := config.get(CONF_DFU):
|
||||
CORE.add_job(_dfu_to_code, dfu_config)
|
||||
|
||||
|
||||
@coroutine_with_priority(90)
|
||||
async def _dfu_to_code(dfu_config):
|
||||
cg.add_define("USE_NRF52_DFU")
|
||||
var = cg.new_Pvariable(dfu_config[CONF_ID])
|
||||
pin = await cg.gpio_pin_expression(dfu_config[CONF_RESET_PIN])
|
||||
cg.add(var.set_reset_pin(pin))
|
||||
zephyr_add_prj_conf("CDC_ACM_DTE_RATE_CALLBACK_SUPPORT", True)
|
||||
await cg.register_component(var, dfu_config)
|
||||
|
||||
|
||||
def copy_files() -> None:
|
||||
"""Copy files to the build directory."""
|
||||
|
||||
Reference in New Issue
Block a user