From 21a270462e6c76e52ab4158fb503d250e5951719 Mon Sep 17 00:00:00 2001 From: Andrey Tolstoy Date: Tue, 29 Nov 2016 23:12:33 +0700 Subject: [PATCH 1/2] HAL_USB_WillPreempt should return false when interrupts are masked --- hal/src/stm32f2xx/usb_hal.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/hal/src/stm32f2xx/usb_hal.c b/hal/src/stm32f2xx/usb_hal.c index e2a353858d..7354c8a43e 100644 --- a/hal/src/stm32f2xx/usb_hal.c +++ b/hal/src/stm32f2xx/usb_hal.c @@ -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; From 7d10df8c5b2cc49d97b4dfc084d73f6c541808af Mon Sep 17 00:00:00 2001 From: Andrey Tolstoy Date: Tue, 29 Nov 2016 23:30:01 +0700 Subject: [PATCH 2/2] Test USBSERIAL_01_SerialDoesNotDeadlockWhenInterruptsAreMasked added1 --- user/tests/wiring/usbserial/usbserial.cpp | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/user/tests/wiring/usbserial/usbserial.cpp b/user/tests/wiring/usbserial/usbserial.cpp index a3eff08d65..0f45fd259a 100644 --- a/user/tests/wiring/usbserial/usbserial.cpp +++ b/user/tests/wiring/usbserial/usbserial.cpp @@ -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; @@ -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]; @@ -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(); @@ -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");