/***********************************************************************/
/*                                                                     */
/*  FILE        :hwinit.c                                              */
/*  DATE        :Thu, May 28, 2009                                     */
/*  DESCRIPTION :hardware initialization file.                         */
/*  CPU GROUP   :R8C/33A(ROM32K)                                            */
/*                                                                     */
/*                                                                     */
/***********************************************************************/

#include "includes.h"



/************************************************************************************
Name:			mcu_init
Parameters:		None
Returns:		None
Description:	MCU initialize
************************************************************************************/
//__attribute__ ((section (".page0f1"))) 
__attribute__ ((section (".hwinit"))) 
void hw_initialise (void) 
{
    volatile unsigned char osc_stab = 128;

    prc0 = 1;     	  	// switch protection off
	
    cm14 = 0;      	 	// select low-speed on-chip oscillator
    cm13 = 1;      	 	// use P4.6/4.7 as XIN/XOUT-pins
    cm05 = 0;      	 	// XIN on
    
    cm16 = 0;       	// select main clock = no division mode
    cm17 = 0;
    cm06 = 0;      		// enable cm16/cm17

	while (osc_stab)	// wait a little bit for stable oscillator
	{
		--osc_stab;
	}

    cm07 = 0;       	// select XIN clock
    ocd2 = 0;       	// switch to main-clock

    prc0 = 0;       	// switch protection on again

}




/************************************************************************************
Name:			Init_ADC
Description:	ADC initialize
************************************************************************************/
// adc v rezimu EDK pouze nacita JUMPERY po startu, preruseni se negeneruje
__attribute__ ((section (".page0f2"))) 
void Init_ADC(tUChar port, tUChar mode, tUChar sweep, tUChar adint)
{
  unsigned char i;                        
	
  adic.byte = INT_DISABLED;		// disable interrupt, preruseni od ADC nebude pouzito
  adcon0.byte = 0x00;			// Stop A/D conversion

  // Cut off on-chip reference voltage and analog input 
  prc3 = 1;						// Protect off 
  ocvrefcr.byte = 0;
  prc3 = 0;    					// Protect on 


  // PORT MUX	
  adinsel.byte = 0;
  adinsel.byte = (adinsel.byte & 0x3F) | (port<<6);		// prepnuti na port P0/P1


 // CLOCK
  cks0 = 1;
  cks1 = 1;						// fAD/1
  cks2 = 0; 					// clock source f1

  
 // ADC mode  
  admod.byte = (admod.byte & 0xCF) | (mode<<3);		// nastaveni mode bitu md0/md1/md2


 // SWEEP
  if ((mode == ADC_MODE_SINGLE_SWEEP) || (mode == ADC_MODE_REPEAT_SWEEP))
  {
    adinsel.byte = (adinsel.byte & 0xCF) | (sweep<<4);		// nastaveni sweep bitu scan0/1
  }
   

  // When the CKS2 bit is changed, wait for min.3AD cycles before starting A/D conversion 
  for(i = 0; i < 10; i++) {};

  adcon1.byte = 0x30;   		// 10-bit mode, Enable A/D operation 
  for(i = 0; i < 10; i++){};	// When the ADSTBY bit is changed wait for 1AD cycle 

 if (adint)
  {
    adic.byte = INT_LEVEL_ADC;  // Enable A/D converter interrupt (level 1)
  }

  adst = 1;			 			// Start A/D conversion
}

 


