From dd1fdf61e4bc8c3bebdb2fe81c2ba2365d7833bf Mon Sep 17 00:00:00 2001 From: "David A. Mellis" Date: Sat, 1 Nov 2008 22:06:13 +0000 Subject: [PATCH] Switching tests for __AVR_ATmega168__ to tests for __AVR_ATmega8__ so that less changes are needed to support other processors. --- hardware/cores/arduino/WInterrupts.c | 18 +++++----- hardware/cores/arduino/pins_arduino.c | 20 +++++------ hardware/cores/arduino/wiring.c | 34 +++++++++--------- hardware/cores/arduino/wiring_analog.c | 14 ++++---- hardware/cores/arduino/wiring_digital.c | 6 ++-- hardware/cores/arduino/wiring_serial.c | 46 ++++++++++++------------- 6 files changed, 69 insertions(+), 69 deletions(-) diff --git a/hardware/cores/arduino/WInterrupts.c b/hardware/cores/arduino/WInterrupts.c index 21ebe625a..bcee7b719 100755 --- a/hardware/cores/arduino/WInterrupts.c +++ b/hardware/cores/arduino/WInterrupts.c @@ -35,9 +35,9 @@ volatile static voidFuncPtr intFunc[EXTERNAL_NUM_INTERRUPTS]; // volatile static voidFuncPtr twiIntFunc; -#if defined(__AVR_ATmega168__) -#define MCUCR EICRA -#define GICR EIMSK +#if defined(__AVR_ATmega8__) +#define EICRA MCUCR +#define EIMSK GICR #endif void attachInterrupt(uint8_t interruptNum, void (*userFunc)(void), int mode) { @@ -49,13 +49,13 @@ void attachInterrupt(uint8_t interruptNum, void (*userFunc)(void), int mode) { // edge, or falling edge). The mode constants were chosen to correspond // to the configuration bits in the hardware register, so we simply shift // the mode into place. - MCUCR = (MCUCR & ~((1 << ISC00) | (1 << ISC01))) | (mode << ISC00); + EICRA = (EICRA & ~((1 << ISC00) | (1 << ISC01))) | (mode << ISC00); // Enable the interrupt. - GICR |= (1 << INT0); + EIMSK |= (1 << INT0); } else { - MCUCR = (MCUCR & ~((1 << ISC10) | (1 << ISC11))) | (mode << ISC10); - GICR |= (1 << INT1); + EICRA = (EICRA & ~((1 << ISC10) | (1 << ISC11))) | (mode << ISC10); + EIMSK |= (1 << INT1); } } } @@ -64,9 +64,9 @@ void detachInterrupt(uint8_t interruptNum) { if(interruptNum < EXTERNAL_NUM_INTERRUPTS) { if (interruptNum == 0) // Disable the interrupt. - GICR &= ~(1 << INT0); + EIMSK &= ~(1 << INT0); else - GICR &= ~(1 << INT1); + EIMSK &= ~(1 << INT1); intFunc[interruptNum] = 0; } diff --git a/hardware/cores/arduino/pins_arduino.c b/hardware/cores/arduino/pins_arduino.c index 3ef07b653..ef3f16882 100755 --- a/hardware/cores/arduino/pins_arduino.c +++ b/hardware/cores/arduino/pins_arduino.c @@ -134,28 +134,28 @@ const uint8_t PROGMEM digital_pin_to_timer_PGM[] = { NOT_ON_TIMER, NOT_ON_TIMER, // on the ATmega168, digital pin 3 has hardware pwm -#if defined(__AVR_ATmega168__) - TIMER2B, -#else +#if defined(__AVR_ATmega8__) NOT_ON_TIMER, +#else + TIMER2B, #endif NOT_ON_TIMER, // on the ATmega168, digital pins 5 and 6 have hardware pwm -#if defined(__AVR_ATmega168__) +#if defined(__AVR_ATmega8__) + NOT_ON_TIMER, + NOT_ON_TIMER, +#else TIMER0B, TIMER0A, -#else - NOT_ON_TIMER, - NOT_ON_TIMER, #endif NOT_ON_TIMER, NOT_ON_TIMER, /* 8 - port B */ TIMER1A, TIMER1B, -#if defined(__AVR_ATmega168__) - TIMER2A, -#else +#if defined(__AVR_ATmega8__) TIMER2, +#else + TIMER2A, #endif NOT_ON_TIMER, NOT_ON_TIMER, diff --git a/hardware/cores/arduino/wiring.c b/hardware/cores/arduino/wiring.c index 31e024719..19f348c33 100755 --- a/hardware/cores/arduino/wiring.c +++ b/hardware/cores/arduino/wiring.c @@ -130,23 +130,23 @@ 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_ATmega168__) +#if !defined(__AVR_ATmega8__) sbi(TCCR0A, WGM01); sbi(TCCR0A, WGM00); #endif // set timer 0 prescale factor to 64 -#if defined(__AVR_ATmega168__) - sbi(TCCR0B, CS01); - sbi(TCCR0B, CS00); -#else +#if defined(__AVR_ATmega8__) sbi(TCCR0, CS01); sbi(TCCR0, CS00); +#else + sbi(TCCR0B, CS01); + sbi(TCCR0B, CS00); #endif // enable timer 0 overflow interrupt -#if defined(__AVR_ATmega168__) - sbi(TIMSK0, TOIE0); -#else +#if defined(__AVR_ATmega8__) sbi(TIMSK, TOIE0); +#else + sbi(TIMSK0, TOIE0); #endif // timers 1 and 2 are used for phase-correct hardware pwm @@ -161,16 +161,16 @@ void init() sbi(TCCR1A, WGM10); // set timer 2 prescale factor to 64 -#if defined(__AVR_ATmega168__) - sbi(TCCR2B, CS22); -#else +#if defined(__AVR_ATmega8__) sbi(TCCR2, CS22); +#else + sbi(TCCR2B, CS22); #endif // configure timer 2 for phase correct pwm (8-bit) -#if defined(__AVR_ATmega168__) - sbi(TCCR2A, WGM20); -#else +#if defined(__AVR_ATmega8__) sbi(TCCR2, WGM20); +#else + sbi(TCCR2A, WGM20); #endif // set a2d prescale factor to 128 @@ -187,9 +187,9 @@ void init() // 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_ATmega168__) - UCSR0B = 0; -#else +#if defined(__AVR_ATmega8__) UCSRB = 0; +#else + UCSR0B = 0; #endif } \ No newline at end of file diff --git a/hardware/cores/arduino/wiring_analog.c b/hardware/cores/arduino/wiring_analog.c index de0372e02..f3767d29f 100755 --- a/hardware/cores/arduino/wiring_analog.c +++ b/hardware/cores/arduino/wiring_analog.c @@ -87,7 +87,13 @@ void analogWrite(uint8_t pin, int val) sbi(TCCR1A, COM1B1); // set pwm duty OCR1B = val; -#if defined(__AVR_ATmega168__) +#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); @@ -116,12 +122,6 @@ void analogWrite(uint8_t pin, int val) sbi(TCCR2A, COM2B1); // set pwm duty OCR2B = val; -#else - } else if (digitalPinToTimer(pin) == TIMER2) { - // connect pwm to pin on timer 2, channel B - sbi(TCCR2, COM21); - // set pwm duty - OCR2 = val; #endif } else if (val < 128) digitalWrite(pin, LOW); diff --git a/hardware/cores/arduino/wiring_digital.c b/hardware/cores/arduino/wiring_digital.c index f38b2e15e..3044e1875 100755 --- a/hardware/cores/arduino/wiring_digital.c +++ b/hardware/cores/arduino/wiring_digital.c @@ -53,13 +53,13 @@ static inline void turnOffPWM(uint8_t timer) if (timer == TIMER1A) cbi(TCCR1A, COM1A1); if (timer == TIMER1B) cbi(TCCR1A, COM1B1); -#if defined(__AVR_ATmega168__) +#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); -#else - if (timer == TIMER2) cbi(TCCR2, COM21); #endif } diff --git a/hardware/cores/arduino/wiring_serial.c b/hardware/cores/arduino/wiring_serial.c index 4cac8913d..2dd242b80 100755 --- a/hardware/cores/arduino/wiring_serial.c +++ b/hardware/cores/arduino/wiring_serial.c @@ -37,17 +37,7 @@ int rx_buffer_tail = 0; void beginSerial(long baud) { -#if defined(__AVR_ATmega168__) - UBRR0H = ((F_CPU / 16 + baud / 2) / baud - 1) >> 8; - UBRR0L = ((F_CPU / 16 + baud / 2) / baud - 1); - - // enable rx and tx - sbi(UCSR0B, RXEN0); - sbi(UCSR0B, TXEN0); - - // enable interrupt on complete reception of a byte - sbi(UCSR0B, RXCIE0); -#else +#if defined(__AVR_ATmega8__) UBRRH = ((F_CPU / 16 + baud / 2) / baud - 1) >> 8; UBRRL = ((F_CPU / 16 + baud / 2) / baud - 1); @@ -57,6 +47,16 @@ void beginSerial(long baud) // enable interrupt on complete reception of a byte sbi(UCSRB, RXCIE); +#else + UBRR0H = ((F_CPU / 16 + baud / 2) / baud - 1) >> 8; + UBRR0L = ((F_CPU / 16 + baud / 2) / baud - 1); + + // enable rx and tx + sbi(UCSR0B, RXEN0); + sbi(UCSR0B, TXEN0); + + // enable interrupt on complete reception of a byte + sbi(UCSR0B, RXCIE0); #endif // defaults to 8-bit, no parity, 1 stop bit @@ -64,16 +64,16 @@ void beginSerial(long baud) void serialWrite(unsigned char c) { -#if defined(__AVR_ATmega168__) - while (!(UCSR0A & (1 << UDRE0))) - ; - - UDR0 = c; -#else +#if defined(__AVR_ATmega8__) while (!(UCSRA & (1 << UDRE))) ; UDR = c; +#else + while (!(UCSR0A & (1 << UDRE0))) + ; + + UDR0 = c; #endif } @@ -104,16 +104,16 @@ void serialFlush() rx_buffer_head = rx_buffer_tail; } -#if defined(__AVR_ATmega168__) -SIGNAL(SIG_USART_RECV) -#else +#if defined(__AVR_ATmega8__) SIGNAL(SIG_UART_RECV) +#else +SIGNAL(SIG_USART_RECV) #endif { -#if defined(__AVR_ATmega168__) - unsigned char c = UDR0; -#else +#if defined(__AVR_ATmega8__) unsigned char c = UDR; +#else + unsigned char c = UDR0; #endif int i = (rx_buffer_head + 1) % RX_BUFFER_SIZE;