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

📄 ff.c.bak

📁 An complete pmp solution for mattel juicebox player. Using crossworks for arm.
💻 BAK
📖 第 1 页 / 共 3 页
字号:
		dirname[n++] = c;
	}
	return 1;
}



/*-------------------*/
/* Trace a File Path */

static
FRESULT prvTracePath (
	DIR *scan,			/* Pointer to directory object to return last directory */
	char *fn,			/* Pointer to last segment name to return */
	const char *path,	/* Full-path string to trace a file or directory */
	BYTE **dir			/* Directory pointer in Win[] to retutn */
)
{
	DWORD clust;
	char ds;
	BYTE *dptr = NULL;
	FATFS *fs = FatFs;

	/* Initialize directory object */
	clust = fs->dirbase;
	if (fs->fs_type == FS_FAT32) {
		scan->clust = scan->sclust = clust;
		scan->sect = prvClust2Sect(clust);
	} else {
		scan->clust = scan->sclust = 0;
		scan->sect = clust;
	}
	scan->index = 0;

	while ((*path == ' ') || (*path == '/')) path++;	/* Skip leading spaces */
	if ((BYTE)*path < ' ') {							/* Null path means the root directory */
		*dir = NULL; return FR_OK;
	}

	for (;;) {
		ds = prvMakeDirFile(&path, fn);			/* Get a paragraph into fn[] */
		if (ds == 1) return FR_INVALID_NAME;
		for (;;) {
			if (!prvMoveWindow(scan->sect)) return FR_RW_ERROR;
			dptr = &(fs->win[(scan->index & 15) * 32]);	/* Pointer to the directory entry */
			if (*dptr == 0)								/* Has it reached to end of dir? */
				return !ds ? FR_NO_FILE : FR_NO_PATH;
			if (    (*dptr != 0xE5)						/* Matched? */
				&& !(*(dptr+11) & AM_VOL)
				&& !memcmp(dptr, fn, 8+3) ) break;
			if (!prvNextDirEntry(scan))					/* Next directory pointer */
				return !ds ? FR_NO_FILE : FR_NO_PATH;
		}
		if (!ds) { *dir = dptr; return FR_OK; }			/* Matched with end of path */
		if (!(*(dptr+11) & AM_DIR)) return FR_NO_PATH;	/* Cannot trace because it is a file */
		clust = ((DWORD)LD_WORD(dptr+20) << 16) | LD_WORD(dptr+26); /* Get cluster# of the directory */
		scan->clust = scan->sclust = clust;				/* Restart scan with the new directory */
		scan->sect = prvClust2Sect(clust);
		scan->index = 0;
	}
}



/*---------------------------*/
/* Reserve a Directory Entry */

#ifndef _FS_READONLY
static
BYTE* prvReserveDirEntry (
	DIR *scan			/* Target directory to create new entry */
)
{
	DWORD clust, sector;
	BYTE c, n, *dptr;
	FATFS *fs = FatFs;


	/* Re-initialize directory object */
	clust = scan->sclust;
	if (clust) {	/* Dynamic directory table */
		scan->clust = clust;
		scan->sect = prvClust2Sect(clust);
	} else {		/* Static directory table */
		scan->sect = fs->dirbase;
	}
	scan->index = 0;

	do {
		if (!prvMoveWindow(scan->sect)) return NULL;
		dptr = &(fs->win[(scan->index & 15) * 32]);		/* Pointer to the directory entry */
		c = *dptr;
		if ((c == 0) || (c == 0xE5)) return dptr;		/* Found an empty entry! */
	} while (prvNextDirEntry(scan));						/* Next directory pointer */
	/* Reached to end of the directory table */

	/* Abort when static table or could not stretch dynamic table */
	if ((!clust) || !(clust = prvCreateChain(scan->clust))) return NULL;
	if (!prvMoveWindow(0)) return 0;

	fs->winsect = sector = prvClust2Sect(clust);			/* Cleanup the expanded table */
	memset(fs->win, 0, 512);
	for (n = fs->sects_clust; n; n--) {
		if (wDiskWrite(fs->win, sector, 1) != RES_OK) return NULL;
		sector++;
	}
	fs->winflag = 1;
	return fs->win;
}
#endif /* _FS_READONLY */



/*-----------------------------------------*/
/* Make Sure that the File System is Valid */

static
FRESULT prvCheckMounted ()
{
	FATFS *fs = FatFs;


	if (!fs) return FR_NOT_ENABLED;		/* Has the FatFs been enabled? */

	if (wDiskStatus() & STA_NOINIT) {	/* The drive has not been initialized */
		if (fs->files)					/* Drive was uninitialized with any file left opend */
			return FR_INCORRECT_DISK_CHANGE;
		else
			return wMountDrive();		/* Initialize file system and return resulut */
	} else {							/* The drive has been initialized */
		if (!fs->fs_type)				/* But the file system has not been initialized */
			return wMountDrive();		/* Initialize file system and return resulut */
	}
	return FR_OK;						/* File system is valid */
}





/*--------------------------------------------------------------------------*/
/* Public Funciotns                                                         */
/*--------------------------------------------------------------------------*/


/*----------------------------------------------------------*/
/* Load File System Information and Initialize FatFs Module */

FRESULT wMountDrive (void)
{
	BYTE fat;
	DWORD sect, fatend, maxsect;
	FATFS *fs = FatFs;


	if (!fs) return FR_NOT_ENABLED;

	/* Initialize file system object */
	memset(fs, 0, sizeof(FATFS));

	/* Initialize disk drive */
	if (wDiskInitialize() & STA_NOINIT)	return FR_NOT_READY;

	/* Search FAT partition */
	fat = prvCheckFS(sect = 0);		/* Check sector 0 as an SFD format */
	if (!fat) {						/* Not a FAT boot record, it will be an FDISK format */
		/* Check a partition listed in top of the partition table */
		if (fs->win[0x1C2]) {					/* Is the partition existing? */
			sect = LD_DWORD(&(fs->win[0x1C6]));	/* Partition offset in LBA */
			fat = prvCheckFS(sect);				/* Check the partition */
		}
	}
	if (!fat) return FR_NO_FILESYSTEM;	/* No FAT patition */

	/* Initialize file system object */
	fs->fs_type = fat;								/* FAT type */
	fs->sects_fat = 								/* Sectors per FAT */
		(fat == FS_FAT32) ? LD_DWORD(&(fs->win[0x24])) : LD_WORD(&(fs->win[0x16]));
	fs->sects_clust = fs->win[0x0D];				/* Sectors per cluster */
	fs->n_fats = fs->win[0x10];						/* Number of FAT copies */
	fs->fatbase = sect + LD_WORD(&(fs->win[0x0E]));	/* FAT start sector (physical) */
	fs->n_rootdir = LD_WORD(&(fs->win[0x11]));		/* Nmuber of root directory entries */

	fatend = fs->sects_fat * fs->n_fats + fs->fatbase;
	if (fat == FS_FAT32) {
		fs->dirbase = LD_DWORD(&(fs->win[0x2C]));	/* FAT32: Directory start cluster */
		fs->database = fatend;	 					/* FAT32: Data start sector (physical) */
	} else {
		fs->dirbase = fatend;						/* Directory start sector (physical) */
		fs->database = fs->n_rootdir / 16 + fatend;	/* Data start sector (physical) */
	}
	maxsect = LD_DWORD(&(fs->win[0x20]));			/* Calculate maximum cluster number */
	if (!maxsect) maxsect = LD_WORD(&(fs->win[0x13]));
	fs->max_clust = (maxsect - fs->database + sect) / fs->sects_clust + 2;

	return FR_OK;
}



/*-----------------------*/
/* Open or Create a File */

