Skip to content

Commit 8b08fa0

Browse files
feat(usb_host): Remote wake debug WIP
1 parent 721c79c commit 8b08fa0

File tree

5 files changed

+131
-22
lines changed

5 files changed

+131
-22
lines changed

host/usb/src/hcd_dwc.c

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -832,7 +832,7 @@ static hcd_port_event_t _intr_hdlr_hprt(port_t *port, usb_dwc_hal_port_event_t h
832832
break;
833833
}
834834
case USB_DWC_HAL_PORT_EVENT_REMOTE_WAKEUP: {
835-
ESP_EARLY_LOGI(HCD_DWC_TAG, "WAKE");
835+
//ESP_EARLY_LOGI(HCD_DWC_TAG, "WAKE");
836836
//port->state = HCD_PORT_STATE_ENABLED;
837837
port_event = HCD_PORT_EVENT_REMOTE_WAKEUP;
838838
break;
@@ -1334,19 +1334,21 @@ static esp_err_t _port_cmd_bus_suspend(port_t *port)
13341334
// Port must have been previously enabled, and all pipes must already be halted
13351335
if (!(port->state == HCD_PORT_STATE_ENABLED && _port_check_all_pipes_halted(port))) {
13361336
ret = ESP_ERR_INVALID_STATE;
1337+
esp_rom_printf("ERR1\n");
13371338
goto exit;
13381339
}
13391340

13401341
usb_dwc_hal_port_suspend(port->hal);
13411342
port->state = HCD_PORT_STATE_SUSPENDING;
13421343

13431344
HCD_EXIT_CRITICAL();
1344-
vTaskDelay(pdMS_TO_TICKS(SUSPEND_ENTRY_MS));
1345+
vTaskDelay(pdMS_TO_TICKS(100));
13451346
HCD_ENTER_CRITICAL();
13461347

13471348
if (port->state != HCD_PORT_STATE_SUSPENDING) {
13481349
// Port state unexpectedly changed
13491350
ret = ESP_ERR_INVALID_RESPONSE;
1351+
esp_rom_printf("ERR2\n");
13501352
goto exit;
13511353
}
13521354

@@ -1358,6 +1360,7 @@ static esp_err_t _port_cmd_bus_suspend(port_t *port)
13581360
// Sanity check, the root port should have entered the suspended state after the SUSPEND_ENTRY_MS delay
13591361
if (!usb_dwc_ll_hprt_get_port_suspend(port->hal->dev)) {
13601362
ret = ESP_ERR_NOT_FINISHED;
1363+
esp_rom_printf("ERR3\n");
13611364
goto exit;
13621365
}
13631366

@@ -1370,23 +1373,36 @@ static esp_err_t _port_cmd_bus_suspend(port_t *port)
13701373

13711374
static esp_err_t _port_cmd_bus_resume(port_t *port)
13721375
{
1376+
uint32_t hprt, gintsts, state;
13731377
esp_err_t ret;
13741378
// Port can only be resumed if it was previously suspended
13751379
if (port->state != HCD_PORT_STATE_SUSPENDED) {
13761380
ret = ESP_ERR_INVALID_STATE;
13771381
goto exit;
13781382
}
1379-
HCD_EXIT_CRITICAL();
1380-
//vTaskDelay(pdMS_TO_TICKS(RESUME_HOLD_MS));
1381-
vTaskDelay(pdMS_TO_TICKS(500));
1382-
HCD_ENTER_CRITICAL();
1383+
hprt = usb_dwc_hal_port_get_hprt_val(port->hal);
1384+
gintsts = usb_dwc_hal_port_get_gintsts_val(port->hal);
1385+
state = usb_dwc_hal_hprt_get_pwr_line_status(port->hal);
1386+
1387+
// Res1 (Wkp1)
1388+
esp_rom_printf("HPRT: %lx\n", hprt);
1389+
esp_rom_printf("GINTSTS: %lx\n", gintsts);
1390+
esp_rom_printf("State %lx\n", state);
13831391

13841392
// Put and hold the bus in the K state.
13851393
usb_dwc_hal_port_toggle_resume(port->hal, true);
13861394
port->state = HCD_PORT_STATE_RESUMING;
13871395
HCD_EXIT_CRITICAL();
1388-
//vTaskDelay(pdMS_TO_TICKS(RESUME_HOLD_MS));
1389-
vTaskDelay(pdMS_TO_TICKS(500));
1396+
vTaskDelay(pdMS_TO_TICKS(50));
1397+
1398+
// Res2 (Wkp2)
1399+
hprt = usb_dwc_hal_port_get_hprt_val(port->hal);
1400+
gintsts = usb_dwc_hal_port_get_gintsts_val(port->hal);
1401+
state = usb_dwc_hal_hprt_get_pwr_line_status(port->hal);
1402+
esp_rom_printf("HPRT: %lx\n", hprt);
1403+
esp_rom_printf("GINTSTS: %lx\n", gintsts);
1404+
esp_rom_printf("State %lx\n", state);
1405+
13901406
HCD_ENTER_CRITICAL();
13911407
// Return and hold the bus to the J state (as port of the LS EOP)
13921408
usb_dwc_hal_port_toggle_resume(port->hal, false);
@@ -1396,8 +1412,15 @@ static esp_err_t _port_cmd_bus_resume(port_t *port)
13961412
goto exit;
13971413
}
13981414
HCD_EXIT_CRITICAL();
1399-
//vTaskDelay(pdMS_TO_TICKS(RESUME_RECOVERY_MS));
1400-
vTaskDelay(pdMS_TO_TICKS(500));
1415+
vTaskDelay(pdMS_TO_TICKS(100));
1416+
1417+
// Res3 (Wkp3)
1418+
hprt = usb_dwc_hal_port_get_hprt_val(port->hal);
1419+
gintsts = usb_dwc_hal_port_get_gintsts_val(port->hal);
1420+
state = usb_dwc_hal_hprt_get_pwr_line_status(port->hal);
1421+
esp_rom_printf("HPRT: %lx\n", hprt);
1422+
esp_rom_printf("GINTSTS: %lx\n", gintsts);
1423+
esp_rom_printf("State %lx\n", state);
14011424
HCD_ENTER_CRITICAL();
14021425
if (port->state != HCD_PORT_STATE_RESUMING || !port->flags.conn_dev_ena) {
14031426
// Port state unexpectedly changed
@@ -1406,14 +1429,7 @@ static esp_err_t _port_cmd_bus_resume(port_t *port)
14061429
}
14071430
port->state = HCD_PORT_STATE_ENABLED;
14081431

1409-
if (usb_dwc_hal_port_check_if_suspended(port->hal)) {
1410-
printf("Again suspended\n");
1411-
}
1412-
14131432
ret = ESP_OK;
1414-
// esp_rom_printf("resume: HPRT = %lx\n", usb_dwc_hal_port_get_hprt_val(port->hal));
1415-
// esp_rom_printf("power on: GINTSTS = %lx\n", usb_dwc_hal_port_get_gintsts_val(port->hal));
1416-
14171433
exit:
14181434
return ret;
14191435
}

