Skip to content

Commit 00b547b

Browse files
committedMay 13, 2025
bricks/ev3/mphalport: Don't block on USB writes when disconnected.
1 parent ae2757f commit 00b547b

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed
 

‎lib/pbio/drv/usb/usb_ev3.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ extern const tUSBBuffer g_sRxBuffer;
165165

166166
// Global flag indicating that a USB configuration has been set.
167167
static volatile bool g_bUSBConfigured = false;
168+
static bool g_bUSBSerialConfigured = false;
168169

169170
static void GetLineCoding(tLineCoding *psLineCoding) {
170171
//
@@ -328,6 +329,7 @@ unsigned int ControlHandler(void *pvCBData, unsigned int ulEvent, unsigned int u
328329
//
329330
case USB_EVENT_DISCONNECTED:
330331
g_bUSBConfigured = false;
332+
g_bUSBSerialConfigured = false;
331333
process_poll(&pbdrv_usb_process);
332334
break;
333335

@@ -348,6 +350,8 @@ unsigned int ControlHandler(void *pvCBData, unsigned int ulEvent, unsigned int u
348350
// Set the current serial communication parameters.
349351
//
350352
case USBD_CDC_EVENT_SET_CONTROL_LINE_STATE:
353+
g_bUSBSerialConfigured = true;
354+
process_poll(&pbdrv_usb_process);
351355
//
352356
// TODO: If configured with GPIOs controlling the handshake lines,
353357
// set them appropriately depending upon the flags passed in the wValue
@@ -502,7 +506,16 @@ pbdrv_usb_bcd_t pbdrv_usb_get_bcd(void) {
502506
}
503507

504508
uint32_t pbdrv_usb_write(const uint8_t *data, uint32_t size) {
505-
return USBBufferWrite((tUSBBuffer *)&g_sTxBuffer, data, size);
509+
// Attempt to write to the USB buffer.
510+
uint32_t written = USBBufferWrite((tUSBBuffer *)&g_sTxBuffer, data, size);
511+
512+
// If configured, return the number of bytes written so we can await completion.
513+
if (g_bUSBSerialConfigured) {
514+
return written;
515+
}
516+
517+
// If not configured, return the size requested so that caller doesn't block.
518+
return size;
506519
}
507520

508521
uint32_t pbdrv_usb_rx_data_available(void) {

0 commit comments

Comments
 (0)