
#ifndef __CONFIG_C
#define __CONFIG_C

#include "opora.h"
#include "config.h"

//--- Clock configuration ---
void ClkConfig()
{
	RST_CLK->HS_CONTROL=0x00000001;					//HSE - On; Osc mode On
	while((RST_CLK->CLOCK_STATUS&0x04)!=0x04);		//Wait until HSE not ready

	RST_CLK->CPU_CLOCK=0x00000102;					//CPU Clock = 8 MHz

	RST_CLK->PLL_CONTROL=(PLL_MULL<<8)|(1<<2);		//PLL CPU On;
	while((RST_CLK->CLOCK_STATUS&0x02)!=0x02);		//wait until PLL CPU not ready
	RST_CLK->PER_CLOCK|=0x08;						//EEPROM_CTRL Clock enable
#if PLL_MULL<3										//Freqency < 25MHz
	EEPROM->CMD=0;									//Delay = 0
#elif (PLL_MULL>2)&&(PLL_MULL<6)					//25 <= Freqency < 50MHz
	EEPROM->CMD=1<<3;								//Delay = 1
#elif (PLL_MULL>5)&&(PLL_MULL<9)					//50 <= Freqency < 75MHz
	EEPROM->CMD=2<<3;								//Delay = 2	 
#elif (PLL_MULL>8)&&(PLL_MULL<12)					//75 <= Freqency < 100MHz
	EEPROM->CMD=3<<3;								//Delay = 3
#elif (PLL_MULL>11)&&(PLL_MULL<14)					//100 <= Freqency < 125MHz
	EEPROM->CMD=4<<3;								//Delay = 4
#else
	EEPROM->CMD=7<<3;								//Delay = 7	
#endif	//PLL_MULL<3
	RST_CLK->PER_CLOCK&=(~0x08);					//EEPROM_CTRL Clock disable

	RST_CLK->CPU_CLOCK=0x00000106;					//CPU Clock = 8*(PLL_MULL+1) MHz = XX MHz

	RST_CLK->PER_CLOCK|=1<<29;					//clock of PORTD, PORTC On
	RST_CLK->PER_CLOCK|=1<<10;						//clock of MIL-STD-1553_2 On
	RST_CLK->ETH_CLOCK=1<<25;						//clock of MIL-STD-1553_1 Enable

//	RST_CLK->PER_CLOCK|=0x03<<23;					//clock of PORTD, PORTC On
//	RST_CLK->PER_CLOCK|=1<<9;						//clock of MIL-STD-1553_1 On
//	RST_CLK->ETH_CLOCK=1<<25;						//clock of MIL-STD-1553_1 Enable
}

//--- Ports configuration ---
void PortConfig()
{
	//*** Config for MIL-STD-1553 ***
	/*
	//PORTC 13, 14, 15 - MIL-STD-1553_1 functions
	PORTC->FUNC = 0x54000000;
	PORTC->ANALOG = 0xE000;
	//PORTC->PWR = 0x54000000;	//slow edge
	//PORTC->PWR = 0xA8000000;
	PORTC->PWR = 0xFC000000;	//fast edge
	//*** Config PortD for Leds and MIL-STD-1553_1 ***
	PORTD->FUNC = 0x00001555;
	PORTD->OE = 0x7F80;
	PORTD->ANALOG = 0x7FFF;
	//PORTD->PWR = 0x15555555;	//slow edge
	//PORTD->PWR = 0x2AAAAAAA;
	PORTD->PWR = 0x3FFFFFFF;	//fast edge
	*/
	PORTF->FUNC=0x55555555;
	PORTF->ANALOG = 0xFFFF;
	PORTF->PWR = 0xFFFFFFFF;	//fast edge


}

//--- MIL-STD-1553 initialization function ---
void MilStd1553Init()
{
	MIL_STD_15531->CONTROL = 0;									//MIL-STD-1553 work mode
	MIL_STD_15531->CONTROL = 0x0038|(___DIV<<11)|(MYADDR<<6);	//находятся в файле milstd_cfg.h
	MIL_STD_15531->StatusWord1 = MYADDR<<11;
	MIL_STD_15531->ModeData = 0x000055AA;
	MIL_STD_15531->INTEN = 0x0000000E;
}

void MilStd1553DataInit()
{
	unsigned int i;
	unsigned int* TxBuffer=(unsigned int*)MIL_STD_1553_TX_BUFFER;

	for(i=0;i<1024;i++)	*TxBuffer++ = i;	
}

#endif	//__CONFIG_C
