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

📄 tff.i

📁 This is the latest VS1053B chip interface routines combined with Elm Chan s FatFs library. Whole cod
💻 I
📖 第 1 页 / 共 5 页
字号:
)
{
	CLUST clust;
	WORD idx;


	idx = dj->index + 1;
	if ((idx & 15) == 0) {		/* Table sector changed? */
		dj->sect++;				/* Next sector */
		if (dj->clust == 0) {	/* In static table */
			if (idx >= dj->fs->n_rootdir) return 0;	/* Reached to end of table */
		} else {				/* In dynamic table */
			if (((idx / 16) & (dj->fs->csize - 1)) == 0) {	/* Cluster changed? */
				clust = get_cluster(dj->clust);		/* Get next cluster */
				if (clust < 2 || clust >= dj->fs->max_clust)	/* Reached to end of table */
					return 0;
				dj->clust = clust;				/* Initialize for new cluster */
				dj->sect = clust2sect(clust);
			}
		}
	}
	dj->index = idx;	/* Lower 4 bit of dj->index indicates offset in dj->sect */
	return 1;
}




/*-----------------------------------------------------------------------*/
/* Get file status from directory entry                                  */
/*-----------------------------------------------------------------------*/

static
void get_fileinfo (	/* No return code */
	FILINFO *finfo, /* Ptr to store the File Information */
	const BYTE *dir	/* Ptr to the directory entry */
)
{
	BYTE n, c, a;
	char *p;


	p = &finfo->fname[0];
	a = 1 ? dir[12] : 0;	/* NT flag */
	for (n = 0; n < 8; n++) {	/* Convert file name (body) */
		c = dir[n];
		if (c == ' ') break;
		if (c == 0x05) c = 0xE5;
		if (a & 0x08 && c >= 'A' && c <= 'Z') c += 0x20;
		*p++ = c;
	}
	if (dir[8] != ' ') {		/* Convert file name (extension) */
		*p++ = '.';
		for (n = 8; n < 11; n++) {
			c = dir[n];
			if (c == ' ') break;
			if (a & 0x10 && c >= 'A' && c <= 'Z') c += 0x20;
			*p++ = c;
		}
	}
	*p = '\0';

	finfo->fattrib = dir[11];			/* Attribute */
	finfo->fsize = (DWORD)(((DWORD)*(volatile BYTE*)((&dir[28])+3)<<24)|((DWORD)*(volatile BYTE*)((&dir[28])+2)<<16)|((WORD)*(volatile BYTE*)((&dir[28])+1)<<8)|*(volatile BYTE*)(&dir[28]));	/* Size */
	finfo->fdate = (WORD)(((WORD)*(volatile BYTE*)((&dir[24])+1)<<8)|(WORD)*(volatile BYTE*)(&dir[24]));	/* Date */
	finfo->ftime = (WORD)(((WORD)*(volatile BYTE*)((&dir[22])+1)<<8)|(WORD)*(volatile BYTE*)(&dir[22]));	/* Time */
}




/*-----------------------------------------------------------------------*/
/* Pick a paragraph and create the name in format of directory entry     */
/*-----------------------------------------------------------------------*/

