/*
 * 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_ABCDEF_H
#define ECXX_PLATFORM_AVR_PIN_MAPPING_STD_ABCDEF_H


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

//
// The pin map specified below is based on the DT-AVR Inoduino board that was
// manufactured by Innovative Electronics:
//     http://www.innovativeelectronics.com/index.php?pg=ie_pdet&idp=29
//


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

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

static constexpr uint8_t minADCPinNumber     = 39;
static constexpr uint8_t maxADCPinNumber     = 46;
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][7][2] = { {
    {  7, ECXX_TIMER3C },
    {  8, ECXX_TIMER3B },
    {  9, ECXX_TIMER3A },
    { 10, ECXX_TIMER2A },
    { 11, ECXX_TIMER1A },
    { 12, ECXX_TIMER1B },
    { 13, ECXX_TIMER1C }
} };


// Digital pin number to port number
ECXX_UINT8_ARRAY pin_to_pnum[] = {
             //               Alternate port function (TQFP64)
             //               90USB1286
    ECXX_PD, // PD 2 **  0 ** INT2/RXD1 USART1_RX
    ECXX_PD, // PD 3 **  1 ** INT3/TXD1 USART1_TX
    ECXX_PE, // PE 4 **  2 ** INT4
    ECXX_PE, // PE 5 **  3 ** INT5
    ECXX_PC, // PC 7 **  4 **
    ECXX_PD, // PD 0 **  5 ** INT0/OC0B TWI_SCL   TIMER0_INTERNAL
    ECXX_PD, // PD 1 **  6 ** INT1/0C2B TWI_SDA
    ECXX_PC, // PC 4 **  7 ** 0C3C      PWM7
    ECXX_PC, // PC 5 **  8 ** 0C3B      PWM8
    ECXX_PC, // PC 6 **  9 ** 0C3A      PWM9
    ECXX_PB, // PB 4 ** 10 ** 0C2A      PWM10
    ECXX_PB, // PB 5 ** 11 ** 0C1A      PWM11
    ECXX_PB, // PB 6 ** 12 ** 0C1B      PWM12
    ECXX_PB, // PB 7 ** 13 ** 0C1C      PWM13     INDICATOR_LED
    ECXX_PA, // PA 0 ** 14 **
    ECXX_PA, // PA 1 ** 15 **
    ECXX_PA, // PA 2 ** 16 **
    ECXX_PA, // PA 3 ** 17 **
    ECXX_PA, // PA 4 ** 18 **
    ECXX_PA, // PA 5 ** 19 **
    ECXX_PA, // PA 6 ** 20 **
    ECXX_PA, // PA 7 ** 21 **
    ECXX_PB, // PB 0 ** 22 **           SPI_SS
    ECXX_PB, // PB 1 ** 23 **           SPI_SCK
    ECXX_PB, // PB 2 ** 24 **           SPI_MOSI
    ECXX_PB, // PB 3 ** 25 **           SPI_MISO
    ECXX_PC, // PC 0 ** 26 **
    ECXX_PC, // PC 1 ** 27 **
    ECXX_PC, // PC 2 ** 28 **
    ECXX_PC, // PC 3 ** 29 **
    ECXX_PD, // PD 4 ** 30 **
    ECXX_PD, // PD 5 ** 31 **
    ECXX_PD, // PD 6 ** 32 **
    ECXX_PD, // PD 7 ** 33 **
    ECXX_PE, // PE 0 ** 34 **
    ECXX_PE, // PE 1 ** 35 **
    ECXX_PE, // PE 3 ** 36 **
    ECXX_PE, // PE 6 ** 37 ** INT6
    ECXX_PE, // PE 7 ** 38 ** INT7
    ECXX_PF, // PF 0 ** 39 ** A0
    ECXX_PF, // PF 1 ** 40 ** A1
    ECXX_PF, // PF 2 ** 41 ** A2
    ECXX_PF, // PF 3 ** 42 ** A3
    ECXX_PF, // PF 4 ** 43 ** A4
    ECXX_PF, // PF 5 ** 44 ** A5
    ECXX_PF, // PF 6 ** 45 ** A6
    ECXX_PF  // PF 7 ** 46 ** A7
};


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


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


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


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


#endif
