7. Use of M601 File System

1 File system API


The data structure and API introduced in this article can refer to the zyf_fs.h file in the SDK


1.1 Usage


Storage only supports FLASH storage. When you want to create or open a file or directory, you must use an absolute path. For example, if you want to create a file, you can set "/" like this.
·The ZYF_FS_GetDiskTotalSpace function can be used to obtain the total space size
of the Flash. The ZYF_FS_GetDiskFreeSpace function can be used to obtain the remaining size
of the Flash . The ZYF_FS_GetFileSize function is used to obtain the size of a specific file in bytes
. The ZYF_FS_Open function is used to create or open a file. The opening and access mode must be defined. For more information, please refer to the header file description of this function
. ZYF_FS_Read function ZYF_FS_Write function is used to read and write files, and the file must be confirmed to be opened
. ZYF_FS_Seek function ZYF_FS_GetFilePosition function is used to set or get the file pointer position, and the file must be confirmed to be opened
. ZYF_FS_Delete function ZYF_FS_CheckFile Function is used to delete and find files
·ZYF_FS_Close function is used to close files


1.2 API functions


1.2.1. ZYF_FS_Open
opens or creates a file with a specified name
· Function prototype
int32_t ZYF_FS_Open(const char *fileName, uint32_t Flag)
· Parameter
fileName: [input] file name. The maximum length is 252 characters, and the absolute path must be used, such as "/"
Flag:[input] Open and access mode, possible values ​​are as follows:
ZYF_FS_READ_WRITE, read and write
ZYF_FS_READ_ONLY, read only
ZYF_FS_CREATE, if the file exists, open the file; If the file does not exist, create the file:
ZYF_FS_CREATE_ALWAYS, create a new file; if the file exists, overwrite the file and clear the existing attributes

Return result
Success: Return a value to specify the file handle.
Failure: Return a negative number, indicating that the file failed to open

1.2.2 ZYF_FS_Close
closes the file with the specified handle
· Function prototype
void ZYF_FS_Close(int32_t fileHandle);
· Parameter
fileHandle: file handle. Handle
returned when opening the file for ZYF_FS_Open · Return value
None


1.2.3 ZYF_FS_Read
read file content
· Function prototype
int32_t ZYF_FS_Read(int32_t fileHandle, uint8_t *readBuffer,
uint32_t numberOfBytesToRead, uint32_t *numberOfBytesRead);
· Parameter
fileHandle: [input] File handle. The handle returned when the file is opened for ZYF_FS_Open
readBuffer:[output] Read data, point to the data buffer
for reading numberOfBytesToRead:[input] The length of the data to be read
numberOfBytesRead:[Output] The length of the data read from the file. Before performing operations or checking errors, set this value to zero
. Return value
Success FS_NO_ERROR
Failure, please refer to fs_error_enum

1.2.4 ZYF_FS_Write
This function is used to write files
· Function prototype
int32_t ZYF_FS_Write(int32_t fileHandle, uint8_t *writeBuffer,
uint32_t numberOfBytesToWrite, uint32_t *numberOfBytesWritten);
· Parameter
fileHandle: [input] File handle. The handle returned when opening the file for ZYF_FS_Open
writeBuffer:[input] The data to be written to the file, pointing to the data buffer to be read
numberOfBytesToWrite:[input] The length of the data to be written
numberOfBytesWritten:[Output] The length of the data that has been written to the file . Before performing operations or checking errors, set this value to zero
. Return value
Success FS_NO_ERROR
Failure, please refer to fs_error_enum


1.2.5 ZYF_FS_Seek
This function is used to manipulate the file pointer offset
· Function prototype
int32_t ZYF_FS_Seek(int32_t fileHandle, int32_t offset, uint32_t
whence);
· Parameter
fileHandle: [input] file handle. Handle returned when opening the file for ZYF_FS_Open
offset:[input] the number of bytes offset from the file pointer
whyce:[input] file pointer reference position, see Enum_FsSeekPos
·Return value

Successful return result is greater than or equal to 0 (file offset), failure please refer to fs_error_enum

1.2.6 ZYF_FS_GetFileSize
This function is used to get the file size
. Function prototype
int32_t ZYF_FS_GetFileSize(int32_t fileHandle, uint32_t *size);
· Parameter
fileHandle: [input] file handle. The handle returned when opening a file for ZYF_FS_Open
size:[output] The size of the retrieved file
· Return value
Success FS_NO_ERROR
failure, please refer to fs_error_enum


1.2.7 ZYF_FS_Delete
This function is used to delete files
· Function prototype
int32_t ZYF_FS_Delete(const char *fileName);
· Parameter
fileName: [input] The name of the file to be deleted. The maximum length is 252 characters, and the absolute path must be used, such as "/"
· Return value If
successful FS_NO_ERROR
failed, please refer to fs_error_enum

