diff --git a/hw/usb/tinyusb/dfu/src/dfu.c b/hw/usb/tinyusb/dfu/src/dfu.c index 17521bb431..316bf4e14c 100644 --- a/hw/usb/tinyusb/dfu/src/dfu.c +++ b/hw/usb/tinyusb/dfu/src/dfu.c @@ -19,12 +19,15 @@ #include +#include #include #include #include #include #include +#include +#include /* * If DFU is activated from bootloader it writes to SLOT0. @@ -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 } diff --git a/hw/usb/tinyusb/dfu/syscfg.yml b/hw/usb/tinyusb/dfu/syscfg.yml index c50eb3cbd2..88f0cad228 100644 --- a/hw/usb/tinyusb/dfu/syscfg.yml +++ b/hw/usb/tinyusb/dfu/syscfg.yml @@ -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.