Skip to content

Commit 1b02ac6

Browse files
committed
pbio/drv/usb/usb_ev3.c: Implement status keepalive timer
Without this timer, if the hub state doesn't change and the user program doesn't output anything, the hub won't be able to detect that the host software has closed (because it never sends anything which could time out). Keeping this state reasonably up-to-date is important for the hub auto-power-off functionality.
1 parent 1f0f52c commit 1b02ac6

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

lib/pbio/drv/usb/usb_ev3.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -884,6 +884,7 @@ static pbio_os_process_t pbdrv_usb_ev3_process;
884884
static pbio_error_t pbdrv_usb_ev3_process_thread(pbio_os_state_t *state, void *context) {
885885
static pbio_os_timer_t delay_timer;
886886
static pbio_os_timer_t tx_timeout_timer;
887+
static pbio_os_timer_t keepalive_timer;
887888
static bool was_transmitting = false;
888889
static bool is_transmitting = false;
889890
static uint32_t prev_status_flags = ~0;
@@ -911,6 +912,7 @@ static pbio_error_t pbdrv_usb_ev3_process_thread(pbio_os_state_t *state, void *c
911912
if (usb_rx_sz && !usb_tx_response_is_not_ready) {
912913
switch (ep1_rx_buf[0]) {
913914
case PBIO_PYBRICKS_OUT_EP_MSG_SUBSCRIBE:
915+
pbio_os_timer_set(&keepalive_timer, 1000);
914916
pbdrv_usb_is_events_subscribed = ep1_rx_buf[1];
915917
ep1_tx_response_buf[0] = PBIO_PYBRICKS_IN_EP_MSG_RESPONSE;
916918
pbio_set_uint32_le(&ep1_tx_response_buf[1], PBIO_PYBRICKS_ERROR_OK);
@@ -937,14 +939,17 @@ static pbio_error_t pbdrv_usb_ev3_process_thread(pbio_os_state_t *state, void *c
937939

938940
// Send status flags if they've changed (and we can)
939941
new_status_flags = pbsys_status_get_flags();
940-
if (pbdrv_usb_is_events_subscribed && !usb_tx_status_is_not_ready && new_status_flags != prev_status_flags) {
942+
if (pbdrv_usb_is_events_subscribed && !usb_tx_status_is_not_ready &&
943+
(new_status_flags != prev_status_flags || pbio_os_timer_is_expired(&keepalive_timer))) {
941944
ep1_tx_status_buf[0] = PBIO_PYBRICKS_IN_EP_MSG_EVENT;
942945
uint32_t usb_status_sz = 1 + pbsys_status_get_status_report(&ep1_tx_status_buf[1]);
943946

944947
usb_tx_status_is_not_ready = true;
945948
usb_setup_tx_dma_desc(CPPI_DESC_TX_STATUS, ep1_tx_status_buf, usb_status_sz);
946949

947950
prev_status_flags = new_status_flags;
951+
952+
pbio_os_timer_set(&keepalive_timer, 1000);
948953
}
949954

950955
// Handle timeouts

0 commit comments

Comments
 (0)