
#include "opora.h"

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

int CurrentLed=0, Temp=0;
int main()
{
	ClkConfig();
	PortConfig();
	SysTickInit();

	while(1)
	{
		if((SysTick->CTRL&0x10000)==0x10000)
		{
			PORTD->RXTX=0;
			PORTD->RXTX=1<<(7+CurrentLed++);
			if(CurrentLed>7) CurrentLed=0;	
		}
	}
}

//--- 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=0x02;					//CPU_C1 = HSE (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 = HSE (25MHz)
	RST_CLK->PER_CLOCK |= 1<<24; 				//clock of PORTD ON
}

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

//--- System Timer initialization ---
void SysTickInit()
{
	SysTick->LOAD = 0x00249EFF;	//Pause 100 ms (HCLK = 25MHz)
	SysTick->CTRL = 0x00000001;	//Enabel SysTick
}

