[STM32H7] Chapter 11 UDP client / server of ThreadX NetXDUO

Posted by MtPHP2 on Tue, 18 Jan 2022 16:22:58 +0100

Latest tutorial Download: ThreadX NetXDUO network protocol stack tutorial update record post, the first 11 chapters have been released (2022-01-03) - UCOS & ucGUI & EMWIN & embos & touchgfx & ThreadX - tough guy embedded Forum - Powered by Discuz!

Chapter 11 UDP client / server of ThreadX NetXDUO

This chapter explains the UDP client implementation of NetXDUO. Before learning this chapter, you must give priority to learning the basic knowledge of UDP transmission control protocol in Chapter 10. With these basic knowledge, this chapter will have twice the effect with half the effort.

catalogue

11.1 important tips for beginners

11.2 UDP API functions

11.2.1} function nx_system_initialize

11.2.2} function nx_packet_pool_create

11.2.3} function nx_ip_create

11.2.4} function nx_arp_enable

11.2.5} function nx_ip_fragment_enable

11.2.6} function nx_udp_enable

11.2.7} function nx_udp_socket_create

11.2.8} function nx_udp_socket_bind

11.2.9} function nx_packet_data_retrieve

11.2.10} function nx_packet_data_extract_offset

11.2.11} function nx_udp_socket_receive

11.2.12} function nx_udp_socket_send

11.2.13} function nx_packet_release

11.3 implementation method of UDP

11.3.1} NetXDUO initialization

11.3.2 UDP implementation

11.3.3 implementation of UDP loopback communication

11.4 commissioning operation steps of network commissioning assistant and board

11.4.1 test the DM916X network port used and pay attention to the jumper cap

11.4.2 green light and yellow light on RJ45 network transformer socket

11.4.3 step 1, set the board IP address

11.4.4 step 2, set the computer IP address

11.4.5 step 3: test whether the ping is successful

11.4.6 step 5: the network debugging assistant creates a UDP server

11.4.7} step 6: connect the UDP Socket at the board end

11.4.8 step 5: UDP loopback test

11.5 experimental routine

11.6 summary

11.1 important tips for beginners

1. Before learning this chapter, make sure you have learned the basic knowledge of Chapter 10.

2. There are a few functions to master in this chapter. You can first learn the basic use, and then deeply understand the precautions in the use of these functions, so as to achieve skilled use.

3. Application and release of UDP Socket packets of ThreadX NetXDUO

  • Function nx_udp_socket_receive will apply for an NX_ The packet is used for receiving. If the user does not use it, the function NX must be used_ packet_ Release.
  • Use function nx_udp_socket_send must have NX applied for_ For packet data, the function NX can be used_ packet_ For allocate application, NX can also be used_ udp_ socket_ Received application.

Here's the special note, function NX_ udp_ socket_ Nx is released after the send call_ packet_ Allocate or nx_udp_socket_receive the requested packet. There is no need for the user to call the function nx_packet_release.

11.2 UDP API functions

11.2.1} function nx_system_initialize

Function prototype:

VOID nx_system_initialize(VOID); 

Function Description:

NetXDUO initialization, this function must be called before all other function calls.

11.2.2} function nx_packet_pool_create

Function prototype:

UINT nx_packet_pool_create(
                          NX_PACKET_POOL *pool_ptr,
                          CHAR *name,
                          ULONG payload_size,
                          VOID *memory_ptr,
                          ULONG memory_size);     

Function Description:

This function is used for packet memory pool creation

Function parameters:

  1. The first parameter is the address of the memory pool control block.
  2. The second parameter is the memory pool name.
  3. The third parameter is the number of bytes per packet in the memory pool. This value must be at least 40 bytes and must be divisible by 4.
  4. The fourth parameter is the data address in the memory pool, which must be aligned with ULONG.
  5. The fifth parameter is the memory pool size.
  6. Return value:
  •  NX_SUCCESS: (0x00) the memory pool was created successfully.
  •  NX_PTR_ERROR: (0x07) the first parameter address is invalid.
  •  NX_SIZE_ERROR: (0x09) the fifth parameter memory pool size is invalid.
  •  NX_CALLER_ERROR: (0x11) the caller of this service is invalid.

Use examples:

  /* Create memory pool */
    status =  nx_packet_pool_create(&pool_0,                 /* Memory pool control block */
                                    "NetX Main Packet Pool",/* Memory pool name */
               1536, /* The size of each packet in the memory pool, in bytes. This value must be at least 40 bytes and must be divisible by 4 */
             (ULONG*)(((int)packet_pool_area + 15) & ~15) ,/* Memory pool address, which must be aligned with ULONG */
               NX_PACKET_POOL_SIZE);                        /* Memory pool size */     

11.2.3} function nx_ip_create

Function prototype:

UINT nx_ip_create(
    NX_IP *ip_ptr, 
    CHAR *name, ULONG ip_address,
    ULONG network_mask, 
    NX_PACKET_POOL *default_pool,
    VOID (*ip_network_driver)(NX_IP_DRIVER *),
    VOID *memory_ptr, 
    ULONG memory_size,
    UINT priority);    

Function Description:

This function creates an IP instance using the IP address, packet memory pool and network driver provided by the user. Note that the network driver will not be called until the IP task is executed.

Function parameters:

  1. The first parameter is the control block pointer to create the IP instance.
  2. The second parameter is the name of the IP instance.
  3. The third parameter is the IP address.
  4. The fourth parameter is the subnet mask
  5. The fifth parameter is the memory pool address.
  6. The sixth parameter is the network card driver address.
  7. The seventh parameter is the IP task stack address
  8. The eighth parameter is the IP task stack size, in bytes.
  9. The ninth parameter is IP task priority.
  10. Return value
  •   NX_SUCCESS: (0x00) IP instance created successfully.
  •   NX_NOT_IMPLEMENTED: (0x4A) NetX Duo library is not configured correctly.
  •   NX_PTR_ERROR: (0x07) invalid IP control block address, network driver function pointer, memory pool address or task stack address.
  •   NX_SIZE_ERROR: (0x09) the provided task stack size is too small.
  •   NX_CALLER_ERROR: (0x11) the caller of this service is invalid.
  •   NX_IP_ADDRESS_ERROR: (0x21) the IP address provided is invalid.
  •   NX_OPTION_ERROR: (0x21) the IP task priority provided is invalid.

Use examples:

/* Instantiated IP */
    status = nx_ip_create(&ip_0,                                                   /* IP Instance control block */                                    
                            "NetX IP Instance 0",                                  /* IP Instance name */     
                            IP_ADDRESS(IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3),    /* IP address */
                            0xFFFFFF00UL,                                          /* Subnet mask */
                            &pool_0,                                               /* Memory pool */
                        nx_driver_stm32h7xx,                                   /* Network card driver */
                            (UCHAR*)AppTaskNetXStk,                                /* IP Task stack address */
                            sizeof(AppTaskNetXStk),                             /* IP Task stack size, in bytes */
                            APP_CFG_TASK_NETX_PRIO);                            /* IP Task priority */

11.2.4} function nx_arp_enable

Function prototype:

UINT nx_arp_enable(
    NX_IP *ip_ptr, 
    VOID *arp_cache_memory,
    ULONG arp_cache_size);    

Function Description:

This function is used to enable ARP address resolution.

Function parameters:

  1.   ip_ptr: IP instance address.
  2.   arp_cache_memory: ARP cache address.
  3.   arp_cache_size: each ARP entry is 52 bytes, so the total number of ARP entries is an integer multiple of 52 bytes.
  4. Return value
  •   NX_SUCCESS: (0x00) ARP enabled successfully.
  •   NX_PTR_ERROR: (0x07) invalid IP instance address or ARP cache address.
  •   NX_SIZE_ERROR: (0x09) the ARP cache memory provided by the user is too small.
  •   NX_CALLER_ERROR: (0x11) the caller of this service is invalid.
  •   NX_ALREADY_ENABLED: (0x15) this component is enabled.

Use examples:

int32_t tcp_sock;

tcp_sock = netTCP_GetSocket (tcp_cb_server);
    
if (tcp_sock > 0) 
{
res = netTCP_Listen (tcp_sock, PORT_NUM);
}

if(netTCP_SendReady(tcp_sock) == true )
{

}

11.2.5} function nx_ip_fragment_enable

Function prototype:

UINT nx_ip_fragment_enable(NX_IP *ip_ptr);

Function Description:

This function enables IPv4 and IPv6 packet segmentation and reorganization. This service is automatically disabled when an IP task is created.

Function parameters:

  1. The first parameter is the IP instance address.
  2. Return value
  •  NX_SUCCESS: (0x00) IP segmentation enabled successfully.
  •  NX_PTR_ERROR: (0x07) invalid IP instance address.
  •  NX_CALLER_ERROR: (0x11) the caller of this service is invalid.
  •  NX_NOT_ENABLED: (0x14) IP segmentation function is not compiled into NetX Duo.

Use examples:

    /* Enable fragment */    
status = nx_ip_fragment_enable(&ip_0);

11.2.6} function nx_udp_enable

Function prototype:

UINT nx_udp_enable(NX_IP *ip_ptr);

Function Description:

This function is used to enable UDP components.

Function parameters:

  1. The first parameter is the IP instance address.
  2. Return value
  •  NX_SUCCESS: (0x00) UDP enabled successfully.
  •  NX_PTR_ERROR: (0x07) invalid IP pointer.
  •  NX_CALLER_ERROR: (0x11) the caller of this service is invalid.
  •  NX_ALREADY_ENABLED: (0x15) this component is enabled.

Use examples:

    /* Enable UDP */
    status =  nx_udp_enable(&ip_0);

11.2.7} function nx_udp_socket_create

Function prototype:

UINT nx_udp_socket_create(
    NX_IP *ip_ptr,
    NX_UDP_SOCKET *socket_ptr, 
    CHAR *name,
    ULONG type_of_service, 
    ULONG fragment,
    UINT time_to_live, 
    ULONG queue_maximum);

Function Description:

This function is used to create a UDP Socket.

Function parameters:

1. The first parameter is the IP instance pointer.

2. The second parameter is the UDP Socket pointer.

3. The third parameter is the UDP Socket name.

4. The fourth parameter is the transmission service type. The supported parameters are as follows:

  •  NX_IP_NORMAL (0x00000000)
  •  NX_IP_MIN_DELAY (0x00100000)
  •  NX_IP_MAX_DATA (0x00080000)
  •  NX_IP_MAX_RELIABLE (0x00040000)
  •  NX_IP_MIN_COST (0x00020000)

5. The fifth parameter specifies whether IP segmentation is allowed. If NX is specified_ FRAGMENT_ Okay (0x0), IP segmentation is allowed. If NX is specified_ DONT_ Fragment (0x4000), IP segmentation will be prohibited.

6. The sixth parameter specifies an 8-bit value to define the number of routers that this packet can pass through before being discarded. The default value is determined by NX_IP_TIME_TO_LIVE specifies.

7. The seventh parameter is the maximum number of UDP Socket messages supported. When the maximum value is reached, the oldest UDP packet will be released when each new packet is received.

8. Return value

  •  NX_SUCCESS: (0x00) UDP Socket created successfully.
  •  NX_OPTION_ERROR: (0x0A) invalid service type, segmentation or lifetime option.
  •  NX_PTR_ERROR: (0x07) invalid IP or Socket pointer.
  •  NX_CALLER_ERROR: (0x11) the caller of this service is invalid.
  •  NX_NOT_ENABLED (0x14) this component has not been enabled.

Use examples:

    /* Create UDP socket */
    ret = nx_udp_socket_create(&ip_0,                 /* IP Instance control block */    
                                &UDPSocket,           /* UDP Control block */ 
                                "UDP Server Socket",  /* UDP name */ 
                                NX_IP_NORMAL,         /* IP Service type */ 
                                NX_FRAGMENT_OKAY,     /* Enable IP segmentation */ 
                                NX_IP_TIME_TO_LIVE,   /*Used to define the number of routers that this packet can pass through before being discarded */ 
                                512);                 /* Number of messages supported */

11.2.8} function nx_udp_socket_bind

Function prototype:

UINT nx_udp_socket_bind(
    NX_UDP_SOCKET *socket_ptr, 
    UINT port,
    ULONG wait_option);

Function Description:

This function is used to bind the port for the created UDP Socket. If the set port number is not available, you can set the waiting time.

Function parameters:

1. The first parameter is the UDP Socket pointer.

