去年搞了一年的算法,今年终于得空继续学学PYNQ,参考了网上的例子,发现发现SD卡读写是如此简单。
第一步:在zynq中接口设置中选中SD卡,在pynq的默认配置中已经选中SD卡,所以这里不需要任何操作。
第二步:在SDK中添加xilffs库
第三步:添加函数文件sd.c
复制代码
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
114
115
116
117
118
119
120
121
122
123
124
125
126/****************************************************************************** * * Copyright (C) 2009 - 2014 Xilinx, Inc. All rights reserved. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * Use of the Software is limited solely to applications: * (a) running on a Xilinx device, or * (b) that interact with a Xilinx device through a bus or interconnect. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * XILINX BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. * * Except as contained in this notice, the name of the Xilinx shall not be used * in advertising or otherwise to promote the sale, use or other dealings in * this Software without prior written authorization from Xilinx. * ******************************************************************************/ /* * helloworld.c: simple test application * * This application configures UART 16550 to baud rate 9600. * PS7 UART (Zynq) is not initialized by this application, since * bootrom/bsp configures it to baud rate 115200 * * ------------------------------------------------ * | UART TYPE BAUD RATE | * ------------------------------------------------ * uartns550 9600 * uartlite Configurable only in HW design * ps7_uart 115200 (configured by bootrom/bsp) */ #include "sd.h" int SD_Init() { FRESULT rc; rc = f_mount(&fatfs,"",0); if(rc) { xil_printf("ERROR: f_mount returned %drn",rc); return XST_FAILURE; } return XST_SUCCESS; } int SD_Transfer_read(char *FileName,u32 DestinationAddress,u32 ByteLength) { FIL fil; FRESULT rc; UINT br; rc=f_open(&fil,FileName,FA_READ); if(rc) { xil_printf("ERROR:f_open returned %drn",rc); return XST_FAILURE; } rc = f_lseek(&fil,0); if(rc) { xil_printf("ERROR:f_open returned %drn",rc); return XST_FAILURE; } rc = f_read(&fil,(void*)DestinationAddress,ByteLength,&br); if(rc) { xil_printf("ERROR:f_open returned %drn",rc); return XST_FAILURE; } rc = f_close(&fil); if(rc) { xil_printf("ERROR:f_open returned %drn",rc); return XST_FAILURE; } return XST_SUCCESS; } int SD_Transfer_write(char *FileName,u32 SourceAddress,u32 ByteLength) { FIL fil; FRESULT rc; UINT bw; rc = f_open(&fil,FileName,FA_CREATE_ALWAYS | FA_WRITE); if(rc) { xil_printf("ERROR : f_open returned %drn",rc); return XST_FAILURE; } rc = f_lseek(&fil, 0); if(rc) { xil_printf("ERROR : f_lseek returned %drn",rc); return XST_FAILURE; } rc = f_write(&fil,(void*) SourceAddress,ByteLength,&bw); if(rc) { xil_printf("ERROR : f_write returned %drn", rc); return XST_FAILURE; } rc = f_close(&fil); if(rc){ xil_printf("ERROR : f_close returned %drn",rc); return XST_FAILURE; } return XST_SUCCESS; }
头文件:
复制代码
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/****************************************************************************** * * Copyright (C) 2009 - 2014 Xilinx, Inc. All rights reserved. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * Use of the Software is limited solely to applications: * (a) running on a Xilinx device, or * (b) that interact with a Xilinx device through a bus or interconnect. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * XILINX BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. * * Except as contained in this notice, the name of the Xilinx shall not be used * in advertising or otherwise to promote the sale, use or other dealings in * this Software without prior written authorization from Xilinx. * ******************************************************************************/ /* * helloworld.c: simple test application * * This application configures UART 16550 to baud rate 9600. * PS7 UART (Zynq) is not initialized by this application, since * bootrom/bsp configures it to baud rate 115200 * * ------------------------------------------------ * | UART TYPE BAUD RATE | * ------------------------------------------------ * uartns550 9600 * uartlite Configurable only in HW design * ps7_uart 115200 (configured by bootrom/bsp) */ #include "platform.h" #include "xparameters.h" #include "xil_printf.h" #include "ff.h" #include "xdevcfg.h" static FATFS fatfs; int SD_Init(); int SD_Transfer_read(char *FileName,u32 DestinationAddress,u32 ByteLength); int SD_Transfer_write(char *FileName,u32 SourceAddress,u32 ByteLength);
第四步:调用测试
main函数如下所示:
复制代码
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#define FILE "test.txt" int main() { init_platform(); int rc; const char src_str[] = "hsp test sd card write and read!"; u32 len = strlen(src_str); rc = SD_Init(); if(XST_SUCCESS != rc) { xil_printf("fail to open SD Card~nr"); } else { xil_printf("success to open SD Card~nr"); } rc = SD_Transfer_write(FILE,(u32)src_str,(len/256+1)*256); xil_printf("%d",(len/256+1)*256); if(XST_SUCCESS != rc) { xil_printf("fail to write SD Card~nr"); } else { xil_printf("success to write SD Card~nr"); } char dest_str[33]; rc = SD_Transfer_read(FILE,(u32)dest_str,(len+1)); if(XST_SUCCESS != rc) { xil_printf("fail to read SD Card~nr"); } else { xil_printf("success to read SD Card~nr"); } xil_printf("%srn",dest_str); printf("SD Card Write and Read Success~"); cleanup_platform(); return 0; }
调用注意事项为:有时候重新编译报错,需要重新添加xilffs库。
最后
以上就是英俊紫菜最近收集整理的关于PYNQ裸跑之读写SD卡的全部内容,更多相关PYNQ裸跑之读写SD卡内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复