Skip to content

Commit d834200

Browse files
authored
add usb transmission backpressure (#536)
* heater-shaker: add a wait after send This looks gross and is gross but fixes an issue where if the host comms task has two messages to send in quick succession, you can end up running again swapping out the tx buffer before it actually hits the wire. * add usb tx backpressure We can actually get whether the usb is busy out of the usb cdc machinery. We can use that to handle delaying and retrying if the usb is busy, and waiting until the usb transaction completes to actually send data. * limit retries * format
1 parent 55f1614 commit d834200

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

stm32-modules/heater-shaker/firmware/host_comms_task/freertos_comms_task.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,21 @@ void run(void *param) { // NOLINT(misc-unused-parameters)
152152
reinterpret_cast<uint8_t *>(
153153
local_task->tx_buf.committed()->data()),
154154
tx_end - local_task->tx_buf.committed()->data());
155-
USBD_CDC_TransmitPacket(&_local_task.usb_handle);
155+
uint8_t tx_result =
156+
USBD_CDC_TransmitPacket(&_local_task.usb_handle);
157+
while (tx_result == USBD_BUSY) {
158+
vTaskDelay(1);
159+
tx_result = USBD_CDC_TransmitPacket(&_local_task.usb_handle);
160+
}
161+
if (tx_result != USBD_FAIL) {
162+
uint8_t retries = 0;
163+
while ((((USBD_CDC_HandleTypeDef *)
164+
_local_task.usb_handle.pClassData)
165+
->TxState == 1) &&
166+
retries++ < 10) {
167+
vTaskDelay(1);
168+
}
169+
}
156170
if (UartReady) {
157171
UartReady = false;
158172
HAL_UART_Transmit_IT(

stm32-modules/thermocycler-gen2/firmware/host_comms_task/freertos_comms_task.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ void run(void *param) { // NOLINT(misc-unused-parameters)
8484
reinterpret_cast<uint8_t *>(
8585
local_task->tx_buf.committed()->data()),
8686
tx_end - local_task->tx_buf.committed()->data());
87-
vTaskDelay(1);
8887
}
8988
}
9089
}

stm32-modules/thermocycler-gen2/firmware/host_comms_task/usb_hardware.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,5 +215,17 @@ void usb_hw_send(uint8_t *buf, uint16_t len) {
215215
USBD_CDC_SetTxBuffer(
216216
&_local_config.usb_handle,
217217
buf, len);
218-
USBD_CDC_TransmitPacket(&_local_config.usb_handle);
218+
uint8_t tx_result = USBD_CDC_TransmitPacket(&_local_config.usb_handle);
219+
while (tx_result == USBD_BUSY) {
220+
vTaskDelay(1);
221+
tx_result = USBD_CDC_TransmitPacket(&_local_config.usb_handle);
222+
}
223+
if (tx_result == USBD_FAIL) {
224+
return;
225+
}
226+
uint8_t retries = 0;
227+
228+
while ((((USBD_CDC_HandleTypeDef*)_local_config.usb_handle.pClassData)->TxState == 1) && retries++ < 10) {
229+
vTaskDelay(1);
230+
}
219231
}

0 commit comments

Comments
 (0)