/*
 * tipos.h
 *
 * Created: 03/06/2018 7:57:40
 *  Author: Ledo
 */ 


#ifndef TIPOS_H_
#define TIPOS_H_

//System LED
#define SYS_LED                 PIO_PD31_IDX
#define SYS_LED_MASK            (1<<31)
//Transistors Digital Outputs
#define OUT0_PIN                PIO_PA0_IDX
#define OUT0_PIN_MASK           (1<<0)
#define OUT1_PIN                PIO_PA1_IDX
#define OUT1_PIN_MASK           (1<<1)
#define OUT2_PIN                PIO_PA2_IDX
#define OUT2_PIN_MASK           (1<<2)

//EQM402B Control Signals (J15-3 and J15-4)
#define MOTOR_CNTRL_PIN         PIO_PA16_IDX
#define MOTOR_CNTRL_PIN_MASK    (1<<16)
#define BRAKE_CNTRL_PIN         PIO_PA25_IDX
#define BRAKE_CNTRL_PIN_MASK    (1<<25)


//Memoria Flash serie Winbond
#define WB_P_PIN                PIO_PD23_IDX
#define WB_P_PIN_MASK           (1<<23)
#define WB_CS_PIN               PIO_PA11_IDX
#define WB_CS_PIN_MASK          (1<<11)

//AD7843 TP Controller
#define AD7843_CS_PIN               PIO_PA9_IDX
#define AD7843_CS_PIN_MASK          (1<<9)
#define AD7843_TP_PEN_PIN           PIO_PA26_IDX
#define AD7843_TP_PEN_PIN_MASK      (1<<26)
#define AD7843_SDI_PIN               PIO_PA13_IDX
#define AD7843_SDI_PIN_MASK          (1<<13)

//External SPI Chip Select
#define EXT_SPI_CS_PIN               PIO_PA10_IDX
#define EXT_SPI_CS_PIN_MASK          (1<<10)


/*** Display TFT *******************************/

#define DB0_PIN_bp               0
#define DB1_PIN_bp               1
#define DB2_PIN_bp               2
#define DB3_PIN_bp               3
#define DB4_PIN_bp               4
#define DB5_PIN_bp               5
#define DB6_PIN_bp               6
#define DB7_PIN_bp               7
#define DB8_PIN_bp               8
#define DB9_PIN_bp               9
#define DB10_PIN_bp              10
#define DB11_PIN_bp              11
#define DB12_PIN_bp              12
#define DB13_PIN_bp              13
#define DB14_PIN_bp              14
#define DB15_PIN_bp              15
#define TFT_DC_PIN_bp            18
#define TFT_CS_PIN_bp            19
#define TFT_RD_PIN_bp            21
#define TFT_WR_PIN_bp            22
#define TFT_IM0_PIN_bp           25
#define TFT_IM1_PIN_bp           26
#define TFT_BKL_PIN_bp           27
#define TFT_RST_PIN_bp           28


#define DB0_PIN_MASK             (1<<0)
#define DB1_PIN_MASK             (1<<1)
#define DB2_PIN_MASK             (1<<2)
#define DB3_PIN_MASK             (1<<3)
#define DB4_PIN_MASK             (1<<4)
#define DB5_PIN_MASK             (1<<5)
#define DB6_PIN_MASK             (1<<6)
#define DB7_PIN_MASK             (1<<7)
#define DB8_PIN_MASK             (1<<8)
#define DB9_PIN_MASK             (1<<9)
#define DB10_PIN_MASK            (1<<10)
#define DB11_PIN_MASK            (1<<11)
#define DB12_PIN_MASK            (1<<12)
#define DB13_PIN_MASK            (1<<13)
#define DB14_PIN_MASK            (1<<14)
#define DB15_PIN_MASK            (1<<15)
#define DB16_PIN_MASK            (1<<16)
#define DB17_PIN_MASK            (1<<17)
#define TFT_DC_PIN_MASK          (1<<18)
#define TFT_CS_PIN_MASK          (1<<19)
#define TFT_RD_PIN_MASK          (1<<21)
#define TFT_WR_PIN_MASK          (1<<22)
#define TFT_IM0_PIN_MASK         (1<<25)
#define TFT_IM1_PIN_MASK         (1<<26)
#define TFT_BKL_PIN_MASK         (1<<27)
#define TFT_RST_PIN_MASK         (1<<28)


//*********************************
// COLORS
//*********************************
// VGA color palette
#define VGA_BLACK		0x0000
#define VGA_WHITE		0xFFFF
#define VGA_RED			0xF800
#define VGA_GREEN		0x0400
#define VGA_BLUE		0x001F
#define VGA_SILVER		0xC618
#define VGA_GRAY		0x8410
#define VGA_MAROON		0x8000
#define VGA_YELLOW		0xFFE0
#define VGA_OLIVE		0x8400
#define VGA_LIME		0x07E0
#define VGA_AQUA		0x07FF
#define VGA_TEAL		0x0410
#define VGA_NAVY		0x0010
#define VGA_FUCHSIA		0xF81F
#define VGA_PURPLE		0x8010
#define VGA_TRANSPARENT	0xFFFFFFFF

#define RS4_BUFFER_LENGHT      64
extern uint8_t rs4_rxbuffer[RS4_BUFFER_LENGHT];
extern uint8_t rs4_txbuffer[RS4_BUFFER_LENGHT];
extern uint8_t rs4_rx_index;
extern bool rs4_datos_listos;

/**************************************************************
* MACROS
**************************************************************/
#define MOTOR_ON() pio_set_pin_high(MOTOR_CNTRL_PIN);
#define MOTOR_OFF() pio_set_pin_low(MOTOR_CNTRL_PIN);
#define BRAKE_ON() pio_set_pin_high(BRAKE_CNTRL_PIN);
#define BRAKE_OFF() pio_set_pin_low(BRAKE_CNTRL_PIN);
#define OUT0_ON() pio_set_pin_high(OUT0_PIN);
#define OUT0_OFF() pio_set_pin_low(OUT0_PIN);
//#define TP_PRESET() io_port_ge
//#define TOUCH_PRESSED()  !ioport_get_pin_level(PIO_PA26_IDX)


typedef struct PARAMETROS
{
	uint8_t memory_blank;
}PARAMETROS;

extern PARAMETROS params;

typedef struct FLAGS
{
	volatile bool sys_led_toogle;
	volatile bool CAN_cycle;
	bool bitmap;
	volatile bool encoder_pulse;
	bool keys;
	bool show_time;
}FLAGS;

extern FLAGS Flags;


typedef struct ENCODER
{
	volatile bool direction;
	volatile uint8_t pulse_counter;
	volatile bool pulse;
	
}ENCODER;
extern ENCODER Encoder;




#endif /* TIPOS_H_ */