STM32音乐播放器实验源码FATFS部分函数理解

f_read

The f_read function reads data from a file.

FRESULT f_read (
  FIL* fp,     /* [IN] File object */
  void* buff,  /* [OUT] Buffer to store read data */
  UINT btr,    /* [IN] Number of bytes to read */
  UINT* br     /* [OUT] Number of bytes read */
);

Parameters
fp
Pointer to the open file object.
buff
Pointer to the buffer to store the read data.
btr
Number of bytes to read in range of UINT type.
br
Pointer to the UINT variable that receives number of bytes read. This value is always valid after the function call regardless of the function return code. If the return value is equal to btr, the function return code should be FR_OK.
Return Values
FR_OK, FR_DISK_ERR, FR_INT_ERR, FR_DENIED, FR_INVALID_OBJECT, FR_TIMEOUT

Description
The function starts to read data from the file at the file offset pointed by read/write pointer. The read/write pointer advances as number of bytes read. After the function succeeded, *br should be checked to detect end of the file. In case of *br < btr, it means the read/write pointer reached end of the file during read operation.

QuickInfo
Always available.

Example
Refer to the example in f_open.

fp:指向打开文件对象的指针。
buff:指向缓冲区的指针,用于存储读取的数据。
btr:在UINT类型范围内要读取的字节数。
br :指向接收读取的字节数的UINT变量的指针。 无论函数返回代码如何,此值在函数调用后始终有效。 如果返回值等于btr,则函数返回码应为FR_OK。

该函数以读/写指针指向的文件偏移量开始从文件中读取数据。 读/写指针随着读取的字节数而增加。 函数成功后,应检查* br以检测文件结尾。 如果* br <btr,则表示在读操作期间读/写指针到达文件的末尾。

f_open

The f_open function opens a file.

FRESULT f_open (
  FIL* fp,           /* [OUT] Pointer to the file object structure */
  const TCHAR* path, /* [IN] File name */
  BYTE mode          /* [IN] Mode flags */
);

Parameters
fp
Pointer to the blank file object structure.
path
Pointer to the null-terminated string that specifies the file name to open or create.
mode
Mode flags that specifies the type of access and open method for the file. It is specified by a combination of following flags.
在这里插入图片描述
Mode flags in POSIX fopen() function corresponds to FatFs mode flags as follows:
在这里插入图片描述
fp:
指向空白文件对象结构的指针。
path:
指向以空值结尾的字符串的指针,该字符串指定要打开或创建的文件名。
mode:
模式标志,用于指定文件的访问和打开方法的类型。 由以下标志的组合指定。

f_open函数打开一个文件并创建一个文件对象。 文件对象用于随后对文件的读/写操作以标识文件。 访问文件的会话之后,应使用f_close函数关闭打开的文件。 如果对文件进行了任何更改并且在关机前未关闭文件,则介质移除或重新安装或文件可能会折叠。

如果需要打开重复的文件,请仔细阅读此处。 但是,始终禁止使用任何写模式标志重复打开文件。

f_open(audiodev.file,(TCHAR*)fname,FA_READ);	//´打开fname文件路径下的audiodev.file文件,然后FA_READ模式读取

f_opendir

首先是官网的一个对于代码的讲解

The f_opendir function opens a directory.

FRESULT f_opendir (
  DIR* dp,           /* [OUT] Pointer to the directory object structure */
  const TCHAR* path  /* [IN] Directory name */
);

Parameters

  • dp
    Pointer to the blank directory object to create a new one.
  • path
    Pointer to the null-terminated string that specifies the directory name to be opened.

Return Values
FR_OK, FR_DISK_ERR, FR_INT_ERR, FR_NOT_READY, FR_NO_PATH, FR_INVALID_NAME, FR_INVALID_OBJECT, FR_INVALID_DRIVE, FR_NOT_ENABLED, FR_NO_FILESYSTEM, FR_TIMEOUT, FR_NOT_ENOUGH_CORE, FR_TOO_MANY_OPEN_FILES

Description
The f_opendir function opens an exsisting directory and creates a directory object for subsequent f_readdir function.

QuickInfo
Available when FF_FS_MINIMIZE <= 1.

对于DIR这个结构体,包含以下信息,用来做目录
在这里插入图片描述
f_opendir函数的功能是打开一个目录,并且为后续f_readdir函数创建目录对象

参数:
dp:记录打开目录的信息
path:打开目录的路径

返回值:
FR_OK = 0;也就是f_opendir正常执行,返回FR_OK

f_opendir(&wavdir,"0:/MUSIC")

打开路径0:/MUSIC,然后把目录信息传递到wavdir,成功则返回FR_OK

f_readdir目录项目

The f_readdir function reads an item of the directory.

FRESULT f_readdir (
  DIR* dp,      /* [IN] Directory object */
  FILINFO* fno  /* [OUT] File information structure */
);

Parameters
dp
Pointer to the open directory object.
fno
Pointer to the file information structure to store the information about read item. A null pointer rewinds the read index of the directory.

