cancel
Showing results for 
Search instead for 
Did you mean: 

UART set up in STM32L0

pbdiazam
Associate II

I want to stablish a UART communication between two boards (B-L072Z-LRWAN1 and 8PYA00-SIMCOM-EVB_V1.02). I tried to make a version of UART communication looking at all the examples provided by ST:

static USART_HandleTypeDef huart;

huart.Instance = USART1;

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;

if(HAL_UART_Init(&huart) != HAL_OK)

{

Error_Handler();

}

GPIO_InitTypeDef initStruct = {0};

initStruct.Speed = GPIO_SPEED_FREQ_HIGH;

initStruct.Alternate = USART1_AF ;

// TX

initStruct.Mode = GPIO_MODE_AF_PP;

initStruct.Pull = GPIO_NOPULL ;

HW_GPIO_Init( txport, txpin, &initStruct);

// RX

initStruct.Mode = GPIO_MODE_INPUT;

initStruct.Pull = GPIO_PULLUP;

HW_GPIO_Init( rxport, rxpin, &initStruct );

Do you see if something is missing in the set up?

Also, here is the board pinout: https://os.mbed.com/platforms/ST-Discovery-LRWAN1/

Can anyone explain me which of these pins I can use for the UART communication?

5 REPLIES 5

> Do you see if something is missing in the set up?

No, but the standard debugging method is to read out the contrent of relevant USART and GPIO registers and check if they are set as expected.

> Can anyone explain me which of these pins I can use for the UART communication?

In the datasheet to the STM32 you have, there is a "Pin descriptions" chapter, and there is a "pin definitions" table which lists all functions for each pin, and then "alternate functions" tables where the individual functionalities under columns with numbers to be programmed into the respective GPIOx_AFR registers are listed.

JW

I don't like the form you're using. Both pins should be in GPIO_MODE_AF_PP, the peripheral controls the IO cell. Make sure you enable the GPIO clock before initializing.

void vcom_IoInit(void)

{

 GPIO_InitTypeDef GPIO_InitStruct={0};

   /* Enable GPIO TX/RX clock */

 USARTX_TX_GPIO_CLK_ENABLE();

 USARTX_RX_GPIO_CLK_ENABLE();

   /* UART TX GPIO pin configuration */

 GPIO_InitStruct.Pin      = USARTX_TX_PIN;

 GPIO_InitStruct.Mode     = GPIO_MODE_AF_PP;

 GPIO_InitStruct.Pull     = GPIO_PULLUP;

 GPIO_InitStruct.Speed    = GPIO_SPEED_HIGH;

 GPIO_InitStruct.Alternate = USARTX_TX_AF;

 HAL_GPIO_Init(USARTX_TX_GPIO_PORT, &GPIO_InitStruct);

 /* UART RX GPIO pin configuration */

 GPIO_InitStruct.Pin = USARTX_RX_PIN;

 GPIO_InitStruct.Alternate = USARTX_RX_AF;

 HAL_GPIO_Init(USARTX_RX_GPIO_PORT, &GPIO_InitStruct);

}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Cross posted here. If you're going to post in multiple places please provide links.

https://electronics.stackexchange.com/questions/396982/uart-set-up-in-stm32l0

The DISCO board is unlikely to be able to power a modem.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
pbdiazam
Associate II

I ended up making some changes in the code as you said but still need some help:

/* 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 (for testing purposes). I press the board button to transmit/receive. The code used to transmit/receive is the following:

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

uint8_t rxData[5] = {0x00, 0x00, 0x00, 0x00, 0x00};

The transmitter side code is:

if(HAL_UART_Transmit(&huart, txData, 2, 10000) != HAL_OK)

{

PRINTF("Transmission error\n\r");

}

else

{

PRINTF("Transmission done\n\r");

}

The receiver side code is:

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.

What am I doing wrong?

>>What am I doing wrong?

Hard to tell. You're going to have to debug things, making sure it makes it to that piece of code, and that the USART is ready to receive, and doesn't have some error state (parity, noise, framing).

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..