Skip to content

Commit

Permalink
[Gen3] hal: fix the issue that device runs into hardfault if logging …
Browse files Browse the repository at this point in the history
…from ISR is enabled while using Serial1 as the log interface.
  • Loading branch information
XuGuohui committed Apr 29, 2022
1 parent 00f8056 commit 46bf742
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions hal/src/nRF52840/usb_hal.cpp
Expand Up @@ -22,6 +22,8 @@
#include "usb_hal_cdc.h"
#include "usb_settings.h"
#include <mutex>
#include <nrf52840.h>
#include <nrf_nvic.h>

#ifdef USB_CDC_ENABLE

Expand All @@ -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 ||
Expand All @@ -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) {
Expand Down

0 comments on commit 46bf742

Please sign in to comment.