/************************************************************************************
Name:			Init_GPIO
				inicializace pinu procesoru pro variantu EDK
************************************************************************************/
#ifndef GR_EXT8
void InitGPIO(void)
{
//***************************************************
//  inicializace pinu procesoru pro variantu EDK
#ifdef CORE_EDK
    prc2 = 1;				// pro zapis do Pd0 je nutne vypnout ochranu pro zazdy zapis!
	LED_RED_DIR_PIN = 1;	 

	prc2 = 1;
	LED_GRN_DIR_PIN  = 1;

	prc2 = 1;
	RELE_DIR_PIN = 1;	

	prc2 = 1;
	BUZZER_DIR_PIN = 1;
	
	prc2 = 1;
#if !defined DEBUG
	WDT_DIR_PIN = 1;
#endif	
	prc2 = 0;				// zamknout PRCR bit prc2	


// UART piny
 #ifdef INTERFACE_RS
    pd1_6 = 1; 	 // pin DE RS485
    pd1_4 = 1; 	 // pin TXD RS485
 #endif
 
//    pu00 = 0;		// na pinech p0_0 - p0_3 neni interni pull-up
    //pu01 = 0;		// na pinech p0_4 - p0_7 neni interni pull-up
	//pd0.byte = 0;	// port0 je cely vstupni
	p0.byte = 0;


// inicializace LED vystupu
	LED_RED_OFF;
	LED_GRN_OFF;	
	RELE_OFF;
    BUZZER_OFF;


// inicializace pinu pro cteci obvod EMMARIN EM4095
 #ifdef READER_EMMARIN
	EM_CTRL_PIN = 1;		// 1= SHUTDOWN
	EM_CTRL_DIR_PIN = 1;	// vystupni pin, povoleni/blokovani EM4095

	EM_DATA_PIN = 0;		 		
	EM_DATA_DIR_PIN = 0;	// vstupni pin, data z EM4095

	EM_CLK_PIN = 1;
	EM_CLK_DIR_PIN = 0;		// vstupni hodinovy signal = frekvence 125kHz
 #endif


// inicializace pinu pro cteci obvod MIFARE CLRC632
 #ifdef READER_MIFARE
	RC500_RST_DIR_PIN = 1;	// RESET RF

	SPI_CS_DIR_PIN = 1;		// CS RF
	SPI_CS_PIN = 1;	

	SPI_MOSI_DIR_PIN = 1;	
	SPI_MOSI_PIN = 0;	

	SPI_CLK_DIR_PIN = 1;	
	SPI_CLK_PIN = 0;	
 #endif


// inicializace pinu vstupy IN1+IN2
	IN1_PIN = 1;
	IN1_DIR_PIN	= 0;			// default oba vstupy IN1 a IN2 jsou vstupni

	IN2_PIN = 1;
	IN2_DIR_PIN = 0;

	M1_LED_PIN = 0;				// LED nesviti
	M1_LED_DIR_PIN = 1;			// LED dioda optozavory 

	M2_TR_PIN = 1;				// default stav 
	M2_TR_DIR_PIN = 0;			// vstupni tranzistor optozavory



// pokud je nastaven vystupni format WIEGAND tak je nutne prekonfigurovat datove piny D0/D1
	if (EDK.input.state & EDK_IN_WGOUT)
 	 {
	  DATA0 = 1;				// na vystupnim pinu D0 je invertor
	  DATA0_DIR = 1;

	  DATA1 = 0;
	  DATA1_DIR = 1;
	
	  // ovladani jeste jednou
	  // nevim proc vyse uvedene nastaveni pinu D0 nejede
	  DATA0 = 1;				// na vystupnim pinu D0 je invertor
	  DATA1 = 0;	
	 }
// pouze pro vystupni format ASC - synchronizacni signal generuje terminal
	else	
	 {									// inicializace Keyboard interrupt od synchronizacniho vstupu SYN
	   SYN_PIN_DIR = 0;					// pin SYN je vstup
	   ki0en = 1;						// p10 enable KI interrupt
	   ki0pl = RISING_EDGE_INTERRUPT;	// vychozi stav raising edge interupt
	   kupic.byte = 5;					// interrupt priority
	   ir_kupic = 0;					// clear flag
	 }

#endif
}
#endif


/************************************************************************************
Name:			Init_GPIO
************************************************************************************/
#ifdef GR_EXT8
void InitGPIO(void)
{
 


	prc2 = 1;
#if !defined DEBUG
	WDT_DIR_PIN = 1;
#endif	
	prc2 = 0;				// zamknout PRCR bit prc2	


// UART piny
 #ifdef INTERFACE_RS
    pd1_6 = 1; 	 // pin DE RS485
    pd1_4 = 1; 	 // pin TXD RS485
 #endif
  
	IN1_PIN = 1;
	IN1_DIR_PIN	= 0;	

	IN2_PIN = 1;
	IN2_DIR_PIN = 0;

	IN3_PIN = 1;
	IN3_DIR_PIN	= 0;	

	IN4_PIN = 1;
	IN4_DIR_PIN = 0;
	

 	ADR1_2_PIN = 0;
	ADR1_2_DIR_PIN = 1;			


	ADR1_PIN = 0;
	ADR1_DIR_PIN = 1;			

	ADR2_PIN = 0;			
 	ADR2_DIR_PIN = 1;	


//	L2_PIN	= 1;		
// 	L2_DIR_PIN = 0;					

 	L1_PIN = 1;			
	L1_DIR_PIN = 0;		

}
 
#endif




