icis4

UART IT + FreeRTOS Queue

Apr 26th, 2019
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.50 KB | None | 0 0
  1. /**
  2.   * @brief This function handles USART1 global interrupt.
  3.   */
  4. void USART1_IRQHandler(void)
  5. {
  6.   /* USER CODE BEGIN USART1_IRQn 0 */
  7.   UART_IRQHandler(&huart1);
  8.   /* USER CODE END USART1_IRQn 0 */
  9.   HAL_UART_IRQHandler(&huart1);
  10.   /* USER CODE BEGIN USART1_IRQn 1 */
  11.  
  12.   /* USER CODE END USART1_IRQn 1 */
  13. }
  14.  
  15.  
  16. /*----------------------------------------------------------------------------
  17.   UART_IRQHandler
  18.   Handles UART global interrupt request.
  19.  *----------------------------------------------------------------------------*/
  20. void UART_IRQHandler(UART_HandleTypeDef* uartHandle)
  21. {
  22.     uint8_t data;
  23.     BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  24.  
  25.     if (__HAL_UART_GET_IT(uartHandle, UART_IT_RXNE)) { // read interrupt
  26.         __HAL_UART_CLEAR_IT(uartHandle, UART_IT_RXNE);  // clear interrupt
  27.  
  28.         data = uartHandle->Instance->RDR & 0x0FF;
  29.         xQueueSendToBackFromISR(uartInQueueHandle, &data, &xHigherPriorityTaskWoken);
  30.  
  31.         xTaskNotifyFromISR(uartTaskHandle, 1, eSetBits, NULL);
  32.     }
  33.  
  34.     if (__HAL_UART_GET_IT(uartHandle, UART_IT_TXE)) {
  35.         __HAL_UART_CLEAR_IT(uartHandle, UART_IT_TXE); // clear interrupt
  36.  
  37.       if (uxQueueMessagesWaitingFromISR(uartOutQueueHandle)) {
  38.           xQueueReceiveFromISR(uartOutQueueHandle, &data, &xHigherPriorityTaskWoken);
  39.           uartHandle->Instance->TDR = data;
  40.           tx_restart = 0;
  41.       }
  42.       else {
  43.         tx_restart = 1;
  44.         __HAL_UART_DISABLE_IT(uartHandle, UART_IT_TXE); // disable TX interrupt if nothing to send
  45.  
  46.       }
  47.     }
  48. }
Add Comment
Please, Sign In to add comment