STM32cubeMX USART operation in interrupt mode

Discussion in 'Embedded Systems and Microcontrollers' started by zazas321, Aug 16, 2019 at 5:04 AM.

  1. zazas321

    Thread Starter Member

    Nov 29, 2015
    430
    6
    Hey. I require to read multiple bytes using USART ( 3 bytes of MIDI data ). I have generated my project using STM32cubeMX and using USART generated functions. I do not need to transmit anything, I only require to receive 3 bytes and then process the bytes ( processing will come later) at this point, I just want to make sure that I properly receive 3 bytes . I have few questions regarding how USART works and hopefully someone with a little bit more knowledge can help me out.

    1. What is the difference between void USART1_IRQHandler(void) and void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart1),?

    I am using function HAL_UART_Receive_IT to receive 3 bytes and I am calling it like that :

    uint8_t aRxBuffer[3];
    HAL_UART_Receive_IT(&huart1, (uint8_t *)aRxBuffer, sizeof(aRxBuffer))

    I have created an RxBuffer with a size of 3 to store 3 bytes of data. I assume that when there is some data to be received ( MIDI instrument key pressed), the RXcallback or irqhandler is called where I manually set the flag to start receiving bytes.

    Code (C):
    1.  
    2. void USART1_IRQHandler(void)
    3. {
    4.   /* USER CODE BEGIN USART1_IRQn 0 */
    5.   got_byte = 1;
    6.  
    7.  
    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.  


    The code in my main is :

    Code (C):
    1.  
    2.  
    3.  
    4. if(HAL_UART_Receive_IT(&huart1, (uint8_t *)aRxBuffer, sizeof(aRxBuffer)) != HAL_OK)
    5.             Error_Handler();
    6.   while (1)
    7.   {
    8.       if(got_byte == 1)// RECEIVE ONE BYTE AND STORE IT INTO byte[] BUFFER
    9.       {      
    10.         byte[i++]=aRxBuffer[i];
    11.         got_byte = 0;
    12.       }
    13.   }
    14.  
    15.  
    So as you can see, Im checking whether the flag has been set and then I fill the buffer that I have crated byte[] with the data from RxBuffer. However I am concerned whether I am understanding the USART correctly?

    Do i need to set the flag USART1_IRQHandler(void) or void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart1)?
     
    Last edited: Aug 16, 2019 at 5:23 AM
Loading...