ESP8266SoftwareSerial.cpp 9.58 KB
Newer Older
1
2
/*

Eric Duminil's avatar
Eric Duminil committed
3
ESP8266SoftwareSerial.cpp - Implementation of the Arduino software serial for ESP8266.
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Copyright (c) 2015-2016 Peter Lerup. All rights reserved.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

*/
Eric Duminil's avatar
Eric Duminil committed
21
#if defined(ESP8266)
22
23
24
25
26
27
28
29
#include <Arduino.h>

// The Arduino standard GPIO routines are not enough,
// must use some from the Espressif SDK as well
extern "C" {
#include "gpio.h"
}

Eric Duminil's avatar
Eric Duminil committed
30
#include "ESP8266SoftwareSerial.h"
31
32
33
34
35

#define MAX_PIN 15

// As the Arduino attachInterrupt has no parameter, lists of objects
// and callbacks corresponding to each possible GPIO pins have to be defined
Eric Duminil's avatar
Eric Duminil committed
36
ESP8266SoftwareSerial *ObjList[MAX_PIN + 1];
37

Eric Duminil's avatar
Eric Duminil committed
38
39
40
41
42
43
void IRAM_ATTR sws_isr_0() { ObjList[0]->rxRead(); };
void IRAM_ATTR sws_isr_1() { ObjList[1]->rxRead(); };
void IRAM_ATTR sws_isr_2() { ObjList[2]->rxRead(); };
void IRAM_ATTR sws_isr_3() { ObjList[3]->rxRead(); };
void IRAM_ATTR sws_isr_4() { ObjList[4]->rxRead(); };
void IRAM_ATTR sws_isr_5() { ObjList[5]->rxRead(); };
44
// Pin 6 to 11 can not be used
Eric Duminil's avatar
Eric Duminil committed
45
46
47
48
void IRAM_ATTR sws_isr_12() { ObjList[12]->rxRead(); };
void IRAM_ATTR sws_isr_13() { ObjList[13]->rxRead(); };
void IRAM_ATTR sws_isr_14() { ObjList[14]->rxRead(); };
void IRAM_ATTR sws_isr_15() { ObjList[15]->rxRead(); };
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68

static void(*ISRList[MAX_PIN + 1])() = {
	  sws_isr_0,
	  sws_isr_1,
	  sws_isr_2,
	  sws_isr_3,
	  sws_isr_4,
	  sws_isr_5,
	  NULL,
	  NULL,
	  NULL,
	  NULL,
	  NULL,
	  NULL,
	  sws_isr_12,
	  sws_isr_13,
	  sws_isr_14,
	  sws_isr_15
};

Eric Duminil's avatar
Eric Duminil committed
69
ESP8266SoftwareSerial::ESP8266SoftwareSerial(int receivePin, int transmitPin, bool inverse_logic, unsigned int buffSize, bool edge_triggered) {
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
	m_oneWire = (receivePin == transmitPin);
	m_rxValid = m_txValid = m_txEnableValid = false;
	m_buffer = NULL;
	m_invert = inverse_logic;
	m_edge = edge_triggered;
	m_overflow = false;
	m_rxEnabled = false;
	if (isValidGPIOpin(receivePin)) {
		m_rxPin = receivePin;
		m_buffSize = buffSize;
		m_buffer = (uint8_t*)malloc(m_buffSize);
		if (m_buffer != NULL) {
			m_rxValid = true;
			m_inPos = m_outPos = 0;
			pinMode(m_rxPin, INPUT);
			ObjList[m_rxPin] = this;
		}
	}
	if (isValidGPIOpin(transmitPin) || (!m_oneWire && (transmitPin == 16))) {
		m_txValid = true;
		m_txPin = transmitPin;
		if (!m_oneWire) {
			pinMode(m_txPin, OUTPUT);
			digitalWrite(m_txPin, !m_invert);
		}
	}
	// Default speed
	begin(9600);
}

Eric Duminil's avatar
Eric Duminil committed
100
ESP8266SoftwareSerial::~ESP8266SoftwareSerial() {
101
102
103
104
105
106
107
	enableRx(false);
	if (m_rxValid)
		ObjList[m_rxPin] = NULL;
	if (m_buffer)
		free(m_buffer);
}

Eric Duminil's avatar
Eric Duminil committed
108
bool ESP8266SoftwareSerial::isValidGPIOpin(int pin) {
109
110
111
	return (pin >= 0 && pin <= 5) || (pin >= 12 && pin <= MAX_PIN);
}

Eric Duminil's avatar
Eric Duminil committed
112
void ESP8266SoftwareSerial::begin(long speed) {
113
114
115
116
117
118
119
120
121
	// Use getCycleCount() loop to get as exact timing as possible
	m_bitTime = F_CPU / speed;
	// By default enable interrupt during tx only for low speed
	m_intTxEnabled = speed < 9600;

	if (!m_rxEnabled)
		enableRx(true);
}