host/usb/src/hub.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ static void root_port_req(hcd_port_handle_t root_port_hdl)
491491
}
492492
}
493493
if (port_reqs & PORT_REQ_SUSPEND) {
494-
ESP_LOGI(HUB_DRIVER_TAG, "Suspending the root port");
494+
ESP_LOGD(HUB_DRIVER_TAG, "Suspending the root port");
495495

496496
HUB_DRIVER_ENTER_CRITICAL();
497497
const root_port_state_t root_state = p_hub_driver_obj->dynamic.root_port_state;
@@ -514,7 +514,7 @@ static void root_port_req(hcd_port_handle_t root_port_hdl)
514514
usbh_devs_set_pm_actions_all(USBH_DEV_SUSPEND_EVT);
515515
}
516516
if (port_reqs & PORT_REQ_RESUME) {
517-
ESP_LOGI(HUB_DRIVER_TAG, "Resuming the root port");
517+
ESP_LOGD(HUB_DRIVER_TAG, "Resuming the root port");
518518

519519
HUB_DRIVER_ENTER_CRITICAL();
520520
const root_port_state_t root_state = p_hub_driver_obj->dynamic.root_port_state;

host/usb/test/target_test/common/hcd_common.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ void test_hcd_expect_port_event(hcd_port_handle_t port_hdl, hcd_port_event_t exp
127127
// Check the contents of that event message
128128
TEST_ASSERT_EQUAL(port_hdl, msg.port_hdl);
129129
TEST_ASSERT_EQUAL_MESSAGE(expected_event, msg.port_event, "Unexpected event");
130-
printf("\t-> Port event\n");
130+
//printf("\t-> Port event\n");
131131
}
132132

133133
void test_hcd_expect_pipe_event(hcd_pipe_handle_t pipe_hdl, hcd_pipe_event_t expected_event)

host/usb/test/target_test/hcd/main/test_hcd_port.c

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ TEST_CASE("Test HCD port suspend and resume", "[port][low_speed][full_speed][hig
173173
TEST_ASSERT_EQUAL(ESP_OK, hcd_port_command(port_hdl, HCD_PORT_CMD_SUSPEND));
174174
TEST_ASSERT_EQUAL(HCD_PORT_STATE_SUSPENDED, hcd_port_get_state(port_hdl));
175175
printf("Suspended\n");
176-
vTaskDelay(pdMS_TO_TICKS(100)); // Give some time for bus to remain suspended
176+
vTaskDelay(pdMS_TO_TICKS(5000)); // Give some time for bus to remain suspended
177177

178178
// Resume the port
179179
TEST_ASSERT_EQUAL(ESP_OK, hcd_port_command(port_hdl, HCD_PORT_CMD_RESUME));
@@ -392,3 +392,96 @@ TEST_CASE("Test HCD port command bailout", "[port][low_speed][full_speed][high_s
392392
vTaskDelete(task_handle);
393393
vSemaphoreDelete(sync_sem);
394394
}
395+
396+
bool test_hcd_check_remote_wake(hcd_pipe_handle_t *pipe, urb_t *get_status_urb)
397+
{
398+
// Initialize with a "Get status request" USB_SETUP_PACKET_INIT_GET_STATUS
399+
get_status_urb->transfer.num_bytes = sizeof(usb_setup_packet_t) + sizeof(usb_device_status_t);
400+
USB_SETUP_PACKET_INIT_GET_STATUS((usb_setup_packet_t *)get_status_urb->transfer.data_buffer);
401+
get_status_urb->transfer.context = URB_CONTEXT_VAL;
402+
403+
TEST_ASSERT_EQUAL(ESP_OK, hcd_urb_enqueue(pipe, get_status_urb));
404+
test_hcd_expect_pipe_event(pipe, HCD_PIPE_EVENT_URB_DONE);
405+
urb_t *urb = hcd_urb_dequeue(pipe);
406+
TEST_ASSERT_EQUAL(get_status_urb, urb);
407+
TEST_ASSERT_EQUAL_MESSAGE(USB_TRANSFER_STATUS_COMPLETED, urb->transfer.status, "Transfer NOT completed");
408+
TEST_ASSERT_EQUAL(URB_CONTEXT_VAL, urb->transfer.context);
409+
410+
TEST_ASSERT_EQUAL(sizeof(usb_setup_packet_t) + sizeof(usb_device_status_t), urb->transfer.actual_num_bytes);
411+
TEST_ASSERT_EQUAL(urb->transfer.num_bytes, urb->transfer.actual_num_bytes);
412+
usb_device_status_t *device_status = (usb_device_status_t *)(urb->transfer.data_buffer + sizeof(usb_setup_packet_t));
413+
414+
const bool enabled = device_status->remote_wakeup;
415+
printf("Remote wakeup is currently %s\n", ((enabled)) ? ("enabled") : ("disabled") );
416+
return enabled;
417+
}
418+
419+
void test_hcd_set_remote_wake(hcd_pipe_handle_t *pipe, urb_t *set_feature_urb)
420+
{
421+
set_feature_urb->transfer.num_bytes = sizeof(usb_setup_packet_t);
422+
USB_SETUP_PACKET_INIT_SET_FEATURE((usb_setup_packet_t *)set_feature_urb->transfer.data_buffer, DEVICE_REMOTE_WAKEUP);
423+
set_feature_urb->transfer.context = URB_CONTEXT_VAL;
424+
425+
426+
TEST_ASSERT_EQUAL(ESP_OK, hcd_urb_enqueue(pipe, set_feature_urb));
427+
test_hcd_expect_pipe_event(pipe, HCD_PIPE_EVENT_URB_DONE);
428+
urb_t *urb = hcd_urb_dequeue(pipe);
429+
TEST_ASSERT_EQUAL(set_feature_urb, urb);
430+
TEST_ASSERT_EQUAL_MESSAGE(USB_TRANSFER_STATUS_COMPLETED, urb->transfer.status, "Transfer NOT completed");
431+
TEST_ASSERT_EQUAL(URB_CONTEXT_VAL, urb->transfer.context);
432+
433+
TEST_ASSERT_EQUAL(sizeof(usb_setup_packet_t), urb->transfer.actual_num_bytes);
434+
TEST_ASSERT_EQUAL(urb->transfer.num_bytes, urb->transfer.actual_num_bytes);
435+
}
436+
#define TEST_HID_DEV_SPEED USB_SPEED_LOW
437+
TEST_CASE("Test HCD port remote wakeup", "[port][low_speed][full_speed][high_speed]")
438+
{
439+
usb_speed_t port_speed = test_hcd_wait_for_conn(port_hdl); // Trigger a connection
440+
TEST_ASSERT_EQUAL_MESSAGE(TEST_HID_DEV_SPEED, port_speed, "Connected device is not Low Speed!");
441+
vTaskDelay(pdMS_TO_TICKS(100)); // Short delay send of SOF (for FS) or EOPs (for LS)
442+
443+
hcd_pipe_handle_t default_pipe = test_hcd_pipe_alloc(port_hdl, NULL, 0, port_speed); // Create a default pipe (using a NULL EP descriptor)
444+
uint8_t dev_addr = test_hcd_enum_device(default_pipe);
445+
446+
urb_t *get_status_urb = test_hcd_alloc_urb(0, URB_DATA_BUFF_SIZE);
447+
urb_t *set_feature_urb = test_hcd_alloc_urb(0, URB_DATA_BUFF_SIZE);
448+
449+
test_hcd_check_remote_wake(default_pipe, get_status_urb);
450+
test_hcd_set_remote_wake(default_pipe, set_feature_urb);
451+
test_hcd_check_remote_wake(default_pipe, get_status_urb);
452+
453+
// Halt the default pipe before suspending
454+
TEST_ASSERT_EQUAL(HCD_PIPE_STATE_ACTIVE, hcd_pipe_get_state(default_pipe));
455+
TEST_ASSERT_EQUAL(ESP_OK, hcd_pipe_command(default_pipe, HCD_PIPE_CMD_HALT));
456+
TEST_ASSERT_EQUAL(HCD_PIPE_STATE_HALTED, hcd_pipe_get_state(default_pipe));
457+
458+
// Suspend the port
459+
TEST_ASSERT_EQUAL(ESP_OK, hcd_port_command(port_hdl, HCD_PORT_CMD_SUSPEND));
460+
TEST_ASSERT_EQUAL(HCD_PORT_STATE_SUSPENDED, hcd_port_get_state(port_hdl));
461+
printf("Suspended\n");
462+
test_hcd_expect_port_event(port_hdl, HCD_PORT_EVENT_REMOTE_WAKEUP);
463+
TEST_ASSERT_EQUAL(HCD_PORT_EVENT_REMOTE_WAKEUP, hcd_port_handle_event(port_hdl));
464+
//printf("Waken\n");
465+
466+
// Resume the port
467+
TEST_ASSERT_EQUAL(ESP_OK, hcd_port_command(port_hdl, HCD_PORT_CMD_RESUME));
468+
TEST_ASSERT_EQUAL(HCD_PORT_STATE_ENABLED, hcd_port_get_state(port_hdl));
469+
printf("Resumed\n");
470+
471+
vTaskDelay(pdMS_TO_TICKS(5000));
472+
473+
TEST_ASSERT_EQUAL(HCD_PIPE_STATE_HALTED, hcd_pipe_get_state(default_pipe));
474+
TEST_ASSERT_EQUAL(ESP_OK, hcd_pipe_command(default_pipe, HCD_PIPE_CMD_CLEAR));
475+
TEST_ASSERT_EQUAL(HCD_PIPE_STATE_ACTIVE, hcd_pipe_get_state(default_pipe));
476+
477+
478+
479+
printf("Issue check\n");
480+
test_hcd_check_remote_wake(default_pipe, get_status_urb);
481+
482+
test_hcd_pipe_free(default_pipe);
483+
test_hcd_free_urb(get_status_urb);
484+
test_hcd_free_urb(set_feature_urb);
485+
// Cleanup
486+
test_hcd_wait_for_disconn(port_hdl, false);
487+
}

host/usb/test/target_test/usb_host/main/test_usb_host_async.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,7 @@ TEST_CASE("Test USB Host remote wakeup setup", "[usb_host][low_speed][full_speed
700700
TEST_ASSERT_EQUAL_MESSAGE(
701701
pdTRUE, xSemaphoreTake(dev_ready_smp, TEST_REMOTE_WAKE_SMP_WAIT_MS), "Remote wake not checked on time");
702702

703-
printf("Remote wake enabled\n");
703+
// printf("Remote wake enabled\n");
704704
usb_host_lib_root_port_suspend();
705705
vTaskDelay(pdMS_TO_TICKS(5000));
706706
// usb_host_lib_root_port_resume();

0 commit comments

Comments
 (0)