1.2.8 ZYF_FS_CheckFile
This function is used to check whether the file exists
·Function prototype

int32_t ZYF_FS_CheckFile(const char *fileName);
· Parameter
fileName: [input] The name of the file to be checked
· Return value
Success FS_NO_ERROR
failure, please refer to fs_error_enum

1.2.9 ZYF_FS_Rename
This function is used to rename files
· Function prototype
int32_t ZYF_FS_Rename(const char *fileName, const char *newName);
· Parameter
fileName: [input] The name of the file to be renamed. The maximum length is 252 characters, and an absolute path must be used, such as "/"
newName:[input] The new name of the file. The maximum length is 252 characters, and the absolute path must be used, such as "/"
· Return value If
successful FS_NO_ERROR
failed, please refer to fs_error_enum


1.2.10 ZYF_FS_GetDiskFreeSpace
This function is used to obtain disk free space
. Function prototype
int32_t ZYF_FS_GetDiskFreeSpace(, uint32_t *free_space);
· Parameter
free_space: [output] The size of the remaining disk space
. Return value If
successful FS_NO_ERROR
fails, please refer to fs_error_enum

1.2.11 ZYF_FS_CreateDir
This function is used to create a file directory
· Function prototype
int32_t ZYF_FS_CreateDir(constchar *dirName);
· Parameter dirName
: [input] directory name
· Return value
Success FS_NO_ERROR
failure, please refer to fs_error_enum


1.2.12 ZYF_FS_DeleteDir
This function is used to delete a file directory
· Function prototype
int32_tZYF_FS_DeleteDir (const char *dirName);
· Parameter
dirName: [input] directory name
· Return value
Success FS_NO_ERROR
failure, please refer to fs_error_enum

2 Introduction to file system routines


This chapter mainly introduces how to use example_fs.c in the SDK to test the file system function separately.
Compiling method: .\examples\build\ corresponds to the .bat file by double-clicking to execute or opening it to compile.
Generated file: .\out\corresponding directory\hex\M601_example_**.pac

Use ZYF_FS_GetDiskFreeSpace() to obtain the remaining disk space, use ZYF_FileWrite() to write 0x35 to the disk, and then use ZYF_FileRead() to read out the information in the disk.

#include <string.h>
#include "zyf_fs.h"
#include "zyf_trace.h"
#include "zyf_app.h"
#include "zyf_uart.h"
#include "zyf_thread.h"



#define USER_DIR "user"

#define USER_FILE "user_test.txt"

#define SD_USER_FILE "/user_test.txt"

static Uart_Param_t g_uart1param;

void UartWriteCallBack(void* Param) // general com
{
    Uart_Param_t *uartparam = (Uart_Param_t *)Param; 
    if(Param == NULL)
    {
        return;
    }    

    ZYF_UartWrite(uartparam->port,(uint8_t *)"UartWrite succeed\r\n",strlen("UartWrite succeed\r\n"));
    ZYF_UartWriteCallbackSwitch(uartparam->port,false);

}

void UartReadCallBack(void* Param) // 
{
    uint32_t recvlen = 0;
    Uart_Param_t *uartparam = (Uart_Param_t *)Param; 

    ZYF_LOG("Uart%d recv",uartparam->port);

    while(ZYF_UartRead(uartparam->port, &(uartparam->uartbuf[recvlen]), 1))
    {
        ZYF_LOG("recv :%02x",uartparam->uartbuf[recvlen]);
        recvlen++;
    }
    ZYF_UartWrite(uartparam->port,uartparam->uartbuf,recvlen);
    ZYF_UartWriteCallbackSwitch(uartparam->port,true);
}


static void AppUartInit(void)
{
    int32_t ret;
    g_uart1param.port = DEBUG_PORT;
    ZYF_UartRegister(g_uart1param.port, UartReadCallBack,&g_uart1param);
    ZYF_UartWriteCbRegister(g_uart1param.port,UartWriteCallBack,&g_uart1param);
    ZYF_UartOpen(g_uart1param.port, 115200, ZYF_FC_NONE);

    ZYF_LOG("AppUartInit");
    return;
}



uint32_t ZYF_FileRead(const char *path, void *dst, uint32_t size)
{
    int fd = ZYF_FS_Open(path, ZYF_RDONLY);
    if (fd < 0)
        return -1;

    uint32_t read_len  = 0;
    ZYF_FS_Read(fd, dst, size,&read_len);
    ZYF_FS_Close(fd);
    return read_len;
}

uint32_t ZYF_SD_FileRead(const char *path, void *dst, uint32_t size)
{
    int fd = ZYF_FS_SD_Open(path, ZYF_RDONLY);
    if (fd < 0)
        return -1;

    uint32_t read_len  = 0;
    ZYF_FS_Read(fd, dst, size,&read_len);
    ZYF_FS_Close(fd);
    return read_len;
}


