
#include "opora.h"

#define UART_1 1	//select UART1 or UART2. If UART_1 = 1, then select UART1, if UART_1 = 0, then select UART2.
#define REVISION_2

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

uint32_t Temp;
int main()
{
	ClkConfig();
	PortConfig();
	UARTConfig();
	while(1)
	{
#if	UART_1 == 1
		while((UART1->FR&(1<<4))==(1<<4));		//wait until Rx empty
		Temp = (uint32_t)UART1->DR + 1;
		PORTD->RXTX = (uint16_t)((Temp - 1)<<7);
		while((UART1->FR&(1<<5)) == (1<<5));	//wait until Tx full
		UART1->DR = (uint8_t)Temp;
#else	//UART_1 == 1
		while((UART2->FR&(1<<4))==(1<<4));		//wait until Rx empty
		Temp = (uint32_t)UART2->DR + 1;
		PORTD->RXTX = (uint16_t)((Temp - 1)<<7)&(0x3F<<7);
		while((UART2->FR&(1<<5)) == (1<<5));	//wait until Tx full
		UART2->DR = (uint8_t)Temp;
#endif	//UART_1 == 1
	}
}

//--- 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 Clock = HSE (8MHz)
	RST_CLK->PER_CLOCK|=0x08;					//EEPROM Clock enable
	EEPROM->CMD = 0;
	RST_CLK->PER_CLOCK&=(~0x08);				//EEPROM Clock disable
	RST_CLK->PLL_CONTROL=(1<<2)|(2<<8);			//CPU_PLL On, PLL_MULL=2 
	while((RST_CLK->CLOCK_STATUS&0x02)!=0x02);	//Wait until PLL not ready
	RST_CLK->CPU_CLOCK = 0x00000106;			//CPU Clock=3*8MHz=24MHz
#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 Clock enable
	EEPROM->CMD = 0;
	RST_CLK->CPU_CLOCK = 0x00000102;			//CPU Clock = HSE (25MHz)
#endif	//REVISION_2

	RST_CLK->PER_CLOCK |= 1<<24; 				//clock of PORTD ON	

#if UART_1 == 1
	RST_CLK->PER_CLOCK |= 1<<6;					//clock of UART1 On
	RST_CLK->UART_CLOCK = 1<<24;				//enable CLK of UART1
	RST_CLK->PER_CLOCK |= 1<<23;				//clock of PORTC ON
#else	//UART1 == 1
	RST_CLK->PER_CLOCK |= 1<<7;					//clock of UART2 On
	RST_CLK->UART_CLOCK = 1<<25;				//enable CLK of UART2
#endif	//UART1 == 1

}

//--- Ports configuration ---
void PortConfig()
{
	//*** Config port D for Leds ***
	PORTD->FUNC = 0x00000000;
	PORTD->RXTX = 0x0000;
	PORTD->OE = 0x7F80;
	PORTD->ANALOG = 0x7F80;
	PORTD->PWR = 0x3FFFC000;

#if UART_1 == 1
	PORTC->FUNC = 0x00000140;
	PORTC->RXTX = 0x0000;
	PORTC->ANALOG = 0x0018;
	PORTC->PWR = 0x000003C0;
#else	//UART_1 == 1
	PORTD->FUNC |= (5<<26);
#endif	//UART_1 == 1
}

//--- UART configuration ---
void UARTConfig()
{
#if UART_1 == 1
#ifdef REVISION_2
	UART1->IBRD = 0x000D;	//13
	UART1->FBRD = 0x0001;	//36
#else
	UART1->IBRD = 0x000D;	//13
	UART1->FBRD = 0x0024;	//36
#endif	//REVISION_2
	UART1->LCR_H = 0x0070;
	UART1->CR = 0x0301;

#else	//UART1 == 1
#ifdef REVISION_2
	UART2->IBRD = 0x000D;	//13
	UART2->FBRD = 0x0001;	//36
#else
	UART2->IBRD = 0x000D;	//13
	UART2->FBRD = 0x0024;	//36
#endif	//REVISION_2
	UART2->LCR_H = 0x0070;
	UART2->CR = 0x0301;
#endif	//UART1 == 1
}

