隨想錄(fatfs的學習)

來源:互聯網
上載者:User

標籤:drive   編譯   dial   define   包含   產生   文檔   indent   cal   


【 聲明:著作權全部,歡迎轉載,請勿用於商業用途。  聯絡信箱:feixiaoxing @163.com】


    上學的時候就對檔案系統非常有興趣。可是苦於沒有合適的fs代碼能夠學習。市面上的fs代碼,要麼太大。像linux一樣,動不動就是幾萬行,幾十萬行。要麼就是沒有開源,你僅僅會使用它的介面,卻不太清楚裡面是怎麼實現的。


    一直到後來工作的時候,發現了fatfs這麼一個開源庫代碼。

fatfs大小合適。內容也比較緊湊。僅僅要做好底層的介面移植就能夠使用了。眼下fatfs用來管理最多的還是sd卡裝置,nandflash用的比較少。


    fatfs固然短小精悍,可是在pc上學習確實還是不方便,要是讓fatfs能夠在vc環境下自由地模擬調試就好了。

今天。還真找了這麼一份代碼,大家能夠到http://www.raw-os.org/Download.html看一看,當中有一份Raw-OS 1.042 + yaffs + fatfs VC平台移植版的下載文檔。執行環境是vs2010。有過windows開發經驗的同學肯定不陌生。

當然,這份代碼你不光能夠學習fatfs,還能夠學習yaffs檔案系統。


    事實上移植開源庫基本上包含三個方面:(1)底層介面移植,通常是裝置驅動等等;(2)資料移植,一般依據編譯器重定義資料類型。(3)os的匹配移植,依據os設計自己須要的一些系統函數,比方建立thread、產生訊號量、分配記憶體等等。等到做好了這些,就能夠使用庫提供的api,做我們自己想做的事情了。

    

    最後。給出fatfs底層簡單的代碼移植方法,我認為還是蠻有意思的。

/*-----------------------------------------------------------------------*//* Low level disk I/O module skeleton for FatFs     (C)ChaN, 2012        *//*-----------------------------------------------------------------------*//* 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 module to the FatFs module with a defined API.        *//*-----------------------------------------------------------------------*/#include "raw_api.h"#include "diskio.h"/* FatFs lower layer API */#include "ff.h"//#include "usbdisk.h"/* Example: USB drive control */#include <string.h>#include <stdio.h>static RAW_U8 *simulated_space;static int init_flag;/*-----------------------------------------------------------------------*//* Inidialize a Drive                                                    *//*-----------------------------------------------------------------------*/DSTATUS disk_initialize (BYTE drv/* Physical drive nmuber (0..) */){if (init_flag == 0) {simulated_space = malloc(5 * 1024 *1024);raw_memset(simulated_space, 0, 5 * 1024 *1024);if (simulated_space == 0) {RAW_ASSERT(0);}init_flag = 1;}return 0;}/*-----------------------------------------------------------------------*//* Get Disk Status                                                       *//*-----------------------------------------------------------------------*/DSTATUS disk_status (BYTE drv/* Physical drive nmuber (0..) */){return 0;}/*-----------------------------------------------------------------------*//* Read Sector(s)                                                        *//*-----------------------------------------------------------------------*/DRESULT disk_read (BYTE pdrv, BYTE* buff, DWORD sector, UINT count){raw_memcpy(buff, simulated_space + (512 * sector), 512 * count);return RES_OK;}/*-----------------------------------------------------------------------*//* Write Sector(s)                                                       *//*-----------------------------------------------------------------------*/DRESULT disk_write (BYTE pdrv, const BYTE* buff, DWORD sector, UINT count){raw_memcpy(simulated_space + (512 * sector), buff, 512 * count);return RES_OK;}/*-----------------------------------------------------------------------*//* Miscellaneous Functions                                               *//*-----------------------------------------------------------------------*/DRESULT disk_ioctl (BYTE drv,/* Physical drive nmuber (0..) */BYTE ctrl,/* Control code */void *buff/* Buffer to send/receive control data */){DRESULT res = RES_PARERR;switch (ctrl) {case CTRL_SYNC:res = RES_OK;break;case GET_SECTOR_COUNT:*(DWORD*)buff = (10240); /*5M space*/res = RES_OK;break;case GET_SECTOR_SIZE:*(WORD*)buff = 512;res = RES_OK;break;default:break;}return res;}DWORD get_fattime (void){ return 0;}



隨想錄(fatfs的學習)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.