Skip to content

Commit 0ddb3b2

Browse files
committed
Merge branch 'feat/bootloader_uf2' into 'master'
feat(bootloader_uf2): add bootloader uf2 See merge request ae_group/esp-iot-solution!1170
2 parents 5ebc23c + d43b699 commit 0ddb3b2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+919
-41
lines changed

.gitlab/ci/build.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,14 @@ build_example_ulp_lp_cpu_lp_environment_sensor:
700700
variables:
701701
EXAMPLE_DIR: examples/ulp/lp_cpu/lp_environment_sensor
702702

703+
build_example_usb_device_bootloader_uf2:
704+
extends:
705+
- .build_examples_template
706+
- .rules:build:example_usb_device_bootloader_uf2
707+
- .build_idf_version_greater_equal_v5_0
708+
variables:
709+
EXAMPLE_DIR: examples/usb/device/bootloader_uf2
710+
703711
build_example_usb_device_usb_dongle:
704712
extends:
705713
- .build_examples_template

.gitlab/ci/deploy.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ pack-upload_files:
9595
- job: "build_example_display_lcd_rgb_avoid_tearing: [espressif/idf:release-v5.1]"
9696
- job: "build_example_display_lcd_rgb_lcd_8bit: [espressif/idf:release-v5.1]"
9797
- job: "build_example_keyboard: [espressif/idf:release-v5.3]"
98+
- job: "build_example_usb_device_bootloader_uf2: [espressif/idf:release-v5.3]"
9899
- job: "build_example_usb_device_usb_dongle: [espressif/idf:release-v5.3]"
99100
- job: "build_example_usb_device_usb_dual_uvc_device: [espressif/idf:release-v5.3]"
100101
- job: "build_example_usb_device_usb_extend_screen"

.gitlab/ci/rules.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,9 @@
510510
.patterns-example_sensors_sensor_hub_monitor: &patterns-example_sensors_sensor_hub_monitor
511511
- "examples/sensors/sensor_hub_monitor/**/*"
512512

513+
.patterns-example_usb_device_bootloader_uf2: &patterns-example_usb_device_bootloader_uf2
514+
- "examples/usb/device/bootloader_uf2/**/*"
515+
513516
.patterns-example_usb_device_usb_dongle: &patterns-example_usb_device_usb_dongle
514517
- "examples/usb/device/usb_dongle/**/*"
515518

@@ -1256,6 +1259,17 @@
12561259
- <<: *if-dev-push
12571260
changes: *patterns-example_usb_device_usb_lcd_display
12581261

1262+
.rules:build:example_usb_device_bootloader_uf2:
1263+
rules:
1264+
- <<: *if-protected
1265+
- <<: *if-label-build
1266+
- <<: *if-dev-push
1267+
changes: *patterns-build_system
1268+
- <<: *if-dev-push
1269+
changes: *patterns-components_usb_esp_tinyuf2
1270+
- <<: *if-dev-push
1271+
changes: *patterns-example_usb_device_bootloader_uf2
1272+
12591273
.rules:build:example_usb_device_usb_dongle:
12601274
rules:
12611275
- <<: *if-protected

components/usb/esp_tinyuf2/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# ChangeLog
22

3+
## v1.1.0 - 2024-12-11
4+
5+
* Support bootloader uf2
6+
* Support ESP32-P4
7+
38
## v0.2.2 - 2024-06-19
49

510
* Fix usb may not be enumerated after switch from usb-serial-jtag to usb-otg

components/usb/esp_tinyuf2/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
if(BOOTLOADER_BUILD)
2+
idf_component_register()
3+
return()
4+
endif()
5+
16
idf_component_get_property(TUSB_PATH leeebo__tinyusb_src COMPONENT_DIR)
27
idf_component_get_property(DCD_CORE_INCLUDES leeebo__tinyusb_src DCD_CORE_INCLUDES)
38
idf_component_get_property(DCD_CORE_SRCS leeebo__tinyusb_src DCD_CORE_SRCS)

