#ifndef __HWINIT_H
#define __HWINIT_H 


void hw_initialise (void);
void InitGPIO_POW(void);
void InitGPIO_EDK(void);

void uart_init(void);
void IRQ_Init(void);
void Pipak_Init(void);
void LED_Init(void);
void ADU_Init(void);
void SCI_Init(void);
void VdetInit(void);
void ReaderInit(void);
void InitGPIO(void);
void Init_ADC(tUChar port, tUChar mode, tUChar sweep, tUChar adint);



// MIFARE ma jiny krystal nez EMMARIN a POW (kolize frekvenci 18Mhz->13.56Mhz)
#ifdef READER_MIFARE
	#define f1_CLK_SPEED		13560000		// hodnota externiho krystalu pro ctecku MIFARE											
#else 
	#define f1_CLK_SPEED		18432000		// hodnota externiho krystalu pro EDK-Emmarin + POW
#endif


//BAUDRATE = ((f1_CLK_SPEED/16)/BAUD_RATE)-1
#define SET_BAUD_19200	 u0brg.byte = (unsigned char)(((f1_CLK_SPEED/16)/19200)-1)	
#define SET_BAUD_9600	 u0brg.byte = (unsigned char)(((f1_CLK_SPEED/16)/9600)-1)	
	


/*******************************************************************************/
// pokud je osazen DIP prepinac - definice funkce prepinacu, cislovani LSB=DIP8														   
/*******************************************************************************/
 #define DIP_PORT			p0.byte;	// port DIPu
 #define DIP_OC_INV			(1<<7)
 #define DIP_ADR			(0x0f)		// adresa 


/*******************************************************************************/
// definice masky vstupu ve strukture EDK.input
/*******************************************************************************/
#define EDK_IN_INPUT1		(1<<0)		// vstup 1
#define EDK_IN_INPUT2		(1<<1)		// vstup 2 
#define EDK_IN_BUZZER		(1<<2)		// pipak aktivni a/n
#define EDK_IN_RELE_INV		(1<<3)		// inverzni rele n/a
#define EDK_IN_EHS			(1<<4)		// sporic n/a
#define EDK_IN_READER2		(1<<5)		// ctecka c.2 n/a
#define EDK_IN_TAMPER		(1<<6)		// optozavora
#define EDK_IN_WGOUT		(1<<7)		// vystup WIEGAND/ACS a/n
#define EDK_IN_W26			(1<<8)		// format W26/W42 n/a
#define EDK_IN_REZ1			(1<<9)		// rezerva

// GR_EXT8
#define EDK_IN_INPUT3		(1<<0)		// vstup 3
#define EDK_IN_INPUT4		(1<<1)		// vstup 4 
#define EDK_IN_INPUT5		(1<<12)		// MUX vstup 1
#define EDK_IN_INPUT6		(1<<13)		// MUX vstup 2
#define EDK_IN_INPUT7		(1<<14)		// MUX vstup 3
#define EDK_IN_INPUT8		(1<<15)		// MUX vstup 4


/*******************************************************************************/
// definice masky v strukture EDK.output
/*******************************************************************************/
#define EDK_OUT_RELAY		(1<<0)		// rele
#define EDK_OUT_BUZZER		(1<<1)		// pipak
#define EDK_OUT_LED_RED		(1<<2)		// LED RED
#define EDK_OUT_LED_GRN		(1<<3)		// LED GREEN





/*******************************************************************************/
// definice pinu procesoru
/*******************************************************************************/

/*HW VERZE 2*/
#if (HW_VERSION == 2)

