ZYNQ系統中實現FAT32檔案系統的SD卡讀寫之三 SDK程式設計除錯
阿新 • • 發佈:2019-01-26
匯入到SDK後直接模板生成一個HELLO WORLD專案,之後在XILINX TOOLS-》BOARD SUPPORT PACKAGE SETTING裡面設定選擇XILFFS。
XILFFS各項可以設定引數按照預設,如下圖:
之後修改main函式所在檔案,直接拷貝一下內容覆蓋原檔案 :
#include "platform.h" #include "xparameters.h" #include "xil_printf.h" #include "ff.h" #include "xdevcfg.h" static FATFS fatfs; int SD_Init() { FRESULT rc; rc = f_mount(&fatfs,"",0); if(rc) { xil_printf("ERROR : f_mount returned %d\r\n",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 %d\r\n",rc); return XST_FAILURE; } rc = f_lseek(&fil, 0); if(rc) { xil_printf("ERROR : f_lseek returned %d\r\n",rc); return XST_FAILURE; } rc = f_read(&fil, (void*)DestinationAddress,ByteLength,&br); if(rc) { xil_printf("ERROR : f_read returned %d\r\n",rc); return XST_FAILURE; } rc = f_close(&fil); if(rc) { xil_printf(" ERROR : f_close returned %d\r\n", 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 %d\r\n",rc); return XST_FAILURE; } rc = f_lseek(&fil, 0); if(rc) { xil_printf("ERROR : f_lseek returned %d\r\n",rc); return XST_FAILURE; } rc = f_write(&fil,(void*) SourceAddress,ByteLength,&bw); printf("SD_Transfer_write -> f_write result len is %d \n\r" ,bw ); if(rc) { xil_printf("ERROR : f_write returned %d\r\n", rc); return XST_FAILURE; } rc = f_close(&fil); if(rc){ xil_printf("ERROR : f_close returned %d\r\n",rc); return XST_FAILURE; } return XST_SUCCESS; } #define FILE "test123.txt" #define MAX_LEN 1024*1024*200 static char src_str[MAX_LEN] = { 0 } ; static char dst_str[MAX_LEN] = { 0 } ; int main() { int i,r; init_platform(); u32 len = strlen(src_str); r = SD_Init(); if ( XST_SUCCESS != r ){ printf("fail to open SD1\n\r"); return 1 ; } for(i=0;i<MAX_LEN;++i) src_str[i] = i + 5 ; printf("OK to open SD1\n\r"); printf("write SD1\n\r"); printf("%d\n",src_str[0]); SD_Transfer_write(FILE,(void *)src_str,(MAX_LEN));//當直接指定len時沒有寫出,需要指定較大的長度才會寫出,原因未知 printf("%d\n",src_str[0]); printf("write done SD1\n\r"); SD_Init(); printf("read SD1\n\r"); SD_Transfer_read(FILE,(void*)dst_str,(MAX_LEN)); printf("read done SD1\n\r"); printf("start check data ...\n\r"); for(i=0;i<MAX_LEN;++i) if (src_str[i]!=dst_str[i] ){ printf("error @ %d src %d <> dst %d \n\r",i,src_str[i],dst_str[i]); } print("SD write and read over!\r\n"); cleanup_platform(); return 0; }
之後連結JTAG直接下載到板子上線上執行,注意可以不少些PL部分,因為這裡僅僅是PS程式。
下一篇我重點分析一下這個程式碼,介紹一下注意事項。