AnsweredAssumed Answered

CubeMX HAL_USART_Receive_IT returning always Noise Error

Question asked by Trackk on Feb 28, 2015
I have checked the demo in:
STM32Cube_FW_F4_V1.3.0\Projects\STM32F4-Discovery\Examples\UART\UART_TwoBoards_ComIT

I setup same configuration following STM32CubeMX and I can Tx but I can't Rx. Instead of calling HAL_UART_RxCpltCallback() on reception it calls HAL_UART_ErrorCallback(). A quick look to the UartHandle->ErrorCode reveals I am getting HAL_UART_ERROR_NE.

However if I go step-by-step on the HAL_UART_IRQHandler() I can see that the Demo code enters this condition:
tmp1 = __HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE);
tmp2 = __HAL_UART_GET_IT_SOURCE(huart, UART_IT_RXNE);
/* UART in mode Receiver ---------------------------------------------------*/
if((tmp1 != RESET) && (tmp2 != RESET))
{
  UART_Receive_IT(huart);
}

But when executing my code it enters this one instead:
tmp1 = __HAL_UART_GET_FLAG(huart, UART_FLAG_TXE);
  tmp2 = __HAL_UART_GET_IT_SOURCE(huart, UART_IT_TXE);
  /* UART in mode Transmitter ------------------------------------------------*/
  if((tmp1 != RESET) && (tmp2 != RESET))
  {
    UART_Transmit_IT(huart);
  }

I even tried changing the configuration mode from UART_MODE_TX_RX to only UART_MODE_RX, but the result is the same.

I am not sending anything and I am only calling the function (which always lights up the blue LED):
if(HAL_UART_Receive_IT(&huart2, (uint8_t *)aRxBuffer, 2)!= HAL_OK){
    HAL_GPIO_TogglePin(GPIOD, LED_ORANGE);
  } else {
    HAL_GPIO_TogglePin(GPIOD, LED_BLUE);
  }

But as soon as a character is received through the port I get the error. The configuration is as follows:
/** System Clock Configuration
*/
void SystemClock_Config(void)
{
 
  RCC_OscInitTypeDef RCC_OscInitStruct;
 
  __PWR_CLK_ENABLE();
 
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);
 
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = 6;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  HAL_RCC_OscConfig(&RCC_OscInitStruct);
 
}
 
/* USART2 init function */
void MX_USART2_UART_Init(void)
{
 
  huart2.Instance = USART2;
  huart2.Init.BaudRate = 9600;
  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;
  HAL_UART_Init(&huart2);
 
}
 
void MX_GPIO_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStruct;
 
  /* GPIO Ports Clock Enable */
  __GPIOA_CLK_ENABLE();
  __GPIOD_CLK_ENABLE();
 
  /*Configure GPIO pins : PD12 PD13 PD14 PD15 */
  GPIO_InitStruct.Pin = GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
  HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
}


Main program:
int main(void)
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();
 
  /* Configure the system clock */
  SystemClock_Config();
 
  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USART2_UART_Init();
   
    if(HAL_UART_Receive_IT(&huart2, (uint8_t *)aRxBuffer, 2)!= HAL_OK){
    HAL_GPIO_TogglePin(GPIOD, LED_ORANGE);
    } else {
    HAL_GPIO_TogglePin(GPIOD, LED_BLUE); //BLUE
    }
  while (1)
  {
  }
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle)
{
  HAL_GPIO_TogglePin(GPIOD, LED_GREEN); //Got it
}
 
void HAL_UART_ErrorCallback(UART_HandleTypeDef *UartHandle)
{
  /* Turn LED3 on: Transfer error in reception/transmission process */
  HAL_GPIO_TogglePin(GPIOD, LED_RED); //Error
}

The reception works fine when using blocking mode HAL_UART_Receive(), but not when using HAL_UART_Receive_IT().

Outcomes