/*
 * 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_AVRX_PIN_NAME_STD_H
#error "this file cannot be included directly"
#endif
#undef INCLUDE_ECXX_PLATFORM_AVRX_PIN_NAME_STD_H


#ifndef ECXX_PLATFORM_AVR_PIN_MAPPING_STD_AXCDXF_H
#define ECXX_PLATFORM_AVR_PIN_MAPPING_STD_AXCDXF_H


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

//
// The pin map specified below is based on the Thinary ATmega4808 board that was
// manufactured by Thinary Electronic:
//     https://github.com/Thinary/ThinaryArduino/tree/master/Thinary-megaavr
//     https://www.aliexpress.com/item/4000160961890.html
//
// NOTE : The pin map has been modified from the one specified by the silkscreen
//        on the Thinary ATmega4808 board
//

// Maximum digital pin number
static constexpr uint8_t DMax = 25;

// ADC pins
static constexpr uint8_t A0   = 14;
static constexpr uint8_t A1   = 15;
static constexpr uint8_t A2   = 16;
static constexpr uint8_t A3   = 17;
static constexpr uint8_t A4   = 18;
static constexpr uint8_t A5   = 19;
static constexpr uint8_t A6   = 20;
static constexpr uint8_t A7   = 21;
static constexpr uint8_t A8   = 22;
static constexpr uint8_t A9   = 23;
static constexpr uint8_t A10  = 24;
static constexpr uint8_t A11  = 25;  /** Pin is not available for use in Thinary ATmega4808 board (it is also the VREFA pin) **/

static constexpr uint8_t AMin =  A0;
static constexpr uint8_t AMax = A11;

// PWM pins
static constexpr uint8_t PWM0 = 14;
static constexpr uint8_t PWM1 = 15;
static constexpr uint8_t PWM2 = 16;
static constexpr uint8_t PWM3 = 17;
static constexpr uint8_t PWM4 = 22;
static constexpr uint8_t PWM5 = 23;

// Interrupt pins
static constexpr uint8_t EINT0 =  4;
static constexpr uint8_t EINT1 =  8;
static constexpr uint8_t EINT2 = 13;
static constexpr uint8_t EINT3 = 16;
static constexpr uint8_t EINT4 = 24;
static constexpr uint8_t EINT5 = 18;

// Communication pins
static constexpr uint8_t UART0_Rx =  0;
static constexpr uint8_t UART0_Tx =  1;

static constexpr uint8_t UART1_Rx =  3;
static constexpr uint8_t UART1_Tx =  2;

static constexpr uint8_t UART2_Rx = 12;
static constexpr uint8_t UART2_Tx = 11;

static constexpr uint8_t UART_Rx  = UART0_Rx;
static constexpr uint8_t UART_Tx  = UART0_Tx;

static constexpr uint8_t SPI_MISO =  7;
static constexpr uint8_t SPI_MOSI =  6;
static constexpr uint8_t SPI_SCK  =  8;
static constexpr uint8_t SPI_SS   =  9;

static constexpr uint8_t TWI_SDA  =  4;
static constexpr uint8_t TWI_SCL  =  5;

// Configurable custom logic pins
static constexpr uint8_t LUT0_In0    =  2;         /* PA 0 */
static constexpr uint8_t LUT0_In1    =  3;         /* PA 1 */
static constexpr uint8_t LUT0_In2    =  4;         /* PA 2 */
static constexpr uint8_t LUT0_OutDef =  5;         /* PA 3 */
static constexpr uint8_t LUT0_OUTAlt =  8;         /* PA 6 */

static constexpr uint8_t LUT1_In0    = 11;         /* PC 0 */
static constexpr uint8_t LUT1_In1    = 12;         /* PC 1 */
static constexpr uint8_t LUT1_In2    = 13;         /* PC 2 */
static constexpr uint8_t LUT1_OutDef = 10;         /* PC 3 */
static constexpr uint8_t LUT1_OutAlt = InvalidPin; /* PC 6 */ /** Pin is not available for use in TQFP32 package **/

