/***********************************************************************/
/*                                                                     */
/*  FILE        :hwinit.c                                              */
/*  DATE        :Mon, Feb 01, 2010                                     */
/*  DESCRIPTION :Hardware Setup file                                   */
/*  CPU TYPE    :Other                                                 */
/*                                                                     */
/*  This file is generated by KPIT GNU Project Generator.              */
/*                                                                     */
/***********************************************************************/

#include "iodefine.h"
#include "macros.h"

extern void HardwareSetup(void);

void HardwareSetup(void)
{
//---------------------------< Nastaveni hodin >---------------------------

  WDT.WRITE.WTCSR = 0xa51e;		   // WDT stop, WDT count clock setting 1/4096 x P-clock (49,152; 21,3msec)
	WDT.WRITE.WTCNT = 0x5a85;		     // Counter initial setting 10msec 
  CPG.ACLKCR.BIT.ASDIVS = 0x03;    // AD    =  49.152MHz 
  CPG.FRQCR.WORD = 0x0303;         // B(fi)1/4 = 49.152MHz;I(fi)1/1 = 196,608MHz; P(fi)1/4 = 49,152MHz
  CPG.MCLKCR.BYTE = 0x43;          // Hodiny pro MTU2S/4 = 49.512MHz	
	
	asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); // 32x nop (musí být)
	asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop");
	asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop");
	asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop");
	asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop");                  
 	asm("nop"); asm("nop");

//------< Enable module clock >-----
  STB.CR2.BYTE = 0x00;  
  STB.CR3.BYTE = 0x02;        		 // HIZ,MTU2S,MTU2,POE2,IIC3,ADC0,Reserve(1),FLASH 
  STB.CR4.BYTE = 0xF3;             // Reserve(1),Reserve(1),Reserve(1),SCIF3,Reserve(0),CMT,Reserve(1),EtherC
  STB.CR5.BYTE = 0x12;             // Module Standby Clear 	
	
	
	
	
//-------------------------------< PFC >----------------------------------
//-------< PORTA >------
  PFC.PACRH2.WORD = 0x0006;
  PFC.PACRH1.WORD = 0x6100;
  PFC.PACRL4.WORD = 0x0007;	
  PFC.PACRL3.WORD = 0x7777; 
  PFC.PACRL2.WORD = 0x7700;
  PFC.PACRL1.WORD = 0x0000;
  PFC.PAIORH.WORD = 0x003f;  // I/O
  PFC.PAIORL.WORD = 0xffc1;  // I/O

//-------< PORTB >------
  PFC.PBCRL4.WORD = 0x0066;	// IIC
  PFC.PBCRL3.WORD = 0x0000; 
  PFC.PBCRL2.WORD = 0x0000;
  PFC.PBCRL1.WORD = 0x4000;
  PFC.PBIORL.WORD = 0x8f37;  // I/O
  PB.DR.BIT.B9 = 1;  // zakazu zapis do eeprom
//-------< PORTC >------
  PFC.PCCRL4.WORD = 0x0000;	
  PFC.PCCRL3.WORD = 0x0000; 
  PFC.PCCRL2.WORD = 0x0000;
  PFC.PCCRL1.WORD = 0x0000;
  PFC.PCIORL.WORD = 0x0000;  // I/O
  
//-------< PORTD >------
  PFC.PDCRH2.WORD = 0x7777;
  PFC.PDCRH1.WORD = 0x7777;
  PFC.PDCRH2.WORD = 0x7007;
  PFC.PDCRH1.WORD = 0x7000;
  PFC.PDCRL4.WORD = 0x0000;	
  PFC.PDCRL3.WORD = 0x0000; 
  PFC.PDCRL2.WORD = 0x0000;
  PFC.PDCRL1.WORD = 0x0000;
  PFC.PDIORH.WORD = 0xffff;  // I/O
  PFC.PDIORL.WORD = 0xffff;  // I/O
	
//-------< PORTE >------
  PFC.PECRL3.WORD = 0x0000; 
  PFC.PECRL2.WORD = 0x0000;
  PFC.PECRL1.WORD = 0x0000;
	PFC.PECRL4.WORD = 0x4000;	 // 0x4000
//	PFC.PECRL4.WORD = 0x0400;
  PFC.PEIORL.WORD = 0xf7ff;  // I/O
  
//-------< PORTF >------
// Port F je A/D prevodnik (Nenastavuje se)	

//----------------------< Nastaveni preruseni >-------------------------
  INTC.ICR0.BIT.NMIL = 0;    // 0 aby nenastal Nonmaskable interrupt
  INTC.IRQRR.WORD = 0;       // Smazu priznaky ext. interrupts
  INTC.IPR01.WORD = 0;       // Nastavim nizkou prioritu pro ext.interrupts
  INTC.IPR02.WORD = 0;       // Nastavim nizkou prioritu pro ext.interrupts
  INTC.IPR08.BIT._CMT0 = 0x0d;    // cmt0
  INTC.IPR08.BIT._CMT1 = 0x01;    // cmt1
	INTC.IPR05.BIT._AD0  = 0x0c;    // AD0
//	INTC.IPR08.BIT._WDT  = 0x0f;    // WDT
	INTC.IPR09.BIT._MTU20G = 0x0b;  //TIOC0 (mereni frekvence

//------------------------< Nastaveni Per. >----------------------------

