Skip to content

Commit e776386

Browse files
committed
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 <jerzy@apache.org>
1 parent 09f4f8d commit e776386

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

hw/usb/tinyusb/dfu/pkg.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,16 @@ pkg.deps:
2929
- "@apache-mynewt-core/hw/hal"
3030
- "@apache-mynewt-core/kernel/os"
3131
- "@apache-mynewt-core/hw/usb/tinyusb"
32+
33+
pkg.deps.USBD_DFU:
3234
- "@apache-mynewt-core/mgmt/imgmgr"
3335
- "@mcuboot/boot/bootutil"
3436

37+
pkg.source_files.USBD_DFU:
38+
- src/dfu.c
39+
40+
pkg.source_files.USBD_DFU_RUNTIME:
41+
- src/dfu_runtime.c
42+
3543
pkg.init.USBD_DFU_AUTO_CONFIRM:
3644
dfu_init: 'MYNEWT_VAL(USBD_DFU_SYSINIT_STAGE)'
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
#include <os/mynewt.h>
21+
#include <bsp/bsp.h>
22+
23+
#include <class/dfu/dfu_device.h>
24+
#include <tinyusb/tinyusb.h>
25+
#include <hal/hal_nvreg.h>
26+
27+
/*
28+
* DFU callbacks
29+
* Note: alt is used as the partition number, in order to support multiple
30+
* partitions like FLASH, EEPROM, etc.
31+
*/
32+
33+
/* Invoked when a DFU_DETACH request is received and bitWillDetach is set */
34+
void
35+
tud_dfu_runtime_reboot_to_dfu_cb(void)
36+
{
37+
_Static_assert(MYNEWT_VAL_USBD_DFU_MAGIC_NVREG >= 0, "No NVReg specified");
38+
/* Write magic value to NVReg so bootloader will start in USB DFU mode */
39+
hal_nvreg_write(MYNEWT_VAL_USBD_DFU_MAGIC_NVREG, MYNEWT_VAL_USBD_DFU_MAGIC_VALUE);
40+
hal_system_reset();
41+
}

hw/usb/tinyusb/dfu/syscfg.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ syscfg.defs:
130130
syscfg.vals:
131131
USBD_DFU: 1
132132

133+
syscfg.vals.USBD_DFU_RUNTIME:
134+
USBD_DFU: 0
135+
133136
syscfg.logs:
134137
USBD_DFU_LOG:
135138
module: MYNEWT_VAL(USBD_DFU_LOG_MODULE)

0 commit comments

Comments
 (0)