Skip to content

Commit 9dde154

Browse files
committed
Squashed 'avr/cores/MCUdude_corefiles/' changes from 278334f4..0d56ecc5
0d56ecc5 Use template based functions for radians() and degrees() by default a9ade41d Use template functions instead of macros by default This will make it possible to use the previous "typeof" macros outside functions and structures b811cd11 Use template based min/max functions by default 5ddffca3 Reorder hardware serial macros ... to ensure no poisoned macros are being used 21b32d15 Add support for AVR 5 and 9 series 88a427b0 Add support for AVR 5 and 9 series 54808586 Reorder macro checking To prevent GCC poison error on when compiling for ATmega165 b1703a37 Initial support for AVR ATmega 5 and 9-series ATmega165/325/645, ATmega3250/6450, ATmega169/239/649, ATmega3290/6490 6b36d8f1 Fix typo More detals here: arduino/ArduinoCore-avr#466 ecb12fa4 Add GCC printf formatting checks git-subtree-dir: avr/cores/MCUdude_corefiles git-subtree-split: 0d56ecc539d7047899835fdc040d8748c3afc564
1 parent bd1057d commit 9dde154

7 files changed

+278
-140
lines changed

Arduino.h

+101-36
Original file line numberDiff line numberDiff line change
@@ -75,65 +75,70 @@ void yield(void);
7575

7676
/* Analog reference definitions */
7777

78-
// ATmega8535, ATmega8, ATmega16, ATmega32, ATmega64, ATmega128
79-
#if defined(__AVR_ATmega8535__) || defined(__AVR_ATmega8__) || defined(__AVR_ATmega16__) \
80-
|| defined(__AVR_ATmega32__) || defined(__AVR_ATmega64__) || defined(__AVR_ATmega128__)
78+
// "Classic" series
79+
#if defined(__AVR_ATmega8535__) || defined(__AVR_ATmega8__) || defined(__AVR_ATmega16__) \
80+
|| defined(__AVR_ATmega32__) || defined(__AVR_ATmega64__) || defined(__AVR_ATmega128__)
8181
#define EXTERNAL 0
8282
#define DEFAULT 1 // Default -> AVCC with external capacitor at AREF pin
8383
#define INTERNAL2V56 3
8484
#define INTERNAL 3
8585

86-
// ATmega48/P/PB, ATmega88/P/PB, ATmega168/P/PB, ATmega328/P/PB
87-
#elif defined(__AVR_ATmega48__) || defined(__AVR_ATmega48P__) || defined(__AVR_ATmega48PB__) \
88-
|| defined(__AVR_ATmega88__) || defined(__AVR_ATmega88P__) || defined(__AVR_ATmega88PB__) \
89-
|| defined(__AVR_ATmega168__) || defined(__AVR_ATmega168P__) || defined(__AVR_ATmega168PB__) \
90-
|| defined(__AVR_ATmega328__) || defined(__AVR_ATmega328P__) || defined(__AVR_ATmega328PB__)
86+
// 0/1 series
87+
#elif defined(__AVR_ATmega640__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega1281__) \
88+
|| defined(__AVR_ATmega2560__) || defined(__AVR_ATmega2561__)
9189
#define EXTERNAL 0
9290
#define DEFAULT 1 // Default -> AVCC with external capacitor at AREF pin
93-
#define INTERNAL1V1 3
91+
#define INTERNAL1V1 2
92+
#define INTERNAL2V56 3
9493
#define INTERNAL 3
9594

96-
// ATmega640, ATmega1280, ATmega1281, ATmega2560, ATmega2561
97-
#elif defined(__AVR_ATmega640__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega1281__) \
98-
|| defined(__AVR_ATmega2560__) || defined(__AVR_ATmega2561__)
95+
// 4 series
96+
#elif defined(__AVR_ATmega164A__) || defined(__AVR_ATmega164P__) || defined(__AVR_ATmega324A__) \
97+
|| defined(__AVR_ATmega324P__) || defined(__AVR_ATmega324PA__) || defined(__AVR_ATmega324PB__) \
98+
|| defined(__AVR_ATmega644A__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega1284__) \
99+
|| defined(__AVR_ATmega1284P__)
99100
#define EXTERNAL 0
100101
#define DEFAULT 1 // Default -> AVCC with external capacitor at AREF pin
101102
#define INTERNAL1V1 2
102103
#define INTERNAL2V56 3
103104
#define INTERNAL 3
104105

