📄 main.i
字号:
/ * The FatFs module is a free software and there is no warranty.
/ * You can use, modify and/or redistribute it for personal, non-profit or
/ profit use without any restriction under your responsibility.
/ * Redistributions of source code must retain the above copyright notice.
/
/---------------------------------------------------------------------------/
/ Feb 26, 2006 R0.00 Prototype.
/ Apr 29, 2006 R0.01 First stable version.
/ Jun 01, 2006 R0.02 Added FAT12 support.
/ Removed unbuffered mode.
/ Fixed a problem on small (<32M) patition.
/ Jun 10, 2006 R0.02a Added a configuration option (_FS_MINIMUM).
/ Sep 22, 2006 R0.03 Added f_rename().
/ Changed option _FS_MINIMUM to _FS_MINIMIZE.
/ Dec 09, 2006 R0.03a Improved cluster scan algolithm to write files fast.
/ Feb 04, 2007 R0.04 Added FAT32 supprt.
/ Changed some interfaces incidental to FatFs.
/ Changed f_mountdrv() to f_mount().
/ Apr 01, 2007 R0.04a Added a capability of extending file size to f_lseek().
/ Added minimization level 3.
/ Fixed a problem in FAT32 support.
/ May 05, 2007 R0.04b Added a configuration option _USE_NTFLAG.
/ Added FSInfo support.
/ Fixed some problems corresponds to FAT32 support.
/ Fixed DBCS name can result FR_INVALID_NAME.
/ Fixed short seek (<= csize) collapses the file object.
/ Aug 25, 2007 R0.05 Changed arguments of f_read() and f_write().
/---------------------------------------------------------------------------*/
// CodeVisionAVR C Compiler
// (C) 1998-2005 Pavel Haiduc, HP InfoTech S.R.L.
// Prototypes for string functions
#pragma used+
char *strcat(char *str1,char *str2);
char *strcatf(char *str1,char flash *str2);
char *strchr(char *str,char c);
signed char strcmp(char *str1,char *str2);
signed char strcmpf(char *str1,char flash *str2);
char *strcpy(char *dest,char *src);
char *strcpyf(char *dest,char flash *src);
unsigned char strcspn(char *str,char *set);
unsigned char strcspnf(char *str,char flash *set);
unsigned int strlenf(char flash *str);
char *strncat(char *str1,char *str2,unsigned char n);
char *strncatf(char *str1,char flash *str2,unsigned char n);
signed char strncmp(char *str1,char *str2,unsigned char n);
signed char strncmpf(char *str1,char flash *str2,unsigned char n);
char *strncpy(char *dest,char *src,unsigned char n);
char *strncpyf(char *dest,char flash *src,unsigned char n);
char *strpbrk(char *str,char *set);
char *strpbrkf(char *str,char flash *set);
signed char strpos(char *str,char c);
char *strrchr(char *str,char c);
char *strrpbrk(char *str,char *set);
char *strrpbrkf(char *str,char flash *set);
signed char strrpos(char *str,char c);
char *strstr(char *str1,char *str2);
char *strstrf(char *str1,char flash *str2);
unsigned char strspn(char *str,char *set);
unsigned char strspnf(char *str,char flash *set);
char *strtok(char *str1,char flash *str2);
unsigned int strlen(char *str);
void *memccpy(void *dest,void *src,char c,unsigned n);
void *memchr(void *buf,unsigned char c,unsigned n);
signed char memcmp(void *buf1,void *buf2,unsigned n);
signed char memcmpf(void *buf1,void flash *buf2,unsigned n);
void *memcpy(void *dest,void *src,unsigned n);
void *memcpyf(void *dest,void flash *src,unsigned n);
void *memmove(void *dest,void *src,unsigned n);
void *memset(void *buf,unsigned char c,unsigned n);
#pragma used-
#pragma library string.lib
/*--------------------------------------------------------------------------/
/ Tiny-FatFs - FAT file system module include file R0.05 (C)ChaN, 2007
/
/ Ported To CodevisionAVR By Behzad Khazama www.khazama.com Sabzevar-IRAN 2007
/ tff.h current Port ver:1.0
/
/---------------------------------------------------------------------------/
/ FatFs module is an experimenal project to implement FAT file system to
/ cheap microcontrollers. This is a free software and is opened for education,
/ research and development under license policy of following trems.
/
/ Copyright (C) 2007, ChaN, all right reserved.
/
/ * The FatFs module is a free software and there is no warranty.
/ * You can use, modify and/or redistribute it for personal, non-profit or
/ profit use without any restriction under your responsibility.
/ * Redistributions of source code must retain the above copyright notice.
/
/---------------------------------------------------------------------------*/
/* The _MCU_ENDIAN defines which access method is used to the FAT structure.
/ 1: Enable word access.
/ 2: Disable word access and use byte-by-byte access instead.
/ When the architectural byte order of the MCU is big-endian and/or address
/ miss-aligned access results incorrect behavior, the _MCU_ENDIAN must be set
/ to 2. If it is not the case, it can be set to 1 for good code efficiency. */
/* Setting _FS_READONLY to 1 defines read only configuration. This removes
/ writing functions, f_write, f_sync, f_unlink, f_mkdir, f_chmod, f_rename
/ and useless f_getfree. */
/* The _FS_MINIMIZE option defines minimization level to remove some functions.
/ 0: Full function.
/ 1: f_stat, f_getfree, f_unlink, f_mkdir, f_chmod and f_rename are removed.
/ 2: f_opendir and f_readdir are removed in addition to level 1.
/ 3: f_lseek is removed in addition to level 2. */
/* To enable FAT32 support in addition of FAT12/16, set _FAT32 to 1. */
/* To enable FSInfo support on FAT32 volume, set _USE_FSINFO to 1. */
/* When _USE_SJIS is set to 1, Shift-JIS code transparency is enabled, otherwise
/ only US-ASCII(7bit) code can be accepted as file/directory name. */
/* When _USE_NTFLAG is set to 1, upper/lower case of the file name is preserved.
/ Note that the files are always accessed in case insensitive. */
/* Type definition for cluster number */
typedef DWORD CLUST;
typedef DWORD CLUST;
/* File system object structure */
typedef struct _FATFS {
WORD id; /* File system mount ID */
WORD n_rootdir; /* Number of root directory entries */
DWORD winsect; /* Current sector appearing in the win[] */
DWORD fatbase; /* FAT start sector */
DWORD dirbase; /* Root directory start sector */
DWORD database; /* Data start sector */
CLUST sects_fat; /* Sectors per fat */
CLUST max_clust; /* Maximum cluster# + 1 */
CLUST last_clust; /* Last allocated cluster */
CLUST free_clust; /* Number of free clusters */
DWORD fsi_sector; /* fsinfo sector */
BYTE fsi_flag; /* fsinfo dirty flag (1:must be written back) */
BYTE pad1;
BYTE fs_type; /* FAT sub type */
BYTE sects_clust; /* Sectors per cluster */
BYTE n_fats; /* Number of FAT copies */
BYTE winflag; /* win[] dirty flag (1:must be written back) */
BYTE win[512]; /* Disk access window for Directory/FAT/File */
} FATFS;
/* Directory object structure */
typedef struct _DIR {
WORD id; /* Owner file system mount ID */
WORD index; /* Current index */
FATFS* fs; /* Pointer to the owner file system object */
CLUST sclust; /* Start cluster */
CLUST clust; /* Current cluster */
DWORD sect; /* Current sector */
} DIR;
/* File object structure */
typedef struct _FIL {
WORD id; /* Owner file system mount ID */
BYTE flag; /* File status flags */
BYTE sect_clust; /* Left sectors in cluster */
FATFS* fs; /* Pointer to owner file system */
DWORD fptr; /* File R/W pointer */
DWORD fsize; /* File size */
CLUST org_clust; /* File start cluster */
CLUST curr_clust; /* Current cluster */
DWORD curr_sect; /* Current sector */
DWORD dir_sect; /* Sector containing the directory entry */
BYTE* dir_ptr; /* Ponter to the directory entry in the window */
} FIL;
/* File status structure */
typedef struct _FILINFO {
DWORD fsize; /* Size */
WORD fdate; /* Date */
WORD ftime; /* Time */
BYTE fattrib; /* Attribute */
char fname[8+1+3+1]; /* Name (8.3 format) */
} FILINFO;
/* File function return code (FRESULT) */
typedef enum {
FR_OK = 0, /* 0 */
FR_NOT_READY, /* 1 */
FR_NO_FILE, /* 2 */
FR_NO_PATH, /* 3 */
FR_INVALID_NAME, /* 4 */
FR_INVALID_DRIVE, /* 5 */
FR_DENIED, /* 6 */
FR_EXIST, /* 7 */
FR_RW_ERROR, /* 8 */
FR_WRITE_PROTECTED, /* 9 */
FR_NOT_ENABLED, /* 10 */
FR_NO_FILESYSTEM, /* 11 */
FR_INVALID_OBJECT /* 12 */
} FRESULT;
/*-----------------------------------------------------*/
/* FatFs module application interface */
FRESULT f_mount (BYTE, FATFS*); /* Mount/Unmount a logical drive */
FRESULT f_open (FIL*, const char*, BYTE); /* Open or create a file */
FRESULT f_read (FIL*, void*, UINT, UINT*); /* Read data from a file */
FRESULT f_write (FIL*, const void*, UINT, UINT*); /* Write data to a file */
FRESULT f_lseek (FIL*, DWORD); /* Move file pointer of a file object */
FRESULT f_close (FIL*); /* Close an open file object */
FRESULT f_opendir (DIR*, const char*); /* Open an existing directory */
FRESULT f_readdir (DIR*, FILINFO*); /* Read a directory item */
FRESULT f_stat (const char*, FILINFO*); /* Get file status */
FRESULT f_getfree (const char*, DWORD*, FATFS**); /* Get number of free clusters on the drive */
FRESULT f_sync (FIL*); /* Flush cached data of a writing file */
FRESULT f_unlink (const char*); /* Delete an existing file or directory */
FRESULT f_mkdir (const char*); /* Create a new directory */
FRESULT f_chmod (const char*, BYTE, BYTE); /* Change file/dir attriburte */
FRESULT f_rename (const char*, const char*); /* Rename/Move a file or directory */
/* User defined function to give a current time to fatfs module */
DWORD get_fattime (void); /* 31-25: Year(0-127 +1980), 24-21: Month(1-12), 20-16: Day(1-31) */
/* 15-11: Hour(0-23), 10-5: Minute(0-59), 4-0: Second(0-29 *2) */
/* File access control and file status flags (FIL.flag) */
/* FAT sub type (FATFS.fs_type) */
/* File attribute bits for directory entry */
/* Offset of FAT structure members */
/* Multi-byte word access macros */
/*-----------------------------------------------------------------------
/ Low level disk interface modlue include file R0.05 (C)ChaN, 2007
/-----------------------------------------------------------------------*/
static
static
FATFS *FatFs; /* Pointer to the file system objects (logical drive) */
static
WORD fsid; /* File system mount ID */
/*-------------------------------------------------------------------------
Module Private Functions
-------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------*/
/* Change window offset */
/*-----------------------------------------------------------------------*/
static
BOOL move_window ( /* TRUE: successful, FALSE: failed */
DWORD sector /* Sector number to make apperance in the FatFs->win */
) /* Move to zero only writes back dirty window */
{
DWORD wsect;
FATFS *fs = FatFs;
wsect = fs->winsect;
if (wsect != sector) { /* Changed current window */
BYTE n;
if (fs->winflag) { /* Write back dirty window if needed */
if (disk_write(0, fs->win, wsect, 1) != RES_OK)
return FALSE;
fs->winflag = 0;
if (wsect < (fs->fatbase + fs->sects_fat)) { /* In FAT area */
for (n = fs->n_fats; n >= 2; n--) { /* Refrect the change to all FAT copies */
wsect += fs->sects_fat;
disk_write(0, fs->win, wsect, 1);
}
}
}
if (sector) {
if (disk_read(0, fs->win, sector, 1) != RES_OK)
return FALSE;
fs->winsect = sector;
}
}
return TRUE;
}
/*-----------------------------------------------------------------------*/
/* Clean-up cached data */
/*-----------------------------------------------------------------------*/
static
FRESULT sync (void) /* FR_OK: successful, FR_RW_ERROR: failed */
{
FATFS *fs = FatFs;
fs->winflag = 1;
if (!move_window(0)) return FR_RW_ERROR;
/* Update FSInfo sector if needed */
if (fs->fs_type == 3 && fs->fsi_flag) {
fs->winsect = 0;
memset(fs->win, 0, 512);
*(volatile BYTE*)(&fs->win[510])=(BYTE)(0xAA55); *(volatile BYTE*)((&fs->win[510])+1)=(BYTE)((WORD)(0xAA55)>>8);
*(volatile BYTE*)(&fs->win[0])=(BYTE)(0x41615252); *(volatile BYTE*)((&fs->win[0])+1)=(BYTE)((WORD)(0x41615252)>>8); *(volatile BYTE*)((&fs->win[0])+2)=(BYTE)((DWORD)(0x41615252)>>16); *(volatile BYTE*)((&fs->win[0])+3)=(BYTE)((DWORD)(0x41615252)>>24);
*(volatile BYTE*)(&fs->win[484])=(BYTE)(0x61417272); *(volatile BYTE*)((&fs->win[484])+1)=(BYTE)((WORD)(0x61417272)>>8); *(volatile BYTE*)((&fs->win[484])+2)=(BYTE)((DWORD)(0x61417272)>>16); *(volatile BYTE*)((&fs->win[484])+3)=(BYTE)((DWORD)(0x61417272)>>24);
*(volatile BYTE*)(&fs->win[488])=(BYTE)(fs->free_clust); *(volatile BYTE*)((&fs->win[488])+1)=(BYTE)((WORD)(fs->free_clust)>>8); *(volatile BYTE*)((&fs->win[488])+2)=(BYTE)((DWORD)(fs->free_clust)>>16); *(volatile BYTE*)((&fs->win[488])+3)=(BYTE)((DWORD)(fs->free_clust)>>24);
*(volatile BYTE*)(&fs->win[492])=(BYTE)(fs->last_clust); *(volatile BYTE*)((&fs->win[492])+1)=(BYTE)((WORD)(fs->last_clust)>>8); *(volatile BYTE*)((&fs->win[492])+2)=(BYTE)((DWORD)(fs->last_clust)>>16); *(volatile BYTE*)((&fs->win[492])+3)=(BYTE)((DWORD)(fs->last_clust)>>24);
disk_write(0, fs->win, fs->fsi_sector, 1);
fs->fsi_flag = 0;
}
/* Make sure that no pending write process in the physical drive */
if (disk_ioctl(0, 0 , 0) != RES_OK) return FR_RW_ERROR;
return FR_OK;
}
/*-----------------------------------------------------------------------*/
/* Get a cluster status */
/*-----------------------------------------------------------------------*/
static
CLUST get_cluster ( /* 0,>=2: successful, 1: failed */
CLUST clust /* Cluster# to get the link information */
)
{
WORD wc, bc;
DWORD fatsect;
FATFS *fs = FatFs;
if (clust >= 2 && clust < fs->max_clust) { /* Valid cluster# */
fatsect = fs->fatbase;
switch (fs->fs_type) {
case 1 :
bc = (WORD)clust * 3 / 2;
if (!move_window(fatsect + bc / 512)) break;
wc = fs->win[bc % 512]; bc++;
if (!move_window(fatsect + bc / 512)) break;
wc |= (WORD)fs->win[bc % 512] << 8;
return (clust & 1) ? (wc >> 4) : (wc & 0xFFF);
case 2 :
if (!move_window(fatsect + clust / 256)) break;
return ((WORD)(((WORD)*(volatile BYTE*)((&fs->win[((WORD)clust * 2) % 512])+1)<<8)|(WORD)*(volatile BYTE*)(&fs->win[((WORD)clust * 2) % 512])));
case 3 :
if (!move_window(fatsect + clust / 128)) break;
return ((DWORD)(((DWORD)*(volatile BYTE*)((&fs->win[((WORD)clust * 4) % 512])+3)<<24)|((DWORD)*(volatile BYTE*)((&fs->win[((WORD)clust * 4) % 512])+2)<<16)|((WORD)*(volatile BYTE*)((&fs->win[((WORD)clust * 4) % 512])+1)<<8)|*(volatile BYTE*)(&fs->win[((WORD)clust * 4) % 512]))) & 0x0FFFFFFF;
}
}
return 1; /* There is no cluster information, or an error occured */
}
/*-----------------------------------------------------------------------*/
/* Change a cluster status */
/*-----------------------------------------------------------------------*/
static
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -