-
Notifications
You must be signed in to change notification settings - Fork 7
/
SoftwareSerial.cpp
322 lines (285 loc) · 7.98 KB
/
SoftwareSerial.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
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
313
314
315
316
317
318
319
320
321
322
/*
* SoftwareSerial.cpp (formerly NewSoftSerial.cpp)
*
* Multi-instance software serial library for Arduino/Wiring
* -- Interrupt-driven receive and other improvements by ladyada
* (http://ladyada.net)
* -- Tuning, circular buffer, derivation from class Print/Stream,
* multi-instance support, porting to 8MHz processors,
* various optimizations, PROGMEM delay tables, inverse logic and
* direct port writing by Mikal Hart (http://www.arduiniana.org)
* -- Pin change interrupt macros by Paul Stoffregen (http://www.pjrc.com)
* -- 20MHz processor support by Garrett Mace (http://www.macetech.com)
* -- ATmega1280/2560 support by Brett Hagman (http://www.roguerobotics.com/)
*
* 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
*
* The latest version of this library can always be found at
* http://arduiniana.org.
*/
//
// Includes
//
#include <stdint.h>
#include <stdarg.h>
#include <Arduino.h>
#include "SoftwareSerial.h"
#include "HAL_softserial.h"
//#define FORCE_BAUD_RATE 19200
//
// Statics
//
bool SoftwareSerial::initialised = false;
SoftwareSerial * SoftwareSerial::active_listener = NULL;
SoftwareSerial * volatile SoftwareSerial::active_out = NULL;
SoftwareSerial * volatile SoftwareSerial:: active_in = NULL;
int32_t SoftwareSerial::tx_tick_cnt = 0;
int32_t SoftwareSerial::rx_tick_cnt = 0;
uint32_t SoftwareSerial::tx_buffer = 0;
int32_t SoftwareSerial::tx_bit_cnt = 0;
uint32_t SoftwareSerial::rx_buffer = 0;
int32_t SoftwareSerial::rx_bit_cnt = -1;
uint32_t SoftwareSerial::cur_speed = 0;
//
// Private methods
//
void SoftwareSerial::setSpeed(uint32_t speed)
{
if (speed != cur_speed) {
HAL_softserial_setSpeed(speed);
cur_speed = speed;
}
}
// This function sets the current object as the "listening"
// one and returns true if it replaces another
bool SoftwareSerial::listen() {
if (_receivePin < 0) return false;
// wait for any transmit to complete as we may change speed
while(active_out) ;
if (active_listener) {
active_listener->stopListening();
}
rx_tick_cnt = 1;
rx_bit_cnt = -1;
setSpeed(_speed);
active_listener = this;
if (!_half_duplex)
active_in = this;
return true;
}
// Stop listening. Returns true if we were actually listening.
bool SoftwareSerial::stopListening() {
if (active_listener != this) return false;
// wait for any output to complete
while (active_out) ;
if (_half_duplex)
setRXTX(false);
active_listener = NULL;
active_in = NULL;
// turn off interrupts
setSpeed(0);
return true;
}
inline void SoftwareSerial::setTX() {
// First write, then set output. If we do this the other way around,
// the pin would be output low for a short while before switching to
// output hihg. Now, it is input with pullup for a short while, which
// is fine. With inverse logic, either order is fine.
gpio_set(_transmitPin, _inverse_logic ? LOW : HIGH);
pinMode(_transmitPin, OUTPUT);
}
inline void SoftwareSerial::setRX() {
if (_receivePin >= 0) {
pinMode(_receivePin, _inverse_logic ? INPUT_PULLDOWN : INPUT_PULLUP); // pullup for normal logic!
}
}
inline void SoftwareSerial::setRXTX(bool input) {
if (_half_duplex) {
if (input) {
if (active_in != this) {
setRX();
rx_bit_cnt = -1;
rx_tick_cnt = 2;
active_in = this;
}
}
else {
if (active_in == this) {
setTX();
active_in = NULL;
}
}
}
}
inline void SoftwareSerial::send() {
if (--tx_tick_cnt > 0) return;
if (tx_bit_cnt++ < 10 ) {
// send data (including start and stop bits)
gpio_set(_transmitPin, tx_buffer & 1);
tx_buffer >>= 1;
tx_tick_cnt = OVERSAMPLE;
}
else {
tx_tick_cnt = 1;
if (_output_pending)
active_out = NULL;
else if (tx_bit_cnt > 10 + OVERSAMPLE*5) {
if (_half_duplex && active_listener == this) {
pinMode(_receivePin, _inverse_logic ? INPUT_PULLDOWN : INPUT_PULLUP); // pullup for normal logic!
rx_bit_cnt = -1;
rx_tick_cnt = 2;
active_in = this;
}
active_out = NULL;
}
}
}
//
// The receive routine called by the interrupt handler
//
inline void SoftwareSerial::recv() {
if (--rx_tick_cnt > 0) return;
uint8_t inbit = gpio_get(_receivePin);
if (rx_bit_cnt == -1) {
// waiting for start bit
if (inbit)
rx_tick_cnt = 1;
else {
// got start bit
rx_bit_cnt = 0;
rx_tick_cnt = OVERSAMPLE + 1;
rx_buffer = 0;
}
}
else if (rx_bit_cnt < 8) {
// data bits
rx_buffer >>= 1;
if (inbit)
rx_buffer |= 0x80;
rx_bit_cnt++;
rx_tick_cnt = OVERSAMPLE;
}
else {
if (inbit) {
// stop bit read complete add to buffer
uint8_t next = (_receive_buffer_tail + 1) % _SS_MAX_RX_BUFF;
if (next != _receive_buffer_head) {
// save new data in buffer: tail points to where byte goes
_receive_buffer[_receive_buffer_tail] = rx_buffer; // save new byte
_receive_buffer_tail = next;
}
else
_buffer_overflow = true;
}
rx_tick_cnt = 1;
rx_bit_cnt = -1;
}
}
//
// Interrupt handling
//
/* static */
inline void SoftwareSerial::handle_interrupt() {
if (active_in) active_in->recv();
if (active_out) active_out->send();
}
HAL_SOFTSERIAL_TIMER_ISR() {
HAL_softserial_timer_isr_prologue();
SoftwareSerial::handle_interrupt();
HAL_softserial_timer_isr_epilogue();
}
//
// Constructor
//
SoftwareSerial::SoftwareSerial(int16_t receivePin, int16_t transmitPin, bool inverse_logic /* = false */) :
_receivePin(receivePin),
_transmitPin(transmitPin),
_speed(0),
_buffer_overflow(false),
_inverse_logic(inverse_logic),
_half_duplex(receivePin == transmitPin),
_output_pending(0),
_receive_buffer_tail(0),
_receive_buffer_head(0) {
}
//
// Destructor
//
SoftwareSerial::~SoftwareSerial() {
end();
}
//
// Public methods
//
void SoftwareSerial::begin(long speed) {
#ifdef FORCE_BAUD_RATE
speed = FORCE_BAUD_RATE;
#endif
_speed = speed;
if (!initialised) {
HAL_softSerial_init();
initialised = true;
}
// Set output pin as input, to ensure GPIO clock is started before calling setTX().
pinMode(_transmitPin, _inverse_logic ? INPUT_PULLDOWN : INPUT_PULLUP);
setTX();
if (!_half_duplex) {
setRX();
listen();
}
}
void SoftwareSerial::end() {
stopListening();
}
// Read data from buffer
int SoftwareSerial::read() {
// Empty buffer?
if (_receive_buffer_head == _receive_buffer_tail) return -1;
// Read from "head"
uint8_t d = _receive_buffer[_receive_buffer_head]; // grab next byte
_receive_buffer_head = (_receive_buffer_head + 1) % _SS_MAX_RX_BUFF;
return d;
}
int SoftwareSerial::available() {
return (_receive_buffer_tail + _SS_MAX_RX_BUFF - _receive_buffer_head) % _SS_MAX_RX_BUFF;
}
size_t SoftwareSerial::write(uint8_t b) {
// wait for previous transmit to complete
_output_pending = 1;
while(active_out) ;
// add start and stop bits.
tx_buffer = b << 1 | 0x200;
tx_bit_cnt = 0;
tx_tick_cnt = OVERSAMPLE;
setSpeed(_speed);
if (_half_duplex)
setRXTX(false);
_output_pending = 0;
// make us active
active_out = this;
return 1;
}
void SoftwareSerial::flush() {
cli();
_receive_buffer_head = _receive_buffer_tail = 0;
sei();
}
int SoftwareSerial::peek() {
// Empty buffer?
if (_receive_buffer_head == _receive_buffer_tail)
return -1;
// Read from "head"
return _receive_buffer[_receive_buffer_head];
}