Templatized serial classes (#11982)

This commit is contained in:
Eduardo José Tagle
2018-10-03 00:15:30 -03:00
committed by Scott Lahteine
parent ee53f7d813
commit f6f2246f59
6 changed files with 678 additions and 564 deletions

View File

@ -27,12 +27,13 @@
* Modified 28 September 2010 by Mark Sproul
* Modified 14 February 2016 by Andreas Hardtung (added tx buffer)
* Modified 01 October 2017 by Eduardo José Tagle (added XON/XOFF)
* Templatized 01 October 2018 by Eduardo José Tagle to allow multiple instances
*/
#ifndef _MARLINSERIAL_H_
#define _MARLINSERIAL_H_
#include "../../inc/MarlinConfigPre.h"
#include "../shared/MarlinSerial.h"
#include <WString.h>
@ -40,73 +41,173 @@
#define SERIAL_PORT 0
#endif
// The presence of the UBRRH register is used to detect a UART.
#define UART_PRESENT(port) ((port == 0 && (defined(UBRRH) || defined(UBRR0H))) || \
(port == 1 && defined(UBRR1H)) || (port == 2 && defined(UBRR2H)) || \
(port == 3 && defined(UBRR3H)))
// These are macros to build serial port register names for the selected SERIAL_PORT (C preprocessor
// requires two levels of indirection to expand macro values properly)
#define SERIAL_REGNAME(registerbase,number,suffix) SERIAL_REGNAME_INTERNAL(registerbase,number,suffix)
#if SERIAL_PORT == 0 && (!defined(UBRR0H) || !defined(UDR0)) // use un-numbered registers if necessary
#define SERIAL_REGNAME_INTERNAL(registerbase,number,suffix) registerbase##suffix
#else
#define SERIAL_REGNAME_INTERNAL(registerbase,number,suffix) registerbase##number##suffix
#endif
// Registers used by MarlinSerial class (expanded depending on selected serial port)
#define M_UCSRxA SERIAL_REGNAME(UCSR,SERIAL_PORT,A) // defines M_UCSRxA to be UCSRnA where n is the serial port number
#define M_UCSRxB SERIAL_REGNAME(UCSR,SERIAL_PORT,B)
#define M_RXENx SERIAL_REGNAME(RXEN,SERIAL_PORT,)
#define M_TXENx SERIAL_REGNAME(TXEN,SERIAL_PORT,)
#define M_TXCx SERIAL_REGNAME(TXC,SERIAL_PORT,)
#define M_RXCIEx SERIAL_REGNAME(RXCIE,SERIAL_PORT,)
#define M_UDREx SERIAL_REGNAME(UDRE,SERIAL_PORT,)
#define M_FEx SERIAL_REGNAME(FE,SERIAL_PORT,)
#define M_DORx SERIAL_REGNAME(DOR,SERIAL_PORT,)
#define M_UPEx SERIAL_REGNAME(UPE,SERIAL_PORT,)
#define M_UDRIEx SERIAL_REGNAME(UDRIE,SERIAL_PORT,)
#define M_UDRx SERIAL_REGNAME(UDR,SERIAL_PORT,)
#define M_UBRRxH SERIAL_REGNAME(UBRR,SERIAL_PORT,H)
#define M_UBRRxL SERIAL_REGNAME(UBRR,SERIAL_PORT,L)
#define M_RXCx SERIAL_REGNAME(RXC,SERIAL_PORT,)
#define M_USARTx_RX_vect SERIAL_REGNAME(USART,SERIAL_PORT,_RX_vect)
#define M_U2Xx SERIAL_REGNAME(U2X,SERIAL_PORT,)
#define M_USARTx_UDRE_vect SERIAL_REGNAME(USART,SERIAL_PORT,_UDRE_vect)
#define DEC 10
#define HEX 16
#define OCT 8
#define BIN 2
#define BYTE 0
#ifndef USBCON
// We're using a ring buffer (I think), in which rx_buffer_head is the index of the
// location to which to write the next incoming character and rx_buffer_tail is the
// index of the location from which to read.
#if RX_BUFFER_SIZE > 256
typedef uint16_t ring_buffer_pos_t;
// The presence of the UBRRH register is used to detect a UART.
#define UART_PRESENT(port) ((port == 0 && (defined(UBRRH) || defined(UBRR0H))) || \
(port == 1 && defined(UBRR1H)) || (port == 2 && defined(UBRR2H)) || \
(port == 3 && defined(UBRR3H)))
// These are macros to build serial port register names for the selected SERIAL_PORT (C preprocessor
// requires two levels of indirection to expand macro values properly)
#define SERIAL_REGNAME(registerbase,number,suffix) SERIAL_REGNAME_INTERNAL(registerbase,number,suffix)
#if SERIAL_PORT == 0 && (!defined(UBRR0H) || !defined(UDR0)) // use un-numbered registers if necessary
#define SERIAL_REGNAME_INTERNAL(registerbase,number,suffix) registerbase##suffix
#else
typedef uint8_t ring_buffer_pos_t;
#define SERIAL_REGNAME_INTERNAL(registerbase,number,suffix) registerbase##number##suffix
#endif
#if ENABLED(SERIAL_STATS_DROPPED_RX)
extern uint8_t rx_dropped_bytes;
// Registers used by MarlinSerial class (expanded depending on selected serial port)
// Templated 8bit register (generic)
#define UART_REGISTER_DECL_BASE(registerbase, suffix) \
template<int portNr> struct R_##registerbase##x##suffix {}
// Templated 8bit register (specialization for each port)
#define UART_REGISTER_DECL(port, registerbase, suffix) \
template<> struct R_##registerbase##x##suffix<port> { \
constexpr R_##registerbase##x##suffix(int) {} \
FORCE_INLINE void operator=(uint8_t newVal) const { SERIAL_REGNAME(registerbase,port,suffix) = newVal; } \
FORCE_INLINE operator uint8_t() const { return SERIAL_REGNAME(registerbase,port,suffix); } \
}
// Templated 1bit register (generic)
#define UART_BIT_DECL_BASE(registerbase, suffix, bit) \
template<int portNr>struct B_##bit##x {}
// Templated 1bit register (specialization for each port)
#define UART_BIT_DECL(port, registerbase, suffix, bit) \
template<> struct B_##bit##x<port> { \
constexpr B_##bit##x(int) {} \
FORCE_INLINE void operator=(int newVal) const { \
if (newVal) \
SBI(SERIAL_REGNAME(registerbase,port,suffix),SERIAL_REGNAME(bit,port,)); \
else \
CBI(SERIAL_REGNAME(registerbase,port,suffix),SERIAL_REGNAME(bit,port,)); \
} \
FORCE_INLINE operator bool() const { return TEST(SERIAL_REGNAME(registerbase,port,suffix),SERIAL_REGNAME(bit,port,)); } \
}
#define UART_DECL_BASE() \
UART_REGISTER_DECL_BASE(UCSR,A);\
UART_REGISTER_DECL_BASE(UDR,);\
UART_REGISTER_DECL_BASE(UBRR,H);\
UART_REGISTER_DECL_BASE(UBRR,L);\
UART_BIT_DECL_BASE(UCSR,B,RXEN);\
UART_BIT_DECL_BASE(UCSR,B,TXEN);\
UART_BIT_DECL_BASE(UCSR,A,TXC);\
UART_BIT_DECL_BASE(UCSR,B,RXCIE);\
UART_BIT_DECL_BASE(UCSR,A,UDRE);\
UART_BIT_DECL_BASE(UCSR,A,FE);\
UART_BIT_DECL_BASE(UCSR,A,DOR);\
UART_BIT_DECL_BASE(UCSR,B,UDRIE);\
UART_BIT_DECL_BASE(UCSR,A,RXC);\
UART_BIT_DECL_BASE(UCSR,A,U2X)
#define UART_DECL(port) \
UART_REGISTER_DECL(port,UCSR,A);\
UART_REGISTER_DECL(port,UDR,);\
UART_REGISTER_DECL(port,UBRR,H);\
UART_REGISTER_DECL(port,UBRR,L);\
UART_BIT_DECL(port,UCSR,B,RXEN);\
UART_BIT_DECL(port,UCSR,B,TXEN);\
UART_BIT_DECL(port,UCSR,A,TXC);\
UART_BIT_DECL(port,UCSR,B,RXCIE);\
UART_BIT_DECL(port,UCSR,A,UDRE);\
UART_BIT_DECL(port,UCSR,A,FE);\
UART_BIT_DECL(port,UCSR,A,DOR);\
UART_BIT_DECL(port,UCSR,B,UDRIE);\
UART_BIT_DECL(port,UCSR,A,RXC);\
UART_BIT_DECL(port,UCSR,A,U2X)
// Declare empty templates
UART_DECL_BASE();
// And all the specializations for each possible serial port
#if UART_PRESENT(0)
UART_DECL(0);
#endif
#if UART_PRESENT(1)
UART_DECL(1);
#endif
#if UART_PRESENT(2)
UART_DECL(2);
#endif
#if UART_PRESENT(3)
UART_DECL(3);
#endif
#if ENABLED(SERIAL_STATS_RX_BUFFER_OVERRUNS)
extern uint8_t rx_buffer_overruns;
#endif
#define DEC 10
#define HEX 16
#define OCT 8
#define BIN 2
#define BYTE 0
#if ENABLED(SERIAL_STATS_RX_FRAMING_ERRORS)
extern uint8_t rx_framing_errors;
#endif
#if ENABLED(SERIAL_STATS_MAX_RX_QUEUED)
extern ring_buffer_pos_t rx_max_enqueued;
#endif
// Templated type selector
template<bool b, typename T, typename F> struct TypeSelector { typedef T type;} ;
template<typename T, typename F> struct TypeSelector<false, T, F> { typedef F type; };
template<typename Cfg>
class MarlinSerial {
protected:
// Registers
static constexpr R_UCSRxA<Cfg::PORT> R_UCSRA = 0;
static constexpr R_UDRx<Cfg::PORT> R_UDR = 0;
static constexpr R_UBRRxH<Cfg::PORT> R_UBRRH = 0;
static constexpr R_UBRRxL<Cfg::PORT> R_UBRRL = 0;
// Bits
static constexpr B_RXENx<Cfg::PORT> B_RXEN = 0;
static constexpr B_TXENx<Cfg::PORT> B_TXEN = 0;
static constexpr B_TXCx<Cfg::PORT> B_TXC = 0;
static constexpr B_RXCIEx<Cfg::PORT> B_RXCIE = 0;
static constexpr B_UDREx<Cfg::PORT> B_UDRE = 0;
static constexpr B_FEx<Cfg::PORT> B_FE = 0;
static constexpr B_DORx<Cfg::PORT> B_DOR = 0;
static constexpr B_UDRIEx<Cfg::PORT> B_UDRIE = 0;
static constexpr B_RXCx<Cfg::PORT> B_RXC = 0;
static constexpr B_U2Xx<Cfg::PORT> B_U2X = 0;
// Base size of type on buffer size
typedef typename TypeSelector<(Cfg::RX_SIZE>256), uint16_t, uint8_t>::type ring_buffer_pos_t;
struct ring_buffer_r {
volatile ring_buffer_pos_t head, tail;
unsigned char buffer[Cfg::RX_SIZE];
};
struct ring_buffer_t {
volatile uint8_t head, tail;
unsigned char buffer[Cfg::TX_SIZE];
};
static ring_buffer_r rx_buffer;
static ring_buffer_t tx_buffer;
static bool _written;
static constexpr uint8_t XON_XOFF_CHAR_SENT = 0x80, // XON / XOFF Character was sent
XON_XOFF_CHAR_MASK = 0x1F; // XON / XOFF character to send
// XON / XOFF character definitions
static constexpr uint8_t XON_CHAR = 17, XOFF_CHAR = 19;
static uint8_t xon_xoff_state,
rx_dropped_bytes,
rx_buffer_overruns,
rx_framing_errors;
static ring_buffer_pos_t rx_max_enqueued;
static FORCE_INLINE ring_buffer_pos_t atomic_read_rx_head();
static volatile bool rx_tail_value_not_stable;
static volatile uint16_t rx_tail_value_backup;
static FORCE_INLINE void atomic_set_rx_tail(ring_buffer_pos_t value);
static FORCE_INLINE ring_buffer_pos_t atomic_read_rx_tail();
public:
FORCE_INLINE static void store_rxd_char();
FORCE_INLINE static void _tx_udr_empty_irq(void);
public:
MarlinSerial() {};
@ -119,21 +220,10 @@
static void write(const uint8_t c);
static void flushTX(void);
#if ENABLED(SERIAL_STATS_DROPPED_RX)
FORCE_INLINE static uint32_t dropped() { return rx_dropped_bytes; }
#endif
#if ENABLED(SERIAL_STATS_RX_BUFFER_OVERRUNS)
FORCE_INLINE static uint32_t buffer_overruns() { return rx_buffer_overruns; }
#endif
#if ENABLED(SERIAL_STATS_RX_FRAMING_ERRORS)
FORCE_INLINE static uint32_t framing_errors() { return rx_framing_errors; }
#endif
#if ENABLED(SERIAL_STATS_MAX_RX_QUEUED)
FORCE_INLINE static ring_buffer_pos_t rxMaxEnqueued() { return rx_max_enqueued; }
#endif
FORCE_INLINE static uint8_t dropped() { return Cfg::DROPPED_RX ? rx_dropped_bytes : 0; }
FORCE_INLINE static uint8_t buffer_overruns() { return Cfg::RX_OVERRUNS ? rx_buffer_overruns : 0; }
FORCE_INLINE static uint8_t framing_errors() { return Cfg::RX_FRAMING_ERRORS ? rx_framing_errors : 0; }
FORCE_INLINE static ring_buffer_pos_t rxMaxEnqueued() { return Cfg::MAX_RX_QUEUED ? rx_max_enqueued : 0; }
FORCE_INLINE static void write(const char* str) { while (*str) write(*str++); }
FORCE_INLINE static void write(const uint8_t* buffer, size_t size) { while (size--) write(*buffer++); }
@ -165,7 +255,20 @@
static void printFloat(double, uint8_t);
};
extern MarlinSerial customizedSerial;
// Serial port configuration
struct MarlinSerialCfg {
static constexpr int PORT = SERIAL_PORT;
static constexpr unsigned int RX_SIZE = RX_BUFFER_SIZE;
static constexpr unsigned int TX_SIZE = TX_BUFFER_SIZE;
static constexpr bool XONOFF = bSERIAL_XON_XOFF;
static constexpr bool EMERGENCYPARSER = bEMERGENCY_PARSER;
static constexpr bool DROPPED_RX = bSERIAL_STATS_DROPPED_RX;
static constexpr bool RX_OVERRUNS = bSERIAL_STATS_RX_BUFFER_OVERRUNS;
static constexpr bool RX_FRAMING_ERRORS = bSERIAL_STATS_RX_FRAMING_ERRORS;
static constexpr bool MAX_RX_QUEUED = bSERIAL_STATS_MAX_RX_QUEUED;
};
extern MarlinSerial<MarlinSerialCfg> customizedSerial;
#endif // !USBCON