forked from esp8266/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHardwareSerial.cpp
764 lines (658 loc) · 20.9 KB
/
HardwareSerial.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
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
/*
HardwareSerial.cpp - esp8266 UART support
Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
This file is part of the esp8266 core for Arduino environment.
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
Modified 31 March 2015 by Markus Sattler (rewrite the code for UART0 + UART1 support in ESP8266)
Modified 25 April 2015 by Thomas Flayols (add configuration different from 8N1 in ESP8266)
Modified 3 May 2015 by Hristo Gochkov (change register access methods)
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include "Arduino.h"
#include "cbuf.h"
#include "interrupts.h"
extern "C" {
#include "osapi.h"
#include "ets_sys.h"
#include "mem.h"
#include "user_interface.h"
}
#include "HardwareSerial.h"
#define UART_TX_FIFO_SIZE 0x80
struct uart_ {
int uart_nr;
int baud_rate;
bool rxEnabled;
bool txEnabled;
uint8_t rxPin;
uint8_t txPin;
};
static const int UART0 = 0;
static const int UART1 = 1;
static const int UART_NO = -1;
/**
* UART GPIOs
*
* UART0 TX: 1 or 2
* UART0 RX: 3
*
* UART0 SWAP TX: 15
* UART0 SWAP RX: 13
*
*
* UART1 TX: 7 (NC) or 2
* UART1 RX: 8 (NC)
*
* UART1 SWAP TX: 11 (NC)
* UART1 SWAP RX: 6 (NC)
*
* NC = Not Connected to Module Pads --> No Access
*
*/
// ####################################################################################################
// ####################################################################################################
// ####################################################################################################
HardwareSerial Serial(UART0);
HardwareSerial Serial1(UART1);
// ####################################################################################################
// ####################################################################################################
// ####################################################################################################
void uart_interrupt_handler(uart_t* uart);
void uart_wait_for_tx_fifo(uart_t* uart, size_t size_needed);
size_t uart_get_tx_fifo_room(uart_t* uart);
void uart_wait_for_transmit(uart_t* uart);
void uart_transmit_char(uart_t* uart, char c);
void uart_transmit(uart_t* uart, const char* buf, size_t size);
void uart_flush(uart_t* uart);
void uart_interrupt_enable(uart_t* uart);
void uart_interrupt_disable(uart_t* uart);
void uart_arm_tx_interrupt(uart_t* uart);
void uart_disarm_tx_interrupt(uart_t* uart);
void uart_set_baudrate(uart_t* uart, int baud_rate);
int uart_get_baudrate(uart_t* uart);
uart_t* uart_start_init(int uart_nr, int baudrate, byte config, uint8_t use_tx);
void uart_finish_init(uart_t* uart);
void uart_uninit(uart_t* uart);
void uart_swap(uart_t* uart, uint8_t use_tx);
void uart_set_tx(uart_t* uart, uint8_t use_tx);
void uart_set_pins(uart_t* uart, uint8_t tx, uint8_t rx);
void uart_ignore_char(char c);
void uart0_write_char(char c);
void uart1_write_char(char c);
void uart_set_debug(int uart_nr);
int uart_get_debug();
// ####################################################################################################
// ####################################################################################################
// ####################################################################################################
// These function internals can be used from interrupt handlers to ensure they
// are in instruction RAM, or anywhere that the uart_nr has been validated.
#define UART_GET_TX_FIFO_ROOM(uart_nr) (UART_TX_FIFO_SIZE - ((USS(uart_nr) >> USTXC) & 0xff))
#define UART_TRANSMIT_CHAR(uart_nr, c) do { USF(uart_nr) = (c); } while(0)
#define UART_ARM_TX_INTERRUPT(uart_nr) do { USIE(uart_nr) |= (1 << UIFE); } while(0)
#define UART_DISARM_TX_INTERRUPT(uart_nr) do { USIE(uart_nr) &= ~(1 << UIFE); } while(0)
void ICACHE_RAM_ATTR uart_interrupt_handler(uart_t* uart) {
// -------------- UART 0 --------------
if(Serial.isRxEnabled()) {
while(U0IS & (1 << UIFF)) {
Serial._rx_complete_irq((char) (U0F & 0xff));
U0IC = (1 << UIFF);
}
}
if(Serial.isTxEnabled()) {
if(U0IS & (1 << UIFE)) {
U0IC = (1 << UIFE);
Serial._tx_empty_irq();
}
}
// -------------- UART 1 --------------
// Note: only TX is supported on UART 1.
if(Serial1.isTxEnabled()) {
if(U1IS & (1 << UIFE)) {
U1IC = (1 << UIFE);
Serial1._tx_empty_irq();
}
}
}
// ####################################################################################################
void uart_wait_for_tx_fifo(uart_t* uart, size_t size_needed) {
if(uart == 0)
return;
if(uart->txEnabled) {
while(true) {
size_t tx_count = (USS(uart->uart_nr) >> USTXC) & 0xff;
if(tx_count <= (UART_TX_FIFO_SIZE - size_needed))
break;
}
}
}
size_t uart_get_tx_fifo_room(uart_t* uart) {
if(uart == 0)
return 0;
if(uart->txEnabled) {
return UART_GET_TX_FIFO_ROOM(uart->uart_nr);
}
return 0;
}
void uart_wait_for_transmit(uart_t* uart) {
if(uart == 0)
return;
if(uart->txEnabled) {
uart_wait_for_tx_fifo(uart, UART_TX_FIFO_SIZE);
}
}
void uart_transmit_char(uart_t* uart, char c) {
if(uart == 0)
return;
if(uart->txEnabled) {
UART_TRANSMIT_CHAR(uart->uart_nr, c);
}
}
void uart_transmit(uart_t* uart, const char* buf, size_t size) {
if(uart == 0)
return;
if(uart->txEnabled) {
while(size) {
size_t part_size = (size > UART_TX_FIFO_SIZE) ? UART_TX_FIFO_SIZE : size;
size -= part_size;
uart_wait_for_tx_fifo(uart, part_size);
for(; part_size; --part_size, ++buf)
USF(uart->uart_nr) = *buf;
}
}
}
void uart_flush(uart_t* uart) {
uint32_t tmp = 0x00000000;
if(uart == 0)
return;
if(uart->rxEnabled) {
tmp |= (1 << UCRXRST);
}
if(uart->txEnabled) {
tmp |= (1 << UCTXRST);
}
USC0(uart->uart_nr) |= (tmp);
USC0(uart->uart_nr) &= ~(tmp);
}
void uart_interrupt_enable(uart_t* uart) {
if(uart == 0)
return;
USIC(uart->uart_nr) = 0x1ff;
ETS_UART_INTR_ATTACH(&uart_interrupt_handler, uart); // uart parameter is not osed in irq function!
if(uart->rxEnabled) {
USIE(uart->uart_nr) |= (1 << UIFF);
}
ETS_UART_INTR_ENABLE();
}
void uart_interrupt_disable(uart_t* uart) {
if(uart == 0)
return;
if(uart->rxEnabled) {
USIE(uart->uart_nr) &= ~(1 << UIFF);
}
if(uart->txEnabled) {
USIE(uart->uart_nr) &= ~(1 << UIFE);
}
//ETS_UART_INTR_DISABLE(); // never disable irq complete may its needed by the other Serial Interface!
}
void uart_arm_tx_interrupt(uart_t* uart) {
if(uart == 0)
return;
if(uart->txEnabled) {
UART_ARM_TX_INTERRUPT(uart->uart_nr);
}
}
void uart_disarm_tx_interrupt(uart_t* uart) {
if(uart == 0)
return;
if(uart->txEnabled) {
UART_DISARM_TX_INTERRUPT(uart->uart_nr);
}
}
void uart_set_baudrate(uart_t* uart, int baud_rate) {
if(uart == 0)
return;
uart->baud_rate = baud_rate;
USD(uart->uart_nr) = (ESP8266_CLOCK / uart->baud_rate);
}
int uart_get_baudrate(uart_t* uart) {
if(uart == 0)
return 0;
return uart->baud_rate;
}
uart_t* uart_start_init(int uart_nr, int baudrate, byte config, byte mode, uint8_t use_tx) {
uart_t* uart = (uart_t*) os_malloc(sizeof(uart_t));
if(uart == 0) {
return 0;
}
uart->uart_nr = uart_nr;
switch(uart->uart_nr) {
case UART0:
uart->rxEnabled = (mode != SERIAL_TX_ONLY);
uart->txEnabled = (mode != SERIAL_RX_ONLY);
uart->rxPin = (uart->rxEnabled)?3:255;
if(uart->rxEnabled) {
if (use_tx == 2) {
uart->txPin = 2;
pinMode(uart->rxPin, FUNCTION_4);
} else {
uart->txPin = 1;
pinMode(uart->rxPin, SPECIAL);
}
} else uart->txPin = 255;
if(uart->txEnabled) pinMode(uart->txPin, SPECIAL);
IOSWAP &= ~(1 << IOSWAPU0);
break;
case UART1:
// Note: uart_interrupt_handler does not support RX on UART 1.
uart->rxEnabled = false;
uart->txEnabled = (mode != SERIAL_RX_ONLY);
uart->rxPin = 255;
uart->txPin = (uart->txEnabled)?2:255; // GPIO7 as TX not possible! See GPIO pins used by UART
if(uart->txEnabled) pinMode(uart->txPin, SPECIAL);
break;
case UART_NO:
default:
// big fail!
os_free(uart);
return 0;
}
uart_set_baudrate(uart, baudrate);
USC0(uart->uart_nr) = config;
return uart;
}
void uart_finish_init(uart_t* uart) {
uint32_t conf1 = 0x00000000;
uart_flush(uart);
uart_interrupt_enable(uart);
if(uart->rxEnabled) {
conf1 |= (0x01 << UCFFT);
}
if(uart->txEnabled) {
conf1 |= (0x20 << UCFET);
}
USC1(uart->uart_nr) = conf1;
}
void uart_uninit(uart_t* uart) {
if(uart == 0)
return;
uart_interrupt_disable(uart);
switch(uart->rxPin) {
case 3:
pinMode(3, INPUT);
break;
case 13:
pinMode(13, INPUT);
break;
}
switch(uart->txPin) {
case 1:
pinMode(1, INPUT);
break;
case 2:
pinMode(2, INPUT);
break;
case 15:
pinMode(15, INPUT);
break;
}
os_free(uart);
}
void uart_swap(uart_t* uart, uint8_t use_tx) {
if(uart == 0)
return;
switch(uart->uart_nr) {
case UART0:
if(((uart->txPin == 1 || uart->txPin == 2) && uart->txEnabled) || (uart->rxPin == 3 && uart->rxEnabled)) {
if(uart->txEnabled){ //TX
pinMode(uart->txPin, INPUT);
uart->txPin = 15;
}
if(uart->rxEnabled){ //RX
pinMode(uart->rxPin, INPUT);
uart->rxPin = 13;
}
if(uart->txEnabled) pinMode(uart->txPin, FUNCTION_4); //TX
if(uart->rxEnabled) pinMode(uart->rxPin, FUNCTION_4); //RX
IOSWAP |= (1 << IOSWAPU0);
} else {
if(uart->txEnabled){ //TX
pinMode(uart->txPin, INPUT);
uart->txPin = (use_tx == 2)?2:1;
}
if(uart->rxEnabled){ //RX
pinMode(uart->rxPin, INPUT);
uart->rxPin = 3;
}
if(uart->txEnabled) pinMode(uart->txPin, (use_tx == 2)?FUNCTION_4:SPECIAL); //TX
if(uart->rxEnabled) pinMode(3, SPECIAL); //RX
IOSWAP &= ~(1 << IOSWAPU0);
}
break;
case UART1:
// Currently no swap possible! See GPIO pins used by UART
break;
default:
break;
}
}
void uart_set_tx(uart_t* uart, uint8_t use_tx) {
if(uart == 0)
return;
switch(uart->uart_nr) {
case UART0:
if(uart->txEnabled) {
if (uart->txPin == 1 && use_tx == 2) {
pinMode(uart->txPin, INPUT);
uart->txPin = 2;
pinMode(uart->txPin, FUNCTION_4);
} else if (uart->txPin == 2 && use_tx != 2) {
pinMode(uart->txPin, INPUT);
uart->txPin = 1;
pinMode(uart->txPin, SPECIAL);
}
}
break;
case UART1:
// GPIO7 as TX not possible! See GPIO pins used by UART
break;
default:
break;
}
}
void uart_set_pins(uart_t* uart, uint8_t tx, uint8_t rx) {
if(uart == 0)
return;
if(uart->uart_nr == UART0) { // Only UART0 allows pin changes
if(uart->txEnabled && uart->txPin != tx) {
if( rx == 13 && tx == 15) {
uart_swap(uart, 15);
} else if (rx == 3 && (tx == 1 || tx == 2)) {
if (uart->rxPin != rx) uart_swap(uart, tx);
else uart_set_tx(uart, tx);
}
}
if(uart->rxEnabled && uart->rxPin != rx && rx == 13 && tx == 15) {
uart_swap(uart, 15);
}
}
}
// ####################################################################################################
// ####################################################################################################
// ####################################################################################################
void uart_ignore_char(char c) {
}
void uart0_write_char(char c) {
if(&Serial != NULL && Serial.isTxEnabled()) {
if(Serial.availableForWrite() > 0) {
if(c == '\n') {
Serial.write('\r');
}
Serial.write(c);
return;
}
}
// wait for the Hardware FIFO
while(true) {
if(((USS(0) >> USTXC) & 0xff) <= (UART_TX_FIFO_SIZE - 2)) {
break;
}
}
if(c == '\n') {
USF(0) = '\r';
}
USF(0) = c;
}
void uart1_write_char(char c) {
if(&Serial1 != NULL && Serial1.isTxEnabled()) {
if(Serial1.availableForWrite() > 0) {
if(c == '\n') {
Serial1.write('\r');
}
Serial1.write(c);
return;
}
}
// wait for the Hardware FIFO
while(true) {
if(((USS(1) >> USTXC) & 0xff) <= (UART_TX_FIFO_SIZE - 2)) {
break;
}
}
if(c == '\n') {
USF(1) = '\r';
}
USF(1) = c;
}
static int s_uart_debug_nr = UART0;
void uart_set_debug(int uart_nr) {
s_uart_debug_nr = uart_nr;
switch(s_uart_debug_nr) {
case UART0:
system_set_os_print(1);
ets_install_putc1((void *) &uart0_write_char);
break;
case UART1:
system_set_os_print(1);
ets_install_putc1((void *) &uart1_write_char);
break;
case UART_NO:
default:
system_set_os_print(0);
ets_install_putc1((void *) &uart_ignore_char);
break;
}
}
int uart_get_debug() {
return s_uart_debug_nr;
}
// ####################################################################################################
// ####################################################################################################
// ####################################################################################################
HardwareSerial::HardwareSerial(int uart_nr) :
_uart_nr(uart_nr), _uart(0), _tx_buffer(0), _rx_buffer(0) {
}
void HardwareSerial::begin(unsigned long baud, byte config, byte mode, uint8_t use_tx) {
InterruptLock il;
// disable debug for this interface
if(uart_get_debug() == _uart_nr) {
uart_set_debug(UART_NO);
}
if (_uart) {
os_free(_uart);
}
_uart = uart_start_init(_uart_nr, baud, config, mode, use_tx);
if(_uart == 0) {
return;
}
// Disable the RX and/or TX functions if we fail to allocate circular buffers.
// The user can confirm they are enabled with isRxEnabled() and isTxEnabled().
if(_uart->rxEnabled && !_rx_buffer) {
_rx_buffer = new cbuf(SERIAL_RX_BUFFER_SIZE);
if(!_rx_buffer) {
_uart->rxEnabled = false;
}
}
if(_uart->txEnabled && !_tx_buffer) {
_tx_buffer = new cbuf(SERIAL_TX_BUFFER_SIZE);
if(!_tx_buffer) {
_uart->txEnabled = false;
}
}
delay(1);
uart_finish_init(_uart);
}
void HardwareSerial::end() {
InterruptLock il;
if(uart_get_debug() == _uart_nr) {
uart_set_debug(UART_NO);
}
uart_uninit(_uart);
delete _rx_buffer;
delete _tx_buffer;
_uart = 0;
_rx_buffer = 0;
_tx_buffer = 0;
}
void HardwareSerial::swap(uint8_t use_tx) {
if(_uart == 0)
return;
uart_swap(_uart, use_tx);
}
void HardwareSerial::set_tx(uint8_t use_tx) {
if(_uart == 0)
return;
uart_set_tx(_uart, use_tx);
}
void HardwareSerial::pins(uint8_t tx, uint8_t rx) {
if(_uart == 0)
return;
uart_set_pins(_uart, tx, rx);
}
void HardwareSerial::setDebugOutput(bool en) {
if(_uart == 0)
return;
if(en) {
if(_uart->txEnabled)
uart_set_debug(_uart->uart_nr);
else
uart_set_debug(UART_NO);
} else {
// disable debug for this interface
if(uart_get_debug() == _uart_nr) {
uart_set_debug(UART_NO);
}
}
}
bool ICACHE_RAM_ATTR HardwareSerial::isTxEnabled(void) {
if(_uart == 0)
return false;
return _uart->txEnabled;
}
bool ICACHE_RAM_ATTR HardwareSerial::isRxEnabled(void) {
if(_uart == 0)
return false;
return _uart->rxEnabled;
}
int HardwareSerial::available(void) {
int result = 0;
if (_uart != NULL && _uart->rxEnabled) {
InterruptLock il;
result = static_cast<int>(_rx_buffer->getSize());
}
if (!result) {
optimistic_yield(USD(_uart->uart_nr) / 128);
}
return result;
}
int HardwareSerial::peek(void) {
if(_uart == 0)
return -1;
if(_uart->rxEnabled) {
InterruptLock il;
return _rx_buffer->peek();
} else {
return -1;
}
}
int HardwareSerial::read(void) {
if(_uart == 0)
return -1;
if(_uart->rxEnabled) {
InterruptLock il;
return _rx_buffer->read();
} else {
return -1;
}
}
int HardwareSerial::availableForWrite(void) {
if(_uart == 0)
return 0;
if(_uart->txEnabled) {
InterruptLock il;
return static_cast<int>(_tx_buffer->room());
} else {
return 0;
}
}
void HardwareSerial::flush() {
if(_uart == 0)
return;
if(!_uart->txEnabled)
return;
const int uart_nr = _uart->uart_nr;
while(true) {
{
InterruptLock il;
if(_tx_buffer->getSize() == 0 &&
UART_GET_TX_FIFO_ROOM(uart_nr) >= UART_TX_FIFO_SIZE) {
break;
} else if(il.savedInterruptLevel() > 0) {
_tx_empty_irq();
continue;
}
}
yield();
}
}
size_t HardwareSerial::write(uint8_t c) {
if(_uart == 0 || !_uart->txEnabled)
return 0;
bool tx_now = false;
const int uart_nr = _uart->uart_nr;
while(true) {
{
InterruptLock il;
if(_tx_buffer->empty()) {
if(UART_GET_TX_FIFO_ROOM(uart_nr) > 0) {
tx_now = true;
} else {
_tx_buffer->write(c);
UART_ARM_TX_INTERRUPT(uart_nr);
}
break;
} else if(_tx_buffer->write(c)) {
break;
} else if(il.savedInterruptLevel() > 0) {
_tx_empty_irq();
continue;
}
}
yield();
}
if (tx_now) {
UART_TRANSMIT_CHAR(uart_nr, c);
}
return 1;
}
HardwareSerial::operator bool() const {
return _uart != 0;
}
void ICACHE_RAM_ATTR HardwareSerial::_rx_complete_irq(char c) {
_rx_buffer->write(c);
}
void ICACHE_RAM_ATTR HardwareSerial::_tx_empty_irq(void) {
const int uart_nr = _uart->uart_nr;
size_t queued = _tx_buffer->getSize();
if(!queued) {
UART_DISARM_TX_INTERRUPT(uart_nr);
return;
}
size_t room = UART_GET_TX_FIFO_ROOM(uart_nr);
int n = static_cast<int>((queued < room) ? queued : room);
while(n--) {
UART_TRANSMIT_CHAR(uart_nr, _tx_buffer->read());
}
}