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 09f4f8d2e48be57c50e936cdee5121c6ec5178ac
Author: Jerzy Kasenberg <[email protected]>
AuthorDate: Thu Jun 20 21:52:28 2024 +0200

    tinyusb/dfu: Add option to enter booloader on reset pin
    
    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.
    
    Additionally USBD_DFU_MAGIC_NVREG and USBD_DFU_MAGIC_VALUE
    syscfg values can be directly use to enter DFU mode in boot.
    If this register is written with magic value by application
    after reset bootloader will start in DFU mode.
    
    Signed-off-by: Jerzy Kasenberg <[email protected]>
---
 hw/usb/tinyusb/dfu/src/dfu.c  | 44 ++++++++++++++++++++++++++++++++++++++-----
 hw/usb/tinyusb/dfu/syscfg.yml | 23 ++++++++++++++++++++++
 2 files changed, 62 insertions(+), 5 deletions(-)

diff --git a/hw/usb/tinyusb/dfu/src/dfu.c b/hw/usb/tinyusb/dfu/src/dfu.c
index d52771b68..030332f0a 100644
--- a/hw/usb/tinyusb/dfu/src/dfu.c
+++ b/hw/usb/tinyusb/dfu/src/dfu.c
@@ -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.
@@ -169,14 +172,45 @@ tud_dfu_detach_cb(void)
 void
 boot_preboot(void)
 {
-#if MYNEWT_VAL(USBD_DFU_BOOT_PIN) >= 0
-    hal_gpio_init_in(MYNEWT_VAL(USBD_DFU_BOOT_PIN), 
MYNEWT_VAL(USBD_DFU_BOOT_PIN_PULL));
-    if (hal_gpio_read(MYNEWT_VAL(USBD_DFU_BOOT_PIN)) == 
MYNEWT_VAL(USBD_DFU_BOOT_PIN_VALUE)) {
+    bool start_dfu = false;
+    if (MYNEWT_VAL_USBD_DFU_BOOT_PIN >= 0) {
+        hal_gpio_init_in(MYNEWT_VAL(USBD_DFU_BOOT_PIN),
+                         MYNEWT_VAL(USBD_DFU_BOOT_PIN_PULL));
+        if (hal_gpio_read(MYNEWT_VAL(USBD_DFU_BOOT_PIN)) ==
+            MYNEWT_VAL(USBD_DFU_BOOT_PIN_VALUE)) {
+            hal_gpio_deinit(MYNEWT_VAL(USBD_DFU_BOOT_PIN));
+            start_dfu = true;
+        }
         hal_gpio_deinit(MYNEWT_VAL(USBD_DFU_BOOT_PIN));
+    } else if (MYNEWT_VAL_USBD_DFU_RESET_COUNT_NVREG >= 0) {
+        uint32_t counter = 
hal_nvreg_read(MYNEWT_VAL_USBD_DFU_RESET_COUNT_NVREG);
+        uint32_t new_counter = 0;
+
+        if (counter == MYNEWT_VAL_USBD_DFU_RESET_COUNT) {
+            new_counter = counter;
+        } else if (hal_reset_cause() == HAL_RESET_PIN) {
+            /* Reset pin count increment */
+            new_counter = counter + 1;
+        }
+        if (new_counter == MYNEWT_VAL_USBD_DFU_RESET_COUNT) {
+            start_dfu = true;
+            new_counter = 0;
+        }
+        if (new_counter != counter) {
+            /* Write if value changed */
+            hal_nvreg_write(MYNEWT_VAL_USBD_DFU_RESET_COUNT_NVREG, 
new_counter);
+        }
+    } else if (MYNEWT_VAL_USBD_DFU_MAGIC_NVREG >= 0 &&
+               (hal_nvreg_read(MYNEWT_VAL_USBD_DFU_MAGIC_NVREG) ==
+                MYNEWT_VAL_USBD_DFU_MAGIC_VALUE)) {
+        /* Reset flag in NVReg */
+        hal_nvreg_write(MYNEWT_VAL_USBD_DFU_MAGIC_NVREG, 0);
+        start_dfu = true;
+    }
+
+    if (start_dfu) {
         tinyusb_start();
     }
-    hal_gpio_deinit(MYNEWT_VAL(USBD_DFU_BOOT_PIN));
-#endif
 }
 
 void
diff --git a/hw/usb/tinyusb/dfu/syscfg.yml b/hw/usb/tinyusb/dfu/syscfg.yml
index c50eb3cbd..f2cc08471 100644
--- a/hw/usb/tinyusb/dfu/syscfg.yml
+++ b/hw/usb/tinyusb/dfu/syscfg.yml
@@ -93,6 +93,29 @@ syscfg.defs:
             Set to 2 if boto pin needs internal pull-down resistor.
         value: 0
 
+    USBD_DFU_RESET_COUNT_NVREG:
+        description: >
+            NVReg number to use for reset counting.
+        value: -1
+    USBD_DFU_RESET_COUNT:
+        description: >
+            Number of time bootloader must detects reset cause by reset pin 
before
+            starting dfu bootloader.
+            This can be usefull if board does not have dedicated button that 
can
+            be used to switch to DFU mode during boot.
+            Pressing Reset button this number of times will start DFU in 
bootloader.
+        value: 0
+    USBD_DFU_MAGIC_NVREG:
+        description: >
+            NVReg register number to use in bootloader to start DFU.
+        value: -1
+    USBD_DFU_MAGIC_VALUE:
+        description: >
+            Value stored in NVReg (indexed by USBD_DFU_MAGIC_NVREG)
+            to start DFU. This can be used by application that does not have
+            DFU support to request DFU in bootloader.
+        value: 0xDF00DF00
+
     USBD_DFU_SYSINIT_STAGE:
         description: >
             Sysinit stage for DFU functionality.

Reply via email to