STM32L0 communication through UART

by Pablo Díaz Amores   Last Updated September 24, 2018 16:25 PM

I am trying to stablish a UART communication between 2 boards (the 2 board are B-L072Z-LRWAN1, for testing purposes).

I need a bit of help because the transmission/reception is not working. Here are the UART settings:

/* UART -------------------------------------------------------- */

 /* Properties */
 huart.Instance    = USART2;
 huart.Init.BaudRate  = 115200;
 huart.Init.WordLength = UART_WORDLENGTH_8B;
 huart.Init.StopBits  = UART_STOPBITS_1;
 huart.Init.Parity   = UART_PARITY_NONE;
 huart.Init.HwFlowCtl = UART_HWCONTROL_NONE;
 huart.Init.Mode    = UART_MODE_TX_RX;
 huart.Init.OverSampling = UART_OVERSAMPLING_16;
 huart.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
 huart.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;

 /* UART Init */
 if(HAL_UART_Init(&huart) != HAL_OK)
 {
     Error_Handler();
 }

 /* UART Hardware */
 GPIO_InitTypeDef initStruct1 = {0};
 /* USART1 GPIO Configuration
 PA9   ------> USART1_TX
 PA10  ------> USART1_RX
 */
 __GPIOA_CLK_ENABLE();
 __USART2_CLK_ENABLE();

 // TX
 initStruct1.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
 initStruct1.Mode = GPIO_MODE_AF_PP;
 initStruct1.Pull = GPIO_NOPULL;
 initStruct1.Alternate = GPIO_AF4_USART2;
 HW_GPIO_Init( GPIOA, GPIO_PIN_9, &initStruct1);

 // RX    
 initStruct1.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
 initStruct1.Mode = GPIO_MODE_AF_PP;
 initStruct1.Pull = GPIO_NOPULL;
 initStruct1.Alternate = GPIO_AF4_USART2;
 HW_GPIO_Init( GPIOA, GPIO_PIN_10, &initStruct1);

 /* ------------------------------------------------------------- */ 

I use one board as transmitter and other board as receiver. I press the board button to transmit/receive and the code I use to transmit/receive is the following:

uint8_t txData[5] = {0x0F, 0x0E, 0x0D, 0x0C, 0x0B};
uint8_t rxData[5] = {0x00, 0x00, 0x00, 0x00, 0x00};

// TX side
if(HAL_UART_Transmit(&huart, txData, 2, 10000) != HAL_OK)
{
    PRINTF("Transmission error\n\r");
}
else
{
    PRINTF("Transmission done\n\r");
}

// RX side
if(HAL_UART_Receive(&huart, rxData, 2, 10000) != HAL_OK)
{
    PRINTF("Reception error\n\r");
}
else
{
    PRINTF("Reception done\n\r");
    PRINTF("%X", rxData[0]);
    PRINTF("%X", rxData[1]);
    PRINTF("%X", rxData[2]);
    PRINTF("%X", rxData[3]);
    PRINTF("%X", rxData[4]);
}

I have wired up PA_9 (TX) to PA_10 (RX). When I press the button in transmitter side I get the "Transmission done" message but the receiver is not responding to the button and I dont get anything on screen in receiver side.

Tags : stm32 c uart embedded


Related Questions


Using circular buffer over UART

Updated March 08, 2018 18:25 PM

Receive data with variable length over UART

Updated March 11, 2018 21:25 PM


GPS interface PIC16LF1937 using UART read issue

Updated May 11, 2017 08:25 AM

Periodic junk on serial interface

Updated May 24, 2018 15:25 PM