
FAT (File Allocation Table) 文件系统是一种广泛使用的基于磁盘的文件系统,尤其适用于小型嵌入式系统和存储卡。FAT_FS 就是一个专门针对 FAT 文件系统的开源实现。
SDIO (Secure Digital Input Output) 是一种通信协议,用于与 SD/SDHC/SDXC 存储卡进行数据交互。而 FAT_FS 则是一种文件系统,用于管理存储在这些存储卡上的文件数据。
通常情况下,嵌入式系统会将 FAT_FS 文件系统与 SDIO 驱动程序集成在一起,形成一个完整的存储子系统。SDIO 驱动程序负责与物理存储卡进行底层的数据交互,而 FAT_FS 则提供上层的文件系统功能,使得应用程序可以方便地访问存储卡上的文件数据。
这种组合使得嵌入式系统能够轻松地支持基于 SD 卡的文件存储和交换功能,广泛应用于各种电子设备中。
fat_fs仓库地址:FatFs - Generic FAT Filesystem Module

sd_card_info_struct sd_cardinfo;
- if(SD_OK == status) {
- status = sd_card_information_get(&sd_cardinfo);
- }
将下载的文件导入到工程中

替换diskio.c文件内容
- #include "ff.h"
- FATFS fs;
- u8 buff_tx[512]="SD卡扇区读写测试,fat文件系统读写测试www.icheima.com\r\n";
- u8 buff_rx[512];
- void test_fatfs_write(const TCHAR *file_name,u8 *buff,u32 len);
- void test_fatfs_read(const TCHAR *file_name,u8 *buff,u32 len);
-
- void test_fatfs(){
- uint8_t stat=f_mount(&fs,"0:",1);//SD卡挂载
- printf("stat=%d\r\n",stat);
- if(stat==0) printf("磁盘挂载成功\r\n");
- test_fatfs_write("0:/icheima.txt",buff_tx,strlen((char *)buff_tx));
- test_fatfs_read("0:/icheima.txt",buff_rx,strlen((char *)buff_tx));
- printf("rx:%s\r\n",buff_rx);
- //while(1){}
- }
- void test_fatfs_write(const TCHAR *file_name,u8 *buff,u32 len)
- {
- /*1.打开文件*/
- FIL fp;
- FRESULT res;
- UINT bw;
- res=f_open(&fp,file_name,FA_WRITE|FA_CREATE_ALWAYS);//写+创建
- if(res!=FR_OK)
- {
- printf("文件打开或创建失败:ERR%d\r\n",res);
- return ;
- }
- printf("文件创建或打开成功\r\n");
- res=f_write(&fp,buff,len,&bw);
- if(res==FR_OK)
- {
- printf("数据写入成功 %d Byte\r\n",bw);
- }
- else printf("写入失败ERR:%d\r\n",res);
- f_close(&fp);//关闭文件
- }
-
- void test_fatfs_read(const TCHAR *file_name,u8 *buff,u32 len)
- {
- /*1.打开文件*/
- FIL fp;
- FRESULT res;
- UINT br;
- res=f_open(&fp,file_name,FA_READ);//读
- if(res!=FR_OK)
- {
- printf("文件打开失败:ERR%d\r\n",res);
- return ;
- }
- printf("文件打开成功\r\n");
- res=f_read(&fp,buff,len,&br);
- if(res!=FR_OK)
- {
- printf("读取数据失败ERR:%d \r\n",res);
- }
- else
- {
- printf("读取数据成功 %d byte\r\n",br);
- }
- f_close(&fp);//关闭文件
- }
uint8_t stat=f_mount(&fs,"0:",1);//SD卡挂载
调用以上代码,如果出现13错误码,则使用SDFormatter对SD卡进行格式化即可

在lv_conf配置文件中,启用文件系统
- #define LV_USE_FS_WIN32 1
- #if LV_USE_FS_WIN32
- #define LV_FS_WIN32_LETTER 'D' /*Set an upper cased letter on which the drive will accessible (e.g. 'A')*/
- #define LV_FS_WIN32_PATH "D:/videos/code/lv_port_pc_eclipse-release-v8.3/images" /*Set the working directory. File/directory paths will be appended to it.*/
- #define LV_FS_WIN32_CACHE_SIZE 0 /*>0 to cache this number of bytes in lv_fs_read()*/
- #endif
其中“D:/videos/code/lv_port_pc_eclipse-release-v8.3/images”表示windows上面的路径,我们将它映射为lvgl中的文件盘符为D
例如,我们想访问“D:/videos/code/lv_port_pc_eclipse-release-v8.3/images”下的aaa.txt文件,在代码中我们其实只需要写"D:/aaa.txt"即可
在代码中调用
- void demo14(){
- printf("demo14\n");
-
- lv_fs_dir_t d;
- if (lv_fs_dir_open(&d, "/") == LV_FS_RES_OK)
- {
- char b[50];
- memset(b, 0, 50);
- while (lv_fs_dir_read(&d, b) == LV_FS_RES_OK)
- {
- printf("%s\n", b);
- }
- lv_fs_dir_close(&d);
- }
- lv_fs_file_t file_p;
- lv_fs_res_t res = lv_fs_open(&file_p,"D:/mergeBinFile.c",LV_FS_MODE_RD);
- // 1. 获取屏幕
- lv_obj_t* screen = lv_scr_act();
- // 2. 创建要显示的内容
- lv_obj_t* img = lv_img_create(screen);
- // 3. 对内容进行设置 参数1:img, 参数2:要显示的图片
- lv_img_set_src(img,"D:/a0001.bin");
-
- int i = 1;
- int time_count=0;
- char buffer[10];
-
- while(1){
- if(time_count%5 == 0){
-
- if(i > 392){
- i=0;
- }
- sprintf(buffer, "D:/a%04d.bin", i);
- printf("%s \n",buffer);
- lv_img_set_src(img,buffer);
- i++;
- }
- time_count++;
- lv_timer_handler();
- usleep(5 * 1000);
- }
- }
运行上述代码,我们可以看到视频画面


在lv_conf文件中配置

在代码中,初始化SD卡
- uint8_t stat=f_mount(&fs,"D",1);//SD卡挂载
- printf("stat=%d\r\n",stat);
初始化lvgl中的文件系统
lv_fs_fatfs_init();
参考示例代码
- // 1. 获取屏幕
- lv_obj_t* screen = lv_scr_act();
- // 2. 创建要显示的内容
- lv_obj_t* img = lv_img_create(screen);
- // 3. 对内容进行设置 参数1:img, 参数2:要显示的图片
- lv_img_set_src(img,"D:/images/a0001.bin");
注: 上面代码需要在sd根目录新建images文件夹,并在其中放入a0001.bin文件