嵌入式技术论坛
直播中

申换换

7年用户 1489经验值
私信 关注
[经验]

基于AB32VG1开发板使用SDIO驱动SD卡

1.项目创建

这里使用SDIO驱动SD卡

这里就不详细介绍了

2.配置项目

双击RT-Thread Settings文件,打开 RT-Thread 项目配置界面,启用DFS, Fatfs和SDIO驱动。

1.jpg

点击更多设置,选择硬件选项,勾选Onboard Peripheral Drivers 下的 Enable SDCARD选项,使能SDCARD, 勾选后On-chip Peripheral Drivers 下的 Enable SDIO 会默认勾选的。然后保存即可。

1.jpg

3.代码

挂载文件elm-fat系统,工程配置好后, 在applications目录下有个mnt.c 文件,这个文件就是把SD卡挂载到文件elm-fat系统, 无须重新写挂载!这个mnt.c 使用了线程是实现挂载,SD初始化的时候存在延时,这也就是使用线程去实现挂载的原因吧!

1.jpg

测试代码, 参考了RT-Thread 文档中心-虚拟文件系统

tatic void readwrite_sample(void)
{
int fd, size;
char s[] = “RT-Thread Programmer!”, buffer[80];

rt_kprintf(“Write string %s to test.txt.\n”, s);

/ 以创建和读写模式打开 /text.txt 文件,如果该文件不存在则创建该文件 /
fd = open(“/text.txt”, O_WRONLY | O_CREAT);
if (fd>= 0)
{

 write(fd, s, sizeof(s));
 close(fd);
 rt_kprintf("Write done.\n");
}

/ 以只读模式打开 /text.txt 文件 /
fd = open(“/text.txt”, O_RDONLY);
if (fd>= 0)
{

 size = read(fd, buffer, sizeof(buffer));
 close(fd);
 rt_kprintf("Read from file test.txt : %s \n", buffer);
 if (size < 0)
     return ;
}
}
/ 导出到 msh 命令列表中 /
MSH_CMD_EXPORT(readwrite_sample, readwrite sample);

static void rename_sample(void)
{
rt_kprintf(“%s => %s”, “/text.txt”, “/text1.txt”);

if (rename("/text.txt", "/text1.txt") < 0)
    rt_kprintf("[error!]\n");
else
    rt_kprintf("[ok!]\n");
}
/ 导出到 msh 命令列表中 /
MSH_CMD_EXPORT(rename_sample, rename sample);

static void stat_sample(void)
{
int ret;
struct stat buf;
ret = stat(“/text.txt”, &buf);
if(ret == 0)
rt_kprintf(“text.txt file size = %d\n”, buf.st_size);
else
rt_kprintf(“text.txt file not fonud\n”);
}
/ 导出到 msh 命令列表中 /
MSH_CMD_EXPORT(stat_sample, show text.txt stat sample);

static void mkdir_sample(void)
{
int ret;

/* 创建目录 */
ret = mkdir("/dir_test", 0x777);
if (ret < 0)
{
    /* 创建目录失败 */
    rt_kprintf("dir error!\n");
}
else
{
    /* 创建目录成功 */
    rt_kprintf("mkdir ok!\n");
}
}
/ 导出到 msh 命令列表中 /
MSH_CMD_EXPORT(mkdir_sample, mkdir sample);

3.下载测试

1.SDIO驱动使用后,可以通过串口调试,可以看到SD卡信息

1.jpg

可以通过list_device 命令可以看到 sd0 的使用,可以通过上面的写好代码,已经使用想要的命令readwrite_sample,rename_sample,mkdir_sample,stat_sample, 可以实现读写,改名,新建文件夹,SD卡里面的文件,查询文件相关信息。

4.心得

很早就拿到【中科蓝讯】AB32VG1开发板了,工作上太忙了,现在才写测, SDIO的使用早在其他开发板测试过,直到主要的是挂载文件系统的时候,由于SD卡初始化会延时,需要延时挂载,或者想项目中使用独立线程挂载,挂载成功后退出线程。有个缺点,fatfs文件系统无法读取大文件。

原作者:ljhya

更多回帖

发帖
×
20
完善资料,
赚取积分