Skip to content

Commit

Permalink
Squashed 'avr/cores/MCUdude_corefiles/' changes from 278334f48..0d56e…
Browse files Browse the repository at this point in the history
…cc53

0d56ecc53 Use template based functions for radians() and degrees() by default
a9ade41df Use template functions instead of macros by default This will make it possible to use the previous "typeof" macros outside functions and structures
b811cd119 Use template based min/max functions by default
5ddffca3f Reorder hardware serial macros ... to ensure no poisoned macros are being used
21b32d156 Add support for AVR 5 and 9 series
88a427b0f Add support for AVR 5 and 9 series
54808586e Reorder macro checking To prevent GCC poison error on when compiling for ATmega165
b1703a37e Initial support for AVR ATmega 5 and 9-series ATmega165/325/645, ATmega3250/6450, ATmega169/239/649, ATmega3290/6490
6b36d8f1a Fix typo More detals here: arduino/ArduinoCore-avr#466
ecb12fa46 Add GCC printf formatting checks

git-subtree-dir: avr/cores/MCUdude_corefiles
git-subtree-split: 0d56ecc539d7047899835fdc040d8748c3afc564
  • Loading branch information
MCUdude committed Jan 15, 2023
1 parent fdfba9f commit 65f1bee
Show file tree
Hide file tree
Showing 7 changed files with 278 additions and 140 deletions.
137 changes: 101 additions & 36 deletions Arduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,65 +75,70 @@ void yield(void);

/* Analog reference definitions */

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

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

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

// 5 series
#elif defined(__AVR_ATmega165__) || defined(__AVR_ATmega165A__) || defined(__AVR_ATmega165P__) \
|| defined(__AVR_ATmega165PA__) || defined(__AVR_ATmega325__) || defined(__AVR_ATmega325A__) \
|| defined(__AVR_ATmega325P__) || defined(__AVR_ATmega325PA__) || defined(__AVR_ATmega3250__) \
|| defined(__AVR_ATmega3250A__) || defined(__AVR_ATmega3250P__) || defined(__AVR_ATmega3250PA__) \
|| defined(__AVR_ATmega645__) || defined(__AVR_ATmega645A__) || defined(__AVR_ATmega645P__) \
|| defined(__AVR_ATmega6450__) || defined(__AVR_ATmega6450A__) || defined(__AVR_ATmega6450P__)
#define EXTERNAL 0
#define DEFAULT 1 // Default -> AVCC with external capacitor at AREF pin
#define INTERNAL1V1 3
#define INTERNAL 3

// ATmega164A/P, ATmega324A/P/PA/PB, ATmega644/P, ATmega1284/P
#elif defined(__AVR_ATmega164A__) || defined(__AVR_ATmega164P__) || defined(__AVR_ATmega324A__) \
|| defined(__AVR_ATmega324P__) || defined(__AVR_ATmega324PA__) || defined(__AVR_ATmega324PB__) \
|| defined(__AVR_ATmega644A__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega1284__) \
|| defined(__AVR_ATmega1284P__)
// 8 series
#elif defined(__AVR_ATmega48__) || defined(__AVR_ATmega48P__) || defined(__AVR_ATmega48PB__) \
|| defined(__AVR_ATmega88__) || defined(__AVR_ATmega88P__) || defined(__AVR_ATmega88PB__) \
|| defined(__AVR_ATmega168__) || defined(__AVR_ATmega168P__) || defined(__AVR_ATmega168PB__) \
|| defined(__AVR_ATmega328__) || defined(__AVR_ATmega328P__) || defined(__AVR_ATmega328PB__)
#define EXTERNAL 0
#define DEFAULT 1 // Default -> AVCC with external capacitor at AREF pin
#define INTERNAL1V1 2
#define INTERNAL2V56 3
#define INTERNAL1V1 3
#define INTERNAL 3

