From 1620def30779a8b397e23d793365f23706bc6a5d Mon Sep 17 00:00:00 2001 From: XuGuohui Date: Fri, 29 Apr 2022 14:44:37 +0800 Subject: [PATCH] [Gen3] hal: fix the issue that device runs into hardfault if logging from ISR is enabled while using Serial1 as the log interface. --- hal/src/nRF52840/usb_hal.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/hal/src/nRF52840/usb_hal.cpp b/hal/src/nRF52840/usb_hal.cpp index 38b89a0188..ddc3f225f5 100644 --- a/hal/src/nRF52840/usb_hal.cpp +++ b/hal/src/nRF52840/usb_hal.cpp @@ -22,6 +22,8 @@ #include "usb_hal_cdc.h" #include "usb_settings.h" #include +#include +#include #ifdef USB_CDC_ENABLE @@ -38,6 +40,18 @@ void HAL_USB_Detach() { } void HAL_USB_USART_Init(HAL_USB_USART_Serial serial, const HAL_USB_USART_Config* config) { + /* + * Note: This function will call into SD API, which requires the SVC interrupt to be enabled, + * otherwise, device runs into hardfault. Without the workaround, When LOG_FROM_ISR is defined, + * device will run into hardfault due to the following code in spark_wiring_logging.cpp: + * + * if (stream_ == &Serial && Network.listening()) { + * return; // Do not mix logging and serial console output + * } + */ + int st = __get_BASEPRI(); + __set_BASEPRI(5 << (8 - __NVIC_PRIO_BITS)); // The SVC interrupt priority is 4 + if ((config == NULL) || (config && (config->rx_buffer == NULL || config->rx_buffer_size == 0 || @@ -57,6 +71,8 @@ void HAL_USB_USART_Init(HAL_USB_USART_Serial serial, const HAL_USB_USART_Config* } else { usb_uart_init(config->rx_buffer, config->rx_buffer_size, config->tx_buffer, config->tx_buffer_size); } + + __set_BASEPRI(st); } void HAL_USB_USART_Begin(HAL_USB_USART_Serial serial, uint32_t baud, void *reserved) {