历史上的今天
返回首页

历史上的今天

今天是: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"

推荐阅读

史海拾趣

广州盛炬(GZSJ)公司的发展小趣事

机顶盒,全称为数字视频变换盒(Set Top Box,简称STB),是现代家庭娱乐系统中不可或缺的关键设备。它作为连接电视机与外部信号源的桥梁,能够将接收到的数字电视信号转换成适合电视播放的格式,极大地丰富了用户的视听体验。

机顶盒的起源可追溯至20世纪90年代初,最初是为了解决有线电视收视费问题而设计的解扰设备。随着数字电视技术的发展,机顶盒的功能不断扩展,现已成为集数字信号接收、解码、显示以及多种增值服务于一体的智能终端。

从技术层面看,机顶盒支持多种信号源,包括有线电缆、卫星天线、宽带网络及地面广播等。它不仅能够接收高清、超高清电视节目,还能提供电子节目指南(EPG)、因特网网页浏览、视频点播、游戏等多元化服务。此外,一些先进的机顶盒还集成了智能语音助手、云计算和边缘计算技术,实现了更为便捷、智能的用户交互和内容分发。

在内容创新方面,机顶盒通过与各大内容提供商合作,不断引入优质资源,如电影、电视剧、综艺节目等,并根据用户偏好提供个性化推荐服务。同时,其跨平台整合能力也使得用户可以在不同设备间无缝切换,享受高质量的视频内容。

综上所述,机顶盒作为数字电视技术的核心组成部分,以其强大的功能、丰富的内容和便捷的操作体验,成为了现代家庭娱乐的重要选择。随着技术的不断进步和市场的持续扩大,机顶盒将继续发展,为用户带来更加优质、便捷、智能的观影体验。

GTE Microcircuits公司的发展小趣事
减少噪声的方法包括选用低噪声系数的放大器、优化电路布局以减少电磁干扰(EMI)、以及在关键信号路径上使用屏蔽和滤波技术。同时,合理设计电源去耦网络,避免电源噪声对电路的影响也是关键。
Elite Semiconductor Products Inc公司的发展小趣事

随着技术的不断进步和市场的不断变化,Elite意识到单一市场已经无法满足公司的发展需求。因此,公司开始积极拓展国际市场,寻求更广阔的发展空间。在海外市场拓展过程中,Elite注重了解当地市场的需求和文化特点,制定针对性的市场策略。同时,公司还积极与当地企业建立合作关系,共同开拓市场。这些努力让Elite在国际市场上取得了不俗的成绩,也为公司的持续发展注入了新的动力。

E-T-A [E-T-A Circuit Breakers]公司的发展小趣事

E-T-A公司的前身可以追溯到1948年,当时由Jakob Ellenberger和Harald A. Poensgen在德国共同创立了ELPO GmbH公司。这家初创企业专注于电气设备的研发和生产。随着技术的不断发展和市场的日益扩大,公司逐渐意识到设备用断路器在电路保护领域的重要性。因此,在1953年,公司正式推出了设备用的ETA断路器,并开始逐渐将重心转移到断路器领域,这也为日后E-T-A公司的成立奠定了基础。

Atmel (Microchip)公司的发展小趣事

Atmel(Microchip)一直注重技术创新和产品升级。公司不断投入研发资金,推动新产品的研发和现有产品的改进。通过引入新技术、优化产品设计等方式,Atmel的产品在性能、功耗、可靠性等方面都得到了显著提升。这些创新成果不仅满足了客户日益增长的需求,也为公司赢得了更多的市场份额。

DAPAudio公司的发展小趣事

随着数字化和智能化的快速发展,音频处理行业也面临着巨大的变革。DAPAudio积极应对这一变革,不断推出适应市场需求的新产品和技术。通过与科技公司、高校和研究机构的合作,DAPAudio在人工智能、物联网等新兴领域取得了重要突破,为公司未来的发展奠定了坚实的基础。

问答坊 | AI 解惑

希望热心的哥哥姐姐能帮帮我

本帖最后由 paulhyde 于 2014-9-15 09:42 编辑 :$ :$ :$ 毕业设计运气不好:剩下了一个最难的课题 完全不懂该怎么入手 求求万能的电子工程世界的论坛友友们帮帮忙?给点思路也行 谢谢了 任务:采用单片机或CPLD完成信号发生器的设计 ...…

查看全部问答>

电赛有老师带的朋友,说说你们现在在干嘛?

本帖最后由 paulhyde 于 2014-9-15 09:18 编辑 该考试的考完了,该毕业的毕业了,是时候该准备比赛了。 有老师带,可以有比较完备的备赛准备 但是没有老师带的同学,就似乎有些摸不着头脑了 那胸有成竹的朋友分享下你们现在在干嘛?该如何备赛 ...…

查看全部问答>

labview教材 (word格式)

labview教材 (word格式) …

查看全部问答>

了解一下AATI 原厂的手机产品应用框图!

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 & ...…

查看全部问答>

高速串行接口的编码技术(8B10B)

高速串行接口的编码技术(8B10B)…

查看全部问答>

LED驱动电源设计考量--teleda

LED由于环保、寿命长、光电效率高等众多优点,近年来在各行业应用得以快速发展,LED的驱动电源成了关注热点,理论上,LED的使用寿命在10万小时以上,但在实际应用过程中,由于驱动电源的设计及驱动方式选择不当,使LED极易损坏.随着LED的应用日益广泛,LED ...…

查看全部问答>

有没有在单片之间用红外线通信的? 进来说说方案~~~~~~

我自己搭了个简单的,发送管直接接IO,收接用三极管放大一次,好像能接收,不过数据不正确........郁闷中....... 高手进来说说通常都怎样弄的吧.....…

查看全部问答>

关于win CE用CAB打包问题

我用CAB打包后,出的错误Log文件的内容是 “Error: Section [SourceDisksFiles] - file D:\\Program Files\\HelloCE\\HelloCE\\bin\\Debug\\HelloCE.exe cannot have a drive or pathname” 请问是什么原因?谢谢…

查看全部问答>

如何构建一个IRP命令使U盘弹出?

如何构建一个IRP命令使U盘弹出? 我想在U盘已经插入电脑上的情况下,构建一个IRP命令将U盘弹出,但不知道怎么做? 向大家请教…

查看全部问答>

pda设备号

请问各位大侠,我用vc2005做的windows mobile 下的软件,想做个简单的加密,请问该如何获得设备的一个唯一的号呢?…

查看全部问答>