Skip to content

Commit ab2d7bc

Browse files
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]>
1 parent 11982df commit ab2d7bc

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.
@@ -434,6 +435,9 @@ int main(void)
434435
{
435436
struct boot_rsp rsp;
436437
int rc;
438+
#if defined(CONFIG_BOOT_USB_DFU_GPIO) || defined(CONFIG_BOOT_USB_DFU_WAIT)
439+
bool usb_dfu_requested = false;
440+
#endif
437441
FIH_DECLARE(fih_rc, FIH_FAILURE);
438442

439443
MCUBOOT_WATCHDOG_SETUP();
@@ -473,35 +477,37 @@ int main(void)
473477

474478
#if defined(CONFIG_BOOT_USB_DFU_GPIO)
475479
if (io_detect_pin()) {
480+
usb_dfu_requested = true;
481+
476482
#ifdef CONFIG_MCUBOOT_INDICATION_LED
477483
io_led_set(1);
478484
#endif
479485

480486
mcuboot_status_change(MCUBOOT_STATUS_USB_DFU_ENTERED);
487+
}
488+
#elif defined(CONFIG_BOOT_USB_DFU_WAIT)
489+
usb_dfu_requested = true;
490+
#endif
481491

492+
#if defined(CONFIG_BOOT_USB_DFU_GPIO) || defined(CONFIG_BOOT_USB_DFU_WAIT)
493+
if (usb_dfu_requested) {
482494
rc = usb_enable(NULL);
483495
if (rc) {
484-
BOOT_LOG_ERR("Cannot enable USB");
496+
BOOT_LOG_ERR("Cannot enable USB: %d", rc);
485497
} else {
486498
BOOT_LOG_INF("Waiting for USB DFU");
487-
wait_for_usb_dfu(K_FOREVER);
499+
500+
#if defined(CONFIG_BOOT_USB_DFU_WAIT)
501+
mcuboot_status_change(MCUBOOT_STATUS_USB_DFU_WAITING);
502+
wait_for_usb_dfu(K_MSEC(CONFIG_BOOT_USB_DFU_WAIT_DELAY_MS));
488503
BOOT_LOG_INF("USB DFU wait time elapsed");
504+
mcuboot_status_change(MCUBOOT_STATUS_USB_DFU_TIMED_OUT);
505+
#else
506+
wait_for_usb_dfu(K_FOREVER);
507+
BOOT_LOG_INF("USB DFU wait terminated");
508+
#endif
489509
}
490510
}
491-
#elif defined(CONFIG_BOOT_USB_DFU_WAIT)
492-
rc = usb_enable(NULL);
493-
if (rc) {
494-
BOOT_LOG_ERR("Cannot enable USB");
495-
} else {
496-
BOOT_LOG_INF("Waiting for USB DFU");
497-
498-
mcuboot_status_change(MCUBOOT_STATUS_USB_DFU_WAITING);
499-
500-
wait_for_usb_dfu(K_MSEC(CONFIG_BOOT_USB_DFU_WAIT_DELAY_MS));
501-
BOOT_LOG_INF("USB DFU wait time elapsed");
502-
503-
mcuboot_status_change(MCUBOOT_STATUS_USB_DFU_TIMED_OUT);
504-
}
505511
#endif
506512

507513
#ifdef CONFIG_BOOT_SERIAL_WAIT_FOR_DFU

0 commit comments

Comments
 (0)