2021-02-18 11:14:35 +01:00
|
|
|
// Copyright 2015-2021 Espressif Systems (Shanghai) PTE LTD
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2018-04-16 16:34:39 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
|
2018-09-21 08:39:36 +02:00
|
|
|
#include "pins_arduino.h"
|
2018-04-16 16:34:39 +02:00
|
|
|
#include "HardwareSerial.h"
|
|
|
|
|
2018-09-21 08:39:36 +02:00
|
|
|
#ifndef RX1
|
|
|
|
#define RX1 9
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef TX1
|
|
|
|
#define TX1 10
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef RX2
|
|
|
|
#define RX2 16
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef TX2
|
|
|
|
#define TX2 17
|
|
|
|
#endif
|
|
|
|
|
2018-04-16 16:34:39 +02:00
|
|
|
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SERIAL)
|
|
|
|
HardwareSerial Serial(0);
|
2018-07-03 20:41:03 +02:00
|
|
|
HardwareSerial Serial1(1);
|
|
|
|
HardwareSerial Serial2(2);
|
2018-04-16 16:34:39 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
HardwareSerial::HardwareSerial(int uart_nr) : _uart_nr(uart_nr), _uart(NULL) {}
|
|
|
|
|
2018-11-19 16:51:55 +01:00
|
|
|
void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, int8_t txPin, bool invert, unsigned long timeout_ms)
|
2018-04-16 16:34:39 +02:00
|
|
|
{
|
|
|
|
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) {
|
|
|
|
rxPin = 3;
|
|
|
|
txPin = 1;
|
|
|
|
}
|
|
|
|
if(_uart_nr == 1 && rxPin < 0 && txPin < 0) {
|
2018-09-21 08:39:36 +02:00
|
|
|
rxPin = RX1;
|
|
|
|
txPin = TX1;
|
2018-04-16 16:34:39 +02:00
|
|
|
}
|
|
|
|
if(_uart_nr == 2 && rxPin < 0 && txPin < 0) {
|
2018-09-21 08:39:36 +02:00
|
|
|
rxPin = RX2;
|
|
|
|
txPin = TX2;
|
2018-04-16 16:34:39 +02:00
|
|
|
}
|
2018-11-19 16:51:55 +01:00
|
|
|
|
|
|
|
_uart = uartBegin(_uart_nr, baud ? baud : 9600, config, rxPin, txPin, 256, invert);
|
2020-09-30 14:04:18 +02:00
|
|
|
_tx_pin = txPin;
|
|
|
|
_rx_pin = rxPin;
|
2018-11-19 16:51:55 +01:00
|
|
|
|
|
|
|
if(!baud) {
|
2019-09-08 23:59:32 +02:00
|
|
|
uartStartDetectBaudrate(_uart);
|
2018-11-19 16:51:55 +01:00
|
|
|
time_t startMillis = millis();
|
2018-11-26 23:22:37 +01:00
|
|
|
unsigned long detectedBaudRate = 0;
|
2018-11-19 16:51:55 +01:00
|
|
|
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;
|
2020-09-30 14:04:18 +02:00
|
|
|
_tx_pin = 255;
|
|
|
|
_rx_pin = 255;
|
2018-11-19 16:51:55 +01:00
|
|
|
}
|
|
|
|
}
|
2018-04-16 16:34:39 +02:00
|
|
|
}
|
|
|
|
|
2018-12-23 20:15:06 +01:00
|
|
|
void HardwareSerial::updateBaudRate(unsigned long baud)
|
|
|
|
{
|
|
|
|
uartSetBaudRate(_uart, baud);
|
|
|
|
}
|
|
|
|
|
2018-04-16 16:34:39 +02:00
|
|
|
void HardwareSerial::end()
|
|
|
|
{
|
|
|
|
if(uartGetDebug() == _uart_nr) {
|
|
|
|
uartSetDebug(0);
|
|
|
|
}
|
2020-09-30 14:04:18 +02:00
|
|
|
log_v("pins %d %d",_tx_pin, _rx_pin);
|
|
|
|
uartEnd(_uart, _tx_pin, _rx_pin);
|
2018-04-16 16:34:39 +02:00
|
|
|
_uart = 0;
|
|
|
|
}
|
|
|
|
|
2018-09-17 21:16:18 +02:00
|
|
|
size_t HardwareSerial::setRxBufferSize(size_t new_size) {
|
|
|
|
return uartResizeRxBuffer(_uart, new_size);
|
|
|
|
}
|
|
|
|
|
2018-04-16 16:34:39 +02:00
|
|
|
void HardwareSerial::setDebugOutput(bool en)
|
|
|
|
{
|
|
|
|
if(_uart == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(en) {
|
|
|
|
uartSetDebug(_uart);
|
|
|
|
} else {
|
|
|
|
if(uartGetDebug() == _uart_nr) {
|
|
|
|
uartSetDebug(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-01-20 14:54:50 +01:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2019-11-11 15:37:35 +01:00
|
|
|
void HardwareSerial::flush(void)
|
2018-04-16 16:34:39 +02:00
|
|
|
{
|
|
|
|
uartFlush(_uart);
|
|
|
|
}
|
|
|
|
|
2019-11-11 15:37:35 +01:00
|
|
|
void HardwareSerial::flush(bool txOnly)
|
|
|
|
{
|
|
|
|
uartFlushTxOnly(_uart, txOnly);
|
|
|
|
}
|
|
|
|
|
2018-04-16 16:34:39 +02:00
|
|
|
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;
|
|
|
|
}
|
2020-10-01 12:58:48 +02:00
|
|
|
|
|
|
|
void HardwareSerial::setRxInvert(bool invert)
|
|
|
|
{
|
|
|
|
uartSetRxInvert(_uart, invert);
|
|
|
|
}
|