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

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

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

	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 Clock enable
	EEPROM->CMD=3<<3;								//Delay = 3	 
		 
	RST_CLK->CPU_CLOCK=0x00000106;					//CPU Clock = 10*8MHz = 80 MHz

	RST_CLK->PER_CLOCK|=(5<<22)|(1<<29);			//clock of PORTD, PORTB, PORTF On

	RST_CLK->PER_CLOCK|=5<<26;						//clock of ARINC429R and ARINC429T On
}

//--- Ports configuration ---
void PortConfig()
{
	//*** Config PORTB_0 - PORTB_7 for ARINC429R ***
	//PORTC 13, 14, 15 - MIL-STD-1553_1 functions
	PORTB->FUNC = 0x0000AAAA;	//PORTB_0 - PORTB_7 alternativ function
	PORTB->ANALOG = 0x00FF;		//PORTB_0 - PORTB_7	digital
	PORTB->PWR = 0x0000FFFF;	//PORTB_0 - PORTB_7	has max edge

	//*** Config PortD_13, PortD_14, PORTF_13, PORTF_14 for ARINC429T ***
	PORTD->FUNC = 0x28000000;	//PortD_13, PortD_14 alternativ function
	PORTD->ANALOG = 0x6000;		//PortD_13, PortD_14 digital
	PORTD->PWR = 0x3C000000;	//PortD_13, PortD_14 has max edge

	PORTF->FUNC = 0x14000000;	//PortF_13, PortF_14 general function	
	PORTF->ANALOG = 0x6000;		//PortF_13, PortF_14 digital
	PORTF->PWR = 0x3C000000;	//PortF_13, PortF_14 has max edge


	PORTD->ANALOG|=1<<7;
	PORTD->OE|=1<<7;
	PORTD->PWR|=3<<15;
}

//--- ARINC429 Tx configuration ---
void Arinc429TxConfig(void)
{
	ARINC429T->CONTROL1=0; 
	ARINC429T->CONTROL2=0;
#if LOWSPEED==1 		
	ARINC429T->CONTROL1|=(DIV_ARINC<<8)|(1<<7)|(1<<6)|(1<<5)|(1<<4)|(1<<3)|(1<<2)|(1<<1)|1;	//Задаем делитель, паритет (дополнение до нечётного) для 1 и 2 каналов, передача бита паритета разрешена для 1 и 2 каналов, передача по каналу 1 и 2 разрешена, 
#else
	ARINC429T->CONTROL1|=(DIV_ARINC<<8)|(1<<7)|(1<<6)|(1<<4)|(1<<3)|(1<<2)|1;	//Задаем делитель, паритет (дополнение до нечётного) для 1 и 2 каналов, передача бита паритета разрешена для 1 и 2 каналов, передача по каналу 1 и 2 разрешена, 
#endif	//LOWSPEED==1
}

void Arinc429RxConfig(void)
{
	ARINC429R->CONTROL1=0;
	ARINC429R->CONTROL2=DIV_ARINC>>4;
	ARINC429R->CONTROL3=0;
	ARINC429R->CONTROL3|=(1<<28)|(1<<29);
#if LOWSPEED==1
	ARINC429R->CONTROL1|=(DIV_ARINC<<28)|(0xFF<<14)|0x0F;
#else
	ARINC429R->CONTROL1|=(DIV_ARINC<<28)|0x0F;
#endif	//LOWSPEED==1	
}

//--- System Timer configuration ---
void SysTickInit()
{
	SysTick->LOAD = 0x007A1200;	//Pause 100 ms (HCLK = 80MHz)
	SysTick->CTRL = 0x00000003;	//Enabel SysTick, Enable Interrupt
}