static constexpr uint8_t LUT2_In0    = 14;         /* PD 0 */
static constexpr uint8_t LUT2_In1    = 15;         /* PD 1 */
static constexpr uint8_t LUT2_In2    = 16;         /* PD 2 */
static constexpr uint8_t LUT2_OutDef = 17;         /* PD 3 */
static constexpr uint8_t LUT2_OutAlt = 24;         /* PD 6 */

static constexpr uint8_t LUT3_In0    =  1;         /* PF 0 */ /** Tx pin in Thinary ATmega4808 board **/
static constexpr uint8_t LUT3_In1    =  0;         /* PF 1 */ /** Rx pin in Thinary ATmega4808 board **/
static constexpr uint8_t LUT3_In2    = 18;         /* PF 2 */
static constexpr uint8_t LUT3_OutDef = 19;         /* PF 3 */
static constexpr uint8_t LUT3_OutAlt = InvalidPin; /* PF 6 */ /** Pin is not available for use (it is the reset pin) **/

// Analog comparator pins
static constexpr uint8_t AC0_InPos0 = 16; /* PD 2 */
static constexpr uint8_t AC0_InPos1 = 22; /* PD 4 */
static constexpr uint8_t AC0_InPos2 = 24; /* PD 6 */
static constexpr uint8_t AC0_InPos3 = 15; /* PD 1 */

static constexpr uint8_t AC0_InNeg0 = 17; /* PD 3 */
static constexpr uint8_t AC0_InNeg1 = 23; /* PD 5 */
static constexpr uint8_t AC0_InNeg2 = 25; /* PD 7 */ /** Pin is not available for use in Thinary ATmega4808 board (it is also the VREFA pin) **/

static constexpr uint8_t AC0_Out    =  9; /* PA 7 */

// Event system pins
static constexpr uint8_t EvOutA_Def =  4;         /* PA 2 */
static constexpr uint8_t EvOutA_Alt =  9;         /* PA 7 */

static constexpr uint8_t EvOutB_Def = InvalidPin; /* PB 2 */ /** Pin is not available for use in TQFP32 package **/
static constexpr uint8_t EvOutB_Alt = InvalidPin; /* PB 7 */ /** Pin is not available for use in any    package **/

static constexpr uint8_t EvOutC_Def = 13;         /* PC 2 */
static constexpr uint8_t EvOutC_Alt = InvalidPin; /* PC 7 */ /** Pin is not available for use in TQFP32 package **/

static constexpr uint8_t EvOutD_Def = 16;         /* PD 2 */
static constexpr uint8_t EvOutD_Alt = 25;         /* PD 7 */ /** Pin is not available for use in Thinary ATmega4808 board (it is also the VREFA pin) **/

static constexpr uint8_t EvOutE_Def = InvalidPin; /* PE 2 */ /** Pin is not available for use in TQFP32 package **/
static constexpr uint8_t EvOutE_Alt = InvalidPin; /* PE 7 */ /** Pin is not available for use in any    package **/

static constexpr uint8_t EvOutF_Def = 18;         /* PF 2 */
static constexpr uint8_t EvOutF_Alt = InvalidPin; /* PF 7 */ /** Pin is not available for use in any    package **/

// Additional timer/counter B pins
static constexpr uint8_t TCB1_OutDef =  5;         /* PA 3 */
static constexpr uint8_t TCB1_OutAlt = 21;         /* PF 5 */

static constexpr uint8_t TCB2_OutDef = 11;         /* PC 0 */
static constexpr uint8_t TCB2_OutAlt = InvalidPin; /* PB 4 */ /** Pin  is not available for use in TQFP32 package **/

static constexpr uint8_t TCB3_OutDef = InvalidPin; /* PB 5 */ /** TCB3 is not available for use in all x08 series **/
static constexpr uint8_t TCB3_OutAlt = InvalidPin; /* PC 1 */ /** TCB3 is not available for use in all x08 series **/


#endif
