Skip to main content
VijayRakeshM
Associate III
March 8, 2019
Solved

STM32L072 Discovery Board cannot receive the UART data

  • March 8, 2019
  • 5 replies
  • 1318 views

Hi,

I'm using STM32L072 Discovery Board and generated UART code using CubeMx, I'm able to transmit the data via UART but not able to receive the data. Please help me in this regard.

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_USART2_UART_Init();
 /* USER CODE BEGIN 2 */
 
 /* USER CODE END 2 */
 
 /* Infinite loop */
 /* USER CODE BEGIN WHILE */
 while (1)
 {
 /* USER CODE END WHILE */
		HAL_UART_Receive(&huart2,buffer,4,5000);
		HAL_UART_Transmit(&huart2,buffer,4,100);
 /* USER CODE BEGIN 3 */
 }
 /* USER CODE END 3 */
}
 
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;
 huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
 huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
 if (HAL_UART_Init(&huart2) != HAL_OK)
 {
 Error_Handler();
 }
 /* USER CODE BEGIN USART2_Init 2 */
 
 /* USER CODE END USART2_Init 2 */
 
}

Thanks & Regards,

Vijay Rakesh.

This topic has been closed for replies.
Best answer by S.Ma

You should open cube library, look at projects for nucleo board, uart examples and test them.

5 replies

S.Ma
Principal
March 8, 2019

Uart received should be done using interrupt and callback as you don t know when incoming data supposely happens.

VijayRakeshM
Associate III
March 8, 2019

Hi @S.Ma​ ,

Thanks for the reply, I was just trying polling method to test UART looping code. Is it mandatory to enable interrupt for polling method?

Thanks & Regards,

Vijay Rakesh.

S.Ma
Principal
March 8, 2019

Never use polling for USART RX or anything that the microcontroller can't control time such as I2C or SPI Slave modes. unless wasting time.

VijayRakeshM
Associate III
March 12, 2019

Thanks for your suggestion and I will implement interrupt but before moving on to the interrupt i would like to work on polling method so, Could please tell me why i'm not able to receive data via uart but i can transmit data.

S.Ma
S.MaBest answer
Principal
March 12, 2019

You should open cube library, look at projects for nucleo board, uart examples and test them.