int main(void)
{
	// Initialize hardware
	initHW();

    // Display greeting
    putsUart0("Serial Example\r\n");
    putsUart0("Press '0' or '1'\r\n");
    putcUart0('a');
    BLUE_LED = 1;

    // Wait for PB press
    waitPbPress();

    // For each received character, toggle the red LED
    // For each received "1", set the green LED
    // For each received "0", clear the green LED
    while(1)
    {
        char c = getcUart0();
        RED_LED ^= 1;
        if (c == '1')
            GREEN_LED = 1;
        if (c == '0')
            GREEN_LED = 0;
    }

}
void initEEPROM()
{
	SYSCTL_RCGCEEPROM_R &= ~SYSCTL_SREEPROM_R0;//Disable and Clock EEPROM Module
	SYSCTL_RCGCEEPROM_R |= SYSCTL_RCGCEEPROM_R0;//Enable and Clock EEPROM Module
	//6 cycles delay
	__asm(" NOP");
	__asm(" NOP");
	__asm(" NOP");
	__asm(" NOP");
	__asm(" NOP");
	__asm(" NOP");
	while(EEPROM_EEDONE_R & EEPROM_EEDONE_WORKING)//Poll WORKING bit of EEDONE Register
	{}
	if((EEPROM_EESUPP_R & EEPROM_EESUPP_PRETRY) == 0x00000008)
	{
		//Put Error Message on UART
	    putsUart0("EEPROM init Error\r\n");
	}
	else if((EEPROM_EESUPP_R & EEPROM_EESUPP_PRETRY) == 0x00000004)
	{
		//Put Error Message on UART
		putsUart0("EEPROM init Error\r\n");
	}
	else
	{
		//putsUart0("EEPROM init Success\r\n");
	}
	//EEPROM_EEDBGME_R |=0xE37B0001;





//	SYSCTL_SREEPROM_R |= SYSCTL_SREEPROM_R0;//Reset SREEPROM Module
//	__asm("NOP");//6 cycles delay
//	__asm("NOP");
//	__asm("NOP");
//	__asm("NOP");
//	__asm("NOP");
//	__asm("NOP");
//	while((EEPROM_EEDONE_R & EEPROM_EEDONE_WORKING) == 0x00000000)//Poll WORKING bit of EEDONE Register
//	{}
//	if((EEPROM_EESUPP_R & EEPROM_EESUPP_PRETRY) == 0x00000008)
//		{
//			//Put Error Message on UART
//		    putsUart0("EEPROM init Error\r\n");
//		}
//		else if((EEPROM_EESUPP_R & EEPROM_EESUPP_PRETRY) == 0x00000004)
//		{
//			//Put Error Message on UART
//			putsUart0("EEPROM init Error\r\n");
//		}
//		else
//		{
//			putsUart0("EEPROM init Success\r\n");
//		}

}
int main(void)
{
   // uint8_t* udpData;
    uint8_t* tcpData;
    uint8_t p=0;
    uint16_t i=0;
    Seq_number=0xfb0983f7;
    Ack_number=0x00000;
    myport=0xb921;
    id=0x1229;
    uint8_t* http;

    // init controller
    initHw();

    putsUart0("\r\nWeather forecast\r\n");
    // init ethernet interface
    etherInit(ETHER_UNICAST | ETHER_BROADCAST | ETHER_HALFDUPLEX);
    etherSetIpAddress(192,168,137,199);

    // flash phy leds
    etherWritePhy(PHLCON, 0x0880);
    RED_LED = 1;

    waitMicrosecond(500000);
    etherWritePhy(PHLCON, 0x0990);
    RED_LED = 0;
    waitMicrosecond(500000);

    sendSynWeather(data);

    // message loop
    while (1)
    {
        if (etherKbhit())
        {
            if (etherIsOverflow())
            {
                RED_LED = 1;
                waitMicrosecond(100000);
                RED_LED = 0;
            }
            // get packet
            etherGetPacket(data, 1500);
            // handle arp request

            if (etherIsArp(data))
            {
                etherSendArpResp(data);
                RED_LED = 1;
               // GREEN_LED = 1;
                waitMicrosecond(50000);
                RED_LED = 0;
                GREEN_LED = 0;
            }

            // handle ip datagram

            if (etherIsIp(data))
            {
            	if (etherIsIpUnicast(data))
            	{
            		// handle icmp ping request

            		if (etherIsPingReq(data))
					{
					  etherSendPingResp(data);
					  GREEN_LED = 1;
					  waitMicrosecond(50000);
					  GREEN_LED = 0;

					}

					if(etherIsTcp(data))
					{
						tcpData = etherGetTcpData(data);
						if(etherisHandshake1(data))
						{
							etherRespondtoHandshake1(data);
							RED_LED = 1;
							//GREEN_LED=1;
							BLUE_LED=1;
							waitMicrosecond(100000);
							RED_LED = 0;
							//GREEN_LED=0;
							BLUE_LED=0;
							break;
						}

					}


                }
            }
        }
    }

    sendGETRequest(data);

    while(1)
    {
    	if (etherKbhit())
    	{
    		etherGetPacket(data, 1500);
			if(etherIsTcp(data))
			{

				RED_LED = 1;
				//GREEN_LED=1;
				BLUE_LED=1;
				waitMicrosecond(100000);
				RED_LED = 0;
				//GREEN_LED=0;
				BLUE_LED=0;

				if(p<=7)
					{
					if(tcp_DataCount>0)
						etherRespondToData(data);
					}


				//if(p>=2)
				{
					//p=0;
					tcpData = etherGetTcpData(data);
					http=tcpData+9;
					if((*http==0x32))//|(*http==0x35))
					{
						tcpData=tcpData+299;
						for(i=0;i<(tcp_DataCount-299);i++)
						{
						putcUart0(*tcpData);
						tcpData++;
						}
						if(*http==0x32)
								break;
					}
					//break;
					//sendFinRequest(data);

				}
				p++;
			}
    	}

    }
    while(1);

    return 0;
}