
#include "opora.h"

#define REVISION_2

void ClkConfig(void);
void PortConfig(void);

int Current_Led;
uint32_t i;
int main()
{
	ClkConfig();
	PortConfig();
	Current_Led=0;
	while(1)
	{
		//PORTD->RXTX^=1<<(7+Current_Led++);
		PORTB->RXTX^=1<<(Current_Led++);
		if(Current_Led>7) Current_Led=0;
		for(i=0;i<100000;i++); 					//Pause ~88 ms
	}
}

//--- Clock configuration ---
void ClkConfig()
{
#ifdef REVISION_2
	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=0x00000002;				//CPU_C1 = 8MHz
	RST_CLK->PLL_CONTROL=(1<<2)|(2<<8);			//CPU PLL On, PLL_MULL=2
	while((RST_CLK->CLOCK_STATUS&0x02)!=0x02);
	RST_CLK->PER_CLOCK|=0x08;					//EEPROM_CTRL Clock enable
	EEPROM->CMD=0;
	RST_CLK->PER_CLOCK&=(~0x08);				//EEPROM_CTRL Clock disable
	RST_CLK->CPU_CLOCK = 0x00000106;			//CPU Clock = 3*8MHz
#else
	RST_CLK->HS_CONTROL = 0x00000003;			//HSE - On; Generator mode On
	while((RST_CLK->CLOCK_STATUS&0x04)!=0x04);	//Wait until HSE not ready
	RST_CLK->PER_CLOCK|=0x08;					//EEPROM_CTRL Clock enable
	EEPROM->CMD=0;
	RST_CLK->PER_CLOCK&=(~0x08);				//EEPROM_CTRL Clock disable
	RST_CLK->CPU_CLOCK = 0x00000102;			//CPU Clock = HSE (25MHz)
#endif	//REVISION_2
	RST_CLK->PER_CLOCK|=1<<22/*24*/;	 				//clock of PORTD ON
}

//--- Ports configuration ---
void PortConfig()
{
	PORTB->FUNC = 0x00000000;
	PORTB->RXTX = 0x0000;
	PORTB->OE = 0x00FF;
	PORTB->ANALOG = 0x00FF;
	PORTB->PWR = 0x0000FFFF;
/*
	PORTD->FUNC = 0x00000000;
	PORTD->RXTX = 0x0000;
	PORTD->OE = 0x7F80;
	PORTD->ANALOG = 0x7F80;
	PORTD->PWR = 0x3FFFC000;
	*/
}
