Skip to content

Commit 189e367

Browse files
committed
main: add callbacks for discrete USB states
We can save more power by parking threads when suspended, and threads should be deleted when disconnected. Also fix an inefficiency in usb_thread wakeups when the device is yet to be configured, but is addressed - slowing down control transfers. Signed-off-by: Jonathan Bell <jonathan@raspberrypi.com>
1 parent d9c507f commit 189e367

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

src/main.c

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,11 @@ void usb_thread(void *ptr)
7575
else
7676
gpio_put(PROBE_USB_CONNECTED_LED, 0);
7777
#endif
78+
// If suspended or disconnected, delay for 1ms (20 ticks)
79+
if (tud_suspended() || !tud_connected())
80+
xTaskDelayUntil(&wake, 20);
7881
// Go to sleep for up to a tick if nothing to do
79-
if (!tud_task_event_ready())
82+
else if (!tud_task_event_ready())
8083
xTaskDelayUntil(&wake, 1);
8184
} while (1);
8285
}
@@ -189,6 +192,40 @@ bool tud_vendor_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_requ
189192
}
190193
#endif
191194

195+
void tud_suspend_cb(bool remote_wakeup_en)
196+
{
197+
probe_info("Suspended\n");
198+
/* Join DAP and UART threads? Or just suspend them, for transparency */
199+
vTaskSuspend(uart_taskhandle);
200+
vTaskSuspend(dap_taskhandle);
201+
/* slow down clk_sys for power saving ? */
202+
}
203+
204+
void tud_resume_cb(void)
205+
{
206+
probe_info("Resumed\n");
207+
vTaskResume(uart_taskhandle);
208+
vTaskResume(dap_taskhandle);
209+
}
210+
211+
void tud_unmount_cb(void)
212+
{
213+
probe_info("Disconnected\n");
214+
vTaskSuspend(uart_taskhandle);
215+
vTaskSuspend(dap_taskhandle);
216+
vTaskDelete(uart_taskhandle);
217+
vTaskDelete(dap_taskhandle);
218+
}
219+
220+
void tud_mount_cb(void)
221+
{
222+
probe_info("Connected, Configured\n");
223+
/* UART needs to preempt USB as if we don't, characters get lost */
224+
xTaskCreate(cdc_thread, "UART", configMINIMAL_STACK_SIZE, NULL, UART_TASK_PRIO, &uart_taskhandle);
225+
/* Lowest priority thread is debug - need to shuffle buffers before we can toggle swd... */
226+
xTaskCreate(dap_thread, "DAP", configMINIMAL_STACK_SIZE, NULL, DAP_TASK_PRIO, &dap_taskhandle);
227+
}
228+
192229
void vApplicationTickHook (void)
193230
{
194231
};

0 commit comments

Comments
 (0)