Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

USB Serial might deadlock when interrupts are masked #1186

Merged
merged 2 commits into from
Nov 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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