#endif
// 9 series
#elif defined(__AVR_ATmega169__) || defined(__AVR_ATmega169A__) || defined(__AVR_ATmega169P__) \
|| defined(__AVR_ATmega169PA__) || defined(__AVR_ATmega329__) || defined(__AVR_ATmega329A__) \
|| defined(__AVR_ATmega329P__) || defined(__AVR_ATmega329PA__) || defined(__AVR_ATmega3290__) \
|| defined(__AVR_ATmega3290A__) || defined(__AVR_ATmega3290P__) || defined(__AVR_ATmega3290PA__) \
|| defined(__AVR_ATmega649__) || defined(__AVR_ATmega649A__) || defined(__AVR_ATmega649P__) \
|| defined(__AVR_ATmega6490__) || defined(__AVR_ATmega6490A__) || defined(__AVR_ATmega6490P__)
#define EXTERNAL 0
#define DEFAULT 1 // Default -> AVCC with external capacitor at AREF pin
#define INTERNAL1V1 3
#define INTERNAL 3

// undefine stdlib's abs if encountered
#ifdef abs
#undef abs
#endif

#define abs(x) ({ typeof (x) _x = (x); _x > 0 ? _x : -_x; })
#define sq(x) ({ typeof (x) _x = (x); _x * _x; })
#define min(a,b) ({ typeof (a) _a = (a); typeof (b) _b = (b); _a < _b ? _a : _b; })
#define max(a,b) ({ typeof (a) _a = (a); typeof (b) _b = (b); _a > _b ? _a : _b; })
#define round(x) ({ typeof (x) _x = (x); _x >= 0 ? (long)(_x + 0.5) : (long)(_x - 0.5); })
#define radians(deg) ((deg) * DEG_TO_RAD)
#define degrees(rad) ((rad) * RAD_TO_DEG)
#define constrain(x,low,high) ({ \
typeof (x) _x = (x); \
typeof (low) _l = (low); \
typeof (high) _h = (high); \
_x < _l ? _l : _x > _h ? _h : _x; })

#define interrupts() sei()
#define noInterrupts() cli()

Expand Down Expand Up @@ -293,6 +298,66 @@ extern const uint8_t PROGMEM digital_pin_to_timer_PGM[];
} // extern "C"
#endif

// Undefine stdlib's abs if encountered
#ifdef abs
#undef abs
#endif

#ifdef __cplusplus
template<class T>
auto abs(const T& x) -> decltype(x > 0 ? x : -x) {
return x > 0 ? x : -x;
}

template<class T, class L>
auto min(const T& a, const L& b) -> decltype((b < a) ? b : a) {
return (b < a) ? b : a;
}

template<class T, class L>
auto max(const T& a, const L& b) -> decltype((b < a) ? b : a) {
return (a < b) ? b : a;
}

template<class T>
long round(const T& x) {
return (long)(x >= 0 ? (x + 0.5) : (x - 0.5));
}

template<class T>
auto sq(const T& x) -> decltype(x * x) {
return x * x;
}

template<class T>
auto radians(const T& deg) -> decltype(deg * DEG_TO_RAD) {
return deg * DEG_TO_RAD;
}

template<class T>
auto degrees(const T& rad) -> decltype(rad * RAD_TO_DEG) {
return rad * RAD_TO_DEG;
}

template<class T, class L, class H>
auto constrain(const T& x, const L& l, const H& h) -> decltype((x < l) ? l : (x > h) ? h : x) {
return (x < l) ? l : (x > h) ? h : x;
}

#else
#define abs(x) ({ typeof (x) _x = (x); _x > 0 ? _x : -_x; })
#define min(a,b) ({ typeof (a) _a = (a); typeof (b) _b = (b); _a < _b ? _a : _b; })
#define max(a,b) ({ typeof (a) _a = (a); typeof (b) _b = (b); _a > _b ? _a : _b; })
#define sq(x) ({ typeof (x) _x = (x); _x * _x; })
#define radians(deg) ((deg) * DEG_TO_RAD)
#define degrees(rad) ((rad) * RAD_TO_DEG)
#define constrain(x,low,high) ({ \
typeof (x) _x = (x); \
typeof (low) _l = (low); \
typeof (high) _h = (high); \
_x < _l ? _l : _x > _h ? _h : _x; })
#endif // __cplusplus

#ifdef __cplusplus
#include "WCharacter.h"
#include "WString.h"
Expand Down
10 changes: 5 additions & 5 deletions HardwareSerial0.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@

#if defined(HAVE_HWSERIAL0)

