forked from esp8266/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HardwareSerial.cpp
189 lines (156 loc) · 4.18 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
/*
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 "HardwareSerial.h"
HardwareSerial::HardwareSerial(int uart_nr)
: _uart_nr(uart_nr)
{}
void HardwareSerial::begin(unsigned long baud, SerialConfig config, SerialMode mode, uint8_t tx_pin)
{
if(uart_get_debug() == _uart_nr) {
uart_set_debug(UART_NO);
}
if (_uart) {
free(_uart);
}
_uart = uart_init(_uart_nr, baud, (int) config, (int) mode, tx_pin);
_peek_char = -1;
}
void HardwareSerial::end()
{
if(uart_get_debug() == _uart_nr) {
uart_set_debug(UART_NO);
}
uart_uninit(_uart);
}
void HardwareSerial::swap(uint8_t tx_pin)
{
if(!_uart) {
return;
}
uart_swap(_uart, tx_pin);
}
void HardwareSerial::set_tx(uint8_t tx_pin)
{
if(!_uart) {
return;
}
uart_set_tx(_uart, tx_pin);
}
void HardwareSerial::pins(uint8_t tx, uint8_t rx)
{
if(!_uart) {
return;
}
uart_set_pins(_uart, tx, rx);
}
void HardwareSerial::setDebugOutput(bool en)
{
if(!_uart) {
return;
}
if(en) {
if(uart_tx_enabled(_uart)) {
uart_set_debug(_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 HardwareSerial::isTxEnabled(void)
{
return _uart && uart_tx_enabled(_uart);
}
bool HardwareSerial::isRxEnabled(void)
{
return _uart && uart_rx_enabled(_uart);
}
int HardwareSerial::available(void)
{
if(!_uart || !uart_rx_enabled(_uart)) {
return 0;
}
int result = static_cast<int>(uart_rx_available(_uart));
if (_peek_char != -1) {
result += 1;
}
if (!result) {
optimistic_yield(10000);
}
return result;
}
int HardwareSerial::peek(void)
{
if (_peek_char != -1) {
return _peek_char;
}
// this may return -1, but that's okay
_peek_char = uart_read_char(_uart);
return _peek_char;
}
int HardwareSerial::read(void)
{
if(!_uart || !uart_rx_enabled(_uart)) {
return -1;
}
if (_peek_char != -1) {
auto tmp = _peek_char;
_peek_char = -1;
return tmp;
}
return uart_read_char(_uart);
}
int HardwareSerial::availableForWrite(void)
{
if(!_uart || !uart_tx_enabled(_uart)) {
return 0;
}
return static_cast<int>(uart_tx_free(_uart));
}
void HardwareSerial::flush()
{
if(!_uart || !uart_tx_enabled(_uart)) {
return;
}
uart_wait_tx_empty(_uart);
}
size_t HardwareSerial::write(uint8_t c)
{
if(!_uart || !uart_tx_enabled(_uart)) {
return 0;
}
uart_write_char(_uart, c);
return 1;
}
HardwareSerial::operator bool() const
{
return _uart != 0;
}
HardwareSerial Serial(UART0);
HardwareSerial Serial1(UART1);