Przeglądaj źródła

Merge pull request #289 from buildrob/Marlin_v1

Marlin v1 configurable serial port
daid 12 lat temu
rodzic
commit
2f20df3f55
3 zmienionych plików z 67 dodań i 30 usunięć
  1. 5
    0
      Marlin/Configuration.h
  2. 18
    24
      Marlin/MarlinSerial.cpp
  3. 44
    6
      Marlin/MarlinSerial.h

+ 5
- 0
Marlin/Configuration.h Wyświetl plik

@@ -11,6 +11,11 @@
11 11
 #define STRING_VERSION_CONFIG_H "2012-05-02" //Personal revision number for changes to THIS file.
12 12
 #define STRING_CONFIG_H_AUTHOR "erik" //Who made the changes.
13 13
 
14
+// SERIAL_PORT selects which serial port should be used for communication with the host.
15
+// This allows the connection of wireless adapters (for instance) to non-default port pins.
16
+// Serial port 0 is still used by the Arduino bootloader regardless of this setting.
17
+#define SERIAL_PORT 0
18
+
14 19
 // This determines the communication speed of the printer
15 20
 #define BAUDRATE 250000
16 21
 //#define BAUDRATE 115200

+ 18
- 24
Marlin/MarlinSerial.cpp Wyświetl plik

@@ -28,7 +28,7 @@
28 28
 // this is so I can support Attiny series and any other chip without a uart
29 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 32
   ring_buffer rx_buffer  =  { { 0 }, 0, 0 };
33 33
 #endif
34 34
 
@@ -48,18 +48,12 @@ FORCE_INLINE void store_char(unsigned char c)
48 48
 
49 49
 
50 50
 //#elif defined(SIG_USART_RECV)
51
-#if defined(USART0_RX_vect)
51
+#if defined(M_USARTx_RX_vect)
52 52
   // fixed by Mark Sproul this is on the 644/644p
53 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 57
     store_char(c);
64 58
   }
65 59
 #endif
@@ -76,39 +70,39 @@ MarlinSerial::MarlinSerial()
76 70
 void MarlinSerial::begin(long baud)
77 71
 {
78 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 76
   // hardcoded exception for compatibility with the bootloader shipped
83 77
   // with the Duemilanove and previous boards and the firmware on the 8U2
84 78
   // on the Uno and Mega 2560.
85 79
   if (baud == 57600) {
86
-    useU2X0 = false;
80
+    useU2X = false;
87 81
   }
88 82
 #endif
89 83
   
90
-  if (useU2X0) {
91
-    UCSR0A = 1 << U2X0;
84
+  if (useU2X) {
85
+    M_UCSRxA = 1 << M_U2Xx;
92 86
     baud_setting = (F_CPU / 4 / baud - 1) / 2;
93 87
   } else {
94
-    UCSR0A = 0;
88
+    M_UCSRxA = 0;
95 89
     baud_setting = (F_CPU / 8 / baud - 1) / 2;
96 90
   }
97 91
 
98 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 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 Wyświetl plik

@@ -23,6 +23,44 @@
23 23
 #define MarlinSerial_h
24 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 65
 #define DEC 10
28 66
 #define HEX 16
@@ -46,7 +84,7 @@ struct ring_buffer
46 84
   int tail;
47 85
 };
48 86
 
49
-#if defined(UBRRH) || defined(UBRR0H)
87
+#if UART_PRESENT(SERIAL_PORT)
50 88
   extern ring_buffer rx_buffer;
51 89
 #endif
52 90
 
@@ -68,17 +106,17 @@ class MarlinSerial //: public Stream
68 106
     
69 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 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 120
         int i = (unsigned int)(rx_buffer.head + 1) % RX_BUFFER_SIZE;
83 121
 
84 122
         // if we should be storing the received character into the location
@@ -147,4 +185,4 @@ class MarlinSerial //: public Stream
147 185
 extern MarlinSerial MSerial;
148 186
 #endif // ! teensylu
149 187
 
150
-#endif
188
+#endif

Ładowanie…
Anuluj
Zapisz