Skip to content

Commit 7aed6f3

Browse files
author
Mark Sproul
committed
Using register #ifdefs instead of cpu #ifdefs
1 parent ec5d3c6 commit 7aed6f3

File tree

2 files changed

+170
-49
lines changed

2 files changed

+170
-49
lines changed

hardware/arduino/cores/arduino/HardwareSerial.cpp

100755100644
Lines changed: 153 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@
1818
1919
Modified 23 November 2006 by David A. Mellis
2020
*/
21+
//*******************************************************************************************
22+
//* Mar 14, 2010 <MLS> Mark Sproul fixed bug in __AVR_ATmega644__ interrupt vector
23+
//* Mar 25, 2010 <MLS> Fixed bug in __AVR_ATmega644P__ interrupt vector
24+
//* Apr 2, 2010 <MLS> Changed RX_BUFFER_SIZE to 32 for smaller ram processors
25+
//* Jul 30, 2010 <MLS> Chainging #ifdefs to register and signals instead of CPU type
26+
//* Aug 3, 2010 <MLS> Tested on 644P 645 1280, 2560 328 and others
27+
//* Aug 16, 2010 <MLS> Added support for Atmega32
28+
//* Aug 31, 2010 <MLS> The ATmega32U4 has uart1 but NOT uart0
29+
//* Sep 5, 2010 <MLS> V0019 was released, migrated changes into 0019
30+
//* Sep 28, 2010 <MLS> V0020 was released, migrated changes into 0020
31+
//*******************************************************************************************
2132

2233
#include <stdlib.h>
2334
#include <stdio.h>
@@ -26,28 +37,46 @@
2637
#include "wiring.h"
2738
#include "wiring_private.h"
2839

40+
//*******************************************************************************************
41+
//* this next line disables the entire HardwareSerial.cpp,
42+
//* this is so I can support Attiny series and any other chip without a uart
43+
#if defined(UBRRH) || defined(UBRR0H) || defined(UBRR1H) || defined(UBRR2H) || defined(UBRR3H)
44+
2945
#include "HardwareSerial.h"
3046

3147
// Define constants and variables for buffering incoming serial data. We're
3248
// using a ring buffer (I think), in which rx_buffer_head is the index of the
3349
// location to which to write the next incoming character and rx_buffer_tail
3450
// is the index of the location from which to read.
35-
#define RX_BUFFER_SIZE 128
51+
#if (RAMEND < 1500)
52+
#define RX_BUFFER_SIZE 32
53+
#else
54+
#define RX_BUFFER_SIZE 128
55+
#endif
3656

37-
struct ring_buffer {
38-
unsigned char buffer[RX_BUFFER_SIZE];
39-
int head;
40-
int tail;
57+
struct ring_buffer
58+
{
59+
unsigned char buffer[RX_BUFFER_SIZE];
60+
int head;
61+
int tail;
4162
};
4263

43-
ring_buffer rx_buffer = { { 0 }, 0, 0 };
64+
#if defined(UBRRH) || defined(UBRR0H)
65+
ring_buffer rx_buffer = { { 0 }, 0, 0 };
66+
#endif
4467

45-
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
46-
ring_buffer rx_buffer1 = { { 0 }, 0, 0 };
47-
ring_buffer rx_buffer2 = { { 0 }, 0, 0 };
48-
ring_buffer rx_buffer3 = { { 0 }, 0, 0 };
68+
//#if defined(__AVR_ATmega1280__)
69+
#if defined(UBRR1H)
70+
ring_buffer rx_buffer1 = { { 0 }, 0, 0 };
71+
#endif
72+
#if defined(UBRR2H)
73+
ring_buffer rx_buffer2 = { { 0 }, 0, 0 };
74+
#endif
75+
#if defined(UBRR3H)
76+
ring_buffer rx_buffer3 = { { 0 }, 0, 0 };
4977
#endif
5078

79+
//*******************************************************************************************
5180
inline void store_char(unsigned char c, ring_buffer *rx_buffer)
5281
{
5382
int i = (rx_buffer->head + 1) % RX_BUFFER_SIZE;
@@ -62,52 +91,112 @@ inline void store_char(unsigned char c, ring_buffer *rx_buffer)
6291
}
6392
}
6493