Eric Duminil's avatar
Eric Duminil committed
122
long ESP8266SoftwareSerial::baudRate() {
123
124
125
	return F_CPU / m_bitTime;
}

Eric Duminil's avatar
Eric Duminil committed
126
void ESP8266SoftwareSerial::setTransmitEnablePin(int transmitEnablePin) {
127
128
129
130
131
132
133
134
135
136
137
	if (isValidGPIOpin(transmitEnablePin)) {
		m_txEnableValid = true;
		m_txEnablePin = transmitEnablePin;
		pinMode(m_txEnablePin, OUTPUT);
		digitalWrite(m_txEnablePin, LOW);
	}
	else {
		m_txEnableValid = false;
	}
}

Eric Duminil's avatar
Eric Duminil committed
138
void ESP8266SoftwareSerial::enableIntTx(bool on) {
139
140
141
	m_intTxEnabled = on;
}

Eric Duminil's avatar
Eric Duminil committed
142
void ESP8266SoftwareSerial::enableTx(bool on) {
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
	if (m_oneWire && m_txValid) {
		if (on) {
			enableRx(false);
			digitalWrite(m_txPin, !m_invert);
			pinMode(m_rxPin, OUTPUT);
		}
		else {
			digitalWrite(m_txPin, !m_invert);
			pinMode(m_rxPin, INPUT);
			enableRx(true);
		}
		delay(1); // it's important to have a delay after switching
	}
}

Eric Duminil's avatar
Eric Duminil committed
158
void ESP8266SoftwareSerial::enableRx(bool on) {
159
160
161
162
163
164
165
166
167
168
169
170
171
	if (m_rxValid) {
		if (on) {
			if (m_edge)
				attachInterrupt(m_rxPin, ISRList[m_rxPin], CHANGE); // fire at rising and falling edges
			else
				attachInterrupt(m_rxPin, ISRList[m_rxPin], m_invert ? RISING : FALLING);
		}
		else
			detachInterrupt(m_rxPin);
		m_rxEnabled = on;
	}
}

Eric Duminil's avatar
Eric Duminil committed
172
int ESP8266SoftwareSerial::read() {
173
174
175
176
177
178
	if (!m_rxValid || (m_inPos == m_outPos)) return -1;
	uint8_t ch = m_buffer[m_outPos];
	m_outPos = (m_outPos + 1) % m_buffSize;
	return ch;
}

Eric Duminil's avatar
Eric Duminil committed
179
int ESP8266SoftwareSerial::available() {
180
181
182
183
184
185
186
187
	if (!m_rxValid) return 0;
	int avail = m_inPos - m_outPos;
	if (avail < 0) avail += m_buffSize;
	return avail;
}

#define WAIT { while (ESP.getCycleCount()-start < wait) if (m_intTxEnabled) optimistic_yield(1); wait += m_bitTime; }

Eric Duminil's avatar
Eric Duminil committed
188
size_t ESP8266SoftwareSerial::write(uint8_t b) {
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
	if (!m_txValid) return 0;

	if (m_invert) b = ~b;
	if (!m_intTxEnabled)
		// Disable interrupts in order to get a clean transmit
		cli();
	if (m_txEnableValid) digitalWrite(m_txEnablePin, HIGH);
	unsigned long wait = m_bitTime;
	digitalWrite(m_txPin, HIGH);
	unsigned long start = ESP.getCycleCount();
	// Start bit;
	digitalWrite(m_txPin, LOW);
	WAIT;
	for (int i = 0; i < 8; i++) {
		digitalWrite(m_txPin, (b & 1) ? HIGH : LOW);
		WAIT;
		b >>= 1;
	}
	// Stop bit
	digitalWrite(m_txPin, HIGH);
	WAIT;
	if (m_txEnableValid) digitalWrite(m_txEnablePin, LOW);
	if (!m_intTxEnabled)
		sei();
	return 1;
}

Eric Duminil's avatar
Eric Duminil committed
216
void ESP8266SoftwareSerial::flush() {
217
218
219
	m_inPos = m_outPos = 0;
}

Eric Duminil's avatar
Eric Duminil committed
220
bool ESP8266SoftwareSerial::overflow() {
221
222
223
224
225
	bool res = m_overflow;
	m_overflow = false;
	return res;
}

Eric Duminil's avatar
Eric Duminil committed
226
int ESP8266SoftwareSerial::peek() {
227
228
229
230
	if (!m_rxValid || (m_inPos == m_outPos)) return -1;
	return m_buffer[m_outPos];
}

Eric Duminil's avatar
Eric Duminil committed
231
inline bool ESP8266SoftwareSerial::propgateBits(bool level, int pulseBitLength)
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
{
	for (int i = 0; i < pulseBitLength; i++)
	{
		m_rec >>= 1;
		if (level)
			m_rec |= 0x80;
		m_bitNo++;
		if (m_bitNo == 8)
		{
			return true;
		}
	}

	return false;
}

