Skip to content

Disable USART IRQ in uart_write and uart_debug_write #496

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

Closed
wants to merge 3 commits into from
Closed
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
42 changes: 28 additions & 14 deletions cores/arduino/stm32/uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -607,12 +607,16 @@ size_t uart_debug_write(uint8_t *data, uint32_t size)
index = serial_debug.index;
}

HAL_NVIC_DisableIRQ(serial_debug.irq);

while (HAL_UART_Transmit(uart_handlers[index], data, size, TX_TIMEOUT) != HAL_OK) {
if ((HAL_GetTick() - tickstart) >= TX_TIMEOUT) {
HAL_NVIC_EnableIRQ(serial_debug.irq);
return 0;
}
}

HAL_NVIC_EnableIRQ(serial_debug.irq);
return size;
}

Expand Down Expand Up @@ -682,12 +686,14 @@ void uart_attach_rx_callback(serial_t *obj, void (*callback)(serial_t *))
rx_callback[obj->index] = callback;
rx_callback_obj[obj->index] = obj;

/* Must disable interrupt to prevent handle lock contention */
HAL_NVIC_DisableIRQ(obj->irq);

HAL_UART_Receive_IT(uart_handlers[obj->index], &(obj->recv), 1);

/* Enable interrupt */
HAL_NVIC_SetPriority(obj->irq, 0, 1);
HAL_NVIC_EnableIRQ(obj->irq);

if (HAL_UART_Receive_IT(uart_handlers[obj->index], &(obj->recv), 1) != HAL_OK) {
return;
}
}

/**
Expand All @@ -706,14 +712,15 @@ void uart_attach_tx_callback(serial_t *obj, int (*callback)(serial_t *))
tx_callback[obj->index] = callback;
tx_callback_obj[obj->index] = obj;

/* Must disable interrupt to prevent handle lock contention */
HAL_NVIC_DisableIRQ(obj->irq);

/* The following function will enable UART_IT_TXE and error interrupts */
HAL_UART_Transmit_IT(uart_handlers[obj->index], &obj->tx_buff[obj->tx_tail], 1);

/* Enable interrupt */
HAL_NVIC_SetPriority(obj->irq, 0, 2);
HAL_NVIC_EnableIRQ(obj->irq);

/* The following function will enable UART_IT_TXE and error interrupts */
if (HAL_UART_Transmit_IT(uart_handlers[obj->index], &obj->tx_buff[obj->tx_tail], 1) != HAL_OK) {
return;
}
}

/**
Expand Down Expand Up @@ -790,16 +797,23 @@ void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
}
#else
if (__HAL_UART_GET_FLAG(huart, UART_FLAG_PE) != RESET) {
tmpval = huart->Instance->RDR; /* Clear PE flag */
__HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_PEF); /* Clear PE flag */
} else if (__HAL_UART_GET_FLAG(huart, UART_FLAG_FE) != RESET) {
tmpval = huart->Instance->RDR; /* Clear FE flag */
__HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_FEF); /* Clear FE flag */
} else if (__HAL_UART_GET_FLAG(huart, UART_FLAG_NE) != RESET) {
tmpval = huart->Instance->RDR; /* Clear NE flag */
__HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_NEF); /* Clear NE flag */
} else if (__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE) != RESET) {
tmpval = huart->Instance->RDR; /* Clear ORE flag */
__HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF); /* Clear ORE flag */
}
#endif

/* Restart receive interrupt after any error */
uint8_t index = uart_index(huart);
if (index < UART_NUM) {
serial_t *obj = rx_callback_obj[index];
if (!serial_rx_active(obj)) {
HAL_UART_Receive_IT(uart_handlers[obj->index], &(obj->recv), 1);
}
}
UNUSED(tmpval);
}

Expand Down