Skip to content

Migrate MCUboot Zephyr USB support to USB_DEVICE_STACK_NEXT before Zephyr v4.5.0 #2596

@JayToltTech

Description

@JayToltTech

Problem

MCUboot's Zephyr USB serial recovery and USB DFU features currently use the legacy USB_DEVICE_STACK API, which has been deprecated in Zephyr and is scheduled for removal in Zephyr v4.5.0.

Current Impact (Zephyr v4.3.0)

Building MCUboot with USB features enabled generates numerous deprecation warnings:

warning: 'usb_enable' is deprecated [-Wdeprecated-declarations]
warning: 'usb_disable' is deprecated [-Wdeprecated-declarations]
warning: 'usb_dc_ep_write' is deprecated [-Wdeprecated-declarations]
warning: 'usb_dc_ep_read' is deprecated [-Wdeprecated-declarations]
... (50+ similar warnings from zephyr/subsys/usb/device/)

These warnings occur throughout Zephyr's USB subsystem code when compiling MCUboot with:

  • CONFIG_BOOT_SERIAL_CDC_ACM=y (USB serial recovery)
  • CONFIG_BOOT_USB_DFU_WAIT=y (USB DFU mode)

Timeline

  • Zephyr v4.3.0 (current): Legacy USB stack deprecated, warnings generated
  • Zephyr v4.5.0 (upcoming): Legacy USB stack will be removed entirely
  • Result: MCUboot's USB features will break unless migrated

Workaround Currently in Place

Zephyr has a temporary workaround that prevents the new stack from being used with MCUboot:

boards/common/usb/Kconfig.cdc_acm_serial.defconfig

config USB_DEVICE_STACK_NEXT
default y if !MCUBOOT # Explicitly disabled when MCUboot is present

This confirms the Zephyr team is aware of the incompatibility but cannot fix it directly since MCUboot is a separate project.

Proposed Solution

Migrate MCUboot's USB implementation to use the new USB_DEVICE_STACK_NEXT API.

API Changes Required

Files to modify:

  • boot/zephyr/main.c - 3 locations using usb_enable() / usb_disable()
  • boot/zephyr/serial_adapter.c - 1 location using usb_enable()

Legacy API:
usb_enable(NULL);
usb_disable();

New API:
struct usbd_context uds_ctx = / context */;
usbd_init(uds_ctx);
usbd_enable(uds_ctx);
usbd_disable(uds_ctx);

Device Tree Changes

The new stack requires USB device context definition via device tree:

USBD_DEVICE_DEFINE(mcuboot_usbd,
DEVICE_DT_GET(DT_NODELABEL(zephyr_udc0)),
VENDOR_ID,
PRODUCT_ID);

CDC-ACM configuration also moves from Kconfig to device tree.

Kconfig Changes

Replace:
CONFIG_USB_DEVICE_STACK=y

With:
CONFIG_USB_DEVICE_STACK_NEXT=y
CONFIG_USBD_CDC_ACM_CLASS=y

Estimated Scope

  • Lines of code: ~50-100 modifications
  • Complexity: Low to moderate (mostly mechanical API replacement)
  • Testing required: Critical - must verify USB serial recovery and DFU work on multiple platforms

References

Hardware Tested

I can test and validate this migration on:

  • nRF5340 (nrfx_usbd controller with UDC support)

Volunteers

I'm willing to contribute this migration if the maintainers would accept the PR. Please advise on:

  1. Whether this migration is desired
  2. Any specific requirements or constraints
  3. Testing expectations beyond nRF5340

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions