Skip to content

Commit 3bb97fc

Browse files
feat(usb_host): MSC Host add suspend/resume events
- usb_host lib supports global suspend and resume - backward compatibility with older IDF releases - MSC Host target tests
1 parent 96786b0 commit 3bb97fc

File tree

4 files changed

+437
-25
lines changed

4 files changed

+437
-25
lines changed

host/class/msc/usb_host_msc/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## [Unreleased]
22

33
- Added public API support for formatting
4+
- Added global suspend/resume support
45

56
## 1.1.3
67

host/class/msc/usb_host_msc/include/usb/msc_host.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -9,6 +9,7 @@
99
#include <wchar.h>
1010
#include <stdint.h>
1111
#include "esp_err.h"
12+
#include "usb/usb_host.h"
1213
#include "freertos/FreeRTOS.h"
1314

1415
#ifdef __cplusplus
@@ -23,6 +24,11 @@ extern "C" {
2324

2425
#define MSC_STR_DESC_SIZE 32
2526

27+
// For backward compatibility with IDF versions which do not have suspend/resume api
28+
#ifdef USB_HOST_LIB_EVENT_FLAGS_AUTO_SUSPEND
29+
#define MSC_HOST_SUSPEND_RESUME_API_SUPPORTED
30+
#endif
31+
2632
typedef struct msc_host_device *msc_host_device_handle_t; /**< Handle to a Mass Storage Device */
2733

2834
/**
@@ -32,10 +38,14 @@ typedef struct {
3238
enum {
3339
MSC_DEVICE_CONNECTED, /**< MSC device has been connected to the system.*/
3440
MSC_DEVICE_DISCONNECTED, /**< MSC device has been disconnected from the system.*/
41+
#ifdef MSC_HOST_SUSPEND_RESUME_API_SUPPORTED
42+
MSC_DEVICE_SUSPENDED, /**< MSC device has been suspended.*/
43+
MSC_DEVICE_RESUMED, /**< MSC device has been resumed.*/
44+
#endif // MSC_HOST_SUSPEND_RESUME_API_SUPPORTED
3545
} event;
3646
union {
37-
uint8_t address; /**< Address of connected MSC device.*/
38-
msc_host_device_handle_t handle; /**< MSC device handle to disconnected device.*/
47+
uint8_t address; /**< Address of connected MSC device.*/
48+
msc_host_device_handle_t handle; /**< MSC device handle to disconnected, suspended or resumed device.*/
3949
} device;
4050
} msc_host_event_t;
4151

host/class/msc/usb_host_msc/src/msc_host.c

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -375,23 +375,57 @@ static msc_device_t *find_msc_device(usb_device_handle_t device_handle)
375375

376376
static void client_event_cb(const usb_host_client_event_msg_t *event, void *arg)
377377
{
378-
if (event->event == USB_HOST_CLIENT_EVENT_NEW_DEV) {
378+
switch (event->event) {
379+
case USB_HOST_CLIENT_EVENT_NEW_DEV:
380+
ESP_LOGD(TAG, "New device connected");
379381
if (is_mass_storage_device(event->new_dev.address)) {
380382
const msc_host_event_t msc_event = {
381383
.event = MSC_DEVICE_CONNECTED,
382384
.device.address = event->new_dev.address,
383385
};
384386
s_msc_driver->user_cb(&msc_event, s_msc_driver->user_arg);
385387
}
386-
} else if (event->event == USB_HOST_CLIENT_EVENT_DEV_GONE) {
387-
msc_device_t *msc_device = find_msc_device(event->dev_gone.dev_hdl);
388-
if (msc_device) {
388+
break;
389+
case USB_HOST_CLIENT_EVENT_DEV_GONE:
390+
ESP_LOGD(TAG, "Device suddenly disconnected");
391+
msc_device_t *msc_device_gone = find_msc_device(event->dev_gone.dev_hdl);
392+
if (msc_device_gone) {
389393
const msc_host_event_t msc_event = {
390394
.event = MSC_DEVICE_DISCONNECTED,
391-
.device.handle = msc_device,
395+
.device.handle = msc_device_gone,
396+
};
397+
s_msc_driver->user_cb(&msc_event, s_msc_driver->user_arg);
398+
}
399+
break;
400+
401+
#ifdef MSC_HOST_SUSPEND_RESUME_API_SUPPORTED
402+
case USB_HOST_CLIENT_EVENT_DEV_SUSPENDED:
403+
ESP_LOGD(TAG, "Device suspended");
404+
msc_device_t *msc_device_susp = find_msc_device(event->dev_gone.dev_hdl);
405+
if (msc_device_susp) {
406+
const msc_host_event_t msc_event = {
407+
.event = MSC_DEVICE_SUSPENDED,
408+
.device.handle = msc_device_susp,
409+
};
410+
s_msc_driver->user_cb(&msc_event, s_msc_driver->user_arg);
411+
}
412+
break;
413+
case USB_HOST_CLIENT_EVENT_DEV_RESUMED:
414+
ESP_LOGD(TAG, "Device resumed");
415+
msc_device_t *msc_device_res = find_msc_device(event->dev_gone.dev_hdl);
416+
if (msc_device_res) {
417+
const msc_host_event_t msc_event = {
418+
.event = MSC_DEVICE_RESUMED,
419+
.device.handle = msc_device_res,
392420
};
393421
s_msc_driver->user_cb(&msc_event, s_msc_driver->user_arg);
394422
}
423+
break;
424+
#endif // MSC_HOST_SUSPEND_RESUME_API_SUPPORTED
425+
426+
default:
427+
ESP_LOGW(TAG, "Unrecognized USB Host client event");
428+
break;
395429
}
396430
}
397431

0 commit comments

Comments
 (0)