#if defined(USART_RX_vect)
ISR(USART_RX_vect)
#elif defined(USART0_RX_vect)
ISR(USART0_RX_vect)
#elif defined(USART0_RXC_vect)
#if defined(USART0_RXC_vect)
ISR(USART0_RXC_vect)
#elif defined(USART_RXC_vect)
ISR(USART_RXC_vect)
#elif defined(USART0_RX_vect)
ISR(USART0_RX_vect)
#elif defined(USART_RX_vect)
ISR(USART_RX_vect)
#else
#error "Don't know what the Data Received vector is called for Serial"
#endif
Expand Down
9 changes: 3 additions & 6 deletions HardwareSerial_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,15 @@
// UART0 for the other UARTs as well, in case these values ever get
// changed for future hardware.
#if defined(TXC1) && (TXC1 != TXC0 || RXEN1 != RXEN0 || RXCIE1 != RXCIE0 || \
UDRIE1 != UDRIE0 || U2X1 != U2X0 || UPE1 != UPE0 || \
UDRE1 != UDRE0)
UDRIE1 != UDRIE0 || U2X1 != U2X0 || UPE1 != UPE0 || UDRE1 != UDRE0)
#error "Not all bit positions for UART1 are the same as for UART0"
#endif
#if defined(TXC2) && (TXC2 != TXC0 || RXEN2 != RXEN0 || RXCIE2 != RXCIE0 || \
UDRIE2 != UDRIE0 || U2X2 != U2X0 || UPE2 != UPE0 || \
UDRE2 != UDRE0)
UDRIE2 != UDRIE0 || U2X2 != U2X0 || UPE2 != UPE0 || UDRE2 != UDRE0)
#error "Not all bit positions for UART2 are the same as for UART0"
#endif
#if defined(TXC3) && (TXC3 != TXC0 || RXEN3 != RXEN0 || RXCIE3 != RXCIE0 || \
UDRIE3 != UDRIE0 || U3X3 != U3X0 || UPE3 != UPE0 || \
UDRE3 != UDRE0)
UDRIE3 != UDRIE0 || U2X3 != U2X0 || UPE3 != UPE0 || UDRE3 != UDRE0)
#error "Not all bit positions for UART3 are the same as for UART0"
#endif

Expand Down
8 changes: 4 additions & 4 deletions Print.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ class Print
void setWriteError(int err = 1) { write_error = err; }
public:
Print() : write_error(0) {}

int getWriteError() { return write_error; }
void clearWriteError() { setWriteError(0); }

