HardwareSerial.cpp 6.2 KB
Newer Older
1 2 3 4 5
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>

6
#include "pins_arduino.h"
7
#include "HardwareSerial.h"
8
#include "soc/soc_caps.h"
9

10
#ifndef SOC_RX0
11
#if CONFIG_IDF_TARGET_ESP32
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
#define SOC_RX0 3
#elif CONFIG_IDF_TARGET_ESP32S2
#define SOC_RX0 44
#elif CONFIG_IDF_TARGET_ESP32C3
#define SOC_RX0 20
#endif
#endif

#ifndef SOC_TX0
#if CONFIG_IDF_TARGET_ESP32
#define SOC_TX0 1
#elif CONFIG_IDF_TARGET_ESP32S2
#define SOC_TX0 43
#elif CONFIG_IDF_TARGET_ESP32C3
#define SOC_TX0 21
#endif
#endif

void serialEvent(void) __attribute__((weak));
void serialEvent(void) {}

#if SOC_UART_NUM > 1
34

35
#ifndef RX1
36
#if CONFIG_IDF_TARGET_ESP32
37
#define RX1 9
38 39 40 41 42
#elif CONFIG_IDF_TARGET_ESP32S2
#define RX1 18
#elif CONFIG_IDF_TARGET_ESP32C3
#define RX1 18
#endif
43 44 45
#endif

#ifndef TX1
46
#if CONFIG_IDF_TARGET_ESP32
47
#define TX1 10
48 49 50 51
#elif CONFIG_IDF_TARGET_ESP32S2
#define TX1 17
#elif CONFIG_IDF_TARGET_ESP32C3
#define TX1 19
52
#endif
53 54 55 56 57
#endif

void serialEvent1(void) __attribute__((weak));
void serialEvent1(void) {}
#endif /* SOC_UART_NUM > 1 */
58

59
#if SOC_UART_NUM > 2
60
#ifndef RX2
61
#if CONFIG_IDF_TARGET_ESP32
62 63
#define RX2 16
#endif
64
#endif
65 66

#ifndef TX2
67
#if CONFIG_IDF_TARGET_ESP32
68 69
#define TX2 17
#endif
70 71
#endif

72 73 74
void serialEvent2(void) __attribute__((weak));
void serialEvent2(void) {}
#endif /* SOC_UART_NUM > 2 */
75

76
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SERIAL)
77
#if ARDUINO_USB_CDC_ON_BOOT //Serial used for USB CDC
78
HardwareSerial Serial0(0);
79 80
#elif ARDUINO_HW_CDC_ON_BOOT
HardwareSerial Serial0(0);
81
#else
82
HardwareSerial Serial(0);
83
#endif
84
#if SOC_UART_NUM > 1
85
HardwareSerial Serial1(1);
86 87
#endif
#if SOC_UART_NUM > 2
88
HardwareSerial Serial2(2);
89
#endif
90
#endif
91

92 93 94 95
void serialEventRun(void)
{
#if ARDUINO_USB_CDC_ON_BOOT //Serial used for USB CDC
    if(Serial0.available()) serialEvent();
96 97
#elif ARDUINO_HW_CDC_ON_BOOT
    if(Serial0.available()) serialEvent();
98 99 100 101 102 103 104 105 106 107 108 109
#else
    if(Serial.available()) serialEvent();
#endif
#if SOC_UART_NUM > 1
    if(Serial1.available()) serialEvent1();
#endif
#if SOC_UART_NUM > 2
    if(Serial2.available()) serialEvent2();
#endif
}


110
HardwareSerial::HardwareSerial(int uart_nr) : _uart_nr(uart_nr), _uart(NULL), _rxBufferSize(256) {}
111

