forked from esp8266/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathuart.c
789 lines (659 loc) · 18.2 KB
/
uart.c
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
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
/*
uart.cpp - esp8266 UART HAL
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
*/
/**
* 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
*
*/
#include "Arduino.h"
#include <pgmspace.h>
#include "uart.h"
#include "esp8266_peri.h"
#include "user_interface.h"
#include "uart_register.h"
const char overrun_str [] PROGMEM STORE_ATTR = "uart input full!\r\n";
static int s_uart_debug_nr = UART0;
struct uart_rx_buffer_
{
size_t size;
size_t rpos;
size_t wpos;
uint8_t * buffer;
};
struct uart_
{
int uart_nr;
int baud_rate;
bool rx_enabled;
bool tx_enabled;
bool overrun;
uint8_t rx_pin;
uint8_t tx_pin;
struct uart_rx_buffer_ * rx_buffer;
};
/*
In the context of the naming conventions in this file, "_unsafe" means two things:
1. the input arguments are not checked. It is up to the caller to check argument sanity.
2. The function body is not interrupt-safe, i.e.: the isr could fire anywhen during the
body execution, leading to corruption of the data shared between the body and the isr
(parts of the rx_buffer).
The unsafe versions of the functions are private to this TU. There are "safe" versions that
wrap the unsafe ones with disabling/enabling of the uart interrupt for safe public use.
*/
inline size_t
uart_rx_fifo_available(const int uart_nr)
{
return (USS(uart_nr) >> USRXC) & 0xFF;
}
/**********************************************************/
/************ UNSAFE FUNCTIONS ****************************/
/**********************************************************/
inline size_t
uart_rx_buffer_available_unsafe(const struct uart_rx_buffer_ * rx_buffer)
{
if(rx_buffer->wpos < rx_buffer->rpos)
return (rx_buffer->wpos + rx_buffer->size) - rx_buffer->rpos;
return rx_buffer->wpos - rx_buffer->rpos;
}
inline size_t
uart_rx_available_unsafe(uart_t* uart)
{
return uart_rx_buffer_available_unsafe(uart->rx_buffer) + uart_rx_fifo_available(uart->uart_nr);
}
//#define UART_DISCARD_NEWEST
// Copy all the rx fifo bytes that fit into the rx buffer
inline void
uart_rx_copy_fifo_to_buffer_unsafe(uart_t* uart)
{
struct uart_rx_buffer_ *rx_buffer = uart->rx_buffer;
while(uart_rx_fifo_available(uart->uart_nr))
{
size_t nextPos = (rx_buffer->wpos + 1) % rx_buffer->size;
if(nextPos == rx_buffer->rpos)
{
if (!uart->overrun)
{
uart->overrun = true;
os_printf_plus(overrun_str);
}
// a choice has to be made here,
// do we discard newest or oldest data?
#ifdef UART_DISCARD_NEWEST
// discard newest data
// Stop copying if rx buffer is full
USF(uart->uart_nr);
break;
#else
// discard oldest data
if (++rx_buffer->rpos == rx_buffer->size)
rx_buffer->rpos = 0;
#endif
}
uint8_t data = USF(uart->uart_nr);
rx_buffer->buffer[rx_buffer->wpos] = data;
rx_buffer->wpos = nextPos;
}
}
inline int
uart_peek_char_unsafe(uart_t* uart)
{
if (!uart_rx_available_unsafe(uart))
return -1;
//without the following if statement and body, there is a good chance of a fifo overrun
if (uart_rx_buffer_available_unsafe(uart->rx_buffer) == 0)
uart_rx_copy_fifo_to_buffer_unsafe(uart);
return uart->rx_buffer->buffer[uart->rx_buffer->rpos];
}
inline int
uart_read_char_unsafe(uart_t* uart)
{
int data = uart_peek_char_unsafe(uart);
if(data != -1)
uart->rx_buffer->rpos = (uart->rx_buffer->rpos + 1) % uart->rx_buffer->size;
return data;
}
/**********************************************************/
size_t
uart_rx_available(uart_t* uart)
{
if(uart == NULL || !uart->rx_enabled)
return 0;
ETS_UART_INTR_DISABLE();
int uartrxbufferavailable = uart_rx_buffer_available_unsafe(uart->rx_buffer);
ETS_UART_INTR_ENABLE();
return uartrxbufferavailable + uart_rx_fifo_available(uart->uart_nr);
}
int
uart_peek_char(uart_t* uart)
{
if(uart == NULL || !uart->rx_enabled)
return -1;
ETS_UART_INTR_DISABLE(); //access to rx_buffer can be interrupted by the isr (similar to a critical section), so disable interrupts here
int ret = uart_peek_char_unsafe(uart);
ETS_UART_INTR_ENABLE();
return ret;
}
int
uart_read_char(uart_t* uart)
{
if(uart == NULL || !uart->rx_enabled)
return -1;
ETS_UART_INTR_DISABLE();
int data = uart_read_char_unsafe(uart);
ETS_UART_INTR_ENABLE();
return data;
}
size_t
uart_resize_rx_buffer(uart_t* uart, size_t new_size)
{
if(uart == NULL || !uart->rx_enabled)
return 0;
if(uart->rx_buffer->size == new_size)
return uart->rx_buffer->size;
uint8_t * new_buf = (uint8_t*)malloc(new_size);
if(!new_buf)
return uart->rx_buffer->size;
size_t new_wpos = 0;
ETS_UART_INTR_DISABLE();
while(uart_rx_available_unsafe(uart) && new_wpos < new_size)
new_buf[new_wpos++] = uart_read_char_unsafe(uart); //if uart_rx_available_unsafe() returns non-0, uart_read_char_unsafe() can't return -1
uint8_t * old_buf = uart->rx_buffer->buffer;
uart->rx_buffer->rpos = 0;
uart->rx_buffer->wpos = new_wpos;
uart->rx_buffer->size = new_size;
uart->rx_buffer->buffer = new_buf;
ETS_UART_INTR_ENABLE();
free(old_buf);
return uart->rx_buffer->size;
}
void ICACHE_RAM_ATTR
uart_isr(void * arg)
{
uart_t* uart = (uart_t*)arg;
if(uart == NULL || !uart->rx_enabled)
{
USIC(uart->uart_nr) = USIS(uart->uart_nr);
ETS_UART_INTR_DISABLE();
return;
}
if(USIS(uart->uart_nr) & ((1 << UIFF) | (1 << UITO)))
uart_rx_copy_fifo_to_buffer_unsafe(uart);
USIC(uart->uart_nr) = USIS(uart->uart_nr);
}
static void
uart_start_isr(uart_t* uart)
{
if(uart == NULL || !uart->rx_enabled)
return;
// UCFFT value is when the RX fifo full interrupt triggers. A value of 1
// triggers the IRS very often. A value of 127 would not leave much time
// for ISR to clear fifo before the next byte is dropped. So pick a value
// in the middle.
USC1(uart->uart_nr) = (100 << UCFFT) | (0x02 << UCTOT) | (1 <<UCTOE );
USIC(uart->uart_nr) = 0xffff;
USIE(uart->uart_nr) = (1 << UIFF) | (1 << UIFR) | (1 << UITO);
ETS_UART_INTR_ATTACH(uart_isr, (void *)uart);
ETS_UART_INTR_ENABLE();
}
static void
uart_stop_isr(uart_t* uart)
{
if(uart == NULL || !uart->rx_enabled)
return;
ETS_UART_INTR_DISABLE();
USC1(uart->uart_nr) = 0;
USIC(uart->uart_nr) = 0xffff;
USIE(uart->uart_nr) = 0;
ETS_UART_INTR_ATTACH(NULL, NULL);
}
/*
Reference for uart_tx_fifo_available() and uart_tx_fifo_full():
-Espressif Techinical Reference doc, chapter 11.3.7
-tools/sdk/uart_register.h
-cores/esp8266/esp8266_peri.h
*/
inline size_t
uart_tx_fifo_available(const int uart_nr)
{
return (USS(uart_nr) >> USTXC) & 0xff;
}
inline bool
uart_tx_fifo_full(const int uart_nr)
{
return uart_tx_fifo_available(uart_nr) >= 0x7f;
}
static void
uart_do_write_char(const int uart_nr, char c)
{
while(uart_tx_fifo_full(uart_nr));
USF(uart_nr) = c;
}
size_t
uart_write_char(uart_t* uart, char c)
{
if(uart == NULL || !uart->tx_enabled)
return 0;
uart_do_write_char(uart->uart_nr, c);
return 1;
}
size_t
uart_write(uart_t* uart, const char* buf, size_t size)
{
if(uart == NULL || !uart->tx_enabled)
return 0;
size_t ret = size;
const int uart_nr = uart->uart_nr;
while (size--)
uart_do_write_char(uart_nr, *buf++);
return ret;
}
size_t
uart_tx_free(uart_t* uart)
{
if(uart == NULL || !uart->tx_enabled)
return 0;
return UART_TX_FIFO_SIZE - uart_tx_fifo_available(uart->uart_nr);
}
void
uart_wait_tx_empty(uart_t* uart)
{
if(uart == NULL || !uart->tx_enabled)
return;
while(uart_tx_fifo_available(uart->uart_nr) > 0)
delay(0);
}
void
uart_flush(uart_t* uart)
{
if(uart == NULL)
return;
uint32_t tmp = 0x00000000;
if(uart->rx_enabled)
{
tmp |= (1 << UCRXRST);
ETS_UART_INTR_DISABLE();
uart->rx_buffer->rpos = 0;
uart->rx_buffer->wpos = 0;
ETS_UART_INTR_ENABLE();
}
if(uart->tx_enabled)
tmp |= (1 << UCTXRST);
USC0(uart->uart_nr) |= (tmp);
USC0(uart->uart_nr) &= ~(tmp);
}
void
uart_set_baudrate(uart_t* uart, int baud_rate)
{
if(uart == NULL)
return;
uart->baud_rate = baud_rate;
USD(uart->uart_nr) = (ESP8266_CLOCK / uart->baud_rate);
}
int
uart_get_baudrate(uart_t* uart)
{
if(uart == NULL)
return 0;
return uart->baud_rate;
}
uart_t*
uart_init(int uart_nr, int baudrate, int config, int mode, int tx_pin, size_t rx_size)
{
uart_t* uart = (uart_t*) malloc(sizeof(uart_t));
if(uart == NULL)
return NULL;
uart->uart_nr = uart_nr;
uart->overrun = false;
switch(uart->uart_nr)
{
case UART0:
ETS_UART_INTR_DISABLE();
ETS_UART_INTR_ATTACH(NULL, NULL);
uart->rx_enabled = (mode != UART_TX_ONLY);
uart->tx_enabled = (mode != UART_RX_ONLY);
uart->rx_pin = (uart->rx_enabled)?3:255;
if(uart->rx_enabled)
{
struct uart_rx_buffer_ * rx_buffer = (struct uart_rx_buffer_ *)malloc(sizeof(struct uart_rx_buffer_));
if(rx_buffer == NULL)
{
free(uart);
return NULL;
}
rx_buffer->size = rx_size;//var this
rx_buffer->rpos = 0;
rx_buffer->wpos = 0;
rx_buffer->buffer = (uint8_t *)malloc(rx_buffer->size);
if(rx_buffer->buffer == NULL)
{
free(rx_buffer);
free(uart);
return NULL;
}
uart->rx_buffer = rx_buffer;
pinMode(uart->rx_pin, SPECIAL);
}
if(uart->tx_enabled)
{
if (tx_pin == 2)
{
uart->tx_pin = 2;
pinMode(uart->tx_pin, FUNCTION_4);
}
else
{
uart->tx_pin = 1;
pinMode(uart->tx_pin, FUNCTION_0);
}
}
else
{
uart->tx_pin = 255;
}
IOSWAP &= ~(1 << IOSWAPU0);
break;
case UART1:
// Note: uart_interrupt_handler does not support RX on UART 1.
uart->rx_enabled = false;
uart->tx_enabled = (mode != UART_RX_ONLY);
uart->rx_pin = 255;
uart->tx_pin = (uart->tx_enabled)?2:255; // GPIO7 as TX not possible! See GPIO pins used by UART
if(uart->tx_enabled)
pinMode(uart->tx_pin, SPECIAL);
break;
case UART_NO:
default:
// big fail!
free(uart);
return NULL;
}
uart_set_baudrate(uart, baudrate);
USC0(uart->uart_nr) = config;
uart_flush(uart);
USC1(uart->uart_nr) = 0;
USIC(uart->uart_nr) = 0xffff;
USIE(uart->uart_nr) = 0;
if(uart->uart_nr == UART0 && uart->rx_enabled)
uart_start_isr(uart);
return uart;
}
void
uart_uninit(uart_t* uart)
{
if(uart == NULL)
return;
uart_stop_isr(uart);
switch(uart->rx_pin)
{
case 3:
pinMode(3, INPUT);
break;
case 13:
pinMode(13, INPUT);
break;
}
switch(uart->tx_pin)
{
case 1:
pinMode(1, INPUT);
break;
case 2:
pinMode(2, INPUT);
break;
case 15:
pinMode(15, INPUT);
break;
}
if(uart->rx_enabled)
{
uart_stop_isr(uart);
free(uart->rx_buffer->buffer);
free(uart->rx_buffer);
}
free(uart);
}
void
uart_swap(uart_t* uart, int tx_pin)
{
if(uart == NULL)
return;
switch(uart->uart_nr)
{
case UART0:
if(((uart->tx_pin == 1 || uart->tx_pin == 2) && uart->tx_enabled) || (uart->rx_pin == 3 && uart->rx_enabled))
{
if(uart->tx_enabled) //TX
{
pinMode(uart->tx_pin, INPUT);
uart->tx_pin = 15;
}
if(uart->rx_enabled) //RX
{
pinMode(uart->rx_pin, INPUT);
uart->rx_pin = 13;
}
if(uart->tx_enabled)
pinMode(uart->tx_pin, FUNCTION_4); //TX
if(uart->rx_enabled)
pinMode(uart->rx_pin, FUNCTION_4); //RX
IOSWAP |= (1 << IOSWAPU0);
}
else
{
if(uart->tx_enabled) //TX
{
pinMode(uart->tx_pin, INPUT);
uart->tx_pin = (tx_pin == 2)?2:1;
}
if(uart->rx_enabled) //RX
{
pinMode(uart->rx_pin, INPUT);
uart->rx_pin = 3;
}
if(uart->tx_enabled)
pinMode(uart->tx_pin, (tx_pin == 2)?FUNCTION_4:SPECIAL); //TX
if(uart->rx_enabled)
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, int tx_pin)
{
if(uart == NULL)
return;
switch(uart->uart_nr)
{
case UART0:
if(uart->tx_enabled)
{
if (uart->tx_pin == 1 && tx_pin == 2)
{
pinMode(uart->tx_pin, INPUT);
uart->tx_pin = 2;
pinMode(uart->tx_pin, FUNCTION_4);
}
else if (uart->tx_pin == 2 && tx_pin != 2)
{
pinMode(uart->tx_pin, INPUT);
uart->tx_pin = 1;
pinMode(uart->tx_pin, 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, int tx, int rx)
{
if(uart == NULL)
return;
if(uart->uart_nr == UART0) // Only UART0 allows pin changes
{
if(uart->tx_enabled && uart->tx_pin != tx)
{
if( rx == 13 && tx == 15)
{
uart_swap(uart, 15);
}
else if (rx == 3 && (tx == 1 || tx == 2))
{
if (uart->rx_pin != rx)
uart_swap(uart, tx);
else
uart_set_tx(uart, tx);
}
}
if(uart->rx_enabled && uart->rx_pin != rx && rx == 13 && tx == 15)
uart_swap(uart, 15);
}
}
bool
uart_tx_enabled(uart_t* uart)
{
if(uart == NULL)
return false;
return uart->tx_enabled;
}
bool
uart_rx_enabled(uart_t* uart)
{
if(uart == NULL)
return false;
return uart->rx_enabled;
}
bool
uart_has_overrun (uart_t* uart)
{
if (uart == NULL || !uart->overrun)
return false;
// clear flag
uart->overrun = false;
return true;
}
static void
uart_ignore_char(char c)
{
(void) c;
}
inline void
uart_write_char_delay(const int uart_nr, char c)
{
while(uart_tx_fifo_full(uart_nr))
delay(0);
USF(uart_nr) = c;
}
static void
uart0_write_char(char c)
{
uart_write_char_delay(0, c);
}
static void
uart1_write_char(char c)
{
uart_write_char_delay(1, c);
}
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;
}
/*
To start detection of baud rate with the UART the UART_AUTOBAUD_EN bit needs to be cleared and set. The ROM function uart_baudrate_detect() does this only once, so on a next call the UartDev.rcv_state is not equal to BAUD_RATE_DET. Instead of poking around in the UartDev struct with unknown effect, the UART_AUTOBAUD_EN bit is directly triggered by the function uart_detect_baudrate().
*/
void
uart_start_detect_baudrate(int uart_nr)
{
USA(uart_nr) &= ~(UART_GLITCH_FILT << UART_GLITCH_FILT_S | UART_AUTOBAUD_EN);
USA(uart_nr) = 0x08 << UART_GLITCH_FILT_S | UART_AUTOBAUD_EN;
}
int
uart_detect_baudrate(int uart_nr)
{
static bool doTrigger = true;
if (doTrigger)
{
uart_start_detect_baudrate(uart_nr);
doTrigger = false;
}
int32_t divisor = uart_baudrate_detect(uart_nr, 1);
if (!divisor) {
return 0;
}
doTrigger = true; // Initialize for a next round
int32_t baudrate = UART_CLK_FREQ / divisor;
static const int default_rates[] = {300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 74880, 115200, 230400, 256000, 460800, 921600, 1843200, 3686400};
size_t i;
for (i = 1; i < sizeof(default_rates) / sizeof(default_rates[0]) - 1; i++) // find the nearest real baudrate
{
if (baudrate <= default_rates[i])
{
if (baudrate - default_rates[i - 1] < default_rates[i] - baudrate) {
i--;
}
break;
}
}
return default_rates[i];
}