mirror of
https://github.com/esp8266/Arduino.git
synced 2025-07-07 06:01:35 +03:00
rewrite HardwareSerial code for UART0 + UART1 support in ESP8266
This commit is contained in:
@ -1,26 +1,27 @@
|
||||
/*
|
||||
HardwareSerial.h - Hardware serial library for Wiring
|
||||
Copyright (c) 2006 Nicholas Zambetti. All right reserved.
|
||||
HardwareSerial.h - Hardware serial library for Wiring
|
||||
Copyright (c) 2006 Nicholas Zambetti. All right reserved.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
Modified 28 September 2010 by Mark Sproul
|
||||
Modified 14 August 2012 by Alarus
|
||||
Modified 3 December 2013 by Matthijs Kooijman
|
||||
Modified 18 December 2014 by Ivan Grokhotkov (esp8266 platform support)
|
||||
*/
|
||||
Modified 28 September 2010 by Mark Sproul
|
||||
Modified 14 August 2012 by Alarus
|
||||
Modified 3 December 2013 by Matthijs Kooijman
|
||||
Modified 18 December 2014 by Ivan Grokhotkov (esp8266 platform support)
|
||||
Modified 31 March 2015 by Markus Sattler (rewrite the code for UART0 + UART1 support in ESP8266)
|
||||
*/
|
||||
|
||||
#ifndef HardwareSerial_h
|
||||
#define HardwareSerial_h
|
||||
@ -59,44 +60,71 @@
|
||||
// #define SERIAL_8O2 0x3E
|
||||
|
||||
class cbuf;
|
||||
typedef struct uart_ uart_t;
|
||||
|
||||
class HardwareSerial : public Stream
|
||||
{
|
||||
public:
|
||||
HardwareSerial();
|
||||
typedef enum {
|
||||
UART0 = 0,
|
||||
UART1 = 1,
|
||||
UART_NO = 0xFF
|
||||
} UARTnr_t;
|
||||
|
||||
void begin(unsigned long baud) { begin(baud, 0); }
|
||||
void begin(unsigned long, uint8_t);
|
||||
void end();
|
||||
void swap(); //use GPIO13 and GPIO15 as RX and TX
|
||||
int available(void) override;
|
||||
int peek(void) override;
|
||||
int read(void) override;
|
||||
int availableForWrite(void);
|
||||
void flush(void) override;
|
||||
size_t write(uint8_t) override;
|
||||
inline size_t write(unsigned long n) { return write((uint8_t)n); }
|
||||
inline size_t write(long n) { return write((uint8_t)n); }
|
||||
inline size_t write(unsigned int n) { return write((uint8_t)n); }
|
||||
inline size_t write(int n) { return write((uint8_t)n); }
|
||||
using Print::write; // pull in write(str) and write(buf, size) from Print
|
||||
operator bool() const;
|
||||
typedef struct {
|
||||
UARTnr_t uart_nr;
|
||||
int baud_rate;
|
||||
bool rxEnabled;
|
||||
bool txEnabled;
|
||||
uint8_t rxPin;
|
||||
uint8_t txPin;
|
||||
} uart_t;
|
||||
|
||||
void setDebugOutput(bool);
|
||||
class HardwareSerial: public Stream {
|
||||
public:
|
||||
HardwareSerial(UARTnr_t uart_nr);
|
||||
|
||||
protected:
|
||||
friend void uart0_interrupt_handler(uart_t* uart);
|
||||
void _rx_complete_irq(char c);
|
||||
void _tx_empty_irq(void);
|
||||
void begin(unsigned long baud) {
|
||||
begin(baud, 0);
|
||||
}
|
||||
void begin(unsigned long, uint8_t);
|
||||
void end();
|
||||
void swap(); //use GPIO13 and GPIO15 as RX and TX
|
||||
int available(void) override;
|
||||
int peek(void) override;
|
||||
int read(void) override;
|
||||
int availableForWrite(void);
|
||||
void flush(void) override;
|
||||
size_t write(uint8_t) override;
|
||||
inline size_t write(unsigned long n) {
|
||||
return write((uint8_t) n);
|
||||
}
|
||||
inline size_t write(long n) {
|
||||
return write((uint8_t) n);
|
||||
}
|
||||
inline size_t write(unsigned int n) {
|
||||
return write((uint8_t) n);
|
||||
}
|
||||
inline size_t write(int n) {
|
||||
return write((uint8_t) n);
|
||||
}
|
||||
using Print::write; // pull in write(str) and write(buf, size) from Print
|
||||
operator bool() const;
|
||||
|
||||
protected:
|
||||
uart_t* _uart;
|
||||
cbuf* _tx_buffer;
|
||||
cbuf* _rx_buffer;
|
||||
bool _written;
|
||||
void setDebugOutput(bool);
|
||||
bool isTxEnabled(void);
|
||||
bool isRxEnabled(void);
|
||||
|
||||
protected:
|
||||
friend void uart_interrupt_handler(uart_t* uart);
|
||||
void _rx_complete_irq(char c);
|
||||
void _tx_empty_irq(void);
|
||||
|
||||
protected:
|
||||
UARTnr_t _uart_nr;
|
||||
uart_t* _uart;
|
||||
cbuf* _tx_buffer;
|
||||
cbuf* _rx_buffer;
|
||||
bool _written;
|
||||
};
|
||||
|
||||
extern HardwareSerial Serial;
|
||||
extern HardwareSerial Serial1;
|
||||
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user