cancel
Showing results for 
Search instead for 
Did you mean: 

UART mixed flow control

DCarr.1
Senior

Hello all,

I have a device connected to a stm32l496rg on a custom via uart.

At system startup, the remote uart may or may not have hardware flow control enabled, so what I want to do is configure the MCU UART for no hardware flow control and put the 

RTS and CTS lines in a state which, if it turns out to be in hardware flow control mode, will convince the remote UART that it can send and receive.

the MCU UART is configured initially in cubemx to use hardware flow control, so I use the following function to switch:

static bool rtscts = true; // uart is configured to RTS/CTS in cubemx
 
static void set_rtscts(bool value) {
	/* reconfigure the uart */
	HAL_UART_DeInit(&huart2);
 
	huart2.Init.HwFlowCtl = value? UART_HWCONTROL_RTS_CTS: UART_HWCONTROL_NONE;
	HAL_UART_Init(&huart2);
 
	if(!value) { /* override RTS and CTS control pins */
		GPIO_InitTypeDef gpio = {
			.Pin = 0,
			.Mode = 0,
			.Pull = GPIO_NOPULL,
			.Speed = GPIO_SPEED_FREQ_VERY_HIGH,
			.Alternate = 0,
		};
 
		gpio.Pin = BL_RTS_Pin; gpio.Mode = ??; HAL_GPIO_Init(GPIOA, &gpio);
		gpio.Pin = BL_CTS_Pin; gpio.Mode = ??; HAL_GPIO_Init(GPIOA, &gpio);
 
                // set some initial values on rts/cts?
	}
}

The first thing I'll do is send a command to the remote device to set it to hardware flow control then switch my local uart back and carry on from there.

What configuration and values of the RTS and CTS pins do I need to use to convince the remote UART that it can send and receive if it turns out that it was already in hardware flow control mode?

Thanks

2 REPLIES 2
TDK
Guru

You only control whether or not your can receive. The other side will signal when you can send using the same mechanism.

Keep RTS asserted (low) to signal you can receive.

Send only when CTS is asserted.

If you feel a post has answered your question, please click "Accept as Solution".

Thanks, that makes sense.

So my set_rtscts function becomes

static void set_rtscts(bool value) {
	/* reconfigure the uart (snipped) */
        /* this leaves the RTS and CTS pins configured for the UART, which I'll override now */
	if(!value) { /* override RTS and CTS control pins */
		GPIO_InitTypeDef gpio = { /* snipped */ };
 
		HAL_GPIO_WritePin(GPIOA, BL_RTS_Pin, GPIO_PIN_RESET);
 
		gpio.Pin = BL_RTS_Pin; gpio.Mode = GPIO_MODE_OUTPUT_PP; HAL_GPIO_Init(GPIOA, &gpio);
		gpio.Pin = BL_CTS_Pin; gpio.Mode = GPIO_MODE_INPUT; HAL_GPIO_Init(GPIOA, &gpio);
 
	}
}

When I've got hardware flow control on the local UART turned off, only sending when CTS is asserted corresponds to assuming that the remote UART is in CTSRTS mode and respecting its CTS, right? if the remote UART is not in CTSRTS mode, will my CTS ever get asserted?