@@ -41,6 +41,20 @@ extern "C" {
41
41
42
42
#define UART_TX_FIFO_SIZE 0x80
43
43
44
+ struct uart_ {
45
+ int uart_nr;
46
+ int baud_rate;
47
+ bool rxEnabled;
48
+ bool txEnabled;
49
+ uint8_t rxPin;
50
+ uint8_t txPin;
51
+ };
52
+
53
+ static const int UART0 = 0 ;
54
+ static const int UART1 = 1 ;
55
+ static const int UART_NO = -1 ;
56
+
57
+
44
58
/* *
45
59
* UART GPIOs
46
60
*
@@ -86,16 +100,16 @@ void uart_disarm_tx_interrupt(uart_t* uart);
86
100
void uart_set_baudrate (uart_t * uart, int baud_rate);
87
101
int uart_get_baudrate (uart_t * uart);
88
102
89
- uart_t * uart_init (UARTnr_t uart_nr, int baudrate, byte config);
103
+ uart_t * uart_init (int uart_nr, int baudrate, byte config);
90
104
void uart_uninit (uart_t * uart);
91
105
void uart_swap (uart_t * uart);
92
106
93
107
void uart_ignore_char (char c);
94
108
void uart0_write_char (char c);
95
109
void uart1_write_char (char c);
96
110
97
- void uart_set_debug (UARTnr_t uart_nr);
98
- UARTnr_t uart_get_debug ();
111
+ void uart_set_debug (int uart_nr);
112
+ int uart_get_debug ();
99
113
100
114
// ####################################################################################################
101
115
// ####################################################################################################
@@ -108,12 +122,12 @@ void ICACHE_RAM_ATTR uart_interrupt_handler(uart_t* uart) {
108
122
if (Serial.isRxEnabled ()) {
109
123
if (status & (1 << UIFF)) {
110
124
while (true ) {
111
- int rx_count = (U0S >> USTXC) & 0xFF ;
125
+ int rx_count = (U0S >> USTXC) & 0xff ;
112
126
if (!rx_count)
113
127
break ;
114
128
115
129
while (rx_count--) {
116
- char c = U0F & 0xFF ;
130
+ char c = U0F & 0xff ;
117
131
Serial._rx_complete_irq (c);
118
132
}
119
133
}
@@ -133,12 +147,12 @@ void ICACHE_RAM_ATTR uart_interrupt_handler(uart_t* uart) {
133
147
if (Serial1.isRxEnabled ()) {
134
148
if (status & (1 << UIFF)) {
135
149
while (true ) {
136
- int rx_count = (U1S >> USTXC) & 0xFF ;
150
+ int rx_count = (U1S >> USTXC) & 0xff ;
137
151
if (!rx_count)
138
152
break ;
139
153
140
154
while (rx_count--) {
141
- char c = U1F & 0xFF ;
155
+ char c = U1F & 0xff ;
142
156
Serial1._rx_complete_irq (c);
143
157
}
144
158
}
@@ -156,44 +170,44 @@ void ICACHE_RAM_ATTR uart_interrupt_handler(uart_t* uart) {
156
170
157
171
// ####################################################################################################
158
172
159
- void ICACHE_FLASH_ATTR uart_wait_for_tx_fifo (uart_t * uart, size_t size_needed) {
173
+ void uart_wait_for_tx_fifo (uart_t * uart, size_t size_needed) {
160
174
if (uart == 0 )
161
175
return ;
162
176
if (uart->txEnabled ) {
163
177
while (true ) {
164
- size_t tx_count = (USS (uart->uart_nr ) >> USTXC) & 0xFF ;
178
+ size_t tx_count = (USS (uart->uart_nr ) >> USTXC) & 0xff ;
165
179
if (tx_count <= (UART_TX_FIFO_SIZE - size_needed))
166
180
break ;
167
181
}
168
182
}
169
183
}
170
184
171
- size_t ICACHE_FLASH_ATTR uart_get_tx_fifo_room (uart_t * uart) {
185
+ size_t uart_get_tx_fifo_room (uart_t * uart) {
172
186
if (uart == 0 )
173
187
return 0 ;
174
188
if (uart->txEnabled ) {
175
- return UART_TX_FIFO_SIZE - ((USS (uart->uart_nr ) >> USTXC) & 0xFF );
189
+ return UART_TX_FIFO_SIZE - ((USS (uart->uart_nr ) >> USTXC) & 0xff );
176
190
}
177
191
return 0 ;
178
192
}
179
193
180
- void ICACHE_FLASH_ATTR uart_wait_for_transmit (uart_t * uart) {
194
+ void uart_wait_for_transmit (uart_t * uart) {
181
195
if (uart == 0 )
182
196
return ;
183
197
if (uart->txEnabled ) {
184
198
uart_wait_for_tx_fifo (uart, UART_TX_FIFO_SIZE);
185
199
}
186
200
}
187
201
188
- void ICACHE_FLASH_ATTR uart_transmit_char (uart_t * uart, char c) {
202
+ void uart_transmit_char (uart_t * uart, char c) {
189
203
if (uart == 0 )
190
204
return ;
191
205
if (uart->txEnabled ) {
192
206
USF (uart->uart_nr ) = c;
193
207
}
194
208
}
195
209
196
- void ICACHE_FLASH_ATTR uart_transmit (uart_t * uart, const char * buf, size_t size) {
210
+ void uart_transmit (uart_t * uart, const char * buf, size_t size) {
197
211
if (uart == 0 )
198
212
return ;
199
213
if (uart->txEnabled ) {
@@ -208,7 +222,7 @@ void ICACHE_FLASH_ATTR uart_transmit(uart_t* uart, const char* buf, size_t size)
208
222
}
209
223
}
210
224
211
- void ICACHE_FLASH_ATTR uart_flush (uart_t * uart) {
225
+ void uart_flush (uart_t * uart) {
212
226
uint32_t tmp = 0x00000000 ;
213
227
214
228
if (uart == 0 )
@@ -226,7 +240,7 @@ void ICACHE_FLASH_ATTR uart_flush(uart_t* uart) {
226
240
USC0 (uart->uart_nr ) &= ~(tmp);
227
241
}
228
242
229
- void ICACHE_FLASH_ATTR uart_interrupt_enable (uart_t * uart) {
243
+ void uart_interrupt_enable (uart_t * uart) {
230
244
if (uart == 0 )
231
245
return ;
232
246
USIC (uart->uart_nr ) = 0x1ff ;
@@ -237,7 +251,7 @@ void ICACHE_FLASH_ATTR uart_interrupt_enable(uart_t* uart) {
237
251
ETS_UART_INTR_ENABLE ();
238
252
}
239
253
240
- void ICACHE_FLASH_ATTR uart_interrupt_disable (uart_t * uart) {
254
+ void uart_interrupt_disable (uart_t * uart) {
241
255
if (uart == 0 )
242
256
return ;
243
257
if (uart->rxEnabled ) {
@@ -249,36 +263,36 @@ void ICACHE_FLASH_ATTR uart_interrupt_disable(uart_t* uart) {
249
263
// ETS_UART_INTR_DISABLE(); // never disable irq complete may its needed by the other Serial Interface!
250
264
}
251
265
252
- void ICACHE_FLASH_ATTR uart_arm_tx_interrupt (uart_t * uart) {
266
+ void uart_arm_tx_interrupt (uart_t * uart) {
253
267
if (uart == 0 )
254
268
return ;
255
269
if (uart->txEnabled ) {
256
270
USIE (uart->uart_nr ) |= (1 << UIFE);
257
271
}
258
272
}
259
273
260
- void ICACHE_FLASH_ATTR uart_disarm_tx_interrupt (uart_t * uart) {
274
+ void uart_disarm_tx_interrupt (uart_t * uart) {
261
275
if (uart == 0 )
262
276
return ;
263
277
if (uart->txEnabled ) {
264
278
USIE (uart->uart_nr ) &= ~(1 << UIFE);
265
279
}
266
280
}
267
281
268
- void ICACHE_FLASH_ATTR uart_set_baudrate (uart_t * uart, int baud_rate) {
282
+ void uart_set_baudrate (uart_t * uart, int baud_rate) {
269
283
if (uart == 0 )
270
284
return ;
271
285
uart->baud_rate = baud_rate;
272
286
USD (uart->uart_nr ) = (80000000UL / uart->baud_rate );
273
287
}
274
288
275
- int ICACHE_FLASH_ATTR uart_get_baudrate (uart_t * uart) {
289
+ int uart_get_baudrate (uart_t * uart) {
276
290
if (uart == 0 )
277
291
return 0 ;
278
292
return uart->baud_rate ;
279
293
}
280
294
281
- uart_t * ICACHE_FLASH_ATTR uart_init (UARTnr_t uart_nr, int baudrate, byte config) {
295
+ uart_t * uart_init (int uart_nr, int baudrate, byte config) {
282
296
283
297
uint32_t conf1 = 0x00000000 ;
284
298
uart_t * uart = (uart_t *) os_malloc (sizeof (uart_t ));
@@ -329,7 +343,7 @@ uart_t* ICACHE_FLASH_ATTR uart_init(UARTnr_t uart_nr, int baudrate, byte config)
329
343
return uart;
330
344
}
331
345
332
- void ICACHE_FLASH_ATTR uart_uninit (uart_t * uart) {
346
+ void uart_uninit (uart_t * uart) {
333
347
if (uart == 0 )
334
348
return ;
335
349
uart_interrupt_disable (uart);
@@ -358,7 +372,7 @@ void ICACHE_FLASH_ATTR uart_uninit(uart_t* uart) {
358
372
os_free (uart);
359
373
}
360
374
361
- void ICACHE_FLASH_ATTR uart_swap (uart_t * uart) {
375
+ void uart_swap (uart_t * uart) {
362
376
if (uart == 0 )
363
377
return ;
364
378
switch (uart->uart_nr ) {
@@ -394,10 +408,10 @@ void ICACHE_FLASH_ATTR uart_swap(uart_t* uart) {
394
408
// ####################################################################################################
395
409
// ####################################################################################################
396
410
397
- void ICACHE_FLASH_ATTR uart_ignore_char (char c) {
411
+ void uart_ignore_char (char c) {
398
412
}
399
413
400
- void ICACHE_FLASH_ATTR uart0_write_char (char c) {
414
+ void uart0_write_char (char c) {
401
415
if (&Serial != NULL && Serial.isTxEnabled ()) {
402
416
if (c == ' \n ' ) {
403
417
Serial.write (' \r ' );
@@ -411,7 +425,7 @@ void ICACHE_FLASH_ATTR uart0_write_char(char c) {
411
425
}
412
426
}
413
427
414
- void ICACHE_FLASH_ATTR uart1_write_char (char c) {
428
+ void uart1_write_char (char c) {
415
429
if (&Serial1 != NULL && Serial1.isTxEnabled ()) {
416
430
if (c == ' \n ' ) {
417
431
Serial1.write (' \r ' );
@@ -425,8 +439,9 @@ void ICACHE_FLASH_ATTR uart1_write_char(char c) {
425
439
}
426
440
}
427
441
428
- static UARTnr_t s_uart_debug_nr = UART0;
429
- void ICACHE_FLASH_ATTR uart_set_debug (UARTnr_t uart_nr) {
442
+ static int s_uart_debug_nr = UART0;
443
+
444
+ void uart_set_debug (int uart_nr) {
430
445
s_uart_debug_nr = uart_nr;
431
446
switch (s_uart_debug_nr) {
432
447
case UART0:
@@ -445,20 +460,19 @@ void ICACHE_FLASH_ATTR uart_set_debug(UARTnr_t uart_nr) {
445
460
}
446
461
}
447
462
448
- UARTnr_t ICACHE_FLASH_ATTR uart_get_debug () {
463
+ int uart_get_debug () {
449
464
return s_uart_debug_nr;
450
465
}
451
466
452
467
// ####################################################################################################
453
468
// ####################################################################################################
454
469
// ####################################################################################################
455
470
456
- ICACHE_FLASH_ATTR HardwareSerial::HardwareSerial (UARTnr_t uart_nr) :
457
- _uart(0 ), _tx_buffer(0 ), _rx_buffer(0 ), _written(false ) {
458
- _uart_nr = uart_nr;
471
+ HardwareSerial::HardwareSerial (int uart_nr) :
472
+ _uart_nr(uart_nr), _uart(0 ), _tx_buffer(0 ), _rx_buffer(0 ), _written(false ) {
459
473
}
460
474
461
- void ICACHE_FLASH_ATTR HardwareSerial::begin (unsigned long baud, byte config) {
475
+ void HardwareSerial::begin (unsigned long baud, byte config) {
462
476
463
477
// disable debug for this interface
464
478
if (uart_get_debug () == _uart_nr) {
@@ -472,16 +486,18 @@ void ICACHE_FLASH_ATTR HardwareSerial::begin(unsigned long baud, byte config) {
472
486
}
473
487
474
488
if (_uart->rxEnabled ) {
475
- _rx_buffer = new cbuf (SERIAL_RX_BUFFER_SIZE);
489
+ if (!_rx_buffer)
490
+ _rx_buffer = new cbuf (SERIAL_RX_BUFFER_SIZE);
476
491
}
477
492
if (_uart->txEnabled ) {
478
- _tx_buffer = new cbuf (SERIAL_TX_BUFFER_SIZE);
493
+ if (!_tx_buffer)
494
+ _tx_buffer = new cbuf (SERIAL_TX_BUFFER_SIZE);
479
495
}
480
496
_written = false ;
481
497
delay (1 );
482
498
}
483
499
484
- void ICACHE_FLASH_ATTR HardwareSerial::end () {
500
+ void HardwareSerial::end () {
485
501
if (uart_get_debug () == _uart_nr) {
486
502
uart_set_debug (UART_NO);
487
503
}
@@ -493,13 +509,13 @@ void ICACHE_FLASH_ATTR HardwareSerial::end() {
493
509
_tx_buffer = 0 ;
494
510
}
495
511
496
- void ICACHE_FLASH_ATTR HardwareSerial::swap () {
512
+ void HardwareSerial::swap () {
497
513
if (_uart == 0 )
498
514
return ;
499
515
uart_swap (_uart);
500
516
}
501
517
502
- void ICACHE_FLASH_ATTR HardwareSerial::setDebugOutput (bool en) {
518
+ void HardwareSerial::setDebugOutput (bool en) {
503
519
if (_uart == 0 )
504
520
return ;
505
521
if (en) {
@@ -512,19 +528,19 @@ void ICACHE_FLASH_ATTR HardwareSerial::setDebugOutput(bool en) {
512
528
}
513
529
}
514
530
515
- bool ICACHE_FLASH_ATTR HardwareSerial::isTxEnabled (void ) {
531
+ bool HardwareSerial::isTxEnabled (void ) {
516
532
if (_uart == 0 )
517
533
return false ;
518
534
return _uart->txEnabled ;
519
535
}
520
536
521
- bool ICACHE_FLASH_ATTR HardwareSerial::isRxEnabled (void ) {
537
+ bool HardwareSerial::isRxEnabled (void ) {
522
538
if (_uart == 0 )
523
539
return false ;
524
540
return _uart->rxEnabled ;
525
541
}
526
542
527
- int ICACHE_FLASH_ATTR HardwareSerial::available (void ) {
543
+ int HardwareSerial::available (void ) {
528
544
if (_uart == 0 )
529
545
return 0 ;
530
546
if (_uart->rxEnabled ) {
@@ -534,7 +550,7 @@ int ICACHE_FLASH_ATTR HardwareSerial::available(void) {
534
550
}
535
551
}
536
552
537
- int ICACHE_FLASH_ATTR HardwareSerial::peek (void ) {
553
+ int HardwareSerial::peek (void ) {
538
554
if (_uart == 0 )
539
555
return -1 ;
540
556
if (_uart->rxEnabled ) {
@@ -544,7 +560,7 @@ int ICACHE_FLASH_ATTR HardwareSerial::peek(void) {
544
560
}
545
561
}
546
562
547
- int ICACHE_FLASH_ATTR HardwareSerial::read (void ) {
563
+ int HardwareSerial::read (void ) {
548
564
if (_uart == 0 )
549
565
return -1 ;
550
566
if (_uart->rxEnabled ) {
@@ -554,7 +570,7 @@ int ICACHE_FLASH_ATTR HardwareSerial::read(void) {
554
570
}
555
571
}
556
572
557
- int ICACHE_FLASH_ATTR HardwareSerial::availableForWrite (void ) {
573
+ int HardwareSerial::availableForWrite (void ) {
558
574
if (_uart == 0 )
559
575
return 0 ;
560
576
if (_uart->txEnabled ) {
@@ -564,7 +580,7 @@ int ICACHE_FLASH_ATTR HardwareSerial::availableForWrite(void) {
564
580
}
565
581
}
566
582
567
- void ICACHE_FLASH_ATTR HardwareSerial::flush () {
583
+ void HardwareSerial::flush () {
568
584
if (_uart == 0 )
569
585
return ;
570
586
if (!_uart->txEnabled )
@@ -578,7 +594,7 @@ void ICACHE_FLASH_ATTR HardwareSerial::flush() {
578
594
_written = false ;
579
595
}
580
596
581
- size_t ICACHE_FLASH_ATTR HardwareSerial::write (uint8_t c) {
597
+ size_t HardwareSerial::write (uint8_t c) {
582
598
if (_uart == 0 || !_uart->txEnabled )
583
599
return 0 ;
584
600
_written = true ;
@@ -599,17 +615,17 @@ size_t ICACHE_FLASH_ATTR HardwareSerial::write(uint8_t c) {
599
615
return 1 ;
600
616
}
601
617
602
- ICACHE_FLASH_ATTR HardwareSerial::operator bool () const {
618
+ HardwareSerial::operator bool () const {
603
619
return _uart != 0 ;
604
620
}
605
621
606
- void ICACHE_FLASH_ATTR HardwareSerial::_rx_complete_irq (char c) {
622
+ void HardwareSerial::_rx_complete_irq (char c) {
607
623
if (_rx_buffer) {
608
624
_rx_buffer->write (c);
609
625
}
610
626
}
611
627
612
- void ICACHE_FLASH_ATTR HardwareSerial::_tx_empty_irq (void ) {
628
+ void HardwareSerial::_tx_empty_irq (void ) {
613
629
if (_uart == 0 )
614
630
return ;
615
631
if (_tx_buffer == 0 )
0 commit comments