Sming Framework API
Sming - Open Source framework for high efficiency WiFi SoC ESP8266 native development with C++ language.
HardwareSerial.h
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/anakod/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * HardwareSerial.h
8  *
9  ****/
10 
16 #ifndef _SMING_CORE_HARDWARE_SERIAL_H_
17 #define _SMING_CORE_HARDWARE_SERIAL_H_
18 
19 #include "WiringFrameworkDependencies.h"
20 #include "Data/Stream/ReadWriteStream.h"
21 #include "Delegate.h"
22 #include "espinc/uart.h"
23 
24 #define UART_ID_0 0
25 #define UART_ID_1 1
26 
27 #define NUMBER_UARTS UART_COUNT
28 
30 
45 
50 
51 class CommandExecutor;
52 
53 enum SerialConfig {
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,
78 };
79 
81 enum SerialMode { SERIAL_FULL = UART_FULL, SERIAL_RX_ONLY = UART_RX_ONLY, SERIAL_TX_ONLY = UART_TX_ONLY };
82 
83 #ifndef DEFAULT_RX_BUFFER_SIZE
84 #define DEFAULT_RX_BUFFER_SIZE 256
85 #endif
86 
87 #ifndef DEFAULT_TX_BUFFER_SIZE
88 #define DEFAULT_TX_BUFFER_SIZE 0
89 #endif
90 
94  eSERS_Overflow = UIOF,
97 };
98 
101 {
102 public:
107  HardwareSerial(int uartPort) : uartNr(uartPort)
108  {
109  }
110 
111  ~HardwareSerial();
112 
113  void setPort(int uartPort)
114  {
115  end();
116  uartNr = uartPort;
117  }
118 
119  int getPort()
120  {
121  return uartNr;
122  }
123 
127  void begin(uint32_t baud = 9600)
128  {
129  begin(baud, SERIAL_8N1, SERIAL_FULL, 1);
130  }
131 
139  void begin(uint32_t baud, SerialConfig config)
140  {
141  begin(baud, config, SERIAL_FULL, 1);
142  }
143 
152  void begin(uint32_t baud, SerialConfig config, SerialMode mode)
153  {
154  begin(baud, config, mode, 1);
155  }
156 
157  void begin(uint32_t baud, SerialConfig config, SerialMode mode, uint8_t txPin);
158 
162  void end();
163 
169  size_t setRxBufferSize(size_t size);
170 
176  size_t setTxBufferSize(size_t size);
177 
183  void setTxWait(bool wait)
184  {
185  bitWrite(options, UART_OPT_TXWAIT, wait);
186  uart_set_options(uart, options);
187  }
188 
194  void swap()
195  {
196  swap(1);
197  }
198 
203  void swap(uint8_t tx_pin)
204  {
205  uart_swap(uart, tx_pin);
206  }
207 
215  void setTx(uint8_t tx_pin)
216  {
217  uart_set_tx(uart, tx_pin);
218  }
219 
227  void pins(uint8_t tx, uint8_t rx)
228  {
229  uart_set_pins(uart, tx, rx);
230  }
231 
235  int available() override
236  {
237  return (int)uart_rx_available(uart);
238  }
239 
244  int read() override
245  {
246  return uart_read_char(uart);
247  }
248 
256  uint16_t readMemoryBlock(char* buf, int max_len) override
257  {
258  return uart_read(uart, buf, max_len);
259  }
260 
261  bool seek(int len) override
262  {
263  return false;
264  }
265 
266  bool isFinished() override
267  {
268  return false;
269  }
270 
275  int peek() override
276  {
277  return uart_peek_char(uart);
278  }
279 
284  void clear(SerialMode mode = SERIAL_FULL)
285  {
286  uart_flush(uart, uart_mode_t(mode));
287  }
288 
292  void flush() override // Stream
293  {
294  uart_wait_tx_empty(uart);
295  }
296 
297  using Stream::write;
298 
304  size_t write(const uint8_t* buffer, size_t size) override
305  {
306  return uart_write(uart, buffer, size);
307  }
308 
313  void systemDebugOutput(bool enabled);
314 
320  void commandProcessing(bool reqEnable);
321 
326  bool setCallback(StreamDataReceivedDelegate dataReceivedDelegate)
327  {
328  return onDataReceived(dataReceivedDelegate);
329  }
330 
335  bool onDataReceived(StreamDataReceivedDelegate dataReceivedDelegate)
336  {
337  this->HWSDelegate = dataReceivedDelegate;
338  return updateUartCallback();
339  }
340 
345  bool onTransmitComplete(TransmitCompleteDelegate transmitCompleteDelegate)
346  {
347  this->transmitComplete = transmitCompleteDelegate;
348  return updateUartCallback();
349  }
350 
356  __forceinline void setUartCallback(uart_callback_t callback, void* param = nullptr)
357  {
358  uart_set_callback(uart, callback, param);
359  }
360 
365  bool isTxEnabled()
366  {
367  return uart_tx_enabled(uart);
368  }
369 
374  bool isRxEnabled()
375  {
376  return uart_rx_enabled(uart);
377  }
378 
383  uint32_t baudRate()
384  {
385  return uart_get_baudrate(uart);
386  }
387 
393  uint32_t setBaudRate(uint32_t baudrate)
394  {
395  return uart_set_baudrate(uart, baudrate);
396  }
397 
401  operator bool() const
402  {
403  return uart != nullptr;
404  }
405 
411  int indexOf(char c) override
412  {
413  return uart_rx_find(uart, c);
414  }
415 
420  uart_t* getUart()
421  {
422  return uart;
423  }
424 
430  unsigned getStatus()
431  {
432  return uart_get_status(uart);
433  }
434 
435 private:
436  int uartNr = -1;
437  TransmitCompleteDelegate transmitComplete = nullptr;
438  StreamDataReceivedDelegate HWSDelegate = nullptr;
439  CommandExecutor* commandExecutor = nullptr;
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;
447 
453  static void IRAM_ATTR staticCallbackHandler(uart_t* uart, uint32_t status);
454  static void staticOnStatusChange(uint32_t param);
455  void invokeCallbacks();
456 
461  bool updateUartCallback();
462 };
463 
473 extern HardwareSerial Serial;
474 
476 #endif /* _SMING_CORE_HARDWARE_SERIAL_H_ */
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