Skip to content

Commit

Permalink
Merge branch 'docs/esp_tinyuf2' into 'master'
Browse files Browse the repository at this point in the history
docs(esp_tinyuf2): add api reference

Closes AEG-1008

See merge request ae_group/esp-iot-solution!1084
  • Loading branch information
leeebo committed Oct 15, 2024
2 parents b4e05ec + 23bdc16 commit 6d8e9c9
Show file tree
Hide file tree
Showing 14 changed files with 279 additions and 126 deletions.
21 changes: 11 additions & 10 deletions .gitlab/ci/rules.yml
Original file line number Diff line number Diff line change
Expand Up @@ -308,10 +308,18 @@
- "**/*.md"

.patterns-docs_inc: &patterns-docs_inc

- "components/audio/dac_audio/include/dac_audio.h"
- "components/audio/pwm_audio/include/pwm_audio.h"
- "components/bluetooth/ble_conn_mgr/include/esp_ble_conn_mgr.h"
- "components/bluetooth/ble_conn_mgr/include/esp_ble_conn_mgr.h"
- "components/bluetooth/ble_hci/include/ble_hci.h"
- "components/bluetooth/ble_services/ans/include/esp_ans.h"
- "components/bluetooth/ble_services/bas/include/esp_bas.h"
- "components/bluetooth/ble_services/dis/include/esp_dis.h"
- "components/bluetooth/ble_services/hrs/include/esp_hrs.h"
- "components/bluetooth/ble_services/hts/include/esp_hts.h"
- "components/bluetooth/ble_services/tps/include/esp_tps.h"
- "components/bus/include/spi_bus.h"
- "components/button/include/iot_button.h"
- "components/display/screen/interface_driver/scr_interface_driver.h"
Expand All @@ -329,28 +337,21 @@
- "components/motor/esp_sensorless_bldc_control/user_cfg/bldc_user_cfg.h"
- "components/motor/servo/include/iot_servo.h"
- "components/openai/include/OpenAI.h"
- "components/sensors/ntc_driver/include/ntc_driver.h"
- "components/sensors/power_monitor/ina236/include/ina236.h"
- "components/sensors/sensor_hub/include/hal/humiture_hal.h"
- "components/sensors/sensor_hub/include/hal/imu_hal.h"
- "components/sensors/sensor_hub/include/hal/light_sensor_hal.h"
- "components/sensors/sensor_hub/include/iot_sensor_hub.h"
- "components/sensors/sensor_hub/include/sensor_type.h"
- "components/sensors/ntc_driver/include/ntc_driver.h"
- "components/sensors/power_monitor/ina236/include/ina236.h"
- "components/touch/touch_proximity_sensor/include/touch_proximity_sensor.h"
- "components/usb/esp_msc_ota/include/esp_msc_ota.h"
- "components/usb/esp_tinyuf2/esp_tinyuf2.h"
- "components/usb/iot_usbh_cdc/include/iot_usbh_cdc.h"
- "components/usb/usb_device_uac/include/usb_device_uac.h"
- "components/usb/usb_device_uvc/include/usb_device_uvc.h"
- "components/usb/usb_stream/include/usb_stream.h"
- "components/zero_detection/include/zero_detection.h"
- "components/bluetooth/ble_conn_mgr/include/esp_ble_conn_mgr.h"
- "components/bluetooth/ble_services/ans/include/esp_ans.h"
- "components/bluetooth/ble_services/bas/include/esp_bas.h"
- "components/bluetooth/ble_services/dis/include/esp_dis.h"
- "components/bluetooth/ble_services/hrs/include/esp_hrs.h"
- "components/bluetooth/ble_services/hts/include/esp_hts.h"
- "components/bluetooth/ble_services/tps/include/esp_tps.h"

# examples folder, in the alphabetic order
.patterns-example_audio_wav_player: &patterns-example_audio_wav_player
- "examples/audio/**/*"
Expand Down
95 changes: 7 additions & 88 deletions components/usb/esp_tinyuf2/README.md
Original file line number Diff line number Diff line change
@@ -1,96 +1,15 @@
## ESP TinyUF2

