增加文件传输协议,接收上位机发送过来的文件
This commit is contained in:
parent
73bfb1e4f7
commit
bc481bd691
@ -26,6 +26,6 @@ uint32_t frameStreamRead_Get_index(stream_data_t* esp_data_interface);
|
|||||||
uint32_t frameStreamRecLen(stream_data_t* esp_data_interface);//已使用缓存字节数
|
uint32_t frameStreamRecLen(stream_data_t* esp_data_interface);//已使用缓存字节数
|
||||||
uint32_t frameStreamRecEmptyLen(stream_data_t* esp_data_interface);//剩余空间
|
uint32_t frameStreamRecEmptyLen(stream_data_t* esp_data_interface);//剩余空间
|
||||||
uint8_t ISframeStreamEmpty(stream_data_t* esp_data_interface);
|
uint8_t ISframeStreamEmpty(stream_data_t* esp_data_interface);
|
||||||
|
uint32_t WaitPrnBufBytes(stream_data_t* esp_data_interface,uint32_t len, uint32_t timeout, uint8_t *buffer);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
@ -88,4 +88,41 @@ uint8_t ISframeStreamEmpty(stream_data_t* esp_data_interface)
|
|||||||
}else{
|
}else{
|
||||||
return ESP_FAIL;
|
return ESP_FAIL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uint32_t WaitPrnBufBytes(stream_data_t* esp_data_interface,uint32_t len, uint32_t timeout, uint8_t *buffer)
|
||||||
|
{
|
||||||
|
TickType_t now_time;
|
||||||
|
TickType_t end_time;
|
||||||
|
uint32_t cnt;
|
||||||
|
uint8_t ch;
|
||||||
|
cnt = 0;
|
||||||
|
end_time = xTaskGetTickCount() + timeout;
|
||||||
|
while(cnt < len)
|
||||||
|
{
|
||||||
|
if (ISframeStreamEmpty(esp_data_interface) != ESP_OK)
|
||||||
|
{
|
||||||
|
end_time = xTaskGetTickCount() + timeout;
|
||||||
|
frameStreamRead(esp_data_interface,&ch,1);
|
||||||
|
*buffer++ = ch;
|
||||||
|
*buffer = 0;
|
||||||
|
++cnt;
|
||||||
|
if (cnt == len)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
now_time = xTaskGetTickCount();
|
||||||
|
if (now_time >= end_time)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
vTaskDelay(1000/ portTICK_PERIOD_MS);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (cnt);
|
||||||
|
}
|
||||||
|
|||||||
@ -3,9 +3,11 @@ file(GLOB PORT_SRCS "wifi/*.c" )
|
|||||||
|
|
||||||
file(GLOB FILE_SYS_SRCS "file_system/spiffs.c" )
|
file(GLOB FILE_SYS_SRCS "file_system/spiffs.c" )
|
||||||
|
|
||||||
idf_component_register(SRCS "main.c" "usb_serial_task"
|
idf_component_register(SRCS "main.c" "usb_serial_task" "Receiver.c"
|
||||||
${PORT_SRCS}
|
${PORT_SRCS}
|
||||||
${FILE_SYS_SRCS}
|
${FILE_SYS_SRCS}
|
||||||
INCLUDE_DIRS "."
|
INCLUDE_DIRS "."
|
||||||
INCLUDE_DIRS "file_system/"
|
INCLUDE_DIRS "file_system/"
|
||||||
INCLUDE_DIRS "wifi/")
|
INCLUDE_DIRS "wifi/")
|
||||||
|
|
||||||
|
#spiffs_create_partition_image(storage spiffs_image FLASH_IN_PROJECT)
|
||||||
|
|||||||
183
main/Receiver.c
Normal file
183
main/Receiver.c
Normal file
@ -0,0 +1,183 @@
|
|||||||
|
#include "public.h"
|
||||||
|
#include "esp_log.h"
|
||||||
|
#include "stream.h"
|
||||||
|
|
||||||
|
static const char *TAG = "RECV";
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
uint8_t Funcode;//功能码
|
||||||
|
uint8_t Cmdcode;//命令码
|
||||||
|
uint16_t waitcount;
|
||||||
|
void (* const codefunc)(uint8_t *buffer); //对应code执行的命令
|
||||||
|
}DECODERCMD;
|
||||||
|
|
||||||
|
void Connection_test(uint8_t *buffer);
|
||||||
|
void Transfer_file(uint8_t *buffer);
|
||||||
|
|
||||||
|
const DECODERCMD Cmd_Receiver[] = {
|
||||||
|
{0x01,0x01,0x05,Connection_test},
|
||||||
|
{0x02,0x01,0x03,Transfer_file},
|
||||||
|
{0,0,0,NULL},
|
||||||
|
};
|
||||||
|
|
||||||
|
void HandleCmd(const DECODERCMD* pCmd,uint8_t ch)
|
||||||
|
{
|
||||||
|
uint8_t i,j, result;
|
||||||
|
uint8_t PrnCmd[16];
|
||||||
|
|
||||||
|
result = 0;
|
||||||
|
for(i = 0; i < 256 ; i++)
|
||||||
|
{
|
||||||
|
if (pCmd[i].Funcode == 0) {
|
||||||
|
break;
|
||||||
|
} else if(pCmd[i].Funcode == ch) {
|
||||||
|
|
||||||
|
if (pCmd[i].waitcount > 0)
|
||||||
|
{
|
||||||
|
if (WaitPrnBufBytes(&uart_interface_data,pCmd[i].waitcount, 1000, PrnCmd) != pCmd[i].waitcount)
|
||||||
|
{
|
||||||
|
result = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (pCmd[i].codefunc != NULL)
|
||||||
|
{
|
||||||
|
pCmd[i].codefunc(&PrnCmd[0]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// CRC16 (Modbus) calculation function
|
||||||
|
uint16_t crc16_modbus(const uint8_t *data, uint16_t len) {
|
||||||
|
uint16_t crc = 0xFFFF;
|
||||||
|
for (int i = 0; i < len; i++) {
|
||||||
|
crc ^= data[i];
|
||||||
|
for (int j = 0; j < 8; j++) {
|
||||||
|
if (crc & 0x0001)
|
||||||
|
crc = (crc >> 1) ^ 0xA001;
|
||||||
|
else
|
||||||
|
crc = crc >> 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return crc;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
|
||||||
|
| 帧头(2字节) | 功能码(1字节) | 命令码(1字节) |长度(2字节) | 数据(N字节) | CRC16(2字节) |
|
||||||
|
例:0xAA 0x55 + 0x01 + 0x01 + 0x0008 + 数据内容 + CRC校验
|
||||||
|
*/
|
||||||
|
|
||||||
|
void Connection_test(uint8_t *buffer)
|
||||||
|
{
|
||||||
|
ESP_LOGI(TAG,"Connection_test");
|
||||||
|
if (buffer[0] == 0x01) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Funcode(0x02)
|
||||||
|
Cmdcode:(0x01)----文件名
|
||||||
|
Cmdcode:(0x02)----文件数据
|
||||||
|
*/
|
||||||
|
|
||||||
|
void Transfer_file(uint8_t *buffer)
|
||||||
|
{
|
||||||
|
uint16_t data_len = 0;
|
||||||
|
uint16_t crc_calc = 0xffff;
|
||||||
|
uint16_t crc_received = 0;
|
||||||
|
static FILE* file;
|
||||||
|
char* data_buff = NULL;
|
||||||
|
char file_name[64];
|
||||||
|
ESP_LOGI(TAG,"Transfer_file");
|
||||||
|
data_len = buffer[1] << 8 | buffer[2];
|
||||||
|
ESP_LOGI(TAG,"data_len:%d",data_len);
|
||||||
|
data_buff = malloc(data_len +10);
|
||||||
|
if(buffer[0] == 0x01) {//文件名
|
||||||
|
if (WaitPrnBufBytes(&uart_interface_data,data_len,1000,(uint8_t*)&data_buff[0]) == data_len)
|
||||||
|
{
|
||||||
|
crc_received = data_buff[data_len-1]<<8 | data_buff[data_len-2];
|
||||||
|
data_buff[data_len-2] = 0;
|
||||||
|
data_buff[data_len-1] = 0;
|
||||||
|
|
||||||
|
crc_calc = crc16_modbus((uint8_t*)&data_buff[0],data_len-2);
|
||||||
|
if (crc_calc != crc_received)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
sprintf(&file_name[0],"/spiffs/%s",data_buff);
|
||||||
|
ESP_LOGI(TAG,"file name:%s",file_name);
|
||||||
|
file = fopen(file_name, "ab");
|
||||||
|
if (file == NULL) {
|
||||||
|
printf("Failed to open file for writing\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// fclose(file);
|
||||||
|
}
|
||||||
|
|
||||||
|
} else if(buffer[0] == 0x02) {//文件内容
|
||||||
|
// 包数 | (0xff - 包数)| 数据 | crc
|
||||||
|
// 01 | 7f | data | crc
|
||||||
|
if (WaitPrnBufBytes(&uart_interface_data,data_len,1000,(uint8_t*)&data_buff[0]) == data_len)
|
||||||
|
{
|
||||||
|
crc_received = data_buff[data_len-1]<<8 | data_buff[data_len-2];
|
||||||
|
data_buff[data_len-2] = 0;
|
||||||
|
data_buff[data_len-1] = 0;
|
||||||
|
//
|
||||||
|
if (data_buff[0] != 0xff)
|
||||||
|
{
|
||||||
|
size_t written = fwrite(&data_buff[2], 1, (data_len - 4), file);
|
||||||
|
if (written != (data_len - 4)) {
|
||||||
|
ESP_LOGE("SPIFFS", "fwrite failed, written=%d", written);
|
||||||
|
} else {
|
||||||
|
ESP_LOGI("SPIFFS", "Write success: %d bytes", written);
|
||||||
|
}
|
||||||
|
fclose(file);
|
||||||
|
}else{
|
||||||
|
// fclose(file);
|
||||||
|
}
|
||||||
|
crc_calc = crc16_modbus((uint8_t*)&data_buff[0],data_len-2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ESP_LOGI(TAG,"crc:%X----%X",crc_calc,crc_received);
|
||||||
|
free(data_buff);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Read_CMD_task(void *pvParameter)
|
||||||
|
{
|
||||||
|
uint8_t ch;
|
||||||
|
uint8_t data[24];
|
||||||
|
stream_data_t* stream_data;
|
||||||
|
stream_data = &uart_interface_data;
|
||||||
|
ESP_LOGI(TAG, "Read_CMD_task...");
|
||||||
|
while (1)
|
||||||
|
{
|
||||||
|
if (ISframeStreamEmpty(&uart_interface_data) != ESP_OK)
|
||||||
|
{
|
||||||
|
|
||||||
|
frameStreamRead(&uart_interface_data,&ch,1);
|
||||||
|
ESP_LOGI(TAG, "Read:%X",ch);
|
||||||
|
if(ch == 0xAA)
|
||||||
|
{
|
||||||
|
if (WaitPrnBufBytes(&uart_interface_data,2,1000,&data[0]) == 2)
|
||||||
|
{
|
||||||
|
if (data[0] == 0x55) {
|
||||||
|
ESP_LOGI(TAG, "---Read:%X %X",data[0],data[1]);
|
||||||
|
HandleCmd(Cmd_Receiver,data[1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// HandleRecChar(ch);
|
||||||
|
// ESP_LOG_BUFFER_HEXDUMP("Recv str: ", ch, 1, ESP_LOG_INFO);
|
||||||
|
}
|
||||||
|
vTaskDelay(10/ portTICK_PERIOD_MS);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -1,4 +1,6 @@
|
|||||||
#include "spiffs.h"
|
#include "spiffs.h"
|
||||||
|
#include "dirent.h"
|
||||||
|
#include <sys/stat.h>
|
||||||
|
|
||||||
static const char *TAG = "file System";
|
static const char *TAG = "file System";
|
||||||
|
|
||||||
@ -36,6 +38,30 @@ esp_err_t init_spiffs(void)
|
|||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void list_files_in_spiffs() {
|
||||||
|
DIR *dir = opendir("/spiffs"); // ´ò¿ª SPIFFS ¸ùĿ¼
|
||||||
|
if (dir == NULL) {
|
||||||
|
ESP_LOGE(TAG, "Failed to open directory");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct dirent *entry;
|
||||||
|
char filepath[300];
|
||||||
|
struct stat st;
|
||||||
|
|
||||||
|
while ((entry = readdir(dir)) != NULL) {
|
||||||
|
snprintf(filepath, sizeof(filepath), "/spiffs/%s", entry->d_name);
|
||||||
|
if (stat(filepath, &st) == 0) {
|
||||||
|
ESP_LOGI(TAG, "Found file: %s, size: %ld bytes", entry->d_name, st.st_size);
|
||||||
|
} else {
|
||||||
|
ESP_LOGE(TAG, "Failed to get size of file: %s", entry->d_name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
closedir(dir); // ¹Ø±ÕĿ¼
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// void Read_file(char* filename, Read_data data,uint16_t size)
|
// void Read_file(char* filename, Read_data data,uint16_t size)
|
||||||
// {
|
// {
|
||||||
// FILE* f ;
|
// FILE* f ;
|
||||||
|
|||||||
@ -24,6 +24,7 @@ typedef struct
|
|||||||
|
|
||||||
extern esp_err_t init_spiffs(void);
|
extern esp_err_t init_spiffs(void);
|
||||||
extern esp_err_t sdmmc_init(void);
|
extern esp_err_t sdmmc_init(void);
|
||||||
|
extern void list_files_in_spiffs();
|
||||||
// void Read_file(char* filename, Read_data data,uint16_t size);
|
// void Read_file(char* filename, Read_data data,uint16_t size);
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
31
main/main.c
31
main/main.c
@ -62,6 +62,7 @@ uint8_t* load_html2Ram(wifi_html_t html)
|
|||||||
|
|
||||||
extern uint8_t* home_html_data;
|
extern uint8_t* home_html_data;
|
||||||
stream_data_t wifi_interface_data;
|
stream_data_t wifi_interface_data;
|
||||||
|
stream_data_t uart_interface_data;
|
||||||
|
|
||||||
void app_main(void)
|
void app_main(void)
|
||||||
{
|
{
|
||||||
@ -75,9 +76,10 @@ void app_main(void)
|
|||||||
ESP_ERROR_CHECK(esp_netif_init());
|
ESP_ERROR_CHECK(esp_netif_init());
|
||||||
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||||||
|
|
||||||
// init_spiffs();
|
init_spiffs();
|
||||||
|
list_files_in_spiffs();
|
||||||
ESPFlash_Init();
|
ESPFlash_Init();
|
||||||
stream_init(&wifi_interface_data,1024*10);
|
stream_init(&uart_interface_data,1024*10);
|
||||||
|
|
||||||
ESP_LOGI(TAG, "mem free:%ld",esp_get_free_heap_size());
|
ESP_LOGI(TAG, "mem free:%ld",esp_get_free_heap_size());
|
||||||
gpio_config_t config = {
|
gpio_config_t config = {
|
||||||
@ -89,7 +91,8 @@ void app_main(void)
|
|||||||
gpio_set_level(8,0);
|
gpio_set_level(8,0);
|
||||||
// gpio_set_level(18,1);
|
// gpio_set_level(18,1);
|
||||||
usb_serial2Uart(NULL);
|
usb_serial2Uart(NULL);
|
||||||
|
xTaskCreate(&Read_CMD_task, "Read_CMD_task", 1024*50, NULL, 14, NULL);
|
||||||
|
#if 0
|
||||||
home_html.html_data = load_html2Ram(home_html);
|
home_html.html_data = load_html2Ram(home_html);
|
||||||
wifi_config_html.html_data = load_html2Ram(wifi_config_html);
|
wifi_config_html.html_data = load_html2Ram(wifi_config_html);
|
||||||
upload_ota_html.html_data = load_html2Ram(upload_ota_html);
|
upload_ota_html.html_data = load_html2Ram(upload_ota_html);
|
||||||
@ -106,11 +109,24 @@ void app_main(void)
|
|||||||
}else{
|
}else{
|
||||||
wifi_init_station();
|
wifi_init_station();
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
// while (1)
|
|
||||||
// {
|
// FILE* f = fopen("/spiffs/hello.txt", "w");
|
||||||
// vTaskDelay(1000/ portTICK_PERIOD_MS);
|
// if (f == NULL) {
|
||||||
|
// printf("Failed to open file for writing\n");
|
||||||
|
// return;
|
||||||
// }
|
// }
|
||||||
|
// fprintf(f, "Hello World!\n");
|
||||||
|
// fclose(f);
|
||||||
|
|
||||||
|
// f = fopen("/spiffs/hello.txt", "r");
|
||||||
|
// char line[64];
|
||||||
|
// fgets(line, sizeof(line), f);
|
||||||
|
// fclose(f);
|
||||||
|
// printf("Read from file: '%s'\n", line);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
uint8_t mac_wifi[6];
|
uint8_t mac_wifi[6];
|
||||||
uint8_t mac_bt[6];
|
uint8_t mac_bt[6];
|
||||||
@ -207,3 +223,4 @@ void app_main(void)
|
|||||||
loader_port_esp32_deinit();
|
loader_port_esp32_deinit();
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -35,8 +35,11 @@
|
|||||||
#include "bsp_flash.h"
|
#include "bsp_flash.h"
|
||||||
|
|
||||||
#include "esp_efuse.h"
|
#include "esp_efuse.h"
|
||||||
|
#include "stream.h"
|
||||||
|
|
||||||
|
extern stream_data_t uart_interface_data;
|
||||||
|
|
||||||
|
|
||||||
extern void usb_serial2Uart(void *pvParameters);
|
extern void usb_serial2Uart(void *pvParameters);
|
||||||
|
extern void Read_CMD_task(void *pvParameter);
|
||||||
#endif
|
#endif
|
||||||
@ -37,9 +37,10 @@ static void echo_task(void *arg)
|
|||||||
// Write data back to the USB SERIAL JTAG
|
// Write data back to the USB SERIAL JTAG
|
||||||
if (len) {
|
if (len) {
|
||||||
usb_serial_jtag_write_bytes((const char *) data, len, 20 / portTICK_PERIOD_MS);
|
usb_serial_jtag_write_bytes((const char *) data, len, 20 / portTICK_PERIOD_MS);
|
||||||
|
frameStreamWrite(&uart_interface_data,&data[0],len);
|
||||||
// uart_write_bytes(EX_UART_NUM, (const char *) data, len);
|
// uart_write_bytes(EX_UART_NUM, (const char *) data, len);
|
||||||
data[len] = '\0';
|
// data[len] = '\0';
|
||||||
ESP_LOG_BUFFER_HEXDUMP("Recv str: ", data, len, ESP_LOG_INFO);
|
// ESP_LOG_BUFFER_HEXDUMP("Recv str: ", data, len, ESP_LOG_INFO);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
32
tools/monitor.py
Normal file
32
tools/monitor.py
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
import os
|
||||||
|
import subprocess
|
||||||
|
|
||||||
|
# 设置环境变量
|
||||||
|
os.environ["IDF_PATH"] = r"D:\esp\v5.3.2\esp-idf"
|
||||||
|
|
||||||
|
# 参数配置
|
||||||
|
python_exe = r"D:\esp\.espressif\python_env\idf5.3_py3.11_env\Scripts\python.exe"
|
||||||
|
idf_monitor = os.path.join(os.environ["IDF_PATH"], "tools", "idf_monitor.py")
|
||||||
|
port = "COM13"
|
||||||
|
baudrate = "115200"
|
||||||
|
toolchain_prefix = "riscv32-esp-elf-"
|
||||||
|
make_cmd = f"'{python_exe}' '{os.path.join(os.environ['IDF_PATH'], 'tools', 'idf.py')}'"
|
||||||
|
target = "esp32c3"
|
||||||
|
elf_file = r"d:\esp\WorkSpace\esp32c3-usb_serial\build\usb_serial_jtag_echo.elf"
|
||||||
|
|
||||||
|
# 构造命令行
|
||||||
|
cmd = [
|
||||||
|
python_exe,
|
||||||
|
idf_monitor,
|
||||||
|
"-p", port,
|
||||||
|
"-b", baudrate,
|
||||||
|
"--toolchain-prefix", toolchain_prefix,
|
||||||
|
"--make", make_cmd,
|
||||||
|
"--target", target,
|
||||||
|
elf_file
|
||||||
|
]
|
||||||
|
|
||||||
|
print("运行命令: ", " ".join(cmd))
|
||||||
|
|
||||||
|
# 运行命令
|
||||||
|
subprocess.run(cmd)
|
||||||
Loading…
Reference in New Issue
Block a user