//******************************************************************************
//! @ProjectName: iSTP
//! @ModuleName:  conf_board.h
//!
//! @Purpose:
//! This is a header module for configuration of iSTP board
//!
//! Honeywell Proprietary
//!
//! @Copyright 2016 Honeywell SGN. All rights reserved.
//!
//! SVN revision information:
//! @version  $Revision: 2189 $
//! @author   $Author: Pavel Sofr $
//! @date     $Date: 2016-11-30 16:04:56 +0100 (Wed, 30 Nov 2016) $
//******************************************************************************


#ifndef CONF_BOARD_H
#define CONF_BOARD_H

//******************************************************************************
// Definitions
//******************************************************************************
#define BOARD_FREQ_MAINCK_XTAL			(12000000U)
#define BOARD_FREQ_MAINCK_BYPASS		(12000000U)
#define BOARD_FREQ_SLCK_XTAL			(0U)
#define BOARD_FREQ_SLCK_BYPASS			(0U)
#define BOARD_OSC_STARTUP_US			15625
//#define BOARD_MCK						CHIP_FREQ_CPU_MAX

#ifdef MAINBOARD40
	// Power Lines (PIOB)				(active low)
	#define PWR_3V3						(1<<0)
	#define PWR_5VD						(1<<1)
	#define PWR_5VA						(1<<2)
	#define PWR_EXT						(1<<3)

	// USB protocol power status masks
	#define PWR_3V3_MASK				0x0001
	#define PWR_5VD_MASK				0x0002
	#define PWR_5VA_MASK				0x0004
	#define PWR_EXT_MASK				0x0008


	// Chip (Board) Select GPIOs (PIOC)	(active low)
	#define CS_A0						(1<<18)
	#define CS_A1						(1<<19)
	#define CS_A2						(1<<16)
	#define CS_A3						(1<<17)
	#define CS_A4						(1<<11)
	#define CS_A5						(1<<14)
	#define CS_B0						(1<<3)
	#define CS_B1						(1<<4)
	#define CS_B2						(1<<5)
	#define CS_B3						(1<<6)
	#define CS_B4						(1<<7)
	#define CS_B5						(1<<0)
	#define CS_C0						(1<<9)
	#define CS_C1						(1<<10)
	#define CS_C2						(1<<28)
	#define CS_C3						(1<<8)
	#define CS_C4						(1<<2)
	#define CS_C5						(1<<1)
	// Chip (mainboard) Select GPIOs (PIOA and GPIOC)	Not used
	#define CS_IMU						(0)   // GPIOA
	#define CS_ADC						(0)   // GPIOC
	#define CS_HUM						(0)	  // GPIOA

#else
	// Power Lines (PIOB)				(active low)
	#define PWR_3V3						(1<<3)
	#define PWR_5VD						(1<<2)
	#define PWR_5VA						(1<<1)
	#define PWR_EXT						(1<<0)

	// USB protocol power status masks
	#define PWR_EXT_MASK	0x0001
	#define PWR_5VA_MASK	0x0002
	#define PWR_5VD_MASK	0x0004
	#define PWR_3V3_MASK	0x0008

	// Chip (Board) Select GPIOs (PIOC)	(active low)
	#define CS_B0						(1<<14)	// GPIO 10
	#define CS_B1						(1<<11)	// GPIO 11
	#define CS_B2						(1<<17)	// GPIO 12
	#define CS_B3						(1<<16)	// GPIO 13
	#define CS_B4						(1<<19)	// GPIO 14
	#define CS_B5						(1<<18)	// GPIO 15


	#define CS_A0						(1<<7)	// GPIO 20
	#define CS_A1						(1<<0)	// GPIO 21
	#define CS_A2						(1<<5)	// GPIO 22
	#define CS_A3						(1<<6)	// GPIO 23
	#define CS_A4						(1<<3)	// GPIO 24
	#define CS_A5						(1<<4)	// GPIO 25


	#define CS_C0						(1<<9)	// GPIO 30
	#define CS_C1						(1<<10)	// GPIO 31
	#define CS_C2						(1<<28)	// GPIO 32
	#define CS_C3						(1<<8)	// GPIO 33
	#define CS_C4						(1<<2)	// GPIO 34
	#define CS_C5						(1<<1)	// GPIO 35

	// Chip (mainboard) Select GPIOs (PIOA and GPIOC)	(active low)
	#define CS_IMU						(1<<18)   // GPIOA
	#define CS_ADC						(1<<30)   // GPIOC	
	#define CS_HUM						(1<<31)	  // GPIOA
#endif


// LEDs	(PIOA)						(active high)
#ifdef MAINBOARD54
	#define LED_7					(1<<30)	
	#define LED_14					(1<<23)
	#define LED_15					(1<<24)
	#define LED_16					(1<<25)
#elif defined (MAINBOARD53)
	#define LED_7				(1<<30)
#else //MAINBOARD 4.0 & 5.1
	#define LED_7					(1<<7)
#endif

#define LED_4						(1<<15)
#define LED_8						(1<<16)
#define LED_C						LED_4
#define LED_L						LED_8
#define LED_R						LED_7

#ifdef MAINBOARD54
	#define EN_AXIS_A_PORT			PIOC
	#define EN_AXIS_A_PIN			(1<<12)
	#define EN_AXIS_B_PORT			PIOB
	#define EN_AXIS_B_PIN			(1<<14)
	#define EN_AXIS_C_PORT			PIOA
	#define EN_AXIS_C_PIN			(1<<11)
#endif

// DEC_A, DEC_B, DEC_C (PIOC)		(active high)
#define DEC_A						(1<<20)
#define DEC_B						(1<<21)
#define DEC_C						(1<<22)

