|
1 | | -/* |
2 | | - HardwareSerial.cpp - esp8266 UART support |
3 | | -
|
4 | | - Copyright (c) 2014 Ivan Grokhotkov. All rights reserved. |
5 | | - This file is part of the esp8266 core for Arduino environment. |
6 | | -
|
7 | | - This library is free software; you can redistribute it and/or |
8 | | - modify it under the terms of the GNU Lesser General Public |
9 | | - License as published by the Free Software Foundation; either |
10 | | - version 2.1 of the License, or (at your option) any later version. |
11 | | -
|
12 | | - This library is distributed in the hope that it will be useful, |
13 | | - but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 | | - Lesser General Public License for more details. |
16 | | -
|
17 | | - You should have received a copy of the GNU Lesser General Public |
18 | | - License along with this library; if not, write to the Free Software |
19 | | - Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
20 | | -
|
21 | | - Modified 31 March 2015 by Markus Sattler (rewrite the code for UART0 + UART1 support in ESP8266) |
22 | | - Modified 25 April 2015 by Thomas Flayols (add configuration different from 8N1 in ESP8266) |
23 | | - Modified 3 May 2015 by Hristo Gochkov (change register access methods) |
24 | | - */ |
25 | | - |
26 | | -#include <stdlib.h> |
27 | | -#include <stdio.h> |
28 | | -#include <string.h> |
29 | | -#include <inttypes.h> |
30 | | -#include <PolledTimeout.h> |
31 | | -#include "Arduino.h" |
32 | | -#include "HardwareSerial.h" |
33 | | -#include "Esp.h" |
34 | | - |
35 | | -HardwareSerial::HardwareSerial(int uart_nr) |
36 | | - : _uart_nr(uart_nr), _rx_size(256) |
37 | | -{} |
38 | | - |
39 | | -void HardwareSerial::begin(unsigned long baud, SerialConfig config, SerialMode mode, uint8_t tx_pin) |
40 | | -{ |
41 | | - end(); |
42 | | - _uart = uart_init(_uart_nr, baud, (int) config, (int) mode, tx_pin, _rx_size); |
43 | | -#if defined(DEBUG_ESP_PORT) && !defined(NDEBUG) |
44 | | - if (static_cast<void*>(this) == static_cast<void*>(&DEBUG_ESP_PORT)) |
45 | | - { |
46 | | - setDebugOutput(true); |
47 | | - println(); |
48 | | - println(ESP.getFullVersion()); |
49 | | - } |
50 | | -#endif |
51 | | -} |
52 | | - |
53 | | -void HardwareSerial::end() |
54 | | -{ |
55 | | - if(uart_get_debug() == _uart_nr) { |
56 | | - uart_set_debug(UART_NO); |
57 | | - } |
58 | | - |
59 | | - uart_uninit(_uart); |
60 | | - _uart = NULL; |
61 | | -} |
62 | | - |
63 | | -void HardwareSerial::updateBaudRate(unsigned long baud) |
64 | | -{ |
65 | | - if(!_uart) { |
66 | | - return; |
67 | | - } |
68 | | - |
69 | | - uart_set_baudrate(_uart, baud); |
70 | | -} |
71 | | - |
72 | | -size_t HardwareSerial::setRxBufferSize(size_t size){ |
73 | | - if(_uart) { |
74 | | - _rx_size = uart_resize_rx_buffer(_uart, size); |
75 | | - } else { |
76 | | - _rx_size = size; |
77 | | - } |
78 | | - return _rx_size; |
79 | | -} |
80 | | - |
81 | | -void HardwareSerial::setDebugOutput(bool en) |
82 | | -{ |
83 | | - if(!_uart) { |
84 | | - return; |
85 | | - } |
86 | | - if(en) { |
87 | | - if(uart_tx_enabled(_uart)) { |
88 | | - uart_set_debug(_uart_nr); |
89 | | - } else { |
90 | | - uart_set_debug(UART_NO); |
91 | | - } |
92 | | - } else { |
93 | | - // disable debug for this interface |
94 | | - if(uart_get_debug() == _uart_nr) { |
95 | | - uart_set_debug(UART_NO); |
96 | | - } |
97 | | - } |
98 | | -} |
99 | | - |
100 | | -int HardwareSerial::available(void) |
101 | | -{ |
102 | | - int result = static_cast<int>(uart_rx_available(_uart)); |
103 | | - if (!result) { |
104 | | - optimistic_yield(10000); |
105 | | - } |
106 | | - return result; |
107 | | -} |
108 | | - |
109 | | -void HardwareSerial::flush() |
110 | | -{ |
| 1 | +/* |
| 2 | + HardwareSerial.cpp - esp8266 UART support |
| 3 | +
|
| 4 | + Copyright (c) 2014 Ivan Grokhotkov. All rights reserved. |
| 5 | + This file is part of the esp8266 core for Arduino environment. |
| 6 | +
|
| 7 | + This library is free software; you can redistribute it and/or |
| 8 | + modify it under the terms of the GNU Lesser General Public |
| 9 | + License as published by the Free Software Foundation; either |
| 10 | + version 2.1 of the License, or (at your option) any later version. |
| 11 | +
|
| 12 | + This library is distributed in the hope that it will be useful, |
| 13 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | + Lesser General Public License for more details. |
| 16 | +
|
| 17 | + You should have received a copy of the GNU Lesser General Public |
| 18 | + License along with this library; if not, write to the Free Software |
| 19 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | +
|
| 21 | + Modified 31 March 2015 by Markus Sattler (rewrite the code for UART0 + UART1 support in ESP8266) |
| 22 | + Modified 25 April 2015 by Thomas Flayols (add configuration different from 8N1 in ESP8266) |
| 23 | + Modified 3 May 2015 by Hristo Gochkov (change register access methods) |
| 24 | + */ |
| 25 | + |
| 26 | +#include <stdlib.h> |
| 27 | +#include <stdio.h> |
| 28 | +#include <string.h> |
| 29 | +#include <inttypes.h> |
| 30 | +#include <PolledTimeout.h> |
| 31 | +#include "Arduino.h" |
| 32 | +#include "HardwareSerial.h" |
| 33 | +#include "Esp.h" |
| 34 | + |
| 35 | +HardwareSerial::HardwareSerial(int uart_nr) |
| 36 | + : _uart_nr(uart_nr), _rx_size(256) |
| 37 | +{} |
| 38 | + |
| 39 | +void HardwareSerial::begin(unsigned long baud, SerialConfig config, SerialMode mode, uint8_t tx_pin) |
| 40 | +{ |
| 41 | + end(); |
| 42 | + _uart = uart_init(_uart_nr, baud, (int) config, (int) mode, tx_pin, _rx_size); |
| 43 | +#if defined(DEBUG_ESP_PORT) && !defined(NDEBUG) |
| 44 | + if (static_cast<void*>(this) == static_cast<void*>(&DEBUG_ESP_PORT)) |
| 45 | + { |
| 46 | + setDebugOutput(true); |
| 47 | + println(); |
| 48 | + println(ESP.getFullVersion()); |
| 49 | + } |
| 50 | +#endif |
| 51 | +} |
| 52 | + |
| 53 | +void HardwareSerial::end() |
| 54 | +{ |
| 55 | + if(uart_get_debug() == _uart_nr) { |
| 56 | + uart_set_debug(UART_NO); |
| 57 | + } |
| 58 | + |
| 59 | + uart_uninit(_uart); |
| 60 | + _uart = NULL; |
| 61 | +} |
| 62 | + |
| 63 | +void HardwareSerial::updateBaudRate(unsigned long baud) |
| 64 | +{ |
| 65 | + if(!_uart) { |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + uart_set_baudrate(_uart, baud); |
| 70 | +} |
| 71 | + |
| 72 | +size_t HardwareSerial::setRxBufferSize(size_t size){ |
| 73 | + if(_uart) { |
| 74 | + _rx_size = uart_resize_rx_buffer(_uart, size); |
| 75 | + } else { |
| 76 | + _rx_size = size; |
| 77 | + } |
| 78 | + return _rx_size; |
| 79 | +} |
| 80 | + |
| 81 | +void HardwareSerial::setDebugOutput(bool en) |
| 82 | +{ |
| 83 | + if(!_uart) { |
| 84 | + return; |
| 85 | + } |
| 86 | + if(en) { |
| 87 | + if(uart_tx_enabled(_uart)) { |
| 88 | + uart_set_debug(_uart_nr); |
| 89 | + } else { |
| 90 | + uart_set_debug(UART_NO); |
| 91 | + } |
| 92 | + } else { |
| 93 | + // disable debug for this interface |
| 94 | + if(uart_get_debug() == _uart_nr) { |
| 95 | + uart_set_debug(UART_NO); |
| 96 | + } |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +int HardwareSerial::available(void) |
| 101 | +{ |
| 102 | + int result = static_cast<int>(uart_rx_available(_uart)); |
| 103 | + if (!result) { |
| 104 | + optimistic_yield(10000); |
| 105 | + } |
| 106 | + return result; |
| 107 | +} |
| 108 | + |
| 109 | +void HardwareSerial::flush() |
| 110 | +{ |
111 | 111 | uint8_t bit_length = 0; |
112 | | - if(!_uart || !uart_tx_enabled(_uart)) { |
113 | | - return; |
114 | | - } |
115 | | - |
| 112 | + if(!_uart || !uart_tx_enabled(_uart)) { |
| 113 | + return; |
| 114 | + } |
| 115 | + |
116 | 116 | bit_length = uart_get_bit_length(_uart_nr); // data width, parity and stop |
117 | | - uart_wait_tx_empty(_uart); |
118 | | - //Workaround for a bug in serial not actually being finished yet |
119 | | - //Wait for 8 data bits, 1 parity and 2 stop bits, just in case |
120 | | - delayMicroseconds(bit_length * 1000000 / uart_get_baudrate(_uart) + 1); |
121 | | -} |
122 | | - |
123 | | -void HardwareSerial::startDetectBaudrate() |
124 | | -{ |
125 | | - uart_start_detect_baudrate(_uart_nr); |
126 | | -} |
127 | | - |
128 | | -unsigned long HardwareSerial::testBaudrate() |
129 | | -{ |
130 | | - return uart_detect_baudrate(_uart_nr); |
131 | | -} |
132 | | - |
133 | | -unsigned long HardwareSerial::detectBaudrate(time_t timeoutMillis) |
134 | | -{ |
135 | | - esp8266::polledTimeout::oneShotFastMs timeOut(timeoutMillis); |
136 | | - unsigned long detectedBaudrate; |
137 | | - while (!timeOut) { |
138 | | - if ((detectedBaudrate = testBaudrate())) { |
139 | | - break; |
140 | | - } |
141 | | - yield(); |
142 | | - delay(100); |
143 | | - } |
144 | | - return detectedBaudrate; |
145 | | -} |
146 | | - |
147 | | -size_t HardwareSerial::readBytes(char* buffer, size_t size) |
148 | | -{ |
149 | | - size_t got = 0; |
150 | | - |
151 | | - while (got < size) |
152 | | - { |
153 | | - esp8266::polledTimeout::oneShotFastMs timeOut(_timeout); |
154 | | - size_t avail; |
155 | | - while ((avail = available()) == 0 && !timeOut); |
156 | | - if (avail == 0) |
157 | | - break; |
158 | | - got += read(buffer + got, std::min(size - got, avail)); |
159 | | - } |
160 | | - return got; |
161 | | -} |
162 | | - |
163 | | -#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SERIAL) |
164 | | -HardwareSerial Serial(UART0); |
165 | | -#endif |
166 | | -#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SERIAL1) |
167 | | -HardwareSerial Serial1(UART1); |
168 | | -#endif |
| 117 | + uart_wait_tx_empty(_uart); |
| 118 | + //Workaround for a bug in serial not actually being finished yet |
| 119 | + //Wait for 8 data bits, 1 parity and 2 stop bits, just in case |
| 120 | + delayMicroseconds(bit_length * 1000000 / uart_get_baudrate(_uart) + 1); |
| 121 | +} |
| 122 | + |
| 123 | +void HardwareSerial::startDetectBaudrate() |
| 124 | +{ |
| 125 | + uart_start_detect_baudrate(_uart_nr); |
| 126 | +} |
| 127 | + |
| 128 | +unsigned long HardwareSerial::testBaudrate() |
| 129 | +{ |
| 130 | + return uart_detect_baudrate(_uart_nr); |
| 131 | +} |
| 132 | + |
| 133 | +unsigned long HardwareSerial::detectBaudrate(time_t timeoutMillis) |
| 134 | +{ |
| 135 | + esp8266::polledTimeout::oneShotFastMs timeOut(timeoutMillis); |
| 136 | + unsigned long detectedBaudrate; |
| 137 | + while (!timeOut) { |
| 138 | + if ((detectedBaudrate = testBaudrate())) { |
| 139 | + break; |
| 140 | + } |
| 141 | + yield(); |
| 142 | + delay(100); |
| 143 | + } |
| 144 | + return detectedBaudrate; |
| 145 | +} |
| 146 | + |
| 147 | +size_t HardwareSerial::readBytes(char* buffer, size_t size) |
| 148 | +{ |
| 149 | + size_t got = 0; |
| 150 | + |
| 151 | + while (got < size) |
| 152 | + { |
| 153 | + esp8266::polledTimeout::oneShotFastMs timeOut(_timeout); |
| 154 | + size_t avail; |
| 155 | + while ((avail = available()) == 0 && !timeOut); |
| 156 | + if (avail == 0) |
| 157 | + break; |
| 158 | + got += read(buffer + got, std::min(size - got, avail)); |
| 159 | + } |
| 160 | + return got; |
| 161 | +} |
| 162 | + |
| 163 | +#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SERIAL) |
| 164 | +HardwareSerial Serial(UART0); |
| 165 | +#endif |
| 166 | +#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SERIAL1) |
| 167 | +HardwareSerial Serial1(UART1); |
| 168 | +#endif |
0 commit comments