Skip to content

boot: zephyr: Refactor USB DFU entry logic #2277

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 22 additions & 16 deletions boot/zephyr/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* Copyright (c) 2012-2014 Wind River Systems, Inc.
* Copyright (c) 2020 Arm Limited
* Copyright (c) 2021-2023 Nordic Semiconductor ASA
* Copyright (c) 2025 Aerlync Labs Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -441,6 +442,9 @@ int main(void)
{
struct boot_rsp rsp;
int rc;
#if defined(CONFIG_BOOT_USB_DFU_GPIO) || defined(CONFIG_BOOT_USB_DFU_WAIT)
bool usb_dfu_requested = false;
#endif
FIH_DECLARE(fih_rc, FIH_FAILURE);

MCUBOOT_WATCHDOG_SETUP();
Expand Down Expand Up @@ -480,35 +484,37 @@ int main(void)

#if defined(CONFIG_BOOT_USB_DFU_GPIO)
if (io_detect_pin()) {
usb_dfu_requested = true;

#ifdef CONFIG_MCUBOOT_INDICATION_LED
io_led_set(1);
#endif

mcuboot_status_change(MCUBOOT_STATUS_USB_DFU_ENTERED);
}
#elif defined(CONFIG_BOOT_USB_DFU_WAIT)
usb_dfu_requested = true;
#endif

#if defined(CONFIG_BOOT_USB_DFU_GPIO) || defined(CONFIG_BOOT_USB_DFU_WAIT)
if (usb_dfu_requested) {
rc = usb_enable(NULL);
if (rc) {
BOOT_LOG_ERR("Cannot enable USB");
BOOT_LOG_ERR("Cannot enable USB %d", rc);
} else {
BOOT_LOG_INF("Waiting for USB DFU");
wait_for_usb_dfu(K_FOREVER);

#if defined(CONFIG_BOOT_USB_DFU_WAIT)
mcuboot_status_change(MCUBOOT_STATUS_USB_DFU_WAITING);
wait_for_usb_dfu(K_MSEC(CONFIG_BOOT_USB_DFU_WAIT_DELAY_MS));
BOOT_LOG_INF("USB DFU wait time elapsed");
mcuboot_status_change(MCUBOOT_STATUS_USB_DFU_TIMED_OUT);
#else
wait_for_usb_dfu(K_FOREVER);
BOOT_LOG_INF("USB DFU wait terminated");
#endif
}
}
#elif defined(CONFIG_BOOT_USB_DFU_WAIT)
rc = usb_enable(NULL);
if (rc) {
BOOT_LOG_ERR("Cannot enable USB");
} else {
BOOT_LOG_INF("Waiting for USB DFU");

mcuboot_status_change(MCUBOOT_STATUS_USB_DFU_WAITING);

wait_for_usb_dfu(K_MSEC(CONFIG_BOOT_USB_DFU_WAIT_DELAY_MS));
BOOT_LOG_INF("USB DFU wait time elapsed");

mcuboot_status_change(MCUBOOT_STATUS_USB_DFU_TIMED_OUT);
}
#endif

#ifdef CONFIG_BOOT_SERIAL_WAIT_FOR_DFU
Expand Down
Loading