Wiznet makers

Arnold

Published May 08, 2026 ©

32 UCC

1 VAR

0 Contests

0 Followers

0 Following

Original Link

How to Build an 8-Socket UDP Client/Server Model with W5500 on STM32F10x?

This STM32F10x project demonstrates UDP communication with the WIZnet W5500 Ethernet controller using WIZnet ioLibrary.

COMPONENTS
PROJECT DESCRIPTION

How to Build an 8-Socket UDP Client/Server Model with W5500 on STM32F10x?

Summary

This STM32F10x project demonstrates UDP communication with the WIZnet W5500 Ethernet controller using WIZnet ioLibrary. The firmware configures W5500 over SPI, assigns static network parameters, opens W5500 sockets in UDP mode, and performs recvfrom() / sendto() loopback testing. W5500’s role is to provide the Ethernet MAC/PHY, hardwired TCP/IP engine, socket layer, and internal packet buffers for a maker-oriented UDP experiment.

What the Project Does

The project tests UDP client and server behavior on STM32F10x with W5500. The article explains that UDP itself is connectionless, so “client” and “server” are application roles rather than protocol-level connection states. In client mode, the example maps W5500 socket 0 through socket 7 to local ports 5000–5007 and sends UDP messages to destination ports 6000–6007 on 192.168.1.190. In server mode, socket 0 is opened in UDP mode and receives datagrams from multiple remote UDP clients.

The firmware path is clear enough for maker debugging. STM32F10x initializes delay, interrupt priority, UART, LED, TIM2, SPI1, W5500 reset/interrupt GPIO, W5500 core state, and static network parameters before entering the UDP loop. The test then uses W5500 socket APIs to open UDP sockets, receive datagrams, print the sender IP/port and payload, and send the received payload back.

Where WIZnet Fits

The WIZnet product used is W5500. In this project, W5500 is the wired Ethernet and UDP socket engine between the STM32F10x firmware and the LAN. The STM32F10x does not implement UDP packet handling from the Ethernet frame level; it drives W5500 through SPI and uses ioLibrary socket functions such as socket(), recvfrom(), and sendto().

W5500 fits this maker project because it exposes a concrete socket model that can be tested with simple PC network tools. The official WIZnet product page lists support for hardwired TCP/IP protocols including TCP, UDP, ICMP, IPv4, ARP, IGMP, and PPPoE, 8 simultaneous independent sockets, internal 32 KB Tx/Rx buffer memory, and an embedded 10Base-T/100Base-TX Ethernet PHY.

Implementation Notes

The original article provides code sections rather than a full line-addressable repository view for each source file. The related WIZnet HK repository lists 5.UDP as the UDP loopback example for STM32F10x W5500 projects, and the repository page states MulanPSL-2.0 licensing.

wiz_platform.c — STM32F10x SPI1 interface for W5500

 
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
SPI_Init(SPI1, &SPI_InitStructure);
SPI_Cmd(SPI1, ENABLE);
 

This configures SPI1 as the host interface to W5500. The same article maps PA5 to SCK, PA6 to MISO, PA7 to MOSI, and PA4 to chip select, then registers W5500 chip-select, single-byte SPI, and burst SPI callback functions for ioLibrary.

TestUDPClient.c — UDP receive/send loopback

 
case SOCK_UDP:
    if((size = getSn_RX_RSR(sn)) > 0) {
        ret = recvfrom(sn, buf, size, destip, (uint16_t*)&destport);
        printf("SOCKET%d recv form[%d.%d.%d.%d][%d]: %s\n",
               sn, destip[0], destip[1], destip[2], destip[3], destport, buf);

        ret = sendto(sn, buf, size, destip, destport);
    }
    break;

case SOCK_CLOSED:
    ret = socket(sn, Sn_MR_UDP, local_port[sn], 0x00);
    break;
 

This is the core UDP loopback flow. When the socket is already in UDP mode, the firmware checks the receive buffer size, reads a datagram with recvfrom(), prints the sender address and payload, and echoes it back with sendto(). When the socket is closed, it opens that socket in UDP mode using the socket’s assigned local port.

