/*
 * GccApplication4.c
 *
 * Created: 03.08.2020 16:38:39
 * Author : oli_r
 */ 

#include "main.h"
#include <avr/io.h>
#include <avr/interrupt.h>
#include "enc28j60.h"
//#include "lib/http.h"
#include "net.h"
#include "net_tcp.h"
#include "usart.h"

#include "avr/eeprom.h"


volatile uint16_t   debugLevel;
volatile uint8_t    timeflags;
volatile uint16_t   counter;


#define DEBUGLVL_DEFAULT          (DEBUGLVL_STACK_TCP_ERROR | DEBUGLVL_STACK_TCP_INFO | DEBUGLVL_STACK_INFO | DEBUGLVL_STACK_ERROR)

// --------------------------------------
// Timeflags
#define TIMEFLAG_10MS             0x01        // Jeweils nur einzelne Bits, damit diese unabhängig voneinander sind
#define TIMEFLAG_100MS            0x02
#define TIMEFLAG_1S               0x04

#ifndef NULL
#define NULL (void *)0;
#endif

// --------------------------------------

ISR ( USART0_RX_vect )
{
	USART_NEW_CHAR();
}


ISR ( TIMER0_COMPA_vect )
{
	// Counter fuer Sequence - Nummern neuer TCP - Verbindungen hochzaehlen
	if(tcpInitialSeqCnt==NULL)tcpInitialSeqCnt=1;
	else tcpInitialSeqCnt++;
	 
	counter ++;
	if (( counter % 10) == 0)
	{
		timeflags |= TIMEFLAG_10MS ;
	}
	if (( counter % 100) == 0)
	{
		timeflags |= TIMEFLAG_100MS ;
	}
	if( counter >= 1000)
	{
		timeflags |= TIMEFLAG_1S ;
		counter = 0;
	}
}

ISR ( INT2_vect )
{
	newPacketPresent = 1;
	ETH_INTERRUPT_DISABLE() ;
}

int main(void)
{
	
	uint8_t remoteIp [] = {192 , 168 , 0, 123};
	uint8_t tmp ;
	
	tmp = tcpOpen( remoteIp , 1397 , 1234 , NULL ); // Verbidung oeffnen
	//tmp = tcpOpen( ( uint8_t *)remoteIp , 1397 , 1234 , NULL ); // Verbidung oeffnen

	if( tmp != 255)
	{
		tcpSend ( tmp , ( uint8_t *)" Hallo Welt !\r", 12) ; // Text senden
		tcpClose ( tmp ); // Verbindung wieder schliessen
	}
	else
	{
		 
	}

	
    while (1) 
    {
		
	}
    
}

