cancel
Showing results for 
Search instead for 
Did you mean: 

UART get stuck in UART_WaitOnFlagUntilTimeout

mmed
Associate III

Hi,

I dont know the reason of this issue, I just generate code via STM32cubeMX after enabling UART4 and below there is the main program!: But my program cannot neither receive nor transmit any data and stuck in UART_WaitOnFlagUntilTimeout. Help please

#include "main.h"

/* Private includes ----------------------------------------------------------*/

/* USER CODE BEGIN Includes */

/* 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 huart4;

/* USER CODE BEGIN PV */

uint8_t data[]="hello world \n";

uint8_t data1[10];

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/

void SystemClock_Config(void);

static void MX_GPIO_Init(void);

static void MX_UART4_Init(void);

/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/

/* USER CODE BEGIN 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_UART4_Init();

 /* USER CODE BEGIN 2 */

 /* USER CODE END 2 */

 /* Infinite loop */

 /* USER CODE BEGIN WHILE */

 while (1)

 {

  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */

//HAL_UART_Receive ( &huart4, data1, sizeof(data), 10);

HAL_UART_Transmit ( &huart4, data, sizeof(data), 100);

 }

 /* 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_SCALE1);

 /**Initializes the CPU, AHB and APB busses clocks 

 */

 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;

 RCC_OscInitStruct.HSIState = RCC_HSI_ON;

 RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;

 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;

 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;

 RCC_OscInitStruct.PLL.PLLM = 8;

 RCC_OscInitStruct.PLL.PLLN = 64;

 RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;

 RCC_OscInitStruct.PLL.PLLQ = 7;

 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)

 {

  Error_Handler();

 }

 /**Initializes the CPU, AHB and APB busses clocks 

 */

 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK

               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;

 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;

 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV2;

 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;

 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV4;

 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)

 {

  Error_Handler();

 }

}

/**

 * @brief UART4 Initialization Function

 * @param None

 * @retval None

 */

static void MX_UART4_Init(void)

{

 /* USER CODE BEGIN UART4_Init 0 */

 /* USER CODE END UART4_Init 0 */

 /* USER CODE BEGIN UART4_Init 1 */

 /* USER CODE END UART4_Init 1 */

 huart4.Instance = UART4;

 huart4.Init.BaudRate = 115200;

 huart4.Init.WordLength = UART_WORDLENGTH_8B;

 huart4.Init.StopBits = UART_STOPBITS_1;

 huart4.Init.Parity = UART_PARITY_NONE;

 huart4.Init.Mode = UART_MODE_TX_RX;

 huart4.Init.HwFlowCtl = UART_HWCONTROL_NONE;

 huart4.Init.OverSampling = UART_OVERSAMPLING_16;

 if (HAL_UART_Init(&huart4) != HAL_OK)

 {

  Error_Handler();

 }

 /* USER CODE BEGIN UART4_Init 2 */

 /* USER CODE END UART4_Init 2 */

}

/**

 * @brief GPIO Initialization Function

 * @param None

 * @retval None

 */

static void MX_GPIO_Init(void)

{

 /* GPIO Ports Clock Enable */

 __HAL_RCC_GPIOA_CLK_ENABLE();

}

/* USER CODE BEGIN 4 */

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

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

   tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

 /* USER CODE END 6 */

}

#endif /* USE_FULL_ASSERT */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

11 REPLIES 11
mmed
Associate III

Here the clock tree

mmed
Associate III

0690X000006DA6BQAW.png

S.Ma
Principal

Maybe using the LL vs HAL for USART is more flexible?

mmed
Associate III

same issue using LL driver!

S.Ma
Principal

What STM32 and which package is being used here? Which GPIO for USART are selected?

mmed
Associate III

its STM32F407 Disco, using UART4:

  PA0-WKUP   ------> UART4_TX

  PA1   ------> UART4_RX 

S.Ma
Principal

Sanity check: If in main you use HAL_Delay() which uses systick, does it work?

This in case Systick is not properly working and all timeouts and delay functions are KO

Otherwise, I shared an extract of code with FIFO by SW + USART as LL on this forum. It overrides CubeMX generated code.

mmed
Associate III

Can you give me the link of this post or the code after your change please!

mmed
Associate III

any suggestions please