Electronic – start a new uart1 interrupt function inside a previous uart1 interrupt complete callback function

stm32f7

I am using STM32F46ZG NUCLEO discovery board. My code is like:

int main(){
  HAL_UART_Transmit_IT(&huart1, buff, 5);

  while(1)
  {
    do something;
  }
}

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart){
  HAL_UART_Transmit_IT(&huart1, buff, 5);
}

My code has no RTOS, and I want to continuously transmit the 5 bytes. Once the previous interrupt transmit finishes, I want to transmit 5 bytes right away. So can I just call HAL_UART_Transmit_IT() in its own call back function?

Best Answer

Sure, just make sure to avoid race conditions and blocking in anything called from an ISR. For example, if HAL_UART_Transmit_IT blocks for buffer space to become available, you must not call it from an ISR as this will almost certainly cause a hang. Also, HAL_UART_Transmit_IT must be reentrant as your interrupt could fire while HAL_UART_Transmit_IT is executing in your main loop. Another option would be to set a flag and handle the transmission somewhere else.