一般来说,使用互联网的首要目的就是实现信息共享,文件传输是信息共享非常重要的内容。我们知道互联网是一个非常复杂的计算机环境,而各种设备和操作系统之间的文件交流问题,需要建立一个统一的文件传输协议,这就是FTP协议。
W5100S/W5500是一款集成全硬件 TCP/IP 协议栈的嵌入式以太网控制器,同时也是一颗工业级以太网控制芯片。本教程将介绍W5100S/W5500以太网FTP应用的基本原理、使用步骤、应用实例以及注意事项,帮助读者更好地掌握这一技术。
FTP是一种用于在互联网上控制文件双向传输的协议。它规定了如何在Internet上传输文件,并可以用于服务器程序文件的上传和下载。简单来说,FTP就是一种让用户能够在Internet上传输文的工具。
WIZnet 主流硬件协议栈以太网芯片参数对比
Model | Embedded Core | Host I/F | TX/RX Buffer | HW Socket | Network Performance |
---|---|---|---|---|---|
W5100S | TCP/IPv4, MAC & PHY | 8bit BUS, SPI | 16KB | 4 | Max.25Mbps |
W6100 | TCP/IPv4/IPv6, MAC & PHY | 8bit BUS, Fast SPI | 32KB | 8 | Max.25Mbps |
W5500 | TCP/IPv4, MAC & PHY | Fast SPI | 32KB | 8 | Max 15Mbps |
程序的运行框图如下所示:
软件
硬件
通过数据线连接PC的USB口(主要用于烧录程序,也可以虚拟出串口使用)
通过TTL串口转USB,连接UART0 的默认引脚:
使用模块连接RP2040 进行接线时
通过PC和设备都通过网线连接路由器LAN口
我们使用的是WIZnet官方的ioLibrary_Driver库。该库支持的协议丰富,操作简单,芯片在硬件上集成了TCP/IP协议栈,该库又封装好了TCP/IP层之上的协议,我们只需简单调用相应函数即可完成协议的应用。
第一步:在ftp_client.c文件中引用对应的库文件。
第二步:定义DHCP配置所需要的宏。
第三步:定义网络地址信息。
第四步:编写定时器回调函数,用于处理DHCP超时信息。
第五步:主函数首先是对串口和SPI进行初始化以及链路检测。然后是设置W5100S的网络地址,首先使用DHCP的方式进行获取,失败后使用预设的静态IP地址,然后进入FTP 客户端的初始化,初始化完成之后就会运行FTP客户端。
#include "dhcp.h" // Use dhcp
#include "socket.h" // Use socket
#include "ftpc.h" // Use ftp
#define UART_ID uart0
#define SOCKET_ID 0 // Socket number
#define ETHERNET_BUF_MAX_SIZE (1024 * 2) // Send and receive cache size
#define DHCP_RETRY_COUNT 5 // DHCP retry times
/* Network information to be configured. */
wiz_NetInfo net_info = {
.mac = {0x00, 0x08, 0xdc, 0x1e, 0xed, 0x2e}, // Configured MAC address
.ip = {192, 168, 1, 10}, // Configured IP address
.sn = {255, 255, 255, 0}, // Configured subnet mask
.gw = {192, 168, 1, 1}, // Configured gateway
.dns = {8, 8, 8, 8}, // Configured domain address
.dhcp = NETINFO_DHCP}; // Configured dhcp model,NETINFO_DHCP:use dhcp; NETINFO_STATIC: use static ip.
static uint8_t ethernet_buf[ETHERNET_BUF_MAX_SIZE] = {
0,
}; // Send and receive cachestatic uint8_t destip[4]={192, 168, 1, 2}; // udp destination ip
static uint8_t uart_buf[ETHERNET_BUF_MAX_SIZE] = {
0,
};
int main()
{
struct repeating_timer timer; // Define the timer structure
wiz_NetInfo get_info;
/* MCU init */
stdio_init_all(); // Initialize the main control peripheral
wizchip_initialize(); // Initialize the chip interface
/*dhcp init*/
DHCP_init(SOCKET_ID, ethernet_buf); // DHCP initialization
add_repeating_timer_ms(1000, repeating_timer_callback, NULL, &timer); // Add DHCP 1s Tick Timer handler
printf("wiznet chip ftp_client example.\r\n");
network_init(&net_info); // Configuring Network Information
print_network_information(&get_info); // Read back the configuration information and print it
getIPfromDHCP(local_ip); // Get the local IP address
ftpc_init(local_ip); // Initialize FTP Client
while (true)
{
ftpc_run(ethernet_buf); // Run FTP Client
}
}
(1)在library/ioLibrary_Driver/Ethernet/下找到wizchip_conf.h这个头文件,将_WIZCHIP_ 宏定义修改为W5500。
(2)在library下找到CMakeLists.txt文件,将COMPILE_SEL设置为ON即可,OFF为W5100S,ON为W5500。
6 相关链接
WIZnet官网
WIZnet官方库链接
本章相关例程
想了解更多,评论留言哦!