⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tff.i

📁 This is the latest VS1053B chip interface routines combined with Elm Chan s FatFs library. Whole cod
💻 I
📖 第 1 页 / 共 5 页
字号:
	if (ofs > 0) {
		csize = (DWORD)fp->fs->csize * 512U;		/* Cluster size (byte) */
		if (ifptr > 0 &&
			(ofs - 1) / csize >= (ifptr - 1) / csize) {/* When seek to same or following cluster, */
			fp->fptr = (ifptr - 1) & ~(csize - 1);	/* start from the current cluster */
			ofs -= fp->fptr;
			clust = fp->curr_clust;
		} else {									/* When seek to back cluster, */
			clust = fp->org_clust;					/* start from the first cluster */
			if (clust == 0) {						/* If no cluster chain, create a new chain */
				clust = create_chain(0);
				if (clust == 1) goto fk_error;
				fp->org_clust = clust;
			}
			fp->curr_clust = clust;
		}
		if (clust != 0) {
			while (ofs > csize) {					/* Cluster following loop */
				if (fp->flag & 0x02) {			/* Check if in write mode or not */
					clust = create_chain(clust);	/* Force streached if in write mode */
					if (clust == 0) {				/* When disk gets full, clip file size */
						ofs = csize; break;
					}
				} else
					clust = get_cluster(clust);		/* Follow cluster chain if not in write mode */
				if (clust < 2 || clust >= fp->fs->max_clust) goto fk_error;
				fp->curr_clust = clust;
				fp->fptr += csize;
				ofs -= csize;
			}
			fp->fptr += ofs;
			fp->csect = (BYTE)(ofs / 512U);	/* Sector offset in the cluster */
			if (ofs % 512U) fp->csect++;
		}
	}

	if (fp->fptr > fp->fsize) {			/* Set changed flag if the file was extended */
		fp->fsize = fp->fptr;
		fp->flag |= 0x20;
	}

	return FR_OK;

fk_error:	/* Abort this file due to an unrecoverable error */
	fp->flag |= 0x80;
	return FR_RW_ERROR;
}




/*-----------------------------------------------------------------------*/
/* Create a directroy object                                             */
/*-----------------------------------------------------------------------*/

FRESULT f_opendir (
	DIR *dj,			/* Pointer to directory object to create */
	const char *path	/* Pointer to the directory path */
)
{
	FRESULT res;
	BYTE *dir;
	char fn[8+3+1];


	res = auto_mount(&path, 0);
	if (res == FR_OK) {
		res = trace_path(dj, fn, path, &dir);	/* Trace the directory path */
		if (res == FR_OK) {						/* Trace completed */
			if (dir) {							/* It is not the root dir */
				if (dir[11] & 0x10	) {	/* The entry is a directory */
	 				dj->clust =
						((DWORD)((WORD)(((WORD)*(volatile BYTE*)((&dir[20])+1)<<8)|(WORD)*(volatile BYTE*)(&dir[20]))) << 16) |
							(WORD)(((WORD)*(volatile BYTE*)((&dir[26])+1)<<8)|(WORD)*(volatile BYTE*)(&dir[26]));
					dj->sect = clust2sect(dj->clust);
					dj->index = 2;
				} else {						/* The entry is not a directory */
					res = FR_NO_FILE;
				}
			}
			dj->id = dj->fs->id;
		}
	}

	return res;
}




/*-----------------------------------------------------------------------*/
/* Read Directory Entry in Sequense                                      */
/*-----------------------------------------------------------------------*/

FRESULT f_readdir (
	DIR *dj,			/* Pointer to the directory object */
	FILINFO *finfo		/* Pointer to file information to return */
)
{
	FRESULT res;
	BYTE *dir, c;


	res = validate(dj->fs, dj->id);			/* Check validity of the object */
	if (res != FR_OK) return res;

	finfo->fname[0] = 0;
	while (dj->sect) {
		if (!move_window(dj->sect))
			return FR_RW_ERROR;
		dir = &dj->fs->win[(dj->index & 15) * 32];	/* pointer to the directory entry */
		c = dir[0];
		if (c == 0) break;							/* Has it reached to end of dir? */
		if (c != 0xE5 && !(dir[11] & 0x08	))	/* Is it a valid entry? */
			get_fileinfo(finfo, dir);
		if (!next_dir_entry(dj)) dj->sect = 0;		/* Next entry */
		if (finfo->fname[0]) break;					/* Found valid entry */
	}

	return FR_OK;
}




/*-----------------------------------------------------------------------*/
/* Get File Status                                                       */
/*-----------------------------------------------------------------------*/