2. The second parameter is the bound port, ranging from 1 - 65535. If set to NX_ANY_PORT (0x0000), an available port number will be searched.

3. The third parameter is the definition of waiting time when the port number is unavailable:

  •   NX_NO_WAIT (0x00000000)
  •   NX_WAIT_FOREVER (0xFFFFFFFF)
  • Waiting time: (0x00000001 to 0xFFFFFFFE), unit: ThreadX system clock beat.

4. Return value

  •   NX_SUCCESS: (0x00) binding UDP Socket succeeded.
  •   NX_ALREADY_BOUND: (0x22) this UDP Socket is bound to another UDP port.
  •   NX_PORT_UNAVAILABLE: (0x23) port is bound to other sockets.
  •   NX_NO_FREE_PORTS: (0x45) no ports available.
  •   NX_WAIT_ABORTED: (0x1A) passed the call tx_thread_wait_abort abort request.
  •   NX_INVALID_PORT: (0x46) port is invalid.
  •   NX_PTR_ERROR: (0x07) invalid socket pointer.
  •   NX_CALLER_ERROR: (0x11) the caller of this service is invalid.
  •   NX_NOT_ENABLED: (0x14) this component has not been enabled.

Use examples:

   /* UDP Socket Binding port */
    ret = nx_udp_socket_bind(&UDPSocket, DEFAULT_PORT, TX_WAIT_FOREVER);

    if (ret != NX_SUCCESS)
    {
        Error_Handler(__FILE__, __LINE__);   
    }

11.2.9} function nx_packet_data_retrieve

Function prototype:

UINT nx_packet_data_retrieve(
    NX_PACKET *packet_ptr,
    VOID *buffer_start,
    ULONG *bytes_copied);

Function Description:

This function copies the data in the supplied packet to the supplied buffer. The actual number of bytes copied is determined by the formal parameter bytes_ The storage unit pointed to by copied is returned.

Note that this function does not change the internal state of the packet. The retrieved data still exists in the packet.

Function parameters:

  1. The first parameter is a pointer to the source packet
  2. The second parameter is the address of the destination packet.
  3. The third parameter is the number of bytes to be copied and the storage address.
  4. Return value: return the following status values:
  •   NX_SUCCESS: (0x00) successfully copied packet data.
  •   NX_INVALID_PACKET: (0x12) invalid packet.
  •   NX_PTR_ERROR: (0x07) invalid parameter address.

matters needing attention:

The destination buffer must be large enough to hold the contents of the packet. Otherwise, the memory will be damaged, resulting in unpredictable results.

Use examples:

/* Get the data from the client */
 nx_packet_data_retrieve(data_packet,    /* Received packets */
                      data_buffer,    /* Parse out data */
                      &bytes_read);   /* data size */

11.2.10} function nx_packet_data_extract_offset

Function prototype:

UINT nx_packet_data_extract_offset(
    NX_PACKET *packet_ptr,
    ULONG offset,
    VOID *buffer_start,
    ULONG buffer_length,
    ULONG *bytes_copied);

Function Description:

This function copies the data in the NetX Duo packet to the specified buffer. You can specify the offset position of the data to be copied. The number of bytes actually copied is in bytes_ Returned from copied. This function does not delete data from the packet, nor does it adjust the leading pointer or other internal state information.

Function parameters:

  1. The first parameter is the packet pointer.
  2. The second parameter is the offset address of the packet.
  3. The third parameter is the buffer address to be saved after copying.
  4. The fourth parameter is the number of bytes to copy.
  5. The fifth parameter is the number of bytes actually copied
  6. Return value: return the following status values:
  •   NX_SUCCESS: (0x00) successfully copied packet data.
  •   NX_PACKET_OFFSET_ERROR (0x53) provided an invalid offset value.
  •   NX_PTR_ERROR: (0x07) invalid parameter address.

matters needing attention:

The destination buffer must be large enough to hold the contents of the packet. Otherwise, the memory will be damaged, resulting in unpredictable results.

Use examples:

/* Copy the data in the UDP packet to the buffer data_buffer */
  nx_packet_data_extract_offset(RecPacket,            /* data packet */
                                0,                    /* Packet address offset */
                                data_buffer,          /* Target buffer */
                                sizeof(data_buffer),  /* Target buffer size */
                                &bytes_read);         /* Bytes of data replication */

