0

I am using a STM32L07x8 chip and the HAL driver from stcube. I cannot make a uart work in interrupt mode in any way. In blocking mode it works fine. I tried stuff like

__HAL_UART_ENABLE_IT(&UartHandle_RADIO, UART_IT_RXNE);
    HAL_NVIC_SetPriority(USART1_IRQn, 0, 2);
    HAL_NVIC_EnableIRQ(USART1_IRQn);

before or after i enable the UART. The USART1_IRQHandler() function or the HAL_UART_RxCpltCallback is never called. The RXNE bite in CR1 is enabled. I just want my program to wait untill it receives a bite in the UART. I tried calling

HAL_UART_Receive_IT(&UartHandle, (uint8_t *) rxBuffer, 1);

Before a main loop, during the main loop, still nothing. I do not understand at all how this HAL works. All the examples i find do nothing. I just need my program to wait for a few specific characters to come via UART, do a few steps after, then go back to waiting for those characters again.

    __HAL_RCC_USART1_CLK_ENABLE();
    UartHandle_RADIO.Instance = USART1;
    UartHandle_RADIO.Init.BaudRate = 115200; // 9600;
    UartHandle_RADIO.Init.WordLength = USART_WORDLENGTH_8B;
    UartHandle_RADIO.Init.StopBits = USART_STOPBITS_1;
    UartHandle_RADIO.Init.Parity = USART_PARITY_NONE;
    UartHandle_RADIO.Init.Mode = USART_MODE_TX_RX;

//  __HAL_UART_ENABLE_IT(&UartHandle_RADIO, UART_IT_RXNE);
    HAL_NVIC_SetPriority(USART1_IRQn, 0, 2);
    HAL_NVIC_EnableIRQ(USART1_IRQn);

    if (HAL_UART_DeInit(&UartHandle_RADIO) != HAL_OK) {
        Error_Handler();
    }

    if (HAL_UART_Init(&UartHandle_RADIO) != HAL_OK) {
        Error_Handler();
    }
Tarick Welling
  • 3,119
  • 3
  • 19
  • 44
andrei filip
  • 23
  • 1
  • 8
  • Could you please share your UART initialization code? – recep Feb 03 '21 at 08:33
  • I edited the post and added the UART initialization – andrei filip Feb 03 '21 at 08:37
  • UART receive interrupt with length 1, receives one byte data and stops. You should reconfigure it after receiving the byte. Increase length and place break point to your receive complete callback function and debug it. – recep Feb 03 '21 at 10:24
  • i tried with more than 1 length. The breakPoint in the callback function never triggers. The function is never called – andrei filip Feb 03 '21 at 10:34
  • Enable USART global interrupt from cube usart configurations, and regenerate the code. – recep Feb 03 '21 at 10:47

1 Answers1

0

My USART configuration for STM32F4:

static void MX_USART2_UART_Init(void)
{

  /* USER CODE BEGIN USART2_Init 0 */

  /* USER CODE END USART2_Init 0 */

  /* USER CODE BEGIN USART2_Init 1 */

  /* USER CODE END USART2_Init 1 */
  huart2.Instance = USART2;
  huart2.Init.BaudRate = 115200;
  huart2.Init.WordLength = UART_WORDLENGTH_8B;
  huart2.Init.StopBits = UART_STOPBITS_1;
  huart2.Init.Parity = UART_PARITY_NONE;
  huart2.Init.Mode = UART_MODE_TX_RX;
  huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  if (HAL_UART_Init(&huart2) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN USART2_Init 2 */

  /* USER CODE END USART2_Init 2 */

}
void HAL_UART_MspInit(UART_HandleTypeDef* huart)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};
  if(huart->Instance==USART2)
  {
  /* USER CODE BEGIN USART2_MspInit 0 */

  /* USER CODE END USART2_MspInit 0 */
    /* Peripheral clock enable */
    __HAL_RCC_USART2_CLK_ENABLE();

    __HAL_RCC_GPIOA_CLK_ENABLE();
    /**USART2 GPIO Configuration
    PA2     ------> USART2_TX
    PA3     ------> USART2_RX
    */
    GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_3;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    /* USART2 interrupt Init */
    HAL_NVIC_SetPriority(USART2_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(USART2_IRQn);
  /* USER CODE BEGIN USART2_MspInit 1 */

  /* USER CODE END USART2_MspInit 1 */
  }

}

In stm32f4xx_it.c :

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 */
}

/* USER CODE BEGIN 1 */
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
    HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
    rxCmplt = 1;
}

In main.c :

MX_USART2_UART_Init();
HAL_UART_Receive_IT(&huart2, rxData, 20);
  while (1)
  {
    if(rxCmplt != 0) {
        rxCmplt = 0;
        HAL_UART_Receive_IT(&huart2, rxData, 20);
    }
  }

I have tested it on STM32F4, it works properly.

recep
  • 124
  • 5