// ----------------------------------------------------------------------------------------
//
//  avr/device/hwdefs.h
//    
//  Bob Cook Development, Robotics Library
//  http://www.bobcookdev.com/rl/
//    
//  This file provides abstracted definitions for the underlying AVR hardware.
//
//  Copyright (C) 2007 Bob Cook
//
//      This program is free software; you can redistribute it and/or modify it
//      under the terms of the GNU General Public License as published by the
//      Free Software Foundation; either version 2 of the License, or (at your
//      option) any later version.
//    
//      This program is distributed in the hope that it will be useful, but 
//      WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
//      or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License 
//      for more details.
//    
//      You should have received a copy of the GNU General Public License along
//      with this program; if not, write to the Free Software Foundation, Inc., 
//      51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
//
// ----------------------------------------------------------------------------------------
 

#if !defined( BCDRL_AVR_DEVICE_HWDEFS_H )
#define BCDRL_AVR_DEVICE_HWDEFS_H


#include <avr/io.h>

#include "project_defs.h"


// ----------------------------------------------------------------------------------------

#if !defined( __AVR_ATmega8__ ) \
    && !defined( __AVR_ATmega16__ ) \
    && !defined( __AVR_ATmega88__ ) \
    && !defined( __AVR_ATmega168__ ) \
    && !defined( __AVR_ATmega128__ )
#error Unsupported Hardware
#endif


// ----------------------------------------------------------------------------------------
//  General Definitions
    
#if defined( __AVR_ATmega8__ )

#define     HW_HAS_PORTA
#define     HW_HAS_PORTB
#define     HW_HAS_PORTC
#define     HW_HAS_PORTD

#define     HW_RAM_SIZE                 1024
#define     HW_FLASH_SIZE               (8 * 1024)
#define     HW_EEPROM_SIZE              512

#define     HW_RESET_PORT               PORTC
#define     HW_RESET_DDR                DDRC
#define     HW_RESET_PIN                PC6

#elif defined( __AVR_ATmega88__ )

#define     HW_HAS_PORTA
#define     HW_HAS_PORTB
#define     HW_HAS_PORTC
#define     HW_HAS_PORTD

#define     HW_RAM_SIZE                 1024
#define     HW_FLASH_SIZE               (8 * 1024)
#define     HW_EEPROM_SIZE              512

#define     HW_RESET_PORT               PORTC
#define     HW_RESET_DDR                DDRC
#define     HW_RESET_PIN                PC6

#elif defined( __AVR_ATmega168__ )

#define     HW_HAS_PORTA
#define     HW_HAS_PORTB
#define     HW_HAS_PORTC
#define     HW_HAS_PORTD

#define     HW_RAM_SIZE                 1024
#define     HW_FLASH_SIZE               (16 * 1024)
#define     HW_EEPROM_SIZE              512

#define     HW_RESET_PORT               PORTC
#define     HW_RESET_DDR                DDRC
#define     HW_RESET_PIN                PC6

#elif defined( __AVR_ATmega16__ )

#define     HW_HAS_PORTA
#define     HW_HAS_PORTB
#define     HW_HAS_PORTC
#define     HW_HAS_PORTD

#define     HW_RAM_SIZE                 1024
#define     HW_FLASH_SIZE               (16 * 1024)
#define     HW_EEPROM_SIZE              512

#elif defined( __AVR_ATmega128__ )

#define     HW_HAS_PORTA
#define     HW_HAS_PORTB
#define     HW_HAS_PORTC
#define     HW_HAS_PORTD
#define     HW_HAS_PORTE
#define     HW_HAS_PORTF
#define     HW_HAS_PORTG

#define     HW_RAM_SIZE                 4096
#define     HW_FLASH_SIZE               (128 * 1024)
#define     HW_EEPROM_SIZE              4096

#endif


// ----------------------------------------------------------------------------------------
//  SPI hardware
    
#if defined( __AVR_ATmega8__ )

#define     HW_HAS_SPI

#define     HW_SPI_PORT                 PORTB
#define     HW_SPI_DDR                  DDRB

