/*
  Copyright (c) 2014-2015 Arduino LLC.  All right reserved.

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  See the GNU Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#ifndef _VARIANT_MOTEINO_M0_
#define _VARIANT_MOTEINO_M0_

// The definitions here needs a SAMD core >=1.6.10
#define ARDUINO_SAMD_VARIANT_COMPLIANCE 10610

/*----------------------------------------------------------------------------
 *        Definitions
 *----------------------------------------------------------------------------*/

// Mapping PAxx and PBxx pin labels to Arduino pin numbers
// github.com/arduino/ArduinoCore-samd/blob/1.8.6/variants/arduino_zero/variant.cpp
#define SCC_PA11  (0ul)
#define SCC_PA10  (1ul)
#define SCC_PA14  (2ul)
#define SCC_PA09  (3ul)
#define SCC_PA08  (4ul)
#define SCC_PA15  (5ul)
#define SCC_PA20  (6ul)
#define SCC_PA21  (7ul)
#define SCC_PA06  (8ul)
#define SCC_PA07  (9ul)
#define SCC_PA18  (10ul)
#define SCC_PA16  (11ul)
#define SCC_PA19  (12ul)
#define SCC_PA17  (13ul)
#define SCC_PA02  (14ul)
#define SCC_PB08  (15ul)
#define SCC_PB09  (16ul)
#define SCC_PA04  (17ul)
#define SCC_PA05  (18ul)
#define SCC_PB02  (19ul)
#define SCC_PA22  (20ul)
#define SCC_PA23  (21ul)
#define SCC_PA12  (22ul)
#define SCC_PB10  (23ul)
#define SCC_PB11  (24ul)
#define SCC_PB03  (25ul)
#define SCC_PA27  (26ul)
#define SCC_PA28  (27ul)
#define SCC_PA24  (28ul)
#define SCC_PA25  (29ul)
#define SCC_PB22  (30ul)
//#define SCC_PA23  (31ul)
//#define SCC_PA22  (32ul)
#define SCC_PB23  (33ul)
//#define SCC_PA19  (34ul)
//s#define SCC_PA16  (35ul)
//#define SCC_PA18  (36ul)
//#define SCC_PA17  (37ul)
#define SCC_PA13  (38ul)
//#define SCC_PA21  (39ul)
//#define SCC_PA06  (40ul)
//#define SCC_PA07  (41ul)
#define SCC_PA03  (42ul)
//#define SCC_PA02  (43ul)
#define SCC_PA30  (44ul)
#define SCC_PA31  (45ul)

/** Frequency of the board main oscillator */
#define VARIANT_MAINOSC		(32768ul)

/** Master clock frequency */
#define VARIANT_MCK	(F_CPU)

/*----------------------------------------------------------------------------
 *        Headers
 *----------------------------------------------------------------------------*/

#include "WVariant.h"

#ifdef __cplusplus
#include "SERCOM.h"
#include "Uart.h"
#endif // __cplusplus

