#ifndef __CONFIG_H_
#define __CONFIG_H_

//Управляющие регистры Ethernrt-контроллера 5600ВГ1У
typedef struct
{
	unsigned int MAC_CTRL;
    unsigned int MinFrame;
    unsigned int MaxFrame;
    unsigned int CollConfig;
    unsigned int IPGTx;
    unsigned int MAC_ADDR_T;
    unsigned int MAC_ADDR_M;
    unsigned int MAC_ADDR_H;
    unsigned int HASH0;
    unsigned int HASH1;
    unsigned int HASH2;
    unsigned int HASH3;
    unsigned int INT_MSK;
    unsigned int INT_SRC;
    unsigned int PHY_CTRL;
    unsigned int PHY_STAT;
    unsigned int RXBF_HEAD;
    unsigned int RXBF_TAIL;
    unsigned int dammy0;
    unsigned int dammy1;
    unsigned int STAT_RX_ALL;
    unsigned int STAT_RX_OK;
    unsigned int STAT_RX_OVF;
    unsigned int STAT_RX_LOST;
    unsigned int STAT_TX_ALL;
    unsigned int STAT_TX_OK;
    unsigned int base_RxBF;
    unsigned int base_TxBF;
    unsigned int base_RxBD;
    unsigned int base_TxBD;
    unsigned int base_RG;
    unsigned int GCTRL;
} _ethernet;


//Стартовый адрес буфера приемника
#define BASE_ETH_RXBuffer                  0x60000000  //Absolut ADDRESS = 0x0000

//Стартовый адрес буфера дескрипторов принимаемых пакетов
#define BASE_ETH_RXDescBuffer              0x60002000  //Absolut ADDRESS = 0x0800

//Стартовый адрес передатчика
#define BASE_ETH_TXBuffer                  0x60004000  //Absolut ADDRESS = 0x1000

//Стартовый адрес буфера дескрипторов передаваемых пакетов
#define BASE_ETH_TXDescBuffer              0x60006000  //Absolut ADDRESS = 0x1800

//Стартовый адрес управляющих регистров Ethernrt-контроллера 5600ВГ1У
#define BASE_ETHERNET                      0x60007F00  //Absolut ADDRESS = 0x1FC0

#define Ethernet              ((_ethernet *)BASE_ETHERNET)

#define RxBuffer              ((unsigned int *)BASE_ETH_RXBuffer)
#define RxDescriptor          ((unsigned int *)BASE_ETH_RXDescBuffer)

#define TxBuffer              ((unsigned int *)BASE_ETH_TXBuffer)
#define TxDescriptor          ((unsigned int *)BASE_ETH_TXDescBuffer)

//#include "1986be91t1.h"
#include "1901BC1F.h"
#include "ethconfig.c"

void ExtBusCfg(void);
void PortConfig(void);
void ClkCfg(void);

//--------------------------------------------------------------------------------------
//Функция для настройки внешней системной шины
//Параметр:	нет
//Возвращаемых знаений нет
//--------------------------------------------------------------------------------------
__inline void ExtBusCfg()
{
	EXT_BUS_CNTRL->EXT_BUS_CONTROL = 0x2002;  //wait state=0 (3 HCLK), RAM mode
}

//--------------------------------------------------------------------------------------
//Функция для настройки портов контроллера 1986ВЕ91Т1
//Параметр:	нет
//Возвращаемых знаений нет
//-------------------------------------------------------------------------------------- 
void PortConfig()
{
//Data Bus[0:15]
		PORTA->ANALOG = 0xFFFF;
		PORTA->PWR = 0xAAAAAAAA;
		PORTA->FUNC = 0x55555555;

//OE-WE & Buttons
		PORTC->ANALOG = 0xFFFF; // OE-WE
		PORTC->PWR = 0xAAAAAAAA; // OE-WE
		PORTC->FUNC = 0x00000014; // OE-WE
//		PORTC->PD   = 0x00000000; // OE-WE//Светодиоды
//		PORTC->RXTX = 0x0000;
//		PORTC->OE = 0x0080;
//LED

        PORTD->RXTX = 0;
        PORTD->OE = 0x7C00; //PORTD10 - PORTD14 - outputs
        PORTD->FUNC = 0x00000000; //PORTD - ports
        PORTD->ANALOG = 0xFF00; //PORTD - digital
        PORTD->PWR = 0xAAAA0000; //PORTD8 - PORTD15 - fast edge

//Address Bus[0:12]
		PORTE->ANALOG = 0xFFFF; // CS
		PORTE->PWR = 0xAAAAAAAA; // CS
		PORTE->RXTX = 0x0000;
		PORTE->OE = 0x0030;
		PORTE->FUNC = 0x01000000; // CS

		PORTF->ANALOG = 0xFFFF;
		PORTF->PWR = 0xFFFFFFFF;
		PORTF->FUNC = 0x55555555;

		PORTB->RXTX = 0;
        PORTB->OE = 0x8000; //PORTD10 - PORTD14 - outputs
        PORTB->FUNC = 0x00000000; //PORTD - ports
        PORTB->ANALOG = 0xC000; //PORTD - digital
        PORTB->PWR = 0xC0000000; //PORTD8 - PORTD15 - fast edge
}

//--------------------------------------------------------------------------------------
//Функция для настройки тактовых сигналов контроллера 1986ВЕ91Т1
//Параметр:	нет
//Возвращаемых знаений нет
//-------------------------------------------------------------------------------------- 
void ClkCfg()
{
        RST_CLK->HS_CONTROL = 0x00000001;
        while((RST_CLK->CLOCK_STATUS&0x00000004)!=0x00000004){}
		RST_CLK->CPU_CLOCK = 0x00000102;
		//RST_CLK->PER_CLOCK = 0x63E00018;
		RST_CLK->PER_CLOCK = 0xffffffff;
}

#endif /*__CONFIG_H_*/

