title: STM32F4 UART Rx Interrupt Example Code
body: <p>I've been trying to implement a basic per-byte UART Rx Interrupt on a STM32F4 board using HAL skeleton code generated by STMCubeMX version 4.26.0</p>
  
  <p>Quite simply - I want to receive a character in UART1 via an Rx interrupt and transmit it on UART 6</p>
  
  <p>I have successfully implemented a polled version of what I want to achieve</p>
  
  <pre><code>    uint8_t in_usart1[10];
  
      HAL_StatusTypeDef usart1_status;
  
      usart1_status = HAL_UART_Receive(&amp;huart1, in_usart1, 1, 1);
  
      if (usart1_status != HAL_TIMEOUT)
      {
              HAL_UART_Transmit(&amp;huart6, in_usart1, 1, 100);
      }
  </code></pre>
  
  <p>I've enabled the UART 1 NVIC interrupt in STMCubeMX and stm32f4xx_it.c contains the IRQ handler which I've added my own user handler to:</p>
  
  <pre><code>void USART1_IRQHandler(void)
  {
      /* USER CODE BEGIN USART1_IRQn 0 */
  
      /* USER CODE END USART1_IRQn 0 */
      HAL_UART_IRQHandler(&amp;huart1);
      /* USER CODE BEGIN USART1_IRQn 1 */
      HAX_USART1_IRQHandler(&amp;huart1); /* My Handler */
      /* USER CODE END USART1_IRQn 1 */
  }
  </code></pre>
  
  <p>I've seen lot's of commentary about UART_Receive_IT() - but I suspect this is based on older versions of HAL due to UART_Receive_IT() being defined in stm32f4xx_hal_uart.c</p>
  
  <p>My suspicion is that I need to enable to interrupt / clear the interrupt flag as when I debug, USART1_IRQHandler() is NEVER called</p>
  
  <p>Does any one have any code that demonstrates what I am trying to achieve? My google-foo has failed me</p>
  
  <p>EDIT:
  I've gotten a little closer... In main.c I added (comments are existing code)</p>
  
  <pre><code>  /* USER CODE BEGIN PV */
    uint8_t rx_buffer;
    /* USER CODE END PV */
  
    ...
  
    /* USER CODE BEGIN 2 */
    HAL_UART_Receive_IT(&amp;huart1, (uint8_t *)rx_buffer, 10);
    /* USER CODE END 2 */
  </code></pre>
  
  <p>And then created:</p>
  
  <pre><code>  void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
    {
          if (huart-&gt;Instance == USART1)
          {
                HAL_UART_Transmit(&amp;huart6, &amp;rx_buffer, 1, 100);
          }
    }
  </code></pre>
  
  <p>Now the Rx interrupt gets fired - but it's a bit flakey on the USART6 Tx, and the Rx interrupt only gets fired once</p>