11.2.11} function nx_udp_socket_receive

Function prototype:

UINT nx_udp_socket_receive(
    NX_UDP_SOCKET *socket_ptr,
    NX_PACKET **packet_ptr,
    ULONG wait_option);

Function Description:

This function is used to receive UDP data from the specified Socket. If there is no queued data on the specified Socket, the caller will suspend according to the provided wait option parameters.

Function parameters:

1. The first parameter is the UDP Socket pointer

2. The second parameter is UDP packet pointer.

3. The third parameter is the processing when there is no data on the Socket queue:

  •   NX_NO_WAIT (0x00000000).
  •   NX_WAIT_FOREVER (0xFFFFFFFF).
  • Timeout value in clock cycles (0x00000001 to 0xFFFFFFFE).

4. Return value:

  •   NX_SUCCESS: (0x00) Socket data received successfully.
  •   NX_NOT_BOUND: (0x24) Socket is not bound.
  •   NX_NO_PACKET: (0x01) no data received.
  •   NX_WAIT_ABORTED: (0x1A) by calling tx_thread_wait_abort abort pending.
  •   NX_PTR_ERROR: (0x07) invalid socket pointer or return packet pointer.
  •   NX_CALLER_ERROR: (0x11) the caller of this service is invalid.
  •   NX_NOT_ENABLED: (0x14) this component has not been enabled.

matters needing attention:

  1. If NX is returned_ Success, the application is responsible for releasing the packet when it is no longer necessary to receive it.

Use examples:

/* receive data  */
ret = nx_udp_socket_receive(&UDPSocket, &RecPacket, TX_WAIT_FOREVER);

11.2.12} function nx_udp_socket_send

Function prototype:

UINT nx_udp_socket_send(
    NX_UDP_SOCKET *socket_ptr,
    NX_PACKET *packet_ptr,
    ULONG ip_address, 
    UINT port);

Function Description:

This function is used to send UDP Socket data. Note that this service will return immediately whether the UDP datagram has been successfully sent or not..

Function parameters:

  1. The first parameter is the UDP Socket handle.
  2. The second parameter is the UDP packet pointer.
  3. The third parameter is the destination address.
  4. The fourth parameter is the target port number, ranging from 1 to 65535.
  5. Return value: return the following status values:
  •   NX_SUCCESS: (0x00) Socket sent successfully.
  •   NX_NOT_BOUND: (0x24) Socket is not bound to any port.
  •   NX_NO_INTERFACE_ADDRESS: (0x50) no suitable outgoing interface was found.
  •   NX_TX_QUEUE_DEPTH: (0x49) the maximum transmission queue depth has been reached.
  •   NX_OVERFLOW: (0x03) invalid packet append pointer.
  •   NX_UNDERFLOW: (0x02) invalid packet leading pointer.
  •   NX_PTR_ERROR: (0x07) invalid socket pointer.
  •   NX_CALLER_ERROR: (0x11) the caller of this service is invalid.
  •   NX_NOT_ENABLED: (0x14) this component has not been enabled.

Use examples:

/* Send packet to UDP sender */
ret =  nx_udp_socket_send(&UDPSocket, TraPacket, source_ip_address, source_port);

11.2.13} function nx_packet_release

Function prototype:

UINT nx_packet_release(NX_PACKET *packet_ptr);

Function Description:

This function releases packets, including any other packets linked to the specified packet. If there are other tasks waiting for this packet, the task will get the packet and continue to execute.

Function parameters:

  1. The first parameter is the packet address.
  2. Return value: return the following status values:
  •   NX_SUCCESS: (0x00) packet release succeeded.
  •   NX_PTR_ERROR: (0x07) invalid packet pointer.
  •   NX_UNDERFLOW: (0x02) the preset pointer is less than the payload start position.
  •   NX_OVERFLOW: (0x03) the append pointer is greater than the payload end position.

matters needing attention:

The application must prevent releasing the same packet multiple times, otherwise it will lead to unpredictable results.

11.3 implementation method of UDP

11.3.1} NetXDUO initialization

Before creating UDP, initialize NetX, create a memory pool, and instantiate IP:

