六、在zephyr上配置sdmmc和fatfs演示

📅 2026/7/10 1:10:36
六、在zephyr上配置sdmmc和fatfs演示
1、配置sdmmc使得系统能检测到sd卡并测试sd的性能1配置prj.conf #开启 Zephyr SD 卡子系统与 SDHC 驱动 CONFIG_SDHCy CONFIG_DISK_ACCESSy 开启文件系统支持以 FatFs 为例 CONFIG_FILE_SYSTEMy CONFIG_FAT_FILESYSTEM_ELMy CONFIG_LOG_MODE_IMMEDIATEy 配置app.overlay文件。dmamux1{statusokay;};dma1{statusokay;};sdmmc1{statusokay;compatiblest,stm32-sdmmc;pinctrl-0sdmmc1_ck_pc12sdmmc1_cmd_pd2sdmmc1_d0_pc8sdmmc1_d1_pc9sdmmc1_d2_pc10sdmmc1_d3_pc11;pinctrl-namesdefault;bus-width4;disk-nameSD;};配置 sd卡的时钟过高可能导致sd检测不到设置到合理的范围。如 div-q 10; 配置一下符合48mhz倍数。当前是配置完为48mhz主要线程调用。#includezephyr/storage/disk_access.h#includezephyr/linker/linker-defs.h#includeinttypes.h#includezephyr/linker/linker-defs.h#includezephyr/fs/fs.h#includezephyr/arch/cpu.h#defineTEST_BLOCK_SIZE512/* 标准扇区大小 */#defineTEST_BLOCK_COUNT100/* 每次连续读写的扇区数 (100 * 512 50KB) */#defineTEST_ITERATIONS20/* 循环测试次数 */#defineTOTAL_DATA_SIZE(TEST_BLOCK_SIZE*TEST_BLOCK_COUNT)/* 定义 512 字节的扇区缓冲区并确保 32 字节对齐 */staticuint8_t__aligned(32)test_write_buf[TOTAL_DATA_SIZE]__attribute__((section(.nocache.sd_nocache_mem)));staticuint8_t__aligned(32)test_read_buf[TOTAL_DATA_SIZE]__attribute__((section(.nocache.sd_nocache_mem)));#defineMAX_RETRIES3intsd_write_with_retry(constchar*pdrv,constuint8_t*buf,uint32_tsector,uint32_tcount){intret;for(inti0;iMAX_RETRIES;i){retdisk_access_write(pdrv,buf,sector,count);if(ret0)return0;// 成功则直接返回LOG_WRN(Write failed (attempt %d), retrying...,i1);k_msleep(10);// 等待 10ms 后重试}returnret;// 重试耗尽返回错误}intsd_read_with_retry(constchar*pdrv,uint8_t*buf,uint32_tsector,uint32_tcount){intret;for(inti0;iMAX_RETRIES;i){retdisk_access_read(pdrv,buf,sector,count);if(ret0)return0;LOG_WRN(Read failed (attempt %d), retrying...,i1);k_msleep(10);}returnret;}voidthread_entry4(void*p1,void*p2,void*p3){intret;uint32_tstart_sector1000;/* 避开 0 扇区MBR/引导区从安全区域开始 */uint32_tstart_time,end_time;uint64_ttotal_write_ms0,total_read_ms0;uint32_tsector_count;uint32_tsector_size;retdisk_access_init(SD);if(ret!0){printk(SD card init failed: %d \n,ret);}printk(SD card initialized successfully!\n);/* 2. 获取 SD 卡容量信息 */retdisk_access_ioctl(SD,DISK_IOCTL_GET_SECTOR_COUNT,sector_count);if(ret!0){printk(Failed to get sector count);}retdisk_access_ioctl(SD,DISK_IOCTL_GET_SECTOR_SIZE,sector_size);if(ret!0){printk(Failed to get sector size);}printk(SD Card Info: %u sectors, %u bytes/sector\n,sector_count,sector_size);printk(Total Capacity: %u MB\n,(sector_count*sector_size)/(1024*1024));/* 填充测试数据 */memset(test_write_buf,0xA5,sizeof(test_write_buf));for(inti0;iTEST_ITERATIONS;i){/* 1. 写入性能测试 *//* 【关键】写入前将 D-Cache 中的数据刷入 AXI SRAM确保 DMA 读到最新数据 */SCB_CleanDCache_by_Addr((uint32_t*)test_write_buf,TOTAL_DATA_SIZE);start_timek_uptime_get();retsd_write_with_retry(SD,test_write_buf,start_sector,TEST_BLOCK_COUNT);end_timek_uptime_get();if(ret!0){printk(Write failed at iteration %d, err: %d,i,ret);//return;}total_write_ms(end_time-start_time);SCB_InvalidateDCache_by_Addr((uint32_t*)test_read_buf,TOTAL_DATA_SIZE);/* 2. 读取性能测试 */start_timek_uptime_get();retsd_read_with_retry(SD,test_read_buf,start_sector,TEST_BLOCK_COUNT);end_timek_uptime_get();if(ret!0){printk(Read failed at iteration %d, err: %d,i,ret);// return;}SCB_InvalidateDCache_by_Addr((uint32_t*)test_read_buf,TOTAL_DATA_SIZE);total_read_ms(end_time-start_time);}/* 3. 数据完整性校验 */if(memcmp(test_write_buf,test_read_buf,TOTAL_DATA_SIZE)!0){printk(Data mismatch! Read/Write data is inconsistent.);//return;}/* 4. 计算并打印吞吐量 (MB/s) */floatwrite_speed((float)(TOTAL_DATA_SIZE*TEST_ITERATIONS)/(1024.0f*1024.0f))/((float)total_write_ms/1000.0f);floatread_speed((float)(TOTAL_DATA_SIZE*TEST_ITERATIONS)/(1024.0f*1024.0f))/((float)total_read_ms/1000.0f);uint32_twrite_speed_int(uint32_t)(write_speed*100);uint32_tread_speed_int(uint32_t)(read_speed*100);printk( Test Completed Successfully );printk(Avg Write Speed: %u.%02u MB/s (Total: %PRIu64 ms),write_speed_int/100,write_speed_int%100,total_write_ms);printk(Avg Read Speed: %u.%02u MB/s (Total: %PRIu64 ms),read_speed_int/100,read_speed_int%100,total_read_ms);while(1){k_sleep(K_FOREVER);}}演示结果2、在sdmmc基础上加上配置 fatfs系统# 开启 Zephyr SD 卡子系统与 SDHC 驱动 CONFIG_SDHCy CONFIG_DISK_ACCESSy CONFIG_DISK_DRIVER_SDMMCy CONFIG_SDMMC_STM32y # 开启文件系统支持以 FatFs 为例 CONFIG_FILE_SYSTEMy CONFIG_FAT_FILESYSTEM_ELMy CONFIG_LOG_MODE_IMMEDIATEy CONFIG_HEAP_MEM_POOL_SIZE8192CONFIG_MAIN_STACK_SIZE16384CONFIG_DMAy # 显式启用 DMA 支持 CONFIG_DMA_STM32y CONFIG_NOCACHE_MEMORYy # 保持 DMA 缓冲区一致性代码#includestm32h7xx_hal.h#includestring.h#includestdbool.h#includezephyr/drivers/dma.h#includezephyr/cache.h#includezephyr/devicetree.h#includezephyr/storage/disk_access.h#includezephyr/linker/linker-defs.h#includeinttypes.h#includezephyr/arch/cpu.h#includezephyr/fs/fs.h#includeff.h#defineTEST_FILE_PATH/SD:/test.txtstaticFATFS fatfs_fs;staticstructfs_mount_tfat_fs_mnt{.typeFS_FATFS,.fs_datafatfs_fs,.mnt_point/SD:,};staticintlsdir(constchar*path){intres;structfs_dir_tdirp;structfs_dirententry;fs_dir_t_init(dirp);resfs_opendir(dirp,path);if(res){LOG_ERR(Error opening dir %s [%d],path,res);returnres;}LOG_INF(Listing dir %s ...,path);for(;;){resfs_readdir(dirp,entry);if(res||entry.name[0]0){break;}if(entry.typeFS_DIR_ENTRY_DIR){LOG_INF([DIR ] %s,entry.name);}else{LOG_INF([FILE] %s (size %zu),entry.name,entry.size);}}fs_closedir(dirp);returnres;}/** * brief 创建文件并写入内容 */staticintcreate_and_write_file(constchar*path,constchar*data,size_tlen){structfs_file_tfile;intres;fs_file_t_init(file);/* 以 写 模式打开文件如果文件不存在则创建存在则清空 */resfs_open(file,path,FS_O_CREATE|FS_O_WRITE);if(res){LOG_ERR(Failed to open file for writing [%d],res);returnres;}/* 将数据写入文件 */resfs_write(file,data,len);if(res0){LOG_ERR(Failed to write to file [%d],res);gotocleanup;}LOG_INF(Successfully wrote %d bytes to %s,res,path);cleanup:/* 关闭文件 */fs_close(file);returnres;}/** * brief 读取文件内容 */staticintread_file_content(constchar*path,char*buf,size_tbuf_size){structfs_file_tfile;intres;ssize_tbytes_read;fs_file_t_init(file);/* 以 读 模式打开文件 */resfs_open(file,path,FS_O_READ);if(res){LOG_ERR(Failed to open file for reading [%d],res);returnres;}/* 读取文件内容到缓冲区 */bytes_readfs_read(file,buf,buf_size);if(bytes_read0){LOG_ERR(Failed to read from file [%d],bytes_read);resbytes_read;gotocleanup;}/* 确保字符串以 \0 结尾方便打印 */buf[bytes_read]\0;LOG_INF(Successfully read %d bytes from %s,bytes_read,path);LOG_INF(File Content: %s,buf);cleanup:/* 关闭文件 */fs_close(file);returnres;}voidled_thread_entry4(void*p1,void*p2,void*p3){intret;printk( SDIO Performance Test Start \n);retdisk_access_init(SD);if(ret!0){printk(Disk init failed: %d\n,ret);}else{printk(Disk initialized successfully.\n);}intresfs_mount(fat_fs_mnt);if(resFR_OK){LOG_INF(SD Card mounted successfully.);lsdir(fat_fs_mnt.mnt_point);}else{LOG_ERR(Error mounting disk: %d,res);}constchar*test_dataHello Zephyr on STM32H723ZG!\nThis is a test file.;charread_buffer[128];create_and_write_file(TEST_FILE_PATH,test_data,strlen(test_data));k_sleep(K_MSEC(5));read_file_content(TEST_FILE_PATH,read_buffer,sizeof(read_buffer));while(1){k_sleep(K_FOREVER);}}结果演示依据