2016-10-06 13:21:30 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
|
|
|
|
#include "HardwareSerial.h"
|
|
|
|
|
2018-04-06 18:02:59 +02:00
|
|
|
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SERIAL)
|
2016-10-06 13:21:30 +02:00
|
|
|
HardwareSerial Serial(0);
|
2018-04-06 18:02:59 +02:00
|
|
|
#endif
|
2016-10-06 13:21:30 +02:00
|
|
|
|
|
|
|
HardwareSerial::HardwareSerial(int uart_nr) : _uart_nr(uart_nr), _uart(NULL) {}
|
|
|
|
|
2017-10-13 10:46:56 +02:00
|
|
|
void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, int8_t txPin, bool invert)
|
2016-10-06 13:21:30 +02:00
|
|
|
{
|
2017-10-04 05:12:21 +02:00
|
|
|
if(0 > _uart_nr || _uart_nr > 2) {
|
|
|
|
log_e("Serial number is invalid, please use 0, 1 or 2");
|
|
|
|
return;
|
|
|
|
}
|
2017-08-01 23:32:42 +02:00
|
|
|
if(_uart) {
|
|
|
|
end();
|
|
|
|
}
|
2016-10-06 13:21:30 +02:00
|
|
|
if(_uart_nr == 0 && rxPin < 0 && txPin < 0) {
|
|
|
|
rxPin = 3;
|
|
|
|
txPin = 1;
|
|
|
|
}
|
2016-11-13 16:01:44 +01:00
|
|
|
if(_uart_nr == 1 && rxPin < 0 && txPin < 0) {
|
|
|
|
rxPin = 9;
|
|
|
|
txPin = 10;
|
|
|
|
}
|
|
|
|
if(_uart_nr == 2 && rxPin < 0 && txPin < 0) {
|
|
|
|
rxPin = 16;
|
|
|
|
txPin = 17;
|
|
|
|
}
|
2017-10-13 10:46:56 +02:00
|
|
|
_uart = uartBegin(_uart_nr, baud, config, rxPin, txPin, 256, invert);
|
2016-10-06 13:21:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void HardwareSerial::end()
|
|
|
|
{
|
|
|
|
if(uartGetDebug() == _uart_nr) {
|
|
|
|
uartSetDebug(0);
|
|
|
|
}
|
|
|
|
uartEnd(_uart);
|
|
|
|
_uart = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void HardwareSerial::setDebugOutput(bool en)
|
|
|
|
{
|
|
|
|
if(_uart == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(en) {
|
2016-10-14 02:02:40 +02:00
|
|
|
uartSetDebug(_uart);
|
2016-10-06 13:21:30 +02:00
|
|
|
} else {
|
|
|
|
if(uartGetDebug() == _uart_nr) {
|
|
|
|
uartSetDebug(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int HardwareSerial::available(void)
|
|
|
|
{
|
2016-10-14 02:02:40 +02:00
|
|
|
return uartAvailable(_uart);
|
2016-10-06 13:21:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int HardwareSerial::peek(void)
|
|
|
|
{
|
2016-10-14 02:02:40 +02:00
|
|
|
if (available()) {
|
2016-10-06 13:21:30 +02:00
|
|
|
return uartPeek(_uart);
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int HardwareSerial::read(void)
|
|
|
|
{
|
2016-10-14 02:02:40 +02:00
|
|
|
if(available()) {
|
2016-10-06 13:21:30 +02:00
|
|
|
return uartRead(_uart);
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void HardwareSerial::flush()
|
|
|
|
{
|
|
|
|
uartFlush(_uart);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2017-09-21 11:00:12 +02:00
|
|
|
uint32_t HardwareSerial::baudRate()
|
2016-10-06 13:21:30 +02:00
|
|
|
|
2017-09-21 11:00:12 +02:00
|
|
|
{
|
|
|
|
return uartGetBaudRate(_uart);
|
|
|
|
}
|
2016-10-06 13:21:30 +02:00
|
|
|
HardwareSerial::operator bool() const
|
|
|
|
{
|
2017-03-20 10:26:32 +01:00
|
|
|
return true;
|
2016-10-06 13:21:30 +02:00
|
|
|
}
|