How to Run an FTP Server with WIZnet W5500 on STM32F10x?
This STM32F10x maker project builds an FTP server with the WIZnet W5500 Ethernet controller and WIZnet ioLibrary.
How to Run an FTP Server with WIZnet W5500 on STM32F10x?
Summary
This STM32F10x maker project builds an FTP server with the WIZnet W5500 Ethernet controller and WIZnet ioLibrary. The STM32 initializes W5500 over SPI, configures TCP sockets for FTP control and data transfer, connects the FTP daemon to a FATFS-backed file layer, and verifies upload/download behavior with FileZilla. W5500’s role is the wired TCP/IP socket engine that carries FTP control commands and file payloads between the MCU board and a PC.
What the Project Does
The project demonstrates file transfer between an STM32F10x board and a PC using FTP over Ethernet. FTP uses a TCP control connection, typically port 21, and a separate TCP data connection, typically port 20, so the example is more complex than a single-socket TCP echo test. The article explains both active and passive FTP modes and then adapts WIZnet ioLibrary’s FTP server files for the STM32 + W5500 environment.
The firmware stack combines several layers: STM32 board initialization, CPU Flash and FATFS-style file operations, W5500 SPI platform support, W5500 network configuration, FTP command parsing, and FileZilla-based PC testing. The author states that ftpd.c and ftpd.h came from the ioLibrary FTP Server library but required modification, including adding scan_files() and changing ftpd.c behavior after FileZilla testing.
The tested flow is: initialize the file workspace, create/read sample files in CPU Flash, initialize W5500, set local network information, open a TCP socket, initialize the FTP daemon, and repeatedly run ftpd_run() while FileZilla connects from the PC. The article reports that the modified version can transfer a single file in both directions for the test purpose.
Where WIZnet Fits
The exact WIZnet product is W5500. In this project, W5500 is the Ethernet controller and TCP/IP offload device between the STM32F10x firmware and the LAN. The STM32 handles file-system operations, FTP command parsing, UART logs, and application state. W5500 handles the Ethernet MAC/PHY, TCP socket state, packet buffering, and SPI-accessible socket registers.
This matters because FTP needs multiple TCP paths and persistent socket-state handling. The code uses W5500 sockets for FTP control channels and a data channel: CTRL_SOCK, DATA_SOCK, and CTRL_SOCK1 are mapped to socket numbers 1, 2, and 3, while FTP control and data ports are defined as 21 and 20.
W5500 is a hardwired TCP/IP stack internet controller with SPI up to 80 MHz, embedded 10/100 Ethernet MAC and PHY, TCP/UDP/IPv4 support, 8 independent sockets, and 32 KB internal Tx/Rx buffer memory. WIZnet also documents ioLibrary as an MCU-independent library for W5x00 and W6x00 chips, with services including DHCP, DNS, MQTT, SNTP, TFTP, and HTTP Server.
Implementation Notes
File: ftpd.c
What it configures: FTP login state, working directory, local IP, and FTP control sockets.
Why it matters: FTP is command-driven. The server must be ready to accept control connections on port 21 before it can process USER, PASS, LIST, RETR, STOR, and related commands.
void ftpd_init(uint8_t * src_ip)
{
ftp.state = FTPS_NOT_LOGIN;
ftp.current_cmd = NO_CMD;
ftp.dsock_mode = ACTIVE_MODE;
FTP_Port = 35000;
strcpy(ftp.workingdir, "/");
socket(CTRL_SOCK, Sn_MR_TCP, IPPORT_FTP, 0x0);
socket(CTRL_SOCK1, Sn_MR_TCP, IPPORT_FTP, 0x0);
}This snippet exists to bind W5500 TCP sockets to FTP control behavior. The same source stores username/password state, working directory state, and the local IP used for FTP data-session handling.
File: ftpd.c
What it configures: FTP control-socket state handling with W5500 socket registers.
Why it matters: The server must move between closed, initialized, listening, established, and close-wait states without assuming that the PC client remains connected.
Sn_SR_Value = getSn_SR(CTRL_SOCK);
switch(Sn_SR_Value)
{
case SOCK_ESTABLISHED:
ret = send(CTRL_SOCK, (uint8_t *)dbuf,
strlen((const char *)dbuf));
break;
case SOCK_CLOSED:
socket(CTRL_SOCK, Sn_MR_TCP, IPPORT_FTP, 0x0);
break;
case SOCK_INIT:
listen(CTRL_SOCK);
break;
}This is the core W5500 network-stack pattern in the FTP server. The firmware reads the W5500 socket-state register, sends the FTP banner after connection, recreates the socket if it is closed, and enters listen() when the socket is initialized.
Practical Tips / Pitfalls
- Verify W5500 SPI read/write before testing FTP. The repository’s troubleshooting notes recommend checking SPI access and chip-version reading first.
- Treat FTP as a multi-socket protocol. Control and data paths are separate, so socket allocation and close/reopen behavior must be explicit.
- Keep FATFS and network buffers separate. W5500 provides internal socket buffers, but file read/write still needs MCU-side buffers and careful
_MAX_SShandling. - Test both upload and download with the same client tool. The article used FileZilla and modified
ftpd.cafter client-side testing. - Be careful with active/passive mode behavior. The article notes FileZilla configuration differences around passive settings and server-side transfer behavior.
- Add timeout and recovery paths. The code includes timeout counters and socket disconnect handling, which are necessary because FTP clients may leave control or data sockets open.
FAQ
Q: Why use WIZnet W5500 for an FTP server on STM32F10x?
A: FTP needs reliable TCP sockets for both control commands and file data. W5500 provides hardwired TCP/IP, an embedded 10/100 Ethernet MAC/PHY, 8 sockets, and 32 KB internal Tx/Rx buffer memory, so the STM32 can focus on FATFS, command parsing, and file handling instead of running a full software TCP/IP stack.
Q: How does W5500 connect to the STM32F10x platform?
A: The project initializes SPI1, W5500 reset, and W5500 interrupt pins, then calls wizchip_initialize() to register chip-select, single-byte SPI, and multi-byte SPI functions before using socket APIs. The article also checks the W5500 version and PHY status before applying network settings.
Q: What role does W5500 play in this FTP project?
A: W5500 is the TCP socket engine for the FTP server. It provides the control connection on FTP port 21, supports the data-transfer path, exposes socket states through registers such as Sn_SR, and moves FTP command and file payload data through wired Ethernet.
Q: Can beginners follow this maker project?
A: Yes, but it is best for makers who already understand STM32 peripheral initialization, SPI, TCP sockets, and basic file-system concepts. Compared with a TCP echo example, this project adds FTP command parsing, active/passive transfer behavior, file listing, file upload, and file download.
Q: How does W5500 compare with ENC28J60 for this FTP server use case?
A: W5500 integrates a hardwired TCP/IP stack, 8 independent sockets, and 32 KB internal socket buffer memory, which is useful for FTP’s multiple TCP paths. ENC28J60 is a 10Base-T Ethernet controller with an SPI interface; Microchip’s product page identifies it as a stand-alone Ethernet controller, and ENC28J60-based designs usually require more TCP/IP stack work in the MCU firmware.
Source
Original article: CSDN, “测试W5500的第12步_FTP服务器,” first published on 2025-06-30 and marked as CC 4.0 BY-SA.
Source repository referenced by the article: WIZnet HK, STM32F10x_W5500_Examples, licensed under Mulan PSL v2. The repository describes 18.FTP_Server as an FTP server mode example where other devices can download files by accessing the server.
WIZnet product reference: W5500 documentation and ioLibrary Driver documentation.
Alternative reference for comparison: Microchip ENC28J60 product page.
Tags
#W5500 #WIZnet #STM32F10x #STM32F103 #FTPServer #FileZilla #FATFS #Ethernet #SPI #ioLibrary #EmbeddedC #Maker #NetworkStack #ENC28J60
STM32F10x에서 WIZnet W5500으로 FTP 서버를 실행하는 방법은?
요약
이 STM32F10x 메이커 프로젝트는 WIZnet W5500 이더넷 컨트롤러와 WIZnet ioLibrary를 사용해 FTP 서버를 구현합니다. STM32는 SPI로 W5500을 초기화하고, FTP 제어 및 데이터 전송을 위한 TCP 소켓을 설정하며, FTP 데몬을 FATFS 기반 파일 계층과 연결합니다. 이후 FileZilla를 사용해 업로드와 다운로드 동작을 검증합니다. W5500은 MCU 보드와 PC 사이에서 FTP 제어 명령과 파일 데이터를 운반하는 유선 TCP/IP 소켓 엔진 역할을 합니다.
프로젝트가 하는 일
이 프로젝트는 STM32F10x 보드와 PC 사이에서 이더넷 기반 파일 전송을 구현하는 FTP 서버 예제입니다. FTP는 일반적으로 포트 21의 TCP 제어 연결과 포트 20의 별도 TCP 데이터 연결을 사용하므로, 단일 소켓 TCP echo 테스트보다 구조가 더 복잡합니다. 원문은 active FTP와 passive FTP 모드를 설명한 뒤, WIZnet ioLibrary의 FTP Server 파일을 STM32 + W5500 환경에 맞게 수정합니다.
펌웨어 스택은 여러 계층으로 구성됩니다. STM32 보드 초기화, CPU Flash 및 FATFS 스타일 파일 처리, W5500 SPI 플랫폼 지원, W5500 네트워크 설정, FTP 명령 파싱, FileZilla 기반 PC 테스트가 포함됩니다. 원문 작성자는 ftpd.c와 ftpd.h가 ioLibrary FTP Server 라이브러리에서 온 파일이지만, STM32 환경에서 동작하도록 수정이 필요했다고 설명합니다. 특히 scan_files()를 추가하고, FileZilla 테스트 이후 ftpd.c 동작을 일부 변경했습니다.
테스트 흐름은 파일 작업 공간 초기화, CPU Flash에 샘플 파일 생성 및 읽기, W5500 초기화, 로컬 네트워크 정보 설정, TCP 소켓 열기, FTP 데몬 초기화, FileZilla가 PC에서 접속하는 동안 ftpd_run()을 반복 실행하는 구조입니다. 원문은 수정된 버전이 테스트 목적상 단일 파일을 양방향으로 전송할 수 있다고 설명합니다.
WIZnet이 들어가는 위치
이 프로젝트에서 사용되는 WIZnet 제품은 W5500입니다. W5500은 STM32F10x 펌웨어와 LAN 사이에 위치하는 이더넷 컨트롤러이자 TCP/IP 오프로딩 장치입니다. STM32는 파일 시스템 처리, FTP 명령 파싱, UART 로그, 애플리케이션 상태를 담당합니다. W5500은 Ethernet MAC/PHY, TCP 소켓 상태, 패킷 버퍼링, SPI로 접근 가능한 소켓 레지스터를 담당합니다.
이 점은 FTP에서 특히 중요합니다. FTP는 여러 TCP 경로와 지속적인 소켓 상태 관리를 필요로 합니다. 코드에서는 FTP 제어 채널과 데이터 채널을 위해 W5500 소켓을 사용합니다. CTRL_SOCK, DATA_SOCK, CTRL_SOCK1은 각각 소켓 번호 1, 2, 3에 매핑되고, FTP 제어 및 데이터 포트는 21과 20으로 정의됩니다.
W5500은 하드웨어 TCP/IP 스택 기반 인터넷 컨트롤러이며, 최대 80 MHz SPI, 내장 10/100 Ethernet MAC 및 PHY, TCP/UDP/IPv4 지원, 8개 독립 소켓, 32 KB 내부 Tx/Rx 버퍼 메모리를 제공합니다. WIZnet ioLibrary는 W5x00 및 W6x00 칩을 위한 MCU 독립형 라이브러리이며 DHCP, DNS, MQTT, SNTP, TFTP, HTTP Server 같은 서비스도 제공합니다.
구현 참고 사항
파일: ftpd.c
설정 내용: FTP 로그인 상태, 작업 디렉터리, 로컬 IP, FTP 제어 소켓
중요한 이유: FTP는 명령 기반 프로토콜입니다. 서버는 USER, PASS, LIST, RETR, STOR 같은 명령을 처리하기 전에 포트 21의 제어 연결을 받을 준비가 되어 있어야 합니다.
void ftpd_init(uint8_t * src_ip)
{
ftp.state = FTPS_NOT_LOGIN;
ftp.current_cmd = NO_CMD;
ftp.dsock_mode = ACTIVE_MODE;
FTP_Port = 35000;
strcpy(ftp.workingdir, "/");
socket(CTRL_SOCK, Sn_MR_TCP, IPPORT_FTP, 0x0);
socket(CTRL_SOCK1, Sn_MR_TCP, IPPORT_FTP, 0x0);
}이 코드는 W5500 TCP 소켓을 FTP 제어 동작에 연결하기 위해 존재합니다. 같은 소스에서는 사용자 이름과 비밀번호 상태, 작업 디렉터리 상태, FTP 데이터 세션 처리에 필요한 로컬 IP도 관리합니다.
파일: ftpd.c
설정 내용: W5500 소켓 레지스터를 사용한 FTP 제어 소켓 상태 처리
중요한 이유: 서버는 PC 클라이언트가 항상 연결을 유지한다고 가정하면 안 됩니다. closed, initialized, listening, established, close-wait 같은 상태를 안정적으로 이동해야 합니다.
Sn_SR_Value = getSn_SR(CTRL_SOCK);
switch(Sn_SR_Value)
{
case SOCK_ESTABLISHED:
ret = send(CTRL_SOCK, (uint8_t *)dbuf,
strlen((const char *)dbuf));
break;
case SOCK_CLOSED:
socket(CTRL_SOCK, Sn_MR_TCP, IPPORT_FTP, 0x0);
break;
case SOCK_INIT:
listen(CTRL_SOCK);
break;
}이 부분은 FTP 서버에서 W5500 네트워크 스택을 사용하는 핵심 패턴입니다. 펌웨어는 W5500 소켓 상태 레지스터를 읽고, 연결이 성립되면 FTP 배너를 전송하며, 소켓이 닫혀 있으면 다시 생성하고, 초기화된 상태에서는 listen()으로 접속을 대기합니다.
실무 팁 / 주의점
- FTP를 테스트하기 전에 W5500 SPI 읽기/쓰기를 먼저 검증해야 합니다. SPI 접근과 칩 버전 읽기가 정상이어야 이후 FTP 디버깅이 의미 있습니다.
- FTP는 다중 소켓 프로토콜로 다뤄야 합니다. 제어 경로와 데이터 경로가 분리되어 있으므로 소켓 할당, close, reopen 동작을 명확히 처리해야 합니다.
- FATFS 버퍼와 네트워크 버퍼를 구분해야 합니다. W5500은 내부 소켓 버퍼를 제공하지만, 파일 읽기/쓰기에는 MCU 측 버퍼와
_MAX_SS처리도 필요합니다. - 업로드와 다운로드를 같은 클라이언트 도구로 모두 테스트해야 합니다. 원문은 FileZilla를 사용했고, 클라이언트 테스트 이후
ftpd.c를 수정했습니다. - active/passive 모드 동작에 주의해야 합니다. 원문은 FileZilla의 passive 설정과 서버 측 전송 동작 차이를 언급합니다.
- 타임아웃과 복구 경로를 추가해야 합니다. FTP 클라이언트가 제어 소켓이나 데이터 소켓을 열린 상태로 남길 수 있으므로, 타임아웃 카운터와 소켓 disconnect 처리가 필요합니다.
FAQ
Q: STM32F10x에서 FTP 서버를 구현할 때 왜 WIZnet W5500을 사용하나요?
A: FTP는 제어 명령과 파일 데이터 전송을 위해 안정적인 TCP 소켓이 필요합니다. W5500은 하드웨어 TCP/IP, 내장 10/100 Ethernet MAC/PHY, 8개 소켓, 32 KB 내부 Tx/Rx 버퍼를 제공하므로 STM32는 전체 소프트웨어 TCP/IP 스택보다 FATFS, 명령 파싱, 파일 처리에 집중할 수 있습니다.
Q: W5500은 STM32F10x 플랫폼에 어떻게 연결되나요?
A: 이 프로젝트는 SPI1, W5500 reset, W5500 interrupt 핀을 초기화한 뒤 wizchip_initialize()를 호출합니다. 이 과정에서 chip select, 단일 바이트 SPI, 다중 바이트 SPI 함수가 등록되고, 이후 W5500 소켓 API를 사용할 수 있습니다. 원문은 네트워크 설정을 적용하기 전에 W5500 버전과 PHY 상태도 확인합니다.
Q: 이 FTP 프로젝트에서 W5500은 어떤 역할을 하나요?
A: W5500은 FTP 서버의 TCP 소켓 엔진입니다. FTP 포트 21의 제어 연결을 제공하고, 데이터 전송 경로를 지원하며, Sn_SR 같은 레지스터를 통해 소켓 상태를 노출합니다. 또한 FTP 명령과 파일 payload 데이터를 유선 이더넷으로 송수신합니다.
Q: 초보자도 이 메이커 프로젝트를 따라할 수 있나요?
A: 가능합니다. 다만 STM32 주변장치 초기화, SPI, TCP 소켓, 기본 파일 시스템 개념을 이미 이해하고 있는 메이커에게 적합합니다. TCP echo 예제와 비교하면 이 프로젝트는 FTP 명령 파싱, active/passive 전송 동작, 파일 목록 조회, 파일 업로드, 파일 다운로드가 추가됩니다.
Q: 이 FTP 서버 용도에서 W5500은 ENC28J60과 어떻게 다른가요?
A: W5500은 하드웨어 TCP/IP 스택, 8개 독립 소켓, 32 KB 내부 소켓 버퍼 메모리를 통합하고 있어 FTP의 다중 TCP 경로에 유리합니다. ENC28J60은 SPI 기반 10Base-T 이더넷 컨트롤러이며, 일반적으로 MCU 펌웨어에서 TCP/IP 스택 책임을 더 많이 부담해야 합니다.
출처
Original article: CSDN, “测试W5500的第12步_FTP服务器,” 2025-06-30 게시, CC 4.0 BY-SA로 표시됨.
https://blog.csdn.net/weixin_42550185/article/details/149022907?spm=1001.2014.3001.5502
Source repository referenced by the article: WIZnet HK, STM32F10x_W5500_Examples, Mulan PSL v2 라이선스. 저장소는 18.FTP_Server를 FTP 서버 모드 예제로 설명하며, 다른 장치가 서버에 접근해 파일을 다운로드할 수 있는 예제라고 설명합니다.
https://gitee.com/wiznet-hk/STM32F10x_W5500_Examples
WIZnet product reference: W5500 documentation and ioLibrary Driver documentation.
https://docs.wiznet.io/Product/Chip/Ethernet/W5500
Alternative reference for comparison: Microchip ENC28J60 product page.
https://www.microchip.com/en-us/product/enc28j60
태그
#W5500 #WIZnet #STM32F10x #STM32F103 #FTPServer #FileZilla #FATFS #Ethernet #SPI #ioLibrary #EmbeddedC #Maker #NetworkStack #ENC28J60
