Browse Source

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.
Robert F-C 12 years ago
parent
commit
ab9d183024
3 changed files with 69 additions and 30 deletions
  1. 7
    0
      Marlin/Configuration.h
  2. 18
    24
      Marlin/MarlinSerial.cpp
  3. 44
    6
      Marlin/MarlinSerial.h

+ 7
- 0
Marlin/Configuration.h View File

331
 // SF send wrong arc g-codes when using Arc Point as fillet procedure
331
 // SF send wrong arc g-codes when using Arc Point as fillet procedure
332
 //#define SF_ARC_FIX
332
 //#define SF_ARC_FIX
333
 
333
 
334
+
335
+// SERIAL_PORT selects which serial port should be used for communication with the host.
336
+// This allows the use of wireless adapters (for instance) which are connected to 
337
+// non-default serial port pins.
338
+#define SERIAL_PORT 2
339
+
340
+
334
 #include "Configuration_adv.h"
341
 #include "Configuration_adv.h"
335
 #include "thermistortables.h"
342
 #include "thermistortables.h"
336
 
343
 

+ 18
- 24
Marlin/MarlinSerial.cpp View File

28
 // this is so I can support Attiny series and any other chip without a uart
28
 // this is so I can support Attiny series and any other chip without a uart
29
 #if defined(UBRRH) || defined(UBRR0H) || defined(UBRR1H) || defined(UBRR2H) || defined(UBRR3H)
29
 #if defined(UBRRH) || defined(UBRR0H) || defined(UBRR1H) || defined(UBRR2H) || defined(UBRR3H)
30
 
30
 
31
-#if defined(UBRRH) || defined(UBRR0H)
31
+#if UART_PRESENT(SERIAL_PORT)
32
   ring_buffer rx_buffer  =  { { 0 }, 0, 0 };
32
   ring_buffer rx_buffer  =  { { 0 }, 0, 0 };
33
 #endif
33
 #endif
34
 
34
 
48
 
48
 
49
 
49
 
50
 //#elif defined(SIG_USART_RECV)
50
 //#elif defined(SIG_USART_RECV)
51
-#if defined(USART0_RX_vect)
51
+#if defined(M_USARTx_RX_vect)
52
   // fixed by Mark Sproul this is on the 644/644p
52
   // fixed by Mark Sproul this is on the 644/644p
53
   //SIGNAL(SIG_USART_RECV)
53
   //SIGNAL(SIG_USART_RECV)
54
-  SIGNAL(USART0_RX_vect)
54
+  SIGNAL(M_USARTx_RX_vect)
55
   {
55
   {
56
-  #if defined(UDR0)
57
-    unsigned char c  =  UDR0;
58
-  #elif defined(UDR)
59
-    unsigned char c  =  UDR;  //  atmega8, atmega32
60
-  #else
61
-    #error UDR not defined
62
-  #endif
56
+    unsigned char c  =  M_UDRx;
63
     store_char(c);
57
     store_char(c);
64
   }
58
   }
65
 #endif
59
 #endif
76
 void MarlinSerial::begin(long baud)
70
 void MarlinSerial::begin(long baud)
77
 {
71
 {
78
   uint16_t baud_setting;
72
   uint16_t baud_setting;
79
-  bool useU2X0 = true;
73
+  bool useU2X = true;
80
 
74
 
81
-#if F_CPU == 16000000UL
75
+#if F_CPU == 16000000UL && SERIAL_PORT == 0
82
   // hardcoded exception for compatibility with the bootloader shipped
76
   // hardcoded exception for compatibility with the bootloader shipped
83
   // with the Duemilanove and previous boards and the firmware on the 8U2
77
   // with the Duemilanove and previous boards and the firmware on the 8U2
84
   // on the Uno and Mega 2560.
78
   // on the Uno and Mega 2560.
85
   if (baud == 57600) {
79
   if (baud == 57600) {
86
-    useU2X0 = false;
80
+    useU2X = false;
87
   }
81
   }
88
 #endif
82
 #endif
89
   
83
   
90
-  if (useU2X0) {
91
-    UCSR0A = 1 << U2X0;
84
+  if (useU2X) {
85
+    M_UCSRxA = 1 << M_U2Xx;
92
     baud_setting = (F_CPU / 4 / baud - 1) / 2;
86
     baud_setting = (F_CPU / 4 / baud - 1) / 2;
93
   } else {
87
   } else {
94
-    UCSR0A = 0;
88
+    M_UCSRxA = 0;
95
     baud_setting = (F_CPU / 8 / baud - 1) / 2;
89
     baud_setting = (F_CPU / 8 / baud - 1) / 2;
96
   }
90
   }
97
 
91
 
98
   // assign the baud_setting, a.k.a. ubbr (USART Baud Rate Register)
92
   // assign the baud_setting, a.k.a. ubbr (USART Baud Rate Register)
99
-  UBRR0H = baud_setting >> 8;
100
-  UBRR0L = baud_setting;
93
+  M_UBRRxH = baud_setting >> 8;
94
+  M_UBRRxL = baud_setting;
101
 
95
 
102
-  sbi(UCSR0B, RXEN0);
103
-  sbi(UCSR0B, TXEN0);
104
-  sbi(UCSR0B, RXCIE0);
96
+  sbi(M_UCSRxB, M_RXENx);
97
+  sbi(M_UCSRxB, M_TXENx);
98
+  sbi(M_UCSRxB, M_RXCIEx);
105
 }
99
 }