//---< Nastaveni A/D prevodniku >---
  //-< A/D0 >-
	ADC1.ADCR.BIT.ADST = 0;   // Zastavim prevod A/D1 (Prevod se pousti s periodou urcujici MTU2S) 
  ADC0.ADCR.BIT.ADST = 0;   // Zastavim prevod A/D0 
  ADC0.ADCR.BIT.TRGE = 1;   // Povolim ext. start A/D0
  ADC0.ADSTRGR.BIT.STR5 = 1;// start pomoci MTU2S
  ADC0.ADCR.BIT.ADCS = 0;   // Single scan mode
  ADC0.ADCR.BIT.ACE = 0;    // Clear ADDR after read A/D0
  ADC0.ADANSR.BYTE = 0x0f;  // AN0 to AN3 as analog in
  ADC0.ADCR.BIT.ADIE = 1;   // Povolim preruseni od A/D0
  //-< A/D1 >-
  ADC1.ADCR.BIT.TRGE = 1;   // Povolim ext. start A/D0
  ADC1.ADSTRGR.BIT.STR5 = 1;// start pomoci MTU2S
  ADC1.ADCR.BIT.ADCS = 0;   // Single scan mode
  ADC1.ADCR.BIT.ACE = 1;    // Clear ADDR after read A/D0
  ADC1.ADANSR.BYTE = 0x0f;  // AN4 to AN7 as analog in

//---< Nastaveni Timeru0 >---
  CMT.CMSTR.BIT.STR0 = 0;
  CMT0.CMCSR.BIT.CKS  = 0;  // P(fi)/32 = 1,536MHz
  CMT0.CMCSR.BIT.CMF = 0;   // Smazu priznak preruseni
  CMT0.CMCSR.BIT.CMIE = 1;  // Povolim preruseni od timeru
	CMT0.CMCOR = 61440;       // Zapisu konstantu pro 10mS
  CMT.CMSTR.BIT.STR0 = 1;   // Pustim Timer0

//---< Nastaveni Timeru 1 >---
  CMT.CMSTR.BIT.STR1 = 0;
  CMT1.CMCSR.BIT.CMF = 0;   // Smazu priznak preruseni
	CMT1.CMCOR = 1;           // Zapisu konstantu pro 10mS
  CMT1.CMCSR.BIT.CMIE = 1;  // Povolim preruseni od timeru

//---< Nastaveni MTU2S pro spousteni A/D >---
	MTU2S.TSTR.BIT.CST4 = 0;
  MTU2S4.TCR.BYTE = 0xc0;
	MTU2S4.TGRD = 7680;
	MTU2S4.TADCORA = 7680;
	MTU2S4.TIER.BIT.TTGE = 1;   // A/D Converter Start Request Enable 
  MTU2S4.TADCR.BIT.UT4AE = 1; // Preruseni pri citani up
	MTU2S.TSTR.BIT.CST4 = 0;


//---< Nastaveni PWM pro podsviceni >---
	MTU2.TSTR.BIT.CST4 = 0;
	MTU2.TOER.BIT.OE4D = 1;
	MTU24.TMDR.BYTE = 0x00;
  MTU24.TCR.BYTE = 0xc0;
  MTU24.TIOR.BYTE.L = 0x70;
	MTU24.TGRD = 49152;
	MTU2.TSTR.BIT.CST4 = 1;

//---------------------------------------	
/*
	MTU2.TSTR.BIT.CST4 = 0;     // Stop PWM
  MTU24.TCR.BYTE = 0x60;      // TCNT kdyz TGRD, citat s nabeznou hranou, bez delicky(posledni # bity)
	MTU24.TIOR.BYTE.L = 0x03;   // Change toggle initial out 1
	TGRC = 12000;               // Nabezna hrana
	TGRD = 15360;               // Sestupna hrana
	MTU24.TMDR.BIT.MD = 2;      // Mode PWM1
	MTU2.TSTR.BIT.CST4 = 1;     // Start PWM 
*/
	
	
//---< Nastaveni mereni frekvence (Input capture TI0C0C)>---
  MTU20.TCR.BYTE = 0x02;    //P(fi)/16
	MTU20.TMDR.BYTE = 0x00;   
	MTU20.TIOR.BIT.IOC = 9;   // Citani s nabeznou hranou
	MTU20.TIER.BIT.TGIEC = 1; // Povolim preruseni
	MTU20.TSR.BIT.TGFC = 0;   // smazu priznak preruseni
	MTU20.TCNT = 0;
	MTU20.TGRC = 0;
	MTU2.TSTR.BIT.CST0 = 1;   //
	
//---< Nastaveni klidovych urovni pro Epson >---
	PD.DR.BIT.B16 = 1;     // LCD reset
	asm("nop");
	PA.DR.BIT.B0 = 1;      // LCD cs
	PB.DR.BIT.B0 = 1;      // LCD wr
	PA.DR.BIT.B17 = 1;     // LCD rd
	PA.DR.BIT.B13 = 1;     // LCD dc
	PD.DR.WORD.L = 0x0000; // LCD io

//---------------< IIC init >-------------------
  IIC3.ICCR1.BIT.ICE = 0x01;      // IIC3 transfer enabled 
  IIC3.ICCR1.BIT.CKS = 0x0f;      // Transfer clock P(fi)/496 => 99kHz 
  IIC3.ICCR1.BIT.MST = 1;         // Nastavim do master mode 
  IIC3.ICMR.BYTE = 0x30;
}


