<meter id="pryje"><nav id="pryje"><delect id="pryje"></delect></nav></meter>
          <label id="pryje"></label>

          新聞中心

          EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > STM32+SDIO+FATFS文件系統(tǒng) 直讀SD卡

          STM32+SDIO+FATFS文件系統(tǒng) 直讀SD卡

          作者: 時(shí)間:2016-12-03 來源:網(wǎng)絡(luò) 收藏
          網(wǎng)上關(guān)于小型嵌入式文件系統(tǒng)有好多~當(dāng)然要數(shù) FATFS 很是出名 一來小巧,二來免費(fèi)。當(dāng)然了國(guó)產(chǎn)的振南的znFAT 一樣開源好用而且極其的省資源~!非常適合51單片。更重要的是國(guó)語的支持,呵呵!這次在STM32上為SD卡移植文件系統(tǒng)還是非常簡(jiǎn)單順利的,這多虧了ST 官方提供的驅(qū)動(dòng),而我自己不用動(dòng)手編寫SD卡的命令省了很多時(shí)間而且官方做的驅(qū)動(dòng)雖然效率一般但是極其嚴(yán)謹(jǐn)我很是佩服。

          FATFS的官方網(wǎng)站是

          本文引用地址:http://www.ex-cimer.com/article/201612/325209.htm

          http://elm-chan.org/fsw/ff/00index_e.html

          znFAT的官方網(wǎng)站是

          http://www.znmcu.cn/softshow.asp?id=47

          今天上午問個(gè)妹子要電話,沒給我........唉~太失敗了。

          SD卡可以用SPI驅(qū)動(dòng) 也可以直接用 SDIO 驅(qū)動(dòng) STM32 256KB FLASH 以上的片子全部都有SDIO,我們當(dāng)然要用高速快捷的SDIO 方式了!至于 SDIO 又有 1位 4位 8 位的之分 我想不來8位SDIO 是怎么回事?SD卡上最多只能接4位嘛~網(wǎng)上有人說4位的SDIO 不好用多半是固件版本太老的緣故了。呵呵這里還是要靠庫~STM32真適合懶人用。

          網(wǎng)上關(guān)于的FATFS 的文章很多 不過都太老舊,很多東西已經(jīng)不適用了 。我建議閣下到官方去下載最新的版本 目前是最新是R0.08b,使用最新的版本好處是很多網(wǎng)上很多要改來改去的地方只要你使用了新版本那就是完全可以規(guī)避的。另外STM32 的SDIO驅(qū)動(dòng)也一定要用最新的,老版本問題很多不少人的失敗就在這。我這次用的是V3.3的庫沒有任何改動(dòng)就可以了,現(xiàn)在最新的好像在3.4以上了。好了說說移植 ffconf.h是配置的頭文件 簡(jiǎn)單的修改宏就可以了,英文注釋的很完全而且網(wǎng)上也有翻譯我不多說了自己看http://www.openrtos.cn/fatfs主要在這里進(jìn)行功能裁剪寫寫我的配置。

          #define_FS_TINY0/* 0:Normal or 1:Tiny 完整的FATFS 精簡(jiǎn)版的是Tiny */

          #define _FS_READONLY0/* 0:Read/Write or 1:Read only 能讀能寫*/

          #define _FS_MINIMIZE1/* 0 to 3 簡(jiǎn)單的裁剪f_mkdir, f_chmod..這些功能沒法用的*/
          /* The _FS_MINIMIZE option defines minimization level to remove some functions.
          /
          / 0: Full function.
          / 1: f_stat, f_getfree, f_unlink, f_mkdir, f_chmod, f_truncate and f_rename
          / are removed.
          / 2: f_opendir and f_readdir are removed in addition to 1.
          / 3: f_lseek is removed in addition to 2. */


          #define_USE_STRFUNC0/* 0:Disable or 1/2:Enable是否使用字符串文件接口 */
          /* To enable string functions, set _USE_STRFUNC to 1 or 2. */


          #define_USE_MKFS0/* 0:Disable or 1:Enable制作文件系統(tǒng)我在PC上一般已經(jīng)格式化好了*/
          /* To enable f_mkfs function, set _USE_MKFS to 1 and set _FS_READONLY to 0 */


          #define_USE_FORWARD0/* 0:Disable or 1:Enable 發(fā)文件流?*/
          /* To enable f_forward function, set _USE_FORWARD to 1 and set _FS_TINY to 1. */


          #define_USE_FASTSEEK0/* 0:Disable or 1:Enable 搜索*/
          /* To enable fast seek feature, set _USE_FASTSEEK to 1. */

          #define _CODE_PAGE1//1 - ASCII only (Valid for non LFN cfg.)

          #define_USE_LFN0/* 0 to 3 */
          #define_MAX_LFN255/* Maximum LFN length to handle (12 to 255) 這些都是長(zhǎng)文件名或是漢字文件支持很費(fèi)資源所以不開啟這些*/

          #define_FS_SHARE0/* 0:Disable or >=1:Enable 不使用相對(duì)路徑*/

          #define_FS_SHARE0/* 0:Disable or >=1:Enable 文件共享多任務(wù)的操作系統(tǒng)會(huì)用到的*/

          #define _FS_REENTRANT0/* 0:Disable or 1:Enable 這些是啥用?同步什么呢?默認(rèn)就好了*/
          #define _FS_TIMEOUT1000/* Timeout period in unit of time ticks */
          #define_SYNC_tHANDLE/* O/S dependent type of sync object. e.g. HANDLE, OS_EVENT*, ID and etc.. */

          integer.h主要定義了文件的類型 若是最新的可以不用修改。

          好了 說說最關(guān)鍵的I/O module 我自己建立一個(gè)文件diskio.c新的版本要自己建立函數(shù)文件官方連個(gè)模板都沒提供作者似乎不怎么照顧新人呢~

          都說要移植5個(gè)函數(shù) 其實(shí)兩個(gè)就足以了。

          /*-----------------------------------------------------------------------*/
          /* Low level disk I/O module skeleton for FatFs (C)ChaN, 2011 */
          /*-----------------------------------------------------------------------*/
          /* This is a stub disk I/O module that acts as front end of the existing */
          /* disk I/O modules and attach it to FatFs module with common interface. */
          /*-----------------------------------------------------------------------*/

          #include"diskio.h"
          #include"stm32f10x.h"
          #include"stm32_eval_sdio_sd.h"

          #defineBLOCK_SIZE 512/* Block Size in Bytes */

          /*-----------------------------------------------------------------------*/
          /* Inidialize a Drive */

          DSTATUS disk_initialize (
          BYTE drv/* Physical drive nmuber (0..) */
          )
          {
          SD_Error Status;
          /* Supports only singledriveFATFS支持多個(gè)設(shè)備所以有個(gè)設(shè)備號(hào)drive nmuber當(dāng)然了我就一個(gè)SD卡所以只有零號(hào)*/
          if (drv)
          {
          return STA_NOINIT;
          }
          /*-------------------------- SD Init ----------------------------- */
          Status = SD_Init();
          if (Status!=SD_OK )
          {
          return STA_NOINIT;
          }
          else
          {
          return RES_OK;
          }

          }

          /*-----------------------------------------------------------------------*/
          /* Return Disk Status */

          DSTATUS disk_status (
          BYTE drv/* Physical drive nmuber (0..) */
          )
          {
          return RES_OK;//懶的管了 有空寫寫 可以加個(gè)
          }

          /*-----------------------------------------------------------------------*/
          /* Read Sector(s) */

          DRESULT disk_read (
          BYTE drv,/* Physical drive nmuber (0..) */
          BYTE *buff,/* Data buffer to store read data */
          DWORD sector,/* Sector address (LBA) 注意這個(gè)是扇區(qū)地址也就是第幾個(gè)扇區(qū)*/
          BYTE count/* Number of sectors to read (1..255) 讀取的扇區(qū)數(shù)*/
          )
          {
          //SD_Error Status;
          if (count > 1)
          {
          SD_ReadMultiBlocks(buff, sector*BLOCK_SIZE, BLOCK_SIZE, count);//扇區(qū)地址*512就是實(shí)際地址 默認(rèn)一個(gè)扇區(qū)就是512個(gè)字節(jié)
          }
          else
          {
          SD_ReadBlock(buff, sector*BLOCK_SIZE, BLOCK_SIZE);
          }
          return RES_OK;
          }

          /*-----------------------------------------------------------------------*/
          /* Write Sector(s) */

          #if _READONLY == 0
          DRESULT disk_write (
          BYTE drv,/* Physical drive nmuber (0..) */
          const BYTE *buff,/* Data to be written */
          DWORD sector,/* Sector address (LBA) */
          BYTE count/* Number of sectors to write (1..255) */
          )
          {
          if (count > 1)
          {
          SD_WriteMultiBlocks((uint8_t *)buff, sector*BLOCK_SIZE, BLOCK_SIZE, count);

          /*這里大家看到了有個(gè)地址轉(zhuǎn)換 因?yàn)镈MA僅僅支持4字節(jié)指針的所以在函數(shù)內(nèi)部還是有個(gè)轉(zhuǎn)換的這里最好優(yōu)化一下能提高效率*/
          }
          else
          {
          SD_WriteBlock((uint8_t *)buff,sector*BLOCK_SIZE, BLOCK_SIZE);
          }
          return RES_OK;
          }
          #endif/* _READONLY */


          /*-----------------------------------------------------------------------*/
          /* Miscellaneous Functions */

          DRESULT disk_ioctl (
          BYTE drv,/* Physical drive nmuber (0..) */
          BYTE ctrl,/* Control code */
          void *buff/* Buffer to send/receive control data 若是用到擦除函數(shù)這個(gè)函數(shù)一定要補(bǔ)完的有空再寫寫吧我這簡(jiǎn)單的返回就好了*/
          )
          {
          return RES_OK;
          }

          /*-----------------------------------------------------------------------*/
          /* Get current time */
          /*-----------------------------------------------------------------------*/
          DWORD get_fattime(void)
          {
          return ((2011UL-1980) << 25)// Year = 2011
          | (3UL << 21)// Month = Mar
          | (26UL << 16)// Day = 26
          | (13U << 11)// Hour = 13
          | (19U << 5)// Min =19
          | (0U >> 1) // Sec = 0
          ;
          }

          最終無優(yōu)化編譯后 :Program Size: Code=10904RO-data=336RW-data=56ZI-data=2304

          RO是程序中的指令和常量
          RW是程序中的已初始化變量
          ZI是程序中的未初始化的變量
          我覺得這個(gè)大小對(duì)于STM32103FZE來說完全可以接受


          評(píng)論


          技術(shù)專區(qū)

          關(guān)閉
          看屁屁www成人影院,亚洲人妻成人图片,亚洲精品成人午夜在线,日韩在线 欧美成人 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();