/*
*********************************************************************************************************
*	Function name: NetXTest
*	Function Description: TCPnet application
*	Formal parameters: None
*	Return value: None
*********************************************************************************************************
*/    
void NetXTest(void)
{
    UINT status;
    UINT ret;
    ULONG socket_state;
    UINT old_priority;

    NX_PACKET *data_packet;
    ULONG bytes_read;
	
	ULONG peer_ip_address;
	ULONG peer_port;
    
	
    /* Initialize NetX */
    nx_system_initialize();

    /* Create memory pool */
    status =  nx_packet_pool_create(&pool_0,                 /* Memory pool control block */
                                     "NetX Main Packet Pool",/* Memory pool name */
               1536, /* The size of each packet in the memory pool, in bytes. This value must be at least 40 bytes and must be divisible by 4 */
		     (ULONG*)(((int)packet_pool_area + 15) & ~15) ,/* Memory pool address, which must be aligned with ULONG */
               NX_PACKET_POOL_SIZE);                        /* Memory pool size */                  
          
    /* Detect whether the creation failed */
    if (status) error_counter++;

    /* Instantiated IP */
    status = nx_ip_create(&ip_0,                                                   /* IP Instance control block */                                    
                            "NetX IP Instance 0",                                  /* IP Instance name */     
                            IP_ADDRESS(IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3),    /* IP address */
                            0xFFFFFF00UL,                                          /* Subnet mask */
                            &pool_0,                                               /* Memory pool */
						nx_driver_stm32h7xx,                                   /* Network card driver */
                            (UCHAR*)AppTaskNetXStk,                                /* IP Task stack address */
                            sizeof(AppTaskNetXStk),                             /* IP Task stack size, in bytes */
                            APP_CFG_TASK_NETX_PRIO);                            /* IP Task priority */
                            
            
    /* Detect whether the creation failed */
    if (status) error_counter++;

    /* Enable ARP and provide ARP cache */
    status =  nx_arp_enable(&ip_0,               /* IP Instance control block */
					 (void *)arp_space_area,  /* ARP Cache address */
		 sizeof(arp_space_area));   /* Each ARP entry is 52 bytes, so the total number of ARP entries is an integer multiple of 52 bytes */

    /* Enable fragment */    
    status = nx_ip_fragment_enable(&ip_0);

    /* Detection enable successful */
    if (status) error_counter++;

    /* Enable TCP */
    status =  nx_tcp_enable(&ip_0);

    /* Detection enable successful */
    if (status) error_counter++;

    /* Enable UDP  */
    status =  nx_udp_enable(&ip_0);

    /* Detection enable successful */
    if (status) error_counter++;

    /* Enable ICMP */
    status =  nx_icmp_enable(&ip_0);

    /* Detection enable successful */
    if (status) error_counter++;   
    
    /* NETX After initialization, reset the priority */
    tx_thread_priority_change(netx_thread_ptr, APP_CFG_TASK_NETX_PRIO1, &old_priority);
tx_thread_priority_change(&AppTaskNetXProTCB, APP_CFG_TASK_NetXPro_PRIO1, &old_priority);

    /* ellipsis */
}

At the end of the program, the service priority is specially treated. When creating, it is first set to low priority. After detecting the normal connection of the network cable and initializing the network, the priority is set to the normal level.

11.3.2 UDP implementation

Here is how to create UDP:

/*
*********************************************************************************************************
*	Function name: NetXTest
*	Function Description: TCPnet application
*	Formal parameters: None
*	Return value: None
*********************************************************************************************************
*/    
void NetXTest(void)
{
  
     /* ellipsis */


    /* Create UDP socket */
    ret = nx_udp_socket_create(&ip_0,                 /* IP Instance control block */    
                                &UDPSocket,           /* UDP Control block */ 
                                "UDP Server Socket",  /* UDP name */ 
                                NX_IP_NORMAL,         /* IP Service type */ 
                                NX_FRAGMENT_OKAY,     /* Enable IP segmentation */ 
                                NX_IP_TIME_TO_LIVE,   /* Used to define the number of routers that this packet can pass through before being discarded */ 
                                512);                 /* Number of messages supported */

    if (ret != NX_SUCCESS)
    {
        Error_Handler(__FILE__, __LINE__);   
    }
    
    /* UDP Socket Binding port */
    ret = nx_udp_socket_bind(&UDPSocket, DEFAULT_PORT, TX_WAIT_FOREVER);

    if (ret != NX_SUCCESS)
    {
        Error_Handler(__FILE__, __LINE__);   
    }

     /* ellipsis */
}

