Skip to content

Commit

Permalink
tinyusb/dfu: Add option to enter booloader on reset pin
Browse files Browse the repository at this point in the history
For device that don't have any button to use to enter bootloader
this adds possibility to enter bootloader after number of
pin resets. Number of resets is kept in NVReg of user choice.

Additionally if user application enters correct value into NVReg
and calls hal_system_reset it can directly start bootloader
with dfu capabilities.

Signed-off-by: Jerzy Kasenberg <jerzy@apache.org>
  • Loading branch information
kasjer committed Jun 20, 2024
1 parent ec704b6 commit 1e45c3d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
19 changes: 19 additions & 0 deletions hw/usb/tinyusb/dfu/src/dfu.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@

#include <os/mynewt.h>

#include <sysflash/sysflash.h>
#include <class/dfu/dfu_device.h>

#include <bsp/bsp.h>
#include <img_mgmt/img_mgmt.h>
#include <tinyusb/tinyusb.h>
#include <hal/hal_gpio.h>
#include <hal/hal_nvreg.h>
#include <tusb.h>

/*
* If DFU is activated from bootloader it writes to SLOT0.
Expand Down Expand Up @@ -182,6 +185,22 @@ boot_preboot(void)
tinyusb_start();
}
hal_gpio_deinit(MYNEWT_VAL(USBD_DFU_BOOT_PIN));
#elif MYNEWT_VAL(USBD_DFU_BOOT_RESET_COUNT_NVREG) >= 0
uint32_t counter = hal_nvreg_read(MYNEWT_VAL(USBD_DFU_BOOT_RESET_COUNT_NVREG));
/* If special value is stored in counter register or counter due to reset pin was done number of times start dfu */
if (counter == MYNEWT_VAL(USBD_DFU_BOOT_RESET_COUNT_NVREG) ||
((MYNEWT_VAL(USBD_DFU_BOOT_RESET_COUNT) > 0) && counter >= MYNEWT_VAL(USBD_DFU_BOOT_RESET_COUNT))) {
hal_nvreg_write(MYNEWT_VAL(USBD_DFU_BOOT_RESET_COUNT_NVREG), 0);
tinyusb_start();
} else {
if (hal_reset_cause() == HAL_RESET_PIN) {
/* Reset pin count increment */
hal_nvreg_write(MYNEWT_VAL(USBD_DFU_BOOT_RESET_COUNT_NVREG), counter + 1);
} else if (counter) {
/* Write 0 if not already there */
hal_nvreg_write(MYNEWT_VAL(USBD_DFU_BOOT_RESET_COUNT_NVREG), 0);
}
}
#endif
}

Expand Down
16 changes: 16 additions & 0 deletions hw/usb/tinyusb/dfu/syscfg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,22 @@ syscfg.defs:
Set to 2 if boto pin needs internal pull-down resistor.
value: 0

USBD_DFU_BOOT_RESET_COUNT:
description: >
Number of time bootloader must detects reset cause by reset pin before
starting dfu bootloader.
value: 0
USBD_DFU_BOOT_RESET_COUNT_NVREG:
description: >
NVReg number to use for reset counting.
value: -1
USBD_DFU_BOOT_RESET_COUNT_MAGIC:
description: >
Value stored in NVReg (indexed by USBD_DFU_BOOT_RESET_COUNT_NVREG)
to start DFU. This can be used by application that does not have
DFU support to request DFU in bootloader.
value: 0xDF00AE01

USBD_DFU_SYSINIT_STAGE:
description: >
Sysinit stage for DFU functionality.
Expand Down

0 comments on commit 1e45c3d

Please sign in to comment.