UART 'realtime' processing

ShaunWesty
Posts: 4
Joined: Tue Mar 19, 2019 6:44 am

UART 'realtime' processing

Postby ShaunWesty » Tue Mar 19, 2019 7:11 am

Hi All,

I am trying to get the ESP32 to process the UART when every character is received and am having difficulty.
The coms is running at a blistering 2400 BAUD so I do not think this should be a problem for the ESP32.
My code is currently searching for 0x01 followed by 0x03 and the response should be sent out the TX port.
When I send the following (in HEX): 0xFF 0x09 0x01 0x00 0x00 0x08 0x01 0x03, I get the correct response with a 4-10ms delay
However when I send 2 payloads: 0xFF 0x09 0x01 0x00 0x00 0x08 0x01 0x03 0xFF 0x09 0x01 0x00 0x00 0x08 0x01 0x03, I get two responses at the end of the sequence. There is no gap between 'payloads' in the data stream above. I would be expecting a response after the first 0x01 0x03, a gap while the other payload was being processed.
So it looks like the receive is gathering all the data and then only providing it to the uart_read_bytes() function when there is a timeout. I looked at the uart_set_rx_timeout() function with hope, but setting this to 0 stopped any processing (maybe I got this wrong).

I have tried to use an async task (examples/peripherals/uart/uart_async_rxtxtask) even setting the number of bytes to read down to 2. It did not help.

Code: Select all

for(;;) {
	rxBytes = uart_read_bytes(UART_NUM_0, data, 2, 0);
	if (rxBytes > 0){
		processBytes(data, rxBytes);
	}
	vTaskDelay(4 / portTICK_PERIOD_MS);
}
I have tried to use the Interrupt (examples/peripherals/uart/uart_events) system with the same result.
Should I be using the pattern match system? The problem is that 0x01 0x03 needs to be proceeded by another packet (and this will have different values in it). It will start with an 0xFF 0x09 and have a total of 8 characters, not sure I can pattern match on this vague requirement.

Any direction would be wonderful.

As a final question.. Is there any way to get the TX data to be sent quicker (reducing the delay)? I enabled
uart_config.use_ref_tick = true
and that changed the delay from 30ms to 8ms. If I change the BAUD to 115200, the TX delay is less than 1ms.

Shaun

ESP_Sprite
Posts: 9043
Joined: Thu Nov 26, 2015 4:08 am

Re: UART 'realtime' processing

Postby ESP_Sprite » Tue Mar 19, 2019 8:27 am

The UART uses a receive FIFO; it normally stores some data there and only interrupts the main CPU once either the FIFO gets full-ish or some time with no transmission passes. You should be able to configure the 'full' threshold to 1 character using uart_intr_config(), if I'm not mistaken that should give you the behaviour you expected.

Not sure why using ref_tick changes the behaviour... it shouldn't do so in a meaningful way at these speeds.

Who is online

Users browsing this forum: MicroController, ngobduong and 117 guests