mirror of
https://github.com/esphome/esphome.git
synced 2025-09-04 04:12:23 +01: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 pathlib import Path
|
||||||
|
|
||||||
|
from esphome import pins
|
||||||
import esphome.codegen as cg
|
import esphome.codegen as cg
|
||||||
from esphome.components.zephyr import (
|
from esphome.components.zephyr import (
|
||||||
copy_files as zephyr_copy_files,
|
copy_files as zephyr_copy_files,
|
||||||
zephyr_add_pm_static,
|
zephyr_add_pm_static,
|
||||||
|
zephyr_add_prj_conf,
|
||||||
|
zephyr_data,
|
||||||
zephyr_set_core_data,
|
zephyr_set_core_data,
|
||||||
zephyr_to_code,
|
zephyr_to_code,
|
||||||
)
|
)
|
||||||
@@ -18,6 +21,8 @@ import esphome.config_validation as cv
|
|||||||
from esphome.const import (
|
from esphome.const import (
|
||||||
CONF_BOARD,
|
CONF_BOARD,
|
||||||
CONF_FRAMEWORK,
|
CONF_FRAMEWORK,
|
||||||
|
CONF_ID,
|
||||||
|
CONF_RESET_PIN,
|
||||||
KEY_CORE,
|
KEY_CORE,
|
||||||
KEY_FRAMEWORK_VERSION,
|
KEY_FRAMEWORK_VERSION,
|
||||||
KEY_TARGET_FRAMEWORK,
|
KEY_TARGET_FRAMEWORK,
|
||||||
@@ -90,18 +95,43 @@ def _detect_bootloader(config: ConfigType) -> ConfigType:
|
|||||||
return config
|
return config
|
||||||
|
|
||||||
|
|
||||||
|
nrf52_ns = cg.esphome_ns.namespace("nrf52")
|
||||||
|
DeviceFirmwareUpdate = nrf52_ns.class_("DeviceFirmwareUpdate", cg.Component)
|
||||||
|
|
||||||
|
CONF_DFU = "dfu"
|
||||||
|
|
||||||
CONFIG_SCHEMA = cv.All(
|
CONFIG_SCHEMA = cv.All(
|
||||||
|
set_core_data,
|
||||||
cv.Schema(
|
cv.Schema(
|
||||||
{
|
{
|
||||||
cv.Required(CONF_BOARD): cv.string_strict,
|
cv.Required(CONF_BOARD): cv.string_strict,
|
||||||
cv.Optional(KEY_BOOTLOADER): cv.one_of(*BOOTLOADERS, lower=True),
|
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,
|
_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)
|
@coroutine_with_priority(1000)
|
||||||
async def to_code(config: ConfigType) -> None:
|
async def to_code(config: ConfigType) -> None:
|
||||||
"""Convert the configuration to code."""
|
"""Convert the configuration to code."""
|
||||||
@@ -136,6 +166,19 @@ async def to_code(config: ConfigType) -> None:
|
|||||||
|
|
||||||
zephyr_to_code(config)
|
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:
|
def copy_files() -> None:
|
||||||
"""Copy files to the build directory."""
|
"""Copy files to the build directory."""
|
||||||
|
@@ -2,6 +2,7 @@ BOOTLOADER_ADAFRUIT = "adafruit"
|
|||||||
BOOTLOADER_ADAFRUIT_NRF52_SD132 = "adafruit_nrf52_sd132"
|
BOOTLOADER_ADAFRUIT_NRF52_SD132 = "adafruit_nrf52_sd132"
|
||||||
BOOTLOADER_ADAFRUIT_NRF52_SD140_V6 = "adafruit_nrf52_sd140_v6"
|
BOOTLOADER_ADAFRUIT_NRF52_SD140_V6 = "adafruit_nrf52_sd140_v6"
|
||||||
BOOTLOADER_ADAFRUIT_NRF52_SD140_V7 = "adafruit_nrf52_sd140_v7"
|
BOOTLOADER_ADAFRUIT_NRF52_SD140_V7 = "adafruit_nrf52_sd140_v7"
|
||||||
|
|
||||||
EXTRA_ADC = [
|
EXTRA_ADC = [
|
||||||
"VDD",
|
"VDD",
|
||||||
"VDDHDIV5",
|
"VDDHDIV5",
|
||||||
|
51
esphome/components/nrf52/dfu.cpp
Normal file
51
esphome/components/nrf52/dfu.cpp
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
#include "dfu.h"
|
||||||
|
|
||||||
|
#ifdef USE_NRF52_DFU
|
||||||
|
|
||||||
|
#include <zephyr/device.h>
|
||||||
|
#include <zephyr/drivers/uart.h>
|
||||||
|
#include <zephyr/drivers/uart/cdc_acm.h>
|
||||||
|
#include "esphome/core/log.h"
|
||||||
|
|
||||||
|
namespace esphome {
|
||||||
|
namespace nrf52 {
|
||||||
|
|
||||||
|
static const char *const TAG = "dfu";
|
||||||
|
|
||||||
|
volatile bool goto_dfu = false; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||||
|
|
||||||
|
static const uint32_t DFU_DBL_RESET_MAGIC = 0x5A1AD5; // SALADS
|
||||||
|
|
||||||
|
#define DEVICE_AND_COMMA(node_id) DEVICE_DT_GET(node_id),
|
||||||
|
|
||||||
|
static void cdc_dte_rate_callback(const struct device * /*unused*/, uint32_t rate) {
|
||||||
|
if (rate == 1200) {
|
||||||
|
goto_dfu = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void DeviceFirmwareUpdate::setup() {
|
||||||
|
this->reset_pin_->setup();
|
||||||
|
const struct device *cdc_dev[] = {DT_FOREACH_STATUS_OKAY(zephyr_cdc_acm_uart, DEVICE_AND_COMMA)};
|
||||||
|
for (auto &idx : cdc_dev) {
|
||||||
|
cdc_acm_dte_rate_callback_set(idx, cdc_dte_rate_callback);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void DeviceFirmwareUpdate::loop() {
|
||||||
|
if (goto_dfu) {
|
||||||
|
goto_dfu = false;
|
||||||
|
volatile uint32_t *dbl_reset_mem = (volatile uint32_t *) 0x20007F7C;
|
||||||
|
(*dbl_reset_mem) = DFU_DBL_RESET_MAGIC;
|
||||||
|
this->reset_pin_->digital_write(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void DeviceFirmwareUpdate::dump_config() {
|
||||||
|
ESP_LOGCONFIG(TAG, "DFU:");
|
||||||
|
LOG_PIN(" RESET Pin: ", this->reset_pin_);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace nrf52
|
||||||
|
} // namespace esphome
|
||||||
|
|
||||||
|
#endif
|
24
esphome/components/nrf52/dfu.h
Normal file
24
esphome/components/nrf52/dfu.h
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "esphome/core/defines.h"
|
||||||
|
#ifdef USE_NRF52_DFU
|
||||||
|
#include "esphome/core/component.h"
|
||||||
|
#include "esphome/core/gpio.h"
|
||||||
|
|
||||||
|
namespace esphome {
|
||||||
|
namespace nrf52 {
|
||||||
|
class DeviceFirmwareUpdate : public Component {
|
||||||
|
public:
|
||||||
|
void setup() override;
|
||||||
|
void loop() override;
|
||||||
|
void set_reset_pin(GPIOPin *reset) { this->reset_pin_ = reset; }
|
||||||
|
void dump_config() override;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
GPIOPin *reset_pin_;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace nrf52
|
||||||
|
} // namespace esphome
|
||||||
|
|
||||||
|
#endif
|
@@ -240,6 +240,10 @@
|
|||||||
#define USE_SOCKET_SELECT_SUPPORT
|
#define USE_SOCKET_SELECT_SUPPORT
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef USE_NRF52
|
||||||
|
#define USE_NRF52_DFU
|
||||||
|
#endif
|
||||||
|
|
||||||
// Disabled feature flags
|
// Disabled feature flags
|
||||||
// #define USE_BSEC // Requires a library with proprietary license
|
// #define USE_BSEC // Requires a library with proprietary license
|
||||||
// #define USE_BSEC2 // Requires a library with proprietary license
|
// #define USE_BSEC2 // Requires a library with proprietary license
|
||||||
|
7
tests/components/nrf52/test.nrf52-adafruit.yaml
Normal file
7
tests/components/nrf52/test.nrf52-adafruit.yaml
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
nrf52:
|
||||||
|
dfu:
|
||||||
|
reset_pin:
|
||||||
|
number: 14
|
||||||
|
inverted: true
|
||||||
|
mode:
|
||||||
|
output: true
|
Reference in New Issue
Block a user