Expand serial support in DUE/AVR hals exploiting the templated MarlinSerial classes (#11988)

This commit is contained in:
Eduardo José Tagle 2018-10-03 02:47:27 -03:00 committed by Scott Lahteine
parent f6f2246f59
commit d6955f25b2
14 changed files with 661 additions and 558 deletions

View file

@ -79,16 +79,32 @@ typedef int8_t pin_t;
//extern uint8_t MCUSR;
#define NUM_SERIAL 1
// Serial ports
#ifdef USBCON
#if ENABLED(BLUETOOTH)
#define MYSERIAL0 bluetoothSerial
#else
#define MYSERIAL0 Serial
#endif
#define NUM_SERIAL 1
#else
#define MYSERIAL0 customizedSerial
#if !WITHIN(SERIAL_PORT, -1, 3)
#error "SERIAL_PORT must be from -1 to 3"
#endif
#define MYSERIAL0 customizedSerial1
#ifdef SERIAL_PORT_2
#if !WITHIN(SERIAL_PORT_2, -1, 3)
#error "SERIAL_PORT_2 must be from -1 to 3"
#elif SERIAL_PORT_2 == SERIAL_PORT
#error "SERIAL_PORT_2 must be different than SERIAL_PORT"
#endif
#define NUM_SERIAL 2
#define MYSERIAL1 customizedSerial2
#else
#define NUM_SERIAL 1
#endif
#endif
// --------------------------------------------------------------------------

View file

@ -705,18 +705,37 @@
// Hookup ISR handlers
ISR(SERIAL_REGNAME(USART,SERIAL_PORT,_RX_vect)) {
MarlinSerial<MarlinSerialCfg>::store_rxd_char();
MarlinSerial<MarlinSerialCfg1>::store_rxd_char();
}
ISR(SERIAL_REGNAME(USART,SERIAL_PORT,_UDRE_vect)) {
MarlinSerial<MarlinSerialCfg>::_tx_udr_empty_irq();
MarlinSerial<MarlinSerialCfg1>::_tx_udr_empty_irq();
}
// Preinstantiate
template class MarlinSerial<MarlinSerialCfg>;
template class MarlinSerial<MarlinSerialCfg1>;
// Instantiate
MarlinSerial<MarlinSerialCfg> customizedSerial;
MarlinSerial<MarlinSerialCfg1> customizedSerial1;
#ifdef SERIAL_PORT_2
// Hookup ISR handlers
ISR(SERIAL_REGNAME(USART,SERIAL_PORT_2,_RX_vect)) {
MarlinSerial<MarlinSerialCfg2>::store_rxd_char();
}
ISR(SERIAL_REGNAME(USART,SERIAL_PORT_2,_UDRE_vect)) {
MarlinSerial<MarlinSerialCfg2>::_tx_udr_empty_irq();
}
// Preinstantiate
template class MarlinSerial<MarlinSerialCfg2>;
// Instantiate
MarlinSerial<MarlinSerialCfg2> customizedSerial2;
#endif
#endif // !USBCON && (UBRRH || UBRR0H || UBRR1H || UBRR2H || UBRR3H)

View file

@ -256,7 +256,7 @@
};
// Serial port configuration
struct MarlinSerialCfg {
struct MarlinSerialCfg1 {
static constexpr int PORT = SERIAL_PORT;
static constexpr unsigned int RX_SIZE = RX_BUFFER_SIZE;
static constexpr unsigned int TX_SIZE = TX_BUFFER_SIZE;
@ -268,7 +268,27 @@
static constexpr bool MAX_RX_QUEUED = bSERIAL_STATS_MAX_RX_QUEUED;
};
extern MarlinSerial<MarlinSerialCfg> customizedSerial;
extern MarlinSerial<MarlinSerialCfg1> customizedSerial1;
#ifdef SERIAL_PORT_2
// Serial port configuration
struct MarlinSerialCfg2 {
static constexpr int PORT = SERIAL_PORT_2;
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<MarlinSerialCfg2> customizedSerial2;
#endif
#endif // !USBCON

View file

@ -29,6 +29,7 @@
// Includes
// --------------------------------------------------------------------------
#include "../../inc/MarlinConfig.h"
#include "HAL.h"
#include <Wire.h>

View file

@ -41,9 +41,25 @@
#include "watchdog_Due.h"
#include "HAL_timers_Due.h"
#define NUM_SERIAL 1
// Required before the include or compilation fails
#define MYSERIAL0 customizedSerial
// Serial ports
#if !WITHIN(SERIAL_PORT, -1, 3)
#error "SERIAL_PORT must be from -1 to 3"
#endif
// MYSERIAL0 required before MarlinSerial includes!
#define MYSERIAL0 customizedSerial1
#ifdef SERIAL_PORT_2
#if !WITHIN(SERIAL_PORT_2, -1, 3)
#error "SERIAL_PORT_2 must be from -1 to 3"
#elif SERIAL_PORT_2 == SERIAL_PORT
#error "SERIAL_PORT_2 must be different than SERIAL_PORT"
#endif
#define NUM_SERIAL 2
#define MYSERIAL1 customizedSerial2
#else
#define NUM_SERIAL 1
#endif
#include "MarlinSerial_Due.h"
#include "MarlinSerialUSB_Due.h"

View file

@ -31,7 +31,7 @@
// --------------------------------------------------------------------------
// Includes
// --------------------------------------------------------------------------
#include "../../inc/MarlinConfig.h"
#include "HAL.h"
#include "HAL_timers_Due.h"

View file

@ -32,6 +32,7 @@
*/
#ifdef ARDUINO_ARCH_SAM
#include "../../inc/MarlinConfig.h"
#include "HAL.h"
#include "InterruptVectors_Due.h"

View file

@ -285,7 +285,7 @@ void MarlinSerialUSB::printFloat(double number, uint8_t digits) {
}
// Preinstantiate
MarlinSerialUSB customizedSerial;
MarlinSerialUSB customizedSerial1;
#endif // SERIAL_PORT == -1

View file

@ -89,7 +89,7 @@ private:
static void printFloat(double, uint8_t);
};
extern MarlinSerialUSB customizedSerial;
extern MarlinSerialUSB customizedSerial1;
#endif // SERIAL_PORT == -1
#endif // MARLINSERIAL_DUE_H

View file

@ -29,30 +29,27 @@
#include "../../inc/MarlinConfig.h"
// If not using the USB port as serial port
#if SERIAL_PORT >= 0
#include "MarlinSerial_Due.h"
#include "InterruptVectors_Due.h"
#include "../../Marlin.h"
#include "MarlinSerial_Due.h"
#include "InterruptVectors_Due.h"
#include "../../Marlin.h"
template<typename Cfg> typename MarlinSerial<Cfg>::ring_buffer_r MarlinSerial<Cfg>::rx_buffer = { 0 };
template<typename Cfg> typename MarlinSerial<Cfg>::ring_buffer_t MarlinSerial<Cfg>::tx_buffer = { 0 };
template<typename Cfg> bool MarlinSerial<Cfg>::_written = false;
template<typename Cfg> uint8_t MarlinSerial<Cfg>::xon_xoff_state = MarlinSerial<Cfg>::XON_XOFF_CHAR_SENT | MarlinSerial<Cfg>::XON_CHAR;
template<typename Cfg> uint8_t MarlinSerial<Cfg>::rx_dropped_bytes = 0;
template<typename Cfg> uint8_t MarlinSerial<Cfg>::rx_buffer_overruns = 0;
template<typename Cfg> uint8_t MarlinSerial<Cfg>::rx_framing_errors = 0;
template<typename Cfg> typename MarlinSerial<Cfg>::ring_buffer_pos_t MarlinSerial<Cfg>::rx_max_enqueued = 0;
template<typename Cfg> typename MarlinSerial<Cfg>::ring_buffer_r MarlinSerial<Cfg>::rx_buffer = { 0 };
template<typename Cfg> typename MarlinSerial<Cfg>::ring_buffer_t MarlinSerial<Cfg>::tx_buffer = { 0 };
template<typename Cfg> bool MarlinSerial<Cfg>::_written = false;
template<typename Cfg> uint8_t MarlinSerial<Cfg>::xon_xoff_state = MarlinSerial<Cfg>::XON_XOFF_CHAR_SENT | MarlinSerial<Cfg>::XON_CHAR;
template<typename Cfg> uint8_t MarlinSerial<Cfg>::rx_dropped_bytes = 0;
template<typename Cfg> uint8_t MarlinSerial<Cfg>::rx_buffer_overruns = 0;
template<typename Cfg> uint8_t MarlinSerial<Cfg>::rx_framing_errors = 0;
template<typename Cfg> typename MarlinSerial<Cfg>::ring_buffer_pos_t MarlinSerial<Cfg>::rx_max_enqueued = 0;
// A SW memory barrier, to ensure GCC does not overoptimize loops
#define sw_barrier() asm volatile("": : :"memory");
// A SW memory barrier, to ensure GCC does not overoptimize loops
#define sw_barrier() asm volatile("": : :"memory");
#include "../../feature/emergency_parser.h"
#include "../../feature/emergency_parser.h"
// (called with RX interrupts disabled)
template<typename Cfg>
FORCE_INLINE void MarlinSerial<Cfg>::store_rxd_char() {
// (called with RX interrupts disabled)
template<typename Cfg>
FORCE_INLINE void MarlinSerial<Cfg>::store_rxd_char() {
static EmergencyParser::State emergency_state; // = EP_RESET
@ -178,10 +175,10 @@
// Store the new head value
rx_buffer.head = h;
}
}
template<typename Cfg>
FORCE_INLINE void MarlinSerial<Cfg>::_tx_thr_empty_irq(void) {
template<typename Cfg>
FORCE_INLINE void MarlinSerial<Cfg>::_tx_thr_empty_irq(void) {
if (Cfg::TX_SIZE > 0) {
// Read positions
uint8_t t = tx_buffer.tail;
@ -221,10 +218,10 @@
// Disable interrupts if there is nothing to transmit following this byte
if (h == t) HWUART->UART_IDR = UART_IDR_TXRDY;
}
}
}
template<typename Cfg>
void MarlinSerial<Cfg>::UART_ISR(void) {
template<typename Cfg>
void MarlinSerial<Cfg>::UART_ISR(void) {
const uint32_t status = HWUART->UART_SR;
// Data received?
@ -244,11 +241,11 @@
// TODO: error reporting outside ISR
HWUART->UART_CR = UART_CR_RSTSTA;
}
}
}
// Public Methods
template<typename Cfg>
void MarlinSerial<Cfg>::begin(const long baud_setting) {
// Public Methods
template<typename Cfg>
void MarlinSerial<Cfg>::begin(const long baud_setting) {
// Disable UART interrupt in NVIC
NVIC_DisableIRQ( HWUART_IRQ );
@ -295,10 +292,10 @@
HWUART->UART_CR = UART_CR_RXEN | UART_CR_TXEN;
if (Cfg::TX_SIZE > 0) _written = false;
}
}
template<typename Cfg>
void MarlinSerial<Cfg>::end() {
template<typename Cfg>
void MarlinSerial<Cfg>::end() {
// Disable UART interrupt in NVIC
NVIC_DisableIRQ( HWUART_IRQ );
@ -308,16 +305,16 @@
__ISB();
pmc_disable_periph_clk( HWUART_IRQ_ID );
}
}
template<typename Cfg>
int MarlinSerial<Cfg>::peek(void) {
template<typename Cfg>
int MarlinSerial<Cfg>::peek(void) {
const int v = rx_buffer.head == rx_buffer.tail ? -1 : rx_buffer.buffer[rx_buffer.tail];
return v;
}
}
template<typename Cfg>
int MarlinSerial<Cfg>::read(void) {
template<typename Cfg>
int MarlinSerial<Cfg>::read(void) {
const ring_buffer_pos_t h = rx_buffer.head;
ring_buffer_pos_t t = rx_buffer.tail;
@ -354,16 +351,16 @@
}
return v;
}
}
template<typename Cfg>
typename MarlinSerial<Cfg>::ring_buffer_pos_t MarlinSerial<Cfg>::available(void) {
template<typename Cfg>
typename MarlinSerial<Cfg>::ring_buffer_pos_t MarlinSerial<Cfg>::available(void) {
const ring_buffer_pos_t h = rx_buffer.head, t = rx_buffer.tail;
return (ring_buffer_pos_t)(Cfg::RX_SIZE + h - t) & (Cfg::RX_SIZE - 1);
}
}
template<typename Cfg>
void MarlinSerial<Cfg>::flush(void) {
template<typename Cfg>
void MarlinSerial<Cfg>::flush(void) {
rx_buffer.tail = rx_buffer.head;
if (Cfg::XONOFF) {
@ -382,10 +379,10 @@
}
}
}
}
}
template<typename Cfg>
void MarlinSerial<Cfg>::write(const uint8_t c) {
template<typename Cfg>
void MarlinSerial<Cfg>::write(const uint8_t c) {
_written = true;
if (Cfg::TX_SIZE == 0) {
@ -431,10 +428,10 @@
// Enable TX isr - Non atomic, but it will eventually enable TX isr
HWUART->UART_IER = UART_IER_TXRDY;
}
}
}
template<typename Cfg>
void MarlinSerial<Cfg>::flushTX(void) {
template<typename Cfg>
void MarlinSerial<Cfg>::flushTX(void) {
// TX
if (Cfg::TX_SIZE == 0) {
@ -474,34 +471,34 @@
// At this point nothing is queued anymore (DRIE is disabled) and
// the hardware finished transmission (TXC is set).
}
}
}
/**
/**
* Imports from print.h
*/
template<typename Cfg>
void MarlinSerial<Cfg>::print(char c, int base) {
template<typename Cfg>
void MarlinSerial<Cfg>::print(char c, int base) {
print((long)c, base);
}
}
template<typename Cfg>
void MarlinSerial<Cfg>::print(unsigned char b, int base) {
template<typename Cfg>
void MarlinSerial<Cfg>::print(unsigned char b, int base) {
print((unsigned long)b, base);
}
}
template<typename Cfg>
void MarlinSerial<Cfg>::print(int n, int base) {
template<typename Cfg>
void MarlinSerial<Cfg>::print(int n, int base) {
print((long)n, base);
}
}
template<typename Cfg>
void MarlinSerial<Cfg>::print(unsigned int n, int base) {
template<typename Cfg>
void MarlinSerial<Cfg>::print(unsigned int n, int base) {
print((unsigned long)n, base);
}
}
template<typename Cfg>
void MarlinSerial<Cfg>::print(long n, int base) {
template<typename Cfg>
void MarlinSerial<Cfg>::print(long n, int base) {
if (base == 0) write(n);
else if (base == 10) {
if (n < 0) { print('-'); n = -n; }
@ -509,82 +506,82 @@
}
else
printNumber(n, base);
}
}
template<typename Cfg>
void MarlinSerial<Cfg>::print(unsigned long n, int base) {
template<typename Cfg>
void MarlinSerial<Cfg>::print(unsigned long n, int base) {
if (base == 0) write(n);
else printNumber(n, base);
}
}
template<typename Cfg>
void MarlinSerial<Cfg>::print(double n, int digits) {
template<typename Cfg>
void MarlinSerial<Cfg>::print(double n, int digits) {
printFloat(n, digits);
}
}
template<typename Cfg>
void MarlinSerial<Cfg>::println(void) {
template<typename Cfg>
void MarlinSerial<Cfg>::println(void) {
print('\r');
print('\n');
}
}
template<typename Cfg>
void MarlinSerial<Cfg>::println(const String& s) {
template<typename Cfg>
void MarlinSerial<Cfg>::println(const String& s) {
print(s);
println();
}
}
template<typename Cfg>
void MarlinSerial<Cfg>::println(const char c[]) {
template<typename Cfg>
void MarlinSerial<Cfg>::println(const char c[]) {
print(c);
println();
}
}
template<typename Cfg>
void MarlinSerial<Cfg>::println(char c, int base) {
template<typename Cfg>
void MarlinSerial<Cfg>::println(char c, int base) {
print(c, base);
println();
}
}
template<typename Cfg>
void MarlinSerial<Cfg>::println(unsigned char b, int base) {
template<typename Cfg>
void MarlinSerial<Cfg>::println(unsigned char b, int base) {
print(b, base);
println();
}
}
template<typename Cfg>
void MarlinSerial<Cfg>::println(int n, int base) {
template<typename Cfg>
void MarlinSerial<Cfg>::println(int n, int base) {
print(n, base);
println();
}
}
template<typename Cfg>
void MarlinSerial<Cfg>::println(unsigned int n, int base) {
template<typename Cfg>
void MarlinSerial<Cfg>::println(unsigned int n, int base) {
print(n, base);
println();
}
}
template<typename Cfg>
void MarlinSerial<Cfg>::println(long n, int base) {
template<typename Cfg>
void MarlinSerial<Cfg>::println(long n, int base) {
print(n, base);
println();
}
}
template<typename Cfg>
void MarlinSerial<Cfg>::println(unsigned long n, int base) {
template<typename Cfg>
void MarlinSerial<Cfg>::println(unsigned long n, int base) {
print(n, base);
println();
}
}
template<typename Cfg>
void MarlinSerial<Cfg>::println(double n, int digits) {
template<typename Cfg>
void MarlinSerial<Cfg>::println(double n, int digits) {
print(n, digits);
println();
}
}
// Private Methods
template<typename Cfg>
void MarlinSerial<Cfg>::printNumber(unsigned long n, uint8_t base) {
// Private Methods
template<typename Cfg>
void MarlinSerial<Cfg>::printNumber(unsigned long n, uint8_t base) {
if (n) {
unsigned char buf[8 * sizeof(long)]; // Enough space for base 2
int8_t i = 0;
@ -597,10 +594,10 @@
}
else
print('0');
}
}
template<typename Cfg>
void MarlinSerial<Cfg>::printFloat(double number, uint8_t digits) {
template<typename Cfg>
void MarlinSerial<Cfg>::printFloat(double number, uint8_t digits) {
// Handle negative numbers
if (number < 0.0) {
print('-');
@ -628,13 +625,26 @@
remainder -= toPrint;
}
}
}
}
// If not using the USB port as serial port
#if SERIAL_PORT >= 0
// Preinstantiate
template class MarlinSerial<MarlinSerialCfg>;
template class MarlinSerial<MarlinSerialCfg1>;
// Instantiate
MarlinSerial<MarlinSerialCfg> customizedSerial;
MarlinSerial<MarlinSerialCfg1> customizedSerial1;
#endif
#ifdef SERIAL_PORT_2
// Preinstantiate
template class MarlinSerial<MarlinSerialCfg2>;
// Instantiate
MarlinSerial<MarlinSerialCfg2> customizedSerial2;
#endif

