Skip to content

Commit

Permalink
Disable USB output on a faulty connection (e.g. closed serial port).
Browse files Browse the repository at this point in the history
Specifically, if a full transmit buffer does not clear in 10ms, the
firmware clears the buffer and disables its USB output until the USB
host re-establishes a connection. This fix may resolve crashing
issues aegean-odyssey#47 and aegean-odyssey#48, and possibly issue aegean-odyssey#5.
  • Loading branch information
aegean-odyssey committed Jan 3, 2021
1 parent 3a2a896 commit 92f2134
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
15 changes: 11 additions & 4 deletions marlin_changes/MarlinSerial_stm32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,10 @@ int8_t MS(fops_Control)(uint8_t cmd, uint8_t * pbuf, uint16_t lng)
// "dtr" modem signal (dte is present)
MS(dtr) = pbuf[0] & 1;
break;
case CDC_SET_LINE_CODING:
// HACK! being here implies a connection
MS(dtr) = 1;
break;
#endif
#if 0 // unnecessary
case CDC_SET_LINE_CODING:
Expand Down Expand Up @@ -629,11 +633,14 @@ void MarlinSerial::write(const uint8_t c)
ring_buffer_pos_t next = (MS(tx_tail)+1) & RXTX_BUFFER_MASK;
if (next == MS(tx_head)) {
HAL_Delay(10);
if (next == MS(tx_head)) {
MS(tx_head) = MS(tx_tail);
MS(fops_DeInit)(); // drop DTR
return;
}
}
if (next != MS(tx_head)) {
MS(tx_buffer)[MS(tx_tail)] = c;
MS(tx_tail) = next;
}
MS(tx_buffer)[MS(tx_tail)] = c;
MS(tx_tail) = next;
}
}
#if MULTIPLEX_MARLIN_SERIAL
Expand Down
23 changes: 14 additions & 9 deletions stm32f0xx/usbd_cdc-ao.c
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ static uint8_t USBD_CDC_DeInit (USB_t * pdev, uint8_t cfgidx)

static uint8_t USBD_CDC_Setup(USB_t * pdev, USBD_SetupReqTypedef * req)
{
static uint8_t ifalt = 0;
static uint16_t ifalt = 0;

CDC_t * cdc = (CDC_t *) pdev->pClassData;
ITF_t * itf = (ITF_t *) pdev->pUserData;
Expand All @@ -483,27 +483,31 @@ static uint8_t USBD_CDC_Setup(USB_t * pdev, USBD_SetupReqTypedef * req)
else {
itf->Control(req->bRequest, (uint8_t *) req, 0);
}
break;
return USBD_OK;

case USB_REQ_TYPE_STANDARD:
if (pdev->dev_state != USBD_STATE_CONFIGURED)
break;
switch (req->bRequest) {
case USB_REQ_GET_STATUS:
USBD_CtlSendData (pdev, (uint8_t *) &ifalt, 2);
return USBD_OK;
case USB_REQ_GET_INTERFACE:
USBD_CtlSendData (pdev, &ifalt, 1);
break;
USBD_CtlSendData (pdev, (uint8_t *) &ifalt, 1);
// fall through
case USB_REQ_SET_INTERFACE:
break;
return USBD_OK;
}
default:
break;
}
return USBD_OK;
return USBD_FAIL;
}

static uint8_t USBD_CDC_DataIn(USB_t * pdev, uint8_t epnum)
{
CDC_t * cdc = (CDC_t *) pdev->pClassData;

if (cdc) {
#if 0
#if 1 //?
PCD_t * hpcd = (PCD_t *) pdev->pData;
uint32_t m = hpcd->IN_ep[epnum].maxpacket;
uint32_t t = pdev->ep_in[epnum].total_length;
Expand Down Expand Up @@ -603,6 +607,7 @@ uint8_t USBD_CDC_TransmitPacket(USB_t * pdev)
if (cdc) {
if (! cdc->TxState) {
cdc->TxState = 1; // tx transfer in progress
pdev->ep_in[CDC_IN_EP & 0xf].total_length = cdc->TxLength;
USBD_LL_Transmit(pdev, CDC_IN_EP, cdc->TxBuffer, cdc->TxLength);
return USBD_OK;
}
Expand Down

0 comments on commit 92f2134

Please sign in to comment.