Practical Tips / Pitfalls

  • Use different local ports when testing multiple UDP sockets. The article uses 5000–5007 for W5500 local ports and 6000–6007 for the PC-side destination ports.
  • Keep socket ownership simple. In UDP client mode, socket 0 through socket 7 are used as separate UDP channels; in server mode, socket 0 can receive datagrams from multiple clients.
  • Static IP is easier for maker testing, but verify that 192.168.1.199 does not conflict with another device on the LAN.
  • Confirm SPI and W5500 version register access before debugging UDP payloads. If SPI is wrong, every UDP symptom becomes misleading.
  • When using a PC network debug tool, select the correct target IP and port before sending. The article notes that forgetting this can make W5500 appear broken even when the firmware is correct.
  • Avoid common test ports if unexpected packets appear. The author observed traffic on port 5001 and removed the issue by changing local ports to 4000–4007

FAQ

Q: Why use W5500 for this STM32F10x UDP project?
A: W5500 provides a hardware socket model for UDP over Ethernet, so the STM32F10x can focus on SPI control, socket selection, buffer handling, and application logic. For maker projects, this makes UDP behavior visible without requiring a full Ethernet driver and software TCP/IP stack.

Q: How does W5500 connect to the STM32F10x?
A: The article uses SPI1 for W5500 access. PA5 is configured as SCK, PA6 as MISO, PA7 as MOSI, and PA4 as chip select; PC5 is used for W5500 reset and PC4 for W5500 interrupt input.

Q: What role does W5500 play in this UDP client/server model?
A: W5500 provides the UDP socket channels. In client mode, the firmware opens up to eight UDP sockets and maps them to different local ports. In server mode, socket 0 can receive datagrams from multiple remote clients because UDP does not require a per-client connection state.

Q: Can beginners follow this project?
A: Yes, if they have basic STM32 GPIO, SPI, UART, and IP address knowledge. The project is practical for makers because a PC UDP test tool can immediately show whether each W5500 socket is sending and receiving datagrams.

Q: Why is there no comparison section against Wi-Fi, LwIP, or ENC28J60?
A: This article is mainly a W5500 UDP socket exercise. The useful comparison is inside the code itself: one W5500 socket can act as a UDP server receiver, while up to eight sockets can be opened as separate UDP client-style channels for testing.

Source

Original article: CSDN, “测试W5500的第4步_使用ioLibrary库创建UDP客户端和服务器端,” first published on May 21, 2025 and modified on May 23, 2025. The article page states CC 4.0 BY-SA licensing.

Related source repository: WIZnet HK STM32F10x_W5500_Examples, including 5.UDP for UDP loopback testing. The repository page states MulanPSL-2.0 licensing.

Product reference: WIZnet W5500 product page for hardwired protocol, socket, buffer, and PHY features.

Tags

#W5500 #WIZnet #STM32F10x #UDP #UDPLoopback #SPI #ioLibrary #SocketProgramming #EmbeddedEthernet #Maker #EthernetDebug


STM32F10x에서 W5500으로 8소켓 UDP 클라이언트/서버 모델을 구현하는 방법

요약

이 STM32F10x 프로젝트는 WIZnet W5500 이더넷 컨트롤러와 WIZnet ioLibrary를 사용해 UDP 통신을 테스트합니다. 펌웨어는 SPI로 W5500을 설정하고, 정적 네트워크 파라미터를 적용하며, W5500 소켓을 UDP 모드로 열고, recvfrom() / sendto() 기반 loopback 테스트를 수행합니다. W5500은 메이커용 UDP 실험에서 Ethernet MAC/PHY, 하드웨어 TCP/IP 엔진, 소켓 계층, 내부 패킷 버퍼 역할을 담당합니다.

프로젝트가 하는 일

이 프로젝트는 STM32F10x와 W5500으로 UDP 클라이언트 및 서버 동작을 테스트합니다. 원문은 UDP가 연결을 만들지 않는 프로토콜이므로 “클라이언트”와 “서버”가 프로토콜 수준의 연결 상태라기보다 애플리케이션에서 정한 역할에 가깝다고 설명합니다. 클라이언트 모드에서는 W5500 소켓 0부터 소켓 7까지를 로컬 포트 5000–5007에 매핑하고, 192.168.1.190의 목적지 포트 6000–6007로 UDP 메시지를 전송합니다. 서버 모드에서는 소켓 0을 UDP 모드로 열고 여러 원격 UDP 클라이언트의 datagram을 수신합니다.