112
void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, int8_t txPin, bool invert, unsigned long timeout_ms, uint8_t rxfifo_full_thrhd)
113
{
114 115
    if(0 > _uart_nr || _uart_nr >= SOC_UART_NUM) {
        log_e("Serial number is invalid, please use numers from 0 to %u", SOC_UART_NUM - 1);
116 117 118
        return;
    }
    if(_uart) {
119 120 121
        // in this case it is a begin() over a previous begin() - maybe to change baud rate
        // thus do not disable debug output
        end(false);
122 123
    }
    if(_uart_nr == 0 && rxPin < 0 && txPin < 0) {
124 125
        rxPin = SOC_RX0;
        txPin = SOC_TX0;
126
    }
127
#if SOC_UART_NUM > 1
128
    if(_uart_nr == 1 && rxPin < 0 && txPin < 0) {
129 130
        rxPin = RX1;
        txPin = TX1;
131
    }
132 133
#endif
#if SOC_UART_NUM > 2
134
    if(_uart_nr == 2 && rxPin < 0 && txPin < 0) {
135 136
        rxPin = RX2;
        txPin = TX2;
137
    }
138
#endif
139

140
    _uart = uartBegin(_uart_nr, baud ? baud : 9600, config, rxPin, txPin, _rxBufferSize, invert, rxfifo_full_thrhd);
141 142
    if (!baud) {
        // using baud rate as zero, forces it to try to detect the current baud rate in place
J
Jeroen88 已提交
143
        uartStartDetectBaudrate(_uart);
144
        time_t startMillis = millis();
145
        unsigned long detectedBaudRate = 0;
146 147 148 149
        while(millis() - startMillis < timeout_ms && !(detectedBaudRate = uartDetectBaudrate(_uart))) {
            yield();
        }

150
        end(false);
151 152 153

        if(detectedBaudRate) {
            delay(100); // Give some time...
154
            _uart = uartBegin(_uart_nr, detectedBaudRate, config, rxPin, txPin, _rxBufferSize, invert, rxfifo_full_thrhd);
155 156 157 158 159
        } else {
            log_e("Could not detect baudrate. Serial data at the port must be present within the timeout for detection to be possible");
            _uart = NULL;
        }
    }
160 161
}

162 163 164 165 166
void HardwareSerial::updateBaudRate(unsigned long baud)
{
	uartSetBaudRate(_uart, baud);
}

167
void HardwareSerial::end(bool turnOffDebug)
168
{
169
    if(turnOffDebug && uartGetDebug() == _uart_nr) {
170 171
        uartSetDebug(0);
    }
L
lbernstone 已提交
172
    delay(10);
173
    uartEnd(_uart);
174 175 176 177 178 179 180 181 182 183 184 185
    _uart = 0;
}

void HardwareSerial::setDebugOutput(bool en)
{
    if(_uart == 0) {
        return;
    }
    if(en) {
        uartSetDebug(_uart);
    } else {
        if(uartGetDebug() == _uart_nr) {
186
            uartSetDebug(NULL);
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
        }
    }
}

int HardwareSerial::available(void)
{
    return uartAvailable(_uart);
}
int HardwareSerial::availableForWrite(void)
{
    return uartAvailableForWrite(_uart);
}

int HardwareSerial::peek(void)
{
    if (available()) {
        return uartPeek(_uart);
    }
    return -1;
}

int HardwareSerial::read(void)
{
    if(available()) {
        return uartRead(_uart);
    }
    return -1;
}

216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
// read characters into buffer
// terminates if size characters have been read, or no further are pending
// returns the number of characters placed in the buffer
// the buffer is NOT null terminated.
size_t HardwareSerial::read(uint8_t *buffer, size_t size)
{
    size_t avail = available();
    if (size < avail) {
        avail = size;
    }
    size_t count = 0;
    while(count < avail) {
        *buffer++ = uartRead(_uart);
        count++;
    }
    return count;
}

C
chuck todd 已提交
234
void HardwareSerial::flush(void)
235 236 237 238
{
    uartFlush(_uart);
}

C
chuck todd 已提交
239 240 241 242 243
void HardwareSerial::flush(bool txOnly)
{
    uartFlushTxOnly(_uart, txOnly);
}

244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
size_t HardwareSerial::write(uint8_t c)
{
    uartWrite(_uart, c);
    return 1;
}

size_t HardwareSerial::write(const uint8_t *buffer, size_t size)
{
    uartWriteBuf(_uart, buffer, size);
    return size;
}
uint32_t  HardwareSerial::baudRate()

{
	return uartGetBaudRate(_uart);
}
HardwareSerial::operator bool() const
{
262
    return uartIsDriverInstalled(_uart);
263
}
264 265 266 267 268

void HardwareSerial::setRxInvert(bool invert)
{
    uartSetRxInvert(_uart, invert);
}
269 270 271 272 273 274

void HardwareSerial::setPins(uint8_t rxPin, uint8_t txPin)
{
    uartSetPins(_uart, rxPin, txPin);
}

275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
size_t HardwareSerial::setRxBufferSize(size_t new_size) {

    if (_uart) {
        log_e("RX Buffer can't be resized when Serial is already running.\n");
        return 0;
    }

    if (new_size <= SOC_UART_FIFO_LEN) {
        log_e("RX Buffer must be higher than %d.\n", SOC_UART_FIFO_LEN);
        return 0;
    }

    _rxBufferSize = new_size;
    return _rxBufferSize;
}