You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by je...@apache.org on 2025/11/07 16:53:31 UTC

(mynewt-core) branch master updated (bfe1f8529 -> e77638680)

This is an automated email from the ASF dual-hosted git repository.

jerzy pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-core.git


    from bfe1f8529 drivers/adxl345: Register command statically
     new 09f4f8d2e tinyusb/dfu: Add option to enter booloader on reset pin
     new e77638680 tinyusb/dfu: Add runtime DFU support

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 hw/usb/tinyusb/dfu/pkg.yml                         |  8 ++++
 hw/usb/tinyusb/dfu/src/dfu.c                       | 44 +++++++++++++++++++---
 .../tinyusb/dfu/src/dfu_runtime.c}                 | 28 +++++++++++---
 hw/usb/tinyusb/dfu/syscfg.yml                      | 26 +++++++++++++
 4 files changed, 95 insertions(+), 11 deletions(-)
 copy hw/{bsp/stm32f3discovery/link/include/memory_regions.ld.h => usb/tinyusb/dfu/src/dfu_runtime.c} (59%)


(mynewt-core) 01/02: tinyusb/dfu: Add option to enter booloader on reset pin

Posted by je...@apache.org.
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 <je...@codecoup.pl>
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 <je...@apache.org>
---
 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.


(mynewt-core) 02/02: tinyusb/dfu: Add runtime DFU support

Posted by je...@apache.org.
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 <je...@codecoup.pl>
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 <je...@apache.org>
---
 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)