/*******************************************************************************/
// definice pro nacitani vstupu do promene EDK.input
/*******************************************************************************/
#define PIN_INPUT1		(p2_0)? 0:1
#define PIN_INPUT2		(p3_3)? 0:1

	// spolecne definice na HW2 pro EDK-MIFARE+EMMARIN
	#define LED_RED_PIN	 		p0_6
	#define LED_RED_DIR_PIN		pd0_6
	#define LED_RED_OFF	 		LED_RED_PIN = 1			  // P07 LED RED
	#define LED_RED_ON	 		LED_RED_PIN = 0	  
	#define LED_RED_FLIP		LED_RED_PIN ^= 1		  // P07 LED RED
	
	#define LED_GRN_PIN	 		p0_5
	#define LED_GRN_DIR_PIN		pd0_5
	#define LED_GRN_OFF			LED_GRN_PIN = 1		  // P06 LED GREEN
	#define LED_GRN_ON			LED_GRN_PIN = 0		  
	#define LED_GRN_FLIP		LED_GRN_PIN ^= 1		  // P06 LED GREEN

	#define RELE_PIN	 		p1_3
	#define RELE_DIR_PIN		pd1_3
	#define RELE_OFF			RELE_PIN = 1			  // P05 rele
	#define RELE_ON				RELE_PIN = 0 


	#define DATA0				p1_4				  // DATA0
	#define DATA1				p1_1				  // DATA1
	#define DATA0_DIR			pd1_4 				  // DATA0
	#define DATA1_DIR			pd1_1				  // DATA1

//	#define DATA0_DRIVE_HI		p1drr4 = 1
//	#define DATA1_DRIVE_HI		p1drr1 = 1

	#define M1_LED_DIR_PIN		pd2_2	
	#define M1_LED_PIN			p2_2	

	#define M2_TR_PIN			p0_1
	#define M2_TR_DIR_PIN		pd0_1

	#define M3_DIR_PIN			pd0_0				
	#define M3_PIN				p0_0	

	#define	SYN_PIN				p1_0
	#define	SYN_PIN_DIR			pd1_0
	
	#define IN1_PIN				p2_0	// drat LED
	#define IN1_DIR_PIN			pd2_0	

	#define IN2_PIN				p3_3	// drat BUZ
	#define IN2_DIR_PIN			pd3_3	

	#define WDT_PIN				p0_2	// externi WDT
	#define WDT_DIR_PIN			pd0_2	
		
	#define BUZZER_PIN	 		p0_4
	#define BUZZER_DIR_PIN		pd0_4
	#define BUZZER_ON    		BUZZER_PIN = 0
	#define BUZZER_OFF   		BUZZER_PIN = 1 					// buzzer 
//	#define BUZZER_PIN_IS_ON 	(BUZZER_PIN == 0)

	// Indikacni LED  - indikace chyboveho stavu
	#define LED_ERROR_PIN	 
	#define LED_ERROR_DIR_PIN	
	#define LED_ERROR_OFF	 	LED_RED_OFF
	#define LED_ERROR_ON	 	LED_RED_ON	  
	#define LED_ERROR_FLIP		LED_RED_FLIP



 // definice pro variantu MIFARE reader
  #ifdef READER_MIFARE
	#define	SPI_CLK_PIN			p3_5	
	#define	SPI_CLK_DIR_PIN	 	pd3_5	

	#define	SPI_MOSI_PIN		p3_7	
	#define	SPI_MOSI_DIR_PIN	pd3_7	

	#define	SPI_MISO_PIN		p3_4	
	#define	SPI_MISO_DIR_PIN	pd3_4	

	#define	SPI_CS_PIN			p3_1	
	#define	SPI_CS_DIR_PIN		pd3_1	

	#define RC500_RST_DIR_PIN 	pd2_1            
	#define RC500_RST_PIN      	p2_1             
 
 // definice pro variantu EMMARIN reader
  #endif

  #ifdef READER_EMMARIN
	#define	EM_CTRL_PIN			p3_1	
	#define	EM_CTRL_DIR_PIN	 	pd3_1	

	#define	EM_DATA_PIN		 	p3_4	
	#define	EM_DATA_DIR_PIN		pd3_4	

	#define	EM_CLK_PIN		 	p2_1
	#define	EM_CLK_DIR_PIN		pd2_1	

	#define EMDATA (p3.byte &0x10)   	// p34 nutno definovat cteni pinu takto
  #endif

	#define OW_DIR_PIN			M3_DIR_PIN
	#define OW_PIN				M3_PIN