static
char make_dirfile (		/* 1: error - detected an invalid format, '\0'or'/': next character */
	/*const*/ char **path,	/* Pointer to the file path pointer */
	char *dirname		/* Pointer to directory name buffer {Name(8), Ext(3), NT flag(1)} */
)
{
	BYTE n, t, c, a, b;


	memset(dirname, ' ', 8+3);	/* Fill buffer with spaces */
	a = 0; b = 0x18;	/* NT flag */
	n = 0; t = 8;
	for (;;) {
		c = *(*path)++;
		if (c == '\0' || c == '/') {		/* Reached to end of str or directory separator */
			if (n == 0) break;
			dirname[11] = 1 ? (a & b) : 0;
			return c;
		}
		if (c <= ' ' || c == 0x7F) break;		/* Reject invisible chars */
		if (c == '.') {
			if (!(a & 1) && n >= 1 && n <= 8) {	/* Enter extension part */
				n = 8; t = 11; continue;
			}
			break;
		}
		if (1 &&
			((c >= 0x81 && c <= 0x9F) ||		/* Accept S-JIS code */
		    (c >= 0xE0 && c <= 0xFC))) {
			if (n == 0 && c == 0xE5)		/* Change heading \xE5 to \x05 */
				c = 0x05;
			a ^= 1; goto md_l2;
		}
		if (c == '"') break;				/* Reject " */
		if (c <= ')') goto md_l1;			/* Accept ! # $ % & ' ( ) */
		if (c <= ',') break;				/* Reject * + , */
		if (c <= '9') goto md_l1;			/* Accept - 0-9 */
		if (c <= '?') break;				/* Reject : ; < = > ? */
		if (!(a & 1)) {	/* These checks are not applied to S-JIS 2nd byte */
			if (c == '|') break;			/* Reject | */
			if (c >= '[' && c <= ']') break;/* Reject [ \ ] */
			if (1 && c >= 'A' && c <= 'Z')
				(t == 8) ? (b &= 0xF7) : (b &= 0xEF);
			if (c >= 'a' && c <= 'z') {		/* Convert to upper case */
				c -= 0x20;
				if (1) (t == 8) ? (a |= 0x08) : (a |= 0x10);
			}
		}
	md_l1:
		a &= 0xFE;
	md_l2:
		if (n >= t) break;
		dirname[n++] = c;
	}
	return 1;
}



/*-----------------------------------------------------------------------*/
/* Trace a file path                                                     */
/*-----------------------------------------------------------------------*/

static
FRESULT trace_path (	/* FR_OK(0): successful, !=0: error code */
	DIR *dj,			/* 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			/* Pointer to pointer to found entry to retutn */
)
{
	CLUST clust;
	char ds;
	BYTE *dptr = 0;
	FATFS *fs = FatFs;

	/* Initialize directory object */
	dj->fs = fs;
	clust = fs->dirbase;
	if (fs->fs_type == 3) {
		dj->clust = dj->sclust = clust;
		dj->sect = clust2sect(clust);
	} else
	{
		dj->clust = dj->sclust = 0;
		dj->sect = clust;
	}
	dj->index = 0;

	if (*path == '\0') {							/* Null path means the root directory */
		*dir = 0; return FR_OK;
	}

	for (;;) {
		ds = make_dirfile(&path, fn);					/* Get a paragraph into fn[] */
		if (ds == 1) return FR_INVALID_NAME;
		for (;;) {
			if (!move_window(dj->sect)) return FR_RW_ERROR;
			dptr = &fs->win[(dj->index & 15) * 32];		/* Pointer to the directory entry */
			if (dptr[0] == 0)					/* Has it reached to end of dir? */
				return !ds ? FR_NO_FILE : FR_NO_PATH;
			if (dptr[0] != 0xE5					/* Matched? */
				&& !(dptr[11] & 0x08	)
				&& !memcmp(&dptr[0], fn, 8+3) ) break;
			if (!next_dir_entry(dj))					/* 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] & 0x10	)) return FR_NO_PATH;	/* Cannot trace because it is a file */
		clust =											/* Get cluster# of the directory */
			((DWORD)((WORD)(((WORD)*(volatile BYTE*)((&dptr[20])+1)<<8)|(WORD)*(volatile BYTE*)(&dptr[20]))) << 16) |
			(WORD)(((WORD)*(volatile BYTE*)((&dptr[26])+1)<<8)|(WORD)*(volatile BYTE*)(&dptr[26]));
		dj->clust = dj->sclust = clust;				/* Restart scannig with the new directory */
		dj->sect = clust2sect(clust);
		dj->index = 2;
	}
}



/*-----------------------------------------------------------------------*/
/* Reserve a directory entry                                             */
/*-----------------------------------------------------------------------*/

static
FRESULT reserve_direntry (	/* FR_OK: successful, FR_DENIED: no free entry, FR_RW_ERROR: a disk error occured */
	DIR *dj,				/* Target directory to create new entry */
	BYTE **dir				/* Pointer to pointer to created entry to retutn */
)
{
	CLUST clust;
	DWORD sector;
	BYTE c, n, *dptr;
	FATFS *fs = dj->fs;


	/* Re-initialize directory object */
	clust = dj->sclust;
	if (clust != 0) {	/* Dyanmic directory table */
		dj->clust = clust;
		dj->sect = clust2sect(clust);
	} else {			/* Static directory table */
		dj->sect = fs->dirbase;
	}
	dj->index = 0;

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

	/* Abort when static table or could not stretch dynamic table */
	if (clust == 0 || !(clust = create_chain(dj->clust))) return FR_DENIED;
	if (clust == 1 || !move_window(0)) return FR_RW_ERROR;

	fs->winsect = sector = clust2sect(clust);	/* Cleanup the expanded table */
	memset(fs->win, 0, 512U);
	for (n = fs->csize; n; n--) {
		if (disk_write(0, fs->win, sector, 1) != RES_OK)
			return FR_RW_ERROR;
		sector++;
	}
	fs->winflag = 1;
	*dir = fs->win;
	return FR_OK;
}




/*-----------------------------------------------------------------------*/
/* Load boot record and check if it is an FAT boot record                */
/*-----------------------------------------------------------------------*/

static
BYTE check_fs (	/* 0:The FAT boot record, 1:Valid boot record but not an FAT, 2:Not a boot record or error */
	DWORD sect	/* Sector# to check if it is an FAT boot record or not */
)
{
	FATFS *fs = FatFs;
        unsigned char c_tmp[8];
        
	if (disk_read(0, fs->win, sect, 1) != RES_OK)	/* Load boot record */
		return 2;
	if ((WORD)(((WORD)*(volatile BYTE*)((&fs->win[510])+1)<<8)|(WORD)*(volatile BYTE*)(&fs->win[510])) != 0xAA55)		/* Check record signature */
		return 2;
        strcpyf(c_tmp, "FAT32");
	if (!memcmp(&fs->win[54], c_tmp, 3))	/* Check FAT signature */
		return 0;
	strcpyf(c_tmp, "FAT32");
        if (!memcmp(&fs->win[82], c_tmp, 5) && !(fs->win[40] & 0x80))
		return 0;
	return 1;
}




/*-----------------------------------------------------------------------*/
/* Make sure that the file system is valid                               */
/*-----------------------------------------------------------------------*/

static
FRESULT auto_mount (	/* FR_OK(0): successful, !=0: any error occured */
	/*const*/ char **path,	/* Pointer to pointer to the path name (drive number) */
	BYTE chk_wp			/* !=0: Check media write protection for write access */
)
{
	BYTE fmt;
	DSTATUS stat;
	DWORD bootsect, fatsize, totalsect, maxclust;
	/*const*/ char *p = *path;
	FATFS *fs;


	while (*p == ' ') p++;	/* Strip leading spaces */
	if (*p == '/') p++;		/* Strip heading slash */
	*path = p;				/* Return pointer to the path name */

	/* Is the file system object registered? */
	fs = FatFs;
	if (!fs) return FR_NOT_ENABLED;

	if (fs->fs_type) {						/* If the logical drive has been mounted */
		stat = disk_status(0);
		if (!(stat & 0x01	)) {			/* and physical drive is kept initialized (has not been changed), */
			if (chk_wp && (stat & 0x04	))	/* Check write protection if needed */
				return FR_WRITE_PROTECTED;
			return FR_OK;					/* The file system object is valid */
		}
	}

	/* The logical drive must be re-mounted. Following code attempts to mount the logical drive */

	memset(fs, 0, sizeof(FATFS));		/* Clean-up the file system object */
	stat = disk_initialize(0);			/* Initialize low level disk I/O layer */
	if (stat & 0x01	)				/* Check if the drive is ready */
		return FR_NOT_READY;
	if (chk_wp && (stat & 0x04	))	/* Check write protection if needed */
		return FR_WRITE_PROTECTED;

	/* Search FAT partition on the drive */
	fmt = check_fs(bootsect = 0);		/* Check sector 0 as an SFD format */
	if (fmt == 1) {						/* Not an FAT boot record, it may be patitioned */
		/* Check a partition listed in top of the partition table */
		if (fs->win[446+4]) {						/* Is the 1st partition existing? */
			bootsect = (DWORD)(((DWORD)*(volatile BYTE*)((&fs->win[446+8])+3)<<24)|((DWORD)*(volatile BYTE*)((&fs->win[446+8])+2)<<16)|((WORD)*(volatile BYTE*)((&fs->win[446+8])+1)<<8)|*(volatile BYTE*)(&fs->win[446+8]));	/* Partition offset in LBA */
			fmt = check_fs(bootsect);					/* Check the partition */
		}
	}
	if (fmt || (WORD)(((WORD)*(volatile BYTE*)((&fs->win[11])+1)<<8)|(WORD)*(volatile BYTE*)(&fs->win[11])) != 512U)	/* No valid FAT patition is found */
		return FR_NO_FILESYSTEM;

	/* Initialize the file system object */
	fatsize = (WORD)(((WORD)*(volatile BYTE*)((&fs->win[22])+1)<<8)|(WORD)*(volatile BYTE*)(&fs->win[22]));			/* Number of sectors per FAT */
	if (!fatsize) fatsize = (DWORD)(((DWORD)*(volatile BYTE*)((&fs->win[36])+3)<<24)|((DWORD)*(volatile BYTE*)((&fs->win[36])+2)<<16)|((WORD)*(volatile BYTE*)((&fs->win[36])+1)<<8)|*(volatile BYTE*)(&fs->win[36]));
	fs->sects_fat = (CLUST)fatsize;
	fs->n_fats = fs->win[16];					/* Number of FAT copies */
	fatsize *= fs->n_fats;								/* (Number of sectors in FAT area) */
	fs->fatbase = bootsect + (WORD)(((WORD)*(volatile BYTE*)((&fs->win[14])+1)<<8)|(WORD)*(volatile BYTE*)(&fs->win[14]));	/* FAT start sector (lba) */
	fs->csize = fs->win[13];				/* Number of sectors per cluster */
	fs->n_rootdir = (WORD)(((WORD)*(volatile BYTE*)((&fs->win[17])+1)<<8)|(WORD)*(volatile BYTE*)(&fs->win[17]));	/* Nmuber of root directory entries */
	totalsect = (WORD)(((WORD)*(volatile BYTE*)((&fs->win[19])+1)<<8)|(WORD)*(volatile BYTE*)(&fs->win[19]));		/* Number of sectors on the file system */
	if (!totalsect) totalsect = (DWORD)(((DWORD)*(volatile BYTE*)((&fs->win[32])+3)<<24)|((DWORD)*(volatile BYTE*)((&fs->win[32])+2)<<16)|((WORD)*(volatile BYTE*)((&fs->win[32])+1)<<8)|*(volatile BYTE*)(&fs->win[32]));
	fs->max_clust = maxclust = (totalsect				/* max_clust = Last cluster# + 1 */
		- (WORD)(((WORD)*(volatile BYTE*)((&fs->win[14])+1)<<8)|(WORD)*(volatile BYTE*)(&fs->win[14])) - fatsize - fs->n_rootdir / 16
		) / fs->csize + 2;

	fmt = 1;										/* Determine the FAT sub type */
	if (maxclust >= 0xFF7) fmt = 2;
	if (maxclust >= 0xFFF7)
				fmt = 3;
	if (fmt == 3)
		fs->dirbase = (DWORD)(((DWORD)*(volatile BYTE*)((&fs->win[44])+3)<<24)|((DWORD)*(volatile BYTE*)((&fs->win[44])+2)<<16)|((WORD)*(volatile BYTE*)((&fs->win[44])+1)<<8)|*(volatile BYTE*)(&fs->win[44]));	/* Root directory start cluster */
	else
		fs->dirbase = fs->fatbase + fatsize;			/* Root directory start sector (lba) */
	fs->database = fs->fatbase + fatsize + fs->n_rootdir / 16;	/* Data start sector (lba) */

	/* Initialize allocation information */
	fs->free_clust = (CLUST)0xFFFFFFFF;
	/* Get fsinfo if needed */

⌨️ 快捷键说明

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