Skip to main content
VijayRakeshM
Associate III
Mar 8, 2019 8:20 AM UTC
Solved

STM32L072 Discovery Board cannot receive the UART data

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.
S.Ma
S.MaBest answer
Principal
Mar 12, 2019 6:30 AM UTC

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

5 replies

S.Ma
Principal
Mar 8, 2019 12:17 PM UTC

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

VijayRakeshM
Associate III
Mar 8, 2019 12:31 PM UTC

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
Mar 8, 2019 8:23 PM UTC

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
Mar 12, 2019 6:04 AM UTC

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
Mar 12, 2019 6:30 AM UTC

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