#define     HW_SPI_SCK_PIN              PB5
#define     HW_SPI_MISO_PIN             PB4
#define     HW_SPI_MOSI_PIN             PB3
#define     HW_SPI_SS_PIN               PB2

#elif defined( __AVR_ATmega88__ )

#define     HW_HAS_SPI

#define     HW_SPI_PORT                 PORTB
#define     HW_SPI_DDR                  DDRB

#define     HW_SPI_SCK_PIN              PB5
#define     HW_SPI_MISO_PIN             PB4
#define     HW_SPI_MOSI_PIN             PB3
#define     HW_SPI_SS_PIN               PB2

#elif defined( __AVR_ATmega168__ )

#define     HW_HAS_SPI

#define     HW_SPI_PORT                 PORTB
#define     HW_SPI_DDR                  DDRB

#define     HW_SPI_SCK_PIN              PB5
#define     HW_SPI_MISO_PIN             PB4
#define     HW_SPI_MOSI_PIN             PB3
#define     HW_SPI_SS_PIN               PB2

#elif defined( __AVR_ATmega16__ )

#define     HW_HAS_SPI

#define     HW_SPI_PORT                 PORTB
#define     HW_SPI_DDR                  DDRB

#define     HW_SPI_SCK_PIN              PB7
#define     HW_SPI_MISO_PIN             PB6
#define     HW_SPI_MOSI_PIN             PB5
#define     HW_SPI_SS_PIN               PB4

#elif defined( __AVR_ATmega128__ )

#define     HW_HAS_SPI

#define     HW_SPI_PORT                 PORTB
#define     HW_SPI_DDR                  DDRB

#define     HW_SPI_SCK_PIN              PB1
#define     HW_SPI_MISO_PIN             PB3
#define     HW_SPI_MOSI_PIN             PB2
#define     HW_SPI_SS_PIN               PB0

#else
#error Unsupported Hardware
#endif


// ----------------------------------------------------------------------------------------
//  USART hardware
    
#if defined( __AVR_ATmega8__ )

#define     HW_HAS_UART0
#define     HW_HAS_UART0_SPEED2X
 
#define     HW_UART0_PORT               PORTD
#define     HW_UART0_DDR                DDRD

#define     HW_UART0_RX_PIN             PD0
#define     HW_UART0_TX_PIN             PD1

#elif defined( __AVR_ATmega88__ )

#define     HW_HAS_UART0
#define     HW_HAS_UART0_SPEED2X

#define     HW_UART0_PORT               PORTD
#define     HW_UART0_DDR                DDRD

#define     HW_UART0_RX_PIN             PD0
#define     HW_UART0_TX_PIN             PD1

#elif defined( __AVR_ATmega168__ )

#define     HW_HAS_UART0
#define     HW_HAS_UART0_SPEED2X

#define     HW_UART0_PORT               PORTD
#define     HW_UART0_DDR                DDRD

#define     HW_UART0_RX_PIN             PD0
#define     HW_UART0_TX_PIN             PD1

#elif defined( __AVR_ATmega16__ )

#define     HW_HAS_UART0
 
#define     HW_UART0_PORT               PORTD
#define     HW_UART0_DDR                DDRD

#define     HW_UART0_RX_PIN             PD0
#define     HW_UART0_TX_PIN             PD1

#elif defined( __AVR_ATmega128__ )

#define     HW_HAS_UART0
#define     HW_HAS_UART0_SPEED2X

#define     HW_UART0_PORT               PORTE
#define     HW_UART0_DDR                DDRE

#define     HW_UART0_RX_PIN             PE0
#define     HW_UART0_TX_PIN             PE1

#define     HW_HAS_UART1
#define     HW_HAS_UART1_SPEED2X

#define     HW_UART1_PORT               PORTD
#define     HW_UART1_DDR                DDRD

#define     HW_UART1_RX_PIN             PD2
#define     HW_UART1_TX_PIN             PD3

#else
#error Unsupported Hardware
#endif


// ----------------------------------------------------------------------------------------
//  TWI hardware
    
#if defined( __AVR_ATmega8__ )

#define     HW_HAS_TWI

#define     HW_TWI_PORT                 PORTC
#define     HW_TWI_DDR                  DDRC

