/*
 * Copyright (C) 2020-2021 Aloysius Indrayanto
 *
 * This file is part of the eCxx library, see LICENSE file for the license details.
 */


#ifndef INCLUDE_ECXX_PLATFORM_AVR_PIN_MAPPING_STD_H
#error "this file cannot be included directly"
#endif
#undef INCLUDE_ECXX_PLATFORM_AVR_PIN_MAPPING_STD_H


#ifndef ECXX_PLATFORM_AVR_PIN_MAPPING_STD_XBCD_H
#define ECXX_PLATFORM_AVR_PIN_MAPPING_STD_XBCD_H


//
// Available ports : B C D (e.g. Arduino variant standard & eightanaloginputs)
//


// Pin number ranges
static constexpr uint8_t maxDigitalPinNumber = AVR::DMax;

static constexpr uint8_t minPWMPinNumber[1]  = {  9 };
static constexpr uint8_t maxPWMPinNumber[1]  = { 11 };

#if defined(__AVR_ATmega48PB__) || defined(__AVR_ATmega88PB__) || defined(__AVR_ATmega168PB__)

static constexpr uint8_t minADCPinNumber     = 14;
static constexpr uint8_t maxADCPinNumber     = 21;
static constexpr uint8_t adcPortLow          = ECXX_PX;
static constexpr uint8_t adcPortHigh         = ECXX_NOT_ADC;

#else

static constexpr uint8_t minADCPinNumber     = 14;
static constexpr uint8_t maxADCPinNumber     = 19;
static constexpr uint8_t adcPortLow          = ECXX_PC;
static constexpr uint8_t adcPortHigh         = ECXX_NOT_ADC;

#endif

static constexpr uint8_t spiPinMISO          = AVR::SPI_MISO;
static constexpr uint8_t spiPinMOSI          = AVR::SPI_MOSI;
static constexpr uint8_t spiPinSCK           = AVR::SPI_SCK;
static constexpr uint8_t spiPinSS            = AVR::SPI_SS;

static constexpr uint8_t twiPinSDA           = AVR::TWI_SDA;
static constexpr uint8_t twiPinSCL           = AVR::TWI_SCL;


// PWM pins [group][set][pin/timer]
static constexpr uint8_t pwm_pins[1][3][2] = { {
    {  9, ECXX_TIMER1A },
    { 10, ECXX_TIMER1B },
#if defined(__AVR_ATmega8__) || defined(__AVR_ATmega8A__)
    { 11, ECXX_TIMER2  }
#else
    { 11, ECXX_TIMER2A }
#endif
} };


// Digital pin number to port number
ECXX_UINT8_ARRAY pin_to_pnum[] = {
             //               Alternate port function (PDIP28 & TQFP32)
             //               8    328
    ECXX_PD, // PD 0 **  0 ** RXD  RXD  USART_RX HOST_PC_COM
    ECXX_PD, // PD 1 **  1 ** TXD  TXD  USART_TX HOST_PC_COM
    ECXX_PD, // PD 2 **  2 ** INT0 INT0
    ECXX_PD, // PD 3 **  3 ** INT1 INT1
    ECXX_PD, // PD 4 **  4 **
    ECXX_PD, // PD 5 **  5 **      OC0B          TIMER0_INTERNAL
    ECXX_PD, // PD 6 **  6 **      OC0A
    ECXX_PD, // PD 7 **  7 **
    ECXX_PB, // PB 0 **  8 **
    ECXX_PB, // PB 1 **  9 ** OC1A OC1A          PWM9
    ECXX_PB, // PB 2 ** 10 ** OC1B OC1B SPI_SS   PWM10
    ECXX_PB, // PB 3 ** 11 ** OC2  OC2A SPI_MOSI PWM11
    ECXX_PB, // PB 4 ** 12 **           SPI_MISO
    ECXX_PB, // PB 5 ** 13 **           SPI_SCK  INDICATOR_LED
    ECXX_PC, // PC 0 ** 14 ** A0   A0
    ECXX_PC, // PC 1 ** 15 ** A1   A1
    ECXX_PC, // PC 2 ** 16 ** A2   A2
    ECXX_PC, // PC 3 ** 17 ** A3   A3
    ECXX_PC, // PC 4 ** 18 ** A4   A4   TWI_SDA
    ECXX_PC  // PC 5 ** 19 ** A5   A5   TWI_SCL
#if defined(__AVR_ATmega48PB__) || defined(__AVR_ATmega88PB__) || defined(__AVR_ATmega168PB__)
    ,
    ECXX_PE, // PE 2 ** 20 ** A6
    ECXX_PE, // PE 3 ** 21 ** A7
    ECXX_PE, // PE 0 ** 22 **
    ECXX_PE  // PE 1 ** 23 **
#endif
};


// Digital pin number to port bit mask
ECXX_UINT8_ARRAY pin_to_bitmask[] = {
    _BV(0), // PD 0 **  0 **
    _BV(1), // PD 1 **  1 **
    _BV(2), // PD 2 **  2 **
    _BV(3), // PD 3 **  3 **
    _BV(4), // PD 4 **  4 **
    _BV(5), // PD 5 **  5 **
    _BV(6), // PD 6 **  6 **
    _BV(7), // PD 7 **  7 **
    _BV(0), // PB 0 **  8 **
    _BV(1), // PB 1 **  9 **
    _BV(2), // PB 2 ** 10 **
    _BV(3), // PB 3 ** 11 **
    _BV(4), // PB 4 ** 12 **
    _BV(5), // PB 5 ** 13 **
    _BV(0), // PC 0 ** 14 **
    _BV(1), // PC 1 ** 15 **
    _BV(2), // PC 2 ** 16 **
    _BV(3), // PC 3 ** 17 **
    _BV(4), // PC 4 ** 18 **
    _BV(5)  // PC 5 ** 19 **
#if defined(__AVR_ATmega48PB__) || defined(__AVR_ATmega88PB__) || defined(__AVR_ATmega168PB__)
    ,
    _BV(2), // PE 2 ** 20 **
    _BV(3), // PE 3 ** 21 **
    _BV(0), // PE 0 ** 22 **
    _BV(1)  // PE 1 ** 23 **
#endif
};


// Port number to direction SFR
static constexpr volatile uint8_t* const pnum_to_DDR[] = {
    ECXX_NOT_IO_REG,
    ECXX_NOT_IO_REG,
    &DDRB,
    &DDRC,
    &DDRD
#if defined(__AVR_ATmega48PB__) || defined(__AVR_ATmega88PB__) || defined(__AVR_ATmega168PB__)
    ,
    &DDRE
#endif
};


// Port number to output SFR
static constexpr volatile uint8_t* const pnum_to_PORT[] = {
    ECXX_NOT_IO_REG,
    ECXX_NOT_IO_REG,
    &PORTB,
    &PORTC,
    &PORTD
#if defined(__AVR_ATmega48PB__) || defined(__AVR_ATmega88PB__) || defined(__AVR_ATmega168PB__)
    ,
    &PORTE
#endif
};


// Port number to input SFR
static constexpr volatile uint8_t* const pnum_to_PIN[] = {
    ECXX_NOT_IO_REG,
    ECXX_NOT_IO_REG,
    &PINB,
    &PINC,
    &PIND
#if defined(__AVR_ATmega48PB__) || defined(__AVR_ATmega88PB__) || defined(__AVR_ATmega168PB__)
    ,
    &PINE
#endif
};


#endif
