Question
STM32F0 - two USARTs in half duplex mode
Posted on July 29, 2012 at 19:47
Hi,
I have a question regarding the USART functionality. The IC I have is supposed to receive data and relay it at a slightly higher rate.I have configured and enabled both USART1 and 2 in half duplex mode, one is running at 9600 bps and another at 11200 bps. Whenever one of the USARTs has received any data (saved to a buffer from within the interrupt handler) it should send it out on the other USART, and viceversa. What is happening though is only the first character gets sent back and forth apparently in an endless loop. I am guessing it's echo, but how can it be? I am changing the USART mode to TX and disabling RX when sending data.if
(UART2_RX_size > 0){
for
(i=0; i<UART2_RX_size; i++)
UART_write_byte(USART1, UART2_RX_buffer[i]);
UART2_RX_size=0;
}
if
(UART1_RX_size > 0){
for
(i=0; i<UART1_RX_size; i++)
UART_write_byte(USART2, UART1_RX_buffer[i]);
UART1_RX_size=0;
}
// example interrupt handler
void
USART1_IRQHandler(
void
)
{
uint8_t data;
/* Pick up the character, prepare its binary form */
if
(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) ){
data = USART_ReceiveData(USART1);
// save it to the buffer
UART1_RX_buffer[UART1_RX_size] = data;
UART1_RX_size++;
}
}
// example send function
void
UART_write_byte(USART_TypeDef* USARTx, uint8_t ch)
{
USART_DirectionModeCmd(USARTx,USART_Mode_Rx, DISABLE);
// enable RX mode // on startup
USART_DirectionModeCmd(USARTx,USART_Mode_Tx, ENABLE);
// disable TX mode
/* Loop until transmit data register is empty */
while
(USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET){}
USART_SendData(USARTx, (uint8_t) ch);
while
(USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET){}
USART_DirectionModeCmd(USARTx,USART_Mode_Tx, DISABLE);
// disable TX mode
USART_DirectionModeCmd(USARTx,USART_Mode_Rx, ENABLE);
// enable RX mode // on startup
}
#stm32f0-usart #stm32f0-usart