/* USER CODE BEGIN Includes */ #include // 用于 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 /* 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 */