FRESULT wFileOpen (
	FIL *fp,			/* Pointer to the buffer of new file object to create */
	const char *path,	/* Pointer to the file name */
	BYTE mode			/* Access mode and file open mode flags */
)
{
	FRESULT res;
	BYTE *dir;
	DIR dirscan;
	char fn[8+3+1];
	FATFS *fs = FatFs;


	if ((res = prvCheckMounted()) != FR_OK) return res;
#ifndef _FS_READONLY
	if ((mode & (FA_WRITE|FA_CREATE_ALWAYS|FA_OPEN_ALWAYS)) && (wDiskStatus() & STA_PROTECT))
		return FR_WRITE_PROTECTED;
#endif

	res = prvTracePath(&dirscan, fn, path, &dir);	/* Trace the file path */

#ifndef _FS_READONLY
	/* Create or Open a File */
	if (mode & (FA_CREATE_ALWAYS|FA_OPEN_ALWAYS)) {
		DWORD dw;
		if (res != FR_OK) {		/* No file, create new */
			mode |= FA_CREATE_ALWAYS;
			if (res != FR_NO_FILE) return res;
			dir = prvReserveDirEntry(&dirscan);	/* Reserve a directory entry */
			if (dir == NULL) return FR_DENIED;
			memcpy(dir, fn, 8+3);		/* Initialize the new entry */
			*(dir+12) = fn[11];
			memset(dir+13, 0, 32-13);
		} else {				/* Any object is already existing */
			if ((dir == NULL) || (*(dir+11) & (AM_RDO|AM_DIR)))	/* Could not overwrite (R/O or DIR) */
				return FR_DENIED;
			if (mode & FA_CREATE_ALWAYS) {	/* Resize it to zero */
				dw = fs->winsect;			/* Remove the cluster chain */
				if (!prvRemoveChain(((DWORD)LD_WORD(dir+20) << 16) | LD_WORD(dir+26))
					|| !prvMoveWindow(dw) )
					return FR_RW_ERROR;
				ST_WORD(dir+20, 0); ST_WORD(dir+26, 0);	/* cluster = 0 */
				ST_DWORD(dir+28, 0);					/* size = 0 */
			}
		}
		if (mode & FA_CREATE_ALWAYS) {
			*(dir+11) = AM_ARC;
			dw = wGetTime();
			ST_DWORD(dir+14, dw);	/* Created time */
			ST_DWORD(dir+22, dw);	/* Updated time */
			fs->winflag = 1;
		}
	}
	/* Open a File */
	else {
#endif /* _FS_READONLY */
		if (res != FR_OK) return res;		/* Trace failed */
		if ((dir == NULL) || (*(dir+11) & AM_DIR))	/* It is a directory */
			return FR_NO_FILE;
#ifndef _FS_READONLY
		if ((mode & FA_WRITE) && (*(dir+11) & AM_RDO)) /* R/O violation */
			return FR_DENIED;
	}
#endif

#ifdef _FS_READONLY
	fp->flag = mode & FA_READ;
#else
	fp->flag = mode & (FA_WRITE|FA_READ);
	fp->dir_sect = fs->winsect;			/* Pointer to the directory entry */
	fp->dir_ptr = dir;
#endif
	fp->org_clust =	((DWORD)LD_WORD(dir+20) << 16) | LD_WORD(dir+26);	/* File start cluster */
	fp->fsize = LD_DWORD(dir+28);		/* File size */
	fp->fptr = 0;						/* File ptr */
	fp->sect_clust = 1;					/* Sector counter */
	fs->files++;
	return FR_OK;
}



/*-----------*/
/* Read File */

