【得捷电子Follow me第4期】终极任务二:ftp服务
[复制链接]
本帖最后由 qinyunti 于 2024-2-17 12:37 编辑
27a4aad1e1a28d8d5c44e603262e660c
w5x00_ftp_server.uf2
(104 KB, 下载次数: 0)
七:终极任务二:使用外部存储器,组建简易FTP文件服务器,并能正常上传下载文件。
7.1 准备
基于ftp的example和前面的入门任务和基础任务一。
添加代码
修改CMakeLists.txt为如下
set(TARGET_NAME w5x00_ftp_server)
aux_source_directory(ZLG_GUI ZLG_GUI_SOURCES)
include_directories(ZLG_GUI/include .)
add_executable(${TARGET_NAME}
${TARGET_NAME}.c
ls013.c
ls013_lcd.c
${ZLG_GUI_SOURCES}
)
target_link_libraries(${TARGET_NAME} PRIVATE
pico_stdlib
hardware_spi
hardware_dma
hardware_clocks
ETHERNET_FILES
IOLIBRARY_FILES
FTPSERVER_FILES
)
pico_enable_stdio_usb(${TARGET_NAME} 1)
pico_enable_stdio_uart(${TARGET_NAME} 0)
pico_add_extra_outputs(${TARGET_NAME})
7.2 代码
/**
* Copyright (c) 2021 WIZnet Co.,Ltd
*
* SPDX-License-Identifier: BSD-3-Clause
*/
/**
* ----------------------------------------------------------------------------------------------------
* Includes
* ----------------------------------------------------------------------------------------------------
*/
#include <stdio.h>
#include "port_common.h"
#include "wizchip_conf.h"
#include "w5x00_spi.h"
#include "ftpd.h"
#include "ls013_lcd.h"
#include "config.h"
/**
* ----------------------------------------------------------------------------------------------------
* Macros
* ----------------------------------------------------------------------------------------------------
*/
/* Clock */
#define PLL_SYS_KHZ (133 * 1000)
/* Buffer */
#define ETHERNET_BUF_MAX_SIZE (1024 * 2)
/**
* ----------------------------------------------------------------------------------------------------
* Variables
* ----------------------------------------------------------------------------------------------------
*/
/* Network */
static wiz_NetInfo g_net_info =
{
.mac = {0x00, 0x08, 0xDC, 0x12, 0x34, 0x56}, // MAC address
.ip = {192, 168, 31, 111}, // IP address
.sn = {255, 255, 255, 0}, // Subnet Mask
.gw = {192, 168, 31, 1}, // Gateway
.dns = {8, 8, 8, 8}, // DNS server
.dhcp = NETINFO_STATIC // DHCP enable/disable
};
/* FTP */
static uint8_t g_ftp_buf[ETHERNET_BUF_MAX_SIZE] = {
0,
};
/**
* ----------------------------------------------------------------------------------------------------
* Functions
* ----------------------------------------------------------------------------------------------------
*/
/* Clock */
static void set_clock_khz(void);
/**
* ----------------------------------------------------------------------------------------------------
* Main
* ----------------------------------------------------------------------------------------------------
*/
int main()
{
/* Initialize */
uint8_t retval = 0;
set_clock_khz();
stdio_init_all();
wizchip_spi_initialize();
wizchip_cris_initialize();
wizchip_reset();
wizchip_initialize();
wizchip_check();
network_initialize(g_net_info);
ftpd_init(g_net_info.ip);
/* Get network information */
print_network_information(g_net_info);
GUI_Initialize();
GUI_SetColor(1,0);
GUI_PutString8_8(10,10,"ftp server test");
/* Infinite loop */
while (1)
{
/* Run FTP server */
if ((retval = ftpd_run(g_ftp_buf)) < 0)
{
printf(" FTP server error : %d\n", retval);
GUI_PutString8_8(10,20,"ftp server err");
while (1)
;
}
else
{
GUI_PutString8_8(10,20,"ftp server ok");
}
GUI_Exec();
}
}
/**
* ----------------------------------------------------------------------------------------------------
* Functions
* ----------------------------------------------------------------------------------------------------
*/
/* Clock */
static void set_clock_khz(void)
{
// set a system clock frequency in khz
set_sys_clock_khz(PLL_SYS_KHZ, true);
// configure the specified clock
clock_configure(
clk_peri,
0, // No glitchless mux
CLOCKS_CLK_PERI_CTRL_AUXSRC_VALUE_CLKSRC_PLL_SYS, // System PLL on AUX mux
PLL_SYS_KHZ * 1000, // Input frequency
PLL_SYS_KHZ * 1000 // Output (must be same as no divider)
);
}
7.3 测试
下载运行D:\BOARD\followme4\RP2040-HAT-C\build\examples\ftp\server\w5x00_ftp_server.uf2
使用FileZilla连接,可以下载上传文件
7.4 增加eeprom读写
参考以下文章使用IO模拟IIC可以驱动各种IIC设备,使用QWIIC转接板可以接各种QWIIC接口的传感器。以下在tfp基础上实现EEPROM读写。
https://mp.weixin.qq.com/s/vlhy7z8XrgxXJoAtYuXw0g 超级精简系列之四:超级精简的IO模拟IIC的C实现:EEPROM编辑工具应用
https://mp.weixin.qq.com/s/ESzWWqxHpQevsWfjV0s2VQ 超级精简系列之三:超级精简的IO模拟IIC的C实现
添加代码
实现IO接口
#include "pico/stdlib.h"
#include "io_iic.h"
#include "eeprom.h"
static void io_iic_port_init(void)
{
gpio_init(5);
gpio_set_dir(5, GPIO_OUT);
gpio_put(5, 0);
gpio_init(6);
gpio_set_dir(6, GPIO_OUT);
gpio_put(6, 0);
}
static void io_iic_port_deinit(void)
{
}
static void io_iic_port_scl_write(uint8_t val)
{
if(val == 0)
{
gpio_put(5, 0);
}
else
{
gpio_put(5, 1);
}
}
static void io_iic_port_sda_write(uint8_t val)
{
if(val == 0)
{
gpio_put(6, 0);
}
else
{
gpio_put(6, 1);
}
}
static void io_iic_port_sda_2read(void)
{
gpio_set_dir(6, GPIO_IN);
}
static uint8_t io_iic_port_sda_read(void)
{
return gpio_get(6);
}
static void io_iic_port_delay(uint32_t delay)
{
sleep_us(delay);
}
static io_iic_dev_st io_iic_dev=
{
.scl_write = io_iic_port_scl_write,
.sda_2read = io_iic_port_sda_2read,
.sda_read = io_iic_port_sda_read,
.sda_write = io_iic_port_sda_write,
.delayus = 10,
.delay_pf = io_iic_port_delay,
.init = io_iic_port_init,
.deinit = io_iic_port_deinit,
};
实现IIC接口
static void eeprom_iic_port_init(void)
{
io_iic_init(&io_iic_dev);
}
static void eeprom_iic_port_deinit(void)
{
io_iic_deinit(&io_iic_dev);
}
static void eeprom_iic_port_start(void)
{
io_iic_start(&io_iic_dev);
}
static void eeprom_iic_port_stop(void)
{
io_iic_stop(&io_iic_dev);
}
static int eeprom_iic_port_read(uint8_t* val ,uint8_t ack)
{
return io_iic_read(&io_iic_dev, val ,ack);
}
static int eeprom_iic_port_write(uint8_t val)
{
return io_iic_write(&io_iic_dev, val);
}
eeprom_dev_st eepromdev=
{
.addr = 0,
.read = eeprom_iic_port_read,
.start = eeprom_iic_port_start,
.stop = eeprom_iic_port_stop,
.write = eeprom_iic_port_write,
.init = eeprom_iic_port_init,
.deinit = eeprom_iic_port_deinit,
};
读写测试
//读命令实现
void printeepromfunc(uint8_t* param)
{
uint8_t buffer[16];
uint32_t addr;
uint32_t len;
uint8_t* p = param;
int res;
//if(3 == sscanf((const char*)param, "%*s %s %d %d", type, &addr, &len))
while(1)
{
if((*p > 'z') || (*p < 'a'))
{
break;
}
else
{
p++;
}
}
while(1)
{
if(*p != ' ')
{
break;
}
else
{
p++;
}
}
addr = atoi((const char*)p);
while(1)
{
if((*p > '9') || (*p < '0'))
{
break;
}
else
{
p++;
}
}
while(1)
{
if(*p != ' ')
{
break;
}
else
{
p++;
}
}
len = atoi((const char*)p);
uint32_t toread;
uint32_t read = 0;
eeprom_init(&eepromdev);
while(read < len)
{
toread = ((len-read) > sizeof(buffer)) ? sizeof(buffer) : (len-read);
if(0 != (res = eeprom_random_read(&eepromdev, addr+read, buffer, toread)))
{
printf("read err %d\r\n",res);
eeprom_deinit(&eepromdev);
return;
}
read += toread;
for(uint32_t i=0; i<toread ;i++)
{
printf("%02x ",buffer);
}
printf("\r\n");
}
eeprom_deinit(&eepromdev);
}
//写命令实现
static uint8_t char2hex(uint8_t ch)
{
if((ch<='9') && (ch>='0'))
{
return ch-'0';
}
else if((ch<='f') && (ch>='a'))
{
return ch-'a' + 10;
}
else if((ch<='F') && (ch>='A'))
{
return ch-'A' + 10;
}
return 0;
}
void writeeepromfunc(uint8_t* param)
{
uint8_t buffer[16];
uint32_t addr;
uint32_t len=0;
uint8_t* p = param;
uint8_t flag;
uint8_t tmp;
int res;
//if(3 == sscanf((const char*)param, "%*s %s %d %d", type, &addr, &len))
while(1)
{
if((*p > 'z') || (*p < 'a'))
{
break;
}
else
{
p++;
}
}
while(1)
{
if(*p != ' ')
{
break;
}
else
{
p++;
}
}
addr = atoi((const char*)p);
while(1)
{
if((*p > '9') || (*p < '0'))
{
break;
}
else
{
p++;
}
}
while(1)
{
if(*p != ' ')
{
break;
}
else
{
p++;
}
}
flag = 0;
while(*p)
{
if(flag == 0)
{
tmp = char2hex(*p) << 4;
flag = 1;
}
else if(flag == 1)
{
tmp |= char2hex(*p);
flag = 0;
buffer[len++] = tmp;
if(len>=16)
{
break;
}
}
p++;
}
eeprom_init(&eepromdev);
if(0 != (res = eeprom_page_write(&eepromdev, addr, buffer, len)))
{
printf("write err %d\r\n",res);
eeprom_deinit(&eepromdev);
return;
}
printf("\r\n");
eeprom_deinit(&eepromdev);
}
读写测试
读
Printeepromfunc(“printeeprom 0 256”);
c0 a9 21 01 10 00 00 00 1b 89 ab eb b7 fd 47 e4
e1 c5 ed eb e4 1f f3 96 2d d3 2b 55 de e6 cb aa
ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
修改8字节
Writeeepromfunc(“writeeeprom 8 46881faa2ffdb2e9”)
修改后查看
c0 a9 21 01 10 00 00 00 46 88 1f aa 2f fd b2 e9
e1 c5 ed eb e4 1f f3 96 2d d3 2b 55 de e6 cb aa
ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
|