Skip to content

Commit 7aa2a38

Browse files
sayoojkkarunnvlsianpu
authored andcommitted
[nrf fromtree] boot: zephyr: Refactor DFU entry logic
Consolidates USB DFU entry logic by unifying GPIO and timeout-based DFU triggers under a common flag. This avoids code duplication and improves maintainability. Also improves log clarity for different DFU exit conditions. Signed-off-by: Sayooj K Karun <[email protected]> (cherry picked from commit 402d3f7) Signed-off-by: Dominik Ermel <[email protected]>
1 parent f359271 commit 7aa2a38

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

boot/zephyr/main.c

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* Copyright (c) 2012-2014 Wind River Systems, Inc.
33
* Copyright (c) 2020 Arm Limited
44
* Copyright (c) 2021-2023 Nordic Semiconductor ASA
5+
* Copyright (c) 2025 Aerlync Labs Inc.
56
*
67
* Licensed under the Apache License, Version 2.0 (the "License");
78
* you may not use this file except in compliance with the License.
@@ -479,6 +480,9 @@ int main(void)
479480
{
480481
struct boot_rsp rsp;
481482
int rc;
483+
#if defined(CONFIG_BOOT_USB_DFU_GPIO) || defined(CONFIG_BOOT_USB_DFU_WAIT)
484+
bool usb_dfu_requested = false;
485+
#endif
482486
FIH_DECLARE(fih_rc, FIH_FAILURE);
483487

484488
MCUBOOT_WATCHDOG_SETUP();
@@ -518,35 +522,37 @@ int main(void)
518522

519523
#if defined(CONFIG_BOOT_USB_DFU_GPIO)
520524
if (io_detect_pin()) {
525+
usb_dfu_requested = true;
526+
521527
#ifdef CONFIG_MCUBOOT_INDICATION_LED
522528
io_led_set(1);
523529
#endif
524530

525531
mcuboot_status_change(MCUBOOT_STATUS_USB_DFU_ENTERED);
532+
}
533+
#elif defined(CONFIG_BOOT_USB_DFU_WAIT)
534+
usb_dfu_requested = true;
535+
#endif
526536

537+
#if defined(CONFIG_BOOT_USB_DFU_GPIO) || defined(CONFIG_BOOT_USB_DFU_WAIT)
538+
if (usb_dfu_requested) {
527539
rc = usb_enable(NULL);
528540
if (rc) {
529-
BOOT_LOG_ERR("Cannot enable USB");
541+
BOOT_LOG_ERR("Cannot enable USB: %d", rc);
530542
} else {
531543
BOOT_LOG_INF("Waiting for USB DFU");
532-
wait_for_usb_dfu(K_FOREVER);
544+
545+
#if defined(CONFIG_BOOT_USB_DFU_WAIT)
546+
mcuboot_status_change(MCUBOOT_STATUS_USB_DFU_WAITING);
547+
wait_for_usb_dfu(K_MSEC(CONFIG_BOOT_USB_DFU_WAIT_DELAY_MS));
533548
BOOT_LOG_INF("USB DFU wait time elapsed");
549+
mcuboot_status_change(MCUBOOT_STATUS_USB_DFU_TIMED_OUT);
550+
#else
551+
wait_for_usb_dfu(K_FOREVER);
552+
BOOT_LOG_INF("USB DFU wait terminated");
553+
#endif
534554
}
535555
}
536-
#elif defined(CONFIG_BOOT_USB_DFU_WAIT)
537-
rc = usb_enable(NULL);
538-
if (rc) {
539-
BOOT_LOG_ERR("Cannot enable USB");
540-
} else {
541-
BOOT_LOG_INF("Waiting for USB DFU");
542-
543-
mcuboot_status_change(MCUBOOT_STATUS_USB_DFU_WAITING);
544-
545-
wait_for_usb_dfu(K_MSEC(CONFIG_BOOT_USB_DFU_WAIT_DELAY_MS));
546-
BOOT_LOG_INF("USB DFU wait time elapsed");
547-
548-
mcuboot_status_change(MCUBOOT_STATUS_USB_DFU_TIMED_OUT);
549-
}
550556
#endif
551557

552558
#ifdef CONFIG_BOOT_SERIAL_WAIT_FOR_DFU

0 commit comments

Comments
 (0)