11.3.3 implementation of UDP loopback communication

Loopback means that after the computer network assistant sends data to the board, the board returns the data. For your convenience, this example defines the received packet and the sent packet respectively:

/*
*********************************************************************************************************
*	Function name: NetXTest
*	Function Description: TCPnet application
*	Formal parameters: None
*	Return value: None
*********************************************************************************************************
*/    
void NetXTest(void)
{
  
     /* ellipsis */

       /* receive data  */
        ret = nx_udp_socket_receive(&UDPSocket, &RecPacket, TX_WAIT_FOREVER);

        if (ret == NX_SUCCESS)
        {
            /* Copy the data in the UDP packet to the buffer data_buffer */
            nx_packet_data_extract_offset(RecPacket,            /* data packet */
                                          0,                    /* Packet address offset */
                                          data_buffer,          /* Target buffer */
                                          sizeof(data_buffer),  /* Target buffer size */
                                          &bytes_read);         /* Bytes of data replication */

            /* Get remote port and IP  */
            nx_udp_source_extract(RecPacket, &source_ip_address, &source_port);

            /* Print received data */
            PRINT_DATA(source_ip_address, source_port, data_buffer);

            /* Release packet */
            nx_packet_release(RecPacket);

            /* Request to send packet */
            ret = nx_packet_allocate(&pool_0, &TraPacket, NX_UDP_PACKET, TX_WAIT_FOREVER);

            if (ret)
            {
                Error_Handler(__FILE__, __LINE__);  
            }

            sprintf((char *)sendbuf, "sendbuf = %d\r\n", count++);

            /*Attach the data to be sent to TraPacket */
            ret = nx_packet_data_append(TraPacket, (VOID *)sendbuf, strlen((char *)sendbuf), 
&pool_0, TX_WAIT_FOREVER);

            if (ret)
            {
                Error_Handler(__FILE__, __LINE__);
            }

            /* Send packet to UDP sender */
            ret =  nx_udp_socket_send(&UDPSocket, TraPacket, source_ip_address, source_port);

        }
	}  	

     /* ellipsis */
}

11.4 commissioning operation steps of network commissioning assistant and board

We use the following debugging assistant here. Of course, any other network debugging assistant can be used without limitation:

 http://www.armbbs.cn/forum.php?mod=viewthread&tid=1568 .

11.4.1 test the DM916X network port used and pay attention to the jumper cap

During the test, the network cable shall be plugged into the DM916X network port:

Pay special attention to the position of jumper cap here and short circuit PG11:

 

11.4.2 green light and yellow light on RJ45 network transformer socket

Various network cards, switches and other network devices are different. Generally speaking, the green light is divided into on or off (representing the network speed), and the yellow light is divided into flashing or off (representing whether there is data sending and receiving).

Green light: long light means 100M; Not on means 10M.

Yellow light: long light means no data sending and receiving; Flashing indicates data transmission and reception.

The lights of some gigabit network cards are distinguished by color. If they are not on, it means 10M / green means 100M / yellow means 1000M. At present, the 10M network is basically invisible. If one light is on for a long time, it basically indicates that the 100m network is or higher, while the other light flashes from time to time, it means that there is data transceiver. The specific depends on the network equipment. Even some low-level network cards, such as TP-LINK, have only one light. When it is on, it represents connectivity, and flashing represents data sending and receiving.

For the light on the RJ45 network transformer socket on the development board, the green light represents data transmission and reception, the long light indicates no data transmission and reception, and the flashing represents data transmission and reception. The yellow light represents the network speed, the long light represents 100M, and the non light represents 10M.

11.4.3 step 1, set the board IP address

We use fixed IP (or static IP), which is easy to set. Here we will explain the direct connection between the development board and the computer, that is, directly connect the network port of the development board with the network port of the computer through a network cable. If you are using a laptop, it is strongly recommended to prohibit the WIFI network of the laptop during the test, and all kinds of agent software and virtual network cards are temporarily closed. After the test, open it one by one to see if there is a problem.

