2016-10-06 14:21:30 +03:00
/*
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 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
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 )
Modified 25 April 2015 by Thomas Flayols ( add configuration different from 8 N1 in ESP8266 )
2018-11-19 16:51:55 +01:00
Modified 13 October 2018 by Jeroen Döll ( add baudrate detection )
Baudrate detection example usage ( detection on Serial1 ) :
void setup ( ) {
Serial . begin ( 115200 ) ;
delay ( 100 ) ;
Serial . println ( ) ;
Serial1 . begin ( 0 , SERIAL_8N1 , - 1 , - 1 , true , 11000UL ) ; // Passing 0 for baudrate to detect it, the last parameter is a timeout in ms
unsigned long detectedBaudRate = Serial1 . baudRate ( ) ;
if ( detectedBaudRate ) {
Serial . printf ( " Detected baudrate is %lu \n " , detectedBaudRate ) ;
} else {
Serial . println ( " No baudrate detected, Serial1 will not work! " ) ;
}
}
Pay attention : the baudrate returned by baudRate ( ) may be rounded , eg 115200 returns 115201
2016-10-06 14:21:30 +03:00
*/
# ifndef HardwareSerial_h
# define HardwareSerial_h
# include <inttypes.h>
2022-03-02 10:20:43 -03:00
# include <functional>
2016-10-06 14:21:30 +03:00
# include "Stream.h"
# include "esp32-hal.h"
2021-08-23 11:25:33 -03:00
# include "soc/soc_caps.h"
2021-08-31 08:47:55 +03:00
# include "HWCDC.h"
2016-10-06 14:21:30 +03:00
2022-03-02 10:20:43 -03:00
# include "freertos/FreeRTOS.h"
# include "freertos/task.h"
# include "freertos/semphr.h"
typedef enum {
UART_BREAK_ERROR ,
UART_BUFFER_FULL_ERROR ,
UART_FIFO_OVF_ERROR ,
UART_FRAME_ERROR ,
UART_PARITY_ERROR
} hardwareSerial_error_t ;
typedef std : : function < void ( void ) > OnReceiveCb ;
typedef std : : function < void ( hardwareSerial_error_t ) > OnReceiveErrorCb ;
2016-10-06 14:21:30 +03:00
class HardwareSerial : public Stream
{
public :
HardwareSerial ( int uart_nr ) ;
2022-03-02 10:20:43 -03:00
~ HardwareSerial ( ) ;
2016-10-06 14:21:30 +03:00
2022-03-28 08:18:30 -03:00
// setRxTimeout sets the timeout after which onReceive callback will be called (after receiving data, it waits for this time of UART rx inactivity to call the callback fnc)
// param symbols_timeout defines a timeout threshold in uart symbol periods. Setting 0 symbol timeout disables the callback call by timeout.
// Maximum timeout setting is calculacted automatically by IDF. If set above the maximum, it is ignored and an error is printed on Serial0 (check console).
// Examples: Maximum for 11 bits symbol is 92 (SERIAL_8N2, SERIAL_8E1, SERIAL_8O1, etc), Maximum for 10 bits symbol is 101 (SERIAL_8N1).
// For example symbols_timeout=1 defines a timeout equal to transmission time of one symbol (~11 bit) on current baudrate.
// For a baudrate of 9600, SERIAL_8N1 (10 bit symbol) and symbols_timeout = 3, the timeout would be 3 / (9600 / 10) = 3.125 ms
void setRxTimeout ( uint8_t symbols_timeout ) ;
// onReceive will setup a callback that will be called whenever an UART interruption occurs (UART_INTR_RXFIFO_FULL or UART_INTR_RXFIFO_TOUT)
// UART_INTR_RXFIFO_FULL interrupt triggers at UART_FULL_THRESH_DEFAULT bytes received (defined as 120 bytes by default in IDF)
// UART_INTR_RXFIFO_TOUT interrupt triggers at UART_TOUT_THRESH_DEFAULT symbols passed without any reception (defined as 10 symbos by default in IDF)
// onlyOnTimeout parameter will define how onReceive will behave:
// Default: true -- The callback will only be called when RX Timeout happens.
// Whole stream of bytes will be ready for being read on the callback function at once.
// This option may lead to Rx Overflow depending on the Rx Buffer Size and number of bytes received in the streaming
// false -- The callback will be called when FIFO reaches 120 bytes and also on RX Timeout.
// The stream of incommig bytes will be "split" into blocks of 120 bytes on each callback.
// This option avoid any sort of Rx Overflow, but leaves the UART packet reassembling work to the Application.
void onReceive ( OnReceiveCb function , bool onlyOnTimeout = true ) ;
// onReceive will be called on error events (see hardwareSerial_error_t)
2022-03-02 10:20:43 -03:00
void onReceiveError ( OnReceiveErrorCb function ) ;
2022-03-28 08:18:30 -03:00
// eventQueueReset clears all events in the queue (the events that trigger onReceive and onReceiveError) - maybe usefull in some use cases
void eventQueueReset ( ) ;
2022-03-02 10:20:43 -03:00
2021-04-08 15:29:53 +03:00
void begin ( unsigned long baud , uint32_t config = SERIAL_8N1 , int8_t rxPin = - 1 , int8_t txPin = - 1 , bool invert = false , unsigned long timeout_ms = 20000UL , uint8_t rxfifo_full_thrhd = 112 ) ;
2022-03-02 10:20:43 -03:00
void end ( bool fullyTerminate = true ) ;
2018-12-23 20:15:06 +01:00
void updateBaudRate ( unsigned long baud ) ;
2016-10-06 14:21:30 +03:00
int available ( void ) ;
2018-04-06 18:07:46 +02:00
int availableForWrite ( void ) ;
2016-10-06 14:21:30 +03:00
int peek ( void ) ;
int read ( void ) ;
2020-01-20 14:54:50 +01:00
size_t read ( uint8_t * buffer , size_t size ) ;
inline size_t read ( char * buffer , size_t size )
{
return read ( ( uint8_t * ) buffer , size ) ;
}
2016-10-06 14:21:30 +03:00
void flush ( void ) ;
2019-11-11 07:37:35 -07:00
void flush ( bool txOnly ) ;
2016-10-06 14:21:30 +03:00
size_t write ( uint8_t ) ;
size_t write ( const uint8_t * buffer , size_t size ) ;
2020-01-20 14:54:50 +01:00
inline size_t write ( const char * buffer , size_t size )
{
return write ( ( uint8_t * ) buffer , size ) ;
}
2016-11-16 17:30:13 +02:00
inline size_t write ( const char * s )
{
return write ( ( uint8_t * ) s , strlen ( s ) ) ;
}
2016-10-06 14:21:30 +03:00
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 ) ;
}
2017-09-21 17:00:12 +08:00
uint32_t baudRate ( ) ;
2016-10-06 14:21:30 +03:00
operator bool ( ) const ;
void setDebugOutput ( bool ) ;
2020-10-01 06:58:48 -04:00
void setRxInvert ( bool ) ;
2022-02-16 22:28:46 -03:00
// Negative Pin Number will keep it unmodified, thus this function can set individual pins
// SetPins shall be called after Serial begin()
void setPins ( int8_t rxPin , int8_t txPin , int8_t ctsPin = - 1 , int8_t rtsPin = - 1 ) ;
// Enables or disables Hardware Flow Control using RTS and/or CTS pins (must use setAllPins() before)
void setHwFlowCtrlMode ( uint8_t mode = HW_FLOWCTRL_CTS_RTS , uint8_t threshold = 64 ) ; // 64 is half FIFO Length
2021-08-24 02:21:20 -03:00
size_t setRxBufferSize ( size_t new_size ) ;
2022-03-28 05:37:12 -03:00
size_t setTxBufferSize ( size_t new_size ) ;
2016-10-06 14:21:30 +03:00
protected :
int _uart_nr ;
uart_t * _uart ;
2021-08-24 02:21:20 -03:00
size_t _rxBufferSize ;
2022-03-28 05:37:12 -03:00
size_t _txBufferSize ;
2022-03-02 10:20:43 -03:00
OnReceiveCb _onReceiveCB ;
2022-03-28 11:04:38 -03:00
OnReceiveErrorCb _onReceiveErrorCB ;
2022-03-28 08:18:30 -03:00
// _onReceive and _rxTimeout have be consistent when timeout is disabled
bool _onReceiveTimeout ;
uint8_t _rxTimeout ;
2022-03-02 10:20:43 -03:00
TaskHandle_t _eventTask ;
2022-03-28 11:04:38 -03:00
# if !CONFIG_DISABLE_HAL_LOCKS
SemaphoreHandle_t _lock ;
# endif
2022-03-02 10:20:43 -03:00
void _createEventTask ( void * args ) ;
void _destroyEventTask ( void ) ;
static void _uartEventTask ( void * args ) ;
2016-10-06 14:21:30 +03:00
} ;
2019-10-04 11:49:39 +02:00
extern void serialEventRun ( void ) __attribute__ ( ( weak ) ) ;
2018-04-07 01:02:59 +09:00
# if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SERIAL)
2021-08-02 15:35:13 +03:00
# ifndef ARDUINO_USB_CDC_ON_BOOT
# define ARDUINO_USB_CDC_ON_BOOT 0
2021-04-05 14:23:58 +03:00
# endif
2021-08-02 15:35:13 +03:00
# if ARDUINO_USB_CDC_ON_BOOT //Serial used for USB CDC
2022-03-28 12:09:41 +03:00
# if !ARDUINO_USB_MODE
2021-04-05 14:23:58 +03:00
# include "USB.h"
# include "USBCDC.h"
2022-03-28 12:09:41 +03:00
# endif
2021-08-31 08:47:55 +03:00
extern HardwareSerial Serial0 ;
2021-04-05 14:23:58 +03:00
# else
2016-10-06 14:21:30 +03:00
extern HardwareSerial Serial ;
2021-04-05 14:23:58 +03:00
# endif
2021-08-23 11:25:33 -03:00
# if SOC_UART_NUM > 1
2018-07-03 20:41:03 +02:00
extern HardwareSerial Serial1 ;
2021-08-23 11:25:33 -03:00
# endif
# if SOC_UART_NUM > 2
2018-07-03 20:41:03 +02:00
extern HardwareSerial Serial2 ;
2018-04-07 01:02:59 +09:00
# endif
2021-04-05 14:23:58 +03:00
# endif
2016-10-06 14:21:30 +03:00
2019-10-04 11:49:39 +02:00
# endif // HardwareSerial_h