106+
// 5 series
107+
#elif defined(__AVR_ATmega165__) || defined(__AVR_ATmega165A__) || defined(__AVR_ATmega165P__) \
108+
|| defined(__AVR_ATmega165PA__) || defined(__AVR_ATmega325__) || defined(__AVR_ATmega325A__) \
109+
|| defined(__AVR_ATmega325P__) || defined(__AVR_ATmega325PA__) || defined(__AVR_ATmega3250__) \
110+
|| defined(__AVR_ATmega3250A__) || defined(__AVR_ATmega3250P__) || defined(__AVR_ATmega3250PA__) \
111+
|| defined(__AVR_ATmega645__) || defined(__AVR_ATmega645A__) || defined(__AVR_ATmega645P__) \
112+
|| defined(__AVR_ATmega6450__) || defined(__AVR_ATmega6450A__) || defined(__AVR_ATmega6450P__)
113+
#define EXTERNAL 0
114+
#define DEFAULT 1 // Default -> AVCC with external capacitor at AREF pin
115+
#define INTERNAL1V1 3
116+
#define INTERNAL 3
105117

106-
// ATmega164A/P, ATmega324A/P/PA/PB, ATmega644/P, ATmega1284/P
107-
#elif defined(__AVR_ATmega164A__) || defined(__AVR_ATmega164P__) || defined(__AVR_ATmega324A__) \
108-
|| defined(__AVR_ATmega324P__) || defined(__AVR_ATmega324PA__) || defined(__AVR_ATmega324PB__) \
109-
|| defined(__AVR_ATmega644A__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega1284__) \
110-
|| defined(__AVR_ATmega1284P__)
118+
// 8 series
119+
#elif defined(__AVR_ATmega48__) || defined(__AVR_ATmega48P__) || defined(__AVR_ATmega48PB__) \
120+
|| defined(__AVR_ATmega88__) || defined(__AVR_ATmega88P__) || defined(__AVR_ATmega88PB__) \
121+
|| defined(__AVR_ATmega168__) || defined(__AVR_ATmega168P__) || defined(__AVR_ATmega168PB__) \
122+
|| defined(__AVR_ATmega328__) || defined(__AVR_ATmega328P__) || defined(__AVR_ATmega328PB__)
111123
#define EXTERNAL 0
112124
#define DEFAULT 1 // Default -> AVCC with external capacitor at AREF pin
113-
#define INTERNAL1V1 2
114-
#define INTERNAL2V56 3
125+
#define INTERNAL1V1 3
115126
#define INTERNAL 3
116127

117-
#endif
128+
// 9 series
129+
#elif defined(__AVR_ATmega169__) || defined(__AVR_ATmega169A__) || defined(__AVR_ATmega169P__) \
130+
|| defined(__AVR_ATmega169PA__) || defined(__AVR_ATmega329__) || defined(__AVR_ATmega329A__) \
131+
|| defined(__AVR_ATmega329P__) || defined(__AVR_ATmega329PA__) || defined(__AVR_ATmega3290__) \
132+
|| defined(__AVR_ATmega3290A__) || defined(__AVR_ATmega3290P__) || defined(__AVR_ATmega3290PA__) \
133+
|| defined(__AVR_ATmega649__) || defined(__AVR_ATmega649A__) || defined(__AVR_ATmega649P__) \
134+
|| defined(__AVR_ATmega6490__) || defined(__AVR_ATmega6490A__) || defined(__AVR_ATmega6490P__)
135+
#define EXTERNAL 0
136+
#define DEFAULT 1 // Default -> AVCC with external capacitor at AREF pin
137+
#define INTERNAL1V1 3
138+
#define INTERNAL 3
118139

119-
// undefine stdlib's abs if encountered
120-
#ifdef abs
121-
#undef abs
122140
#endif
123141

