diff --git a/hardware/arduino/cores/arduino/wiring.c b/hardware/arduino/cores/arduino/wiring.c index eb2d44001..b90d07e59 100755 --- a/hardware/arduino/cores/arduino/wiring.c +++ b/hardware/arduino/cores/arduino/wiring.c @@ -80,7 +80,14 @@ unsigned long micros() { cli(); m = timer0_overflow_count; +#if defined(TCNT0) t = TCNT0; +#elif defined(TCNT0L) + t = TCNT0L; +#else + #error TIMER 0 not defined +#endif + #ifdef TIFR0 if ((TIFR0 & _BV(TOV0)) && (t < 255)) @@ -166,62 +173,99 @@ void init() // on the ATmega168, timer 0 is also used for fast hardware pwm // (using phase-correct PWM would mean that timer 0 overflowed half as often // resulting in different millis() behavior on the ATmega8 and ATmega168) -#if !defined(__AVR_ATmega8__) +#if defined(TCCR0A) && defined(WGM01) sbi(TCCR0A, WGM01); sbi(TCCR0A, WGM00); #endif + // set timer 0 prescale factor to 64 -#if defined(__AVR_ATmega8__) +#if defined(__AVR_ATmega128__) + // CPU specific: different values for the ATmega128 + sbi(TCCR0, CS02); +#elif defined(TCCR0) && defined(CS01) && defined(CS00) + // this combination is for the standard atmega8 sbi(TCCR0, CS01); sbi(TCCR0, CS00); -#else +#elif defined(TCCR0B) && defined(CS01) && defined(CS00) + // this combination is for the standard 168/328/1280/2560 sbi(TCCR0B, CS01); sbi(TCCR0B, CS00); -#endif - // enable timer 0 overflow interrupt -#if defined(__AVR_ATmega8__) - sbi(TIMSK, TOIE0); +#elif defined(TCCR0A) && defined(CS01) && defined(CS00) + // this combination is for the __AVR_ATmega645__ series + sbi(TCCR0A, CS01); + sbi(TCCR0A, CS00); #else + #error Timer 0 prescale factor 64 not set correctly +#endif + + // enable timer 0 overflow interrupt +#if defined(TIMSK) && defined(TOIE0) + sbi(TIMSK, TOIE0); +#elif defined(TIMSK0) && defined(TOIE0) sbi(TIMSK0, TOIE0); +#else + #error Timer 0 overflow interrupt not set correctly #endif // timers 1 and 2 are used for phase-correct hardware pwm // this is better for motors as it ensures an even waveform // note, however, that fast pwm mode can achieve a frequency of up // 8 MHz (with a 16 MHz clock) at 50% duty cycle - - TCCR1B = 0; + + TCCR1B = 0; // set timer 1 prescale factor to 64 +#if defined(TCCR1B) && defined(CS11) && defined(CS10) sbi(TCCR1B, CS11); sbi(TCCR1B, CS10); +#elif defined(TCCR1) && defined(CS11) && defined(CS10) + sbi(TCCR1, CS11); + sbi(TCCR1, CS10); +#endif // put timer 1 in 8-bit phase correct pwm mode +#if defined(TCCR1A) && defined(WGM10) sbi(TCCR1A, WGM10); +#elif defined(TCCR1) + #warning this needs to be finished +#endif // set timer 2 prescale factor to 64 -#if defined(__AVR_ATmega8__) +#if defined(TCCR2) && defined(CS22) sbi(TCCR2, CS22); -#else +#elif defined(TCCR2B) && defined(CS22) sbi(TCCR2B, CS22); -#endif - // configure timer 2 for phase correct pwm (8-bit) -#if defined(__AVR_ATmega8__) - sbi(TCCR2, WGM20); #else + #warning Timer 2 not finished (may not be present on this CPU) +#endif + + // configure timer 2 for phase correct pwm (8-bit) +#if defined(TCCR2) && defined(WGM20) + sbi(TCCR2, WGM20); +#elif defined(TCCR2A) && defined(WGM20) sbi(TCCR2A, WGM20); +#else + #warning Timer 2 not finished (may not be present on this CPU) #endif -#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) - // set timer 3, 4, 5 prescale factor to 64 - sbi(TCCR3B, CS31); sbi(TCCR3B, CS30); - sbi(TCCR4B, CS41); sbi(TCCR4B, CS40); - sbi(TCCR5B, CS51); sbi(TCCR5B, CS50); - // put timer 3, 4, 5 in 8-bit phase correct pwm mode - sbi(TCCR3A, WGM30); - sbi(TCCR4A, WGM40); - sbi(TCCR5A, WGM50); +#if defined(TCCR3B) && defined(CS31) && defined(WGM30) + sbi(TCCR3B, CS31); // set timer 3 prescale factor to 64 + sbi(TCCR3B, CS30); + sbi(TCCR3A, WGM30); // put timer 3 in 8-bit phase correct pwm mode +#endif + +#if defined(TCCR4B) && defined(CS41) && defined(WGM40) + sbi(TCCR4B, CS41); // set timer 4 prescale factor to 64 + sbi(TCCR4B, CS40); + sbi(TCCR4A, WGM40); // put timer 4 in 8-bit phase correct pwm mode #endif +#if defined(TCCR5B) && defined(CS51) && defined(WGM50) + sbi(TCCR5B, CS51); // set timer 5 prescale factor to 64 + sbi(TCCR5B, CS50); + sbi(TCCR5A, WGM50); // put timer 5 in 8-bit phase correct pwm mode +#endif + +#if defined(ADCSRA) // set a2d prescale factor to 128 // 16 MHz / 128 = 125 KHz, inside the desired 50-200 KHz range. // XXX: this will not work properly for other clock speeds, and @@ -232,13 +276,14 @@ void init() // enable a2d conversions sbi(ADCSRA, ADEN); +#endif // the bootloader connects pins 0 and 1 to the USART; disconnect them // here so they can be used as normal digital i/o; they will be // reconnected in Serial.begin() -#if defined(__AVR_ATmega8__) +#if defined(UCSRB) UCSRB = 0; -#else +#elif defined(UCSR0B) UCSR0B = 0; #endif -} \ No newline at end of file +} diff --git a/hardware/arduino/cores/arduino/wiring.h b/hardware/arduino/cores/arduino/wiring.h index 7245797e3..027a8d16e 100755 --- a/hardware/arduino/cores/arduino/wiring.h +++ b/hardware/arduino/cores/arduino/wiring.h @@ -85,8 +85,8 @@ extern "C"{ #define noInterrupts() cli() #define clockCyclesPerMicrosecond() ( F_CPU / 1000000L ) -#define clockCyclesToMicroseconds(a) ( (a) / clockCyclesPerMicrosecond() ) -#define microsecondsToClockCycles(a) ( (a) * clockCyclesPerMicrosecond() ) +#define clockCyclesToMicroseconds(a) ( ((a) * 1000L) / (F_CPU / 1000L) ) +#define microsecondsToClockCycles(a) ( ((a) * (F_CPU / 1000L)) / 1000L ) #define lowByte(w) ((uint8_t) ((w) & 0xff)) #define highByte(w) ((uint8_t) ((w) >> 8)) diff --git a/hardware/arduino/cores/arduino/wiring_analog.c b/hardware/arduino/cores/arduino/wiring_analog.c old mode 100755 new mode 100644 index da0cf0781..d248f4ce8 --- a/hardware/arduino/cores/arduino/wiring_analog.c +++ b/hardware/arduino/cores/arduino/wiring_analog.c @@ -19,6 +19,8 @@ Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + Modified 28 September 2010 by Mark Sproul + $Id: wiring.c 248 2007-02-03 15:36:30Z mellis $ */ @@ -41,22 +43,27 @@ int analogRead(uint8_t pin) #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) if (pin >= 54) pin -= 54; // allow for channel or pin numbers +#else + if (pin >= 14) pin -= 14; // allow for channel or pin numbers +#endif +#if defined(ADCSRB) && defined(MUX5) // the MUX5 bit of ADCSRB selects whether we're reading from channels // 0 to 7 (MUX5 low) or 8 to 15 (MUX5 high). ADCSRB = (ADCSRB & ~(1 << MUX5)) | (((pin >> 3) & 0x01) << MUX5); -#else - if (pin >= 14) pin -= 14; // allow for channel or pin numbers #endif // set the analog reference (high two bits of ADMUX) and select the // channel (low 4 bits). this also sets ADLAR (left-adjust result) // to 0 (the default). +#if defined(ADMUX) ADMUX = (analog_reference << 6) | (pin & 0x07); +#endif // without a delay, we seem to read from the wrong channel //delay(1); +#if defined(ADCSRA) && defined(ADCL) // start the conversion sbi(ADCSRA, ADSC); @@ -67,8 +74,13 @@ int analogRead(uint8_t pin) // and ADCH until ADCH is read. reading ADCL second would // cause the results of each conversion to be discarded, // as ADCL and ADCH would be locked when it completed. - low = ADCL; + low = ADCL; high = ADCH; +#else + // we dont have an ADC, return 0 + low = 0; + high = 0; +#endif // combine the two bytes return (high << 8) | low; @@ -86,98 +98,162 @@ void analogWrite(uint8_t pin, int val) // for consistenty with Wiring, which doesn't require a pinMode // call for the analog output pins. pinMode(pin, OUTPUT); - - if (digitalPinToTimer(pin) == TIMER1A) { - // connect pwm to pin on timer 1, channel A - sbi(TCCR1A, COM1A1); - // set pwm duty - OCR1A = val; - } else if (digitalPinToTimer(pin) == TIMER1B) { - // connect pwm to pin on timer 1, channel B - sbi(TCCR1A, COM1B1); - // set pwm duty - OCR1B = val; -#if defined(__AVR_ATmega8__) - } else if (digitalPinToTimer(pin) == TIMER2) { - // connect pwm to pin on timer 2, channel B - sbi(TCCR2, COM21); - // set pwm duty - OCR2 = val; -#else - } else if (digitalPinToTimer(pin) == TIMER0A) { - if (val == 0) { - digitalWrite(pin, LOW); - } else { - // connect pwm to pin on timer 0, channel A - sbi(TCCR0A, COM0A1); - // set pwm duty - OCR0A = val; - } - } else if (digitalPinToTimer(pin) == TIMER0B) { - if (val == 0) { - digitalWrite(pin, LOW); - } else { - // connect pwm to pin on timer 0, channel B - sbi(TCCR0A, COM0B1); - // set pwm duty - OCR0B = val; - } - } else if (digitalPinToTimer(pin) == TIMER2A) { - // connect pwm to pin on timer 2, channel A - sbi(TCCR2A, COM2A1); - // set pwm duty - OCR2A = val; - } else if (digitalPinToTimer(pin) == TIMER2B) { - // connect pwm to pin on timer 2, channel B - sbi(TCCR2A, COM2B1); - // set pwm duty - OCR2B = val; -#endif -#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) - // XXX: need to handle other timers here - } else if (digitalPinToTimer(pin) == TIMER3A) { - // connect pwm to pin on timer 3, channel A - sbi(TCCR3A, COM3A1); - // set pwm duty - OCR3A = val; - } else if (digitalPinToTimer(pin) == TIMER3B) { - // connect pwm to pin on timer 3, channel B - sbi(TCCR3A, COM3B1); - // set pwm duty - OCR3B = val; - } else if (digitalPinToTimer(pin) == TIMER3C) { - // connect pwm to pin on timer 3, channel C - sbi(TCCR3A, COM3C1); - // set pwm duty - OCR3C = val; - } else if (digitalPinToTimer(pin) == TIMER4A) { - // connect pwm to pin on timer 4, channel A - sbi(TCCR4A, COM4A1); - // set pwm duty - OCR4A = val; - } else if (digitalPinToTimer(pin) == TIMER4B) { - // connect pwm to pin on timer 4, channel B - sbi(TCCR4A, COM4B1); - // set pwm duty - OCR4B = val; - } else if (digitalPinToTimer(pin) == TIMER4C) { - // connect pwm to pin on timer 4, channel C - sbi(TCCR4A, COM4C1); - // set pwm duty - OCR4C = val; - } else if (digitalPinToTimer(pin) == TIMER5A) { - // connect pwm to pin on timer 5, channel A - sbi(TCCR5A, COM5A1); - // set pwm duty - OCR5A = val; - } else if (digitalPinToTimer(pin) == TIMER5B) { - // connect pwm to pin on timer 5, channel B - sbi(TCCR5A, COM5B1); - // set pwm duty - OCR5B = val; -#endif - } else if (val < 128) + if (val == 0) + { digitalWrite(pin, LOW); - else + } + else if (val == 255) + { digitalWrite(pin, HIGH); + } + else + { + switch(digitalPinToTimer(pin)) + { + // XXX fix needed for atmega8 + #if defined(TCCR0) && defined(COM00) && !defined(__AVR_ATmega8__) + case TIMER0A: + // connect pwm to pin on timer 0 + sbi(TCCR0, COM00); + OCR0 = val; // set pwm duty + break; + #endif + + #if defined(TCCR0A) && defined(COM0A1) + case TIMER0A: + // connect pwm to pin on timer 0, channel A + sbi(TCCR0A, COM0A1); + OCR0A = val; // set pwm duty + break; + #endif + + #if defined(TCCR0A) && defined(COM0B1) + case TIMER0B: + // connect pwm to pin on timer 0, channel B + sbi(TCCR0A, COM0B1); + OCR0B = val; // set pwm duty + break; + #endif + + #if defined(TCCR1A) && defined(COM1A1) + case TIMER1A: + // connect pwm to pin on timer 1, channel A + sbi(TCCR1A, COM1A1); + OCR1A = val; // set pwm duty + break; + #endif + + #if defined(TCCR1A) && defined(COM1B1) + case TIMER1B: + // connect pwm to pin on timer 1, channel B + sbi(TCCR1A, COM1B1); + OCR1B = val; // set pwm duty + break; + #endif + + #if defined(TCCR2) && defined(COM21) + case TIMER2: + // connect pwm to pin on timer 2 + sbi(TCCR2, COM21); + OCR2 = val; // set pwm duty + break; + #endif + + #if defined(TCCR2A) && defined(COM2A1) + case TIMER2A: + // connect pwm to pin on timer 2, channel A + sbi(TCCR2A, COM2A1); + OCR2A = val; // set pwm duty + break; + #endif + + #if defined(TCCR2A) && defined(COM2B1) + case TIMER2B: + // connect pwm to pin on timer 2, channel B + sbi(TCCR2A, COM2B1); + OCR2B = val; // set pwm duty + break; + #endif + + #if defined(TCCR3A) && defined(COM3A1) + case TIMER3A: + // connect pwm to pin on timer 3, channel A + sbi(TCCR3A, COM3A1); + OCR3A = val; // set pwm duty + break; + #endif + + #if defined(TCCR3A) && defined(COM3B1) + case TIMER3B: + // connect pwm to pin on timer 3, channel B + sbi(TCCR3A, COM3B1); + OCR3B = val; // set pwm duty + break; + #endif + + #if defined(TCCR3A) && defined(COM3C1) + case TIMER3C: + // connect pwm to pin on timer 3, channel C + sbi(TCCR3A, COM3C1); + OCR3C = val; // set pwm duty + break; + #endif + + #if defined(TCCR4A) && defined(COM4A1) + case TIMER4A: + // connect pwm to pin on timer 4, channel A + sbi(TCCR4A, COM4A1); + OCR4A = val; // set pwm duty + break; + #endif + + #if defined(TCCR4A) && defined(COM4B1) + case TIMER4B: + // connect pwm to pin on timer 4, channel B + sbi(TCCR4A, COM4B1); + OCR4B = val; // set pwm duty + break; + #endif + + #if defined(TCCR4A) && defined(COM4C1) + case TIMER4C: + // connect pwm to pin on timer 4, channel C + sbi(TCCR4A, COM4C1); + OCR4C = val; // set pwm duty + break; + #endif + + #if defined(TCCR5A) && defined(COM5A1) + case TIMER5A: + // connect pwm to pin on timer 5, channel A + sbi(TCCR5A, COM5A1); + OCR5A = val; // set pwm duty + break; + #endif + + #if defined(TCCR5A) && defined(COM5B1) + case TIMER5B: + // connect pwm to pin on timer 5, channel B + sbi(TCCR5A, COM5B1); + OCR5B = val; // set pwm duty + break; + #endif + + #if defined(TCCR5A) && defined(COM5C1) + case TIMER5C: + // connect pwm to pin on timer 5, channel C + sbi(TCCR5A, COM5C1); + OCR5C = val; // set pwm duty + break; + #endif + + case NOT_ON_TIMER: + default: + if (val < 128) { + digitalWrite(pin, LOW); + } else { + digitalWrite(pin, HIGH); + } + } + } } diff --git a/hardware/arduino/cores/arduino/wiring_digital.c b/hardware/arduino/cores/arduino/wiring_digital.c index 1475f0c04..0949da42d 100755 --- a/hardware/arduino/cores/arduino/wiring_digital.c +++ b/hardware/arduino/cores/arduino/wiring_digital.c @@ -19,6 +19,8 @@ Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + Modified 28 September 2010 by Mark Sproul + $Id: wiring.c 248 2007-02-03 15:36:30Z mellis $ */ @@ -56,32 +58,67 @@ void pinMode(uint8_t pin, uint8_t mode) // But shouldn't this be moved into pinMode? Seems silly to check and do on // each digitalread or write. // -static inline void turnOffPWM(uint8_t timer) __attribute__ ((always_inline)); -static inline void turnOffPWM(uint8_t timer) +// Mark Sproul: +// - Removed inline. Save 170 bytes on atmega1280 +// - changed to a switch statment; added 32 bytes but much easier to read and maintain. +// - Added more #ifdefs, now compiles for atmega645 +// +//static inline void turnOffPWM(uint8_t timer) __attribute__ ((always_inline)); +//static inline void turnOffPWM(uint8_t timer) +static void turnOffPWM(uint8_t timer) { - if (timer == TIMER1A) cbi(TCCR1A, COM1A1); - if (timer == TIMER1B) cbi(TCCR1A, COM1B1); + switch (timer) + { + #if defined(TCCR1A) && defined(COM1A1) + case TIMER1A: cbi(TCCR1A, COM1A1); break; + #endif + #if defined(TCCR1A) && defined(COM1B1) + case TIMER1B: cbi(TCCR1A, COM1B1); break; + #endif + + #if defined(TCCR2) && defined(COM21) + case TIMER2: cbi(TCCR2, COM21); break; + #endif + + #if defined(TCCR0A) && defined(COM0A1) + case TIMER0A: cbi(TCCR0A, COM0A1); break; + #endif + + #if defined(TIMER0B) && defined(COM0B1) + case TIMER0B: cbi(TCCR0A, COM0B1); break; + #endif + #if defined(TCCR2A) && defined(COM2A1) + case TIMER2A: cbi(TCCR2A, COM2A1); break; + #endif + #if defined(TCCR2A) && defined(COM2B1) + case TIMER2B: cbi(TCCR2A, COM2B1); break; + #endif + + #if defined(TCCR3A) && defined(COM3A1) + case TIMER3A: cbi(TCCR3A, COM3A1); break; + #endif + #if defined(TCCR3A) && defined(COM3B1) + case TIMER3B: cbi(TCCR3A, COM3B1); break; + #endif + #if defined(TCCR3A) && defined(COM3C1) + case TIMER3C: cbi(TCCR3A, COM3C1); break; + #endif -#if defined(__AVR_ATmega8__) - if (timer == TIMER2) cbi(TCCR2, COM21); -#else - if (timer == TIMER0A) cbi(TCCR0A, COM0A1); - if (timer == TIMER0B) cbi(TCCR0A, COM0B1); - if (timer == TIMER2A) cbi(TCCR2A, COM2A1); - if (timer == TIMER2B) cbi(TCCR2A, COM2B1); -#endif - -#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) - if (timer == TIMER3A) cbi(TCCR3A, COM3A1); - if (timer == TIMER3B) cbi(TCCR3A, COM3B1); - if (timer == TIMER3C) cbi(TCCR3A, COM3C1); - if (timer == TIMER4A) cbi(TCCR4A, COM4A1); - if (timer == TIMER4B) cbi(TCCR4A, COM4B1); - if (timer == TIMER4C) cbi(TCCR4A, COM4C1); - if (timer == TIMER5A) cbi(TCCR5A, COM5A1); - if (timer == TIMER5B) cbi(TCCR5A, COM5B1); - if (timer == TIMER5C) cbi(TCCR5A, COM5C1); -#endif + #if defined(TCCR4A) && defined(COM4A1) + case TIMER4A: cbi(TCCR4A, COM4A1); break; + #endif + #if defined(TCCR4A) && defined(COM4B1) + case TIMER4B: cbi(TCCR4A, COM4B1); break; + #endif + #if defined(TCCR4A) && defined(COM4C1) + case TIMER4C: cbi(TCCR4A, COM4C1); break; + #endif + #if defined(TCCR5A) + case TIMER5A: cbi(TCCR5A, COM5A1); break; + case TIMER5B: cbi(TCCR5A, COM5B1); break; + case TIMER5C: cbi(TCCR5A, COM5C1); break; + #endif + } } void digitalWrite(uint8_t pin, uint8_t val)