For the fixed IP mode, it can also be connected to the router or switch for test. Pay special attention to that the IP address set on the board does not conflict with the IP of other devices on the router or switch. In the test stage, it is still recommended to use the computer direct connection mode, and then run other modes after running through.

In the file demo_ dm9162_ netx. Set the IP address in H, and the specific configuration is as follows (you can update your own situation and modify it):

/*
*********************************************************************************************************
*                                        IP relevant
*********************************************************************************************************
*/
#define DEFAULT_ Port 1000 / * TCP server listening port number*/

#define IP_ADDR0                        192
#define IP_ADDR1                        168
#define IP_ADDR2                        28
#define IP_ADDR3                        245   

11.4.4 step 2, set the computer IP address

The IP address of the computer must be set to the same IP segment as the development board, that is, 192.168.28 X. In step 2, the IP of the development board has been set to 192.168.28.245. Here, we will set the IP of the computer to 192.168.28.221. This is WIN7 64bit system.

(1) Right click the "network" icon on the desktop and select properties.

(2) select "local connection" in the pop-up interface

(3) select "properties"

(4) double click the "Internet Protocol version 4(TCP/Ipv4)" option.

(5) configure IP address, subnet mask and default gateway. DNS does not need to be configured. Remember to click OK

(6) after clicking the "OK" button, return to the previous interface. Don't forget to click the "OK" button here:

 

11.4.5 step 3: test whether the ping is successful

Download the routine to the development board, and then ping 192.168.28.245 to see if it is connected.

(1) Press WIN+R to open the "run" window and enter cmd.

(2) enter ping 192.168.28.245 and press enter.

The sending and receiving are the same, and there is no data loss, indicating that the ping command is also successful.

11.4.6 step 5: the network debugging assistant creates a UDP server

Open the debugging assistant and click Create server in the upper left corner:

The following interface pops up. The specified IP setting is 192.168.28.245, which must be consistent with the board end IP address set in step 2, port number 1000, and finally click OK:

The interface effect after creation is as follows:

 

11.4.7} step 6: connect the UDP Socket at the board end

Click Connect UDP Socket:

Effect after normal connection:

 

11.4.8 step 5: UDP loopback test

After the board and the network debugging assistant establish a connection, they can send and receive data from each other.

A simple display of the characters received at the board end (baud rate 115200, data bit 8, parity bit none, stop bit 1):

 

11.5 experimental routine

Supporting examples:

V7-2404_ThreadX NetXDUO UDP

Purpose of the experiment:

  1. Learn ThreadX NetXDUO UDP implementation

Experiment content:

  1. The following tasks are created. You can print the usage of the task stack through the serial port by pressing the key K1

          ======================================================

OS CPU Usage =  1.31%           =======================================================

Task priority task stack size current stack used  maximum stack used  task name

           Prio     StackSize   CurStack    MaxStack   Taskname

           2         4092        303         459      App Task Start

           30         1020        303         303      App Task STAT

           31         1020        87          71      App Task IDLE

           5          4092        311         551      App Msp Pro

           7         4092        303         719      App Task UserIF

           6         4092        255         359      App NETX Pro

           3         4092        415         535      NetX IP Instance 0

           0         1020        191         235      System Timer Thread   

Serial port software can use SecureCRT or H7-TOOL RTT to view print information.

App Task Start task: start the task, which is used here as a BSP driver package.

App Task MspPro task: message processing.

App Task UserIF task: key message processing.

App Task COM task: used here as LED flashing.

System Timer Thread task: system timer task

Operating instructions:

  1. Since the program uses the DWT clock cycle counter, please power on the board again after downloading the program to prevent the DWT clock cycle counter from being reset normally.
  2. NetX network protocol stack operation:

(1) The default IP address is 192.168.28.245, which is displayed in the demo_dm9162_netx.c, which can be modified as needed.

(2) You can create a TCP server with network debugging software on the computer, with port number 1001.

(3) A simple loop communication is realized. The user uses the data sent by the host computer, and then the board returns other data.

Serial port printing information mode (AC5, AC6 and IAR):

Baud rate 115200, data bit 8, parity bit none, stop bit 1

 

11.6 summary

This chapter explains so much for you. I hope you can do more tests and master the use of these API functions.

Topics: server stm32 udp