106
 
100
 
107
 void MarlinSerial::end()
101
 void MarlinSerial::end()
108
 {
102
 {
109
-  cbi(UCSR0B, RXEN0);
110
-  cbi(UCSR0B, TXEN0);
111
-  cbi(UCSR0B, RXCIE0);  
103
+  cbi(M_UCSRxB, M_RXENx);
104
+  cbi(M_UCSRxB, M_TXENx);
105
+  cbi(M_UCSRxB, M_RXCIEx);  
112
 }
106
 }
113
 
107
 
114
 
108
 

+ 44
- 6
Marlin/MarlinSerial.h View File

23
 #define MarlinSerial_h
23
 #define MarlinSerial_h
24
 #include "Marlin.h"
24
 #include "Marlin.h"
25
 
25
 
26
+#if !defined(SERIAL_PORT) 
27
+#error SERIAL_PORT not set 
28
+#endif
29
+
30
+// The presence of the UBRRH register is used to detect a UART.
31
+#define UART_PRESENT(port) ((port == 0 && (defined(UBRRH) || defined(UBRR0H))) || \
32
+						(port == 1 && defined(UBRR1H)) || (port == 2 && defined(UBRR2H)) || \
33
+						(port == 3 && defined(UBRR3H)))				
34
+						
35
+// These are macros to build serial port register names for the selected SERIAL_PORT (C preprocessor
36
+// requires two levels of indirection to expand macro values properly)
37
+#define SERIAL_REGNAME(registerbase,number,suffix) SERIAL_REGNAME_INTERNAL(registerbase,number,suffix)
38
+#define SERIAL_REGNAME_INTERNAL(registerbase,number,suffix) registerbase##number##suffix
39
+
40
+// Registers used by MarlinSerial class (these are expanded 
41
+// depending on selected serial port
42
+#define M_UCSRxA SERIAL_REGNAME(UCSR,SERIAL_PORT,A) // defines M_UCSRxA to be UCSRxA where x is the serial port number
43
+#define M_UCSRxB SERIAL_REGNAME(UCSR,SERIAL_PORT,B) 
44
+#define M_RXENx SERIAL_REGNAME(RXEN,SERIAL_PORT,)    
45
+#define M_TXENx SERIAL_REGNAME(TXEN,SERIAL_PORT,)    
46
+#define M_RXCIEx SERIAL_REGNAME(RXCIE,SERIAL_PORT,)    
47
+#define M_UDREx SERIAL_REGNAME(UDRE,SERIAL_PORT,)    
48
+#if SERIAL_PORT == 0 && !defined(UDR0)
49
+  #if defined(UDR)
50
+    #define M_UDRx UDR  //  atmega8, atmega32
51
+  #else
52
+    #error UDR not defined
53
+  #endif
54
+#else
55
+  #define M_UDRx SERIAL_REGNAME(UDR,SERIAL_PORT,) 
56
+#endif  
57
+#define M_UBRRxH SERIAL_REGNAME(UBRR,SERIAL_PORT,H)
58
+#define M_UBRRxL SERIAL_REGNAME(UBRR,SERIAL_PORT,L)
59
+#define M_RXCx SERIAL_REGNAME(RXC,SERIAL_PORT,)
60
+#define M_USARTx_RX_vect SERIAL_REGNAME(USART,SERIAL_PORT,_RX_vect)
61
+#define M_U2Xx SERIAL_REGNAME(U2X,SERIAL_PORT,)
62
+
63
+
26
 
64
 
27
 #define DEC 10
65
 #define DEC 10
28
 #define HEX 16
66
 #define HEX 16
46
   int tail;
84
   int tail;
47
 };
85
 };
48
 
86
 
49
-#if defined(UBRRH) || defined(UBRR0H)
87
+#if UART_PRESENT(SERIAL_PORT)
50
   extern ring_buffer rx_buffer;
88
   extern ring_buffer rx_buffer;
51
 #endif
89
 #endif
52
 
90
 
68
     
106
     
69
     FORCE_INLINE void write(uint8_t c)
107
     FORCE_INLINE void write(uint8_t c)
70
     {
108
     {
71
-      while (!((UCSR0A) & (1 << UDRE0)))
109
+      while (!((M_UCSRxA) & (1 << M_UDREx)))
72
         ;
110
         ;
73
 
111
 
74
-      UDR0 = c;
112
+      M_UDRx = c;
75
     }
113
     }
76
     
114
     
77
     
115
     
78
     FORCE_INLINE void checkRx(void)
116
     FORCE_INLINE void checkRx(void)
79
     {
117
     {
80
-      if((UCSR0A & (1<<RXC0)) != 0) {
81
-        unsigned char c  =  UDR0;
118
+      if((M_UCSRxA & (1<<M_RXCx)) != 0) {
119
+        unsigned char c  =  M_UDRx;
82
         int i = (unsigned int)(rx_buffer.head + 1) % RX_BUFFER_SIZE;
120
         int i = (unsigned int)(rx_buffer.head + 1) % RX_BUFFER_SIZE;
83
 
121
 
84
         // if we should be storing the received character into the location
122
         // if we should be storing the received character into the location
147
 extern MarlinSerial MSerial;
185
 extern MarlinSerial MSerial;
148
 #endif // ! teensylu
186
 #endif // ! teensylu
149
 
187
 
150
-#endif
188
+#endif

Loading…
Cancel
Save