// Master SPI (Sensor-boards interface)
#define MSPI_MISO					(1<<12)
#define MSPI_MOSI					(1<<13)
#define MSPI_SCLK					(1<<14)

#ifndef MAINBOARD54
// USB VBUS Monitor
#define VBUS						(1<<31)
#define VBUS_PIN					IOPORT_CREATE_PIN(PIOA, 31)
#define VBUS_PRESENT				true

// USB Communication protocol
#define BUFFER_LENGTH				512
#define HEADER_LENGTH				56
#define PC_MSG_START				0x23232323
#define PC_MSG_STOP					0x24242424
#endif

// EEPROM defines
#define EEPROM_WREN					0x06
#define EEPROM_WRDI					0x04
#define EEPROM_RDSR					0x05
#define EEPROM_WRSR					0x01
#define EEPROM_READ					0x03
#define EEPROM_WRITE				0x02

#define EEPROM_LOCK_NONE			0x00
#define EEPROM_LOCK_QUARTER			0x04
#define EEPROM_LOCK_HALF			0x08
#define EEPROM_LOCK_FULL			0x0C

#define EEPROM_RDY					0x01
#define EEPROM_WEN					0x02
#define EEPROM_WEN_MASK				0x03

/** Enable Com Port. */
#define CONF_BOARD_UART_CONSOLE

/* USART1 module is used in RS485 mode. */
#define CONF_BOARD_USART_RXD
#define CONF_BOARD_USART_TXD
#define CONF_BOARD_USART_RTS

/* RS485 transceiver receiver enable pin configuration. */
/*
#define CONF_BOARD_ADM3485_RE

#define USART_SERIAL                 USART1
#define USART_SERIAL_ID              ID_USART1
#define USART_SERIAL_BAUDRATE        115200//12000000
#define USART_SERIAL_CHAR_LENGTH     US_MR_CHRL_8_BIT
#define USART_SERIAL_PARITY          US_MR_PAR_NO
#define USART_SERIAL_STOP_BIT        US_MR_NBSTOP_1_BIT*/

/** USART1 pin RX */
#define PIN_USART1_RXD				{PIO_PA21A_RXD1, PIOA, ID_PIOA, PIO_PERIPH_A, PIO_DEFAULT}
#define PIN_USART1_RXD_IDX			(PIO_PA21_IDX)
#define PIN_USART1_RXD_FLAGS		(PIO_PERIPH_A | PIO_DEFAULT)
/** USART1 pin TX */
#define PIN_USART1_TXD				{PIO_PA22A_TXD1, PIOA, ID_PIOA, PIO_PERIPH_A, PIO_DEFAULT}
#define PIN_USART1_TXD_IDX			(PIO_PA22_IDX)
#define PIN_USART1_TXD_FLAGS		(PIO_PERIPH_A | PIO_DEFAULT)
/** USART1 pin CTS */
#define PIN_USART1_CTS				{PIO_PA25A_CTS1, PIOA, ID_PIOA, PIO_PERIPH_A, PIO_DEFAULT}
#define PIN_USART1_CTS_IDX			(PIO_PA25_IDX)
#define PIN_USART1_CTS_FLAGS		(PIO_PERIPH_A | PIO_PULLUP)
/** USART1 pin RTS */
#define PIN_USART1_RTS				{PIO_PA24A_RTS1, PIOA, ID_PIOA, PIO_PERIPH_A, PIO_DEFAULT}
#define PIN_USART1_RTS_IDX			(PIO_PA24_IDX)
#define PIN_USART1_RTS_FLAGS		(PIO_PERIPH_A | PIO_DEFAULT)
/** USART1 pin ENABLE */
#define PIN_USART1_EN				{PIO_PA23A_SCK1, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT}
#define PIN_USART1_EN_IDX			(PIO_PA23_IDX)
#define PIN_USART1_EN_FLAGS			(PIO_OUTPUT_0 | PIO_DEFAULT)
/** USART1 pin SCK */
#define PIN_USART1_SCK				{PIO_PA23A_SCK1, PIOA, ID_PIOA, PIO_PERIPH_A, PIO_DEFAULT}
#define PIN_USART1_SCK_IDX			(PIO_PA23_IDX)
#define PIN_USART1_SCK_FLAGS		(PIO_PERIPH_A | PIO_DEFAULT)

/** UART0 pin RX */
#define PIN_UART0_RXD				{PIO_PA9A_URXD0, PIOA, ID_PIOA, PIO_PERIPH_A, PIO_DEFAULT}
#define PIN_UART0_RXD_IDX			(PIO_PA9_IDX)
#define PIN_UART0_RXD_FLAGS			(PIO_PERIPH_A | PIO_DEFAULT)

/** UART0 pin TX */
#define PIN_UART0_TXD				{PIO_PA10A_UTXD0, PIOA, ID_PIOA, PIO_PERIPH_A, PIO_DEFAULT}
#define PIN_UART0_TXD_IDX			(PIO_PA10_IDX)
#define PIN_UART0_TXD_FLAGS			(PIO_PERIPH_A | PIO_DEFAULT)


#define PIN_RE_IDX					PIN_USART1_CTS_IDX
#define PIN_RE_FLAGS				(PIO_OUTPUT_0 | PIO_DEFAULT)
#define PIN_USART_TXD_IDX			PIN_USART1_TXD_IDX
#define PIN_USART_TXD_FLAGS			(PIO_PERIPH_A | PIO_DEFAULT)
#define PIN_USART_TXD_IO_FLAGS		(PIO_OUTPUT_0 | PIO_DEFAULT)

#endif // CONF_BOARD_H