uint32_t ZYF_FileWrite(const char *path, void *dst, uint32_t size)
{
    //check size


    int fd = ZYF_FS_Open(path, ZYF_RDWR | ZYF_CREAT | ZYF_TRUNC);
    if (fd < 0)
        return -1;

    uint32_t write_len  = 0;
    ZYF_FS_Write(fd, dst, size,&write_len);
    ZYF_FS_Close(fd);
    return write_len;
}

uint32_t ZYF_SD_FileWrite(const char *path, void *dst, uint32_t size)  //added by ldk for sdcard
{
    //check size


    int fd = ZYF_FS_SD_Open(path, ZYF_RDWR | ZYF_CREAT | ZYF_TRUNC);
    if (fd < 0)
        return -1;

    uint32_t write_len  = 0;
    ZYF_FS_Write(fd, dst, size,&write_len);
    ZYF_FS_Close(fd);
    return write_len;
}


void ZYF_FSTest(void)
{

    uint8_t buf[128] = {0};
    uint32_t len = 0;

    ZYF_FS_GetDiskFreeSpace( &len);
    ZYF_LOG("Free Space:%lu",len);



    #if 1
    memset(buf,0x35,sizeof(buf));
    len = ZYF_FileWrite(USER_FILE,buf,sizeof(buf));
    if(len != sizeof(buf))
    {
        ZYF_LOG("ZYF_FS_Write fail!ret:%d",len);
        return;
    }
    
    ZYF_LOG("ZYF_FS_Write succeed!");
    #endif
    
    len = 0;
    memset(buf,0x00,sizeof(buf));
    len = ZYF_FileRead(USER_FILE,buf,sizeof(buf));
    if(len != sizeof(buf))
    {
        ZYF_LOG("ZYF_FS_Read fail!ret:%d",len);
        return;
    }
    buf[127] = '\0';
    ZYF_LOG("ZYF_FS_Read data:%s",buf);

    if(ZYF_FS_CheckFile(USER_FILE))
    {
        ZYF_LOG("%s file exists",USER_FILE);
        //ZYF_FS_Delete(USER_FILE);
    }
    else
    {
        ZYF_LOG("%s file does not exist",USER_FILE);
    }
/* //unimplement
    memset(buf,0x35,sizeof(buf));
    len = ZYF_SD_FileWrite(SD_USER_FILE,buf,sizeof(buf));
    if(len != sizeof(buf))
    {
        ZYF_LOG("ZYF_SD_FileWrite fail!ret:%d",len);
        return;
    }
    
    ZYF_LOG("ZYF_SD_FileWrite succeed!");
    
    len = 0;
    memset(buf,0x00,sizeof(buf));
    len = ZYF_SD_FileRead(SD_USER_FILE,buf,sizeof(buf));
    if(len != sizeof(buf))
    {
        ZYF_LOG("ZYF_SD_FileRead fail!ret:%d",len);
        return;
    }
    buf[127] = '\0';
    ZYF_LOG("ZYF_SD_FileRead data:%s",buf);

    if(ZYF_FS_SD_CheckFile(SD_USER_FILE))
    {
        ZYF_LOG("%s file exists",SD_USER_FILE);
        //ZYF_FS_Delete(USER_FILE);
    }
    else
    {
        ZYF_LOG("%s file does not exist",SD_USER_FILE);
    }
 */   
}

void FileSystemThread_Example(void * Param)
{
    ZYF_MsgQ_t *ptMsg;
    ZYF_AppMsg_t tMsg;
    int iRet = -1;
    ptMsg = ZYF_MsgQCreate(10, sizeof(ZYF_AppMsg_t));
    ZYF_LOG("thread enter!");

    ZYF_FSTest();
    
    while (1) {
        ZYF_LOG("in while.");
        iRet = ZYF_MsgQGet(ptMsg, (void *)&tMsg);
        if (iRet < 0) {
            ZYF_LOG("Failed to get msg");
            ZYF_ThreadSleep(1000);
        }
    }

}


static void prvInvokeGlobalCtors(void)
{
    extern void (*__init_array_start[])();
    extern void (*__init_array_end[])();

    size_t count = __init_array_end - __init_array_start;
    for (size_t i = 0; i < count; ++i)
        __init_array_start[i]();
}


int appimg_enter(void *param)
{
    AppUartInit();
    ZYF_LOG("application image enter, param 0x%x", param);

    prvInvokeGlobalCtors();

    ZYF_ThreadCreate("FileSystemThread_Example", FileSystemThread_Example, NULL, ZYF_PRIORITY_HIGH, 10*1024);
    return 0;
}

void appimg_exit(void)
{
    OSI_LOGI(0, "application image exit");
}



 

Guess you like

Origin blog.csdn.net/w_hizyf_m/article/details/107085464