This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CCS/EK-TM4C1294XL: EK-TM4C1294XL-TCP/IP Client

Part Number: EK-TM4C1294XL

Tool/software: Code Composer Studio

Dear, TI

Do you have example TCP/IP Client with board  EK-TM4C1294XL?

Please recoment me.

Thank you

RATWO

  • Hi Ratthawut,

      I came across this client example that you can reference. You will need to fine tune the code to suit your application. Please note, by no means this is a fully verified example. You will be responsible to modify if you came across bugs or performance issues. 

    #include <stdbool.h>
    #include <stdint.h>
    #include <string.h>
    #include <stdio.h>
    #include "inc/hw_ints.h"
    #include "inc/hw_memmap.h"
    #include "driverlib/flash.h"
    #include "driverlib/interrupt.h"
    #include "driverlib/gpio.h"
    #include "driverlib/rom_map.h"
    #include "driverlib/sysctl.h"
    #include "driverlib/systick.h"
    #include "inc/hw_emac.h"
    
    #include "utils/lwiplib.h"
    #include "utils/ustdlib.h"
    #include "utils/uartstdio.h"
    #include "drivers/pinout.h"
    
    #include "lwip/debug.h"
    #include "lwip/stats.h"
    #include "lwip/tcp.h"
    
    
    
    extern void echo_init(void);
    
    err_t echo_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err);
    err_t echo_poll(void *arg, struct tcp_pcb *tpcb);
    void echo_error(void *arg, err_t err);
    err_t echo_sent(void *arg, struct tcp_pcb *tpcb, u16_t len);
    err_t err;
    
    uint32_t tcp_send_packet(void);
    
    static struct tcp_pcb *testpcb;  //pcb global
    
    
    #define SYSTICKHZ               100
    #define SYSTICKMS               (1000 / SYSTICKHZ)
    
    #define SYSTICK_INT_PRIORITY    0x80
    #define ETHERNET_INT_PRIORITY   0xC0
    
    uint32_t g_ui32IPAddress;
    
    uint32_t g_ui32SysClock;
    volatile bool g_bLED;
    bool g_bIPAddrValid = 0;
    
    
    
    //*****************************************************************************
    // The error routine that is called if the driver library encounters an error.
    //*****************************************************************************
    #ifdef DEBUG
    void
    __error__(char *pcFilename, uint32_t ui32Line)
    {
    }
    #endif
    
    
    void
    DisplayIPAddress(uint32_t ui32Addr)
    {
        char pcBuf[16];
        // Convert the IP Address into a string.
         usprintf(pcBuf, "%d.%d.%d.%d", ui32Addr & 0xff, (ui32Addr >> 8) & 0xff,
                (ui32Addr >> 16) & 0xff, (ui32Addr >> 24) & 0xff);
        UARTprintf(pcBuf);
    }
    
    //*****************************************************************************
    // Required by lwIP library to support any host-related timer functions.
    //*****************************************************************************
    void
    lwIPHostTimerHandler(void)
    {
        uint32_t ui32NewIPAddress;
    
        ui32NewIPAddress = lwIPLocalIPAddrGet(); // Get the current IP address.
    
        if(ui32NewIPAddress != g_ui32IPAddress) // See if the IP address has changed.
        {
            if(ui32NewIPAddress == 0xffffffff)
            {
                UARTprintf("Waiting for link.\n");
            }
            else if(ui32NewIPAddress == 0)
            {
                UARTprintf("Waiting for IP address.\n");
            }
            else
            {
            	g_bIPAddrValid = 1;
                UARTprintf("IP Address: ");
                DisplayIPAddress(ui32NewIPAddress);
                UARTprintf("\n\n");
            }
            g_ui32IPAddress = ui32NewIPAddress;
        }
    
        if((ui32NewIPAddress == 0) || (ui32NewIPAddress == 0xffffffff))
        {
            //
            // Do nothing and keep waiting.
            //
        }
    }
    
    //*****************************************************************************
    // The interrupt handler for the SysTick interrupt.
    //*****************************************************************************
    void
    SysTickIntHandler(void)
    {
        lwIPTimer(SYSTICKMS);
    
         g_bLED = true;
    }
    
    
    //*****************************************************************************
    char mydata[1024];
    
    err_t echo_receive(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
    {
        int len;
        char *header = "Client: ";
        int header_len = strlen(header);
    
        UARTprintf("Data received.\n");
        if (p == NULL) {
            UARTprintf("The remote host closed the connection.\n");
            tcp_close(tpcb);
            return ERR_ABRT;
        } else {
            tcp_recved(tpcb, p->tot_len);
    
            //size of the pay load
            len = p->tot_len + header_len;
    
            strcpy(mydata, header);
            strcat(mydata, (char *)p->payload);
    
    
           //Free the packet buffer
            pbuf_free(p);
    
            //check output buffer capacity
            if (len > tcp_sndbuf(tpcb))
            	len= tcp_sndbuf(tpcb);
    
            //Enqueue the data to send out
            //err = tcp_write(tpcb, mydata, len, 0);
            err = tcp_write(tpcb, mydata, len, 0);
            if (err == ERR_OK) {
                UARTprintf("Echo the data out\n");
            }
            else if(err == ERR_MEM)
            {
                /* we are low on memory, try later, defer to poll */
                UARTprintf("Low on memory.\n");
    
            } else
            {
               /* Other problems to take care of */
                UARTprintf("Other problems.\n");
            }
            tcp_sent(tpcb, NULL); /* No need to call back */
        }
    
    
        return ERR_OK;
    }
    
    uint32_t tcp_send_packet(void)
    {
        char *string = "Hello World! \n\r ";
    
        err = tcp_write(testpcb, string, strlen(string), TCP_WRITE_FLAG_COPY);
    
    
        if (err) {
            UARTprintf("ERROR: Code: %d (tcp_send_packet :: tcp_write)\n", err);
            return 1;
        }
    
        /* now send */
        err = tcp_output(testpcb);
        if (err) {
            UARTprintf("ERROR: Code: %d (tcp_send_packet :: tcp_output)\n", err);
            return 1;
        }
        return 0;
    }
    
    err_t connectCallback(void *arg, struct tcp_pcb *tpcb, err_t err)
    {
    
    
        if (err == ERR_OK){
    
    		UARTprintf("Connection Established.\n");
    
    
    		tcp_recv(testpcb, echo_receive);
    		tcp_sent(testpcb, NULL);
    		UARTprintf("Sending a greeting message to the Server\n");
    		tcp_send_packet();
    		return 0;
        } else {
    		UARTprintf("No Connection Established.\n");
    		return 1;
    
        }
    }
    
    void echo_client_init(void)
    {
        /* create an ip */
        struct ip_addr server_addr;
    
    
        /* create the control block */
        testpcb = tcp_new();    //testpcb is a global struct tcp_pcb
    
        if (testpcb != NULL)
        {
            IP4_ADDR(&server_addr, 10,219,15,38);    //IP of the PC computer
    
    		tcp_bind(testpcb, IP_ADDR_ANY, 7000);
    
    		/* now connect */
    		err = tcp_connect(testpcb, &server_addr, 8000, connectCallback);
    	    if (err) {
    	        UARTprintf("ERROR: Code: %d (tcp_connect)\n", err);
    	    }
    
    		UARTprintf("PCB CREATED\n");
    
        } else {
            memp_free(MEMP_TCP_PCB, testpcb);
    		UARTprintf("PCB NOT CREATED\n");
    
        }
    
    }
    
    
    int
    main(void)
    {
        uint32_t ui32User0, ui32User1;
        uint8_t pui8MACArray[8];
    
    
        SysCtlMOSCConfigSet(SYSCTL_MOSC_HIGHFREQ);
    
        g_ui32SysClock = MAP_SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ |
                                                 SYSCTL_OSC_MAIN |
                                                 SYSCTL_USE_PLL |
                                                 SYSCTL_CFG_VCO_480), 120000000);
        PinoutSet(true, false);
    
        UARTStdioConfig(0, 115200, g_ui32SysClock);
    
    
        UARTprintf("\033[2J\033[H");
        UARTprintf("Ethernet lwIP TCP echo client example\n\n");
    
        MAP_GPIOPinTypeGPIOOutput(GPIO_PORTN_BASE, GPIO_PIN_1);
    
        MAP_GPIOPinWrite(GPIO_PORTN_BASE, GPIO_PIN_1, ~GPIO_PIN_1);
    
        MAP_SysTickPeriodSet(g_ui32SysClock / SYSTICKHZ);
        MAP_SysTickEnable();
        MAP_SysTickIntEnable();
    
        MAP_FlashUserGet(&ui32User0, &ui32User1);
        if((ui32User0 == 0xffffffff) || (ui32User1 == 0xffffffff))
        {
            UARTprintf("No MAC programmed!\n");
            while(1)
            {
            }
        }
    
        UARTprintf("Waiting for IP.\n");
    
        pui8MACArray[0] = ((ui32User0 >>  0) & 0xff);
        pui8MACArray[1] = ((ui32User0 >>  8) & 0xff);
        pui8MACArray[2] = ((ui32User0 >> 16) & 0xff);
        pui8MACArray[3] = ((ui32User1 >>  0) & 0xff);
        pui8MACArray[4] = ((ui32User1 >>  8) & 0xff);
        pui8MACArray[5] = ((ui32User1 >> 16) & 0xff);
    
        lwIPInit(g_ui32SysClock, pui8MACArray, 0, 0, 0, IPADDR_USE_DHCP);
    
    
        while (g_bIPAddrValid == 0);
    
    
        echo_client_init();
    
    
        MAP_IntPrioritySet(INT_EMAC0, ETHERNET_INT_PRIORITY);
        MAP_IntPrioritySet(FAULT_SYSTICK, SYSTICK_INT_PRIORITY);
    
    
    
        //
        // Loop forever, processing the LED blinking.  All the work is done in
        // interrupt handlers.
        while(1)
        {
    
            while(g_bLED == false)
            {
            }
    
            //
            // Clear the flag.
            //
            g_bLED = false;
    
            //
            // Toggle the LED.
            //
            MAP_GPIOPinWrite(GPIO_PORTN_BASE, GPIO_PIN_1,
                             (MAP_GPIOPinRead(GPIO_PORTN_BASE, GPIO_PIN_1) ^
                              GPIO_PIN_1));
        }
    }

  • Dear, Charles

    Thank you for you support