FRESULT wFileRead (
	FIL *fp, 		/* Pointer to the file object */
	void *buff,		/* Pointer to data buffer */
	WORD btr,		/* Number of bytes to read */
	WORD *br		/* Pointer to number of bytes read */
)
{
	DWORD clust, sect, ln;
	WORD rcnt;
	BYTE cc, *rbuff = buff;
	FATFS *fs = FatFs;


	*br = 0;
	if (!fs) return FR_NOT_ENABLED;
	if ((wDiskStatus() & STA_NOINIT) || !fs->fs_type) return FR_NOT_READY;	/* Check disk ready */
	if (fp->flag & FA__ERROR) return FR_RW_ERROR;	/* Check error flag */
	if (!(fp->flag & FA_READ)) return FR_DENIED;	/* Check access mode */
	ln = fp->fsize - fp->fptr;
	if (btr > ln) btr = (WORD)ln;					/* Truncate read count by number of bytes left */

	for ( ;  btr;									/* Repeat until all data transferred */
		rbuff += rcnt, fp->fptr += rcnt, *br += rcnt, btr -= rcnt) {
		if ((fp->fptr % 512) == 0) {				/* On the sector boundary */
			if (--(fp->sect_clust)) {				/* Decrement left sector counter */
				sect = fp->curr_sect + 1;			/* Get current sector */
			} else {								/* On the cluster boundary, get next cluster */
				clust = (fp->fptr == 0) ? fp->org_clust : prvGetCluster(fp->curr_clust);
				if ((clust < 2) || (clust >= fs->max_clust)) goto fr_error;
				fp->curr_clust = clust;				/* Current cluster */
				sect = prvClust2Sect(clust);			/* Get current sector */
				fp->sect_clust = fs->sects_clust;	/* Re-initialize the left sector counter */
			}
#ifndef _FS_READONLY
			if (fp->flag & FA__DIRTY) {				/* Flush file I/O buffer if needed */
				if (wDiskWrite(fp->buffer, fp->curr_sect, 1) != RES_OK) goto fr_error;
				fp->flag &= ~FA__DIRTY;
			}
#endif
			fp->curr_sect = sect;					/* Update current sector */
			cc = btr / 512;							/* When left bytes >= 512, */
			if (cc) {								/* Read maximum contiguous sectors directly */
				if (cc > fp->sect_clust) cc = fp->sect_clust;
				if (wDiskRead(rbuff, sect, cc) != RES_OK) goto fr_error;
				fp->sect_clust -= cc - 1;
				fp->curr_sect += cc - 1;
				rcnt = cc * 512; continue;
			}
			if (wDiskRead(fp->buffer, sect, 1) != RES_OK)	/* Load the sector into file I/O buffer */
				goto fr_error;
		}
		rcnt = 512 - ((WORD)fp->fptr % 512);				/* Copy fractional bytes from file I/O buffer */
		if (rcnt > btr) rcnt = btr;
		memcpy(rbuff, &fp->buffer[fp->fptr % 512], rcnt);
	}

	return FR_OK;

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



/*------------*/
/* Write File */

#ifndef _FS_READONLY
FRESULT wFileWrite (
	FIL *fp,			/* Pointer to the file object */
	const void *buff,	/* Pointer to the data to be written */
	WORD btw,			/* Number of bytes to write */
	WORD *bw			/* Pointer to number of bytes written */
)
{
	DWORD clust, sect;
	WORD wcnt;
	BYTE cc;
	const BYTE *wbuff = buff;
	FATFS *fs = FatFs;


	*bw = 0;
	if (!fs) return FR_NOT_ENABLED;
	if ((wDiskStatus() & STA_NOINIT) || !fs->fs_type) return FR_NOT_READY;
	if (fp->flag & FA__ERROR) return FR_RW_ERROR;	/* Check error flag */
	if (!(fp->flag & FA_WRITE)) return FR_DENIED;	/* Check access mode */
	if (fp->fsize + btw < fp->fsize) btw = 0;		/* File size cannot reach 4GB */

	for ( ;  btw;									/* Repeat until all data transferred */
		wbuff += wcnt, fp->fptr += wcnt, *bw += wcnt, btw -= wcnt) {
		if ((fp->fptr % 512) == 0) {				/* On the sector boundary */
			if (--(fp->sect_clust)) {				/* Decrement left sector counter */
				sect = fp->curr_sect + 1;			/* Get current sector */
			} else {								/* On the cluster boundary, get next cluster */
				if (fp->fptr == 0) {				/* Is top of the file */
					clust = fp->org_clust;
					if (clust == 0)					/* No cluster is created yet */
						fp->org_clust = clust = prvCreateChain(0);	/* Create a new cluster chain */
				} else {							/* Middle or end of file */
					clust = prvCreateChain(fp->curr_clust);			/* Trace or streach cluster chain */
				}
				if ((clust < 2) || (clust >= fs->max_clust)) break;
				fp->curr_clust = clust;				/* Current cluster */
				sect = prvClust2Sect(clust);			/* Get current sector */
				fp->sect_clust = fs->sects_clust;	/* Re-initialize the left sector counter */
			}
			if (fp->flag & FA__DIRTY) {				/* Flush file I/O buffer if needed */
				if (wDiskWrite(fp->buffer, fp->curr_sect, 1) != RES_OK) goto fw_error;
				fp->flag &= ~FA__DIRTY;
			}
			fp->curr_sect = sect;					/* Update current sector */
			cc = btw / 512;							/* When left bytes >= 512, */
			if (cc) {								/* Write maximum contiguous sectors directly */
				if (cc > fp->sect_clust) cc = fp->sect_clust;
				if (wDiskWrite(wbuff, sect, cc) != RES_OK) goto fw_error;
				fp->sect_clust -= cc - 1;
				fp->curr_sect += cc - 1;
				wcnt = cc * 512; continue;
			}
			if ((fp->fptr < fp->fsize) &&  			/* Fill sector buffer with file data if needed */
				(wDiskRead(fp->buffer, sect, 1) != RES_OK))
					goto fw_error;
		}
		wcnt = 512 - ((WORD)fp->fptr % 512);		/* Copy fractional bytes to file I/O buffer */
		if (wcnt > btw) wcnt = btw;
		memcpy(&fp->buffer[fp->fptr % 512], wbuff, wcnt);
		fp->flag |= FA__DIRTY;
	}

	if (fp->fptr > fp->fsize) fp->fsize = fp->fptr;	/* Update file size if needed */
	fp->flag |= FA__WRITTEN;						/* Set file changed flag */
	return FR_OK;

fw_error:	/* Abort this file due to an unrecoverable error */
	fp->flag |= FA__ERROR;
	return FR_RW_ERROR;
}
#endif /* _FS_READONLY */



/*-------------------*/
/* Seek File Pointer */

FRESULT wFileSeek (
	FIL *fp,		/* Pointer to the file object */
	DWORD ofs		/* File pointer from top of file */
)
{
	DWORD clust;
	BYTE sc;
	FATFS *fs = FatFs;


	if (!fs) return FR_NOT_ENABLED;
	if ((wDiskStatus() & STA_NOINIT) || !fs->fs_type) return FR_NOT_READY;
	if (fp->flag & FA__ERROR) return FR_RW_ERROR;
#ifndef _FS_READONLY
	if (fp->flag & FA__DIRTY) {			/* Write-back dirty buffer if needed */

⌨️ 快捷键说明

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