RX buffer issue while reading same UART from two different tasks

imdaad
Posts: 10
Joined: Mon Oct 26, 2020 12:58 pm

RX buffer issue while reading same UART from two different tasks

Postby imdaad » Sat Feb 19, 2022 6:05 am

I have two freeRTOS tasks that receive from the same UART port.
However, when moving from one task to another, my buffer gets filled and my algorithms can't proceed with this fault because I take the length which comes from

Code: Select all

uart_read_bytes()
.
I move from the first task to the second task when an interrupt triggers and I pause the first task while proceeding with the second task.
while first task running the uart length I receive starts from zero. But when I moved to the second task it starts from 255 (and it doesn't change even if I received some data). what is the workaround for this?

Code: Select all

  
  uint8_t _length = uart_read_bytes(UART_NUM_1, data, PACKET_SIZE, 50 / portTICK_RATE_MS); //UART read bytes from UART buffer
  ESP_LOGD(TAG, "received %d bytes", _length);

  if (_length == PACKET_SIZE) //if 12 bytes received 
these are my two tasks

Code: Select all

  xTaskCreate(tree_intr_func, "tree interrupt task", 2048, NULL, 1, &intrHandle);
  xTaskCreate(mango_scanning, "mango scanning task", 2048, NULL, 1, &mangoHandle);
I tried using

Code: Select all

ESP_ERROR_CHECK(uart_flush(UART_NUM_1));
but then the code didn't proceed onwards.
Can someone help me out

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

Re: RX buffer issue while reading same UART from two different tasks

Postby ESP_Sprite » Sun Feb 20, 2022 6:36 am

I don't think that UART code is re-entrant, as in, only one task can be 'inside' that function at a time. Suggest you figure out some other design pattern to do this, e.g. one task that does the reception and then informs the other tasks based on that.

imdaad
Posts: 10
Joined: Mon Oct 26, 2020 12:58 pm

Re: RX buffer issue while reading same UART from two different tasks

Postby imdaad » Sun Feb 20, 2022 5:41 pm

yes u r correct, internally UART maintains the semaphore.
so what happens is, when I switch to a different task through interrupt routine, UART gets blocked, and interrupt task cannot take the semaphore.
So now the question that I need to answer is basically, while an interrupt occurs I shouldn't switch tasks immediately. I should wait until current task can be safely blocked. Any feedback is highly appreciated.

Who is online

Users browsing this forum: No registered users and 376 guests