Skip to content

Commit

Permalink
Merge pull request #1186 from spark/feature/fix-usb-serial-deadlock-m…
Browse files Browse the repository at this point in the history
…asked-interrupts

USB Serial might deadlock when interrupts are masked
  • Loading branch information
avtolstoy authored Nov 29, 2016
2 parents d98c1d1 + 7d10df8 commit 47e45de
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
5 changes: 5 additions & 0 deletions hal/src/stm32f2xx/usb_hal.c
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,11 @@ int32_t HAL_USB_USART_Receive_Data(HAL_USB_USART_Serial serial, uint8_t peek)

static bool HAL_USB_WillPreempt()
{
// Ain't no one is preempting us if interrupts are currently disabled
if ((__get_PRIMASK() & 1)) {
return false;
}

if (HAL_IsISR()) {
#ifdef USE_USB_OTG_FS
int32_t irq = OTG_FS_IRQn;
Expand Down
20 changes: 16 additions & 4 deletions user/tests/wiring/usbserial/usbserial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ void consume(Stream& serial)
}
}

test(0_USBSERIAL_RingBufferHelperIsSane) {
test(USBSERIAL_00_RingBufferHelperIsSane) {
uint32_t size = 129;
uint32_t head = 0;
uint32_t tail = 0;
Expand Down Expand Up @@ -108,7 +108,19 @@ test(0_USBSERIAL_RingBufferHelperIsSane) {
assertEqual(0, ring_space_wrapped(size, head, tail));
}

test(1_USBSERIAL_ReadWrite) {
test(USBSERIAL_01_SerialDoesNotDeadlockWhenInterruptsAreMasked) {
int32_t state = HAL_disable_irq();
// Write 2048 + \r\n bytes into Serial TX buffer
for (int i = 0; i < 2048; i++) {
char tmp;
randomString(&tmp, 1);
Serial.write(tmp);
}
HAL_enable_irq(state);
Serial.println();
}

test(USBSERIAL_02_ReadWrite) {
//The following code will test all the important USB Serial routines
char test[] = "hello";
char message[10];
Expand All @@ -122,7 +134,7 @@ test(1_USBSERIAL_ReadWrite) {
assertTrue(strncmp(test, message, 5)==0);
}

test(2_USBSERIAL_isConnectedWorksCorrectly) {
test(USBSERIAL_03_isConnectedWorksCorrectly) {
Serial.println("Please close the USB Serial port and open it again within 60 seconds");

uint32_t mil = millis();
Expand Down Expand Up @@ -150,7 +162,7 @@ test(2_USBSERIAL_isConnectedWorksCorrectly) {
assertTrue(strncmp(test, message, 5)==0);
}

test(3_USBSERIAL_RxBufferFillsCompletely) {
test(USBSERIAL_04_RxBufferFillsCompletely) {
Serial.println("We will now test USB Serial RX buffer");
Serial.println("Please close the USB Serial port and open it again once the device reattaches");

Expand Down

0 comments on commit 47e45de

Please sign in to comment.