添加tftp协议

1.usb连接上之后,可以使用tftp协议传输文件
2.解决写入sd卡时如果buff地址不是4字节对齐时数据错位的问题
This commit is contained in:
2025-10-18 23:55:18 +08:00
parent 045cff4cc6
commit eab38c8eff
8 changed files with 246 additions and 266 deletions

View File

@@ -0,0 +1,97 @@
/*
* File : tftp_port.c
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2017-08-17 armink first version.
*/
#include "bsp_init.h"
#include <ff.h>
#include <lwip/apps/tftp_server.h>
#include <rtthread.h>
static struct tftp_context ctx;
static void *tftp_open(const char *fname, const char *mode, u8_t write) {
FIL *file = malloc(sizeof(FIL));
FRESULT ret;
if (file == NULL) {
return NULL;
}
if (!rt_strcmp(mode, "octet")) {
if (write) {
ret = f_open(file, fname, FA_CREATE_ALWAYS | FA_WRITE);
} else {
ret = f_open(file, fname, FA_READ);
}
} else {
rt_kprintf("tftp: No support this mode(%s).", mode);
}
if (ret != FR_OK) {
rt_kprintf("ftp: open file %s failed,ret=%d\n", fname, ret);
free(file);
file = NULL;
}
return file;
}
static int tftp_write(void *handle, struct pbuf *p) {
FIL *file = (FIL *)handle;
UINT wb = 0;
FRESULT ret;
uint8_t *buf = p->payload;
ret = f_write(file, p->payload, p->len, &wb);
if (ret != FR_OK) {
wb = -1;
}
return (int)wb;
}
static void tftp_close(void *handle) {
f_close((FIL *)handle);
free(handle);
}
static int tftp_read(void *handle, void *buffer, int len) {
UINT rb = 0;
FIL *file = (FIL *)handle;
FRESULT ret;
ret = f_read(file, buffer, len, &rb);
if (ret != FR_OK) {
rb = -1;
}
return (int)rb;
}
static int tftp_server() {
ctx.open = tftp_open;
ctx.close = tftp_close;
ctx.read = tftp_read;
ctx.write = tftp_write;
if (tftp_init(&ctx) == ERR_OK) {
rt_kprintf("TFTP server start successfully.\n");
} else {
rt_kprintf("TFTP server start failed.\n");
}
return 0;
}
extern_init(tftp, tftp_server);

View File

@@ -1,87 +0,0 @@
/*
* File : tftp_port.c
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2017-08-17 armink first version.
*/
#include <rtthread.h>
#include <dfs_posix.h>
#include <lwip/apps/tftp_server.h>
static struct tftp_context ctx;
static void* tftp_open(const char* fname, const char* mode, u8_t write)
{
int fd = -1;
if (!rt_strcmp(mode, "octet"))
{
if (write)
{
fd = open(fname, O_WRONLY | O_CREAT, 0);
}
else
{
fd = open(fname, O_RDONLY, 0);
}
}
else
{
rt_kprintf("tftp: No support this mode(%s).", mode);
}
return (void *) fd;
}
static int tftp_write(void* handle, struct pbuf* p)
{
int fd = (int) handle;
return write(fd, p->payload, p->len);
}
#if defined(RT_USING_FINSH)
#include <finsh.h>
static void tftp_server(uint8_t argc, char **argv)
{
ctx.open = tftp_open;
ctx.close = (void (*)(void *)) close;
ctx.read = (int (*)(void *, void *, int)) read;
ctx.write = tftp_write;
if (tftp_init(&ctx) == ERR_OK)
{
rt_kprintf("TFTP server start successfully.\n");
}
else
{
rt_kprintf("TFTP server start failed.\n");
}
}
FINSH_FUNCTION_EXPORT(tftp_server, start tftp server.);
#if defined(FINSH_USING_MSH)
MSH_CMD_EXPORT(tftp_server, start tftp server.);
#endif /* defined(FINSH_USING_MSH) */
#endif /* defined(RT_USING_FINSH) */

View File

@@ -721,29 +721,31 @@ void mem_init(void)
void *mem_calloc(mem_size_t count, mem_size_t size)
{
// return rt_calloc(count, size);
void *r = mymalloc_exm(size * count);
memset(r, 0, size * count);
return r;
}
// lwip ֻ֧<D6BB>ִ<EFBFBD>bufתС<D7AA><D0A1><EFBFBD><EFBFBD>תС<D7AA><D0A1><EFBFBD><EFBFBD>ֱ<EFBFBD>ӷ<EFBFBD><D3B7><EFBFBD>ԭ<EFBFBD><D4AD>ַ
// <20><><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9>realloc<6F><63><EFBFBD>·<EFBFBD><C2B7><EFBFBD><E4A3AC>pbuf<75><66><EFBFBD>µ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>ݲ<EFBFBD><DDB2><EFBFBD>ȥ
// <20><><EFBFBD>ڼ<EFBFBD><DABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void *mem_trim(void *mem, mem_size_t size)
{
// return rt_realloc(mem, size);
/* not support trim yet */
mem = myrealloc(mem, size);
return mem;
}
// static void *p_last;
void *mem_malloc(mem_size_t size)
{
// return rt_malloc(size);
return mymalloc_exm(size);
void *p = 0;
p = mymalloc_exm(size);
return p;
}
void mem_free(void *mem)
{
// rt_free(mem);
myfree(mem);
}