The enhanced version of [TinyUF2](https://github.com/adafruit/tinyuf2) for ESP32Sx, which supports over-the-air (OTA) updates for APP. **and supports dumping NVS key-value pairs to the config file, users can modify and write back to NVS**.
`esp_tinyuf2` is an enhanced version of [TinyUF2](https://github.com/adafruit/tinyuf2) for ESP chips with USB-OTG support. Which features:

UF2 is a file format developed by Microsoft for [PXT](https://github.com/Microsoft/pxt), that is particularly suitable for flashing microcontrollers over MSC (Mass Storage Class). For a more friendly explanation, check out [this blog post](https://makecode.com/blog/one-chip-to-flash-them-all).
- support over-the-air (OTA) updates through the virtual USB drive
- support dumping NVS key-value pairs to ini file in the virtual USB drive
- support modify ini file and write back to NVS

## Support UF2 OTA/NVS in Your Project
![](./uf2_disk.png)

1. Add the component to your project using `idf.py add_dependency` command.

```sh
idf.py add-dependency "esp_tinyuf2"
```
### How to use

2. Customer your partition table. Like other OTA solutions, you need to reserve at least two OTA app partitions. Please refer to [Partition Tables](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/partition-tables.html) and [usb_uf2_ota](../../../examples/usb/device/usb_uf2_ota/) example for details.
[esp_tinyuf2 user guide](https://docs.espressif.com/projects/esp-iot-solution/en/latest/usb/usb_device/esp_tinyuf2.html)

```
# Partition Table Example
# Name, Type, SubType, Offset, Size, Flags
nvs, data, nvs, , 0x4000,
otadata, data, ota, , 0x2000,
phy_init, data, phy, , 0x1000,
ota_0, app, ota_0, , 1500K,
ota_1, app, ota_1, , 1500K,
```

3. Using `idf.py menuconfig` to config the component's behavior in `(Top) → Component config → TinyUF2 Config`
* `USB Virtual Disk size(MB)`: The size of the virtual U-disk shows in File Explorer, 8MB by default
* `Max APP size(MB)`: Maximum APP size, 4MB by default
* `Flash cache size(KB)`: Cache size used for writing Flash efficiently, 32KB by default
* `USB Device VID`: Espressif VID (0x303A) by default
* `USB Device PID`: Espressif test PID (0x8000) by default, refer [esp-usb-pid](https://github.com/espressif/usb-pids) to apply new.
* `USB Disk Name`: The name of the virtual U-disk shows in File Explorer, "ESP32Sx-UF2" by default
* `USB Device Manufacture`: "Espressif" by default
* `Product Name`: "ESP TinyUF2" by default
* `Product ID`: 12345678 by default
* `Product URL`: A `index` file will be added to the U-disk, users can click to goto the webpage, "https://products.espressif.com/" by default
* `UF2 NVS ini file size`: The `ini` file size prepares for NVS function
4. Install tinyuf2 function like below, for more details, please refer example `usb_uf2_nvs` and `usb_uf2_ota`
```c
/* install UF2 OTA */
tinyuf2_ota_config_t ota_config = DEFAULT_TINYUF2_OTA_CONFIG();
ota_config.complete_cb = uf2_update_complete_cb;
/* disable auto restart, if false manual restart later */
ota_config.if_restart = false;
/* install UF2 NVS */
tinyuf2_nvs_config_t nvs_config = DEFAULT_TINYUF2_NVS_CONFIG();
nvs_config.part_name = "nvs";
nvs_config.namespace_name = "myuf2";
nvs_config.modified_cb = uf2_nvs_modified_cb;
esp_tinyuf2_install(&ota_config, &nvs_config);
```
5. Run `idf.py build flash` for the initial download, later `idf.py uf2-ota` can be used to generate new `uf2` app bin
6. Drag and drop UF2 format file to the disk, to upgrade to new `uf2` app bin
![UF2 Disk](./uf2_disk.png)
## Enable UF2 USB Console
If enable UF2 USB console `(Top) → Component config → TinyUF2 Config → Enable USB Console For log`, the log will be output to the USB Serial port (Output to UART by default).
## Build APP to UF2 format
The new command `idf.py uf2-ota` is added by this component, which can be used to build the APP to UF2 format. After the build is complete, the UF2 file (`${PROJECT_NAME}.uf2`) will be generated in the current `project` directory.
```sh
idf.py uf2-ota
```
## Convert Existing APP to UF2 Format
To convert your existing APP binary to UF2 format, simply use the [uf2conv.py](./utils/uf2conv.py) on a `.bin` file, specifying the family id as `ESP32S2`, `ESP32S3` or their magic number as follows. And you must specify the address of 0x00 with the `-b` switch, the tinyuf2 will use it as offset to write to the OTA partition.
1. convert as follows
using:
```sh
$ENV{UF2_UTILS_PATH}/uf2conv.py your_firmware.bin -c -b 0x00 -f ESP32S3
```
or:
```sh
$ENV{UF2_UTILS_PATH}/uf2conv.py your_firmware.bin -c -b 0x00 -f 0xc47e5767
```
## Note
* To use the UF2 OTA function continuously, the TinyUF2 function must be enabled in the updated APP.
24 changes: 14 additions & 10 deletions components/usb/esp_tinyuf2/esp_tinyuf2.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,26 +41,29 @@ typedef void (*nvs_modified_cb_t)(void);
*
*/
typedef struct {
esp_partition_subtype_t subtype; /*! Partition subtype. if ESP_PARTITION_SUBTYPE_ANY will use the next_update_partition by default. */
const char *label; /*! Partition label. Set this value if looking for partition with a specific name. if subtype==ESP_PARTITION_SUBTYPE_ANY, label default to NULL.*/
bool if_restart; /*! if restart system to new app partition after UF2 flashing done */
update_complete_cb_t complete_cb; /*! user callback called after uf2 update complete */
esp_partition_subtype_t subtype; /*!< Partition subtype. if ESP_PARTITION_SUBTYPE_ANY will use the next_update_partition by default. */
const char *label; /*!< Partition label. Set this value if looking for partition with a specific name. if subtype==ESP_PARTITION_SUBTYPE_ANY, label default to NULL.*/
bool if_restart; /*!< if restart system to new app partition after UF2 flashing done */
update_complete_cb_t complete_cb; /*!< user callback called after uf2 update complete */
} tinyuf2_ota_config_t;

/**
* @brief tinyuf2 nvs configurations
*
*/
typedef struct {
const char *part_name; /*! Partition name. */
const char *namespace_name; /*! Namespace name. */
nvs_modified_cb_t modified_cb; /*! user callback called after uf2 update complete */
const char *part_name; /*!< Partition name. */
const char *namespace_name; /*!< Namespace name. */
nvs_modified_cb_t modified_cb; /*!< user callback called after uf2 update complete */
} tinyuf2_nvs_config_t;

/**
* @brief tinyuf2 current state
*/
typedef enum {
TINYUF2_STATE_NOT_INSTALLED = 0,
TINYUF2_STATE_INSTALLED,
TINYUF2_STATE_MOUNTED,
TINYUF2_STATE_NOT_INSTALLED = 0, /*!< tinyuf2 driver not installed */
TINYUF2_STATE_INSTALLED, /*!< tinyuf2 driver installed */
TINYUF2_STATE_MOUNTED, /*!< USB mounted */
} tinyuf2_state_t;

/**
Expand All @@ -81,6 +84,7 @@ esp_err_t esp_tinyuf2_install(tinyuf2_ota_config_t *ota_config, tinyuf2_nvs_conf
* @note not release memory due to tinyusb not support teardown
*
* @return esp_err_t
* - ESP_ERR_INVALID_STATE tinyuf2 not installed
* - ESP_OK Success
*/
esp_err_t esp_tinyuf2_uninstall(void);
Expand Down
3 changes: 2 additions & 1 deletion components/usb/esp_tinyuf2/idf_component.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
version: "0.2.2"
version: "1.0.0"
targets:
- esp32s2
- esp32s3

description: Enhanced TinyUF2 for ESP32Sx, support OTA update and NVS operate through USB MSC
url: https://github.com/espressif/esp-iot-solution
repository: https://github.com/espressif/esp-iot-solution.git
documentation: https://docs.espressif.com/projects/esp-iot-solution/en/latest/usb/usb_device/esp_tinyuf2.html
issues: https://github.com/espressif/esp-iot-solution/issues

dependencies:
Expand Down
5 changes: 3 additions & 2 deletions docs/Doxyfile
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,16 @@ INPUT = \
$(PROJECT_PATH)/components/motor/esp_sensorless_bldc_control/user_cfg/bldc_user_cfg.h \
$(PROJECT_PATH)/components/motor/servo/include/iot_servo.h \
$(PROJECT_PATH)/components/openai/include/OpenAI.h \
$(PROJECT_PATH)/components/sensors/ntc_driver/include/ntc_driver.h \
$(PROJECT_PATH)/components/sensors/power_monitor/ina236/include/ina236.h \
$(PROJECT_PATH)/components/sensors/sensor_hub/include/hal/humiture_hal.h \
$(PROJECT_PATH)/components/sensors/sensor_hub/include/hal/imu_hal.h \
$(PROJECT_PATH)/components/sensors/sensor_hub/include/hal/light_sensor_hal.h \
$(PROJECT_PATH)/components/sensors/sensor_hub/include/iot_sensor_hub.h \
$(PROJECT_PATH)/components/sensors/sensor_hub/include/sensor_type.h \
$(PROJECT_PATH)/components/sensors/ntc_driver/include/ntc_driver.h \
$(PROJECT_PATH)/components/sensors/power_monitor/ina236/include/ina236.h \
$(PROJECT_PATH)/components/touch/touch_proximity_sensor/include/touch_proximity_sensor.h \
$(PROJECT_PATH)/components/usb/esp_msc_ota/include/esp_msc_ota.h \
$(PROJECT_PATH)/components/usb/esp_tinyuf2/esp_tinyuf2.h \
$(PROJECT_PATH)/components/usb/iot_usbh_cdc/include/iot_usbh_cdc.h \
$(PROJECT_PATH)/components/usb/usb_device_uac/include/usb_device_uac.h \
$(PROJECT_PATH)/components/usb/usb_device_uvc/include/usb_device_uvc.h \
Expand Down
Binary file added docs/_static/usb/uf2_disk.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
113 changes: 113 additions & 0 deletions docs/en/usb/usb_device/esp_tinyuf2.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
ESP TinyUF2
============

:link_to_translation:`zh_CN:[中文]`

``esp_tinyuf2`` is an enhanced version of `TinyUF2 <https://github.com/adafruit/tinyuf2>`__ for ESP chips with USB support. Which features:

- support over-the-air (OTA) updates through the virtual USB drive
- support dumping NVS key-value pairs to ini file in the virtual USB drive
- support modify ini file and write back to NVS

UF2 is a file format developed by Microsoft for `PXT <https://github.com/Microsoft/pxt>`__, that is particularly suitable for flashing microcontrollers over MSC (Mass Storage Class). For a more friendly explanation, check out `the blog post <https://makecode.com/blog/one-chip-to-flash-them-all>`__.

Support UF2 OTA/NVS in Your Project
-----------------------------------

1. Add the component to your project using ``idf.py add_dependency`` command.

.. code:: sh
idf.py add-dependency "esp_tinyuf2"
2. Customer your partition table. Like other OTA solutions, you need to reserve at least two OTA app partitions. Please refer to `Partition Tables <https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/partition-tables.html>`__ and `usb_uf2_ota <https://github.com/espressif/esp-iot-solution/tree/master/examples/usb/device/usb_uf2_ota>`__ example for details.

::

# Partition Table Example
# Name, Type, SubType, Offset, Size, Flags
nvs, data, nvs, , 0x4000,
otadata, data, ota, , 0x2000,
phy_init, data, phy, , 0x1000,
ota_0, app, ota_0, , 1500K,
ota_1, app, ota_1, , 1500K,

3. Using ``idf.py menuconfig`` to config the component’s behavior in ``(Top) → Component config → TinyUF2 Config``

- ``USB Virtual Disk size(MB)``: The size of the virtual U-disk shows in File Explorer, 8MB by default
- ``Max APP size(MB)``: Maximum APP size, 4MB by default
- ``Flash cache size(KB)``: Cache size used for writing Flash efficiently, 32KB by default
- ``USB Device VID``: Espressif VID (0x303A) by default
- ``USB Device PID``: Espressif test PID (0x8000) by default, refer `esp-usb-pid <https://github.com/espressif/usb-pids>`__ to apply new.
- ``USB Disk Name``: The name of the virtual U-disk shows in File Explorer, ``ESP32Sx-UF2`` by default
- ``USB Device Manufacture``: ``Espressif`` by default
- ``Product Name``: ``ESP TinyUF2`` by default
- ``Product ID``: ``12345678`` by default
- ``Product URL``: A ``index`` file will be added to the U-disk, users can click to goto the webpage, ``https://products.espressif.com/`` by default
- ``UF2 NVS ini file size``: The ``ini`` file size prepares for NVS function

4. Install tinyuf2 function like below, for more details, please refer example `usb_uf2_nvs <https://github.com/espressif/esp-iot-solution/tree/master/examples/usb/device/usb_uf2_nvs>`__ and `usb_uf2_ota <https://github.com/espressif/esp-iot-solution/tree/master/examples/usb/device/usb_uf2_ota>`__

.. code:: c
/* install UF2 OTA */
tinyuf2_ota_config_t ota_config = DEFAULT_TINYUF2_OTA_CONFIG();
ota_config.complete_cb = uf2_update_complete_cb;
/* disable auto restart, if false manual restart later */
ota_config.if_restart = false;
/* install UF2 NVS */
tinyuf2_nvs_config_t nvs_config = DEFAULT_TINYUF2_NVS_CONFIG();
nvs_config.part_name = "nvs";
nvs_config.namespace_name = "myuf2";
nvs_config.modified_cb = uf2_nvs_modified_cb;
esp_tinyuf2_install(&ota_config, &nvs_config);
5. Run ``idf.py build flash`` for the initial download, later ``idf.py uf2-ota`` can be used to generate new ``uf2`` app bin

6. Drag and drop UF2 format file to the disk, to upgrade to new ``uf2`` app bin

.. figure:: ../../../_static/usb/uf2_disk.png
:alt: UF2 Disk

Enable UF2 USB Console
-----------------------

Through menuconfig ``(Top) → Component config → TinyUF2 Config → Enable USB Console For log``, the log will be output to the USB Serial port (Output to UART by default).

Build APP to UF2 format
-----------------------

The new command ``idf.py uf2-ota`` is added by this component, which can be used to build the APP to UF2 format. After the build is complete, the UF2 file (``${PROJECT_NAME}.uf2``) will be generated in the current ``project`` directory.

.. code:: sh
idf.py uf2-ota
Convert Existing APP to UF2 Format
----------------------------------

To convert your existing APP binary to UF2 format, simply use the `uf2conv.py <https://github.com/espressif/esp-iot-solution/blob/master/components/usb/esp_tinyuf2/utils/uf2conv.py>`__ on a ``.bin`` file, specifying the family id as ``ESP32S2``, ``ESP32S3`` or their magic number as follows. And you must specify the address of 0x00 with the ``-b`` switch, the tinyuf2 will use it as offset to write to the OTA partition.

1. convert as follows

using:

.. code:: sh
uf2conv.py your_firmware.bin -c -b 0x00 -f ESP32S3
or:

.. code:: sh
uf2conv.py your_firmware.bin -c -b 0x00 -f 0xc47e5767
Note
----

- To use the UF2 OTA function continuously, the TinyUF2 function must be enabled in the updated APP.

API Reference
-------------

.. include-build-file:: inc/esp_tinyuf2.inc
3 changes: 2 additions & 1 deletion docs/en/usb/usb_device/usb_driver_index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ USB Device Drivers
:maxdepth: 1

USB UVC Device <usb_device_uvc>
USB UAC Device <usb_device_uac>
USB UAC Device <usb_device_uac>
USB UF2 Device <esp_tinyuf2>
Loading

0 comments on commit 6d8e9c9

Please sign in to comment.