/*
 * 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_ABCDEFG_H
#define ECXX_PLATFORM_AVR_PIN_MAPPING_STD_ABCDEFG_H


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


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

static constexpr uint8_t minPWMPinNumber[1]  = {  5 };
static constexpr uint8_t maxPWMPinNumber[1]  = { 12 };

static constexpr uint8_t minADCPinNumber     = 45;
static constexpr uint8_t maxADCPinNumber     = 52;
static constexpr uint8_t adcPortLow          = ECXX_PF;
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][8][2] = { {
    {  5, ECXX_TIMER3A },
    {  6, ECXX_TIMER3B },
    {  7, ECXX_TIMER3C },
    {  8, ECXX_NOT_PWM },
    {  9, ECXX_NOT_PWM },
    { 10, ECXX_NOT_PWM },
    { 11, ECXX_TIMER1A },
    { 12, ECXX_TIMER1B }
} };


// Digital pin number to port number
ECXX_UINT8_ARRAY pin_to_pnum[] = {
             //               Alternate port function (TQFP64)
             //               64
    ECXX_PE, // PE 0 **  0 ** RXD0 USART0_RX HOST_PC_COM
    ECXX_PE, // PE 1 **  1 ** TXD0 USART0_TX HOST_PC_COM
    ECXX_PE, // PE 6 **  2 ** INT6
    ECXX_PE, // PE 7 **  3 ** INT7
    ECXX_PE, // PE 2 **  4 **
    ECXX_PE, // PE 3 **  5 ** OC3A           PWM5
    ECXX_PE, // PE 4 **  6 ** OC3B           PWM6
    ECXX_PE, // PE 5 **  7 ** OC3C           PWM7
    ECXX_PG, // PG 0 **  8 **
    ECXX_PG, // PG 1 **  9 **
    ECXX_PB, // PB 4 ** 10 ** OC0            TIMER0_INTERNAL
    ECXX_PB, // PB 5 ** 11 ** OC1A           PWM11
    ECXX_PB, // PB 6 ** 12 ** OC1B           PWM12
    ECXX_PB, // PB 7 ** 13 ** OC1C           INDICATOR_LED
    ECXX_PB, // PB 0 ** 14 **      SPI_SS
    ECXX_PB, // PB 1 ** 15 **      SPI_SCK
    ECXX_PB, // PB 2 ** 16 **      SPI_MOSI
    ECXX_PB, // PB 3 ** 17 **      SPI_MISO
    ECXX_PD, // PD 0 ** 18 ** INT0 TWI_SCL
    ECXX_PD, // PD 1 ** 19 ** INT1 TWI_SDA
    ECXX_PD, // PD 2 ** 20 ** RXD1 USART1_RX
    ECXX_PD, // PD 3 ** 21 ** TXD1 USART1_TX
    ECXX_PD, // PD 4 ** 22 **
    ECXX_PD, // PD 5 ** 23 **
    ECXX_PD, // PD 6 ** 24 **
    ECXX_PD, // PD 7 ** 25 **
    ECXX_PA, // PA 0 ** 26 **
    ECXX_PA, // PA 1 ** 27 **
    ECXX_PA, // PA 2 ** 28 **
    ECXX_PA, // PA 3 ** 29 **
    ECXX_PA, // PA 4 ** 30 **
    ECXX_PA, // PA 5 ** 31 **
    ECXX_PA, // PA 6 ** 32 **
    ECXX_PA, // PA 7 ** 33 **
    ECXX_PC, // PC 0 ** 34 **
    ECXX_PC, // PC 1 ** 35 **
    ECXX_PC, // PC 2 ** 36 **
    ECXX_PC, // PC 3 ** 37 **
    ECXX_PC, // PC 4 ** 38 **
    ECXX_PC, // PC 5 ** 39 **
    ECXX_PC, // PC 6 ** 40 **
    ECXX_PC, // PC 7 ** 41 **
    ECXX_PG, // PG 2 ** 42 **
    ECXX_PG, // PG 3 ** 43 **
    ECXX_PG, // PG 4 ** 44 **
    ECXX_PF, // PF 0 ** 45 ** A0
    ECXX_PF, // PF 1 ** 46 ** A1
    ECXX_PF, // PF 2 ** 47 ** A2
    ECXX_PF, // PF 3 ** 48 ** A3
    ECXX_PF, // PF 4 ** 49 ** A4
    ECXX_PF, // PF 5 ** 50 ** A5
    ECXX_PF, // PF 6 ** 51 ** A6
    ECXX_PF  // PF 7 ** 52 ** A7
};


// Digital pin number to port bit mask
ECXX_UINT8_ARRAY pin_to_bitmask[] = {
    _BV(0), // PE 0 **  0 **
    _BV(1), // PE 1 **  1 **
    _BV(6), // PE 6 **  2 **
    _BV(7), // PE 7 **  3 **
    _BV(2), // PE 2 **  4 **
    _BV(3), // PE 3 **  5 **
    _BV(4), // PE 4 **  6 **
    _BV(5), // PE 5 **  7 **
    _BV(0), // PG 0 **  8 **
    _BV(1), // PG 1 **  9 **
    _BV(4), // PB 4 ** 10 **
    _BV(5), // PB 5 ** 11 **
    _BV(6), // PB 6 ** 12 **
    _BV(7), // PB 7 ** 13 **
    _BV(0), // PB 0 ** 14 **
    _BV(1), // PB 1 ** 15 **
    _BV(2), // PB 2 ** 16 **
    _BV(3), // PB 3 ** 17 **
    _BV(0), // PD 0 ** 18 **
    _BV(1), // PD 1 ** 19 **
    _BV(2), // PD 2 ** 20 **
    _BV(3), // PD 3 ** 21 **
    _BV(4), // PD 4 ** 22 **
    _BV(5), // PD 5 ** 23 **
    _BV(6), // PD 6 ** 24 **
    _BV(7), // PD 7 ** 25 **
    _BV(0), // PA 0 ** 26 **
    _BV(1), // PA 1 ** 27 **
    _BV(2), // PA 2 ** 28 **
    _BV(3), // PA 3 ** 29 **
    _BV(4), // PA 4 ** 30 **
    _BV(5), // PA 5 ** 31 **
    _BV(6), // PA 6 ** 32 **
    _BV(7), // PA 7 ** 33 **
    _BV(0), // PC 0 ** 34 **
    _BV(1), // PC 1 ** 35 **
    _BV(2), // PC 2 ** 36 **
    _BV(3), // PC 3 ** 37 **
    _BV(4), // PC 4 ** 38 **
    _BV(5), // PC 5 ** 39 **
    _BV(6), // PC 6 ** 40 **
    _BV(7), // PC 7 ** 41 **
    _BV(2), // PG 2 ** 42 **
    _BV(3), // PG 3 ** 43 **
    _BV(4), // PG 4 ** 44 **
    _BV(0), // PF 0 ** 45 **
    _BV(1), // PF 1 ** 46 **
    _BV(2), // PF 2 ** 47 **
    _BV(3), // PF 3 ** 48 **
    _BV(4), // PF 4 ** 49 **
    _BV(5), // PF 5 ** 50 **
    _BV(6), // PF 6 ** 51 **
    _BV(7)  // PF 7 ** 52 **
};


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


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


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


#endif