펌웨어 흐름은 메이커가 디버깅하기 쉽게 구성되어 있습니다. STM32F10x는 delay, 인터럽트 우선순위, UART, LED, TIM2, SPI1, W5500 reset/interrupt GPIO, W5500 내부 상태, 정적 네트워크 파라미터를 초기화한 뒤 UDP 루프에 들어갑니다. 테스트는 W5500 소켓 API를 사용해 UDP 소켓을 열고, datagram을 수신하며, 송신자 IP/port와 payload를 출력한 뒤 수신한 payload를 다시 전송합니다.

WIZnet이 들어가는 위치

이 프로젝트에서 사용된 WIZnet 제품은 W5500입니다. W5500은 STM32F10x 펌웨어와 LAN 사이에서 유선 이더넷 및 UDP 소켓 엔진으로 동작합니다. STM32F10x는 Ethernet frame 수준의 UDP 처리를 직접 구현하지 않고, SPI로 W5500을 제어하며 ioLibrary의 socket(), recvfrom(), sendto() 같은 소켓 함수를 사용합니다.

W5500은 단순한 PC 네트워크 도구로 바로 테스트할 수 있는 구체적인 소켓 모델을 제공하기 때문에 이 메이커 프로젝트에 적합합니다. WIZnet 공식 제품 페이지는 W5500이 TCP, UDP, ICMP, IPv4, ARP, IGMP, PPPoE 같은 하드웨어 TCP/IP 프로토콜, 8개 동시 독립 소켓, 32 KB 내부 Tx/Rx 버퍼 메모리, 내장 10Base-T/100Base-TX Ethernet PHY를 지원한다고 설명합니다.

구현 참고 사항

원문은 각 소스 파일을 완전한 저장소 뷰로 제공하기보다 코드 섹션 형태로 설명합니다. 관련 WIZnet HK 저장소에는 STM32F10x W5500 프로젝트용 UDP loopback 예제인 5.UDP가 포함되어 있으며, 저장소 페이지에는 MulanPSL-2.0 라이선스가 명시되어 있습니다.

wiz_platform.c — W5500 접근을 위한 STM32F10x SPI1 인터페이스

 
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
SPI_Init(SPI1, &SPI_InitStructure);
SPI_Cmd(SPI1, ENABLE);
 

이 코드는 SPI1을 W5500 호스트 인터페이스로 설정합니다. 같은 글에서는 PA5를 SCK, PA6을 MISO, PA7을 MOSI, PA4를 chip select로 매핑합니다. 이후 ioLibrary가 사용할 W5500 chip-select, 단일 바이트 SPI, burst SPI 콜백 함수도 등록합니다.

TestUDPClient.c — UDP 수신/송신 loopback

 
case SOCK_UDP:
    if((size = getSn_RX_RSR(sn)) > 0) {
        ret = recvfrom(sn, buf, size, destip, (uint16_t*)&destport);
        printf("SOCKET%d recv form[%d.%d.%d.%d][%d]: %s\n",
               sn, destip[0], destip[1], destip[2], destip[3], destport, buf);

        ret = sendto(sn, buf, size, destip, destport);
    }
    break;

case SOCK_CLOSED:
    ret = socket(sn, Sn_MR_UDP, local_port[sn], 0x00);
    break;
 

이 부분이 UDP loopback 흐름의 핵심입니다. 소켓이 이미 UDP 모드이면 펌웨어는 수신 버퍼 크기를 확인하고, recvfrom()으로 datagram을 읽고, 송신자 주소와 payload를 출력한 뒤 sendto()로 다시 echo 전송합니다. 소켓이 닫혀 있으면 해당 소켓 번호에 할당된 로컬 포트로 UDP 소켓을 엽니다.