Return Values
FR_OK, FR_DISK_ERR, FR_INT_ERR, FR_INVALID_OBJECT, FR_TIMEOUT, FR_NOT_ENOUGH_CORE

功能:
读取指定目录中的项目

参数:
dp:输入目录信息
fno:输出文件信息

描述
f_readdir 函数读取一个目录中的项目以及其文件信息。f_readdir 函数能够顺序读取目录中的项目,子目录中的点条目(“。”和“ …”)被过滤掉,它们将永远不会出现在读取的项目中。所有目录项都已被读取并且没有要读取的项时,会将空字符串存储到fno-> fname []中,而不会出现任何错误。当将空指针赋予fno时,将倒退目录对象的读取索引。
启用对长文件名(LFN)的支持时,在文件信息结构中定义了成员altname []以存储对象的短文件名。如果由于下面列出的某些原因而无法访问长文件名,则将短文件名存储到fname [],并且altname []具有空字符串。

res=f_readdir(&wavdir,&wavfileinfo); 

f_typetell文件类型

在这里插入图片描述
f_typetell(u8 *fname)
报告文件的类型
fname:文件名
返回值:0XFF,表示无法识别的文件类型编号,其他,高四位表示所属大类,低四位表示所属小类

res=f_typetell(fn);

把文件的名称fn输入f_typetell(fn),然后返回一个文件类型0X00-0XFE。

FILINFO文件信息

/* File status structure (FILINFO) */

typedef struct {
	DWORD	fsize;			/* File size */
	WORD	fdate;			/* Last modified date */
	WORD	ftime;			/* Last modified time */
	BYTE	fattrib;		/* Attribute */
	TCHAR	fname[13];		/* Short file name (8.3 format) */
#if _USE_LFN
	TCHAR*	lfname;			/* Pointer to the LFN buffer */
	UINT 	lfsize;			/* Size of LFN buffer in TCHAR */
#endif
} FILINFO;

FILINFO 文件信息的结构体

FILINFO wavfileinfo;

f_lseek移动文件指针

The f_lseek function moves the file read/write pointer of an open file object. It can also be used to expand the file size (cluster pre-allocation).
f_lseek函数移动打开的文件对象的文件读/写指针。 它也可以用来扩展文件大小(集群预分配)。

FRESULT f_lseek (
  FIL*    fp,  /* [IN] File object */
  FSIZE_t ofs  /* [IN] File read/write pointer */
);

Parameters
fp
Pointer to the open file object.
ofs
Byte offset from top of the file to set read/write pointer. The data type FSIZE_t is an alias of either DWORD(32-bit) or QWORD(64-bit) depends on the configuration option FF_FS_EXFAT.
从文件顶部开始设置读取/写入指针的字节偏移量。 数据类型FSIZE_t是DWORD(32位)或QWORD(64位)的别名,取决于配置选项FF_FS_EXFAT。

f_lseek(audiodev.file, wavctrl.datastart);		//跳过文件头

指针指向数据部分,audiodev.file从数据开始

__audiodev音乐播放控制器

音乐播放控制器

typedef __packed struct
{  
	//两个I2S解码的BUF
	u8 *i2sbuf1;
	u8 *i2sbuf2; 
	u8 *tbuf;				//零时数组,仅在24bit解码的时候需要用到
	FIL *file;				//音频文件指针
	
	u8 status;				//bit0: 0,暂停播放;1,继续播放
							//bit1: 0,结束播放;1,开启播放
}__audiodev; 
if(key==WKUP_PRES)//暂停
{
	if(audiodev.status&0X01)
	  audiodev.status&=~(1<<0);
	else 
		audiodev.status|=0X01;  
}
if(key==KEY2_PRES||key==KEY0_PRES)//下一曲/上一曲
{
	res=key;
	break; 
}

WKUP_PRES键承担了播放和暂停两个逻辑功能,所以首先通过if来判断此时的状态是播放还是暂停,然后再做出反向的操作。

if(audiodev.status&0X01)
	  audiodev.status&=~(1<<0);

对于这段代码 audiodev.status&0X01 == 1表示正在播放中,需要进行的操作是暂停播放

*** |= ( 1<< x )的操作结果就是把这个寄存器的bitx置1, &= ~(1<<x)的操作结果就是把bitx置0.
这样的操作都是为了不影响寄存器的其它位,只去操作指定的bit。***

1<<0结果是二进制 0000 0001 取反后为 1111 1110
不管 audiodev.status 原来的状态如何,相**“&”**后,最后一位一定是 “0”

audiodev.status&=~(1<<0);相当于 audiodev.status = audiodev.status & (~(1<<0))也就是audiodev.status最低位置0,其他位保持不变。

audiodev.status的最低位置0后,播放器暂停播放

	else 
		audiodev.status|=0X01; 

audiodev.status |= 0X01 相当于 audiodev.status = audiodev.status | 0X01,也就是 audiodev.status 最低位置1其他位保持不变,播放器切换成播放状态。

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值