cancel
Showing results for 
Search instead for 
Did you mean: 

Half-duplex USART ?

rousseaux
Associate
Posted on February 11, 2014 at 22:28

Hi,

I'd like to use the USART3 of the STM32F407 (Discovery Board) in Half Duplex to control a Dynamixel AX12A. My USART initialization is the following:

void USART3_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
/* Enable GPIO clock */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
/* Enable UART clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
/* Connect PXx to USARTx_Tx*/
GPIO_PinAFConfig(GPIOD, GPIO_PinSource8, GPIO_AF_USART3);
/* Configure USART Tx as alternate function */
GPIO_InitStructure.GPIO_OType = GPIO_Mode_IN; // Input (could also use Open Drain)
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 1000000;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_HalfDuplexCmd(USART3, ENABLE); 
/* USART configuration */
USART_Init(USART3, &USART_InitStructure);
/* Enable USART */
USART_Cmd(USART3, ENABLE);
}

The TX part seems to work fine. The problem is that the device answers (oscilloscope) but I have no RX flag when doing a simple check: while (USART_GetFlagStatus(USART3, USART_FLAG_RXNE) == RESET); Is my configuration correct ? How can I activate the RX part ? And also, is there any trick to disable/enable RX on the fly ? (I can get a RX flag on my own emission but not on the answer) Thanks! #half-duplex-usart
3 REPLIES 3
Posted on February 11, 2014 at 23:14

I might initialize it thusly

void USART3_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
/* Enable GPIO clock */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
/* Enable UART clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
/* Connect PXx to USARTx_Tx*/
GPIO_PinAFConfig(GPIOD, GPIO_PinSource8, GPIO_AF_USART3);
/* Configure USART Tx as alternate function */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_Init(GPIOD, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 1000000;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
/* USART configuration */
USART_Init(USART3, &USART_InitStructure);
/* Enable USART */
USART_Cmd(USART3, ENABLE);
USART_HalfDuplexCmd(USART3, ENABLE); 
}

Not sure there is any magic to half-duplex, you might want to dig up SmartCard or RS485 type examples. You should be able to play with the RX/TX enable bits of the control register. If you can receive your own transmissions, I'd start looking very carefully at the bit timing of the slave response, and the format/framing.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
rousseaux
Associate
Posted on February 12, 2014 at 21:01

Thanks!

Your configuration doesn't change the result : /.

The timing seems to be good since I'm doing a polling of the RX flag after my emission and I see the answer coming during this period (100µs after the emission). 

For the RE bit, I don't understand how I can change its status only USART3. USART_CR1 control register seems common for all USART in the Reference manual...

Posted on February 12, 2014 at 21:27

USART1->CR1 != USART2->CR1

The peripherals are step-and-repeat, each has their own registers, offset from their own unique base address.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..