From 6f7a6b1eeab471013a381846c2934cd80c390874 Mon Sep 17 00:00:00 2001 From: Saurabh Kumar Bansal Date: Mon, 15 May 2023 18:26:47 +0530 Subject: [PATCH] usb: update usb device msc example by adding new API tinyusb_msc_register_callback --- .../usb/device/tusb_msc/main/tusb_msc_main.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/examples/peripherals/usb/device/tusb_msc/main/tusb_msc_main.c b/examples/peripherals/usb/device/tusb_msc/main/tusb_msc_main.c index bd3055d7ec..c1d4684690 100644 --- a/examples/peripherals/usb/device/tusb_msc/main/tusb_msc_main.c +++ b/examples/peripherals/usb/device/tusb_msc/main/tusb_msc_main.c @@ -231,12 +231,19 @@ static int console_status(int argc, char **argv) // exit from application static int console_exit(int argc, char **argv) { + tinyusb_msc_unregister_callback(TINYUSB_MSC_EVENT_MOUNT_CHANGED); tinyusb_msc_storage_deinit(); printf("Application Exiting\n"); exit(0); return 0; } +// callback that is delivered when storage is mounted/unmounted by application. +static void storage_mount_changed_cb(tinyusb_msc_event_t *event) +{ + ESP_LOGI(TAG, "Storage mounted to application: %s", event->mount_changed_data.is_mounted ? "Yes" : "No"); +} + #ifdef CONFIG_EXAMPLE_STORAGE_MEDIA_SPIFLASH static esp_err_t storage_init_spiflash(wl_handle_t *wl_handle) { @@ -339,17 +346,21 @@ void app_main(void) ESP_ERROR_CHECK(storage_init_spiflash(&wl_handle)); const tinyusb_msc_spiflash_config_t config_spi = { - .wl_handle = wl_handle + .wl_handle = wl_handle, + .callback_mount_changed = storage_mount_changed_cb /* First way to register the callback. This is while initializing the storage. */ }; ESP_ERROR_CHECK(tinyusb_msc_storage_init_spiflash(&config_spi)); + ESP_ERROR_CHECK(tinyusb_msc_register_callback(TINYUSB_MSC_EVENT_MOUNT_CHANGED, storage_mount_changed_cb)); /* Other way to register the callback i.e. registering using separate API. If the callback had been already registered, it will be overwritten. */ #else // CONFIG_EXAMPLE_STORAGE_MEDIA_SPIFLASH static sdmmc_card_t *card = NULL; ESP_ERROR_CHECK(storage_init_sdmmc(&card)); const tinyusb_msc_sdmmc_config_t config_sdmmc = { - .card = card + .card = card, + .callback_mount_changed = storage_mount_changed_cb /* First way to register the callback. This is while initializing the storage. */ }; ESP_ERROR_CHECK(tinyusb_msc_storage_init_sdmmc(&config_sdmmc)); + ESP_ERROR_CHECK(tinyusb_msc_register_callback(TINYUSB_MSC_EVENT_MOUNT_CHANGED, storage_mount_changed_cb)); /* Other way to register the callback i.e. registering using separate API. If the callback had been already registered, it will be overwritten. */ #endif // CONFIG_EXAMPLE_STORAGE_MEDIA_SPIFLASH //mounted in the app by default