Skip to content

Commit c73cb90

Browse files
committed
Genericize MSC callback, allowing application to follow update progress
1 parent 998a86e commit c73cb90

File tree

5 files changed

+27
-11
lines changed

5 files changed

+27
-11
lines changed

components/usb/esp_tinyuf2/esp_tinyuf2.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ esp_err_t esp_tinyuf2_install(tinyuf2_ota_config_t *ota_config, tinyuf2_nvs_conf
229229
if (ota_config->if_restart) {
230230
ESP_LOGW(TAG, "Enable restart, SoC will restart after update complete");
231231
}
232-
board_flash_init(ota_config->subtype, ota_config->label, ota_config->complete_cb, ota_config->if_restart);
232+
board_flash_init(ota_config->subtype, ota_config->label, ota_config->event_cb, ota_config->if_restart);
233233
}
234234

235235
if (nvs_config) {

components/usb/esp_tinyuf2/esp_tinyuf2.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,18 @@ extern "C" {
2626

2727
#define UF2_RESET_REASON_VALUE (CONFIG_UF2_OTA_RESET_REASON_VALUE)
2828

29+
typedef enum {
30+
TINYUF2_UPDATE_COMPLETE = 0,
31+
TINYUF2_UPDATE_PCT = 1,
32+
TINYUF2_UPDATE_MOUNT = 2,
33+
TINYUF2_UPDATE_EJECT = 3
34+
} uf2_update_event_t;
35+
2936
/**
3037
* @brief user callback called after uf2 update complete
3138
*
3239
*/
33-
typedef void (*update_complete_cb_t)(void);
40+
typedef void (*update_event_cb_t)(uf2_update_event_t, uint32_t);
3441

3542
/**
3643
* @brief user callback called after nvs modified
@@ -46,7 +53,7 @@ typedef struct {
4653
esp_partition_subtype_t subtype; /*!< Partition subtype. if ESP_PARTITION_SUBTYPE_ANY will use the next_update_partition by default. */
4754
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.*/
4855
bool if_restart; /*!< if restart system to new app partition after UF2 flashing done */
49-
update_complete_cb_t complete_cb; /*!< user callback called after uf2 update complete */
56+
update_event_cb_t event_cb; /*!< user callback called after uf2 update complete */
5057
} tinyuf2_ota_config_t;
5158

5259
/**

components/usb/esp_tinyuf2/msc/msc.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ void tud_msc_write10_complete_cb(uint8_t lun)
184184
ESP_LOGI(TAG, "STATE_WRITING_FINISHED");
185185
board_dfu_complete();
186186
}
187+
else
188+
board_event_cb(TINYUF2_UPDATE_PCT, _wr_state.numWritten * 100 / _wr_state.numBlocks);
187189
}
188190
}
189191

components/usb/esp_tinyuf2/private_include/board_flash.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ extern char *_ini_file_dummy;
8383
// DFU is complete, should reset or jump to application mode and not return
8484
void board_dfu_complete(void);
8585

86+
// Access to user callback from msc.c, etc
87+
void board_event_cb(uf2_update_event_t, uint32_t);
88+
8689
// Fill Serial Number and return its length (limit to 16 bytes)
8790
uint8_t board_usb_get_serial(uint8_t serial_id[16]);
8891

@@ -91,7 +94,7 @@ uint8_t board_usb_get_serial(uint8_t serial_id[16]);
9194
//--------------------------------------------------------------------+
9295

9396
// Initialize flash for DFU
94-
void board_flash_init(esp_partition_subtype_t subtype, const char *label, update_complete_cb_t complete_cb, bool if_restart);
97+
void board_flash_init(esp_partition_subtype_t subtype, const char *label, update_event_cb_t event_cb, bool if_restart);
9598
void board_flash_deinit(void);
9699

97100
// Initialize flash for NVS

components/usb/esp_tinyuf2/uf2/board_flash.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
static uint32_t _fl_addr = FLASH_CACHE_INVALID_ADDR;
5858
static uint8_t *_fl_buf = NULL;
5959
static bool _if_restart = false;
60-
static update_complete_cb_t _complete_cb = NULL;
60+
static update_event_cb_t _event_cb = NULL;
6161
static esp_partition_t const* _part_ota = NULL;
6262
char *_ini_file = NULL;
6363
char *_ini_file_dummy = NULL;
@@ -77,14 +77,17 @@ uint8_t board_usb_get_serial(uint8_t serial_id[16])
7777
return 6;
7878
}
7979

80+
void board_event_cb(uf2_update_event_t e, uint32_t p) {
81+
if (!_event_cb)
82+
return;
83+
_event_cb(e, p);
84+
}
85+
8086
void board_dfu_complete(void)
8187
{
8288
esp_ota_set_boot_partition(_part_ota);
8389

84-
if (_complete_cb) {
85-
PRINTF("dfu_complete: run user callback");
86-
_complete_cb();
87-
}
90+
board_event_cb(TINYUF2_UPDATE_COMPLETE, 0);
8891

8992
if (_if_restart) {
9093
/* code */
@@ -94,11 +97,11 @@ void board_dfu_complete(void)
9497
}
9598
}
9699

97-
void board_flash_init(esp_partition_subtype_t subtype, const char *label, update_complete_cb_t complete_cb, bool if_restart)
100+
void board_flash_init(esp_partition_subtype_t subtype, const char *label, update_event_cb_t event_cb, bool if_restart)
98101
{
99102
_fl_addr = FLASH_CACHE_INVALID_ADDR;
100103
_if_restart = if_restart;
101-
_complete_cb = complete_cb;
104+
_event_cb = event_cb;
102105

103106
if (subtype == ESP_PARTITION_SUBTYPE_ANY) {
104107
_part_ota = esp_ota_get_next_update_partition(NULL);
@@ -261,6 +264,7 @@ static int nvs_write_back(void* user, const char* section, const char* name,
261264
{
262265
if (check_value_if_hidden(name) && strcmp(value, NVS_HIDDEN_PLACEHOLDER) == 0)
263266
{
267+
PRINTFD("Ignore %s", name);
264268
return 1;
265269
}
266270
nvs_handle_t nvs = (nvs_handle_t)user;

0 commit comments

Comments
 (0)