components/usb/esp_tinyuf2/Kconfig

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ menu "TinyUF2 Config"
22
# We not support disable now
33
config ENABLE_UF2_FLASHING
44
bool
5-
default y if IDF_TARGET_ESP32S2 || IDF_TARGET_ESP32S3
5+
default y if IDF_TARGET_ESP32S2 || IDF_TARGET_ESP32S3 || IDF_TARGET_ESP32P4
66
select TINYUSB_SOURCE_CODE_ONLY
77
help
88
Enable UF2, only support ESP SoC with USB-OTG
@@ -38,6 +38,7 @@ menu "TinyUF2 Config"
3838
depends on ENABLE_UF2_FLASHING
3939
default "ESP32S2-UF2" if IDF_TARGET_ESP32S2
4040
default "ESP32S3-UF2" if IDF_TARGET_ESP32S3
41+
default "ESP32P4-UF2" if IDF_TARGET_ESP32P4
4142
config TUSB_MANUFACTURER
4243
string "USB Device Manufacture"
4344
depends on ENABLE_UF2_FLASHING
@@ -59,4 +60,38 @@ menu "TinyUF2 Config"
5960
depends on ENABLE_UF2_FLASHING
6061
default 512
6162
range 256 2048
63+
config UF2_INI_NVS_VALUE_HIDDEN
64+
bool "Enable UF2 ini file hide NVS value"
65+
depends on ENABLE_UF2_FLASHING
66+
default n
67+
config UF2_INI_NVS_HIDDEN_MAX_NUM
68+
int "UF2 ini file hide NVS value max number"
69+
depends on UF2_INI_NVS_VALUE_HIDDEN
70+
default 32
71+
config UF2_OTA_FACTORY_ONLY
72+
bool "OTA Factory Only"
73+
help
74+
Enable this option to limit the OTA updates to the factory partition only.
75+
config UF2_OTA_RESET_REASON_VALUE
76+
hex "OTA Reset Reason Value"
77+
depends on ENABLE_UF2_FLASHING
78+
default 0xF2
79+
help
80+
The reset value for uf2 ota reset.
81+
82+
choice TINYUSB_RHPORT
83+
prompt "TinyUSB Root Port"
84+
default TINYUSB_RHPORT_HS if IDF_TARGET_ESP32P4
85+
default TINYUSB_RHPORT_FS
86+
help
87+
Allows set the USB PHY Controller for TinyUSB: HS (USB OTG2.0 PHY for HighSpeed)
88+
89+
config TINYUSB_RHPORT_HS
90+
depends on IDF_TARGET_ESP32P4
91+
bool "High Speed"
92+
93+
config TINYUSB_RHPORT_FS
94+
bool "Full Speed"
95+
endchoice
96+
6297
endmenu

components/usb/esp_tinyuf2/esp_tinyuf2.c

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
#include "soc/soc_caps.h"
1313
#include "soc/gpio_pins.h"
1414
#include "soc/gpio_sig_map.h"
15+
#if CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
1516
#include "soc/rtc_cntl_struct.h"
17+
#endif
18+
#include "esp_private/system_internal.h"
1619
#include "tusb.h"
1720
#include "esp_tinyuf2.h"
1821
#include "esp_private/usb_phy.h"
@@ -136,13 +139,15 @@ static void usb_device_task(void* param)
136139
}
137140

138141
// To generate a disconnect event
142+
#if CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
139143
static void usbd_vbus_enable(bool enable)
140144
{
141145
esp_rom_gpio_connect_in_signal(enable ? GPIO_MATRIX_CONST_ONE_INPUT : GPIO_MATRIX_CONST_ZERO_INPUT, USB_OTG_VBUSVALID_IN_IDX, 0);
142146
esp_rom_gpio_connect_in_signal(enable ? GPIO_MATRIX_CONST_ONE_INPUT : GPIO_MATRIX_CONST_ZERO_INPUT, USB_SRP_BVALID_IN_IDX, 0);
143147
esp_rom_gpio_connect_in_signal(enable ? GPIO_MATRIX_CONST_ONE_INPUT : GPIO_MATRIX_CONST_ZERO_INPUT, USB_SRP_SESSEND_IN_IDX, 1);
144148
return;
145149
}
150+
#endif
146151

