Skip to content

Commit

Permalink
STM32: (Original/Pico/WiFi) if USB connected but PC not receiving dat…
Browse files Browse the repository at this point in the history
…a, throw away USB data rather than blocking (fix #2446)
  • Loading branch information
gfwilliams committed Feb 1, 2024
1 parent 3814058 commit d217500
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 67 deletions.
1 change: 1 addition & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
Fix lock leak when using flat/flash/native strings as object indices
Fix g.wrapString lockup if wrap width is less than the character width
Fix potential for crash after ReferenceError during function declaration (fix #2457)
STM32: (Original/Pico/WiFi) if USB connected but PC not receiving data, throw away USB data rather than blocking (fix #2446)

2v20 : Ensure String.charCodeAt returns NaN for out of bounds chars
Bangle.js2: When rendering overlays, *do not* use the current FG/BG color for 1 bit overlays
Expand Down
2 changes: 2 additions & 0 deletions src/jshardware.h
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,8 @@ void jshSetupRTCPrescalerValue(unsigned int prescale);
int jshGetRTCPrescalerValue(bool calibrate);
// Reset timers and average systick duration counters for RTC - when coming out of sleep or changing prescaler
void jshResetRTCTimer();
/// Flags that we've been able to send data down USB, so it's ok to have data in the output buffer
void jshClearUSBIdleTimeout();
#endif

#if defined(NRF51_SERIES) || defined(NRF52_SERIES)
Expand Down
65 changes: 34 additions & 31 deletions targetlibs/stm32legacyusb/usb_endp.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
*
* http://www.st.com/software_license_agreement_liberty_v2
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
Expand Down Expand Up @@ -54,36 +54,38 @@ uint8_t USB_Tx_State = 0;
* Return : none.
*******************************************************************************/
void Handle_USBAsynchXfer (void)
{
{
if(USB_Tx_State != 1)
{
unsigned char USB_TX_Buffer[VIRTUAL_COM_PORT_DATA_SIZE];
int USB_Tx_length = 0;

// try and fill the buffer
int c;
while (USB_Tx_length<VIRTUAL_COM_PORT_DATA_SIZE &&
while (USB_Tx_length<VIRTUAL_COM_PORT_DATA_SIZE &&
((c = jshGetCharToTransmit(EV_USBSERIAL)) >=0) ) { // get byte to transmit
USB_TX_Buffer[USB_Tx_length++] = c;
}

jshClearUSBIdleTimeout(); // flag that USB is ready to receive data, so we can keep it in our buffer (otherwise chuck it)

// if nothing, set state to 0
if (USB_Tx_length==0) {
USB_Tx_State = 0;
USB_Tx_State = 0;
return;
}

USB_Tx_State = 1;
USB_Tx_State = 1;

#ifdef USE_STM3210C_EVAL
USB_SIL_Write(EP1_IN, &USB_TX_Buffer[0], USB_Tx_length);
USB_SIL_Write(EP1_IN, &USB_TX_Buffer[0], USB_Tx_length);
#else
UserToPMABufferCopy(&USB_TX_Buffer[0], ENDP1_TXADDR, USB_Tx_length);
SetEPTxCount(ENDP1, USB_Tx_length);
SetEPTxValid(ENDP1);
SetEPTxValid(ENDP1);
#endif /* USE_STM3210C_EVAL */
}
}

}

/*******************************************************************************
Expand All @@ -95,30 +97,31 @@ void Handle_USBAsynchXfer (void)
*******************************************************************************/
void EP1_IN_Callback (void)
{
if (USB_Tx_State == 1)
{
if (USB_Tx_State == 1) {
unsigned char USB_TX_Buffer[VIRTUAL_COM_PORT_DATA_SIZE];
int USB_Tx_length = 0;

// try and fill the buffer
int c;
while (USB_Tx_length<VIRTUAL_COM_PORT_DATA_SIZE &&
while (USB_Tx_length<VIRTUAL_COM_PORT_DATA_SIZE &&
((c = jshGetCharToTransmit(EV_USBSERIAL)) >= 0) ) { // get byte to transmit
USB_TX_Buffer[USB_Tx_length++] = c;
}


jshClearUSBIdleTimeout(); // flag that USB is ready to receive data, so we can keep it in our buffer (otherwise chuck it)

// if nothing, set state to 0
if (USB_Tx_length==0) {
USB_Tx_State = 0;
USB_Tx_State = 0;
return;
}

// else send data and keep going
UserToPMABufferCopy(&USB_TX_Buffer[0], ENDP1_TXADDR, USB_Tx_length);
SetEPTxCount(ENDP1, USB_Tx_length);
SetEPTxValid(ENDP1);
}

// else send data and keep going
UserToPMABufferCopy(&USB_TX_Buffer[0], ENDP1_TXADDR, USB_Tx_length);
SetEPTxCount(ENDP1, USB_Tx_length);
SetEPTxValid(ENDP1);
}
}

/*******************************************************************************
* Function Name : EP3_OUT_Callback
Expand All @@ -131,15 +134,15 @@ void EP3_OUT_Callback(void)
{
uint8_t USB_Rx_Buffer[VIRTUAL_COM_PORT_DATA_SIZE];
int USB_Rx_Cnt;

/* Get the received data buffer and update the counter */
USB_Rx_Cnt = USB_SIL_Read(EP3_OUT, USB_Rx_Buffer);
/* USB data will be immediately processed, this allow next USB traffic being

/* USB data will be immediately processed, this allow next USB traffic being
NAKed till the end of the USART Xfer */

jshPushIOCharEvents(EV_USBSERIAL, USB_Rx_Buffer, USB_Rx_Cnt);

/* Enable the receive of data on EP3 */
SetEPRxStatus(ENDP3, jshHasEventSpaceForChars(VIRTUAL_COM_PORT_DATA_SIZE) ? EP_RX_VALID : EP_RX_NAK);
}
Expand All @@ -155,21 +158,21 @@ void EP3_OUT_Callback(void)
void SOF_Callback(void)
{
static uint32_t FrameCount = 0;

if(bDeviceState == CONFIGURED)
{
SetEPRxStatus(ENDP3, jshHasEventSpaceForChars(VIRTUAL_COM_PORT_DATA_SIZE) ? EP_RX_VALID : EP_RX_NAK);


if (FrameCount++ == VCOMPORT_IN_FRAME_INTERVAL)
{
/* Reset the frame counter */
FrameCount = 0;

/* Check the data to be sent through IN pipe */
Handle_USBAsynchXfer();
}
}
}
}
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

Loading

0 comments on commit d217500

Please sign in to comment.