View file

@ -31,8 +31,6 @@
#include "../shared/MarlinSerial.h"
#if SERIAL_PORT >= 0
#include <WString.h>
#define DEC 10
@ -161,8 +159,10 @@ private:
static void printFloat(double, uint8_t);
};
// Serial port configuration
struct MarlinSerialCfg {
#if SERIAL_PORT >= 0
// Serial port configuration
struct MarlinSerialCfg1 {
static constexpr int PORT = SERIAL_PORT;
static constexpr unsigned int RX_SIZE = RX_BUFFER_SIZE;
static constexpr unsigned int TX_SIZE = TX_BUFFER_SIZE;
@ -172,10 +172,29 @@ struct MarlinSerialCfg {
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;
extern MarlinSerial<MarlinSerialCfg1> customizedSerial1;
#endif // SERIAL_PORT >= 0
#ifdef SERIAL_PORT_2
// Serial port configuration
struct MarlinSerialCfg2 {
static constexpr int PORT = SERIAL_PORT_2;
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<MarlinSerialCfg2> customizedSerial2;
#endif
#endif // MARLINSERIAL_DUE_H

View file

@ -27,6 +27,7 @@
#ifdef ARDUINO_ARCH_SAM
#include "../../inc/MarlinConfig.h"
#include "HAL.h"
#include "HAL_timers_Due.h"

View file

@ -60,19 +60,19 @@ void GcodeSuite::M111() {
SERIAL_ECHOPGM(MSG_DEBUG_OFF);
#if !defined(__AVR__) || !defined(USBCON)
#if ENABLED(SERIAL_STATS_RX_BUFFER_OVERRUNS)
SERIAL_ECHOPAIR("\nBuffer Overruns: ", customizedSerial.buffer_overruns());
SERIAL_ECHOPAIR("\nBuffer Overruns: ", MYSERIAL0.buffer_overruns());
#endif
#if ENABLED(SERIAL_STATS_RX_FRAMING_ERRORS)
SERIAL_ECHOPAIR("\nFraming Errors: ", customizedSerial.framing_errors());
SERIAL_ECHOPAIR("\nFraming Errors: ", MYSERIAL0.framing_errors());
#endif
#if ENABLED(SERIAL_STATS_DROPPED_RX)
SERIAL_ECHOPAIR("\nDropped bytes: ", customizedSerial.dropped());
SERIAL_ECHOPAIR("\nDropped bytes: ", MYSERIAL0.dropped());
#endif
#if ENABLED(SERIAL_STATS_MAX_RX_QUEUED)
SERIAL_ECHOPAIR("\nMax RX Queue Size: ", customizedSerial.rxMaxEnqueued());
SERIAL_ECHOPAIR("\nMax RX Queue Size: ", MYSERIAL0.rxMaxEnqueued());
#endif
#endif // !defined(__AVR__) || !defined(USBCON)
}

View file

@ -562,11 +562,11 @@ void advance_command_queue() {
#if !defined(__AVR__) || !defined(USBCON)
#if ENABLED(SERIAL_STATS_DROPPED_RX)
SERIAL_ECHOLNPAIR("Dropped bytes: ", customizedSerial.dropped());
SERIAL_ECHOLNPAIR("Dropped bytes: ", MYSERIAL0.dropped());
#endif
#if ENABLED(SERIAL_STATS_MAX_RX_QUEUED)
SERIAL_ECHOLNPAIR("Max RX Queue Size: ", customizedSerial.rxMaxEnqueued());
SERIAL_ECHOLNPAIR("Max RX Queue Size: ", MYSERIAL0.rxMaxEnqueued());
#endif
#endif // !defined(__AVR__) || !defined(USBCON)