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 
13 #ifndef _HARDWARESERIAL_H_
14 #define _HARDWARESERIAL_H_
15 
16 #include "../Wiring/WiringFrameworkDependencies.h"
17 #include "../Wiring/Stream.h"
18 #include "../SmingCore/Delegate.h"
19 #include "../Services/CommandProcessing/CommandProcessingIncludes.h"
20 
21 #define UART_ID_0 0
22 #define UART_ID_1 1
23 
24 #define NUMBER_UARTS 2
25 
26 #define SERIAL_SIGNAL_DELEGATE 0
27 #define SERIAL_SIGNAL_COMMAND 1
28 #define SERIAL_QUEUE_LEN 10
29 
30 
35 // Delegate constructor usage: (&YourClass::method, this)
37 
38 class CommandExecutor;
39 
41 typedef struct
42 {
44  CommandExecutor* commandExecutor = nullptr;
46 
47 enum SerialConfig {
48  SERIAL_5N1 = UART_5N1,
49  SERIAL_6N1 = UART_6N1,
50  SERIAL_7N1 = UART_7N1,
51  SERIAL_8N1 = UART_8N1,
52  SERIAL_5N2 = UART_5N2,
53  SERIAL_6N2 = UART_6N2,
54  SERIAL_7N2 = UART_7N2,
55  SERIAL_8N2 = UART_8N2,
56  SERIAL_5E1 = UART_5E1,
57  SERIAL_6E1 = UART_6E1,
58  SERIAL_7E1 = UART_7E1,
59  SERIAL_8E1 = UART_8E1,
60  SERIAL_5E2 = UART_5E2,
61  SERIAL_6E2 = UART_6E2,
62  SERIAL_7E2 = UART_7E2,
63  SERIAL_8E2 = UART_8E2,
64  SERIAL_5O1 = UART_5O1,
65  SERIAL_6O1 = UART_6O1,
66  SERIAL_7O1 = UART_7O1,
67  SERIAL_8O1 = UART_8O1,
68  SERIAL_5O2 = UART_5O2,
69  SERIAL_6O2 = UART_6O2,
70  SERIAL_7O2 = UART_7O2,
71  SERIAL_8O2 = UART_8O2,
72 };
73 
74 enum SerialMode {
75  SERIAL_FULL = UART_FULL,
76  SERIAL_RX_ONLY = UART_RX_ONLY,
77  SERIAL_TX_ONLY = UART_TX_ONLY
78 };
79 
81 class HardwareSerial : public Stream
82 {
83 public:
88  HardwareSerial(const int uartPort);
89  ~HardwareSerial();
90 
94  void begin(const uint32_t baud = 9600) {
95  begin(baud, SERIAL_8N1, SERIAL_FULL, 1);
96  }
97 
105  void begin(const uint32_t baud, SerialConfig config)
106  {
107  begin(baud, config, SERIAL_FULL, 1);
108  }
109 
118  void begin(const uint32_t baud, SerialConfig config, SerialMode mode)
119  {
120  begin(baud, config, mode, 1);
121  }
122 
123  void begin(const uint32_t baud, SerialConfig config, SerialMode mode, uint8_t txPin);
124 
125  /*
126  * @brief De-inits the current UART if it is already used
127  */
128  void end();
129 
130  /*
131  * @brief Sets receiving buffer size
132  * @param size_t requested size
133  * @retval size_t actual size
134  */
135  size_t setRxBufferSize(size_t size);
136 
137  /*
138  * @brief Toggle between use of GPIO13/GPIO15 or GPIO3/GPIO(1/2) as RX and TX
139  * @note UART0 uses pins GPIO1 (TX) and GPIO3 (RX). It may be swapped to GPIO15 (TX) and GPIO13 (RX) by calling .swap() after .begin. C
140  * @note Calling swap again maps UART0 back to GPIO1 and GPIO3.
141  */
142  void swap()
143  {
144  swap(1);
145  }
146 
147  /*
148  * @brief Toggle between use of GPIO13/GPIO15 or GPIO3/GPIO(1/2) as RX and TX
149  * @param uint8_t Pin number.
150  */
151  void swap(uint8_t tx_pin);
152 
153  /*
154  * @brief Toggle between use of GPIO1 and GPIO2 as TX on UART 0.
155  * @note: UART 1 can't be used if GPIO2 is used with UART 0!
156  * @note: If UART1 is not used and UART0 is not swapped - TX for UART0 can be mapped to GPIO2 by calling .setTx(2) after
157  * .begin or directly with .begin(baud, config, mode, 2).
158  *
159  */
160  void setTx(uint8_t tx_pin);
161 
162  /*
163  * @brief Sets the transmission and receiving PINS
164  * @param uint8_t tx Transmission pin number
165  * @param uint8_t rx Receiving pin number
166  * @note UART 0 possible options are (1, 3), (2, 3) or (15, 13)
167  * @note UART 1 allows only TX on 2 if UART 0 is not (2, 3)
168  */
169  void pins(uint8_t tx, uint8_t rx);
170 
171 
175  int available();
176 
181  int read();
182 
188  int readMemoryBlock(char* buf, int max_len);
189 
194  int peek();
195 
199  void flush();
200 
205  size_t write(uint8_t oneChar);
206 
207  using Stream::write;
208 
213  void systemDebugOutput(bool enabled);
214 
220  void commandProcessing(bool reqEnable);
221 
226  bool setCallback(StreamDataReceivedDelegate reqCallback);
227 
230  void resetCallback();
231 
236  bool isTxEnabled(void);
237 
242  bool isRxEnabled(void);
243 
248  int baudRate(void);
249 
253  operator bool() const;
254 
255 private:
256  int uartNr;
257  static HWSerialMemberData memberData[NUMBER_UARTS];
258 
259  uart_t* uart = nullptr;
260  size_t rxSize;
261 
262  static os_event_t *serialQueue;
263 
264  static bool init;
265 
269  static void IRAM_ATTR callbackHandler(uart_t *arg);
270 
274  static void delegateTask (os_event_t *inputEvent);
275 };
276 
286 extern HardwareSerial Serial;
287 
289 #endif /* _HARDWARESERIAL_H_ */
void begin(const uint32_t baud, SerialConfig config)
Initialise and set its configuration.
Definition: HardwareSerial.h:105
int available()
Get quantity characters available from serial input.
StreamDataReceivedDelegate HWSDelegate
Delegate callback handler.
Definition: HardwareSerial.h:43
int readMemoryBlock(char *buf, int max_len)
Read a block of characters from serial port.
bool setCallback(StreamDataReceivedDelegate reqCallback)
Set handler for received data.
Definition: CommandExecutor.h:18
void begin(const uint32_t baud=9600)
Initialise the serial port.
Definition: HardwareSerial.h:94
bool isTxEnabled(void)
Checks if the current UART can transmit(print) information.
virtual size_t write(uint8_t)=0
Writes a single character to output stream.
int read()
Read a character from serial port.
bool isRxEnabled(void)
Checks if the current UART can receive information.
void commandProcessing(bool reqEnable)
Configure serial port for command processing.
void begin(const uint32_t baud, SerialConfig config, SerialMode mode)
Initialise, set its configuration and mode.
Definition: HardwareSerial.h:118
Hardware serial class.
Definition: HardwareSerial.h:81
void systemDebugOutput(bool enabled)
Configure serial port for system debug output and redirect output from debugf.
void resetCallback()
Remove handler for received data.
HardwareSerial Serial
Global instance of serial port UART0.
Hardware serial member data.
Definition: HardwareSerial.h:41
HardwareSerial(const int uartPort)
Create instance of a hardware serial port object.
void flush()
Clear the serial port receive buffer.
int peek()
Read a character from serial port without removing from input buffer.
Delegate< void(Stream &source, char arrivedChar, uint16_t availableCharsCount)> StreamDataReceivedDelegate
Delegate callback type for serial data reception.
Definition: HardwareSerial.h:36
int baudRate(void)
Get the current baud rate.
#define NUMBER_UARTS
Quantity of UARTs available.
Definition: HardwareSerial.h:24
size_t write(uint8_t oneChar)
write a character to serial port
Definition: Stream.h:30