#include "stm32f10x.h"
#include "stdio.h"
#include "config.h"
#include "util.h"
#include "W5500\w5500.h"
#include "W5500\socket.h"
#include "W5500\spi2.h"
#include "APPs\loopback.h"

#include <string.h>
#include <stdlib.h>

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/

CONFIG_MSG Config_Msg;
CHCONFIG_TYPE_DEF Chconfig_Type_Def; 

// Configuration Network Information of W5500

uint8 Enable_DHCP = OFF;

//FOR TCP Client
//Configuration Network Information of TEST PC
uint8 Dest_IP[4] = {192, 168, 11, 3}; //DST_IP Address 
uint16 Dest_PORT = 3000; //DST_IP port

uint8 ch_status[MAX_SOCK_NUM] = { 0, };	/** 0:close, 1:ready, 2:connected */

uint8 TX_BUF[TX_RX_MAX_BUF_SIZE]; // TX Buffer for applications
uint8 RX_BUF[TX_RX_MAX_BUF_SIZE]; // RX Buffer for applications
   
void WIZ_Config(void)
{
  // Call Set_network(): Set network configuration, Init. TX/RX MEM SIZE., and  Set RTR/RCR
  Set_default();
  Set_network(); // at util.c
}       
__IO uint32_t Timer2_Counter;

uint32_t my_time;
uint32_t presentTime;

/* Private function prototypes -----------------------------------------------*/
void RCC_Configuration(void);
void NVIC_Configuration(void);
void GPIO_Configuration(void);
void Timer_Configuration(void);


/* Private functions ---------------------------------------------------------*/

void Timer2_ISR(void)
{
	if (Timer2_Counter++ > 1000) { // 1m x 1000 = 1sec
		Timer2_Counter = 0;
		my_time++;
		
	}
	
}


/*******************************************************************************
* Function Name  : main
* Description    : Main program.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
int main(void)
{  
	RCC_Configuration(); // Configure the system clocks
	NVIC_Configuration(); // NVIC Configuration
	GPIO_Configuration();
        Systick_Init(72);
	USART1_Init();
	Timer_Configuration();
        
	Reset_W5500();
	WIZ_SPI_Init();	
	WIZ_Config(); // network config & Call Set_network ();      
        
        // LED3 and LED4 On!
	LED3_onoff(ON);
	//LED4_onoff(ON);
        
        presentTime = my_time; // For TCP client's connection request delay
        
        // Start Application         
        printf("\r\n\r\n------------------------------------------- \r\n");
	    //printf("TCP Server Loopback using W5500\r\n");  
            printf("UDP Loopback using W5500\r\n");
        printf("------------------------------------------- ");
	while(1) {
		loopback_tcps(0, 5000);
		loopback_tcps(1, 5001);
		loopback_tcps(2, 5002);
		loopback_tcps(3, 5003);		
                loopback_tcpc(4, 5004);              
		loopback_udp(5, 3000);
		loopback_udp(6, 3001);
		loopback_udp(7, 3002);                
	}
        
}


void Timer_Configuration(void)
{

	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;

  /* Time base configuration */	
  TIM_TimeBaseStructure.TIM_Period = 1000;          
  TIM_TimeBaseStructure.TIM_Prescaler = 0;      
  TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;    
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; 
  
  TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

  /* Prescaler configuration */
  TIM_PrescalerConfig(TIM2, 71, TIM_PSCReloadMode_Immediate);

  /* TIM enable counter */
  TIM_Cmd(TIM2, ENABLE);

  /* TIM IT enable */
  TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);  

}





