#ifndef _DRVGPIO_H
#define _DRVGPIO_H

/****************************************************************************
 *
 * Projekt			:	Glacier Batterieboxdeckel Prüfgerät
 * Dateiname		:	drvGpio.h
 * Autor			:	orginal MHoffmann, abgeändert für dieses Projekt Irene Saxer
 * Erstellungsdatum	:	17.04.2019
 * Beschreibung		:	"Treiber fuer die verwendeten IO-Leitungen des ATmega"
 *
 *
 * Versionsgeschichte
 * 	Revision 1.0	17.04.2019 SAI erste Version
 *
 ****************************************************************************/

/****************************************************************************/
/* COMPILER Direktiven */

/****************************************************************************/
/* INCLUDE Dateien */
#include "Common.h"

/****************************************************************************/
/* KONSTANTEN */
#define OBJSMPL_RESET_TIME	100 //10000
#define CALIBRATION 3000

//Bitmasken fuer Zugriffe auf Ports und Pins
#define BM_LCD_POWER
#define BM_LCD_ENABLE	0x10	//PD4
#define BM_LCD_RW		0x40	//PC6
#define BM_LCD_INSTSEL	0x20	//PD5

#define BM_KEY			0x01	//PC0

#define BM_LED			0x80	//PA7

#define BM_ENDSCHALTER	0x20	//PA5 (Mess6)

#define BM_RXD			0x01	//PD0
#define BM_TXD			0x02	//PD1

#define BM_EE_SCK		0x80	//PB7
#define BM_EE_SI		0x20	//PB5
#define BM_EE_SO		0x40	//PB6
#define BM_EE_CS		0x80	//PA7

#define BM_RTC_SCK		0x80	//PB7
#define BM_RTC_SI		0x20	//PB5
#define BM_RTC_SO		0x40	//PB6
#define BM_RTC_CS		0x02	//PC1

#define EE_CS			DDRA|=BM_EE_CS;PORTA&=~BM_EE_CS	//low active
#define EE_NCS			DDRA|=BM_EE_CS;PORTA|=BM_EE_CS
#define EE_ENABLE		DDRB|=(BM_EE_SCK|BM_EE_SI|BM_EE_SO);SPSR = (1<<SPI2X);SPCR = (1<<SPE)|(1<<MSTR)|(0<<SPR0)|(0<<SPR1)|(0<<CPOL)|(0<<CPHA)|(0<<DORD);gu8_Tmp=SPSR;gu8_Tmp=SPDR
#define EE_DISABLE		SPCR = 0;PORTB &= ~(BM_EE_SCK|BM_EE_SI|BM_EE_SO);DDRB &= ~(BM_EE_SCK|BM_EE_SI|BM_EE_SO)

#define RTC_CS			DDRC|=BM_RTC_CS;PORTC|=BM_RTC_CS	//High active
#define RTC_NCS			DDRC|=BM_RTC_CS;PORTC&=~BM_RTC_CS
#define RTC_ENABLE		DDRB|=(BM_RTC_SCK|BM_RTC_SI|BM_RTC_SO);SPSR = (1<<SPI2X);SPCR = (1<<SPE)|(1<<MSTR)|(0<<SPR0)|(1<<SPR1)|(0<<CPOL)|(0<<CPHA)|(0<<DORD);gu8_Tmp=SPSR;gu8_Tmp=SPDR
#define RTC_DISABLE		SPCR = 0;PORTB &= ~(BM_RTC_SCK|BM_RTC_SI|BM_RTC_SO);DDRB &= ~(BM_RTC_SCK|BM_RTC_SI|BM_RTC_SO)

#define BM_U_POT1			0x01	//PA0
#define BM_U_POT2			0x02	//PA1
#define BM_U_POT3			0x04	//PA2
#define BM_U_POT4			0x08	//PA3
#define BM_U_POT5			0x10	//PA4
#define BM_U_POT6			0x20	//PA5
#define BM_U_POT7			0x40	//PA6

/*
#define ADC_U_POT1			0
#define ADC_U_POT2			1
#define ADC_U_POT3			2
#define ADC_U_POT4			3
#define ADC_U_POT5			4
#define ADC_U_POT6			5
#define ADC_U_POT7			6
*/

#define ADC_U_HALL			0
#define ADC_U_CALIB			1
#define ADC_U_OFFSET		2
#define ADC_U_RS232			3


#define DAUERMAGNET			1
#define SPULE				2

//Portzuordnung der LCD-Datenleitungen
#define LCD_DATAOUT	PORTB
#define LCD_DATAIN	PINB
#define LCD_ENABLE	PORTD |=  BM_LCD_ENABLE
#define LCD_DISABLE PORTD &= ~BM_LCD_ENABLE
#define LCD_READ	PORTC |=  BM_LCD_RW;DDRB=0x00
#define LCD_WRITE	PORTC &= ~BM_LCD_RW;DDRB=0xff
#define LCD_DATASEL	PORTD |=  BM_LCD_INSTSEL
#define LCD_INSTSEL	PORTD &= ~BM_LCD_INSTSEL

#define LCD_ON

#define BM_PWM_LED1			0x40	//PD6
#define BM_PWM_LED2			0x80	//PD7

#define SET_LED_RT 	TCCR2A |= (1<<COM2A1) | (1<<COM2A0)
#define CLR_LED_RT	TCCR2A &= ~((1<<COM2A1) | (1<<COM2A0))
#define SET_LED_GN 	TCCR2A |= (1<<COM2B1)
#define CLR_LED_GN	TCCR2A &= ~(1<<COM2B1)

#define SET_U_EXT	PORTC |= (1 << PC7)
#define CLR_U_EXT	PORTC &= ~(1 << PC7)

#define KEY_STATE	(PINC & (BM_KEY))
#define ENDSCHALTER_STATE (PINA & (BM_ENDSCHALTER))

#define DRVGPIO_CALIB_DONE	-2

// Extern ADC
/*********** SCK ALS AUSGANG DEFINIEREN ***************/
#define EADC_SCK		(1<<PORTB7)
#define DEF_EADC_SCK	DDRB  |= EADC_SCK
#define SET_EADC_SCK	PORTB |= EADC_SCK
#define CLR_EADC_SCK	PORTB &=~EADC_SCK 

/*********** MISO ALS EINGANG DEFINIEREN ***************/
#define EADC_DATA		(1<<PORTB4)
#define DEF_EADC_DATA	DDRB &=~EADC_DATA

/*********** PA4 ALS AUSGANG DEFINIEREN FÜR ADC CS ***************/
#define EADC_SEL		(1<<PA4)
#define DEF_EADC_SEL	DDRA  |= EADC_SEL
#define SET_EADC_SEL	PORTA |= EADC_SEL
#define CLR_EADC_SEL	PORTA &=~EADC_SEL
/****************************************************************************/
/* Globale Variablen */

typedef struct
{
	uint8_t		u8_Key:1;			//1 == gedrueckt ( :1 definiert ein 1-bit Bitfeld)
	uint8_t		u8_Endschlater:1;	//1 == gedrückt
	uint8_t		u8_ValveDetect:1;	//1 == Ventil vorhanden
	uint8_t		u8_LastKey;
	uint16_t	u16_TS;
}TGpioIntf;

extern TGpioIntf gudt_GpioIntf;
extern uint8_t	gu8_Tmp;
/****************************************************************************/
/* Prototypen */

void DrvGpio_Init(void);
int16_t	DrvGpio_Handler(void);

#endif	// #define _DRVGPIO_H
