/*
 * 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_ABCD_H
#define ECXX_PLATFORM_AVR_PIN_MAPPING_STD_ABCD_H


//
// Available ports : A B C D (no Arduino variant)
//


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

static constexpr uint8_t minPWMPinNumber[1]  = { 4 };
static constexpr uint8_t maxPWMPinNumber[1]  = { 7 };

static constexpr uint8_t minADCPinNumber     = 24;
static constexpr uint8_t maxADCPinNumber     = 31;
static constexpr uint8_t adcPortLow          = ECXX_PA;
static constexpr uint8_t adcPortHigh         = ECXX_NOT_ADC;

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][4][2] = { {
#if defined(__AVR_ATmega16__) || defined(__AVR_ATmega16A__) || defined(__AVR_ATmega32__) || defined(__AVR_ATmega32A__)
    {  4, ECXX_TIMER1B },
    {  5, ECXX_TIMER1A },
    {  6, ECXX_NOT_PWM },
    {  7, ECXX_TIMER2  }
#else
    {  4, ECXX_TIMER1B },
    {  5, ECXX_TIMER1A },
    {  6, ECXX_TIMER2B },
    {  7, ECXX_TIMER2A }
#endif
} };


// Digital pin number to port number
ECXX_UINT8_ARRAY pin_to_pnum[] = {
             //               Alternate port function (PDIP40)
             //               32   1284
    ECXX_PD, // PD 0 **  0 ** RXD  RXD0 USART0_RX HOST_PC_COM
    ECXX_PD, // PD 1 **  1 ** TXD  TXD0 USART0_TX HOST_PC_COM
    ECXX_PD, // PD 2 **  2 ** INT0 INT0
    ECXX_PD, // PD 3 **  3 ** INT1 INT1
    ECXX_PD, // PD 4 **  4 ** OC1B OC1B           PWM4
    ECXX_PD, // PD 5 **  5 ** OC1A OC1A           PWM5
    ECXX_PD, // PD 6 **  6 **      OC2B           PWM6
    ECXX_PD, // PD 7 **  7 ** OC2  OC2A           PWM7
    ECXX_PB, // PB 0 **  8 **
    ECXX_PB, // PB 1 **  9 **
    ECXX_PB, // PB 2 ** 10 ** INT2 INT2
    ECXX_PB, // PB 3 ** 11 ** OC0  OC0A           TIMER0_INTERNAL
    ECXX_PB, // PB 4 ** 12 **      OC0B SPI_SS
    ECXX_PB, // PB 5 ** 13 **           SPI_MOSI  INDICATOR_LED
    ECXX_PB, // PB 6 ** 14 **           SPI_MISO
    ECXX_PB, // PB 7 ** 15 **           SPI_SCK
    ECXX_PC, // PC 0 ** 16 **           TWI_SCL
    ECXX_PC, // PC 1 ** 17 **           TWI_SDA
    ECXX_PC, // PC 2 ** 18 **
    ECXX_PC, // PC 3 ** 19 **
    ECXX_PC, // PC 4 ** 20 **
    ECXX_PC, // PC 5 ** 21 **
    ECXX_PC, // PC 6 ** 22 **
    ECXX_PC, // PC 7 ** 23 **
    ECXX_PA, // PA 0 ** 24 ** A0   A0
    ECXX_PA, // PA 1 ** 25 ** A1   A1
    ECXX_PA, // PA 2 ** 26 ** A2   A2
    ECXX_PA, // PA 3 ** 27 ** A3   A3
    ECXX_PA, // PA 4 ** 28 ** A4   A4
    ECXX_PA, // PA 5 ** 29 ** A5   A5
    ECXX_PA, // PA 6 ** 30 ** A6   A6
    ECXX_PA  // PA 7 ** 31 ** A7   A7
};


// 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(6), // PB 6 ** 14 **
    _BV(7), // PB 7 ** 15 **
    _BV(0), // PC 0 ** 16 **
    _BV(1), // PC 1 ** 17 **
    _BV(2), // PC 2 ** 18 **
    _BV(3), // PC 3 ** 19 **
    _BV(4), // PC 4 ** 20 **
    _BV(5), // PC 5 ** 21 **
    _BV(6), // PC 6 ** 22 **
    _BV(7), // PC 7 ** 23 **
    _BV(0), // PA 0 ** 24 **
    _BV(1), // PA 1 ** 25 **
    _BV(2), // PA 2 ** 26 **
    _BV(3), // PA 3 ** 27 **
    _BV(4), // PA 4 ** 28 **
    _BV(5), // PA 5 ** 29 **
    _BV(6), // PA 6 ** 30 **
    _BV(7)  // PA 7 ** 31 **
};


// Port number to direction SFR
static constexpr volatile uint8_t* const pnum_to_DDR[] = {
    ECXX_NOT_IO_REG,
    &DDRA,
    &DDRB,
    &DDRC,
    &DDRD
};


// Port number to output SFR
static constexpr volatile uint8_t* const pnum_to_PORT[] = {
    ECXX_NOT_IO_REG,
    &PORTA,
    &PORTB,
    &PORTC,
    &PORTD
};


// Port number to input SFR
static constexpr volatile uint8_t* const pnum_to_PIN[] = {
    ECXX_NOT_IO_REG,
    &PINA,
    &PINB,
    &PINC,
    &PIND
};


#endif
