cancel
Showing results for 
Search instead for 
Did you mean: 

Hello. Does anyone have a configuration example for stm32f103 uart in half-duplex mode(switch TX/RX modes) using Low Level Library. Thanks!

alfinikov
Associate III
 
2 REPLIES 2
Piranha
Chief II

Apart from setting HDSEL bit, there is no much difference to full-duplex UART. Just keep in mind that the RX will always be active and TX will "take over" the pin when data is transmitted. RX will even receive the same data that TX is transmitting. Therefore the code has to ignore it or turn RX off during transmission. And of course the software has to manage concurrency to not try to receive and transmit simultaneously.

alfinikov
Associate III

I find solution!

Initialization:

void MX_USART1_UART_Init(void)

{

 LL_USART_InitTypeDef USART_InitStruct = {0};

 LL_GPIO_InitTypeDef GPIO_InitStruct = {0};

 /* Peripheral clock enable */

 LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_USART1);

 LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOB);

 /**USART1 GPIO Configuration

 PB6  ------> USART1_TX

 */

 GPIO_InitStruct.Pin = LL_GPIO_PIN_6;

 GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;

 GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;

 GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;

 LL_GPIO_Init(GPIOB, &GPIO_InitStruct);

 LL_GPIO_AF_EnableRemap_USART1();

 /* USART1 interrupt Init */

 NVIC_SetPriority(USART1_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 15, 0));

 NVIC_EnableIRQ(USART1_IRQn);

 USART_InitStruct.BaudRate = 115200;

 USART_InitStruct.DataWidth = LL_USART_DATAWIDTH_8B;

 USART_InitStruct.StopBits = LL_USART_STOPBITS_1;

 USART_InitStruct.Parity = LL_USART_PARITY_NONE;

 USART_InitStruct.TransferDirection = LL_USART_DIRECTION_TX_RX;

 USART_InitStruct.OverSampling = LL_USART_OVERSAMPLING_16;

 LL_USART_Init(USART1, &USART_InitStruct);

 LL_USART_ConfigHalfDuplexMode(USART1);

 LL_USART_Enable(USART1);

 LL_USART_EnableHalfDuplex(USART1);

 LL_USART_EnableIT_RXNE(USART1);

}

For select rx mode:

LL_USART_DisableDirectionTx(USART1);

LL_USART_EnableDirectionRx(USART1);

For select tx mode:

LL_USART_DisableDirectionRx(USART1);

LL_USART_EnableDirectionTx(USART1);