HardwareSerial.cpp 4.3 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 8
#include "HardwareSerial.h"

9 10
#if CONFIG_IDF_TARGET_ESP32

11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
#ifndef RX1
#define RX1 9
#endif

#ifndef TX1
#define TX1 10
#endif

#ifndef RX2
#define RX2 16
#endif

#ifndef TX2
#define TX2 17
#endif

27 28 29 30 31 32 33 34 35 36 37 38
#else

#ifndef RX1
#define RX1 18
#endif

#ifndef TX1
#define TX1 17
#endif

#endif

39
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SERIAL)
40 41 42
#if ARDUINO_SERIAL_PORT //Serial used for USB CDC
HardwareSerial Serial0(0);
#else
43
HardwareSerial Serial(0);
44
#endif
45
HardwareSerial Serial1(1);
46
#if CONFIG_IDF_TARGET_ESP32
47
HardwareSerial Serial2(2);
48
#endif
49
#endif
50 51 52

HardwareSerial::HardwareSerial(int uart_nr) : _uart_nr(uart_nr), _uart(NULL) {}

53
void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, int8_t txPin, bool invert, unsigned long timeout_ms)
54 55 56 57 58 59 60 61 62
{
    if(0 > _uart_nr || _uart_nr > 2) {
        log_e("Serial number is invalid, please use 0, 1 or 2");
        return;
    }
    if(_uart) {
        end();
    }
    if(_uart_nr == 0 && rxPin < 0 && txPin < 0) {
63
#if CONFIG_IDF_TARGET_ESP32
64 65
        rxPin = 3;
        txPin = 1;
66 67 68 69
#elif CONFIG_IDF_TARGET_ESP32S2
        rxPin = 44;
        txPin = 43;
#endif
70 71
    }
    if(_uart_nr == 1 && rxPin < 0 && txPin < 0) {
72 73
        rxPin = RX1;
        txPin = TX1;
74
    }
75
#if CONFIG_IDF_TARGET_ESP32
76
    if(_uart_nr == 2 && rxPin < 0 && txPin < 0) {
77 78
        rxPin = RX2;
        txPin = TX2;
79
    }
80
#endif
81
    _uart = uartBegin(_uart_nr, baud ? baud : 9600, config, rxPin, txPin, 256, invert);
82 83
    _tx_pin = txPin;
    _rx_pin = rxPin;
84 85

    if(!baud) {
J
Jeroen88 已提交
86
        uartStartDetectBaudrate(_uart);
87
        time_t startMillis = millis();
88
        unsigned long detectedBaudRate = 0;
89 90 91 92 93 94 95 96 97 98 99 100
        while(millis() - startMillis < timeout_ms && !(detectedBaudRate = uartDetectBaudrate(_uart))) {
            yield();
        }

        end();

        if(detectedBaudRate) {
            delay(100); // Give some time...
            _uart = uartBegin(_uart_nr, detectedBaudRate, config, rxPin, txPin, 256, invert);
        } 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;
101 102
            _tx_pin = 255;
            _rx_pin = 255;
103 104
        }
    }
105 106
}

107 108 109 110 111
void HardwareSerial::updateBaudRate(unsigned long baud)
{
	uartSetBaudRate(_uart, baud);
}

112 113 114 115 116
void HardwareSerial::end()
{
    if(uartGetDebug() == _uart_nr) {
        uartSetDebug(0);
    }
117 118
    log_v("pins %d %d",_tx_pin, _rx_pin);
    uartEnd(_uart, _tx_pin, _rx_pin);
119 120 121
    _uart = 0;
}

122 123 124 125
size_t HardwareSerial::setRxBufferSize(size_t new_size) {
    return uartResizeRxBuffer(_uart, new_size);
}

126 127 128 129 130 131 132 133 134
void HardwareSerial::setDebugOutput(bool en)
{
    if(_uart == 0) {
        return;
    }
    if(en) {
        uartSetDebug(_uart);
    } else {
        if(uartGetDebug() == _uart_nr) {
135
            uartSetDebug(NULL);
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
        }
    }
}

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;
}

165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
// 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 已提交
183
void HardwareSerial::flush(void)
184 185 186 187
{
    uartFlush(_uart);
}

C
chuck todd 已提交
188 189 190 191 192
void HardwareSerial::flush(bool txOnly)
{
    uartFlushTxOnly(_uart, txOnly);
}

193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
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
{
    return true;
}
213 214 215 216 217

void HardwareSerial::setRxInvert(bool invert)
{
    uartSetRxInvert(_uart, invert);
}