Soldered 125kHz RFID board Arduino library 1.0.0
Library for Soldered 125kHz RFID board.
Loading...
Searching...
No Matches
ESPSoftwareSerial.h
Go to the documentation of this file.
1/*
2SoftwareSerial.h
3
4SoftwareSerial.cpp - Implementation of the Arduino software serial for ESP8266/ESP32.
5Copyright (c) 2015-2016 Peter Lerup. All rights reserved.
6Copyright (c) 2018-2019 Dirk O. Kaar. All rights reserved.
7
8This library is free software; you can redistribute it and/or
9modify it under the terms of the GNU Lesser General Public
10License as published by the Free Software Foundation; either
11version 2.1 of the License, or (at your option) any later version.
12
13This library is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16Lesser General Public License for more details.
17
18You should have received a copy of the GNU Lesser General Public
19License along with this library; if not, write to the Free Software
20Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21
22*/
23
24#ifndef __SoftwareSerial_h
25#define __SoftwareSerial_h
26
27#if defined(ARDUINO_ESP32_DEV)
28
30#include <Stream.h>
31
39
82
89class SoftwareSerial : public Stream {
90public:
91 SoftwareSerial();
95 SoftwareSerial(int8_t rxPin, int8_t txPin = -1, bool invert = false);
96 SoftwareSerial(const SoftwareSerial&) = delete;
97 SoftwareSerial& operator= (const SoftwareSerial&) = delete;
98 virtual ~SoftwareSerial();
109 void begin(uint32_t baud, SoftwareSerialConfig config,
110 int8_t rxPin, int8_t txPin, bool invert,
111 int bufCapacity = 64, int isrBufCapacity = 0);
112 void begin(uint32_t baud, SoftwareSerialConfig config,
113 int8_t rxPin, int8_t txPin) {
114 begin(baud, config, rxPin, txPin, m_invert);
115 }
116 void begin(uint32_t baud, SoftwareSerialConfig config,
117 int8_t rxPin) {
118 begin(baud, config, rxPin, m_txPin, m_invert);
119 }
120 void begin(uint32_t baud, SoftwareSerialConfig config = SWSERIAL_8N1) {
121 begin(baud, config, m_rxPin, m_txPin, m_invert);
122 }
123
124 uint32_t baudRate();
126 void setTransmitEnablePin(int8_t txEnablePin);
128 void enableIntTx(bool on);
130 void enableRxGPIOPullup(bool on);
131
132 bool overflow();
133
134 int available() override;
135#if defined(ESP8266)
136 int availableForWrite() override {
137#else
138 int availableForWrite() {
139#endif
140 if (!m_txValid) return 0;
141 return 1;
142 }
143 int peek() override;
144 int read() override;
146 bool readParity()
147 {
148 return m_lastReadParity;
149 }
151 static bool parityEven(uint8_t byte) {
152 byte ^= byte >> 4;
153 byte &= 0xf;
154 return (0x6996 >> byte) & 1;
155 }
157 static bool parityOdd(uint8_t byte) {
158 byte ^= byte >> 4;
159 byte &= 0xf;
160 return (0x9669 >> byte) & 1;
161 }
163 int read(uint8_t* buffer, size_t size)
164#if defined(ESP8266)
165 override
166#endif
167 ;
169 int read(char* buffer, size_t size) {
170 return read(reinterpret_cast<uint8_t*>(buffer), size);
171 }
174 size_t readBytes(uint8_t* buffer, size_t size) override;
177 size_t readBytes(char* buffer, size_t size) override {
178 return readBytes(reinterpret_cast<uint8_t*>(buffer), size);
179 }
180 void flush() override;
181 size_t write(uint8_t byte) override;
182 size_t write(uint8_t byte, SoftwareSerialParity parity);
183 size_t write(const uint8_t* buffer, size_t size) override;
184 size_t write(const char* buffer, size_t size) {
185 return write(reinterpret_cast<const uint8_t*>(buffer), size);
186 }
187 size_t write(const uint8_t* buffer, size_t size, SoftwareSerialParity parity);
188 size_t write(const char* buffer, size_t size, SoftwareSerialParity parity) {
189 return write(reinterpret_cast<const uint8_t*>(buffer), size, parity);
190 }
191 operator bool() const {
192 return (-1 == m_rxPin || m_rxValid) && (-1 == m_txPin || m_txValid) && !(-1 == m_rxPin && m_oneWire);
193 }
194
196 void enableRx(bool on);
198 void enableTx(bool on);
199
200 // AVR compatibility methods.
201 bool listen() { enableRx(true); return true; }
202 void end();
203 bool isListening() { return m_rxEnabled; }
204 bool stopListening() { enableRx(false); return true; }
205
207 void onReceive(Delegate<void(int available), void*> handler);
208
211 void perform_work();
212
213 using Print::write;
214
215private:
216 // It's legal to exceed the deadline, for instance,
217 // by enabling interrupts.
218 void lazyDelay();
219 // Synchronous precise delay
220 void preciseDelay();
221 // If withStopBit is set, either cycle contains a stop bit.
222 // If dutyCycle == 0, the level is not forced to HIGH.
223 // If offCycle == 0, the level remains unchanged from dutyCycle.
224 void writePeriod(
225 uint32_t dutyCycle, uint32_t offCycle, bool withStopBit);
226 bool isValidGPIOpin(int8_t pin);
227 bool isValidRxGPIOpin(int8_t pin);
228 bool isValidTxGPIOpin(int8_t pin);
229 // result is only defined for a valid Rx GPIO pin
230 bool hasRxGPIOPullUp(int8_t pin);
231 // safely set the pin mode for the Rx GPIO pin
232 void setRxGPIOPullUp();
233 /* check m_rxValid that calling is safe */
234 void rxBits();
235 void rxBits(const uint32_t isrCycle);
236 static void disableInterrupts();
237 static void restoreInterrupts();
238
239 static void rxBitISR(SoftwareSerial* self);
240 static void rxBitSyncISR(SoftwareSerial* self);
241
242 // Member variables
243 int8_t m_rxPin = -1;
244 volatile uint32_t* m_rxReg;
245 uint32_t m_rxBitMask;
246 int8_t m_txPin = -1;
247#if !defined(ESP8266)
248 volatile uint32_t* m_txReg;
249#endif
250 uint32_t m_txBitMask;
251 int8_t m_txEnablePin = -1;
252 uint8_t m_dataBits;
253 bool m_oneWire;
254 bool m_rxValid = false;
255 bool m_rxEnabled = false;
256 bool m_txValid = false;
257 bool m_txEnableValid = false;
258 bool m_invert;
260 uint8_t m_pduBits;
261 bool m_intTxEnabled;
262 bool m_rxGPIOPullupEnabled;
263 SoftwareSerialParity m_parityMode;
264 uint8_t m_stopBits;
265 bool m_lastReadParity;
266 bool m_overflow = false;
267 uint32_t m_bitCycles;
268 uint8_t m_parityInPos;
269 uint8_t m_parityOutPos;
270 int8_t m_rxLastBit; // 0 thru (m_pduBits - m_stopBits - 1): data/parity bits. -1: start bit. (m_pduBits - 1): stop bit.
271 uint8_t m_rxCurByte = 0;
274 uint32_t m_periodStart;
275 uint32_t m_periodDuration;
276#ifndef ESP32
277 static uint32_t m_savedPS;
278#else
279 static portMUX_TYPE m_interruptsMux;
280#endif
281 // the ISR stores the relative bit times in the buffer. The inversion corrected level is used as sign bit (2's complement):
282 // 1 = positive including 0, 0 = negative.
284 const Delegate<void(uint32_t&&), SoftwareSerial*> m_isrBufferForEachDel = { [](SoftwareSerial* self, uint32_t&& isrCycle) { self->rxBits(isrCycle); }, this };
285 std::atomic<bool> m_isrOverflow;
286 uint32_t m_isrLastCycle;
287 bool m_rxCurParity = false;
288 Delegate<void(int available), void*> receiveHandler;
289};
290
291#endif
292
293#endif // __SoftwareSerial_h
SoftwareSerialParity
Definition ESPSoftwareSerial.h:32
@ SWSERIAL_PARITY_NONE
Definition ESPSoftwareSerial.h:33
@ SWSERIAL_PARITY_EVEN
Definition ESPSoftwareSerial.h:34
@ SWSERIAL_PARITY_SPACE
Definition ESPSoftwareSerial.h:37
@ SWSERIAL_PARITY_MARK
Definition ESPSoftwareSerial.h:36
@ SWSERIAL_PARITY_ODD
Definition ESPSoftwareSerial.h:35
SoftwareSerialConfig
Definition ESPSoftwareSerial.h:40
@ SWSERIAL_6O2
Definition ESPSoftwareSerial.h:70
@ SWSERIAL_8E2
Definition ESPSoftwareSerial.h:68
@ SWSERIAL_7S1
Definition ESPSoftwareSerial.h:59
@ SWSERIAL_6S2
Definition ESPSoftwareSerial.h:78
@ SWSERIAL_5M1
Definition ESPSoftwareSerial.h:53
@ SWSERIAL_5E1
Definition ESPSoftwareSerial.h:45
@ SWSERIAL_7O1
Definition ESPSoftwareSerial.h:51
@ SWSERIAL_6N1
Definition ESPSoftwareSerial.h:42
@ SWSERIAL_6N2
Definition ESPSoftwareSerial.h:62
@ SWSERIAL_5O2
Definition ESPSoftwareSerial.h:69
@ SWSERIAL_6E1
Definition ESPSoftwareSerial.h:46
@ SWSERIAL_8M2
Definition ESPSoftwareSerial.h:76
@ SWSERIAL_8E1
Definition ESPSoftwareSerial.h:48
@ SWSERIAL_6S1
Definition ESPSoftwareSerial.h:58
@ SWSERIAL_8O1
Definition ESPSoftwareSerial.h:52
@ SWSERIAL_6E2
Definition ESPSoftwareSerial.h:66
@ SWSERIAL_7N2
Definition ESPSoftwareSerial.h:63
@ SWSERIAL_7O2
Definition ESPSoftwareSerial.h:71
@ SWSERIAL_8N1
Definition ESPSoftwareSerial.h:44
@ SWSERIAL_5N1
Definition ESPSoftwareSerial.h:41
@ SWSERIAL_8O2
Definition ESPSoftwareSerial.h:72
@ SWSERIAL_8M1
Definition ESPSoftwareSerial.h:56
@ SWSERIAL_5E2
Definition ESPSoftwareSerial.h:65
@ SWSERIAL_6M2
Definition ESPSoftwareSerial.h:74
@ SWSERIAL_6O1
Definition ESPSoftwareSerial.h:50
@ SWSERIAL_5S1
Definition ESPSoftwareSerial.h:57
@ SWSERIAL_7N1
Definition ESPSoftwareSerial.h:43
@ SWSERIAL_7E2
Definition ESPSoftwareSerial.h:67
@ SWSERIAL_7M2
Definition ESPSoftwareSerial.h:75
@ SWSERIAL_5O1
Definition ESPSoftwareSerial.h:49
@ SWSERIAL_6M1
Definition ESPSoftwareSerial.h:54
@ SWSERIAL_7S2
Definition ESPSoftwareSerial.h:79
@ SWSERIAL_5N2
Definition ESPSoftwareSerial.h:61
@ SWSERIAL_5S2
Definition ESPSoftwareSerial.h:77
@ SWSERIAL_8S1
Definition ESPSoftwareSerial.h:60
@ SWSERIAL_7E1
Definition ESPSoftwareSerial.h:47
@ SWSERIAL_7M1
Definition ESPSoftwareSerial.h:55
@ SWSERIAL_5M2
Definition ESPSoftwareSerial.h:73
@ SWSERIAL_8N2
Definition ESPSoftwareSerial.h:64
@ SWSERIAL_8S2
Definition ESPSoftwareSerial.h:80
Definition Delegate.h:2049
Definition ghostl.h:40
Definition ghostl.h:62