
#ifndef __CONFIG_C
#define __CONFIG_C

#include "../inc/OPORA/opora.h"
#include "../inc/CONFIG/config.h"

//--- Clock configuration ---
void CoreClockConfig()
{
	RST_CLK->HS_CONTROL = 0x00000001;			//HSE - On; Osc mode On
	while((RST_CLK->CLOCK_STATUS&RST_CLK_CLOCK_STATUS_HSE_RDY)==0);	//Wait until HSE not ready
	RST_CLK->PER_CLOCK|=0x08;					//EEPROM Clock enable
	EEPROM->CMD=0;
	RST_CLK->CPU_CLOCK=0x00000102;				//CPU_Clock=HSE (8MHz)
}

void PortsConfig()
{
	RST_CLK->PER_CLOCK|=(0x1F<<21)|(1<<29);	//Enable clock of PORTA, PORTB, PORTC, PORTD, PORTE, PORTF

	//Config Data Bus
	PORTA->FUNC=0x55555555;			//PORTA - DATA bus
	PORTA->ANALOG=0xFFFF;			//PORTA - digital
	PORTA->PWR=0xFFFFFFFF;			//PORTA - fast speed

	PORTB->FUNC=0x55555555;			//PORTB - DATA bus
	PORTB->ANALOG=0xFFFF;			//PORTB - digital
	PORTB->PWR=0xFFFFFFFF;			//PORTB - fast speed

	//Config ~OE, ~WE
	PORTC->FUNC=0x00000005;			//PORTC[1..0] - ~RD(~OE), ~WR(~WE)
	PORTC->ANALOG=0x0003;			//PORTC[1..0] - digital
	PORTC->PWR=0x0000000F;			//PORTC - fast speed

	//Config Address Bus and CS
	PORTF->FUNC=0xAAAAA800;			//PORTF[15..5] - ADDRESS bus
	PORTF->ANALOG=0xFFE0;			//PORTF[15..5] - digital
	PORTF->PWR=0xFFFFFC00;			//PORTF - fast speed

	PORTD->FUNC=0x80000000;			//PORTD_15 - ADDRESS[13]
	PORTD->ANALOG=0x8000;			//PORTD_15 - digital
	PORTD->PWR=0xC0000000;			//PORTD - fast speed

	PORTE->FUNC=0x00000AAA;			//PORTE[5..0] - ADDRESS[19..14], PORTE_8 - ~CS, 
	PORTE->RXTX=0x0100;				//PORTE_8=1 (~CS=1)
	PORTE->OE=0x0100;				//PORTE_8 - Output
	PORTE->ANALOG=0x013F;			//PORTE[5..0] - digital, PORTE_8 - digital
	PORTE->PWR=0x00030FFF;			//PORTE - fast speed

	PORTE->CLRTX=0x100;

	//Config LEDS
	PORTD->RXTX=0;
	PORTD->OE|=0x7F80;
	PORTD->ANALOG|=0x7F80;			//PORTD[14..7] - digital
	PORTD->PWR|=0x3FFFC000;			//PORTD - fast speed
}

void ExternalBusConfig()
{
	RST_CLK->PER_CLOCK|=1<<30;				//Enable clock of External Bus
	EXT_BUS_CNTRL->EXT_BUS_CONTROL=0x0001;	//ROM enable
}

void SysTickConfig()
{
	SysTick->LOAD=0x007A1200;
	SysTick->CTRL=0x00000003;
}



#endif	//__CONFIG_C
