Skip to content

Commit a77c554

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 6cbea0a commit a77c554

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

boot/zephyr/main.c

+22-16
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.
@@ -441,6 +442,9 @@ int main(void)
441442
{
442443
struct boot_rsp rsp;
443444
int rc;
445+
#if defined(CONFIG_BOOT_USB_DFU_GPIO) || defined(CONFIG_BOOT_USB_DFU_WAIT)
446+
bool usb_dfu_requested = false;
447+
#endif
444448
FIH_DECLARE(fih_rc, FIH_FAILURE);
445449

446450
MCUBOOT_WATCHDOG_SETUP();
@@ -480,35 +484,37 @@ int main(void)
480484

481485
#if defined(CONFIG_BOOT_USB_DFU_GPIO)
482486
if (io_detect_pin()) {
487+
usb_dfu_requested = true;
488+
483489
#ifdef CONFIG_MCUBOOT_INDICATION_LED
484490
io_led_set(1);
485491
#endif
486492

487493
mcuboot_status_change(MCUBOOT_STATUS_USB_DFU_ENTERED);
494+
}
495+
#elif defined(CONFIG_BOOT_USB_DFU_WAIT)
496+
usb_dfu_requested = true;
497+
#endif
488498

499+
#if defined(CONFIG_BOOT_USB_DFU_GPIO) || defined(CONFIG_BOOT_USB_DFU_WAIT)
500+
if (usb_dfu_requested) {
489501
rc = usb_enable(NULL);
490502
if (rc) {
491-
BOOT_LOG_ERR("Cannot enable USB");
503+
BOOT_LOG_ERR("Cannot enable USB %d", rc);
492504
} else {
493505
BOOT_LOG_INF("Waiting for USB DFU");
494-
wait_for_usb_dfu(K_FOREVER);
506+
507+
#if defined(CONFIG_BOOT_USB_DFU_WAIT)
508+
mcuboot_status_change(MCUBOOT_STATUS_USB_DFU_WAITING);
509+
wait_for_usb_dfu(K_MSEC(CONFIG_BOOT_USB_DFU_WAIT_DELAY_MS));
495510
BOOT_LOG_INF("USB DFU wait time elapsed");
511+
mcuboot_status_change(MCUBOOT_STATUS_USB_DFU_TIMED_OUT);
512+
#else
513+
wait_for_usb_dfu(K_FOREVER);
514+
BOOT_LOG_INF("USB DFU wait terminated");
515+
#endif
496516
}
497517
}
498-
#elif defined(CONFIG_BOOT_USB_DFU_WAIT)
499-
rc = usb_enable(NULL);
500-
if (rc) {
501-
BOOT_LOG_ERR("Cannot enable USB");
502-
} else {
503-
BOOT_LOG_INF("Waiting for USB DFU");
504-
505-
mcuboot_status_change(MCUBOOT_STATUS_USB_DFU_WAITING);
506-
507-
wait_for_usb_dfu(K_MSEC(CONFIG_BOOT_USB_DFU_WAIT_DELAY_MS));
508-
BOOT_LOG_INF("USB DFU wait time elapsed");
509-
510-
mcuboot_status_change(MCUBOOT_STATUS_USB_DFU_TIMED_OUT);
511-
}
512518
#endif
513519

514520
#ifdef CONFIG_BOOT_SERIAL_WAIT_FOR_DFU

0 commit comments

Comments
 (0)