Hi Again,
I developed a code for Tcp and Udp connection. it's working fine but over 2 days later ethernet connection stops. But mcu is working fine already. My source code is here about ethernet (!! Sorry, it is not clear code , because it has debug codes :) ):
#include <stdbool.h> #include <stdint.h> #include <stdio.h> #include <string.h> #include <ctype.h> #include "inc/hw_ints.h" #include "driverlib/interrupt.h" #include "driverlib/rom.h" #include "driverlib/rom_map.h" #include "driverlib/sysctl.h" #include "driverlib/systick.h" #include "driverlib/gpio.h" #include "driverlib/pin_map.h" #include "driverlib/systick.h" #include "driverlib/flash.h" #include "utils/locator.h" #include "utils/lwiplib.h" #include "utils/ustdlib.h" #include <lwip/ip_addr.h> #include "isl/isl_define.h" #include "isl/isl_systick.h" #include "isl/isl_serial.h" #include "isl/isl_bluetooth.h" #include "isl/isl_ethernet.h" #define SYSTICKHZ 100 #define SYSTICKMS (1000 / SYSTICKHZ) #define SYSTICK_INT_PRIORITY 0x80 #define ETHERNET_INT_PRIORITY 0xC0 volatile enum { iEthNoConnection, iEthDHCPWait, iEthDHCPComplete, iEthDNSWait, iEthTCPConnectWait, iEthTCPConnectComplete, iEthQueryWait, iEthTCPOpen, iEthIdle } eState; typedef struct { struct pbuf *psBuf; uint32_t ui32Idx; } tBufPtr; extern void UDP_Received(char *rcv_msg); extern void TCP_Received(char *rcv_msg); extern void GetIpAddress(uint32_t ip); struct udp_pcb *ptel_pcb; struct tcp_pcb *psTCP; volatile bool tcpConnected=false; volatile bool ipIsOk=false; uint32_t g_ui32IPAddress; struct ip_addr destip; uint16_t destPortTcp; extern void ISL_Ethernet_TCP_State(uint32_t State); extern uint32_t SysClock; struct ip_addr MakeIpAddress(char *str) { struct ip_addr ip; unsigned char value[4] = {0}; size_t index = 0; while (*str) { if (isdigit((unsigned char)*str)) { value[index] *= 10; value[index] += *str - '0'; } else { index++; } str++; } IP4_ADDR(&ip,value[0],value[1],value[2],value[3]); return ip; } void lwIPHostTimerHandler(void) { uint32_t ui32NewIPAddress; ui32NewIPAddress = lwIPLocalIPAddrGet(); if(ui32NewIPAddress != g_ui32IPAddress) { if(ui32NewIPAddress == 0xffffffff) { } else if(ui32NewIPAddress == 0) { } else { g_ui32IPAddress = ui32NewIPAddress; ISL_Ethernet_TCP_State(IP_OK); ipIsOk=true; } g_ui32IPAddress = ui32NewIPAddress; } } void ISL_Send_Ethernet_UDP(char msg[], uint16_t len, char *ip, uint16_t port) { if(!ipIsOk) return; struct pbuf *p; struct ip_addr destip; do { p=pbuf_alloc(PBUF_TRANSPORT,len, PBUF_RAM); }while(p==NULL); memcpy(p->payload,msg,len);; p->len=len; destip=MakeIpAddress(ip); udp_sendto(ptel_pcb,p,&destip,port); pbuf_free(p); } void udp_echo_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, struct ip_addr *addr, u16_t port) { char msg[]=""; memcpy(msg,p->payload,p->len); msg[p->len]='\0'; UDP_Received(msg); /* echo char msg[]=""; if (p != NULL) { memcpy(msg,p->payload,p->len); msg[p->len]='\0'; udp_sendto(pcb,p,addr,1236); pbuf_free(p); } */ } static void BufPtrInit(tBufPtr *psBufPtr, struct pbuf *psBuf) { psBufPtr->psBuf = psBuf; psBufPtr->ui32Idx = 0; } static uint8_t BufData8Get(tBufPtr *psBufPtr) { return(((uint8_t *)psBufPtr->psBuf->payload)[psBufPtr->ui32Idx]); } uint32_t BufPtrInc(tBufPtr *psBufPtr, uint32_t ui32Inc) { psBufPtr->ui32Idx += ui32Inc; if(psBufPtr->ui32Idx >= psBufPtr->psBuf->len) { psBufPtr->ui32Idx = 0; psBufPtr->psBuf = psBufPtr->psBuf->next; if(psBufPtr->psBuf == 0) { ui32Inc = 0; } } return(ui32Inc); } void ISL_Config_Ethernet_UDP(uint32_t Port) { while(!ipIsOk) { } do { ptel_pcb = udp_new(); }while(ptel_pcb == NULL); udp_bind(ptel_pcb, IP_ADDR_ANY, Port); udp_recv(ptel_pcb, udp_echo_recv, NULL); } void ISL_Ethernet_TCP_Close(void) { err_t err; if(psTCP) { tcp_arg(psTCP, NULL); tcp_sent(psTCP, NULL); tcp_recv(psTCP, NULL); tcp_err(psTCP, NULL); } do { err=tcp_close(psTCP); }while(err!=ERR_OK); do { err=tcp_output(psTCP); }while(err!=ERR_OK); ISL_Ethernet_TCP_State(TCP_DISCONNECTED); } static err_t TCPSent(void *pvArg, struct tcp_pcb *psPcb, uint16_t len) { return (ERR_OK); } static err_t TCPReceive(void *pvArg, struct tcp_pcb *psPcb, struct pbuf *psBuf, err_t iErr) { if(psBuf->len==0) { ISL_Ethernet_TCP_Close(); pbuf_free(psBuf); ISL_Ethernet_TCP_State(TCP_DISCONNECTED); return ERR_CONN; } tBufPtr sBufPtr; BufPtrInit(&sBufPtr, psBuf); char rcv[500]; char *str=rcv; while(1) { uint8_t rcv_ch=BufData8Get(&sBufPtr); *str=rcv_ch; str++; if(BufPtrInc(&sBufPtr, 1) != 1) { *str='\0'; TCP_Received(rcv); break; } } struct pbuf *psBufCur; psBufCur = psBuf; while(psBufCur->len!=0) { tcp_recved(psPcb, psBufCur->len); psBufCur = psBufCur->next; if(psBufCur == 0) { break; } } pbuf_free(psBuf); return(ERR_OK); } static err_t TCPConnected(void *pvArg, struct tcp_pcb *psPcb, err_t iErr) { if(iErr != ERR_OK) { ISL_Ethernet_TCP_Close(); return iErr; } else { ISL_Ethernet_TCP_State(TCP_CONNECTED); tcp_recv(psPcb, TCPReceive); tcp_sent(psPcb, TCPSent); eState = iEthTCPConnectComplete; } return (ERR_OK); } void ISL_Send_Ethernet_TCP(char *Data,uint16_t len) { char send[20]; if(len>tcp_sndbuf(psTCP)) { //cok buyuk data return; } err_t eError; eError = tcp_write(psTCP, Data, len,TCP_WRITE_FLAG_COPY); if(eError!=ERR_OK) { sprintf(send,"ISL_Send_Ethernet_TCP ERROR : %d",eError); ISL_Send_SerialPort_String(send,1); ISL_Ethernet_TCP_Close(); ISL_Ethernet_TCP_State(TCP_DISCONNECTED); return; } else { eError=tcp_output(psTCP); if(eError!=ERR_OK) { sprintf(send,"ERROR : %d",eError); ISL_Send_SerialPort_String(send,1); } } } void ISL_Config_Ethernet_TCP(char *ip, uint32_t port) { char send[20]; err_t eError; if(!ipIsOk) { return; } destPortTcp=port; destip=MakeIpAddress(ip); if(psTCP!=NULL) { ISL_Ethernet_TCP_Close(); } tcp_init(); do { psTCP = tcp_new(); }while(psTCP == NULL); do { eError=tcp_connect(psTCP, &destip, destPortTcp, TCPConnected); if(eError!=ERR_OK) { sprintf(send,"ISL_Config_Ethernet_TCP ERROR : %d",eError); ISL_Send_SerialPort_String(send,1); } }while(eError!=ERR_OK); ISL_Send_SerialPort_String("TCP Connected",1); } void ISL_Config_Ethernet(void) { uint8_t MACArray[8]; uint32_t ui32User0,ui32User1; #ifdef PART_TM4C129ENCPDT SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOK); GPIOPinConfigure(GPIO_PF1_EN0LED2); GPIOPinConfigure(GPIO_PK4_EN0LED0); GPIOPinConfigure(GPIO_PK6_EN0LED1); GPIOPinTypeEthernetLED(PORT_F, GPIO_PIN_1); GPIOPinTypeEthernetLED(PORT_K, GPIO_PIN_4); GPIOPinTypeEthernetLED(PORT_K, GPIO_PIN_6); #endif #ifndef __DRIVERLIB_ISL_SDCARD_H__ ROM_SysTickPeriodSet(SysClock / SYSTICKHZ); ROM_SysTickEnable(); ROM_SysTickIntEnable(); #endif ROM_FlashUserGet(&ui32User0,&ui32User1); if((ui32User0 == 0xffffffff) || (ui32User1 == 0xffffffff)) { while(1) { } } MACArray[0] = ((ui32User0 >> 0) & 0xff); MACArray[1] = ((ui32User0 >> 8) & 0xff); MACArray[2] = ((ui32User0 >> 16) & 0xff); MACArray[3] = ((ui32User1 >> 0) & 0xff); MACArray[4] = ((ui32User1 >> 8) & 0xff); MACArray[5] = ((ui32User1 >> 16) & 0xff); lwIPInit(SysClock, MACArray,0,0,0,IPADDR_USE_DHCP); LocatorInit(); LocatorMACAddrSet(MACArray); LocatorAppTitleSet("isb"); ROM_IntPrioritySet(INT_EMAC0, ETHERNET_INT_PRIORITY); ROM_IntPrioritySet(FAULT_SYSTICK, SYSTICK_INT_PRIORITY); while(!ipIsOk); }