Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invoke power state changes on DCD_EVENT_BUS_RESET #2171

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 42 additions & 15 deletions src/device/usbd.c
Original file line number Diff line number Diff line change
Expand Up @@ -486,16 +486,40 @@ void tud_task_ext(uint32_t timeout_ms, bool in_isr)
{
case DCD_EVENT_BUS_RESET:
TU_LOG_USBD(": %s Speed\r\n", tu_str_speed[event.bus_reset.speed]);
usbd_reset(event.rhport);
_usbd_dev.speed = event.bus_reset.speed;
break;
TU_ATTR_FALLTHROUGH;

case DCD_EVENT_UNPLUGGED:
TU_LOG_USBD("\r\n");
// Only inform application about the unmount if it was mounted before
if ( _usbd_dev.cfg_num )
{
_usbd_dev.cfg_num = 0;

// invoke callback
if (tud_umount_cb) tud_umount_cb();
}

// Inform application about a no longer valid suspend state
if ( _usbd_dev.suspended )
{
_usbd_dev.suspended = 0;

// invoke callback
if (tud_resume_cb) tud_resume_cb();
}

// Completely clear the current USB state
usbd_reset(event.rhport);

// invoke callback
if (tud_umount_cb) tud_umount_cb();
// Recover the intended bus speed
if (DCD_EVENT_BUS_RESET == event.event_id)
{
_usbd_dev.speed = event.bus_reset.speed;
}
else if (DCD_EVENT_UNPLUGGED == event.event_id)
{
_usbd_dev.speed = DCD_EVENT_INVALID;
}
break;

case DCD_EVENT_SETUP_RECEIVED:
Expand Down Expand Up @@ -1069,17 +1093,20 @@ static bool process_get_descriptor(uint8_t rhport, tusb_control_request_t const
//--------------------------------------------------------------------+
// DCD Event Handler
//--------------------------------------------------------------------+
TU_ATTR_FAST_FUNC void dcd_event_handler(dcd_event_t const* event, bool in_isr) {
bool send = false;
switch (event->event_id) {
case DCD_EVENT_UNPLUGGED:
_usbd_dev.connected = 0;
_usbd_dev.addressed = 0;
_usbd_dev.cfg_num = 0;
_usbd_dev.suspended = 0;
send = true;
break;
TU_ATTR_FAST_FUNC void dcd_event_handler(dcd_event_t const * event, bool in_isr)
{
volatile static uint8_t last_event_id = DCD_EVENT_INVALID;

// Skip the useless repeating bus states
if (last_event_id == event->event_id && DCD_EVENT_UNPLUGGED <= event->event_id && DCD_EVENT_RESUME >= event->event_id)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will also ignore DCD_EVENT_SOF

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Despite that this can't be merged easily anymore it's also outdated and should contain a bug.

Maybe next week I can provide the newer version which should work on the current branch.

{
return;
}

last_event_id = event->event_id;

switch (event->event_id)
{
case DCD_EVENT_SUSPEND:
// NOTE: When plugging/unplugging device, the D+/D- state are unstable and
// can accidentally meet the SUSPEND condition ( Bus Idle for 3ms ).
Expand Down
2 changes: 2 additions & 0 deletions src/portable/synopsys/dwc2/dcd_dwc2.c
Original file line number Diff line number Diff line change
Expand Up @@ -1078,6 +1078,8 @@ void dcd_int_handler(uint8_t rhport) {
// USBRST is start of reset.
dwc2->gintsts = GINTSTS_USBRST;
bus_reset(rhport);

dcd_event_bus_reset(rhport, TUSB_SPEED_INVALID, true);
}

if (int_status & GINTSTS_ENUMDNE) {
Expand Down
Loading