#endif	  
	  

// GR_EXT8
// ---------------------
#ifdef GR_EXT8


//----------------------------------------------------------------
	// nemam cas vse v kodu odstranovat, definuji jako DUMY MACRO
	#define LED_RED_PIN	 		
	#define LED_RED_DIR_PIN		
	#define LED_RED_OFF	 
	#define LED_RED_ON	
	#define LED_RED_FLIP	
	#define LED_GRN_PIN	 	
	#define LED_GRN_DIR_PIN	
	#define LED_GRN_OFF		
	#define LED_GRN_ON			  
	#define LED_GRN_FLIP	
	#define RELE_PIN	 	
	#define RELE_DIR_PIN		
	#define RELE_OFF		
	#define RELE_ON	
	#define DATA0			
	#define DATA1		
	#define DATA0_DIR		
	#define DATA1_DIR		
	#define M1_LED_DIR_PIN			
	#define M1_LED_PIN				
	#define M2_TR_PIN			
	#define M2_TR_DIR_PIN		
	#define M3_DIR_PIN							
	#define M3_PIN					
	#define	SYN_PIN				
	#define	SYN_PIN_DIR			
	#define BUZZER_PIN	 		
	#define BUZZER_DIR_PIN		
	#define BUZZER_ON    		
	#define BUZZER_OFF   		
	#define LED_ERROR_PIN	 
	#define LED_ERROR_DIR_PIN	
	#define LED_ERROR_OFF	 	
	#define LED_ERROR_ON	 		  
	#define LED_ERROR_FLIP		
// end ---------------------------------------	
	
	
	
#define PIN_INPUT1		(p2_0)? 0:1
#define PIN_INPUT2		(p3_3)? 0:1
#define PIN_INPUT3		(p0_0)? 0:1
#define PIN_INPUT4		(p0_1)? 0:1

#define ADR1_2_PIN		 p0_3
#define ADR1_2_DIR_PIN	 pd0_3	

#define ADR1_PIN		 p0_4
#define ADR1_DIR_PIN	 pd0_4	

#define ADR2_PIN		 p0_5
#define ADR2_DIR_PIN	 pd0_5	

#define ADR2_PIN		 p0_5
#define ADR2_DIR_PIN	 pd0_5	

//#define L2_PIN			p0_6
//#define L2_DIR_PIN		pd0_6

#define L1_PIN			p0_7
#define L1_DIR_PIN		pd0_7

#define MUX_CH1						ADR1_2_PIN=1; ADR1_PIN=0;ADR2_PIN=0 
#define MUX_CH2						ADR1_2_PIN=1; ADR1_PIN=0;ADR2_PIN=1 
#define MUX_CH3						ADR1_2_PIN=1; ADR1_PIN=1;ADR2_PIN=0 
#define MUX_CH4						ADR1_2_PIN=1; ADR1_PIN=1;ADR2_PIN=1 
// zatím nepoužívat
/*#define MUX_CH5					ADR1_2_PIN=0; ADR1_PIN=0;ADR2_PIN=0 
#define MUX_CH6						ADR1_2_PIN=0; ADR1_PIN=0;ADR2_PIN=1 
#define MUX_CH7						ADR1_2_PIN=0; ADR1_PIN=1;ADR2_PIN=0 
#define MUX_CH8						ADR1_2_PIN=0; ADR1_PIN=1;ADR2_PIN=1 
*/



	#define IN1_PIN				p2_0
	#define IN1_DIR_PIN			pd2_0	

	#define IN2_PIN				p3_3	
	#define IN2_DIR_PIN			pd3_3	

	#define IN3_PIN				p0_0	
	#define IN3_DIR_PIN			pd0_0	

	#define IN4_PIN				p0_1
	#define IN4_DIR_PIN			pd0_1
	
	#define WDT_PIN				p0_2	// externi WDT
	#define WDT_DIR_PIN			pd0_2	

#endif
	 
	 
	  
#endif





