670|1

13

帖子

3

TA的资源

一粒金砂(中级)

楼主
 

【得捷Follow me第4期】终极任务二:使用外部存储器,组建简易FTP文件服务器 [复制链接]

 

 【得捷Follow me第4期】终极任务二:使用外部存储器,组建简易FTP文件服务器,并能正常上传下载文件。

前面任务一直使用的arduino IDE,进行最后一个任务的时候一直没把外部存储给搞定,最后只好把软件换成了VScode,顺利启动了FTP服务器。PC端使用filezilla进行访问。

#include <stdio.h>
#include "pico/stdlib.h"
#include "pico/binary_info.h"
#include "hardware/spi.h"
#include "hardware/uart.h"
#include "hardware/irq.h"

#include "wizchip_conf.h"
#include "bsp_spi.h"
#include "dhcp.h"   // Use dhcp
#include "socket.h" // Use socket
#include "ftpd.h"   // Use ftp server


#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

/**
 * [url=home.php?mod=space&uid=159083]@brief[/url] Timer callback processing function, used for dhcp timing processing
 * @param   repeating :Timer structure
 * [url=home.php?mod=space&uid=784970]@return[/url] bool
 */
bool repeating_timer_callback(struct repeating_timer *t);

/**
 * @brief   Initialization of chip network information
 * @param   conf_info :Static configuration information
 * @return  none
 */
void network_init(wiz_NetInfo *conf_info);

/* Network information to be configured. */
wiz_NetInfo net_info = {
    .mac = {0x00, 0x08, 0xdc, 0x11, 0x22, 0x33}, // Configured MAC address
    .ip = {192, 168, 0, 10},                     // Configured IP address
    .sn = {255, 255, 255, 0},                    // Configured subnet mask
    .gw = {192, 168, 0, 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 
static uint8_t uart_buf[ETHERNET_BUF_MAX_SIZE] = {
    0,
};
static uint8_t local_ip[4];
static uint8_t breakout_flag = 0; // Define the DHCP acquisition flag

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
    wizchip_setnetinfo(&net_info); // Configure once first

    /*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 server 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
    ftpd_init(local_ip);

    while (true)
    {
        ftpd_run(ethernet_buf); // Run FTP Server
    }
}

void network_init(wiz_NetInfo *conf_info)
{
    int count = 0;
    uint8_t dhcp_retry = 0;

    if (conf_info->dhcp == NETINFO_DHCP)
    {
        while (true)
        {
            switch (DHCP_run()) // Do the DHCP client
            {
            case DHCP_IP_LEASED: // DHCP resolves the domain name successfully
            {
                if (breakout_flag == 0)
                {
                    printf("DHCP success\r\n");
                    getIPfromDHCP((*conf_info).ip);
                    getGWfromDHCP((*conf_info).gw);
                    getSNfromDHCP((*conf_info).sn);
                    getDNSfromDHCP((*conf_info).dns);
                    wizchip_setnetinfo(conf_info); // Configuring Network Information
                    close(SOCKET_ID);              // After dhcp close the socket, avoid errors in later use
                    breakout_flag = 1;
                }
                break;
            }
            case DHCP_FAILED:
            {
                printf(" DHCP failed \r\n");
                count++;
                if (count <= DHCP_RETRY_COUNT) // If the number of times is less than or equal to the maximum number of times, try again
                {
                    printf("DHCP timeout occurred and retry %d \r\n", count);
                }
                else if (count > DHCP_RETRY_COUNT) // If the number of times is greater than DHCP fails
                {
                    breakout_flag = 1; // if DHCP fail, use the static
                    DHCP_stop();       // Stop processing DHCP protocol
                    conf_info->dhcp = NETINFO_STATIC;
                    wizchip_setnetinfo(conf_info); // Configuring Network Information
                    break;
                }
                break;
            }
            }
            if (breakout_flag)
            {
                printf("config succ\r\n");
                break;
            }
        }
    }
    else
    {
        wizchip_setnetinfo(conf_info); // Configuring Network Information
    }
}

bool repeating_timer_callback(struct repeating_timer *t)
{
    DHCP_time_handler(); // DHCP 1s Tick Timer handler
    return true;
}

启动后,主控板通过DHCP进行配置,配置成功后将服务器地址、账户和密码发送到串口监视器。

 

PC端启动filezilla软件使用穿孔打印的信息登录FTP服务器,测试上传和下载功能

 

最新回复

还是VScode好,能找到底层代码。   详情 回复 发表于 2024-2-20 17:35
点赞 关注
 
 

回复
举报

7244

帖子

2

TA的资源

版主

沙发
 

还是VScode好,能找到底层代码。

 
 
 

回复
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/6 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表