Hardwired TCP/IP Chapter 5: UDP Example W55MH32
Hardwired TCP/IP Chapter 5: UDP Example W55MH32

Hardwired TCP/IP Chapter 5: UDP Example W55MH32
In this article, we will provide a detailed explanation on how to implement UDP communication on the W55MH32 chip. Through practical examples, we will also explain to you how to use UDP for data loopback testing.
Other network protocols used in this example, such as DHCP, please refer to the relevant sections. Regarding the initialization process of W55MH32, please also refer to the Network Install section. We will not elaborate on this here.
1 Introduction to the UDP Protocol
User Datagram Protocol (UDP, User Datagram Protocol) is one of the core protocols in the transport layer of the OSI model. It is a connectionless, datagram-based transmission protocol used for efficient data transmission in IP networks. Compared with TCP protocol, UDP is simpler and lighter, providing only the most basic transmission functions.
2 Characteristics of the UDP Protocol
Disconnection: UDP does not require establishing a connection before transmitting data. The sender can directly send data to the destination address, and the receiver does not need to confirm the receipt of the data.
Independence of data packets: Data is sent in the form of independent data packets, and each data packet contains complete message information.
Efficiency: UDP does not have flow control, congestion control, or retransmission mechanisms, so it has low overhead and high transmission efficiency, making it suitable for applications with high real-time requirements.
Unreliability: UDP does not guarantee reliable data transmission. Data may be lost, duplicated, or out of order.
Support for broadcasting and multicasting: UDP can perform broadcast and multicast data transmission, making it very suitable for one-to-many communication scenarios.
Simple header format: The UDP header is only 8 bytes, including the source port, destination port, length, and checksum.
3 UDP application scenarios
Next, let's explore what operations and applications can be accomplished using the UDP protocol on the W55MH32?
- Real-time data transmission: Use the W55MH32 in industrial automation control and network cameras to achieve real-time data transmission.
- Device discovery and automatic configuration: Implement device discovery and automatic configuration by sending broadcast packets to the W55MH32.
- Broadcast and multicast communication: Use broadcast and multicast communication methods to achieve one-to-many communication. For example, status synchronization and multi-device control.
- Data acquisition: Suitable for scenarios where high-frequency transmission of small data packets is required. For instance, status updates of temperature and humidity sensors.
4 UDP message transmission process
1. Send ARP message to confirm that the other party's address is online.
2. Obtain data information and transmit it via UDP.
3. Add UDP header information.
4. The data packet is transmitted through the IP protocol to the destination address.
5. The receiving party receives the data packet from the destination port.
5 UDP protocol message
• Source Port: The port number of the sender;
• Destination Port: The port number of the receiver;
• Message Length: The total length of the UDP message, including the header and data, measured in bytes.
• Checksum: Checksum;
UDP message example:
C0A8 0001 C0A8 0002 1F90 1F91 0021 F7DF 4865 6C6C 6F2C 2055 4450 21
1.IP section (first 16 bytes):
a) C0A8 0001: Source IP address (192.168.0.1)
b) C0A8 0002: Destination IP address (192.168.0.2)
2.UDP Section (the latter part):
a) 1F90: Source port number (8080)
b) 1F91: Destination port number (8081)
c) 0021: Message length (33 bytes)
d) F7DF: Checksum (for data integrity verification)
e) 4865 6C6C 6F2C 2055 4450 21: Data section ("Hello, UDP!" in ASCII representation)
In W55MH32, the UDP protocol has been implemented in the internal hardware protocol stack. Therefore, we only need to read and write the values of the corresponding registers to achieve data transmission and reception, without the need for manual packet formation.
6 The implementation process
Next, we conduct the UDP loopback test on W55MH32. The function loopback_udps() is the one that implements the UDP loopback test functionality.
Note: The test instances require that the PC and W55MH32 be on the same network segment.
This function needs to be called within the main loop, as shown in the figure below.
1. while (1)
2. {
3. loopback_udps(SOCKET_ID, ethernet_buf, local_port);
4. }
The loopback_udps() function requires three parameters to be passed in, namely the socket number, the socket cache, and the source port number.
The content of the loopback_udps() function is as follows:
1. /**
2. * @brief udp server mode loopback test
3. * @param sn: socket number
4. * @param buf: Data sending and receiving cache
5. * @param port: Local port
6. * @return value for SOCK_ERRORs,return 1:no error
7. */
8. int32_t loopback_udps(uint8_t sn, uint8_t *buf, uint16_t port)
9. {
10. int32_t ret;
11. uint16_t size, sentsize;
12. uint8_t destip[4];
13. uint16_t destport;
14.
15. switch (getSn_SR(sn))
16. {
17. case SOCK_UDP:
18. if ((size = getSn_RX_RSR(sn)) > 0)
19. {
20. if (size > DATA_BUF_SIZE)
21. size = DATA_BUF_SIZE;
22. ret = recvfrom(sn, buf, size, destip, (uint16_t *)&destport);
23. buf[ret] = 0x00;
24. printf("recv form[%d.%d.%d.%d][%d]: %s\r\n", destip[0], destip[1], destip[2], destip[3], destport, buf);
25. if (ret <= 0)
26. {
27. #ifdef _LOOPBACK_DEBUG_
28. printf("%d: recvfrom error. %ld\r\n", sn, ret);
29. #endif
30. return ret;
31. }
32. size = (uint16_t)ret;
33. sentsize = 0;
34. while (sentsize != size)
35. {
36. ret = sendto(sn, buf + sentsize, size - sentsize, destip, destport);
37. if (ret < 0)
38. {
39. #ifdef _LOOPBACK_DEBUG_
40. printf("%d: sendto error. %ld\r\n", sn, ret);
41. #endif
42. return ret;
43. }
44. sentsize += ret; // Don't care SOCKERR_BUSY, because it is zero.
45. }
46. }
47. break;
48. case SOCK_CLOSED:
49. #ifdef _LOOPBACK_DEBUG_
50. // printf("%d:UDP loopback start\r\n",sn);
51. #endif
52. if ((ret = socket(sn, Sn_MR_UDP, port, 0x00)) != sn)
53. return ret;
54. #ifdef _LOOPBACK_DEBUG_
55. printf("%d:Opened, UDP loopback, port [%d]\r\n", sn, port);
56. #endif
57. break;
58. default:
59. break;
60. }
61. return 1;
62. }
After entering the loop test program, a socket state machine will be executed. Within the socket state machine, the following steps will be carried out:
Step 1: Open the socket
When the socket is in the SOCK_CLOSED state (i.e., closed), set the socket to UDP mode and open it.
Step 2: Listen for data reception and loop back the data
When the socket is in the SOCK_UDP state (i.e., the socket has been opened and configured as UDP mode), listen to the Sn_RX_RSR register.
When the value of the Sn_RX_RSR register is greater than 0, it indicates that UDP data has been received. First, use the recvfrom() function to extract the received UDP data.
Then, use the sendto() function to loop back and send the received data.
7 Run results
After the burning routine is executed, the PHY link detection is carried out first, followed by the DHCP process for obtaining the network address result, and finally the UDP loopback test is conducted, as shown in the following figure:
Next, we will open a network debugging tool, such as SocketTester, set it to the UDP mode, select your own IP address and port, then open the socket, and then set the target address and target port to the IP address and port of W55MH32. Finally, send data to W55MH32 for the loopback test process.
It can be seen that on the W55MH32 side, the data sent from the PC end was received and printed out. The SocketTester interface also displayed the loopback data from W55MH32, as shown in the following figure:
When sending data to W55MH32 using the UDP broadcast mode, all you need to do is change the destination address on the SocketTester to the broadcast address of the network segment where W55MH32 is located (192.168.1.255). Here is an example diagram of sending UDP broadcast to W55MH32:
8 Summary
This article introduces the methods for implementing UDP communication and data loopback testing on the W55MH32 chip. It elaborates on the concepts, characteristics, application scenarios, message transmission process and message structure of the UDP protocol, presents the implementation process, and completes the tests using network debugging tools.
The next article will explain the implementation of UDP multicast communication and loopback testing on this chip, introducing the relevant principles and implementation steps. Please stay tuned!
WIZnet is a non-fabrication semiconductor company founded in 1998. Its products include the Internet processor iMCU™, which adopts TOE (TCP/IP Offloading Engine) technology and is based on a unique patented fully hardwired TCP/IP. iMCU™ is designed for embedded Internet devices in various applications.
WIZnet has over 70 distributors worldwide, with offices in Hong Kong, South Korea, and the United States, providing technical support and product marketing.
The region managed by the Hong Kong office includes: Australia, India, Turkey, and Asia (excluding South Korea and Japan).