16 #ifndef _SMING_CORE_HARDWARE_SERIAL_H_ 17 #define _SMING_CORE_HARDWARE_SERIAL_H_ 19 #include "WiringFrameworkDependencies.h" 20 #include "Data/Stream/ReadWriteStream.h" 22 #include "espinc/uart.h" 27 #define NUMBER_UARTS UART_COUNT 54 SERIAL_5N1 = UART_5N1,
55 SERIAL_6N1 = UART_6N1,
56 SERIAL_7N1 = UART_7N1,
57 SERIAL_8N1 = UART_8N1,
58 SERIAL_5N2 = UART_5N2,
59 SERIAL_6N2 = UART_6N2,
60 SERIAL_7N2 = UART_7N2,
61 SERIAL_8N2 = UART_8N2,
62 SERIAL_5E1 = UART_5E1,
63 SERIAL_6E1 = UART_6E1,
64 SERIAL_7E1 = UART_7E1,
65 SERIAL_8E1 = UART_8E1,
66 SERIAL_5E2 = UART_5E2,
67 SERIAL_6E2 = UART_6E2,
68 SERIAL_7E2 = UART_7E2,
69 SERIAL_8E2 = UART_8E2,
70 SERIAL_5O1 = UART_5O1,
71 SERIAL_6O1 = UART_6O1,
72 SERIAL_7O1 = UART_7O1,
73 SERIAL_8O1 = UART_8O1,
74 SERIAL_5O2 = UART_5O2,
75 SERIAL_6O2 = UART_6O2,
76 SERIAL_7O2 = UART_7O2,
77 SERIAL_8O2 = UART_8O2,
81 enum SerialMode { SERIAL_FULL = UART_FULL, SERIAL_RX_ONLY = UART_RX_ONLY, SERIAL_TX_ONLY = UART_TX_ONLY };
83 #ifndef DEFAULT_RX_BUFFER_SIZE 84 #define DEFAULT_RX_BUFFER_SIZE 256 87 #ifndef DEFAULT_TX_BUFFER_SIZE 88 #define DEFAULT_TX_BUFFER_SIZE 0 113 void setPort(
int uartPort)
129 begin(baud, SERIAL_8N1, SERIAL_FULL, 1);
139 void begin(uint32_t baud, SerialConfig config)
141 begin(baud, config, SERIAL_FULL, 1);
154 begin(baud, config, mode, 1);
157 void begin(uint32_t baud, SerialConfig config,
SerialMode mode, uint8_t txPin);
185 bitWrite(options, UART_OPT_TXWAIT, wait);
186 uart_set_options(uart, options);
205 uart_swap(uart, tx_pin);
217 uart_set_tx(uart, tx_pin);
227 void pins(uint8_t tx, uint8_t rx)
229 uart_set_pins(uart, tx, rx);
237 return (
int)uart_rx_available(uart);
246 return uart_read_char(uart);
258 return uart_read(uart, buf, max_len);
277 return uart_peek_char(uart);
286 uart_flush(uart, uart_mode_t(mode));
294 uart_wait_tx_empty(uart);
304 size_t write(
const uint8_t* buffer,
size_t size)
override 306 return uart_write(uart, buffer, size);
337 this->HWSDelegate = dataReceivedDelegate;
338 return updateUartCallback();
347 this->transmitComplete = transmitCompleteDelegate;
348 return updateUartCallback();
358 uart_set_callback(uart, callback, param);
367 return uart_tx_enabled(uart);
376 return uart_rx_enabled(uart);
385 return uart_get_baudrate(uart);
395 return uart_set_baudrate(uart, baudrate);
401 operator bool()
const 403 return uart !=
nullptr;
413 return uart_rx_find(uart, c);
432 return uart_get_status(uart);
440 uart_t* uart =
nullptr;
441 uart_options_t options = _BV(UART_OPT_TXWAIT);
442 size_t txSize = DEFAULT_TX_BUFFER_SIZE;
443 size_t rxSize = DEFAULT_RX_BUFFER_SIZE;
444 volatile uint16_t statusMask = 0;
445 volatile uint16_t callbackStatus = 0;
446 volatile bool callbackQueued =
false;
453 static void IRAM_ATTR staticCallbackHandler(uart_t* uart, uint32_t status);
454 static void staticOnStatusChange(uint32_t param);
455 void invokeCallbacks();
461 bool updateUartCallback();
Break condition detected on receive line.
Definition: HardwareSerial.h:93
void begin(uint32_t baud, SerialConfig config)
Initialise and set its configuration.
Definition: HardwareSerial.h:139
void begin(uint32_t baud=9600)
Initialise the serial port.
Definition: HardwareSerial.h:127
void flush() override
Flush all pending data to the serial port.
Definition: HardwareSerial.h:292
bool isFinished() override
Check if all data has been read.
Definition: HardwareSerial.h:266
void setTxWait(bool wait)
Governs write behaviour when UART transmit buffers are full If false, writes will return short count;...
Definition: HardwareSerial.h:183
void pins(uint8_t tx, uint8_t rx)
Sets the transmission and receiving PINS.
Definition: HardwareSerial.h:227
uint32_t baudRate()
Get the current baud rate.
Definition: HardwareSerial.h:383
void swap(uint8_t tx_pin)
Toggle between use of GPIO13/GPIO15 or GPIO3/GPIO(1/2) as RX and TX.
Definition: HardwareSerial.h:203
SerialMode
values equivalent to uart_mode_t
Definition: HardwareSerial.h:81
uart_t * getUart()
Returns a pointer to the internal uart object. Use with care.
Definition: HardwareSerial.h:420
bool setCallback(StreamDataReceivedDelegate dataReceivedDelegate)
Set handler for received data.
Definition: HardwareSerial.h:326
void end()
De-inits the current UART if it is already used.
void clear(SerialMode mode=SERIAL_FULL)
Clear the serial port transmit/receive buffers.
Definition: HardwareSerial.h:284
size_t setTxBufferSize(size_t size)
Sets transmit buffer size.
void setTx(uint8_t tx_pin)
Toggle between use of GPIO1 and GPIO2 as TX on UART 0.
Definition: HardwareSerial.h:215
Receive buffer overflowed.
Definition: HardwareSerial.h:94
Definition: CommandExecutor.h:18
size_t setRxBufferSize(size_t size)
Sets receiving buffer size.
int read() override
Read a character from serial port.
Definition: HardwareSerial.h:244
virtual size_t write(uint8_t)=0
Writes a single character to output stream.
Receive framing error.
Definition: HardwareSerial.h:95
size_t write(const uint8_t *buffer, size_t size) override
write multiple characters to serial port
Definition: HardwareSerial.h:304
bool seek(int len) override
Move read cursor.
Definition: HardwareSerial.h:261
void swap()
Toggle between use of GPIO13/GPIO15 or GPIO3/GPIO(1/2) as RX and TX.
Definition: HardwareSerial.h:194
void commandProcessing(bool reqEnable)
Configure serial port for command processing.
uint16_t readMemoryBlock(char *buf, int max_len) override
Read a block of characters from serial port.
Definition: HardwareSerial.h:256
Delegate< void(HardwareSerial &serial)> TransmitCompleteDelegate
Delegate callback type for serial data transmit completion.
Definition: HardwareSerial.h:49
int available() override
Get quantity characters available from serial input.
Definition: HardwareSerial.h:235
SerialStatus
Notification and error status bits.
Definition: HardwareSerial.h:92
Parity check failed on received data.
Definition: HardwareSerial.h:96
bool isTxEnabled()
Checks if the current UART can transmit(print) information.
Definition: HardwareSerial.h:365
unsigned getStatus()
Get status error flags and clear them.
Definition: HardwareSerial.h:430
Delegate< void(Stream &source, char arrivedChar, uint16_t availableCharsCount)> StreamDataReceivedDelegate
Delegate callback type for serial data reception.
Definition: HardwareSerial.h:29
Hardware serial class.
Definition: HardwareSerial.h:100
void systemDebugOutput(bool enabled)
Configure serial port for system debug output and redirect output from debugf.
__forceinline void setUartCallback(uart_callback_t callback, void *param=nullptr)
Set callback ISR for received data.
Definition: HardwareSerial.h:356
bool isRxEnabled()
Checks if the current UART can receive information.
Definition: HardwareSerial.h:374
HardwareSerial Serial
Global instance of serial port UART0.
int indexOf(char c) override
Returns the location of the searched character.
Definition: HardwareSerial.h:411
int peek() override
Read a character from serial port without removing from input buffer.
Definition: HardwareSerial.h:275
void begin(uint32_t baud, SerialConfig config, SerialMode mode)
Initialise, set its configuration and mode.
Definition: HardwareSerial.h:152
HardwareSerial(int uartPort)
Create instance of a hardware serial port object.
Definition: HardwareSerial.h:107
bool onTransmitComplete(TransmitCompleteDelegate transmitCompleteDelegate)
Set handler for received data.
Definition: HardwareSerial.h:345
uint32_t setBaudRate(uint32_t baudrate)
Attempt to set the requested baud rate.
Definition: HardwareSerial.h:393
bool onDataReceived(StreamDataReceivedDelegate dataReceivedDelegate)
Set handler for received data.
Definition: HardwareSerial.h:335
Base class for read/write stream.
Definition: ReadWriteStream.h:23