ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
dfu.cpp
Go to the documentation of this file.
1#include "dfu.h"
2
3#ifdef USE_NRF52_DFU
4
6#include "esphome/core/log.h"
8
9#include <hal/nrf_power.h>
10
11namespace esphome::nrf52 {
12
13ESPHOME_LOG_TAG(TAG, "dfu");
14
15static const uint32_t DFU_DBL_RESET_MAGIC = 0x5A1AD5; // SALADS
16static const uint8_t DFU_MAGIC_UF2_RESET = 0x57; // Adafruit nRF52 bootloader UF2 magic
17static const uint8_t DFU_MAGIC_SKIP = 0x6d; // Adafruit nRF52 bootloader: start the app, skip DFU
18// Host-side baud rates that trigger an action; see DeviceFirmwareUpdate::setup().
19static const uint32_t DFU_TOUCH_BAUD_RATE = 1200; // reboot into the bootloader (DFU)
20static const uint32_t RESET_TOUCH_BAUD_RATE = 2001; // plain reboot back into the application
21
23 if (this->reset_pin_ != nullptr) {
24 this->reset_pin_->setup();
25 }
26#if defined(CONFIG_CDC_ACM_DTE_RATE_CALLBACK_SUPPORT)
27 zephyr::global_cdc_acm->add_on_rate_callback([this](const device *, uint32_t rate) {
28 if (rate == RESET_TOUCH_BAUD_RATE) {
29 // A plain reboot for host tools (a logs view's Reset device): the USB CDC has no
30 // reset line, and the bootloader, once entered, has no host command back to the app.
31 NRF_POWER->GPREGRET = DFU_MAGIC_SKIP;
33 App.reboot();
34 } else if (rate == DFU_TOUCH_BAUD_RATE) {
35 volatile uint32_t *dbl_reset_mem = (volatile uint32_t *) 0x20007F7C;
36 (*dbl_reset_mem) = DFU_DBL_RESET_MAGIC;
37 if (this->reset_pin_ != nullptr) {
38 this->reset_pin_->digital_write(true);
39 } else {
40 NRF_POWER->GPREGRET = DFU_MAGIC_UF2_RESET;
42 App.reboot();
43 }
44 }
45 });
46#endif
47}
48
50 ESP_LOGCONFIG(TAG, "DFU:");
51 if (this->reset_pin_ != nullptr) {
52 LOG_PIN(" RESET Pin: ", this->reset_pin_);
53 } else {
54 ESP_LOGCONFIG(TAG, " Method: GPREGRET");
55 }
56}
57
58} // namespace esphome::nrf52
59
60#endif
virtual void setup()=0
virtual void digital_write(bool value)=0
void add_on_rate_callback(F &&callback)
Definition cdc_acm.h:14
ESPHOME_LOG_TAG(TAG, "dfu")
CdcAcm * global_cdc_acm
Definition cdc_acm.cpp:26
void arch_feed_wdt()
Definition hal.cpp:58
Application App
Global storage of Application pointer - only one Application can exist.
static void uint32_t