Skip to content

sdk例程:Fatfs相关的应用函数

minichao9901 edited this page Jun 13, 2024 · 2 revisions

FATFS_USE.h

#ifndef ACZ702_FATFS_USE_H_
#define ACZ702_FATFS_USE_H_

#include "COMMON.h"
#include "../FATFS/ff.h"

int sd_mount();
int sd_write_data(char *file_name,u32 src_addr,u32 byte_len);
int sd_read_data(char *file_name,u32 src_addr,u32 byte_len);

int demo_find_files (void);
int demo_scan_files (void);
int demo_list_dir (void);
int demo_get_free (void);

extern FATFS fs;

#endif /* ACZ702_FATFS_USE_H_ */

FATFS_USE.c

#include "FATFS_USE.h"

FATFS fs;                         //文件系统
static BYTE work[FF_MAX_SS];

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

    //注册一个工作区(挂载分区文件系统)
    //在使用任何其它文件函数之前,必须使用f_mount函数为每个使用卷注册一个工作区
	status = f_mount(&fs, Path, 1);  //挂载SD卡
	if (status != FR_OK) {
		xil_printf("Volume is not FAT formated; formating FAT\r\n");
		//格式化SD卡
		status = f_mkfs(Path, FM_ANY, 0, work, sizeof work);
		if (status != FR_OK) {
			xil_printf("Unable to format fs\r\n");
			return -1;
		}
		//格式化之后,重新挂载SD卡
		status = f_mount(&fs, Path, 1);
		if (status != FR_OK) {
			xil_printf("Unable to mount fs\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函数返回已写入的字节数
    FRESULT status;

    //打开一个文件,如果不存在,则创建一个文件
    status=f_open(&fil,file_name,FA_CREATE_ALWAYS | FA_WRITE);
    xil_printf("%d\n", status);
    //移动打开的文件对象的文件读/写指针     0:指向文件开头
    f_lseek(&fil, 0);
    //向文件中写入数据
    status==f_write(&fil,(void*) src_addr,byte_len,&bw);
    xil_printf("%d\n", status);
    xil_printf("%d\r\n", 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);
    xil_printf("%d\r\n", br);
    //关闭文件
    f_close(&fil);
    return 0;
}

/* Search a directory for objects and display it */

void find_files (const char *search)
{
    FRESULT fr;     /* Return value */
    DIR dj;         /* Directory object */
    FILINFO fno;    /* File information */

    //fr = f_findfirst(&dj, &fno, "", "????????.JPG"); /* Start to search for photo files */
    fr = f_findfirst(&dj, &fno, "0:", search); /* Start to search for photo files */

    while (fr == FR_OK && fno.fname[0]) {         /* Repeat while an item is found */
        printf("%s\n", fno.fname);                /* Print the object name */
        fr = f_findnext(&dj, &fno);               /* Search for next item */
    }

    f_closedir(&dj);
}

/* List contents of a directory */

FRESULT list_dir (const char *path)
{
    FRESULT res;
    DIR dir;
    FILINFO fno;
    int nfile, ndir;


    res = f_opendir(&dir, path);                       /* Open the directory */
    if (res == FR_OK) {
        nfile = ndir = 0;
        for (;;) {
            res = f_readdir(&dir, &fno);                   /* Read a directory item */
            if (res != FR_OK || fno.fname[0] == 0) break;  /* Error or end of dir */
            if (fno.fattrib & AM_DIR) {            /* Directory */
                printf("   <DIR>   %s\n", fno.fname);
                ndir++;
            } else {                               /* File */
                printf("%10u %s\n", fno.fsize, fno.fname);
                nfile++;
            }
        }
        f_closedir(&dir);
        printf("%d dirs, %d files.\n", ndir, nfile);
    } else {
        printf("Failed to open \"%s\". (%u)\n", path, res);
    }
    return res;
}

/* Recursive scan of all items in the directory */

FRESULT scan_files (
    char* path        /* Start node to be scanned (***also used as work area***) */
)
{
    FRESULT res;
    DIR dir;
    UINT i;
    static FILINFO fno;


    res = f_opendir(&dir, path);                       /* Open the directory */
    if (res == FR_OK) {
        for (;;) {
            res = f_readdir(&dir, &fno);                   /* Read a directory item */
            if (res != FR_OK || fno.fname[0] == 0) break;  /* Break on error or end of dir */
            if (fno.fattrib & AM_DIR) {                    /* It is a directory */
                i = strlen(path);
                sprintf(&path[i], "/%s", fno.fname);
                res = scan_files(path);                    /* Enter the directory */
                if (res != FR_OK) break;
                path[i] = 0;
            } else {                                       /* It is a file. */
                printf("%s/%s\n", path, fno.fname);
            }
        }
        f_closedir(&dir);
    }

    return res;
}

/******************************************************************************/
/******************************************************************************/
int demo_find_files (void)
{
    FATFS fs;
    FRESULT res;    
    char buff[256];


    res = f_mount(&fs, "0:", 1);
    if (res == FR_OK) {
        strcpy(buff, "*.bin");
        find_files(buff);
    }

    return res;
}

int demo_scan_files (void)
{
    FATFS fs;
    FRESULT res;
    char buff[256];


    res = f_mount(&fs, "0:", 1);
    if (res == FR_OK) {
        strcpy(buff, "/");
        res = list_dir(buff);
    }

    return res;
}


int demo_list_dir (void)
{
    FATFS fs;
    FRESULT res;
    char buff[256];


    res = f_mount(&fs, "0:", 1);
    if (res == FR_OK) {
        strcpy(buff, "/");
        res = scan_files(buff);
    }

    return res;
}

int demo_get_free (void)
{
    FATFS fs;
    FRESULT res;
    FATFS *pfs=&fs;
    DWORD fre_clust, fre_sect, tot_sect;

    res = f_mount(&fs, "0:", 1);

    /* Get volume information and free clusters of drive 1 */
    res = f_getfree("0:", &fre_clust, &pfs);

    /* Get total sectors and free sectors */
    tot_sect = (pfs->n_fatent - 2) * pfs->csize;
    fre_sect = fre_clust * pfs->csize;

    /* Print the free space (assuming 512 bytes/sector) */
    printf("%10lu KiB total drive space.\n%10lu KiB available.\n", tot_sect / 2, fre_sect / 2);    

    return res;
}



#if(0)
rec_buff_t rec_buffer;
qspi_buff_t qspi_buffer;
int demo(void)
{
	PS_UART_Init(&UartPs1,XPAR_PS7_UART_0_DEVICE_ID, XUARTPS_OPER_MODE_NORMAL, 115200);
    platform_init_fs();

	  PS_UART_RX(&rec_buffer);

    u32 err_cnt=0;
    memcpy(qspi_buffer.wr_buff, rec_buffer.rec_buff, rec_buffer.recv_length);
    qspi_buffer.wr_length=rec_buffer.recv_length;
	  qspi_buffer.rd_length=rec_buffer.recv_length;


    char file_name[30]="aa.bin";
    //SD卡写数据
    sd_write_data(file_name,(u32)qspi_buffer.wr_buff,qspi_buffer.wr_length);
    //SD卡读数据
    sd_read_data(file_name,(u32)qspi_buffer.rd_buff,qspi_buffer.rd_length);


	for (int Count = 0; Count < qspi_buffer.rd_length; Count++) {
		//printf("%d: %x--%x\n", Count, qspi_buffer.wr_buff[Count], qspi_buffer.rd_buff[Count]);
        if(qspi_buffer.wr_buff[Count]!=qspi_buffer.rd_buff[Count]){
            err_cnt++;
        }
	}
	xil_printf("Error_cnt=%d\r\n", err_cnt);

	return 0;
}
#endif
Clone this wiki locally