147152
static esp_err_t tinyusb_init()
148153
{
@@ -154,7 +159,11 @@ static esp_err_t tinyusb_init()
154159
// init device stack on configured roothub port
155160
// This should be called after scheduler/kernel is started.
156161
// Otherwise it could cause kernel issue since USB IRQ handler does use RTOS queue API.
157-
tud_init(BOARD_TUD_RHPORT);
162+
bool usb_init = tusb_init();
163+
if (!usb_init) {
164+
ESP_LOGE(TAG, "USB Device Stack Init Fail");
165+
return ESP_FAIL;
166+
}
158167
xTaskCreatePinnedToCore(usb_device_task, "usbd", 4096, NULL, 5, &_task_handle, 0);
159168
return ESP_OK;
160169
}
@@ -171,8 +180,10 @@ static esp_err_t tinyusb_deinit()
171180
{
172181
//prepare to exit the task
173182
xTaskNotify(_task_handle, (uint32_t)xTaskGetCurrentTaskHandle(), eSetValueWithOverwrite);
183+
#if CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
174184
//we give a false signal to make disconnect event happen
175185
usbd_vbus_enable(false);
186+
#endif
176187
//wait for the task exit
177188
uint32_t notify = ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(2000));
178189
if (!notify) {
@@ -202,10 +213,17 @@ esp_err_t esp_tinyuf2_install(tinyuf2_ota_config_t *ota_config, tinyuf2_nvs_conf
202213
}
203214

204215
if (ota_config) {
216+
#ifndef CONFIG_UF2_OTA_FACTORY_ONLY
205217
if (ota_config->subtype < ESP_PARTITION_SUBTYPE_APP_OTA_MIN || (ota_config->subtype > ESP_PARTITION_SUBTYPE_APP_OTA_MAX && ota_config->subtype != ESP_PARTITION_SUBTYPE_ANY)) {
206218
ESP_LOGE(TAG, "Invalid partition type");
207219
return ESP_ERR_INVALID_ARG;
208220
}
221+
#else
222+
if (ota_config->subtype != ESP_PARTITION_SUBTYPE_APP_FACTORY) {
223+
ESP_LOGE(TAG, "Invalid partition type, only support factory partition");
224+
return ESP_ERR_INVALID_ARG;
225+
}
226+
#endif
209227
if (ota_config->if_restart) {
210228
ESP_LOGW(TAG, "Enable restart, SoC will restart after update complete");
211229
}
@@ -252,3 +270,9 @@ tinyuf2_state_t esp_tinyuf2_current_state(void)
252270
{
253271
return _uf2_state;
254272
}
273+
274+
void esp_restart_from_tinyuf2(void)
275+
{
276+
esp_reset_reason_set_hint(UF2_RESET_REASON_VALUE);
277+
esp_restart();
278+
}

components/usb/esp_tinyuf2/esp_tinyuf2.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ extern "C" {
2424
.namespace_name = "tuf2", \
2525
}
2626

27+
#define UF2_RESET_REASON_VALUE (CONFIG_UF2_OTA_RESET_REASON_VALUE)
28+
2729
/**
2830
* @brief user callback called after uf2 update complete
2931
*
@@ -96,6 +98,45 @@ esp_err_t esp_tinyuf2_uninstall(void);
9698
*/
9799
tinyuf2_state_t esp_tinyuf2_current_state(void);
98100

101+
/**
102+
* @brief Restart system and set reset reason to UF2_RESET_REASON_VALUE
103+
*
104+
*/
105+
void esp_restart_from_tinyuf2(void);
106+
107+
#ifdef CONFIG_UF2_INI_NVS_VALUE_HIDDEN
108+
/**
109+
* @brief Set the state of the "all keys hidden" flag.
110+
*
111+
* This function updates the global flag indicating whether all keys
112+
* should be treated as hidden.
113+
*
114+
* @param[in] if_hidden Boolean flag to indicate the hidden state:
115+
* - true: Set all keys as hidden.
116+
* - false: Set all keys as visible.
117+
*
118+
* @return
119+
* - ESP_OK: Operation was successful.
120+
*/
121+
esp_err_t esp_tinyuf2_set_all_key_hidden(bool if_hidden)
122+
123+
/**
124+
* @brief Add a key to the hidden keys list.
125+
*
126+
* This function dynamically allocates memory to store a copy of the
127+
* provided key and adds it to the list of hidden keys. It ensures
128+
* that the maximum limit of hidden keys is not exceeded (CONFIG_UF2_INI_NVS_HIDDEN_MAX_NUM).
129+
*
130+
* @param[in] key A pointer to the null-terminated string representing the key to hide.
131+
*
132+
* @return
133+
* - ESP_OK: Key added successfully.
134+
* - ESP_ERR_INVALID_ARG: Provided key is NULL.
135+
* - ESP_ERR_NO_MEM: Memory allocation failed or maximum number of hidden keys exceeded.
136+
*/
137+
esp_err_t esp_tinyuf2_add_key_hidden(const char *key)
138+
#endif
139+
99140
#ifdef __cplusplus
100141
}
101142
#endif

components/usb/esp_tinyuf2/idf_component.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
version: "1.0.0"
1+
version: "1.1.0"
22
targets:
33
- esp32s2
44
- esp32s3
5+
- esp32p4
56

67
description: Enhanced TinyUF2 for ESP32Sx, support OTA update and NVS operate through USB MSC
78
url: https://github.com/espressif/esp-iot-solution
@@ -14,11 +15,13 @@ dependencies:
1415
cmake_utilities: "0.*"
1516
leeebo/esp-inih: "0.*"
1617
leeebo/tinyusb_src:
17-
version: ">=0.16.0~5"
18+
version: "==0.16.0~6"
1819

1920
examples:
2021
- path: ../../../examples/usb/device/usb_uf2_ota
2122
- path: ../../../examples/usb/device/usb_uf2_nvs
23+
- path: ../../../examples/usb/device/bootloader_uf2
24+
2225
sbom:
2326
supplier: 'Organization: Espressif Systems (Shanghai) CO LTD'
2427
originator: 'Organization: Espressif Systems (Shanghai) CO LTD'

components/usb/esp_tinyuf2/private_include/board_flash.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
#define BOARD_UF2_FAMILY_ID 0xbfdd4eee
4848
#elif CONFIG_IDF_TARGET_ESP32S3
4949
#define BOARD_UF2_FAMILY_ID 0xc47e5767
50+
#elif CONFIG_IDF_TARGET_ESP32P4
51+
#define BOARD_UF2_FAMILY_ID 0x3d308e94
5052
#else
5153
#error unsupported MCUs
5254
#endif

0 commit comments

Comments
 (0)