#ifdef __cplusplus
extern "C"
{
#endif // __cplusplus

/*----------------------------------------------------------------------------
 *        Pins
 *----------------------------------------------------------------------------*/

// Number of pins defined in PinDescription array
#ifdef __cplusplus
extern "C" unsigned int PINCOUNT_fn();
#endif
#define PINS_COUNT           (PINCOUNT_fn())
#define NUM_DIGITAL_PINS     (20u)
#define NUM_ANALOG_INPUTS    (8u)
#define NUM_ANALOG_OUTPUTS   (1u)
#define analogInputToDigitalPin(p)  ((p < 6u) ? (p) + 14u : -1)

#define digitalPinToPort(P)        ( &(PORT->Group[g_APinDescription[P].ulPort]) )
#define digitalPinToBitMask(P)     ( 1 << g_APinDescription[P].ulPin )
//#define analogInPinToBit(P)        ( )
#define portOutputRegister(port)   ( &(port->OUT.reg) )
#define portInputRegister(port)    ( &(port->IN.reg) )
#define portModeRegister(port)     ( &(port->DIR.reg) )
#define digitalPinHasPWM(P)        ( g_APinDescription[P].ulPWMChannel != NOT_ON_PWM || g_APinDescription[P].ulTCChannel != NOT_ON_TIMER )

/*
 * digitalPinToTimer(..) is AVR-specific and is not defined for SAMD
 * architecture. If you need to check if a pin supports PWM you must
 * use digitalPinHasPWM(..).
 *
 * https://github.com/arduino/Arduino/issues/1833
 */
// #define digitalPinToTimer(P)

// LEDs
#define PIN_LED_13           SCC_PB08
#define PIN_LED_RXL          SCC_PB08
#define PIN_LED_TXL          SCC_PB08
#define PIN_LED              PIN_LED_13
#define PIN_LED2             PIN_LED_RXL
#define PIN_LED3             PIN_LED_TXL
#define LED_BUILTIN          PIN_LED_13

#define SS_FLASHMEM          (8u)

#define EXTERNAL_FLASH_USE_SPI SPI
#define EXTERNAL_FLASH_USE_CS  SS_FLASHMEM

/*
 * Analog pins
 */
#define PIN_A0               (14ul)
#define PIN_A1               (15ul)
#define PIN_A2               (16ul)
#define PIN_A3               (17ul)
#define PIN_A4               (18ul)
#define PIN_A5               (19ul)
#define PIN_A6               (44ul)
#define PIN_A7               (45ul)
#define PIN_DAC0             (14ul)

static const uint8_t A0  = PIN_A0;
static const uint8_t A1  = PIN_A1;
static const uint8_t A2  = PIN_A2;
static const uint8_t A3  = PIN_A3;
static const uint8_t A4  = PIN_A4;
static const uint8_t A5  = PIN_A5;
static const uint8_t A6  = PIN_A6 ;
static const uint8_t A7  = PIN_A7 ;
static const uint8_t DAC0 = PIN_DAC0;
#define ADC_RESOLUTION		12

// Other pins
#define PIN_ATN              (38ul)
#define PIN_SWCLK            (46ul)
#define PIN_SWIO             (47ul)
static const uint8_t ATN = PIN_ATN;
static const uint8_t SWCLK = PIN_SWCLK;
static const uint8_t SWIO = PIN_SWIO;

/*
 * Serial interfaces
 */

// SCC uses different serial ports from arduino_zero as defined below

// Serial5
#define PIN_SERIAL5_RX       SCC_PA23
#define PIN_SERIAL5_TX       SCC_PA22
#define PAD_SERIAL5_TX       (UART_TX_PAD_0)
#define PAD_SERIAL5_RX       (SERCOM_RX_PAD_1)

// Serial3
#define PIN_SERIAL3_RX       SCC_PA17
#define PIN_SERIAL3_TX       SCC_PA16
#define PAD_SERIAL3_TX       (UART_TX_PAD_0)
#define PAD_SERIAL3_RX       (SERCOM_RX_PAD_1)

// Serial1
#define PIN_SERIAL1_RX       49
#define PIN_SERIAL1_TX       48
#define PAD_SERIAL1_TX       (UART_TX_PAD_0)
#define PAD_SERIAL1_RX       (SERCOM_RX_PAD_1)

/*
 * SPI Interfaces
 */

// SCC board matches standard arduino_zero no no changes are needed here

#define SPI_INTERFACES_COUNT 1

#define PIN_SPI_MISO         (22u)
#define PIN_SPI_MOSI         (23u)
#define PIN_SPI_SCK          (24u)
#define PERIPH_SPI           sercom4
#define PAD_SPI_TX           SPI_PAD_2_SCK_3
#define PAD_SPI_RX           SERCOM_RX_PAD_0
#define SS                   SS       //ensure that any libs/sketches which call "#ifndef(SS)..." won't fail because SS is a variable and not a #define
static const uint8_t SS	  = PIN_A2 ;	// SERCOM4 last PAD is present on A2 but HW SS isn't used. Set here only for reference.
static const uint8_t MOSI = PIN_SPI_MOSI ;
static const uint8_t MISO = PIN_SPI_MISO ;
static const uint8_t SCK  = PIN_SPI_SCK ;

/*
 * Wire Interfaces
 */
#define WIRE_INTERFACES_COUNT 1

// Updated to match the SCC board, also updated in variant.cpp
#define PIN_WIRE_SDA         (4u)
#define PIN_WIRE_SCL         (38u)
#define PERIPH_WIRE          sercom2
#define WIRE_IT_HANDLER      SERCOM2_Handler

static const uint8_t SDA = PIN_WIRE_SDA;
static const uint8_t SCL = PIN_WIRE_SCL;

/*
 * USB
 */
#define PIN_USB_HOST_ENABLE (27ul)
#define PIN_USB_DM          (28ul)
#define PIN_USB_DP          (29ul)
#define USB_HOST_EN         PIN_USB_HOST_ENABLE
#define USB_HOST_ENABLE     PIN_USB_HOST_ENABLE

/*
 * I2S Interfaces
 */
#define I2S_INTERFACES_COUNT 1

#define I2S_DEVICE          0
#define I2S_CLOCK_GENERATOR 3
#define PIN_I2S_SD          (9u)
#define PIN_I2S_SCK         (1u)
#define PIN_I2S_FS          (0u)

#ifdef __cplusplus
}
#endif

/*----------------------------------------------------------------------------
 *        Arduino objects - C++ only
 *----------------------------------------------------------------------------*/

#ifdef __cplusplus

/*	=========================
 *	===== SERCOM DEFINITION
 *	=========================
*/
extern SERCOM sercom0;
extern SERCOM sercom1;
extern SERCOM sercom2;
extern SERCOM sercom3;
extern SERCOM sercom4;
extern SERCOM sercom5;

extern Uart Serial1;
extern Uart Serial3;
extern Uart Serial5;

#endif

#ifdef __cplusplus
extern "C" {
#endif
unsigned int PINCOUNT_fn();
#ifdef __cplusplus
}
#endif

// These serial port names are intended to allow libraries and architecture-neutral
// sketches to automatically default to the correct port name for a particular type
// of use.  For example, a GPS module would normally connect to SERIAL_PORT_HARDWARE_OPEN,
// the first hardware serial port whose RX/TX pins are not dedicated to another use.
//
// SERIAL_PORT_MONITOR        Port which normally prints to the Arduino Serial Monitor
//
// SERIAL_PORT_USBVIRTUAL     Port which is USB virtual serial
//
// SERIAL_PORT_LINUXBRIDGE    Port which connects to a Linux system via Bridge library
//
// SERIAL_PORT_HARDWARE       Hardware serial port, physical RX & TX pins.
//
// SERIAL_PORT_HARDWARE_OPEN  Hardware serial ports which are open for use.  Their RX & TX
//                            pins are NOT connected to anything by default.
#define SERIAL_PORT_USBVIRTUAL      Serial
#define SerialUSB                   Serial //for compatibility with v1.4.0 and prior
#define SERIAL_PORT_MONITOR         Serial
// Serial has no physical pins broken out, so it's not listed as HARDWARE port
#define SERIAL_PORT_HARDWARE        Serial1
#define SERIAL_PORT_HARDWARE_OPEN   Serial1

#endif

