AnsweredAssumed Answered

need very simple UART RXNE and IDLE interrupt example

Question asked by panduru.krishna on Sep 8, 2015
Latest reply on Sep 10, 2015 by julien.terrier
Hello,

I've used the STD peripheral libraries before and successfully generating interrupts from UART


//snippet 1

void USART1_IRQHandler(void){
    char chee;
    if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET){
        chee = USART_ReceiveData(USART1);
        BTrxString[BTrxPointer++] = chee;                         //used for something else
        USART_ClearITPendingBit(USART1, USART_IT_RXNE);
    }
    if(USART_GetITStatus(USART1, USART_IT_IDLE) != RESET){
        strncpy(EXT_BTrxString, BTrxString, 200);                    //used for something else
        USART_ClearITPendingBit(USART1, USART_IT_IDLE);
        BTrxPointer = 0;                                   //used for something else
        memset(&BTrxString[0], 0, sizeof(BTrxString));               //used for something else
        chee = USART_ReceiveData(USART1);
        FLAG_BT_TIMEOUT = 1;                              //used for something else
    }
}

I'm having a hard time understanding how to use HAL and the cubeMX.. I am able to send strings on UART

HAL_UART_Transmit_IT(&huart2, ("lol\n"), strlen("lol\n"));

no problems at all. the problem is I need to get RXNE and IDLE interrupts.

interrupts weren't firing from the cubeMX code. while examining the code generated from cubeMX I had to add the following line in the void HAL_UART_MspInit(UART_HandleTypeDef* huart) function. interrupt fires.
 Is that really required? I'am guessing that it should be done by default by cubeMX software.

__HAL_UART_ENABLE_IT(&huart2, UART_IT_RXNE);

I've played around cubeMX EXTI interrupt. I used the HAL_GPIO_EXTI_Callback function to react to button presses. works great no problems.

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin){
      HAL_UART_Transmit_IT(&huart2, ("lol\n"), strlen("lol\n"));
}

Now the question, can I get a simple example as seen in code snippet 1 to generate interrupts. I've tried something like the snippet 3 code. and fcode would'nt jump into HAL_UART_RxCpltCallback woldn't do anything Im definitely sure thats wrong.
but when I try to send a UART char from teraterm, the interrupt fires and jumps into code snippet 2.

// snippet 2

void USART2_IRQHandler(void)
{
  /* USER CODE BEGIN USART2_IRQn 0 */
 
  /* USER CODE END USART2_IRQn 0 */
  HAL_UART_IRQHandler(&huart2);
  /* USER CODE BEGIN USART2_IRQn 1 */
 
  /* USER CODE END USART2_IRQn 1 */
}
 the code jumps into the HAL_UART_IRQHandler as well. and into HAL_UART_IRQHandler and then UART_Receive_IT and returns HAL_BUSY.

// snippet 3

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart){
    char kk = 'p';
 
    if(__HAL_UART_GET_IT(&huart2, UART_IT_RXNE) != RESET){
        HAL_UART_Receive(&huart2, &kk, 1, 10);
        HAL_UART_Transmit_IT(&huart2, &kk, 1);
        HAL_UART_Transmit_IT(&huart2, (" \n"), strlen(" \n"));
    }
    if(__HAL_UART_GET_IT(&huart2, UART_IT_IDLE) != RESET){
        HAL_UART_Transmit_IT(&huart2, ("rxi\n"), strlen("rxi\n"));
        __HAL_UART_CLEAR_IT(&huart2, UART_CLEAR_IDLEF);
    }
}

Could someone show me a simple HAL UART interrupt example.

       
  • cooCox IDE 1.7.8
  •    
  • cubeMX 4.10.0
  •    
  • GNU Tools ARM Embedded 2015q2
  •    
  • STM32L053R8T6 on NUCLEO-L053R8 board

Outcomes