【匠芯创D133CBS】--7.SD卡挂载与文件读写
<div class='showpostmsg'><p> 本篇讲述SD卡挂载与文件读写。通过按键触发读SD卡文件,初次没有则创建,读到文件后打印出来。</p><p> </p>
<p><strong><span style="color:#0000ff;">一.硬件原理</span></strong></p>
<p> 开发板主控SOC有SD/MMC控制器(简称SDMC),支持访问 SD 卡/ eMMC/ SDIO 设备,有硬件原理如下可知使用到SDMC1,开发板上有SD卡座,插上SD卡。</p>
<div style="text-align: center;"></div>
<div style="text-align: center;">图1:SD卡接口原理</div>
<p><strong><span style="color:#0000ff;">二.代码准备</span></strong></p>
<p> 本部分分为SD卡挂载 和 SD卡文件读写,下面分别展开讲述。</p>
<p><strong><span style="color:#cc00ff;"><span style="background-color:#dddddd;">●.SD卡挂载</span></span></strong></p>
<p>1.配置SDMC接口,这里根据硬件原理使用SDMC1</p>
<div style="text-align: center;"></div>
<div style="text-align: center;">图2:SD接口配置</div>
<p>2.设置自动挂载,这里稍微改写board.c里挂载表mount_table,如下</p>
<div style="text-align: center;"></div>
<div style="text-align: center;">图3:设置自动挂载</div>
<p>3.热拔插配置。目前Lite 下只实现了 SDMC1 热插拔,刚好可以支持到。</p>
<div style="text-align: center;"></div>
<div style="text-align: center;">图4:SD卡热拔插配置</div>
<p> 至此,SD卡挂载配置与准备完成。</p>
<p> </p>
<p><strong><span style="color:#cc00ff;"><span style="background-color:#dddddd;">●.SD卡文件读写</span></span></strong></p>
<p> 此部分实现的功能是:使用按键触发文件读写,初次SD卡没有打开读的文件则创建,读到文件则直接打印出来。</p>
<p>1.SD卡读写使用到文件系统,下面配置文件系统。此处需要打开 RT-Thread 中的 elm(即 FatFS 文件系统)</p>
<div style="text-align: center;"></div>
<div style="text-align: center;">图5:文件系统配置</div>
<p>2.定义SD卡根路径,申请文件读写内存大小FILE_DATA_LEN ,以及消息发送数据结构体及消息分类。</p>
<pre>
<code>#define SD_PATH "sdcard/"
#define FILE_DATA_LEN_64K 64*1024
#define FILE_DATA_LEN_16K 16*1024
#define FILE_DATA_LEN 5*1024
typedef enum
{
MSG_LED_BUZZER,
MSG_SD,
}MSG_EVT_TYPE;
typedef struct
{
uint8_t msg_type;
uint16_t msg_len;
uint8_t led_value;
uint8_t buzzer_pwm_percent;
}msg_control;
</code></pre>
<p>3.在按键中断发送SD卡操作消息队列</p>
<pre>
<code>static void key_irq_handler(void *args)
{
unsigned int ret;
u32 pin = *((u32 *)(args));
printf("Key INT \r\n");
memset(&msg_control_t,0,sizeof(msg_control));
msg_control_t.msg_type=MSG_SD;
msg_control_t.msg_len=sizeof(msg_control_t);
app_msg_send(&msg_control_t,sizeof(msg_control_t));
}</code></pre>
<p>4.在消息队列接收任务里实现SD卡文件的读写。里面添加的日志可以便于后面测验。</p>
<pre>
<code>void app_thread_entry(void *parameter)
{
FILE *fp;
char file_path = { 0 };
char *data;
int ret;
int pin;
msg_control msg_control_p;
while(1)
{
if(rt_mq_recv(&user_mq, &msg_control_p, sizeof(msg_control_p), RT_WAITING_FOREVER)==RT_EOK)
{
rt_kprintf("Receive msg quene.msg_type:%d,msg_len:%d\r\n",msg_control_p.msg_type,msg_control_p.msg_len);
switch(msg_control_p.msg_type)
{
case MSG_LED_BUZZER:
rt_kprintf("led_value:%d,buzzer_pwm_percent:%d%%\r\n",\
msg_control_p.led_value,msg_control_p.buzzer_pwm_percent);
pin = rt_pin_get(LED_PIN);
rt_pin_write(pin, msg_control_p.led_value);
hal_pwm_set(BUZZER_PWM_CH, msg_control_p.buzzer_pwm_percent*BUZZER_FREQ_PARAM/100, BUZZER_FREQ_PARAM,PWM_SET_CMPA_CMPB);
break;
case MSG_SD:
data = aicos_malloc_align(0, FILE_DATA_LEN, CACHE_LINE_SIZE);
if (data == NULL)
{
rt_kprintf("Low memory\r\n");
return;
}
rt_kprintf("To Read File...\r\n");
rt_sprintf(file_path, "%s%s", SD_PATH,"test.txt");
fp=fopen(file_path,"rb");
if(fp==NULL)
{
rt_kprintf("Open read file %s failed!\n", file_path);
fclose(fp);
fp=fopen(file_path,"wb");
if (fp == NULL)
{
printf("create file %s failed!\n", file_path);
}
else
{
rt_kprintf("Open write file %s OK!\n", file_path);
rt_sprintf(data, "%s","你好,欢迎使用匠芯创开发板");
ret=fwrite(data, sizeof(char), FILE_DATA_LEN, fp);
{
rt_kprintf("Write file Success! size:%d\n", ret);
rt_kprintf("Write file Content:\r\n%s\r\n", data);
}
}
fclose(fp);
aicos_free_align(0, data);
}
else
{
rt_kprintf("Open read file %s OK!\n", file_path);
ret=fread(data, sizeof(char), FILE_DATA_LEN, fp);
if(ret)
{
rt_kprintf("Read file Success! size:%d\n", ret);
rt_kprintf("Read file Content:\r\n%s\r\n", data);
}
fclose(fp);
aicos_free_align(0, data);
}
break;
default :
break;
}
}
}
}
</code></pre>
<p> 下面是工程关于SD卡读写的逻辑功能代码结构。至此代码准备完毕。</p>
<div style="text-align: center;"></div>
<div style="text-align: center;">图6:SD卡读写工程</div>
<p> </p>
<p><strong><span style="color:#0000ff;">三.测试</span></strong></p>
<p> 编译烧录后,下面开始测验,同样分两部分进行:SD卡挂载,SD卡文件读写。</p>
<p>1.查询SD卡设备,使用命令list_device,如下</p>
<div style="text-align: center;"></div>
<div style="text-align: center;">图7:SD卡识别</div>
<p>2.按一下WEAKUP按键,可以看到没有文件则创建文件并写入数据。再按之后,可以读出文件。命令切入到SD卡下,可以查询到创建了test.txt文件。</p>
<div style="text-align: center;"></div>
<div style="text-align: center;">图8:SD卡读写日志</div>
<p> </p>
<p> 至此,成功挂载SD卡,并实现了文件的读写。</p>
</div><script> var loginstr = '<div class="locked">查看本帖全部内容,请<a href="javascript:;" style="color:#e60000" class="loginf">登录</a>或者<a href="https://bbs.eeworld.com.cn/member.php?mod=register_eeworld.php&action=wechat" style="color:#e60000" target="_blank">注册</a></div>';
if(parseInt(discuz_uid)==0){
(function($){
var postHeight = getTextHeight(400);
$(".showpostmsg").html($(".showpostmsg").html());
$(".showpostmsg").after(loginstr);
$(".showpostmsg").css({height:postHeight,overflow:"hidden"});
})(jQuery);
} </script><script type="text/javascript">(function(d,c){var a=d.createElement("script"),m=d.getElementsByTagName("script"),eewurl="//counter.eeworld.com.cn/pv/count/";a.src=eewurl+c;m.parentNode.insertBefore(a,m)})(document,523)</script> <p>666</p>
页:
[1]