Fix Serial RX and add option for FIFO Full Threshold in Serial.begin

Fixes: https://github.com/espressif/arduino-esp32/issues/5005
This commit is contained in:
me-no-dev 2021-04-08 15:29:53 +03:00
parent 425619dfea
commit 371f382db7
5 changed files with 25 additions and 18 deletions

View File

@ -50,7 +50,7 @@ HardwareSerial Serial2(2);
HardwareSerial::HardwareSerial(int uart_nr) : _uart_nr(uart_nr), _uart(NULL) {}
void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, int8_t txPin, bool invert, unsigned long timeout_ms)
void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, int8_t txPin, bool invert, unsigned long timeout_ms, uint8_t rxfifo_full_thrhd)
{
if(0 > _uart_nr || _uart_nr > 2) {
log_e("Serial number is invalid, please use 0, 1 or 2");
@ -78,7 +78,7 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in
txPin = TX2;
}
#endif
_uart = uartBegin(_uart_nr, baud ? baud : 9600, config, rxPin, txPin, 256, invert);
_uart = uartBegin(_uart_nr, baud ? baud : 9600, config, rxPin, txPin, 256, invert, rxfifo_full_thrhd);
_tx_pin = txPin;
_rx_pin = rxPin;
@ -94,7 +94,7 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in
if(detectedBaudRate) {
delay(100); // Give some time...
_uart = uartBegin(_uart_nr, detectedBaudRate, config, rxPin, txPin, 256, invert);
_uart = uartBegin(_uart_nr, detectedBaudRate, config, rxPin, txPin, 256, invert, rxfifo_full_thrhd);
} else {
log_e("Could not detect baudrate. Serial data at the port must be present within the timeout for detection to be possible");
_uart = NULL;

View File

@ -55,7 +55,7 @@ class HardwareSerial: public Stream
public:
HardwareSerial(int uart_nr);
void begin(unsigned long baud, uint32_t config=SERIAL_8N1, int8_t rxPin=-1, int8_t txPin=-1, bool invert=false, unsigned long timeout_ms = 20000UL);
void begin(unsigned long baud, uint32_t config=SERIAL_8N1, int8_t rxPin=-1, int8_t txPin=-1, bool invert=false, unsigned long timeout_ms = 20000UL, uint8_t rxfifo_full_thrhd = 112);
void end();
void updateBaudRate(unsigned long baud);
int available(void);

View File

@ -48,8 +48,6 @@ typedef struct {
bool external_phy;
} tinyusb_config_t;
static TaskHandle_t s_tusb_tskh;
static void configure_pins(usb_hal_context_t *usb)
{
for (const usb_iopin_dsc_t *iopin = usb_periph_iopins; iopin->pin != -1; ++iopin) {
@ -74,7 +72,6 @@ static void configure_pins(usb_hal_context_t *usb)
esp_err_t tinyusb_driver_install(const tinyusb_config_t *config)
{
int res;
log_i("Driver installation...");
// Hal init

View File

@ -130,10 +130,10 @@ static void ARDUINO_ISR_ATTR _uart_isr(void *arg)
}
}
void uartEnableInterrupt(uart_t* uart)
static void uartEnableInterrupt(uart_t* uart, uint8_t rxfifo_full_thrhd)
{
UART_MUTEX_LOCK();
uart->dev->conf1.rxfifo_full_thrhd = 112;
uart->dev->conf1.rxfifo_full_thrhd = rxfifo_full_thrhd;
#if CONFIG_IDF_TARGET_ESP32
uart->dev->conf1.rx_tout_thrhd = 2;
#else
@ -149,7 +149,7 @@ void uartEnableInterrupt(uart_t* uart)
UART_MUTEX_UNLOCK();
}
void uartDisableInterrupt(uart_t* uart)
static void uartDisableInterrupt(uart_t* uart)
{
UART_MUTEX_LOCK();
uart->dev->conf1.val = 0;
@ -162,7 +162,7 @@ void uartDisableInterrupt(uart_t* uart)
UART_MUTEX_UNLOCK();
}
void uartDetachRx(uart_t* uart, uint8_t rxPin)
static void uartDetachRx(uart_t* uart, uint8_t rxPin)
{
if(uart == NULL) {
return;
@ -171,7 +171,7 @@ void uartDetachRx(uart_t* uart, uint8_t rxPin)
uartDisableInterrupt(uart);
}
void uartDetachTx(uart_t* uart, uint8_t txPin)
static void uartDetachTx(uart_t* uart, uint8_t txPin)
{
if(uart == NULL) {
return;
@ -179,17 +179,17 @@ void uartDetachTx(uart_t* uart, uint8_t txPin)
pinMatrixOutDetach(txPin, false, false);
}
void uartAttachRx(uart_t* uart, uint8_t rxPin, bool inverted)
static void uartAttachRx(uart_t* uart, uint8_t rxPin, bool inverted, uint8_t rxfifo_full_thrhd)
{
if(uart == NULL || rxPin >= GPIO_PIN_COUNT) {
return;
}
pinMode(rxPin, INPUT);
uartEnableInterrupt(uart);
uartEnableInterrupt(uart, rxfifo_full_thrhd);
pinMatrixInAttach(rxPin, UART_RXD_IDX(uart->num), inverted);
}
void uartAttachTx(uart_t* uart, uint8_t txPin, bool inverted)
static void uartAttachTx(uart_t* uart, uint8_t txPin, bool inverted)
{
if(uart == NULL || txPin >= GPIO_PIN_COUNT) {
return;
@ -198,7 +198,7 @@ void uartAttachTx(uart_t* uart, uint8_t txPin, bool inverted)
pinMatrixOutAttach(txPin, UART_TXD_IDX(uart->num), inverted, false);
}
uart_t* uartBegin(uint8_t uart_nr, uint32_t baudrate, uint32_t config, int8_t rxPin, int8_t txPin, uint16_t queueLen, bool inverted)
uart_t* uartBegin(uint8_t uart_nr, uint32_t baudrate, uint32_t config, int8_t rxPin, int8_t txPin, uint16_t queueLen, bool inverted, uint8_t rxfifo_full_thrhd)
{
if(uart_nr >= UART_PORTS_NUM) {
return NULL;
@ -256,7 +256,7 @@ uart_t* uartBegin(uint8_t uart_nr, uint32_t baudrate, uint32_t config, int8_t rx
UART_MUTEX_UNLOCK();
if(rxPin != -1) {
uartAttachRx(uart, rxPin, inverted);
uartAttachRx(uart, rxPin, inverted, rxfifo_full_thrhd);
}
if(txPin != -1) {
@ -322,7 +322,11 @@ uint32_t uartAvailable(uart_t* uart)
if(uart == NULL || uart->queue == NULL) {
return 0;
}
#ifdef UART_READ_RX_FIFO
return (uxQueueMessagesWaiting(uart->queue) + uart->dev->status.rxfifo_cnt) ;
#else
return uxQueueMessagesWaiting(uart->queue);
#endif
}
uint32_t uartAvailableForWrite(uart_t* uart)
@ -333,6 +337,7 @@ uint32_t uartAvailableForWrite(uart_t* uart)
return 0x7f - uart->dev->status.txfifo_cnt;
}
#ifdef UART_READ_RX_FIFO
void uartRxFifoToQueue(uart_t* uart)
{
uint8_t c;
@ -357,6 +362,7 @@ void uartRxFifoToQueue(uart_t* uart)
uart->dev->int_clr.val = 0xffffffff;
UART_MUTEX_UNLOCK();
}
#endif
uint8_t uartRead(uart_t* uart)
{
@ -364,10 +370,12 @@ uint8_t uartRead(uart_t* uart)
return 0;
}
uint8_t c;
#ifdef UART_READ_RX_FIFO
if ((uxQueueMessagesWaiting(uart->queue) == 0) && (uart->dev->status.rxfifo_cnt > 0))
{
uartRxFifoToQueue(uart);
}
#endif
if(xQueueReceive(uart->queue, &c, 0)) {
return c;
}
@ -380,10 +388,12 @@ uint8_t uartPeek(uart_t* uart)
return 0;
}
uint8_t c;
#ifdef UART_READ_RX_FIFO
if ((uxQueueMessagesWaiting(uart->queue) == 0) && (uart->dev->status.rxfifo_cnt > 0))
{
uartRxFifoToQueue(uart);
}
#endif
if(xQueuePeek(uart->queue, &c, 0)) {
return c;
}

View File

@ -51,7 +51,7 @@ extern "C" {
struct uart_struct_t;
typedef struct uart_struct_t uart_t;
uart_t* uartBegin(uint8_t uart_nr, uint32_t baudrate, uint32_t config, int8_t rxPin, int8_t txPin, uint16_t queueLen, bool inverted);
uart_t* uartBegin(uint8_t uart_nr, uint32_t baudrate, uint32_t config, int8_t rxPin, int8_t txPin, uint16_t queueLen, bool inverted, uint8_t rxfifo_full_thrhd);
void uartEnd(uart_t* uart, uint8_t rxPin, uint8_t txPin);
uint32_t uartAvailable(uart_t* uart);