From a77c554c6570238c37ccf2007a3819c09eeed778 Mon Sep 17 00:00:00 2001 From: Sayooj K Karun Date: Wed, 23 Apr 2025 18:13:11 +0530 Subject: [PATCH] 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 --- boot/zephyr/main.c | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/boot/zephyr/main.c b/boot/zephyr/main.c index a46fc724c5..1b2ac607ef 100644 --- a/boot/zephyr/main.c +++ b/boot/zephyr/main.c @@ -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. @@ -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(); @@ -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