forked from PaulStoffregen/AltSoftSerial
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAltSoftSerial.cpp
More file actions
475 lines (400 loc) · 13.5 KB
/
AltSoftSerial.cpp
File metadata and controls
475 lines (400 loc) · 13.5 KB
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/* An Alternative Software Serial Library
* http://www.pjrc.com/teensy/td_libs_AltSoftSerial.html
* Copyright (c) 2014 PJRC.COM, LLC, Paul Stoffregen, paul@pjrc.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
// Revisions are now tracked on GitHub
// https://github.com/PaulStoffregen/AltSoftSerial
//
// Version 1.2: Support Teensy 3.x
//
// Version 1.1: Improve performance in receiver code
//
// Version 1.0: Initial Release
#include "AltSoftSerial.h"
/****************************************/
/** Initialization **/
/****************************************/
bool AltSoftSerial::timing_error=false;
#ifndef INPUT_PULLUP
#define INPUT_PULLUP INPUT
#endif
#define MAX_COUNTS_PER_BIT 6241 // 65536 / 10.5
AltSoftSerial::AltSoftSerial(uint8_t rx_pin, uint8_t tx_pin) { // tx pin
_input_capture_pin = rx_pin;
_output_compare_A_pin = tx_pin;
// Check if it is external interrupt
if(digitalPinToInterrupt(rx_pin) != NOT_AN_INTERRUPT) {
_rxPinType = RX_PIN_TYPE_INT;
setINTPinAsRx(rx_pin);
}
else if (digitalPinToPCMSK(rx_pin) != 0) { // check if it is PCINT
_rxPinType = RX_PIN_TYPE_PCINT;
setPCINTPinAsRx(rx_pin);
}
else { // if it ICP Pin // TODO: ICP pin can be PCINT pin also. So, do this test before PCINT
_rxPinType = RX_PIN_TYPE_ICP;
}
// Timer 0 is used by millis(). So, not using it for AltSoftSerial.
uint8_t timer = digitalPinToTimer(tx_pin);
#if defined(ALTSS_HAVE_TIMER1)
_useAForTx = (timer == TIMER1A);
if(timer == TIMER1A || timer == TIMER1B) {
_TCCRnA = &TCCR1A; _COMnA1 = COM1A1; _COMnA0 = COM1A0; _COMnB1 = COM1B1; _COMnB0 = COM1B0;
_TCCRnB = &TCCR1B; _ICNCn = ICNC1; _CSn0 = CS10; _CSn1 = CS11; _CSn2 = CS12; _ICESn = ICES1;
_TIFRn = &TIFR1; _ICFn = ICF1; _OCFnA = OCF1A; _OCFnB = OCF1B;
_TIMSKn = &TIMSK1; _ICIEn = ICIE1; _OCIEnA = OCIE1A; _OCIEnB = OCIE1B;
_TCNTn = &TCNT1;
_ICRn = &ICR1;
_OCRnA = &OCR1A;
_OCRnB = &OCR1B;
}
#endif
}
/* Sets INTx pin as rx pin. This function should be called before call to begin().
* rx_pin: [IN] Arduino Pin to be set as rx
* INT_pin_number: [IN] 0 for INT0 and 1 for INT1 */
void AltSoftSerial::setINTPinAsRx(uint8_t rx_pin) {
uint8_t INT_pin_number = digitalPinToInterrupt(rx_pin);
if(INT_pin_number == 0) {
_INTm = INT0;
_ISCm0 = ISC00;
_ISCm1 = ISC01;
}
else if (INT_pin_number == 1) {
_INTm = INT1;
_ISCm0 = ISC10;
_ISCm1 = ISC11;
}
else
return;
_input_capture_pin = rx_pin;
_rxPinType = RX_PIN_TYPE_INT;
}
/* Sets PCINTxx pin as rx pin. This function should be called before call to begin().
* rx_pin: [IN] Arduino pin to set as rx
* interrupt_number: [IN] Valid range is 0 to 23 corresponding to PCINT0 to PCINT23 (except
* for PCINT15) */
void AltSoftSerial::setPCINTPinAsRx(uint8_t rx_pin) {
_PCMSKx = digitalPinToPCMSK(rx_pin);
_PCINTm = digitalPinToPCMSKbit(rx_pin);
_PCIEx = digitalPinToPCICRbit(rx_pin);
_input_capture_pin = rx_pin;
_rxPinType = RX_PIN_TYPE_PCINT;
}
void AltSoftSerial::init(uint32_t cycles_per_bit)
{
rx_bit = 0;
rx_stop_ticks = 0;
tx_state = 0;
//Serial.printf("cycles_per_bit = %d\n", cycles_per_bit);
if (cycles_per_bit < MAX_COUNTS_PER_BIT) {
CONFIG_TIMER_NOPRESCALE();
} else {
cycles_per_bit /= 8;
//Serial.printf("cycles_per_bit/8 = %d\n", cycles_per_bit);
if (cycles_per_bit < MAX_COUNTS_PER_BIT) {
CONFIG_TIMER_PRESCALE_8();
} else {
#if defined(CONFIG_TIMER_PRESCALE_256)
cycles_per_bit /= 32;
//Serial.printf("cycles_per_bit/256 = %d\n", cycles_per_bit);
if (cycles_per_bit < MAX_COUNTS_PER_BIT) {
CONFIG_TIMER_PRESCALE_256();
} else {
return; // baud rate too low for AltSoftSerial
}
#elif defined(CONFIG_TIMER_PRESCALE_128)
cycles_per_bit /= 16;
//Serial.printf("cycles_per_bit/128 = %d\n", cycles_per_bit);
if (cycles_per_bit < MAX_COUNTS_PER_BIT) {
CONFIG_TIMER_PRESCALE_128();
} else {
return; // baud rate too low for AltSoftSerial
}
#else
return; // baud rate too low for AltSoftSerial
#endif
}
}
ticks_per_bit = cycles_per_bit;
rx_stop_ticks = cycles_per_bit * 37 / 4; // 9 bit time (time from start of start bit to start of stop bit)
pinMode(_input_capture_pin, INPUT_PULLUP);
digitalWrite(_output_compare_A_pin, HIGH);
pinMode(_output_compare_A_pin, OUTPUT);
rx_state = 0;
rx_buffer_head = 0;
rx_buffer_tail = 0;
tx_state = 0;
tx_buffer_head = 0;
tx_buffer_tail = 0;
CONFIG_CAPTURE_FALLING_EDGE();
ENABLE_INT_INPUT_CAPTURE();
}
void AltSoftSerial::end(void)
{
DISABLE_INT_COMPARE_RX();
DISABLE_INT_INPUT_CAPTURE();
flushInput();
flushOutput();
DISABLE_INT_COMPARE_TX();
// TODO: restore timer to original settings?
}
/****************************************/
/** Transmission **/
/****************************************/
void AltSoftSerial::writeByte(uint8_t b)
{
/* New data is entered at head, old data is transmitted from tail side
* tx_buffer_head points to position, where there is valid data */
uint8_t intr_state, head;
head = tx_buffer_head + 1;
if (head >= TX_BUFFER_SIZE) head = 0;
/* Let all the previous data get transmitted */
while (tx_buffer_tail == head) ; // wait until space in buffer
/* Critical section
* tx_state, tx_byte, tx_bit are modified in compareAInterrupt_isr() */
intr_state = SREG;
cli();
if (tx_state) { // some byte is already being transmitted
tx_buffer[head] = b;
tx_buffer_head = head;
} else {
tx_state = 1;
tx_byte = b;
tx_bit = 0; // Set next bit to be transmitted as low (start bit)
CONFIG_MATCH_CLEAR();
SET_COMPARE_TX(GET_TIMER_COUNT() + 16);
ENABLE_INT_COMPARE_TX();
}
SREG = intr_state;
}
/* The interrupts have priority in accordance with their Interrupt Vector position.
The lower the Interrupt Vector address, the higher the priority.
The priority of interrupts is: capture interrupt (highest priority), compare A interrupt,
capture B interrupt (lowest priority) */
/* Called when timer matches the value in register OCRA. This happens at 3 occassions:
1. Start of first data bit
2. Toggling of tx pin (except for start of first data bit)
3. End of 2nd stop bit */
void AltSoftSerial::compareInterrupt_tx_isr()
{
uint8_t state, byte, bit, head, tail;
uint16_t target; // value of timer when tx pin has to be toggled
/* Byte format is 1 start bit, 8 data bit, no parity bit, 2 stop bits */
state = tx_state; // the number of bits already transmitted
byte = tx_byte; // the byte to be transmitted
target = GET_COMPARE_TX(); // note down the timer value when this isr was invoked
/* 1. Find the next bit (data bit or stop bit) that is different from current bit.
2. Get the timer value when the tx pin has to be toggled.
3. Set the timer to generate compare A interrupt when toggle has to take place.
4. Set the timer to toggle tx pin at approriate timer value */
while (state < 10) { // if byte has not been fully transmitted yet
target += ticks_per_bit; // value for setting OCRA register to cause this interrupt
// to occur after 1 bit time
if (state < 9) // if not all data-bits are transmitted
bit = byte & 1; // get the bit to be transmitted after 1 bit time
else
bit = 1; // stopbit
byte >>= 1;
state++;
// if next bit is same as current bit, then there is no need to this interrupt
// after 1 bit time. Find which next bit is different from current one, and set the isr
// to fire at that time.
if (bit != tx_bit) {
if (bit) {
CONFIG_MATCH_SET(); // set tx pin to high when timer value matches OCRA value
} else {
CONFIG_MATCH_CLEAR(); // set tx pin to low when timer value matches OCRA value
}
SET_COMPARE_TX(target);
tx_bit = bit;
tx_byte = byte;
tx_state = state;
// TODO: how to detect timing_error?
return;
}
}
// Control reaches this point if
// - tx pin is not to be toggled till end of first stop bit (state = 10). This also means
// currently tx pin is High and Timer output compare pin would be set to MATCH_SET.
// - we are at the end of 2nd stop bit (state = 11)
head = tx_buffer_head;
tail = tx_buffer_tail; // the byte currently being transmiited or already transmitted
if (head == tail) {
if (state == 10) {
// Wait for final stop bit to finish
tx_state = 11;
SET_COMPARE_TX(target + ticks_per_bit);
} else {
tx_state = 0;
CONFIG_MATCH_NORMAL(); // do nothing on output pin on compare match
DISABLE_INT_COMPARE_TX();
}
} else {
if (++tail >= TX_BUFFER_SIZE) tail = 0;
tx_buffer_tail = tail;
tx_byte = tx_buffer[tail];
tx_bit = 0; // start bit
CONFIG_MATCH_CLEAR();
if (state == 10)
SET_COMPARE_TX(target + ticks_per_bit); // wait for stop bit com finish
else
SET_COMPARE_TX(GET_TIMER_COUNT() + 16);
tx_state = 1;
// TODO: how to detect timing_error?
}
}
void AltSoftSerial::flushOutput(void)
{
while (tx_state) /* wait */ ;
}
/****************************************/
/** Reception **/
/****************************************/
/* Reception uses 2 ISRs: captureInterrupt_isr() and compareBInterrupt_isr().
* These 2 ISRs don't preempt each other as they are designed to called at
* different times. */
/* This isr is called at start of start bit and at time when level change happens
* during reception of 10 bits. */
void AltSoftSerial::captureInterrupt_isr()
{
// Serial.print("EICRA: ");
// Serial.println(EICRA, HEX);
// Serial.print("EIMSK: ");
// Serial.println(EIMSK, HEX);
uint8_t state, bit, head;
uint16_t capture, target;
uint16_t offset, offset_overflow;
/* When the interrupt occurs, the value of timer count TCNT register is written to
* ICR register. Read the contents of that register */
capture = GET_INPUT_CAPTURE();
bit = rx_bit;
if (bit) {
CONFIG_CAPTURE_FALLING_EDGE();
rx_bit = 0;
} else {
CONFIG_CAPTURE_RISING_EDGE();
rx_bit = 0x80;
}
state = rx_state;
if (state == 0) { // start of start bit
if (!bit) {
uint16_t end = capture + rx_stop_ticks;
SET_COMPARE_RX(end);
ENABLE_INT_COMPARE_RX();
rx_target = capture + ticks_per_bit + ticks_per_bit/2; // middle of 1st data bit
rx_state = 1;
}
} else {
target = rx_target;
offset_overflow = 65535 - ticks_per_bit;
while (1) {
offset = capture - target;
if (offset > offset_overflow) break;
rx_byte = (rx_byte >> 1) | rx_bit;
target += ticks_per_bit;
state++;
if (state >= 9) {
DISABLE_INT_COMPARE_RX();
head = rx_buffer_head + 1;
if (head >= RX_BUFFER_SIZE) head = 0;
if (head != rx_buffer_tail) {
rx_buffer[head] = rx_byte;
rx_buffer_head = head;
}
CONFIG_CAPTURE_FALLING_EDGE();
rx_bit = 0;
rx_state = 0;
return;
}
}
rx_target = target;
rx_state = state;
}
//if (GET_TIMER_COUNT() - capture > ticks_per_bit) AltSoftSerial::timing_error = true;
}
/* This function is called at start of stop bit if 8th data bit is not 1 */
void AltSoftSerial::compareInterrupt_rx_isr()
{
uint8_t head, state, bit;
DISABLE_INT_COMPARE_RX();
CONFIG_CAPTURE_FALLING_EDGE();
state = rx_state;
bit = rx_bit ^ 0x80; // invert the rx_bit
while (state < 9) {
rx_byte = (rx_byte >> 1) | bit;
state++;
}
head = rx_buffer_head + 1;
if (head >= RX_BUFFER_SIZE) head = 0;
if (head != rx_buffer_tail) {
rx_buffer[head] = rx_byte;
rx_buffer_head = head;
}
rx_state = 0;
CONFIG_CAPTURE_FALLING_EDGE();
rx_bit = 0;
}
int AltSoftSerial::read(void)
{
uint8_t head, tail, out;
/* Store value of rx_buffer_head in temporary variable instead of using
rx_buffer_head at multiple places inside the fxn because its value might
be changed by one of the ISRs */
head = rx_buffer_head; // can be changed by ISRs
tail = rx_buffer_tail; // read by ISRs but not written by them
if (head == tail) return -1;
if (++tail >= RX_BUFFER_SIZE) tail = 0;
out = rx_buffer[tail];
rx_buffer_tail = tail;
return out;
}
int AltSoftSerial::peek(void)
{
uint8_t head, tail;
head = rx_buffer_head;
tail = rx_buffer_tail;
if (head == tail) return -1;
if (++tail >= RX_BUFFER_SIZE) tail = 0;
return rx_buffer[tail];
}
int AltSoftSerial::available(void)
{
uint8_t head, tail;
head = rx_buffer_head;
tail = rx_buffer_tail;
if (head >= tail) return head - tail;
return RX_BUFFER_SIZE + head - tail;
}
void AltSoftSerial::flushInput(void)
{
rx_buffer_head = rx_buffer_tail;
}
#ifdef ALTSS_USE_FTM0
void ftm0_isr(void)
{
uint32_t flags = FTM0_STATUS;
FTM0_STATUS = 0;
if (flags & (1<<0) && (FTM0_C0SC & 0x40)) altss_compare_b_interrupt();
if (flags & (1<<5)) altss_capture_interrupt();
if (flags & (1<<6) && (FTM0_C6SC & 0x40)) altss_compare_a_interrupt();
}
#endif