/************************************************************************************
Name:			uart_init
Parameters:		None
Returns:		None
Description:	UART initialize
************************************************************************************/
void uart_init(void)
{

	#ifdef INTERFACE_EDK
		u0sr.byte = 0x05;		/* P1_5 = RXD0, P1_4 = TXD0 */
	#else
		u0sr.byte = 0x04;		/* P1_5 = RXD0 */
	#endif

	s0tic.byte = INT_DISABLED;	/* UART0 transmit interrupt disabled */
	s0ric.byte = INT_DISABLED;	/* UART0 receive interrupt disabled */
	te_u0c1 = 0;				/* Transmission disabled */
	re_u0c1 = 0;				/* Reception disabled */

	u0mr.byte = 0x05;			/* UART mode transfer data 8 bits long */
								/* 1 stop bit */
								/* Parity disabled */
	u0c0.byte = 0x00;			/* BRG count source :"f1" */
								/* TXD0 pin is for CMOS output */
								/* LSB first */
	u0c1.byte = 0x00;			/* Disables transmission */
								/* Disables reception */
								/* Transmission buffer empty causes UART0 transmit interrupt */
	#ifdef INTERFACE_EDK
		SET_BAUD_9600;
	#else
		SET_BAUD_19200;
	#endif


  	u0tbl = u0rbl;				// clear UART0 receive buffer by reading
  	u0tbl = 0;					// clear UART0 transmit buffer

	#ifdef INTERFACE_EDK
		te_u0c1 = 1;			/* Transmission enabled */
	#endif

	re_u0c1 = 1;				/* Reception enabled */

}

																



/*****************************************************************************************************************
*	I R Q  I n i t  
*
*
*****************************************************************************************************************/
void IRQ_Init(void)
{
	int0en = 0;			// INT0 disable
	int1en = 1;			// povoleni preruseni z INT1
	int3en = 0;			// INT3 disable
	
	int0pl = 1;			// preruseni INT0 na obe hrany
	intf.byte = 0;		// vypnout filtraci 

	int1sel0 = 1;		// INT1 pripojen vnitrne na pin p1.5 RXD
	int1sel1 = 0;		
	int1sel2 = 0;
	
// nastaveni priority pro INT0
	int0ic.byte = INT_DISABLED;		// INT0 OFF
//	int0ic.byte = INT_LEVEL_INT0;	// INT0 priority 

// nastaveni priority pro INT1
	int1ic.byte = INT_LEVEL_INT1;	// INT1 priority 
//	int1ic.byte = INT_DISABLED;

// nastaveni priority pro INT3
	int3ic.byte = INT_DISABLED;		// INT3 OFF
}




/*****************************************************************************************************************
*	SCI_Init
*
*
*****************************************************************************************************************/
void SCI_Init(void)
{
    uart_init();				/* UART initialize */
	BusInit();
	SET_VTXD(0);	
}


//************************************************************************
// ReaderInit
// inicializace cteciho cipu RFID - MIFARE
// v pripade EMMARIN, detekuje se jestli je osazen pro pripad teplomeru
//************************************************************************
void ReaderInit(void)
{
 #if defined READER_MIFARE							// inicilaizace MIFARE
   if ( MifareInitReader() == NO_ERROR)
    {
   	  EDK.hwspec_low |=	HWSPEC_LOW_READER_MIFARE;	// nastavit bit identifikace
    }
   else 											// chyba inicializace MIFARE
   {
      appCB.error |= ERR_READER_INTERFACE;			// zastavit beh programu
      ErrorLed();
   }
#endif

 
 #ifndef INTERFACE_EDK						// vsechny ostatni rozhrani krom EDK
   if (!(EDK.input.state & EDK_IN_EHS))		// v pripade odkladacky nespoustet automaticky cteni karty
    {
      EDK.sys_flag |= FLAG_READ_CARD;		// maji cteni karty automaticky povoleno
	}
 #endif


 // ctecka EMMARIN muze mit neosazen cteci cip, v tom pripade se nesnazit nacitat kartu 				
 #if defined READER_EMMARIN	  				
   if (EmmarinInitReader() == NO_ERROR)					// EMMARIN OK
    {
	  EDK.hwspec_low |=	HWSPEC_LOW_READER_EMMARIN;		// osazen EMMARIN, nastavit bit identifikace
	} 
   else													// cteci cip neosazen, detekovana propojka na DPS
    {
	  EDK.sys_flag &= ~FLAG_READ_CARD;					// cteni karty zakazano
	  EDK.hwspec_low &=	~HWSPEC_LOW_READER_MIFARE;		// neosazen zadny cteci obvod
	  EDK.hwspec_low &=	~HWSPEC_LOW_READER_EMMARIN;		// neosazen zadny cteci obvod
	  EDK.hwspec_low |=	HWSPEC_LOW_TEMPERATURE;			// varianta pouze teplomer
    }
	  
 #endif

}