124-
#define abs(x) ({ typeof (x) _x = (x); _x > 0 ? _x : -_x; })
125-
#define sq(x) ({ typeof (x) _x = (x); _x * _x; })
126-
#define min(a,b) ({ typeof (a) _a = (a); typeof (b) _b = (b); _a < _b ? _a : _b; })
127-
#define max(a,b) ({ typeof (a) _a = (a); typeof (b) _b = (b); _a > _b ? _a : _b; })
128-
#define round(x) ({ typeof (x) _x = (x); _x >= 0 ? (long)(_x + 0.5) : (long)(_x - 0.5); })
129-
#define radians(deg) ((deg) * DEG_TO_RAD)
130-
#define degrees(rad) ((rad) * RAD_TO_DEG)
131-
#define constrain(x,low,high) ({ \
132-
typeof (x) _x = (x); \
133-
typeof (low) _l = (low); \
134-
typeof (high) _h = (high); \
135-
_x < _l ? _l : _x > _h ? _h : _x; })
136-
137142
#define interrupts() sei()
138143
#define noInterrupts() cli()
139144

@@ -293,6 +298,66 @@ extern const uint8_t PROGMEM digital_pin_to_timer_PGM[];
293298
} // extern "C"
294299
#endif
295300

301+
// Undefine stdlib's abs if encountered
302+
#ifdef abs
303+
#undef abs
304+
#endif
305+
306+
#ifdef __cplusplus
307+
template<class T>
308+
auto abs(const T& x) -> decltype(x > 0 ? x : -x) {
309+
return x > 0 ? x : -x;
310+
}
311+
312+
template<class T, class L>
313+
auto min(const T& a, const L& b) -> decltype((b < a) ? b : a) {
314+
return (b < a) ? b : a;
315+
}
316+
317+
template<class T, class L>
318+
auto max(const T& a, const L& b) -> decltype((b < a) ? b : a) {
319+
return (a < b) ? b : a;
320+
}
321+
322+
template<class T>
323+
long round(const T& x) {
324+
return (long)(x >= 0 ? (x + 0.5) : (x - 0.5));
325+
}
326+
327+
template<class T>
328+
auto sq(const T& x) -> decltype(x * x) {
329+
return x * x;
330+
}
331+
332+
template<class T>
333+
auto radians(const T& deg) -> decltype(deg * DEG_TO_RAD) {
334+
return deg * DEG_TO_RAD;
335+
}
336+
337+
template<class T>
338+
auto degrees(const T& rad) -> decltype(rad * RAD_TO_DEG) {
339+
return rad * RAD_TO_DEG;
340+
}
341+
342+
template<class T, class L, class H>
343+
auto constrain(const T& x, const L& l, const H& h) -> decltype((x < l) ? l : (x > h) ? h : x) {
344+
return (x < l) ? l : (x > h) ? h : x;
345+
}
346+
347+
#else
348+
#define abs(x) ({ typeof (x) _x = (x); _x > 0 ? _x : -_x; })
349+
#define min(a,b) ({ typeof (a) _a = (a); typeof (b) _b = (b); _a < _b ? _a : _b; })
350+
#define max(a,b) ({ typeof (a) _a = (a); typeof (b) _b = (b); _a > _b ? _a : _b; })
351+
#define sq(x) ({ typeof (x) _x = (x); _x * _x; })
352+
#define radians(deg) ((deg) * DEG_TO_RAD)
353+
#define degrees(rad) ((rad) * RAD_TO_DEG)
354+
#define constrain(x,low,high) ({ \
355+
typeof (x) _x = (x); \
356+
typeof (low) _l = (low); \
357+
typeof (high) _h = (high); \
358+
_x < _l ? _l : _x > _h ? _h : _x; })
359+
#endif // __cplusplus
360+
296361
#ifdef __cplusplus
297362
#include "WCharacter.h"
298363
#include "WString.h"

HardwareSerial0.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@
3636

3737
#if defined(HAVE_HWSERIAL0)
3838

39-
#if defined(USART_RX_vect)
40-
ISR(USART_RX_vect)
41-
#elif defined(USART0_RX_vect)
42-
ISR(USART0_RX_vect)
43-
#elif defined(USART0_RXC_vect)
39+
#if defined(USART0_RXC_vect)
4440
ISR(USART0_RXC_vect)
4541
#elif defined(USART_RXC_vect)
4642
ISR(USART_RXC_vect)
43+
#elif defined(USART0_RX_vect)
44+
ISR(USART0_RX_vect)
45+
#elif defined(USART_RX_vect)
46+
ISR(USART_RX_vect)
4747
#else
4848
#error "Don't know what the Data Received vector is called for Serial"
4949
#endif

HardwareSerial_private.h