#define     HW_TWI_SDA_PIN              PC4
#define     HW_TWI_SCL_PIN              PC5

#elif defined( __AVR_ATmega88__ )

#define     HW_HAS_TWI

#define     HW_TWI_PORT                 PORTC
#define     HW_TWI_DDR                  DDRC

#define     HW_TWI_SDA_PIN              PC4
#define     HW_TWI_SCL_PIN              PC5

#elif defined( __AVR_ATmega168__ )

#define     HW_HAS_TWI

#define     HW_TWI_PORT                 PORTC
#define     HW_TWI_DDR                  DDRC

#define     HW_TWI_SDA_PIN              PC4
#define     HW_TWI_SCL_PIN              PC5

#elif defined( __AVR_ATmega16__ )

#define     HW_HAS_TWI

#define     HW_TWI_PORT                 PORTC
#define     HW_TWI_DDR                  DDRC

#define     HW_TWI_SDA_PIN              PC1
#define     HW_TWI_SCL_PIN              PC0

#elif defined( __AVR_ATmega128__ )

#define     HW_HAS_TWI

#define     HW_TWI_PORT                 PORTD
#define     HW_TWI_DDR                  DDRD

#define     HW_TWI_SDA_PIN              PD1
#define     HW_TWI_SCL_PIN              PD0

#else
#error Unsupported Hardware
#endif


// ----------------------------------------------------------------------------------------
//  ADC hardware
    
#if defined( __AVR_ATmega8__ )

#define     HW_HAS_ADC

#define     HW_ADC_PORT                 PORTC
#define     HW_ADC_DDR                  DDRC
#define     HW_ADC_NUM_CHANNELS         8

#define     HW_ADC_REG_ADCSR            ADCSRA
#define     HW_ADC_REG_ADCSR_ADFR       ADFR

#define     HW_ADC_REG_ADMUX            ADMUX

#elif defined( __AVR_ATmega88__ )

#define     HW_HAS_ADC

#define     HW_ADC_PORT                 PORTC
#define     HW_ADC_DDR                  DDRC
#define     HW_ADC_NUM_CHANNELS         8

#define     HW_ADC_REG_ADCSR            ADCSRA
#define     HW_ADC_REG_ADCSR_ADFR       ADATE
#define     HW_ADC_NUM_CHANNELS         8

#define     HW_ADC_REG_ADMUX            ADMUX

#elif defined( __AVR_ATmega168__ )

#define     HW_HAS_ADC

#define     HW_ADC_PORT                 PORTC
#define     HW_ADC_DDR                  DDRC
#define     HW_ADC_NUM_CHANNELS         8

#define     HW_ADC_REG_ADCSR            ADCSRA
#define     HW_ADC_REG_ADCSR_ADFR       ADATE
#define     HW_ADC_NUM_CHANNELS         8

#define     HW_ADC_REG_ADMUX            ADMUX

#elif defined( __AVR_ATmega16__ )

#define     HW_HAS_ADC

#define     HW_ADC_PORT                 PORTA
#define     HW_ADC_DDR                  DDRA
#define     HW_ADC_NUM_CHANNELS         8

#define     HW_ADC_REG_ADCSR            ADCSRA
#define     HW_ADC_REG_ADCSR_ADATE      ADATE

#define     HW_ADC_REG_ADMUX            ADMUX

#elif defined( __AVR_ATmega128__ )

#define     HW_HAS_ADC

#define     HW_ADC_PORT                 PORTE
#define     HW_ADC_DDR                  DDRE
#define     HW_ADC_NUM_CHANNELS         8

#define     HW_ADC_REG_ADCSR            ADCSRA
#define     HW_ADC_REG_ADCSR_ADFR       ADFR
#define     HW_ADC_NUM_CHANNELS         8

#define     HW_ADC_REG_ADMUX            ADMUX

#else
#error Unsupported Hardware
#endif


#endif  // #if !defined( BCDRL_AVR_DEVICE_HWDEFS_H )
// ----------------------------------------------------------------------------------------
//  end of hwdefs.h
// ----------------------------------------------------------------------------------------

