From c1a3d428f640f72ec2e10c267b16ae0c1345600e Mon Sep 17 00:00:00 2001 From: RockyZeroFour Date: Thu, 29 Feb 2024 22:15:58 +0100 Subject: [PATCH] New unplugged callback & event filtering - A USB event is only added when it's new and different from the last unhandled one - New event to inform when the device is unplugged on USB (like when the PHY is reset) --- src/device/usbd.c | 5 +++++ src/device/usbd.h | 3 +++ 2 files changed, 8 insertions(+) diff --git a/src/device/usbd.c b/src/device/usbd.c index 8363499a6a..75763741c0 100644 --- a/src/device/usbd.c +++ b/src/device/usbd.c @@ -531,6 +531,11 @@ void tud_task_ext(uint32_t timeout_ms, bool in_isr) else if (DCD_EVENT_UNPLUGGED == event.event_id) { _usbd_dev.speed = DCD_EVENT_INVALID; + + if (tud_unplugged_cb) + { + tud_unplugged_cb(); + } } break; diff --git a/src/device/usbd.h b/src/device/usbd.h index 86fa207f03..74ba473165 100644 --- a/src/device/usbd.h +++ b/src/device/usbd.h @@ -142,6 +142,9 @@ TU_ATTR_WEAK void tud_mount_cb(void); // Invoked when device is unmounted TU_ATTR_WEAK void tud_umount_cb(void); +// Invoked when device is unplugged +TU_ATTR_WEAK void tud_unplugged_cb(void); + // Invoked when usb bus is suspended // Within 7ms, device must draw an average of current less than 2.5 mA from bus TU_ATTR_WEAK void tud_suspend_cb(bool remote_wakeup_en);