+3-6
Original file line numberDiff line numberDiff line change
@@ -67,18 +67,15 @@
6767
// UART0 for the other UARTs as well, in case these values ever get
6868
// changed for future hardware.
6969
#if defined(TXC1) && (TXC1 != TXC0 || RXEN1 != RXEN0 || RXCIE1 != RXCIE0 || \
70-
UDRIE1 != UDRIE0 || U2X1 != U2X0 || UPE1 != UPE0 || \
71-
UDRE1 != UDRE0)
70+
UDRIE1 != UDRIE0 || U2X1 != U2X0 || UPE1 != UPE0 || UDRE1 != UDRE0)
7271
#error "Not all bit positions for UART1 are the same as for UART0"
7372
#endif
7473
#if defined(TXC2) && (TXC2 != TXC0 || RXEN2 != RXEN0 || RXCIE2 != RXCIE0 || \
75-
UDRIE2 != UDRIE0 || U2X2 != U2X0 || UPE2 != UPE0 || \
76-
UDRE2 != UDRE0)
74+
UDRIE2 != UDRIE0 || U2X2 != U2X0 || UPE2 != UPE0 || UDRE2 != UDRE0)
7775
#error "Not all bit positions for UART2 are the same as for UART0"
7876
#endif
7977
#if defined(TXC3) && (TXC3 != TXC0 || RXEN3 != RXEN0 || RXCIE3 != RXCIE0 || \
80-
UDRIE3 != UDRIE0 || U3X3 != U3X0 || UPE3 != UPE0 || \
81-
UDRE3 != UDRE0)
78+
UDRIE3 != UDRIE0 || U2X3 != U2X0 || UPE3 != UPE0 || UDRE3 != UDRE0)
8279
#error "Not all bit positions for UART3 are the same as for UART0"
8380
#endif
8481