실전 팁과 주의할 점

  • 여러 UDP 소켓을 테스트할 때는 서로 다른 로컬 포트를 사용해야 합니다. 원문은 W5500 로컬 포트 5000–5007, PC 측 목적지 포트 6000–6007을 사용합니다.
  • 소켓 역할을 단순하게 유지하는 것이 좋습니다. UDP 클라이언트 모드에서는 소켓 0부터 소켓 7까지를 별도 UDP 채널로 사용하고, 서버 모드에서는 소켓 0이 여러 클라이언트의 datagram을 받을 수 있습니다.
  • 정적 IP는 메이커 테스트에 편하지만, 192.168.1.199가 같은 LAN의 다른 장치와 충돌하지 않는지 확인해야 합니다.
  • UDP payload를 디버깅하기 전에 SPI와 W5500 version register 접근을 먼저 확인해야 합니다. SPI가 틀리면 모든 UDP 증상이 잘못 해석됩니다.
  • PC 네트워크 디버그 도구를 사용할 때는 전송 전에 올바른 대상 IP와 포트를 선택해야 합니다. 원문도 이 설정을 놓치면 W5500이 데이터를 받지 못해 펌웨어 문제로 오해할 수 있다고 설명합니다.
  • 예상치 못한 패킷이 들어오면 흔한 테스트 포트를 피하는 것이 좋습니다. 원문 작성자는 5001 포트에서 예상치 못한 트래픽을 관찰했고, 로컬 포트를 4000–4007로 변경한 뒤 문제가 사라졌다고 설명합니다. 

FAQ

Q: 이 STM32F10x UDP 프로젝트에서 왜 W5500을 사용하나요?
A: W5500은 Ethernet UDP를 위한 하드웨어 소켓 모델을 제공합니다. 따라서 STM32F10x는 SPI 제어, 소켓 선택, 버퍼 처리, 애플리케이션 로직에 집중할 수 있습니다. 메이커 프로젝트에서는 전체 Ethernet 드라이버와 소프트웨어 TCP/IP 스택을 직접 구현하지 않아도 UDP 동작을 관찰할 수 있습니다.

Q: W5500은 STM32F10x에 어떻게 연결되나요?
A: 원문은 W5500 접근에 SPI1을 사용합니다. PA5는 SCK, PA6은 MISO, PA7은 MOSI, PA4는 chip select로 설정됩니다. PC5는 W5500 reset, PC4는 W5500 interrupt 입력으로 사용됩니다.

Q: 이 UDP 클라이언트/서버 모델에서 W5500은 어떤 역할을 하나요?
A: W5500은 UDP 소켓 채널을 제공합니다. 클라이언트 모드에서는 최대 8개 UDP 소켓을 열고 서로 다른 로컬 포트에 매핑합니다. 서버 모드에서는 UDP가 클라이언트별 연결 상태를 요구하지 않기 때문에 소켓 0 하나로 여러 원격 클라이언트의 datagram을 받을 수 있습니다.

Q: 초보자도 따라할 수 있나요?
A: STM32 GPIO, SPI, UART, IP 주소 개념을 기본적으로 알고 있다면 따라갈 수 있습니다. PC UDP 테스트 도구로 각 W5500 소켓이 datagram을 송수신하는지 바로 확인할 수 있어 메이커 실습에 적합합니다.

Q: Wi-Fi, LwIP, ENC28J60과 비교하지 않는 이유는 무엇인가요?
A: 이 글의 목적은 W5500 UDP 소켓 사용법 자체를 확인하는 것입니다. 유용한 비교는 코드 안에 있습니다. W5500 소켓 하나는 UDP 서버 수신용으로 사용할 수 있고, 최대 8개 소켓은 별도 UDP 클라이언트형 테스트 채널로 열 수 있습니다.

출처

Original article: CSDN, “测试W5500的第4步_使用ioLibrary库创建UDP客户端和服务器端,” 2025년 5월 21일 최초 게시, 2025년 5월 23일 수정. 원문 페이지에는 CC 4.0 BY-SA 라이선스가 명시되어 있습니다.

Related source repository: WIZnet HK STM32F10x_W5500_Examples, UDP loopback 테스트용 5.UDP 포함. 저장소 페이지에는 MulanPSL-2.0 라이선스가 명시되어 있습니다.

Product reference: WIZnet W5500 제품 페이지. 하드웨어 프로토콜, 소켓, 버퍼, PHY 기능 참고 자료입니다.

Tags

#W5500 #WIZnet #STM32F10x #UDP #UDPLoopback #SPI #ioLibrary #SocketProgramming #EmbeddedEthernet #Maker #EthernetDebug

Documents
Comments Write