FRESULT f_stat (
	const char *path,	/* Pointer to the file path */
	FILINFO *finfo		/* Pointer to file information to return */
)
{
	FRESULT res;
	DIR dj;
	BYTE *dir;
	char fn[8+3+1];


	res = auto_mount(&path, 0);
	if (res == FR_OK) {
		res = trace_path(&dj, fn, path, &dir);	/* Trace the file path */
		if (res == FR_OK) {						/* Trace completed */
			if (dir)	/* Found an object */
				get_fileinfo(finfo, dir);
			else		/* It is root dir */
				res = FR_INVALID_NAME;
		}
	}

	return res;
}




/*-----------------------------------------------------------------------*/
/* Truncate File                                                         */
/*-----------------------------------------------------------------------*/

FRESULT f_truncate (
	FIL *fp		/* Pointer to the file object */
)
{
	FRESULT res;
	CLUST ncl;


	res = validate(fp->fs, fp->id);		/* Check validity of the object */
	if (res != FR_OK) return res;
	if (fp->flag & 0x80) return FR_RW_ERROR;	/* Check error flag */
	if (!(fp->flag & 0x02)) return FR_DENIED;	/* Check access mode */

	if (fp->fsize > fp->fptr) {
		fp->fsize = fp->fptr;	/* Set file size to current R/W point */
		fp->flag |= 0x20;
		if (fp->fptr == 0) {	/* When set file size to zero, remove entire cluster chain */
			if (!remove_chain(fp->org_clust)) goto ft_error;
			fp->org_clust = 0;
		} else {				/* When truncate a part of the file, remove remaining clusters */
			ncl = get_cluster(fp->curr_clust);
			if (ncl < 2) goto ft_error;
			if (ncl < fp->fs->max_clust) {
				if (!put_cluster(fp->curr_clust, (CLUST)0x0FFFFFFF)) goto ft_error;
				if (!remove_chain(ncl)) goto ft_error;
			}
		}
	}

	return FR_OK;

ft_error:	/* Abort this file due to an unrecoverable error */
	fp->flag |= 0x80;
	return FR_RW_ERROR;
}




/*-----------------------------------------------------------------------*/
/* Get Number of Free Clusters                                           */
/*-----------------------------------------------------------------------*/

FRESULT f_getfree (
	const char *drv,	/* Pointer to the logical drive number (root dir) */
	DWORD *nclust,		/* Pointer to the variable to return number of free clusters */
	FATFS **fatfs		/* Pointer to pointer to corresponding file system object to return */
)
{
	FRESULT res;
	FATFS *fs;
	DWORD n, sect;
	CLUST clust;
	BYTE fat, f, *p;


	/* Get drive number */
	res = auto_mount(&drv, 0);
	if (res != FR_OK) return res;
	*fatfs = fs = FatFs;

	/* If number of free cluster is valid, return it without cluster scan. */
	if (fs->free_clust <= fs->max_clust - 2) {
		*nclust = fs->free_clust;
		return FR_OK;
	}

	/* Get number of free clusters */
	fat = fs->fs_type;
	n = 0;
	if (fat == 1) {
		clust = 2;
		do {
			if ((WORD)get_cluster(clust) == 0) n++;
		} while (++clust < fs->max_clust);
	} else {
		clust = fs->max_clust;
		sect = fs->fatbase;
		f = 0; p = 0;
		do {
			if (!f) {
				if (!move_window(sect++)) return FR_RW_ERROR;
				p = fs->win;
			}
			if (!1 || fat == 2) {
				if ((WORD)(((WORD)*(volatile BYTE*)((p)+1)<<8)|(WORD)*(volatile BYTE*)(p)) == 0) n++;
				p += 2; f += 1;
			} else {
				if ((DWORD)(((DWORD)*(volatile BYTE*)((p)+3)<<24)|((DWORD)*(volatile BYTE*)((p)+2)<<16)|((WORD)*(volatile BYTE*)((p)+1)<<8)|*(volatile BYTE*)(p)) == 0) n++;
				p += 4; f += 2;
			}
		} while (--clust);
	}
	fs->free_clust = n;
	if (fat == 3) fs->fsi_flag = 1;

	*nclust = n;
	return FR_OK;
}




/*-----------------------------------------------------------------------*/
/* Delete a File or Directory                                            */
/*-----------------------------------------------------------------------*/