Eric Duminil's avatar
Eric Duminil committed
248
inline void ESP8266SoftwareSerial::setWaitingForStart()
249
250
251
252
{
	m_getByteState = awaitingStart;
}

Eric Duminil's avatar
Eric Duminil committed
253
inline void ESP8266SoftwareSerial::setStartBit(unsigned long start)
254
255
256
257
258
259
260
261
262
263
{
	// mark - the start of a pulse
	// set the timers and wait for the next pulse
	m_byteStart = start;
	m_pulseStart = start;
	m_rec = 0;
	m_bitNo = 0;
	m_getByteState = gotStart;
}

Eric Duminil's avatar
Eric Duminil committed
264
void ICACHE_RAM_ATTR ESP8266SoftwareSerial::rxRead() {
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
	// Advance the starting point for the samples but compensate for the
	// initial delay which occurs before the interrupt is delivered
	unsigned long wait = m_bitTime + m_bitTime / 3 - 500;
	unsigned long start = ESP.getCycleCount();
	if (m_edge) {

		// Get the rxLevel and convert for inverted input
		bool rxLevel;

		if (m_invert)
			rxLevel = !digitalRead(m_rxPin);
		else
			rxLevel = digitalRead(m_rxPin);

		unsigned long byteTime;
		float byteBitLengthFloat;
		int byteBitLength;

		unsigned long pulseTime;
		float pulseBitLengthFloat;
		int pulseBitLength;

		bool previousBit = !rxLevel;

		switch (m_getByteState)
		{

		case awaitingStart:
			if (!rxLevel)
			{
				setStartBit(start);
			}
			break;

		case gotStart:

			byteTime = start - m_byteStart;
			byteBitLengthFloat = (float)byteTime / m_bitTime;
			byteBitLength = (int)(byteBitLengthFloat + 0.5);

			pulseTime = start - m_pulseStart;
			pulseBitLengthFloat = (float)pulseTime / m_bitTime;
			pulseBitLength = (int)(pulseBitLengthFloat + 0.5);

			// don't want to add the start bit into the value
			if (propgateBits(previousBit, pulseBitLength - 1))
			{
				// store byte in buffer
Eric Duminil's avatar
Eric Duminil committed
313
				unsigned int next = (m_inPos + 1) % m_buffSize;
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
				if (next != m_outPos) {
					m_buffer[m_inPos] = m_rec;
					m_inPos = next;
				}
				else {
					m_overflow = true;
				}
				setWaitingForStart();
				return;
			}
			m_pulseStart = start;
			m_getByteState = readingBits;
			break;

		case readingBits:

			byteTime = start - m_byteStart;
			byteBitLengthFloat = (float)byteTime / m_bitTime;
			byteBitLength = (int)(byteBitLengthFloat + 0.5);
			pulseTime = start - m_pulseStart;
			pulseBitLengthFloat = (float)pulseTime / m_bitTime;
			pulseBitLength = (int)(pulseBitLengthFloat + 0.5);

			if (byteBitLength > 9)
			{
				// fill in the last bits with high values
				// because this is the start bit of the next byte
				propgateBits(true, 8);

				// store the byte in the buffer
Eric Duminil's avatar
Eric Duminil committed
344
				unsigned int next = (m_inPos + 1) % m_buffSize;
345
346
347
348
349
350
351
352
353
354
355
356
357
358
				if (next != m_outPos) {
					m_buffer[m_inPos] = m_rec;
					m_inPos = next;
				}
				else {
					m_overflow = true;
				}
				setStartBit(start);
			}
			else
			{
				if (propgateBits(previousBit, pulseBitLength))
				{
					// Store the received value in the buffer unless we have an overflow
Eric Duminil's avatar
Eric Duminil committed
359
					unsigned int next = (m_inPos + 1) % m_buffSize;
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
					if (next != m_outPos) {
						m_buffer[m_inPos] = m_rec;
						m_inPos = next;
					}
					else {
						m_overflow = true;
					}
					setWaitingForStart();
				}
			}
			m_pulseStart = start;
			break;
		}
	}
	else
	{
		uint8_t rec = 0;
		for (int i = 0; i < 8; i++) {
			WAIT;
			rec >>= 1;
			if (digitalRead(m_rxPin))
				rec |= 0x80;
		}
		if (m_invert) rec = ~rec;
		// Stop bit
		WAIT;
		// Store the received value in the buffer unless we have an overflow
Eric Duminil's avatar
Eric Duminil committed
387
		unsigned int next = (m_inPos + 1) % m_buffSize;
388
389
390
391
392
393
394
395
396
397
		if (next != m_inPos) {
			m_buffer[m_inPos] = rec;
			m_inPos = next;
		}
	}

	// Must clear this bit in the interrupt register,
	// it gets set even when interrupts are disabled
	GPIO_REG_WRITE(GPIO_STATUS_W1TC_ADDRESS, 1 << m_rxPin);
}
Eric Duminil's avatar
Eric Duminil committed
398
#endif