- 新增usb连接状态检测函数,根据usb连接状态自动切换数据采集模式 - 新增文件系统动态挂载/卸载功能,usb连接时卸载文件系统,断开时重新挂载 - 修改系统启动逻辑,根据初始usb连接状态决定是否开始数据采集 - 新增usb模式系统状态,完善系统状态机 🐛 fix(sdio): 修复sdio配置问题并启用中断 - 修改sdio时钟分频为2,优化sd nand通信时序 - 启用sdio数据线内部上拉电阻,提高信号稳定性 - 提高sdio dma传输优先级至最高,确保数据传输实时性 - 启用sdio全局中断并设置优先级为9 🔧 chore(config): 优化系统配置参数 - 修改系统滴答定时器中断优先级为0(最高优先级) - 增加堆栈大小至0x1000,增加堆大小至0x800 - 修改usb msc媒体数据包大小至32768,提高usb传输效率 - 修改fatfs配置,设置最大最小扇区大小为512字节 ♻️ refactor(usb): 重构usb存储接口实现 - 修改usb存储初始化逻辑,避免重复初始化sd卡 - 优化usb存储容量报告机制,强制报告512字节扇区 - 增加sd nand读写超时等待机制,确保数据传输完成 - 修改usb中断优先级为11,避免与sdio中断冲突 📝 docs(headers): 更新头文件声明 - 在stm32f4xx_it.h中添加sdio中断处理函数声明 - 在system_monitor.h中添加usb模式系统状态定义 - 更新data_storage.h中的数据存储路径配置
169 lines
5.1 KiB
C
169 lines
5.1 KiB
C
/* USER CODE BEGIN Includes */
|
||
#include <stdio.h> // 用于 printf 打印调试信息
|
||
|
||
#include "fatfs.h"
|
||
#include "main.h"
|
||
#include "dma.h"
|
||
#include "fatfs.h"
|
||
#include "sdio.h"
|
||
#include "spi.h"
|
||
#include "tim.h"
|
||
#include "usart.h"
|
||
#include "usb_device.h"
|
||
#include "gpio.h"
|
||
#include "main.h"
|
||
#include "fatfs.h"
|
||
#include "ff.h"
|
||
#include "data_packet.h"
|
||
#include "correction.h"
|
||
#include <stdint.h>
|
||
|
||
/* USER CODE END Includes */
|
||
|
||
/* USER CODE BEGIN 4 */
|
||
|
||
extern SD_HandleTypeDef hsd;
|
||
//DMA_HandleTypeDef hdma_sdio_rx;
|
||
//DMA_HandleTypeDef hdma_sdio_tx;
|
||
|
||
/**
|
||
* @brief 针对 SD NAND 的专用初始化与格式化函数
|
||
* @return 0: 成功, -1: 失败
|
||
*/
|
||
int SD_NAND_Init_And_Mount(void)
|
||
{
|
||
FRESULT res;
|
||
BYTE workBuffer[4096]; // f_mkfs 需要的工作缓存
|
||
|
||
|
||
// 1. 物理层延时:SD NAND 内部初始化比普通卡慢
|
||
HAL_Delay(500);
|
||
|
||
// 2. 检查底层 SDIO 是否通了
|
||
// 如果这里检测不到,说明硬件接线或 IOC 时钟配置有问题
|
||
HAL_SD_CardStateTypeDef cardState = HAL_SD_GetCardState(&hsd); // 确认句柄是 hsd1 还是 hsd
|
||
if (cardState == HAL_SD_CARD_ERROR) {
|
||
printf("Error: SD Card Hardware Not Ready!\r\n");
|
||
return -1;
|
||
}
|
||
|
||
// 3. 尝试挂载 (Force Mount = 1)
|
||
printf("Attempting to mount SD card...\r\n");
|
||
res = f_mount(&SDFatFS, SDPath, 1);
|
||
|
||
// 4. 分析挂载结果
|
||
if (res == FR_OK) {
|
||
// 挂载成功,打印容量信息
|
||
DWORD free_clusters;
|
||
FATFS *fs;
|
||
f_getfree(SDPath, &free_clusters, &fs);
|
||
uint32_t total_blocks = (fs->n_fatent - 2) * fs->csize;
|
||
printf("Mount Success! Total: %lu sectors\r\n", total_blocks);
|
||
return 0;
|
||
}
|
||
|
||
// 5. 如果没有文件系统 (新芯片或损坏),执行格式化
|
||
else if (res == FR_NO_FILESYSTEM)
|
||
{
|
||
printf("No Filesystem found. Starting formatting (Legacy Mode)...\r\n");
|
||
|
||
/* 针对你的 FatFs 版本 (5个参数) 的修正
|
||
参数说明:
|
||
1. path: 路径
|
||
2. opt: 格式化选项 (FM_FAT32, FM_SFD 等)
|
||
3. au: 簇大小 (Allocation Unit),设为 4096 适配 SD NAND
|
||
4. work: 工作缓存指针
|
||
5. len: 工作缓存大小
|
||
*/
|
||
|
||
// 确保你的 ff.h 中定义了 FM_FAT32。如果没有,尝试用 0x02 代替
|
||
BYTE format_opt = FM_FAT32; // 或者 FM_ANY | FM_SFD
|
||
|
||
// 【关键】针对 SD NAND,强制使用 4096 字节的簇大小
|
||
DWORD au_size = 512;
|
||
|
||
// 修正后的函数调用:传入 5 个参数,而不是结构体
|
||
res = f_mkfs(SDPath, format_opt, au_size, workBuffer, sizeof(workBuffer));
|
||
|
||
if (res == FR_OK) {
|
||
printf("Format Successful! Remounting...\r\n");
|
||
// 格式化后建议取消挂载再重新挂载
|
||
f_mount(NULL, SDPath, 0);
|
||
res = f_mount(&SDFatFS, SDPath, 1);
|
||
if (res == FR_OK) {
|
||
printf("Remount Success!\r\n");
|
||
return 0;
|
||
}
|
||
} else {
|
||
printf("Format Failed! Error: %d\r\n", res);
|
||
}
|
||
}
|
||
|
||
return -1;
|
||
}
|
||
|
||
/* 简单的测试写文件函数 */
|
||
void SD_Test_Write(void) {
|
||
FIL fil;
|
||
UINT bw;
|
||
if (f_open(&fil, "SDTest.txt", FA_WRITE | FA_CREATE_ALWAYS) == FR_OK) {
|
||
f_write(&fil, "Hello SD NAND!", 14, &bw);
|
||
f_close(&fil);
|
||
printf("Write Test: OK\r\n");
|
||
} else {
|
||
printf("Write Test: Failed\r\n");
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @brief 读取测试函数
|
||
* 打开 SDTest.txt,读取内容并通过 printf 打印
|
||
*/
|
||
void SD_Test_Read(void)
|
||
{
|
||
FIL fil; // 文件对象
|
||
FRESULT res; // FatFs 返回结果
|
||
UINT br; // Bytes Read (实际读取到的字节数)
|
||
BYTE readBuf[128]; // 读取缓存 (根据需要调整大小)
|
||
|
||
printf("\r\n--- Starting Read Test ---\r\n");
|
||
|
||
// 1. 打开文件 (使用 FA_READ 模式)
|
||
// 注意:文件名必须与写入时完全一致
|
||
res = f_open(&fil, "SDTest.txt", FA_READ);
|
||
|
||
if (res != FR_OK) {
|
||
printf("Read Failed: f_open error code %d\r\n", res);
|
||
if (res == FR_NO_FILE) {
|
||
printf("Error: File does not exist. Did Write Test run successfully?\r\n");
|
||
}
|
||
return;
|
||
}
|
||
|
||
// 2. 读取文件
|
||
// 为了安全,读取长度设为 buffer大小 - 1,留一个字节给字符串结束符
|
||
res = f_read(&fil, readBuf, sizeof(readBuf) - 1, &br);
|
||
|
||
if (res == FR_OK) {
|
||
// 3. 处理读取到的数据
|
||
readBuf[br] = '\0'; // 手动添加字符串结束符,方便 printf 打印
|
||
|
||
printf("Read Success!\r\n");
|
||
printf("Bytes Read: %d\r\n", br);
|
||
|
||
if (br > 0) {
|
||
printf("File Content: \r\n%s\r\n", readBuf);
|
||
} else {
|
||
printf("Warning: File is empty (0 bytes).\r\n");
|
||
}
|
||
} else {
|
||
printf("Read Failed: f_read error code %d\r\n", res);
|
||
}
|
||
|
||
// 4. 关闭文件 (读取完成后也必须关闭,释放句柄)
|
||
f_close(&fil);
|
||
|
||
printf("--- Read Test Finished ---\r\n");
|
||
}
|
||
/* USER CODE END 4 */
|