65-
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
66-
67-
SIGNAL(SIG_USART0_RECV)
94+
#if defined(USART_RX_vect)
95+
//*******************************************************************************************
96+
SIGNAL(USART_RX_vect)
6897
{
69-
unsigned char c = UDR0;
70-
store_char(c, &rx_buffer);
98+
#if defined(UDR0)
99+
unsigned char c = UDR0;
100+
#elif defined(UDR)
101+
unsigned char c = UDR; // atmega8535
102+
#else
103+
#error UDR not defined
104+
#endif
105+
store_char(c, &rx_buffer);
71106
}
72-
73-
SIGNAL(SIG_USART1_RECV)
107+
#elif defined(SIG_USART0_RECV) && defined(UDR0)
108+
//*******************************************************************************************
109+
SIGNAL(SIG_USART0_RECV)
74110
{
75-
unsigned char c = UDR1;
76-
store_char(c, &rx_buffer1);
111+
unsigned char c = UDR0;
112+
store_char(c, &rx_buffer);
77113
}
78-
79-
SIGNAL(SIG_USART2_RECV)
114+
#elif defined(SIG_UART0_RECV) && defined(UDR0)
115+
//*******************************************************************************************
116+
SIGNAL(SIG_UART0_RECV)
80117
{
81-
unsigned char c = UDR2;
82-
store_char(c, &rx_buffer2);
118+
unsigned char c = UDR0;
119+
store_char(c, &rx_buffer);
83120
}
84-
85-
SIGNAL(SIG_USART3_RECV)
121+
//#elif defined(SIG_USART_RECV)
122+
#elif defined(USART0_RX_vect)
123+
//*******************************************************************************************
124+
//* fixed by Mark Sproul March 25, 2010
125+
//* this is on the 644/644p
126+
//SIGNAL(SIG_USART_RECV)
127+
SIGNAL(USART0_RX_vect)
86128
{
87-
unsigned char c = UDR3;
88-
store_char(c, &rx_buffer3);
89-
}
90-
129+
#if defined(UDR0)
130+
unsigned char c = UDR0;
131+
#elif defined(UDR)
132+
unsigned char c = UDR; // atmega8, atmega32
91133
#else
92-
93-
#if defined(__AVR_ATmega8__)
134+
#error UDR not defined
135+
#endif
136+
store_char(c, &rx_buffer);
137+
}
138+
#elif defined(SIG_UART_RECV)
139+
//*******************************************************************************************
140+
//* this is for atmega8
94141
SIGNAL(SIG_UART_RECV)
142+
{
143+
#if defined(UDR0)
144+
unsigned char c = UDR0; // atmega645
145+
#elif defined(UDR)
146+
unsigned char c = UDR; // atmega8
147+
#endif
148+
149+
store_char(c, &rx_buffer);
150+
}
151+
#elif defined(USBCON)
152+
#warning No interrupt handler for usart 0
153+
#warning Serial(0) is on USB interface
95154
#else
96-
SIGNAL(USART_RX_vect)
155+
#error No interrupt handler for usart 0
97156
#endif
157+
158+
//#if defined(SIG_USART1_RECV)
159+
#if defined(USART1_RX_vect)
160+
//*******************************************************************************************
161+
//SIGNAL(SIG_USART1_RECV)
162+
SIGNAL(USART1_RX_vect)
98163
{
99-
#if defined(__AVR_ATmega8__)
100-
unsigned char c = UDR;
101-
#else
102-
unsigned char c = UDR0;
164+
unsigned char c = UDR1;
165+
store_char(c, &rx_buffer1);
166+
}
167+
#elif defined(SIG_USART1_RECV)
168+
#error SIG_USART1_RECV
103169
#endif
104-
store_char(c, &rx_buffer);
170+
171+
172+
//#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
173+
#if defined(USART2_RX_vect)
174+
//*******************************************************************************************
175+
SIGNAL(USART2_RX_vect)
176+
{
177+
unsigned char c = UDR2;
178+
store_char(c, &rx_buffer2);
105179
}
180+
#elif defined(SIG_USART2_RECV)
181+
#error SIG_USART2_RECV
182+
#endif
106183

