📄 filesys.c
字号:
#include "filesys.h"
#define FAT_INI_VAR
#include "FAT.H"
extern UINT8 sd_u;
/********************************************************************************************************************
** 函数名称: int MyFileOpen(char *file_name, UINT8 mode)
** Name: int MyFileOpen(char *file_name, UINT8 mode)
** 功能描述: 打开文件
** Function: Open the file
** 输 入: UINT8 mode :打开方式,0:读 1: 写
char *file_name :文件名称
** Input: UINT8 mode :mothed of opening file
char *file_name :filename
** 输 出: >=0: 打开文件长度 -1 :错误
** Output: >=0: the length of the file openning -1: error code
*********************************************************************************************************************/
int MyFileOpen(char *file_name, UINT8 mode)
{
UINT8 i;
if(mode == 0) //read
{
strcpy( fat_cmd.open.buffer, file_name );
i = fat_fopen();
if(i!=F_OK)
{
printf("Error: File Open() \n");
return -1;
}
return fat_file_length;
}
else
if(mode == 1) //write
{
strcpy( fat_cmd.open.buffer, file_name );
i = fat_fopen();
if(i == F_OK)
{
fat_fclose( );
strcpy( fat_cmd.del.buffer, file_name);
i =fat_fdelete();
if( i!=F_OK )
{
printf("Error: cannot delete the file \n");
return -1;
}
strcpy( fat_cmd.create.buffer, file_name );
i = fat_fcreate( F_CREATE_ALWAYS );
if(i != F_OK)
{
printf("Error: cannot create the file \n");
return -1;
}
}
else
{
printf("Information: file not exist, create file ... \n");
strcpy( fat_cmd.create.buffer, file_name );
i = fat_fcreate( F_CREATE_ALWAYS );
if(i != F_OK)
{
printf("Error: cannot create the file \n");
return -1;
}
}
return 0;
}
return -1;
}
/********************************************************************************************************************
** 函数名称: UINT8 MyFileClose(UINT8 mode, UINT32 filesize)
** Name: UINT8 MyFileClose(UINT8 mode, UINT32 filesize)
** 功能描述: 关闭文件
** Function: Close the file
** 输 入: UINT8 mode :关闭方式,0:读 1: 写
UINT32 filesize :文件大小
** Input: UINT8 mode :mothed of closing file
UINT32 filesize :length of the file
** 输 出: 0: 正确 >0: 错误码
** Output: 0: right >0: error code
*********************************************************************************************************************/
UINT8 MyFileClose(UINT8 mode, UINT32 filesize)
{
UINT8 i;
if(mode == 1 )
{
fat_cmd.modify.size = filesize;
fat_cmd.modify.date = 0XFFFF;
fat_cmd.modify.time = 0XFFFF;
fat_cmd.modify.attr = 0XFF;
i = fat_fmodify();
if( i!=F_OK )
{
printf("Error: fat_fmodify() \n");
return 1;
}
}
fat_fclose();
return 0;
}
/********************************************************************************************************************
** 函数名称: UINT32 MyFileWrite( UINT8 *buf, UINT32 len)
** Name: UINT32 MyFileWrite( UINT8 *buf, UINT32 len)
** 功能描述: 写文件
** Function: Write the file
** 输 入: UINT8 *buf :读取缓存区
UINT32 len :读取数据长度
** Input: UINT8 *buf :read buffer
UINT32 len :read length
** 输 出: 实际写文件的长度
** Output: the actual writting length
*********************************************************************************************************************/
UINT32 MyFileWrite( UINT8 *buf, UINT32 len)
{
UINT8 i;
UINT32 written_len;
while(len>FAT_FILE_BUF_LEN)
{
memcpy(&buf[written_len],fat_file_buffer, FAT_FILE_BUF_LEN);
fat_cmd.write.sector = FAT_FILE_BUF_LEN / SEC_SIZE;
i = fat_fwrite();
if( i!=F_OK )
{
printf("Error: fat_fread() \n");
return written_len;
}
len = len - FAT_FILE_BUF_LEN;
written_len = written_len + FAT_FILE_BUF_LEN;
}
memcpy(fat_file_buffer,&buf[written_len],len);
memset(&fat_file_buffer[len-1],' ',FAT_FILE_BUF_LEN - len);
fat_cmd.write.sector = len;
i = fat_fwrite();
if( i!=F_OK )
{
printf("Error: fat_fread() \n");
}
written_len = written_len + len;
return written_len;
}
/********************************************************************************************************************
** 函数名称: UINT32 MyFileRead(UINT8 *buf, UINT32 len)
** Name: UINT32 MyFileRead(UINT8 *buf, UINT32 len)
** 功能描述: 读文件
** Function: Read the file
** 输 入: UINT8 *buf :读取缓存区
UINT32 len :读取数据长度
** Input: UINT8 *buf :read buffer
UINT32 len :read length
** 输 出: 实际读取文件的长度
** Output: the actual reading length
*********************************************************************************************************************/
UINT32 MyFileRead(UINT8 *buf, UINT32 len)
{
UINT8 i;
UINT32 readed_len;
readed_len = 0;
while(len>FAT_FILE_BUF_LEN)
{
fat_cmd.read.sector = FAT_FILE_BUF_LEN / SEC_SIZE;
i = fat_fread();
if( i!=F_OK )
{
printf("Error: fat_fread() \n");
return readed_len;
}
memcpy(&buf[readed_len],fat_file_buffer, FAT_FILE_BUF_LEN);
len = len - FAT_FILE_BUF_LEN;
readed_len = readed_len + FAT_FILE_BUF_LEN;
}
fat_cmd.read.sector = len;
i = fat_fread();
if( i!=F_OK )
{
printf("Error: fat_fread() \n");
}
memcpy(&buf[readed_len],fat_file_buffer, len);
readed_len = readed_len + len;
return readed_len;
}
/********************************************************************************************************************
** 函数名称: UINT8 MyDiskSelect(UINT8 dev_id)
** Name: UINT8 MyDiskSelect(UINT8 dev_id)
** 功能描述: 选择磁盘
** Function: select SD or U Disk
** 输 入: UINT8 dev_id : 磁盘号 0:SD 1:U盘
** Input: UINT8 dev_id : Disk Number, 0 select SD, 1 select U Disk
** 输 出: 0: 正确 >0: 错误码
** Output: 0: right >0: error code
*********************************************************************************************************************/
UINT8 MyDiskSelect(UINT8 dev_id)
{
sd_u = dev_id;
return fat_init();
}
/********************************************************************************************************************
** 函数名称: UINT8 MyDiskQuery(UINT32* mDiskFreeSectors, UINT32* mDiskTotalSector)
** Name: UINT8 MyDiskQuery(UINT32* mDiskFreeSectors, UINT32* mDiskTotalSector)
** 功能描述: 查询磁盘总空间和剩余空间大小
** Function: Query the total volume and the free volume of the Disk
** 输 入: UINT32* mDiskFreeSectors : 返回剩余扇区数目
UINT32* mDiskTotalSector :返回总扇区数目
** Input: UINT32* mDiskFreeSectors : return the free sector number of the disk
UINT32* mDiskTotalSector :return the total sector number of the disk
** 输 出: 0: 正确 >0: 错误码
** Output: 0: right >0: error code
*********************************************************************************************************************/
UINT8 MyDiskQuery(UINT32* mDiskFreeSectors, UINT32* mDiskTotalSector)
{
UINT8 i;
i = fat_disk_query( );
if( i==F_OK )
{
*mDiskFreeSectors = fat_cmd.query.free_sector;
*mDiskTotalSector = fat_cmd.query.total_sector;
return 0;
}
else
return 1;
}
/********************************************************************************************************************
** 函数名称: UINT8 MyDeleteFile(char* filename)
** Name: UINT8 MyDeleteFile(char* filename)
** 功能描述: 删除文件
** Function: Delete the file
** 输 入: char* filename : 文件名称
** Input: char* filename : file name
** 输 出: 0: 正确 >0: 错误码
** Output: 0: right >0: error code
*********************************************************************************************************************/
UINT8 MyDeleteFile(char* filename)
{
strcpy( fat_cmd.del.buffer, filename );
return fat_fdelete();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -