Skip to content

Commit

Permalink
Minimize HardwareSerial Receive and Transmit delays (#3664)
Browse files Browse the repository at this point in the history
* Minimize HardwareSerial Receive and Transmit delays

* Remove uartRxFifoToQueue from esp-hal-uart.h

Co-authored-by: Me No Dev <[email protected]>
  • Loading branch information
hreintke and me-no-dev committed Jan 26, 2020
1 parent 80f9f9a commit ed220bd
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion cores/esp32/esp32-hal-uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,11 @@ uart_t* uartBegin(uint8_t uart_nr, uint32_t baudrate, uint32_t config, int8_t rx
uart->dev->conf0.stop_bit_num = ONE_STOP_BITS_CONF;
uart->dev->rs485_conf.dl1_en = 1;
}

// tx_idle_num : idle interval after tx FIFO is empty(unit: the time it takes to send one bit under current baudrate)
// Setting it to 0 prevents line idle time/delays when sending messages with small intervals
uart->dev->idle_conf.tx_idle_num = 0; //

UART_MUTEX_UNLOCK();

if(rxPin != -1) {
Expand Down Expand Up @@ -265,7 +270,7 @@ uint32_t uartAvailable(uart_t* uart)
if(uart == NULL || uart->queue == NULL) {
return 0;
}
return uxQueueMessagesWaiting(uart->queue);
return (uxQueueMessagesWaiting(uart->queue) + uart->dev->status.rxfifo_cnt) ;
}

uint32_t uartAvailableForWrite(uart_t* uart)
Expand All @@ -276,12 +281,27 @@ uint32_t uartAvailableForWrite(uart_t* uart)
return 0x7f - uart->dev->status.txfifo_cnt;
}

void uartRxFifoToQueue(uart_t* uart)
{
uint8_t c;
UART_MUTEX_LOCK();
while(uart->dev->status.rxfifo_cnt || (uart->dev->mem_rx_status.wr_addr != uart->dev->mem_rx_status.rd_addr)) {
c = uart->dev->fifo.rw_byte;
xQueueSend(uart->queue, &c, 0);
}
UART_MUTEX_UNLOCK();
}

uint8_t uartRead(uart_t* uart)
{
if(uart == NULL || uart->queue == NULL) {
return 0;
}
uint8_t c;
if ((uxQueueMessagesWaiting(uart->queue) == 0) && (uart->dev->status.rxfifo_cnt > 0))
{
uartRxFifoToQueue(uart);
}
if(xQueueReceive(uart->queue, &c, 0)) {
return c;
}
Expand All @@ -294,6 +314,10 @@ uint8_t uartPeek(uart_t* uart)
return 0;
}
uint8_t c;
if ((uxQueueMessagesWaiting(uart->queue) == 0) && (uart->dev->status.rxfifo_cnt > 0))
{
uartRxFifoToQueue(uart);
}
if(xQueuePeek(uart->queue, &c, 0)) {
return c;
}
Expand Down

5 comments on commit ed220bd

@ale-trevizoli
Copy link
Contributor

@ale-trevizoli ale-trevizoli commented on ed220bd Apr 20, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi,
I have a software that make serial communication with Biometric Module (not that cheap chineses modules, It's a 5000 finger template capacity module), and is working very well with 1.04 release, but when I tested with Master trunk, I start to receive some corrupt serial packets.
For example, when I'm sending 600 finger templates to module, some times I receive checksum error on response packet, but it is random, one time this happen on 130º template, other time 445º, it's very random.
So I come back to 1.04 release again, and everything come back to work without problem again.
Could you think it have something with this commit change?
Sorry for my bad english, I'm Brazilian. But I'm very experienced developer, so I'm trying to figure out, what is changed on this trunk release that make my serial not working correctly.
I cannot put steps to reproduce it because it is very specific to module communication, but if you think I can help, I'm here to supply any information.
Mainly because this trunk will be released like 1.05 some time, and I want to understand what's happening before it.
Thanks!

@ale-trevizoli
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A little more information. I'm using 115200bps and 80Mhz CPU. I think is not a CPU speed problem because I have this exact same code running on ATMEGA2560 16Mhz and is working well.

@atanisoft
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ale-trevizoli please open an issue if you have an issue with the current code. commenting on an old commit will not get much activity as it will generally not be seen.

@ale-trevizoli
Copy link
Contributor

@ale-trevizoli ale-trevizoli commented on ed220bd Apr 21, 2020 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hreintke
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ale-trevizoli
There is an not merged PR #3713 which probably will solve your issue

Please sign in to comment.