184+
#if defined(USART3_RX_vect)
185+
//*******************************************************************************************
186+
SIGNAL(USART3_RX_vect)
187+
{
188+
unsigned char c = UDR3;
189+
store_char(c, &rx_buffer3);
190+
}
191+
#elif defined(SIG_USART3_RECV)
192+
#error SIG_USART3_RECV
107193
#endif
108194

195+
196+
109197
// Constructors ////////////////////////////////////////////////////////////////
110198

199+
//*******************************************************************************************
111200
HardwareSerial::HardwareSerial(ring_buffer *rx_buffer,
112201
volatile uint8_t *ubrrh, volatile uint8_t *ubrrl,
113202
volatile uint8_t *ucsra, volatile uint8_t *ucsrb,
@@ -129,6 +218,7 @@ HardwareSerial::HardwareSerial(ring_buffer *rx_buffer,
129218

130219
// Public Methods //////////////////////////////////////////////////////////////
131220

221+
//*******************************************************************************************
132222
void HardwareSerial::begin(long baud)
133223
{
134224
uint16_t baud_setting;
@@ -166,18 +256,21 @@ void HardwareSerial::begin(long baud)
166256
sbi(*_ucsrb, _rxcie);
167257
}
168258

259+
//*******************************************************************************************
169260
void HardwareSerial::end()
170261
{
171262
cbi(*_ucsrb, _rxen);
172263
cbi(*_ucsrb, _txen);
173264
cbi(*_ucsrb, _rxcie);
174265
}
175266

267+
//*******************************************************************************************
176268
int HardwareSerial::available(void)
177269
{
178270
return (RX_BUFFER_SIZE + _rx_buffer->head - _rx_buffer->tail) % RX_BUFFER_SIZE;
179271
}
180272

273+
//*******************************************************************************************
181274
int HardwareSerial::peek(void)
182275
{
183276
if (_rx_buffer->head == _rx_buffer->tail) {
@@ -187,6 +280,7 @@ int HardwareSerial::peek(void)
187280
}
188281
}
189282

283+
//*******************************************************************************************
190284
int HardwareSerial::read(void)
191285
{
192286
// if the head isn't ahead of the tail, we don't have any characters
@@ -199,6 +293,7 @@ int HardwareSerial::read(void)
199293
}
200294
}
201295

296+
//*******************************************************************************************
202297
void HardwareSerial::flush()
203298
{
204299
// don't reverse this or there may be problems if the RX interrupt
@@ -213,6 +308,7 @@ void HardwareSerial::flush()
213308
_rx_buffer->head = _rx_buffer->tail;
214309
}
215310

311+
//*******************************************************************************************
216312
void HardwareSerial::write(uint8_t c)
217313
{
218314
while (!((*_ucsra) & (1 << _udre)))
@@ -223,14 +319,27 @@ void HardwareSerial::write(uint8_t c)
223319

224320
// Preinstantiate Objects //////////////////////////////////////////////////////
225321

226-
#if defined(__AVR_ATmega8__)
227-
HardwareSerial Serial(&rx_buffer, &UBRRH, &UBRRL, &UCSRA, &UCSRB, &UDR, RXEN, TXEN, RXCIE, UDRE, U2X);
322+
//#if defined(__AVR_ATmega8__)
323+
#if defined(UBRRH) && defined(UBRRL)
324+
HardwareSerial Serial(&rx_buffer, &UBRRH, &UBRRL, &UCSRA, &UCSRB, &UDR, RXEN, TXEN, RXCIE, UDRE, U2X);
325+
#elif defined(UBRR0H) && defined(UBRR0L)
326+
HardwareSerial Serial(&rx_buffer, &UBRR0H, &UBRR0L, &UCSR0A, &UCSR0B, &UDR0, RXEN0, TXEN0, RXCIE0, UDRE0, U2X0);
327+
#elif defined(USBCON)
328+
#warning no serial port defined (port 0)
228329
#else
229-
HardwareSerial Serial(&rx_buffer, &UBRR0H, &UBRR0L, &UCSR0A, &UCSR0B, &UDR0, RXEN0, TXEN0, RXCIE0, UDRE0, U2X0);
330+
#error no serial port defined (port 0)
230331
#endif
231332

232-
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
233-
HardwareSerial Serial1(&rx_buffer1, &UBRR1H, &UBRR1L, &UCSR1A, &UCSR1B, &UDR1, RXEN1, TXEN1, RXCIE1, UDRE1, U2X1);
234-
HardwareSerial Serial2(&rx_buffer2, &UBRR2H, &UBRR2L, &UCSR2A, &UCSR2B, &UDR2, RXEN2, TXEN2, RXCIE2, UDRE2, U2X2);
235-
HardwareSerial Serial3(&rx_buffer3, &UBRR3H, &UBRR3L, &UCSR3A, &UCSR3B, &UDR3, RXEN3, TXEN3, RXCIE3, UDRE3, U2X3);
333+
//#if defined(__AVR_ATmega1280__)
334+
#if defined(UBRR1H)
335+
HardwareSerial Serial1(&rx_buffer1, &UBRR1H, &UBRR1L, &UCSR1A, &UCSR1B, &UDR1, RXEN1, TXEN1, RXCIE1, UDRE1, U2X1);
336+
#endif
337+
#if defined(UBRR2H)
338+
HardwareSerial Serial2(&rx_buffer2, &UBRR2H, &UBRR2L, &UCSR2A, &UCSR2B, &UDR2, RXEN2, TXEN2, RXCIE2, UDRE2, U2X2);
339+
#endif
340+
#if defined(UBRR3H)
341+
HardwareSerial Serial3(&rx_buffer3, &UBRR3H, &UBRR3L, &UCSR3A, &UCSR3B, &UDR3, RXEN3, TXEN3, RXCIE3, UDRE3, U2X3);
236342
#endif
343+
344+
345+
#endif //defined(UBRRH) || defined(UBRR0H) || defined(UBRR1H) || defined(UBRR2H) || defined(UBRR3H)

hardware/arduino/cores/arduino/HardwareSerial.h

100755100644
Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
License along with this library; if not, write to the Free Software
1717
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1818
*/
19+
//*******************************************************************************************
20+
//* Sep 28, 2010 <MLS> V0020 was released, migrated changes into 0020
21+
//*******************************************************************************************
1922

2023
#ifndef HardwareSerial_h
2124
#define HardwareSerial_h
@@ -56,12 +59,21 @@ class HardwareSerial : public Stream
5659
using Print::write; // pull in write(str) and write(buf, size) from Print
5760
};
5861

59-
extern HardwareSerial Serial;
62+
#if defined(UBRRH) || defined(UBRR0H)
63+
extern HardwareSerial Serial;
64+
#elif defined(USBCON)
65+
#include "usb_api.h"
66+
#endif
6067

61-
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
62-
extern HardwareSerial Serial1;
63-
extern HardwareSerial Serial2;
64-
extern HardwareSerial Serial3;
68+
//#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
69+
#if defined(UBRR1H)
70+
extern HardwareSerial Serial1;
71+
#endif
72+
#if defined(UBRR2H)
73+
extern HardwareSerial Serial2;
74+
#endif
75+
#if defined(UBRR3H)
76+
extern HardwareSerial Serial3;
6577
#endif
6678

6779
#endif

0 commit comments

Comments
 (0)