Make serial port configurable.

This change makes the choice of serial port configurable so that
wireless capability can be easily added by connecting Bluetooth modules
(such as BlueSmirf or JY-MCU) to the expansion port pins.
This commit is contained in:
Robert F-C
2012-11-05 21:34:27 +11:00
parent a7e40b2903
commit ab9d183024
3 changed files with 69 additions and 30 deletions

View File

@ -23,6 +23,44 @@
#define MarlinSerial_h
#include "Marlin.h"
#if !defined(SERIAL_PORT)
#error SERIAL_PORT not set
#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)
#define SERIAL_REGNAME_INTERNAL(registerbase,number,suffix) registerbase##number##suffix
// Registers used by MarlinSerial class (these are expanded
// depending on selected serial port
#define M_UCSRxA SERIAL_REGNAME(UCSR,SERIAL_PORT,A) // defines M_UCSRxA to be UCSRxA where x 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_RXCIEx SERIAL_REGNAME(RXCIE,SERIAL_PORT,)
#define M_UDREx SERIAL_REGNAME(UDRE,SERIAL_PORT,)
#if SERIAL_PORT == 0 && !defined(UDR0)
#if defined(UDR)
#define M_UDRx UDR // atmega8, atmega32
#else
#error UDR not defined
#endif
#else
#define M_UDRx SERIAL_REGNAME(UDR,SERIAL_PORT,)
#endif
#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 DEC 10
#define HEX 16
@ -46,7 +84,7 @@ struct ring_buffer
int tail;
};
#if defined(UBRRH) || defined(UBRR0H)
#if UART_PRESENT(SERIAL_PORT)
extern ring_buffer rx_buffer;
#endif
@ -68,17 +106,17 @@ class MarlinSerial //: public Stream
FORCE_INLINE void write(uint8_t c)
{
while (!((UCSR0A) & (1 << UDRE0)))
while (!((M_UCSRxA) & (1 << M_UDREx)))
;
UDR0 = c;
M_UDRx = c;
}
FORCE_INLINE void checkRx(void)
{
if((UCSR0A & (1<<RXC0)) != 0) {
unsigned char c = UDR0;
if((M_UCSRxA & (1<<M_RXCx)) != 0) {
unsigned char c = M_UDRx;
int i = (unsigned int)(rx_buffer.head + 1) % RX_BUFFER_SIZE;
// if we should be storing the received character into the location
@ -147,4 +185,4 @@ class MarlinSerial //: public Stream
extern MarlinSerial MSerial;
#endif // ! teensylu
#endif
#endif