This is an automated email from the ASF dual-hosted git repository. jerzy pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/mynewt-core.git
commit e77638680bd8c6de3e43694683db045dde7eed99 Author: Jerzy Kasenberg <[email protected]> AuthorDate: Sun Nov 2 13:33:39 2025 +0100 tinyusb/dfu: Add runtime DFU support Runtime DFU specification allows to reconfigure USB device fof DFU. This may be useful when normal DFU that actually overwrites firmware can't be run to update itself. This is true for single slot builds. Default mynewt implementation writes magic value to specific NVReg and does reboot. DFU code in bootloaer will then detect this condition and start USB DFU in bootloader allowing application slot to be updated. Signed-off-by: Jerzy Kasenberg <[email protected]> --- hw/usb/tinyusb/dfu/pkg.yml | 8 +++++++ hw/usb/tinyusb/dfu/src/dfu_runtime.c | 41 ++++++++++++++++++++++++++++++++++++ hw/usb/tinyusb/dfu/syscfg.yml | 3 +++ 3 files changed, 52 insertions(+) diff --git a/hw/usb/tinyusb/dfu/pkg.yml b/hw/usb/tinyusb/dfu/pkg.yml index f1470d06a..73828603b 100644 --- a/hw/usb/tinyusb/dfu/pkg.yml +++ b/hw/usb/tinyusb/dfu/pkg.yml @@ -29,8 +29,16 @@ pkg.deps: - "@apache-mynewt-core/hw/hal" - "@apache-mynewt-core/kernel/os" - "@apache-mynewt-core/hw/usb/tinyusb" + +pkg.deps.USBD_DFU: - "@apache-mynewt-core/mgmt/imgmgr" - "@mcuboot/boot/bootutil" +pkg.source_files.USBD_DFU: + - src/dfu.c + +pkg.source_files.USBD_DFU_RUNTIME: + - src/dfu_runtime.c + pkg.init.USBD_DFU_AUTO_CONFIRM: dfu_init: 'MYNEWT_VAL(USBD_DFU_SYSINIT_STAGE)' diff --git a/hw/usb/tinyusb/dfu/src/dfu_runtime.c b/hw/usb/tinyusb/dfu/src/dfu_runtime.c new file mode 100644 index 000000000..685a8a302 --- /dev/null +++ b/hw/usb/tinyusb/dfu/src/dfu_runtime.c @@ -0,0 +1,41 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include <os/mynewt.h> +#include <bsp/bsp.h> + +#include <class/dfu/dfu_device.h> +#include <tinyusb/tinyusb.h> +#include <hal/hal_nvreg.h> + +/* + * DFU callbacks + * Note: alt is used as the partition number, in order to support multiple + * partitions like FLASH, EEPROM, etc. + */ + +/* Invoked when a DFU_DETACH request is received and bitWillDetach is set */ +void +tud_dfu_runtime_reboot_to_dfu_cb(void) +{ + _Static_assert(MYNEWT_VAL_USBD_DFU_MAGIC_NVREG >= 0, "No NVReg specified"); + /* Write magic value to NVReg so bootloader will start in USB DFU mode */ + hal_nvreg_write(MYNEWT_VAL_USBD_DFU_MAGIC_NVREG, MYNEWT_VAL_USBD_DFU_MAGIC_VALUE); + hal_system_reset(); +} diff --git a/hw/usb/tinyusb/dfu/syscfg.yml b/hw/usb/tinyusb/dfu/syscfg.yml index f2cc08471..95229aa44 100644 --- a/hw/usb/tinyusb/dfu/syscfg.yml +++ b/hw/usb/tinyusb/dfu/syscfg.yml @@ -130,6 +130,9 @@ syscfg.defs: syscfg.vals: USBD_DFU: 1 +syscfg.vals.USBD_DFU_RUNTIME: + USBD_DFU: 0 + syscfg.logs: USBD_DFU_LOG: module: MYNEWT_VAL(USBD_DFU_LOG_MODULE)
