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

发布时间:2026/9/24 23:38:37

六、在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);}}结果演示依据

相关新闻

STM32F101ZG上下拉电阻配置与DTH-08信号控制实践

STM32F101ZG上下拉电阻配置与DTH-08信号控制实践

2026/8/26 8:18:48

1. 项目背景与核心需求在嵌入式系统开发中,信号的上拉和下拉状态切换是一个基础但至关重要的操作。我最近在一个工业控制项目中遇到了这样的需求:需要通过STM32F101ZG微控制器精确控制DTH-08模块的信号状态。这个场景让我深刻理解了上下拉电阻选择对系统…

PIC18LF26K40驱动EPT-14A4005P蜂鸣器的低功耗警报方案

PIC18LF26K40驱动EPT-14A4005P蜂鸣器的低功耗警报方案

2026/9/24 13:01:37

1. 项目背景与核心需求解析在工业控制、安防系统和医疗设备等领域,可靠的声音警报系统是保障安全的关键组件。这次我们要探讨的是基于EPT-14A4005P压电蜂鸣器和PIC18LF26K40微控制器的警报解决方案设计。这个组合特别适合需要低功耗、高可靠性且环境适应性强的应用场…

C语言 atoi 函数深度解析:5个关键行为与 strtol 替代方案

C语言 atoi 函数深度解析:5个关键行为与 strtol 替代方案

2026/9/10 0:12:16

C语言 atoi 函数深度解析:5个关键行为与 strtol 替代方案在C语言开发中,字符串与数值的转换是基础但容易出错的环节。许多开发者习惯性地使用atoi函数进行快速转换,却常常忽略其潜在风险。本文将深入剖析atoi函数的5个关键行为特征&#xff0…

CANN/GE ACL数据集缓冲区添加函数

CANN/GE ACL数据集缓冲区添加函数

2026/9/23 22:20:06

aclmdlAddDatasetBuffer 【免费下载链接】ge GE(Graph Engine)是面向昇腾的图编译器和执行器,提供了计算图优化、多流并行、内存复用和模型下沉等技术手段,加速模型执行效率,减少模型内存占用。 GE 提供对 PyTorch、Te…

用ffmpeg高效批量调整图片尺寸的实战指南

用ffmpeg高效批量调整图片尺寸的实战指南

2026/9/23 14:32:22

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

Transformers 音频特征提取工具库 audio_utils 全解析:从 Mel 刻度换算到对数 Mel 频谱

Transformers 音频特征提取工具库 audio_utils 全解析:从 Mel 刻度换算到对数 Mel 频谱

2026/9/24 3:39:17

Transformers 音频特征提取工具库 audio_utils 全解析:从 Mel 刻度换算到对数 Mel 频谱 【免费下载链接】transformers 🤗 Transformers: the model-definition framework for state-of-the-art machine learning models in text, vision, audio, and mu…

RustFS 多节点集群重启与滚动升级实战:Readiness、Quorum 与 Degraded 模式完全指南

RustFS 多节点集群重启与滚动升级实战:Readiness、Quorum 与 Degraded 模式完全指南

2026/9/23 14:31:31

RustFS 多节点集群重启与滚动升级实战:Readiness、Quorum 与 Degraded 模式完全指南 【免费下载链接】rustfs 🚀2.3x faster than MinIO for 4KB object payloads. RustFS is an open-source, S3-compatible high-performance object storage system sup…

Java Integer缓存揭秘:128陷阱原理、避坑与面试全解

Java Integer缓存揭秘:128陷阱原理、避坑与面试全解

2026/9/24 7:10:52

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

RustFS Scanner 数据用量发布权威性决策:配额准入如何获得可用的权威依据

RustFS Scanner 数据用量发布权威性决策:配额准入如何获得可用的权威依据

2026/9/23 14:34:03

RustFS Scanner 数据用量发布权威性决策:配额准入如何获得可用的权威依据 【免费下载链接】rustfs 🚀2.3x faster than MinIO for 4KB object payloads. RustFS is an open-source, S3-compatible high-performance object storage system supporting mi…

远程协作的工作台整理

远程协作的工作台整理

2026/9/24 16:02:49

远程协作的工作台整理远程协作的核心不是再加一个工具,而是让交接信息足够完整。异步任务要写明目标、输入位置、完成标准和需要决策的人。 工作台的最小配置 将日程、待办、代码和沟通入口收拢到少数固定位置;通知按紧急程度分层。工作台不需要模仿办公…

持续集成 流水线自动化与 声明式交付 实践:原型怎样变成可用功能

持续集成 流水线自动化与 声明式交付 实践:原型怎样变成可用功能

2026/9/21 23:38:13

持续集成 流水线自动化与 声明式交付 实践:原型怎样变成可用功能分类:[AI/大模型]细分主题:AI 增强型 CI/CD 流水线自动化与 GitOps 实践:Agent 工作流、工具调用与任务拆解:从原型到生产的验收清单很多团队在尝试用大…

容器编排 生产环境运维与排障实战:复盘记录怎样真正派上用场

容器编排 生产环境运维与排障实战:复盘记录怎样真正派上用场

2026/9/22 0:48:53

容器编排 生产环境运维与排障实战:复盘记录怎样真正派上用场分类:[工程技术]细分主题:Kubernetes 生产环境运维与排障实战:可复制的项目复盘模板与决策记录大部分团队的事故复盘报告,最后都变成了躺在 Confluence 或钉…