0%

ZYNQ嵌入式之SD卡(基于PYNQ-Z2开发板)

本节主要通过Xilinx SDK自带的FATFS库中的示例,演示了SD卡的读写流程

SD卡读写TXT文本实验

  • 实验任务:通过Xilinx SDK自带的FATFS库,完成对TF卡中TXT文本读写的功能,并将读写测试结果通过串口打印出来

1.Vivado中的相关配置

image-20230928180317821

  • 通过查看用户手册可知CD信号连接到了PS MIO47的引脚
image-20230928180438940
  • 虽然但是,实验是基于pynq-z2的boards的,自动配置时已经将这些都配置好了

2.Vitis中添加xilffs库

image-20230929004159457

image-20230929004246281

  • 然后再重新编译一下工程就可以了

3.Vitis中的代码编写

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include "xparameters.h"
#include "xil_printf.h"
#include "ff.h"
#include "xdevcfg.h"

#define FILE_NAME "ZDYZ.txt" //定义文件名

const char src_str[30] = "www.openedv.com"; //定义文本内容
static FATFS fatfs; //文件系统

//初始化文件系统
int platform_init_fs()
{
FRESULT status;
TCHAR *Path = "0:/";
BYTE work[FF_MAX_SS];

//注册一个工作区(挂载分区文件系统)
//在使用任何其它文件函数之前,必须使用f_mount函数为每个使用卷注册一个工作区
status = f_mount(&fatfs, Path, 1); //挂载SD卡
if (status != FR_OK) {
xil_printf("Volume is not FAT formated; formating FAT\r\n");
//格式化SD卡
status = f_mkfs(Path, FM_FAT32, 0, work, sizeof work);
if (status != FR_OK) {
xil_printf("Unable to format FATfs\r\n");
return -1;
}
//格式化之后,重新挂载SD卡
status = f_mount(&fatfs, Path, 1);
if (status != FR_OK) {
xil_printf("Unable to mount FATfs\r\n");
return -1;
}
}
return 0;
}

//挂载SD(TF)卡
int sd_mount()
{
FRESULT status;
//初始化文件系统(挂载SD卡,如果挂载不成功,则格式化SD卡)
status = platform_init_fs();
if(status){
xil_printf("ERROR: f_mount returned %d!\n",status);
return XST_FAILURE;
}
return XST_SUCCESS;
}

//SD卡写数据
int sd_write_data(char *file_name,u32 src_addr,u32 byte_len)
{
FIL fil; //文件对象
UINT bw; //f_write函数返回已写入的字节数

//打开一个文件,如果不存在,则创建一个文件
f_open(&fil,file_name,FA_CREATE_ALWAYS | FA_WRITE);
//移动打开的文件对象的文件读/写指针 0:指向文件开头
f_lseek(&fil, 0);
//向文件中写入数据
f_write(&fil,(void*) src_addr,byte_len,&bw);
//关闭文件
f_close(&fil);
return 0;
}

//SD卡读数据
int sd_read_data(char *file_name,u32 src_addr,u32 byte_len)
{
FIL fil; //文件对象
UINT br; //f_read函数返回已读出的字节数

//打开一个只读的文件
f_open(&fil,file_name,FA_READ);
//移动打开的文件对象的文件读/写指针 0:指向文件开头
f_lseek(&fil,0);
//从SD卡中读出数据
f_read(&fil,(void*)src_addr,byte_len,&br);
//关闭文件
f_close(&fil);
return 0;
}

//main函数
int main()
{
int status,len;
char dest_str[30] = "";

status = sd_mount(); //挂载SD卡
if(status != XST_SUCCESS){
xil_printf("Failed to open SD card!\n");
return 0;
}
else
xil_printf("Success to open SD card!\n");

len = strlen(src_str); //计算字符串长度
//SD卡写数据
sd_write_data(FILE_NAME,(u32)src_str,len);
//SD卡读数据
sd_read_data(FILE_NAME,(u32)dest_str,len);

//比较写入的字符串和读出的字符串是否相等
if (strcmp(src_str, dest_str) == 0)
xil_printf("src_str is equal to dest_str,SD card test success!\n");
else
xil_printf("src_str is not equal to dest_str,SD card test failed!\n");

return 0;
}
  • 这里的话得创建一个空的C文件运行上述代码才能成功,如果创建的是空的C++文件运行上述代码则会报错,具体原因还未深究

3.实验结果

image-20230928185013950

  • MicroSD卡中的内容
image-20230928185133652
欢迎来到ssy的世界