virtual size_t write(uint8_t) = 0;
size_t write(const char *str) {
if (str == NULL) return 0;
Expand Down Expand Up @@ -88,8 +88,8 @@ class Print
size_t println(double, int = 2);
size_t println(const Printable&);
size_t println(void);
int16_t printf(const char *format, ...);

int16_t printf(const char *format, ...) __attribute__ ((format (printf, 2, 3)));
int16_t printf(const __FlashStringHelper *format, ...);
};

Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ This repo contains the Arduino corefiles used with [MightyCore](https://github.c
* ATmega8535, ATmega16, ATmega32, ATmega164A/P, ATmega324A/P/PA/PB, ATmega644/P, ATmega1284/P
* ATmega8515, ATmega162
* ATmega8, ATmega48/P/PA/PB, ATmega88/P/PA/PB, ATmega168/P/PA/PB, ATmega328/P/PA/PB
* ATmega165/A/P/PA, ATmega325/A/P/PA, ATmega645/A/P
* ATmega3250/A/P, ATmega6450/A/P
* ATmega169/A/P/PA, ATmega329/A/P/PA, ATmega649/A/P
* ATmega3290/A/P, ATmega6490/A/P


## Supported clock frequencies
Expand Down
39 changes: 28 additions & 11 deletions Tone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,24 @@ volatile uint8_t timer2_pin_mask;
#define USE_TIMER3
const uint8_t PROGMEM tone_pin_to_timer_PGM[] = { 3 /*, 1 */ };
static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { 255 /*, 255 */ };


#elif defined(__AVR_ATmega165__) || defined(__AVR_ATmega165A__) || defined(__AVR_ATmega165P__) \
|| defined(__AVR_ATmega165PA__) || defined(__AVR_ATmega325__) || defined(__AVR_ATmega325A__) \
|| defined(__AVR_ATmega325P__) || defined(__AVR_ATmega325PA__) || defined(__AVR_ATmega3250__) \
|| defined(__AVR_ATmega3250A__) || defined(__AVR_ATmega3250P__) || defined(__AVR_ATmega3250PA__) \
|| defined(__AVR_ATmega645__) || defined(__AVR_ATmega645A__) || defined(__AVR_ATmega645P__) \
|| defined(__AVR_ATmega6450__) || defined(__AVR_ATmega6450A__) || defined(__AVR_ATmega6450P__) \
|| defined(__AVR_ATmega169__) || defined(__AVR_ATmega169A__) || defined(__AVR_ATmega169P__) \
|| defined(__AVR_ATmega169PA__) || defined(__AVR_ATmega329__) || defined(__AVR_ATmega329A__) \
|| defined(__AVR_ATmega329P__) || defined(__AVR_ATmega329PA__) || defined(__AVR_ATmega3290__) \
|| defined(__AVR_ATmega3290A__) || defined(__AVR_ATmega3290P__) || defined(__AVR_ATmega3290PA__) \
|| defined(__AVR_ATmega649__) || defined(__AVR_ATmega649A__) || defined(__AVR_ATmega649P__) \
|| defined(__AVR_ATmega6490__) || defined(__AVR_ATmega6490A__) || defined(__AVR_ATmega6490P__)
#define AVAILABLE_TONE_PINS 1
#define USE_TIMER1
const uint8_t PROGMEM tone_pin_to_timer_PGM[] = { 1 };
static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { 255 };

#else // Generic
#define AVAILABLE_TONE_PINS 1
#define USE_TIMER2
Expand All @@ -136,26 +153,26 @@ volatile uint8_t timer2_pin_mask;
#endif

// Identifies the timer which has prescaler values 1/8/32/64/128/256/1024
// Other timers are lacking 32 and 128 because clock select bits are used
// Other timers are lacking 32 and 128 because clock select bits are used
// for external clock input
#if defined(__AVR_ATmega64__) || defined(__AVR_ATmega128__)
#define TIMER_WITH_FULL_PRESCALER 0
#define TIMER_WITH_FULL_PRESCALER 0
#else
#define TIMER_WITH_FULL_PRESCALER 2
#define TIMER_WITH_FULL_PRESCALER 2
#endif


static int8_t toneBegin(uint8_t _pin)
{
int8_t _timer = -1;

// if we're already using the pin, the timer should be configured.
// if we're already using the pin, the timer should be configured.
for (int i = 0; i < AVAILABLE_TONE_PINS; i++) {
if (tone_pins[i] == _pin) {
return pgm_read_byte(tone_pin_to_timer_PGM + i);
}
}

// search for an unused timer.
for (int i = 0; i < AVAILABLE_TONE_PINS; i++) {
if (tone_pins[i] == 255) {
Expand All @@ -164,7 +181,7 @@ static int8_t toneBegin(uint8_t _pin)
break;
}
}

if (_timer != -1)
{
// Set timer specific stuff
Expand Down Expand Up @@ -273,7 +290,7 @@ void tone(uint8_t _pin, unsigned int frequency, unsigned long duration)
{
// Set the pinMode as OUTPUT
pinMode(_pin, OUTPUT);

// if we are using an 8 bit timer, scan through prescalars to find the best fit
if (_timer == 0 || _timer == 2)
{
Expand Down Expand Up @@ -364,7 +381,7 @@ void tone(uint8_t _pin, unsigned int frequency, unsigned long duration)
#endif

}


// Calculate the toggle count
if (duration > 0)
Expand Down Expand Up @@ -500,15 +517,15 @@ void disableTimer(uint8_t _timer)
void noTone(uint8_t _pin)
{
int8_t _timer = -1;

for (int i = 0; i < AVAILABLE_TONE_PINS; i++) {
if (tone_pins[i] == _pin) {
_timer = pgm_read_byte(tone_pin_to_timer_PGM + i);
tone_pins[i] = 255;
break;
}
}

disableTimer(_timer);

digitalWrite(_pin, 0);
Expand Down
Loading

0 comments on commit 65f1bee

Please sign in to comment.