cancel
Showing results for 
Search instead for 
Did you mean: 

HAL_UART_Receive_IT() only receives the first time and printf() is sometimes not working.

some_guy
Visitor

Hello everybody.

This is my first post in this forum and I am quite new to the stm32 MCUs. So sorry in advance if I dont know or misunderstand some things.

 

Right now I am working on a setup to read values from a sensor (dps310) and print them in a console on a linux pc. I am using two Nucleo boards for this because the goal is to have a radio communication between the two boards and to remotely read the values from the sensor. Right now the boards are directly connected to each other with uart because I am doing this step by step. So the setup looks like this:

Sensor --(SPI)--> Nucleo-F429ZI --(UART)--> Nucleo-F439ZI --(UART/USB)--> PC

The Nucleo-F429ZI is working fine and does exactly what it should which is to read the sensor and transmit the data about once every second to the other board.

The Nucleo-F439ZI however is giving me some headaches. The idea is pretty simple. Receive the data with HAL_UART_Receive_IT() and use printf() to transmit the data to the pc in a readable format. But HAL_UART_Receive_IT() only works for the first time after initialization and receives the data correctly. After that no interrupt occurs again. I noticed the ORE flag gets set but i dont know why or how to solve this. I read posts with similar issues but none of their solutions worked for me (or I did it wrong).

The printf() functions inside of the if(flag) statement dont work either even tho flag gets set to 0 which is very strange to me. I can read the output of the other printf() functions in my console just fine.

I am describing these two problems in one post because I dont know if they might be related to each other. Dont mind some of the commented code. I tested quite a bit of stuff already.

Any help is much appricheated. I am using the STM32CubeIDE 1.19.0. See my code below

#include "main.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
UART_HandleTypeDef huart2;
UART_HandleTypeDef huart3;

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART3_UART_Init(void);
static void MX_USART2_UART_Init(void);
/* USER CODE BEGIN PFP */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
float rx_Buffer[2];
uint8_t flag = 0;
uint8_t err = 0;
/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{

  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USART3_UART_Init();
  MX_USART2_UART_Init();
  /* USER CODE BEGIN 2 */
  HAL_UART_Receive_IT(&huart2, (uint8_t*)&rx_Buffer, sizeof(rx_Buffer));
  printf("\n\n\rprintf funktioniert\n\n\r");
  printf("Empfänger\n\n\r");
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
	  //__HAL_UART_CLEAR_OREFLAG(&huart2);
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
	  printf("Test1");
	  if(flag)
	  {
		  printf("Test2");
		  printf("Temperatur: %.2fC Luftdruck: %.2fhPa\r", rx_Buffer[0], rx_Buffer[1]);
		  //HAL_UART_Receive_IT(&huart2, (uint8_t*)&rx_Buffer, sizeof(rx_Buffer));
		  //rx_Buffer[0] = 0;
		  //rx_Buffer[1] = 0;
		  flag = 0;
		  printf("Test3");
	  }
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Configure the main internal regulator output voltage
  */
  __HAL_RCC_PWR_CLK_ENABLE();
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  {
    Error_Handler();
  }
}

/**
  * @brief USART2 Initialization Function
  * @PAram None
  * @retval None
  */
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_9B;
  huart2.Init.StopBits = UART_STOPBITS_1;
  huart2.Init.Parity = UART_PARITY_EVEN;
  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 */

}

/**
  * @brief USART3 Initialization Function
  * @PAram None
  * @retval None
  */
static void MX_USART3_UART_Init(void)
{

  /* USER CODE BEGIN USART3_Init 0 */

  /* USER CODE END USART3_Init 0 */

  /* USER CODE BEGIN USART3_Init 1 */

  /* USER CODE END USART3_Init 1 */
  huart3.Instance = USART3;
  huart3.Init.BaudRate = 115200;
  huart3.Init.WordLength = UART_WORDLENGTH_9B;
  huart3.Init.StopBits = UART_STOPBITS_1;
  huart3.Init.Parity = UART_PARITY_EVEN;
  huart3.Init.Mode = UART_MODE_TX_RX;
  huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart3.Init.OverSampling = UART_OVERSAMPLING_16;
  if (HAL_UART_Init(&huart3) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN USART3_Init 2 */

  /* USER CODE END USART3_Init 2 */

}

/**
  * @brief GPIO Initialization Function
  * @PAram None
  * @retval None
  */
static void MX_GPIO_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};
  /* USER CODE BEGIN MX_GPIO_Init_1 */

  /* USER CODE END MX_GPIO_Init_1 */

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOB_CLK_ENABLE();
  __HAL_RCC_GPIOD_CLK_ENABLE();

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(LD1_Green_GPIO_Port, LD1_Green_Pin, GPIO_PIN_RESET);

  /*Configure GPIO pin : LD1_Green_Pin */
  GPIO_InitStruct.Pin = LD1_Green_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(LD1_Green_GPIO_Port, &GPIO_InitStruct);

  /* USER CODE BEGIN MX_GPIO_Init_2 */

  /* USER CODE END MX_GPIO_Init_2 */
}

/* USER CODE BEGIN 4 */
PUTCHAR_PROTOTYPE
{
  /* Place your implementation of fputc here */
  /* e.g. write a character to the USART1 and Loop until the end of transmission */
  HAL_UART_Transmit(&huart3, (uint8_t *)&ch, 1, 10);

  return ch;
}

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
	if(huart->Instance == USART2)
	{
		flag = 1;
		//USART2->SR &= !(uint32_t)USART_SR_ORE;
		//USART2->DR = 0x0;
		//USART2->CR3 = USART_CR3_EIE;
		if(HAL_UART_Receive_IT(&huart2, (uint8_t*)&rx_Buffer, sizeof(rx_Buffer)) != HAL_OK)
			err = 1;
	}

}
/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @PAram  file: pointer to the source file name
  * @PAram  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

 

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Super User

Perhaps put HAL_Delay(1000) in the main loop so you are not printing characters continuously.

Code seems okay apart from that.

I bet ORE is getting set when you're debugging the code and characters come in without it being ready to receive. Solution for that is to not pause the code while characters come in.

> The printf() functions inside of the if(flag) statement dont work

printf statements within if statements work the same as outside. Recheck your assumptions here. Terminals can buffer until you print a newline. I'd recommend printing "Test1\n" instead of leaving off the newline.

If you feel a post has answered your question, please click "Accept as Solution".

View solution in original post

2 REPLIES 2
TDK
Super User

Perhaps put HAL_Delay(1000) in the main loop so you are not printing characters continuously.

Code seems okay apart from that.

I bet ORE is getting set when you're debugging the code and characters come in without it being ready to receive. Solution for that is to not pause the code while characters come in.

> The printf() functions inside of the if(flag) statement dont work

printf statements within if statements work the same as outside. Recheck your assumptions here. Terminals can buffer until you print a newline. I'd recommend printing "Test1\n" instead of leaving off the newline.

If you feel a post has answered your question, please click "Accept as Solution".
some_guy
Visitor

Thank you very much, you are right. I thought it was called DEbugging :p

If I let the program run normally without a debug session on the board, it works just fine.