FRESULT f_unlink (
	const char *path		/* Pointer to the file or directory path */
)
{
	FRESULT res;
	DIR dj;
	BYTE *dir, *sdir;
	DWORD dsect;
	char fn[8+3+1];
	CLUST dclust;


	res = auto_mount(&path, 1);
	if (res != FR_OK) return res;
	res = trace_path(&dj, fn, path, &dir);	/* Trace the file path */
	if (res != FR_OK) return res;			/* Trace failed */
	if (!dir) return FR_INVALID_NAME;		/* It is the root directory */
	if (dir[11] & 0x01	) return FR_DENIED;	/* It is a R/O object */
	dsect = dj.fs->winsect;
	dclust =
		((DWORD)((WORD)(((WORD)*(volatile BYTE*)((&dir[20])+1)<<8)|(WORD)*(volatile BYTE*)(&dir[20]))) << 16) |
		(WORD)(((WORD)*(volatile BYTE*)((&dir[26])+1)<<8)|(WORD)*(volatile BYTE*)(&dir[26]));
	if (dir[11] & 0x10	) {			/* It is a sub-directory */
		dj.clust = dclust;					/* Check if the sub-dir is empty or not */
		dj.sect = clust2sect(dclust);
		dj.index = 2;
		do {
			if (!move_window(dj.sect)) return FR_RW_ERROR;
			sdir = &dj.fs->win[(dj.index & 15) * 32];
			if (sdir[0] == 0) break;
			if (sdir[0] != 0xE5 && !(sdir[11] & 0x08	))
				return FR_DENIED;	/* The directory is not empty */
		} while (next_dir_entry(&dj));
	}

	if (!move_window(dsect)) return FR_RW_ERROR;	/* Mark the directory entry 'deleted' */
	dir[0] = 0xE5;
	dj.fs->winflag = 1;
	if (!remove_chain(dclust)) return FR_RW_ERROR;	/* Remove the cluster chain */

	return sync();
}




/*-----------------------------------------------------------------------*/
/* Create a Directory                                                    */
/*-----------------------------------------------------------------------*/

FRESULT f_mkdir (
	const char *path		/* Pointer to the directory path */
)
{
	FRESULT res;
	DIR dj;
	BYTE *dir, *fw, n;
	char fn[8+3+1];
	DWORD sect, dsect, tim;
	CLUST dclust, pclust;


	res = auto_mount(&path, 1);
	if (res != FR_OK) return res;
	res = trace_path(&dj, fn, path, &dir);	/* Trace the file path */
	if (res == FR_OK) return FR_EXIST;		/* Any file or directory is already existing */
	if (res != FR_NO_FILE) return res;

	res = reserve_direntry(&dj, &dir); 		/* Reserve a directory entry */
	if (res != FR_OK) return res;
	sect = dj.fs->winsect;
	dclust = create_chain(0);				/* Allocate a cluster for new directory table */
	if (dclust == 1) return FR_RW_ERROR;
	dsect = clust2sect(dclust);
	if (!dsect) return FR_DENIED;
	if (!move_window(dsect)) return FR_RW_ERROR;

	fw = dj.fs->win;
	memset(fw, 0, 512U);					/* Clear the directory table */
	for (n = 1; n < dj.fs->csize; n++) {
		if (disk_write(0, fw, ++dsect, 1) != RES_OK)
			return FR_RW_ERROR;
	}

	memset(&fw[0], ' ', 8+3);		/* Create "." entry */
	fw[0] = '.';
	fw[11] = 0x10	;
	tim = get_fattime();
	*(volatile BYTE*)(&fw[22])=(BYTE)(tim); *(volatile BYTE*)((&fw[22])+1)=(BYTE)((WORD)(tim)>>8); *(volatile BYTE*)((&fw[22])+2)=(BYTE)((DWORD)(tim)>>16); *(volatile BYTE*)((&fw[22])+3)=(BYTE)((DWORD)(tim)>>24);
	memcpy(&fw[32], &fw[0], 32); fw[33] = '.';	/* Create ".." entry */
	pclust = dj.sclust;
	*(volatile BYTE*)(&fw[   20])=(BYTE)(dclust >> 16); *(volatile BYTE*)((&fw[   20])+1)=(BYTE)((WORD)(dclust >> 16)>>8);
	if (dj.fs->fs_type == 3 && pclust == dj.fs->dirbase) pclust = 0;
	*(volatile BYTE*)(&fw[32+20])=(BYTE)(pclust >> 16); *(volatile BYTE*)((&fw[32+20])+1)=(BYTE)((WORD)(pclust >> 16)>>8);
	*(volatile BYTE*)(&fw[   26])=(BYTE)(dclust); *(volatile BYTE*)((&fw[   26])+1)=(BYTE)((WORD)(dclust)>>8);
	*(volatile BYTE*)(&fw[32+26])=(BYTE)(pclust); *(volatile BYTE*)((&fw[32+26])+1)=(BYTE)((WORD)(pclust)>>8);
	dj.fs->winflag = 1;

	if (!move_window(sect)) return FR_RW_ERROR;
	memset(&dir[0], 0, 32);						/* Clean-up the new entry */
	memcpy(&dir[0], fn, 8+3);			/* Name */
	dir[12] = fn[11];
	dir[11] = 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -