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.

UDP Communication with lwIP Library

Other Parts Discussed in Thread: HALCOGEN, TMS570LC4357, TMS570LS3137

Hi TI,

In this link

I can find the lwIP Library code for TMS570LC4357.

This page tells that this library will support the entire library stack. So It has support for UDP Communication. But how to establish the UDP Communication using this library? I can find the UDP API's for this purpose in UDP.h file. But when I tried to use them, instead of UDP Frame, I am getting ARP Frame when calling UDP_Send() function. Please anybody give me simple UDP communication to send the text "Hello World" using this TI lwIP Library.

Please anybody answer this. Lot of people asked the same question. At least a reply to this query may be useful for beginners like me.

Thanks in advance.


Regards,

Karthikeyan.K

  • Karthik,

    I have forwarded your question to the Halcogen team. They will get back to you shortly.

    Thanks,

    Zhaohong
  • Please Gentle men/women,

    Anybody do answering. Please give me a simple hello world program for udp communication using this lwIP Library.

    Thanks in advance.

    Regards,
    Karthikeyan.K
  • Sorry for the late reply.I just found that this demo was actually put together by some other group. We are contacting them for help. It will take some time for them to respond. In meantime, you might want to spend some time studying the source code for this demo to see if it can be modified for your application.

    Thanks and regards,

    Zhaohong
  • Hi Zhaohang Zhang,

    Really thanks for your concern on my questions. Really thanks for that. I spent some time to study that library code and found some logical error (I'm not sure that I may wrong somewhat. ;)  If so please correct me. ). But any how After I made a change in this library code I have my UDP frame transmitted. Anyways, It ate my entire day to find out this. :(

    So I had made some changes in the lwIP library code after tracing the execution of code line by line. After that I can transmit the UDP frame.


    Here I've given my void main code and below I'll will give the changes I've made. Then it will be convenient for you to read my question I think. :)

    /* USER CODE BEGIN (0) */
    #include "lwiplib.h"
    #include"HL_sci.h"
    #include "udp.h"
    #include "lwip/ip_addr.h"
    #include "pbuf.h"
    #include "netif.h"
    #include "HL_emac.h"
    /* USER CODE END */
    
    /* Include Files */
    
    #include "HL_sys_common.h"
    #include "HL_system.h"
    
    /* USER CODE BEGIN (1) */
    extern void EMAC_LwIP_Main (uint8_t * emacAddress);
    /* USER CODE END */
    
    /** @fn void main(void)
    *   @brief Application main function
    *   @note This function is empty by default.
    *
    *   This function is called after startup.
    *   The user can use this function to implement the application.
    */
    
    /* USER CODE BEGIN (2) */
    
    extern err_t hdkif_output(struct netif *netif, struct pbuf *p);
    extern err_t etharp_output(struct netif *netif, struct pbuf *q, ip_addr_t *ipaddr);
    extern err_t hdkif_init(struct netif *netif);
    
    /* USER CODE END */
    
    uint8	emacAddress[6U] = 	{0x00U, 0x08U, 0xEEU, 0x03U, 0xA6U, 0x6CU};
    uint32 	emacPhyAddress	=	1U;
    
    void main(void)
    {
    /* USER CODE BEGIN (3) */
    	sciInit();
    	//EMAC_LwIP_Main(emacAddress);
    	lwIPInit(0, emacAddress, 0x0AA91010, 0xFFFFFF00, 0, IPADDR_USE_STATIC);
    	struct udp_pcb *pcb;
    	err_t error;
    	struct pbuf *p;
    	struct netif hdkNetIF;
    	uint8 data[60],i=0;
    	for(i=0;i<60;i++)
    		data[i]=0xAA;
    
    	p = pbuf_alloc(PBUF_IP, UDP_HLEN, PBUF_RAM);
    
    	p->next=0;
    	p->len=60;
    	p->tot_len=60;
    	p->payload = (void *)data;
    
    	struct ip_addr ip_des;
    	struct ip_addr ip_src;
    	struct ip_addr net_mask;
    	struct ip_addr gw_addr;
    
    
    	ip_src.addr = 0x0AA91010;
    	ip_des.addr = 0x0AA91012;
    	net_mask.addr=0xFFFFFF00;
    	gw_addr.addr=0;
    
    	unsigned int InstNum=0;
    	pcb = udp_new();
    	//(void)netif_add(&hdkNetIF, ip_src, net_mask, gw_addr, &InstNum ,hdkif_init, ip_input);
    
    	error = udp_bind(pcb,&ip_src,8000);
    	error = udp_connect(pcb,&ip_des,8001);
    	error = udp_send(pcb,p);
    	error = udp_send(pcb,p);
    	while(1);
    /* USER CODE END */
    }
    
    /* USER CODE BEGIN (4) */
    /* USER CODE END */
    

    And then I've made a change in lwip-1.4.1/src/netif/etharp.c

    In this window You can see the change that I made. Actually Line No: 917 has the statement like "else {" alone (without quote). So what they check here is whether the message is broadcast or else multi cast or else unicast alone. So in my case I wanna send UDP frame to a specific destination. (So it is also a type of unicast.). But when this else statement get executed, function etharp_query() is getting executed which will turn my entire frame as ARP frame. This where my UDP frame is corrupted as ARP frame. So that I'm getting only ARP frame even when I am sending UDP frame. So I want to make this else part should not get executed. For that only I changed this else statement as else if(0). Then the function etharp_send_ip() is getting executed. So that Now I can transmit exactly UDP frame with all my data fields.

    So Now I can send UDP frame. But even, why they made this part as

    etharp_output() // this is the function getting called by the library when I call the function udp_send() in order to send my own UDP frame.
    {
    if(Broadcast)
    {
     /*Update for Broadcasting...*/
     /*No return statement present here.*/
     }
    else if(Multi-cast)
    {
     /*Update for Multi-casting...*/
     /*No return statement present here.*/
     }
    else
    {
     /*Update for Uni-casting...*/
     /*return statement present here.*/
     return etharp_query(); // this is getting called and this will modify my UDP as ARP Frame.
     }
    
    return etharp_send_ip(); // this is not getting called but this is the function which can sent UDP frame without damaging my data.
    }

    I think now You can understand my problem clearly I think.

    So Please clarify me whether my change is wrong or not and guide me to use this library to establish a Full-Duplex UDP Communication.

    Thanks in advance.

    Regards,

    Karthikeyan.K

  • Hello Karthikeyan,

    This project of yours seems to be great starting point for me. I'm interested in have you finished this? To whom are you sending this UDP datagram? Can you provide me any help with this, I'm newbie in this, and I want to establish communication between two boards over ethernet.

    Thank you in advance!

    Kind regards!

  • Hello Djedjica,

    Have you had a look at the Hercules LWIP Wiki page. It has instructions and example code for a LWIP integration project.

    It is available at this link: processors.wiki.ti.com/.../HALCoGen_Ethernet_Driver_and_lwIP_Integration_Demonstration
  • Hi Djedjica,

    I am done with my UDP Communication. And I am sending this UDP Datagram to host PC. I can help you regarding this.

    After referring the page mentioned by me or Chuck Davenport, you can get some clarity. Feel free to ask questions regarding this.


    Regards,

    Karthikeyan.K

  • Karthik, Ethernet frame uses MAC address for source/destination address. IP frame has IP address. To resolve the unicast address the stack uses ARP to find the MAC address of the host with the IP address you specified as the destination.
  • Hi Karthikeyan,
    I trying to do UDP server application in TMS570LS3137 , here you have configured as client right? if i want to do client what should be the changes in this.
  • hey karthikeyan,

    i was also trying to sen/receive udp data packets.

    i tried the minimal lwip implementation given on this link

    and then i tried udp data transfer as you suggested.

    but udp_send() doesnt work. and it returns error code "-6".

    do you have any idea?

    i am using RM57Lx board btw.

  • Hi Rohit,

    I suspect you have problem with your local ip address configuration. Have a look at below image. Just put breakpoint where I specify, and press F5 ("Step into") and analyse what happens on your own.

    If you found or solved your problem or anything else, please do let me know.

    Thanks,

    Regards,

    Karthikeyan KasiVishwanathan.

  • hello karthikeyan,

    thanks for your reply.
    you are right
    if (!ip_addr_cmp(&(pcb->local_ip), &(netif->ip_addr))) {
    condition does not fail and i exit with error code -6.
    the problem is pcb->local_ip = 0x0A0A0A09 (10.10.10.9) which i set
    and netif->ip_addr = 0x090A0A0A
    byte order is reversed.
    how do i solve this?
  • Hi Rohit,

    In "ethernet/lwip-1.4.1/ports/hdk/lwiplib.c " file line no 150 has the definition following function.
    unsigned int lwIPInit(unsigned int instNum, unsigned char *macArray, unsigned int ipAddr, unsigned int netMask, unsigned int gwAddr, unsigned int ipMode).

    This function only configures NETIF IP address. Probably you called this function to configure your EMAC module and lwIP Stack. Ensure your statement calling this function as follows.
    lwIPInit(0, emacAddress, 0x0A0A0A09, /*NETMASK*/0xFFFF0000,/* GWADDR*/0, IPADDR_USE_STATIC);

    here emacAddress is nothing but an array of characters which holds the MAC Address to be configured in your chip.
    (like char emacAddress[] = {0x00,0x08,0xEE,0x06,0xC6,0x03}; )

    Hope this will help you to program your kit to be able to make udp transfer.

    Thanks,
    Karthikeyan KasiVishwanathan
  • hello karthikeyan,
    UDP send works now thanks..
    i had to reverse the byte order when defining ip_src.addr, ip_des.addr in the main function.

    can you please guide how to recv UDP data?
  • Hi Rohit,
    Glad to hear that your UDP works fine.
    Use Wireshark software (you can download ir for free.) to analyse whatever the frame coming to your PC through internet.
    Before that
    1. Don't forget to update the ARP Table using ARP command in your PC.
    refer this image: ic.ims.hr/.../arp-1.png

    2. Put the destination IP Address to your PC which you use when calling udp_connect(struct udp_pcb *pcb, ip_addr_t *ipaddr, u16_t port) function.
    refer this page: www.howtogeek.com/.../

    Then try sending UDP frame from your controller board to PC. And watch Wireshark window whether the frame is coming or not.

    Regards,
    Karthikeyan Kasi Vishwanathan.
  • hello karthikeyan,

    i have a utility called hercules in which i can receive and send UDP packets.
    and i verified with the utility that i am getting the udp packet at my PC :)
    now how can i recv udp packet on controller? do you have any idea?
  • Hi Rohit,
    You can use Colasoft Packet Builder software to send UDP frame from PC to Controller board. Try for that.

    Regards,
    Karthikeyan Kasi Vishwanthan.
  • Hello Karthikeyan,

    i can send the udp frame using the same utility from my PC.
    but how do i receive it on controller?
  • Hi Bro,
    Folow the steps.

    1. Put a break point in lwIPRxIntHandler(unsigned int instNum) function.
    2. Put breakpoint at the function what you specified to get called by the lwIP stack upon receive at the time of UDP initialisation.
    example: In my case, I have called udp_recv(struct udp_pcb *pcb, udp_recv_fn recv, void *recv_arg) function as follows.
    (void)udp_recv(pcb,OWNUDPReceive,NULL);
    where pcb is,
    struct udp_pcb* pcb;
    where OWNUDPReceive is,

    static void OWNUDPReceive(void *arg, struct udp_pcb *pcb, struct pbuf *p, struct ip_addr *addr, u16_t port)
    {

    }

    3. Trigger UDP Frame transmisstion from PC.

    Your control should come to lwIPRxHandler function.
    And if suppose MAC, IP, Port number are validated, then control will come to OWNUDPReceive() function.

    Try this.

    Regards,
    Karthikeyan.K
  • Hello Karthikeyan,

    the control comes to OWNUDPReceive()
    how should i proceed now?
  • Then all are done bro.
    If control comes to
    static void OWNUDPReceive(void *arg, struct udp_pcb *pcb, struct pbuf *p, struct ip_addr *addr, u16_t port)
    {
    process(p->payload); //payload is the member which holds the base address where your actual data exists.
    pbuf_free(p); //don't forget to call this function.
    }

    where process() can be defined as follows.
    void process(uint8 *Ptr)
    {

    }

    i.e:
    If payload member is equal to 0x08000100, then it means that your first byte is available at 0x08000100 th location of your RAM memory.
  • Hi karthikeyan,

    recv also works, this is awesome everything is taken care by interrupt
    thanks a lot for your help :)
  • Hi Rohit,
    Happy to hear your Receive function works.
    If this answers you, please click on "Verify Answer".

    Regards,
    Karthikeyan Kasi Vishwishvanathan.
  • hey bro,
    there is no button saying "verify answer" but i only see "like" "reply".
  • Hi ,
    Can you please guide me to do udp communication by using halcogen ?
  • hello abhijith,

    follow the exact steps mentioned in this post from the begining , and you will have everything up and running.