历史上的今天
今天是:2024年12月17日(星期二)
2020年12月17日 | STM32F7xx —— FatFS(W25QXX)
2020-12-17 来源:eefocus
使用经典的fatfs1.4.1,下载源码,和我们用户相关的式diskio.c,实现里面的初始化读写和ioctl。ffconf.h用来配置,很容易看懂,不再细说了。后面写了一个测试例子,能读写成功就说明没问题了。Fatfs对文件的操作与Linux文件操作基本类似,会使用就行,想弄清楚实现要深入学习fatfs的文件结构。
/*-----------------------------------------------------------------------*/
/* Low level disk I/O module skeleton for FatFs (C)ChaN, 2016 */
/*-----------------------------------------------------------------------*/
/* If a working storage control module is available, it should be */
/* attached to the FatFs via a glue function rather than modifying it. */
/* This is an example of glue functions to attach various exsisting */
/* storage control modules to the FatFs module with a defined API. */
/*-----------------------------------------------------------------------*/
#include "diskio.h" /* FatFs lower layer API */
#include "global.h"
// 前24M字节给fatfs用 剩余部分,给自己用
#define FLASH_SECTOR_SIZE 512
#define FLASH_SECTOR_COUNT 1024*25*2 // W25Q256,前25M字节给FATFS占用
#define FLASH_BLOCK_SIZE 8 // 每个BLOCK有8个扇区
// 获得磁盘状态
DSTATUS disk_status(
BYTE pdrv /* Physical drive nmuber to identify the drive */
)
{
return RES_OK;
}
//初始化磁盘
DSTATUS disk_initialize(
BYTE pdrv /* Physical drive nmuber to identify the drive */
)
{
W25QXXInit();
return 0;
}
// 读扇区
// pdrv:磁盘编号0~9
DRESULT disk_read(
BYTE pdrv, /* Physical drive nmuber to identify the drive */
BYTE *buff, /* Data buffer to store read data */
DWORD sector, /* Sector address in LBA */
UINT count /* Number of sectors to read */
)
{
if(!count)
{
return RES_PARERR;
}
for(; count > 0; count--)
{
W25QXXRead(sector * FLASH_SECTOR_SIZE, buff, FLASH_SECTOR_SIZE);
sector++;
buff += FLASH_SECTOR_SIZE;
}
return RES_OK;
}
// 写扇区
// pdrv:磁盘编号0~9
// *buff:发送数据首地址
// sector:扇区地址
// count:需要写入的扇区数
DRESULT disk_write(
BYTE pdrv, /* Physical drive nmuber to identify the drive */
const BYTE *buff, /* Data to be written */
DWORD sector, /* Sector address in LBA */
UINT count /* Number of sectors to write */
)
{
if(!count)
{
return RES_PARERR; // count不能等于0,否则返回参数错误
}
for(; count > 0; count--)
{
W25QXXWrite(sector * FLASH_SECTOR_SIZE, (uint8_t *)buff, FLASH_SECTOR_SIZE);
sector++;
buff += FLASH_SECTOR_SIZE;
}
return RES_OK;
}
// 其他表参数的获得
// pdrv:磁盘编号0~9
// ctrl:控制代码
// *buff:发送/接收缓冲区指针
DRESULT disk_ioctl(
BYTE pdrv, /* Physical drive nmuber (0..) */
BYTE cmd, /* Control code */
void *buff /* Buffer to send/receive control data */
)
{
DRESULT res;
switch(cmd)
{
case CTRL_SYNC:
res = RES_OK;
break;
case GET_SECTOR_SIZE:
*(WORD*)buff = FLASH_SECTOR_SIZE;
res = RES_OK;
break;
case GET_BLOCK_SIZE:
*(WORD*)buff = FLASH_BLOCK_SIZE;
res = RES_OK;
break;
case GET_SECTOR_COUNT:
*(DWORD*)buff = FLASH_SECTOR_COUNT;
res = RES_OK;
break;
default:
res = RES_PARERR;
break;
}
return res;
}
// 获得时间
// User defined function to give a current time to fatfs module */
// 31-25: Year(0-127 org.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) */
DWORD get_fattime(void)
{
return 0;
}
// 动态分配内存
void *ff_memalloc(UINT size)
{
return (void *)MemAlloc(SRAM_TYPE_IN, size);
}
// 释放内存
void ff_memfree(void *mf)
{
MemFree(SRAM_TYPE_IN, mf);
}
/*---------------------------------------------------------------------------/
/ FatFs - FAT file system module configuration file R0.12 (C)ChaN, 2016
/---------------------------------------------------------------------------*/
#define _FFCONF 88100 /* Revision ID */
/*---------------------------------------------------------------------------/
/ Function Configurations
/---------------------------------------------------------------------------*/
#define _FS_READONLY 0
/* This option switches read-only configuration. (0:Read/Write or 1:Read-only)
/ Read-only configuration removes writing API functions, f_write(), f_sync(),
/ f_unlink(), f_mkdir(), f_chmod(), f_rename(), f_truncate(), f_getfree()
/ and optional writing functions as well. */
#define _FS_MINIMIZE 0
/* This option defines minimization level to remove some basic API functions.
/
/ 0: All basic functions are enabled.
/ 1: f_stat(), f_getfree(), f_unlink(), f_mkdir(), f_truncate() and f_rename()
/ are removed.
/ 2: f_opendir(), f_readdir() and f_closedir() are removed in addition to 1.
/ 3: f_lseek() function is removed in addition to 2. */
#define _USE_STRFUNC 1
/* This option switches string functions, f_gets(), f_putc(), f_puts() and
/ f_printf().
/
/ 0: Disable string functions.
/ 1: Enable without LF-CRLF conversion.
/ 2: Enable with LF-CRLF conversion. */
#define _USE_FIND 0
/* This option switches filtered directory read functions, f_findfirst() and
/ f_findnext(). (0:Disable, 1:Enable 2:Enable with matching altname[] too) */
#define _USE_MKFS 1
/* This option switches f_mkfs() function. (0:Disable or 1:Enable) */
#define _USE_FASTSEEK 1
/* This option switches fast seek function. (0:Disable or 1:Enable) */
#define _USE_EXPAND 0
/* This option switches f_expand function. (0:Disable or 1:Enable) */
#define _USE_CHMOD 0
/* This option switches attribute manipulation functions, f_chmod() and f_utime().
/ (0:Disable or 1:Enable) Also _FS_READONLY needs to be 0 to enable this option. */
#define _USE_LABEL 1
/* This option switches volume label functions, f_getlabel() and f_setlabel().
/ (0:Disable or 1:Enable) */
#define _USE_FORWARD 0
/* This option switches f_forward() function. (0:Disable or 1:Enable)
/ To enable it, also _FS_TINY need to be 1. */
/*---------------------------------------------------------------------------/
/ Locale and Namespace Configurations
/---------------------------------------------------------------------------*/
#define _CODE_PAGE 936 //采用中文GBK编码
/* This option specifies the OEM code page to be used on the target system.
/ Incorrect setting of the code page can cause a file open failure.
/
/ 1 - ASCII (No extended character. Non-LFN cfg. only)
/ 437 - U.S.
/ 720 - Arabic
/ 737 - Greek
/ 771 - KBL
/ 775 - Baltic
/ 850 - Latin 1
/ 852 - Latin 2
/ 855 - Cyrillic
/ 857 - Turkish
/ 860 - Portuguese
/ 861 - Icelandic
/ 862 - Hebrew
/ 863 - Canadian French
/ 864 - Arabic
/ 865 - Nordic
/ 866 - Russian
/ 869 - Greek 2
/ 932 - Japanese (DBCS)
/ 936 - Simplified Chinese (DBCS)
/ 949 - Korean (DBCS)
/ 950 - Traditional Chinese (DBCS)
*/
#define _USE_LFN 3
#define _MAX_LFN 255
/* The _USE_LFN switches the support of long file name (LFN).
/
/ 0: Disable support of LFN. _MAX_LFN has no effect.
/ 1: Enable LFN with static working buffer on the BSS. Always NOT thread-safe.
/ 2: Enable LFN with dynamic working buffer on the STACK.
/ 3: Enable LFN with dynamic working buffer on the HEAP.
/
/ To enable the LFN, Unicode handling functions (option/unicode.c) must be added
/ to the project. The working buffer occupies (_MAX_LFN + 1) * 2 bytes and
/ additional 608 bytes at exFAT enabled. _MAX_LFN can be in range from 12 to 255.
/ It should be set 255 to support full featured LFN operations.
/ When use stack for the working buffer, take care on stack overflow. When use heap
/ memory for the working buffer, memory management functions, ff_memalloc() and
/ ff_memfree(), must be added to the project. */
#define _LFN_UNICODE 0
/* This option switches character encoding on the API. (0:ANSI/OEM or 1:Unicode)
/ To use Unicode string for the path name, enable LFN and set _LFN_UNICODE = 1.
/ This option also affects behavior of string I/O functions. */
#define _STRF_ENCODE 0
/* When _LFN_UNICODE == 1, this option selects the character encoding on the file to
/ be read/written via string I/O functions, f_gets(), f_putc(), f_puts and f_printf().
/
/ 0: ANSI/OEM
/ 1: UTF-16LE
/ 2: UTF-16BE
/ 3: UTF-8
/
/ This option has no effect when _LFN_UNICODE == 0. */
#define _FS_RPATH 0
/* This option configures support of relative path.
/
/ 0: Disable relative path and remove related functions.
/ 1: Enable relative path. f_chdir() and f_chdrive() are available.
/ 2: f_getcwd() function is available in addition to 1.
*/
/*---------------------------------------------------------------------------/
/ Drive/Volume Configurations
/---------------------------------------------------------------------------*/
#define _VOLUMES 3 //支持3个磁盘
/* Number of volumes (logical drives) to be used. */
#define _STR_VOLUME_ID 0
#define _VOLUME_STRS "RAM","NAND","CF","SD1","SD2","USB1","USB2","USB3"
史海拾趣
|
本帖最后由 paulhyde 于 2014-9-15 09:42 编辑 :$ :$ :$ 毕业设计运气不好:剩下了一个最难的课题 完全不懂该怎么入手 求求万能的电子工程世界的论坛友友们帮帮忙?给点思路也行 谢谢了 任务:采用单片机或CPLD完成信号发生器的设计 ...… 查看全部问答> |
|
本帖最后由 paulhyde 于 2014-9-15 09:18 编辑 该考试的考完了,该毕业的毕业了,是时候该准备比赛了。 有老师带,可以有比较完备的备赛准备 但是没有老师带的同学,就似乎有些摸不着头脑了 那胸有成竹的朋友分享下你们现在在干嘛?该如何备赛 ...… 查看全部问答> |
|
AnalogicTech offers a complete portfolio of products for smartphones and other handheld equipment to fully optimize available energy, minimize losses, and prolong battery life. For Li-ion polymer batteries, linear & ...… 查看全部问答> |
|
LED由于环保、寿命长、光电效率高等众多优点,近年来在各行业应用得以快速发展,LED的驱动电源成了关注热点,理论上,LED的使用寿命在10万小时以上,但在实际应用过程中,由于驱动电源的设计及驱动方式选择不当,使LED极易损坏.随着LED的应用日益广泛,LED ...… 查看全部问答> |
|
我自己搭了个简单的,发送管直接接IO,收接用三极管放大一次,好像能接收,不过数据不正确........郁闷中....... 高手进来说说通常都怎样弄的吧.....… 查看全部问答> |
|
我用CAB打包后,出的错误Log文件的内容是 “Error: Section [SourceDisksFiles] - file D:\\Program Files\\HelloCE\\HelloCE\\bin\\Debug\\HelloCE.exe cannot have a drive or pathname” 请问是什么原因?谢谢… 查看全部问答> |




