Skip to content

Implements UART SerialHardware Refactoring on top of IDF #5549

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 23, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 47 additions & 34 deletions cores/esp32/HardwareSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "HardwareSerial.h"

#if CONFIG_IDF_TARGET_ESP32
// ESP32 UART1 and UART2 suggested default pins - software configurable

#ifndef RX1
#define RX1 9
Expand All @@ -24,8 +25,16 @@
#define TX2 17
#endif

#else
void serialEvent(void) __attribute__((weak));
void serialEvent1(void) __attribute__((weak));
void serialEvent2(void) __attribute__((weak));
void serialEvent(void) {}
void serialEvent1(void) {}
void serialEvent2(void) {}

#elif CONFIG_IDF_TARGET_ESP32S2

// ESP32-S2 UART1 suggested default pins - software configurable
#ifndef RX1
#define RX1 18
#endif
Expand All @@ -34,8 +43,29 @@
#define TX1 17
#endif

void serialEvent(void) __attribute__((weak));
void serialEvent1(void) __attribute__((weak));
void serialEvent(void) {}
void serialEvent1(void) {}

#elif CONFIG_IDF_TARGET_ESP32C3
// ESP32-C3 UART1 suggested default pins - software configurable

#ifndef RX1
#define RX1 18
#endif

#ifndef TX1
#define TX1 19

void serialEvent(void) __attribute__((weak));
void serialEvent1(void) __attribute__((weak));
void serialEvent(void) {}
void serialEvent1(void) {}

#endif

#endif
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SERIAL)
#if ARDUINO_USB_CDC_ON_BOOT //Serial used for USB CDC
HardwareSerial Serial0(0);
Expand All @@ -48,6 +78,15 @@ HardwareSerial Serial2(2);
#endif
#endif

void serialEventRun(void)
{
if(Serial.available()) serialEvent();
if(Serial1.available()) serialEvent1();
#if CONFIG_IDF_TARGET_ESP32
if(Serial2.available()) serialEvent2();
#endif
}

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, uint8_t rxfifo_full_thrhd)
Expand All @@ -57,7 +96,9 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in
return;
}
if(_uart) {
end();
// in this case it is a begin() over a previous begin() - maybe to change baud rate
// thus do not disable debug output
end(false);
}
if(_uart_nr == 0 && rxPin < 0 && txPin < 0) {
#if CONFIG_IDF_TARGET_ESP32
Expand All @@ -82,51 +123,23 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in
}
#endif
_uart = uartBegin(_uart_nr, baud ? baud : 9600, config, rxPin, txPin, 256, invert, rxfifo_full_thrhd);
_tx_pin = txPin;
_rx_pin = rxPin;

if(!baud) {
uartStartDetectBaudrate(_uart);
time_t startMillis = millis();
unsigned long detectedBaudRate = 0;
while(millis() - startMillis < timeout_ms && !(detectedBaudRate = uartDetectBaudrate(_uart))) {
yield();
}

end();

if(detectedBaudRate) {
delay(100); // Give some time...
_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;
_tx_pin = 255;
_rx_pin = 255;
}
}
}

void HardwareSerial::updateBaudRate(unsigned long baud)
{
uartSetBaudRate(_uart, baud);
}

void HardwareSerial::end()
void HardwareSerial::end(bool turnOffDebug)
{
if(uartGetDebug() == _uart_nr) {
if(turnOffDebug && uartGetDebug() == _uart_nr) {
uartSetDebug(0);
}
delay(10);
log_v("pins %d %d",_tx_pin, _rx_pin);
uartEnd(_uart, _tx_pin, _rx_pin);
uartEnd(_uart);
_uart = 0;
}

size_t HardwareSerial::setRxBufferSize(size_t new_size) {
return uartResizeRxBuffer(_uart, new_size);
}

void HardwareSerial::setDebugOutput(bool en)
{
if(_uart == 0) {
Expand Down Expand Up @@ -212,7 +225,7 @@ uint32_t HardwareSerial::baudRate()
}
HardwareSerial::operator bool() const
{
return true;
return uartIsDriverInstalled(_uart);
}

void HardwareSerial::setRxInvert(bool invert)
Expand Down
5 changes: 1 addition & 4 deletions cores/esp32/HardwareSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class HardwareSerial: public Stream
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, uint8_t rxfifo_full_thrhd = 112);
void end();
void end(bool turnOffDebug = true);
void updateBaudRate(unsigned long baud);
int available(void);
int availableForWrite(void);
Expand Down Expand Up @@ -98,16 +98,13 @@ class HardwareSerial: public Stream
uint32_t baudRate();
operator bool() const;

size_t setRxBufferSize(size_t);
void setDebugOutput(bool);

void setRxInvert(bool);

protected:
int _uart_nr;
uart_t* _uart;
uint8_t _tx_pin;
uint8_t _rx_pin;
};

extern void serialEventRun(void) __attribute__((weak));
Expand Down
Loading