/*
 * globales.h
 *
 * Created: 30/05/2018 13:22:15
 *  Author: Ledo
 */ 


#ifndef GLOBALES_H_
#define GLOBALES_H_

#include <sam4e.h>
#include <stdbool.h>

#define SYS_LED                 PIO_PD30_IDX
#define SYS_LED_MASK            (1<<30)
#define BLUE_LED                PIO_PD31_IDX
#define BLUE_LED_MASK           (1<<31)

#define RS4_DE_PIN              PIO_PA29_IDX
#define RS4_DE_PIN_MASK         (1<<29)

#define OUT0_PIN                PIO_PD13_IDX
#define OUT0_PIN_MASK           (1<<13)
#define OUT1_PIN                PIO_PD14_IDX
#define OUT1_PIN_MASK           (1<<14)
#define OUT2_PIN                PIO_PD15_IDX
#define OUT2_PIN_MASK           (1<<15)

#define WIZNET_RST_PIN          PIO_PD29_IDX
#define WIZNET_RST_PIN_MASK     (1<<29)
#define WIZNET_CS_PIN           PIO_PA11_IDX
#define WIZNET_CS_PIN_MASK      (1<<11)

#define LTC1448_CS_PIN          PIO_PA9_IDX
#define LTC1448_CS_PIN_MASK     (1<<9)


#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;

typedef struct PARAMETROS
{
	uint32_t memory_blank;
	uint32_t lavar;
	uint32_t set_V_acido;
	uint32_t set_V_slurry;
	uint32_t set_V_agitador;
	uint32_t set_Temp_reactor;
	uint32_t set_Temp_intercambiador;
	uint32_t set_Temp_secador;
	uint32_t set_Temp_armario;
	uint32_t hora_lavado;
	uint32_t dia_lavado;
	uint32_t timeout_llenado_R;
	uint32_t timeout_vaciado_R;
	uint32_t timeout_llenado_I;
	uint32_t timeout_vaciado_I;
	/* Calibración y linearización */
	uint32_t K_cal; // calibracion x 1000;
	int32_t  P1; //Coefic polinomio x 10000;
	int32_t  P2; //Coefic polinomio x 10000;
	int32_t  P3; //Coefic polinomio x 10000;
	int32_t  P4; //Coefic polinomio x 10000;
	int32_t  q; //Coefic polinomio x 10000;

	}PARAMETROS;
	
extern PARAMETROS params;	

typedef struct bcd
{
	unsigned char dec_miles;
	unsigned char miles;
	unsigned char centenas;
	unsigned char decenas;
	unsigned char unidades;
}bcd;
extern bcd digitos;

/// Structure to store time and date
typedef struct m41t00_time_t
{
	uint8_t sec;            ///< Seconds:       0  to 59
	uint8_t min;            ///< Minutes:       0  to 59
	uint8_t hour;           ///< Hours:         0  to 23
	uint8_t day_of_week;    ///< Day of Week:   1  to 7
	uint8_t day_of_month;   ///< Days of Month: 1  to 31 (depending on month)
	uint8_t month;          ///< Months:        1  to 12
	uint8_t year;           ///< Years:         00 to 99
	uint8_t creg;
} m41t00_time_t;

extern m41t00_time_t decoded_time;
extern m41t00_time_t time;

extern uint16_t adc_value;
extern volatile bool encoder_direction;
extern volatile uint8_t encoder_pulse_counter;
extern volatile bool encoder_pulse;
extern void twi0_init_m41t00(void);
extern void twi_read_rtc(void);
extern void  Time_adjust(uint8_t min,uint8_t hora, uint8_t dia);
extern void configure_usart0(void);
extern void Usart0_sendBytes(uint8_t bytes);

extern void SPI_init(void);
extern uint8_t spi_master_transfer(uint8_t byte);
extern void ltc1448_dac_update(uint16_t chA,uint16_t chB);

 extern void soft_spi_init();
 extern void soft_spi_transfer(uint8_t);


#endif /* GLOBALES_H_ */