• AVR Freaks

Helpful ReplyHot!How to immediately receive incoming packets in a UDP server under ThreadX?

Author
jtron
Starting Member
  • Total Posts : 48
  • Reward points : 0
  • Status: offline
2020/11/01 08:06:21 (permalink)
0

How to immediately receive incoming packets in a UDP server under ThreadX?

I'm developing a UDP server that runs under ThreadX. It must respond quickly to incoming requests, so my UDP server thread calls TCPIP_UDP_SignalHandlerRegister to allow it to immediately receive notification when a UDP packet arrives in the UDP server socket. But I'm confused about how to set up lower layers to support this. What else do I have to do to ensure the UDP server app will be notified immediately when a rx packet arrives?
 
The TCP/IP Stack Libraries Help manual (Manager Module section) says this:
 

In an RTOS environment it is preferred to have the stack manager run in its own thread. This thread will be blocked waiting for network interface events and when such an event occurs it will wake up and process the data. This is a more efficient way of using the CPU resources.

 
I assume this means TCPIP_STACK_Task should be called in a dedicated task as shown in the freertos UDP Server example:
void _TCPIP_STACK_Task(  void *pvParameters  )
{
    while(1)
    {
        TCPIP_STACK_Task(sysObj.tcpip);
        vTaskDelay(1 / portTICK_PERIOD_MS);
    }
}

But in this example TCPIP_STACK_Task is not blocked waiting for network interface events, it just seems to be called periodically.
 
How should I do this, and what else must I do to implement event-driven UDP receives?
#1
rainad
Moderator
  • Total Posts : 1442
  • Reward points : 0
  • Joined: 2009/05/01 13:39:25
  • Location: 0
  • Status: online
Re: How to immediately receive incoming packets in a UDP server under ThreadX? 2020/11/02 06:31:14 (permalink)
4 (1)
The TCPIP_STACK_Task() should be run with fairly high priority to process incoming packets in a timely manner.
For this, the TCPIP_STACK_HandlerRegister() should be called to signal that the stack needs processing time. Once the processing is done the thread goes to sleep until the next signal.
This is how you can eliminate that vTaskDelay() at the end of that thread and run based on events.
 
#2
jtron
Starting Member
  • Total Posts : 48
  • Reward points : 0
  • Status: offline
Re: How to immediately receive incoming packets in a UDP server under ThreadX? 2020/11/02 06:55:13 (permalink)
0
Are you saying I should do something like this?
handlerfunc()
{
    TCPIP_STACK_Task(sysObj.tcpip);
}
 
 
 
AppInit()
{
    TCPIP_STACK_HandlerRegister(handlerfunc);
}

 
(EDIT) Or perhaps something like this?

handlerfunc()
{
    PutSemaphore();
}
 
HighPriorityThread()
{
    while (1) {
        GetSemaphore();
        TCPIP_STACK_Task(sysObj.tcpip);
    }
}
 
AppInit()
{
    TCPIP_STACK_HandlerRegister(handlerfunc);
}

post edited by jtron - 2020/11/02 07:02:38
#3
rainad
Moderator
  • Total Posts : 1442
  • Reward points : 0
  • Joined: 2009/05/01 13:39:25
  • Location: 0
  • Status: online
Re: How to immediately receive incoming packets in a UDP server under ThreadX? 2020/11/02 09:28:48 (permalink) ☄ Helpfulby Luca Pascarella 2020/11/03 22:49:09
0
2nd approach I was thinking of, with the high priority thread calling the TCPIP stack task.
 
The 1st one won't work, the signal handler just needs to do the signaling.
 
#4
jtron
Starting Member
  • Total Posts : 48
  • Reward points : 0
  • Status: offline
Re: How to immediately receive incoming packets in a UDP server under ThreadX? 2020/11/02 11:43:49 (permalink)
0
rainad
2nd approach I was thinking of, with the high priority thread calling the TCPIP stack task.

 
I assume I must also do this for DRV_MIIM_Tasks and possibly other xxx_Tasks functions. Is that correct?
 
If my assumption is correct then I will have to create a dedicated thread and semaphore for each of these functions. Is there some other way to implement event-driven UDP that doesn't use so much memory and resources?
#5
rainad
Moderator
  • Total Posts : 1442
  • Reward points : 0
  • Joined: 2009/05/01 13:39:25
  • Location: 0
  • Status: online
Re: How to immediately receive incoming packets in a UDP server under ThreadX? 2020/11/02 15:16:02 (permalink)
5 (1)
The MIIM driver is used just initially to set up the PHY. After that, with low frequency - 0.5 seconds or so, to check that the PHY hasn't lost the link. So it's pretty low priority, it can stay the way it is.
 
Yes, a semaphore may be expensive. If possible use an event flag - those should be much lighter if implemented correctly and they come in bunches: at least 32 of them because they use just a bit.
I think ThreadX has those. Anyway, pick whatever mechanism is the lightest.
 
 
#6
Jump to:
© 2021 APG vNext Commercial Version 4.5