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/TM4C1294NCPDT: Sendind Data via Ethernet Example

Part Number: TM4C1294NCPDT


Tool/software: Code Composer Studio

Hi

I am looking for an example to use the LWIP stack in the TIVA C microcontroller, I went through the Workbook example enet_lwip, but I have no idea how to modify it, I am able to take some reads from the ADC and I would like to send it via ethernet. 

I will appreciate if someone give me an start point or a guide I can follow to implement the lwip stack in Code Composer, I just want to see how to send a packet via TCP

Thanks in advance

  • Hi,

      The enet_lwip is an HTTP application example. I think you want to start with the tcp echo example. Refer to below links for the echo example and additional LwIP API user's guide.

  • Dear Charles, thanks for your answer

    I tried to modify the enet_lwip following the instructions in the link you kindly share, but I had no success, I can ping to the board but can't see that is sending packets (I tried open a RAW connection in putty and in the browser), I used  UART messages to find out what is happening and seems it cant enter to "connectedCallback" and send the packet.

    This is my code, if you can give it a look would be great

    #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 "utils/locator.h"
    #include "utils/lwiplib.h"
    #include "utils/ustdlib.h"
    #include "utils/uartstdio.h"
    #include "httpserver_raw/httpd.h"
    #include "tcpecho_raw/echo.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;
    
    
    
    //*****************************************************************************
    // 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
            {
                UARTprintf("IP Address: ");
                DisplayIPAddress(ui32NewIPAddress);
                UARTprintf("\nOpen a browser and enter the IP address.\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;
    }
    
    
    //*****************************************************************************
    
    err_t tcpRecvCallback(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
    {
        UARTprintf("Data recieved.\n");
        if (p == NULL) {
            UARTprintf("The remote host closed the connection.\n");
            UARTprintf("Closing the connection.\n");
            tcp_close(testpcb);
            return ERR_ABRT;
        } else {
            UARTprintf("Number of pbufs %d\n", pbuf_clen(p));
            UARTprintf("Contents of pbuf %s\n", (char *)p->payload);
        }
    
        return 0;
    }
    
    uint32_t tcp_send_packet(void)
    {
        char *string = "HEAD /process.php?data1=12&data2=5 HTTP/1.0\r\nHost: mywebsite.com\r\n\r\n ";
        uint32_t len = strlen(string);
    
    
            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)
    {
        UARTprintf("Connection Established.\n");
        UARTprintf("Sending a packet\n");
        tcp_send_packet();
        return 0;
    }
    
    void tcp_setup(void)
    {
        uint32_t data = 0xdeadbeef;
    
        /* create an ip */
        struct ip_addr ip;
    
        IP4_ADDR(&ip, 192,168,0,4);    //IP of the computer
    
        /* create the control block */
        testpcb = tcp_new();    //testpcb is a global struct tcp_pcb
    
        
    
        tcp_arg(testpcb, &data);
    
        tcp_err(testpcb, echo_error);
        tcp_recv(testpcb, tcpRecvCallback);
        tcp_sent(testpcb, echo_sent);
    
        /* now connect */
        tcp_connect(testpcb, &ip, 80, connectCallback);
        UARTprintf("PCB 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 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);
    
         //
        // Setup the device locator service.
        //
        LocatorInit();
        LocatorMACAddrSet(pui8MACArray);
        LocatorAppTitleSet("EK-TM4C1294XL enet_io");
    
    
        // Initialize a sample httpd server.
        //
      //  httpd_init();
    
        echo_init();
    
        tcp_setup();
    
    
        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));
        }
    }
    

    And I also added the TCP app from LWIP int the third party folder 

    I connect the board to my router and the COM port sends this 

    In my computer I just get a refused connection

    I suspect I am missing something obvious, I would appreciate any help 

    Best

  • Hi,
    Did you get the tcp echo to work first? I don't see you binding the pcb to the IP address and the port with the tcp_bind() in your tcp_setup. I will suggest you get the echo example to work first.

    Below is another link to a TCP echo example.
    coherentmusings.wordpress.com/.../
  • Dear Charles, you were absolutely rigth, I added the tcp_bind() function and now is working. Thanks a lot!
  • Hi Rocamadour,
    Do you mind to share your tcpecho_raw/echo.h file so it will benefit the community to reference your success.
  • Sure, here are the files I used. Regards

    tcpecho_raw.rar