Print.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ class Print
4444
void setWriteError(int err = 1) { write_error = err; }
4545
public:
4646
Print() : write_error(0) {}
47-
47+
4848
int getWriteError() { return write_error; }
4949
void clearWriteError() { setWriteError(0); }
50-
50+
5151
virtual size_t write(uint8_t) = 0;
5252
size_t write(const char *str) {
5353
if (str == NULL) return 0;
@@ -88,8 +88,8 @@ class Print
8888
size_t println(double, int = 2);
8989
size_t println(const Printable&);
9090
size_t println(void);
91-
92-
int16_t printf(const char *format, ...);
91+
92+
int16_t printf(const char *format, ...) __attribute__ ((format (printf, 2, 3)));
9393
int16_t printf(const __FlashStringHelper *format, ...);
9494
};
9595

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ This repo contains the Arduino corefiles used with [MightyCore](https://github.c
1212
* ATmega8535, ATmega16, ATmega32, ATmega164A/P, ATmega324A/P/PA/PB, ATmega644/P, ATmega1284/P
1313
* ATmega8515, ATmega162
1414
* ATmega8, ATmega48/P/PA/PB, ATmega88/P/PA/PB, ATmega168/P/PA/PB, ATmega328/P/PA/PB
15+
* ATmega165/A/P/PA, ATmega325/A/P/PA, ATmega645/A/P
16+
* ATmega3250/A/P, ATmega6450/A/P
17+
* ATmega169/A/P/PA, ATmega329/A/P/PA, ATmega649/A/P
18+
* ATmega3290/A/P, ATmega6490/A/P
1519

1620

1721
## Supported clock frequencies

Tone.cpp

+28-11
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,24 @@ volatile uint8_t timer2_pin_mask;
126126
#define USE_TIMER3
127127
const uint8_t PROGMEM tone_pin_to_timer_PGM[] = { 3 /*, 1 */ };
128128
static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { 255 /*, 255 */ };
129-
129+
130+
#elif defined(__AVR_ATmega165__) || defined(__AVR_ATmega165A__) || defined(__AVR_ATmega165P__) \
131+
|| defined(__AVR_ATmega165PA__) || defined(__AVR_ATmega325__) || defined(__AVR_ATmega325A__) \
132+
|| defined(__AVR_ATmega325P__) || defined(__AVR_ATmega325PA__) || defined(__AVR_ATmega3250__) \
133+
|| defined(__AVR_ATmega3250A__) || defined(__AVR_ATmega3250P__) || defined(__AVR_ATmega3250PA__) \
134+
|| defined(__AVR_ATmega645__) || defined(__AVR_ATmega645A__) || defined(__AVR_ATmega645P__) \
135+
|| defined(__AVR_ATmega6450__) || defined(__AVR_ATmega6450A__) || defined(__AVR_ATmega6450P__) \
136+
|| defined(__AVR_ATmega169__) || defined(__AVR_ATmega169A__) || defined(__AVR_ATmega169P__) \
137+
|| defined(__AVR_ATmega169PA__) || defined(__AVR_ATmega329__) || defined(__AVR_ATmega329A__) \
138+
|| defined(__AVR_ATmega329P__) || defined(__AVR_ATmega329PA__) || defined(__AVR_ATmega3290__) \
139+
|| defined(__AVR_ATmega3290A__) || defined(__AVR_ATmega3290P__) || defined(__AVR_ATmega3290PA__) \
140+
|| defined(__AVR_ATmega649__) || defined(__AVR_ATmega649A__) || defined(__AVR_ATmega649P__) \
141+
|| defined(__AVR_ATmega6490__) || defined(__AVR_ATmega6490A__) || defined(__AVR_ATmega6490P__)
142+
#define AVAILABLE_TONE_PINS 1
143+
#define USE_TIMER1
144+
const uint8_t PROGMEM tone_pin_to_timer_PGM[] = { 1 };
145+
static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { 255 };
146+
130147
#else // Generic
131148
#define AVAILABLE_TONE_PINS 1
132149
#define USE_TIMER2
@@ -136,26 +153,26 @@ volatile uint8_t timer2_pin_mask;
136153
#endif
137154

138155
// Identifies the timer which has prescaler values 1/8/32/64/128/256/1024
139-
// Other timers are lacking 32 and 128 because clock select bits are used
156+
// Other timers are lacking 32 and 128 because clock select bits are used
140157
// for external clock input
141158
#if defined(__AVR_ATmega64__) || defined(__AVR_ATmega128__)
142-
#define TIMER_WITH_FULL_PRESCALER 0
159+
#define TIMER_WITH_FULL_PRESCALER 0
143160
#else
144-
#define TIMER_WITH_FULL_PRESCALER 2
161+
#define TIMER_WITH_FULL_PRESCALER 2
145162
#endif
146163

147164

148165
static int8_t toneBegin(uint8_t _pin)
149166
{
150167
int8_t _timer = -1;
151168

152-
// if we're already using the pin, the timer should be configured.
169+
// if we're already using the pin, the timer should be configured.
153170
for (int i = 0; i < AVAILABLE_TONE_PINS; i++) {
154171
if (tone_pins[i] == _pin) {
155172
return pgm_read_byte(tone_pin_to_timer_PGM + i);
156173
}
157174
}
158-
175+
159176
// search for an unused timer.
160177
for (int i = 0; i < AVAILABLE_TONE_PINS; i++) {
161178
if (tone_pins[i] == 255) {
@@ -164,7 +181,7 @@ static int8_t toneBegin(uint8_t _pin)
164181
break;
165182
}
166183
}
167-
184+
168185
if (_timer != -1)
169186
{
170187
// Set timer specific stuff
@@ -273,7 +290,7 @@ void tone(uint8_t _pin, unsigned int frequency, unsigned long duration)
273290
{
274291
// Set the pinMode as OUTPUT
275292
pinMode(_pin, OUTPUT);
276-
293+
277294
// if we are using an 8 bit timer, scan through prescalars to find the best fit
278295
if (_timer == 0 || _timer == 2)
279296
{
@@ -364,7 +381,7 @@ void tone(uint8_t _pin, unsigned int frequency, unsigned long duration)
364381
#endif
365382

366383
}
367-
384+
368385

369386
// Calculate the toggle count
370387
if (duration > 0)
@@ -500,15 +517,15 @@ void disableTimer(uint8_t _timer)
500517
void noTone(uint8_t _pin)
501518
{
502519
int8_t _timer = -1;
503-
520+
504521
for (int i = 0; i < AVAILABLE_TONE_PINS; i++) {
505522
if (tone_pins[i] == _pin) {
506523
_timer = pgm_read_byte(tone_pin_to_timer_PGM + i);
507524
tone_pins[i] = 255;
508525
break;
509526
}
510527
}
511-
528+
512529
disableTimer(_timer);
513530

514531
digitalWrite(_pin, 0);

0 commit comments

Comments
 (0)