Learn from fatfs and fatfs

Source: Internet
Author: User

Learn from fatfs and fatfs


[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]


I was very interested in file systems when I went to school, but I had to learn it without proper fs code. The fs code on the market is either too big. Like linux, tens of thousands of lines and hundreds of thousands of lines are not moved; or there is no open source, and you only use its interfaces, but it is not clear how it is implemented.


Later at work, I found fatfs, an open-source library code. Fatfs is suitable for size and compact content. You only need to port the underlying interface to use it. At present, fatfs is used to manage the most SD card devices, and nandflash is rarely used.


Fatfs is short and concise, but learning on pc is really inconvenient. It would be nice to allow fatfs to freely simulate and debug in the vc environment. Today, I have found such a piece of code. You can go to http://www.raw-os.org/download.html#, where there is a copy of the raw-os 1.042 + yaffs + fatfs VC platform transplanted version of the download documentation. The runtime environment is vs2010, those who have experience in windows development are certainly not unfamiliar. Of course, you can not only learn fatfs but also the yaffs file system.


In fact, porting open-source libraries basically involves three aspects: (1) porting underlying interfaces, generally device drivers, and so on; (2) data migration, generally redefinition of data types according to the compiler; (3) for OS matching and porting, design some system functions required by the OS, such as creating threads, generating semaphores, and allocating memory. After doing this, you can use the APIS provided by the Library to do what we want to do.

Finally, I think the simple code porting method at the bottom layer of fatfs is quite interesting.

/*-----------------------------------------------------------------------*//* 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;}




What is the problem about FATFS transplantation with ARM7?

It is not easy for beginners to transplant a FAT file system. We recommend that you follow these steps:
1. Learn how to use WinHex on your computer. Use WinHex to observe and learn the data structure of the FAT file system. For more information, see the FAT file system documentation provided by Microsoft (Free Download ).
2. After completing step 1, use the VC development tool in Windows to develop a software that can partition and format the USB flash disk.
3. After completing step 1, you will learn to use the memory to simulate device read/write and all the APIs of FATFS to be transplanted. To simulate the storage device with memory.
4. pay a sum of money to join the SD Association to obtain valid SD specifications. Learn SD specifications and write the basic functions for SD card read/write based on your selected ARM7 chip.
5. Integrate the basic functions for reading and writing the SD card with the fatfs api to read and write files in the SD card.
6. download a copy of the latest CompactFlash specification from the official CompactFlash website for free and learn the ATA specification based on CompactFlash. Then, you can design a circuit that connects to the CF card based on your selected ARM7 chip. Based on this circuit, the basic functions for read/write of the CF card are implemented.
7. Combine the basic functions of read/write on the CF card with the fatfs api to read and write files in the CF card.

For beginners, step 1 is completely self-taught and takes about three months. Steps 2nd and 3rd only take 1-2 weeks.
The most difficult part is Step 1 and Step 6. If you have more than five years of C language programming experience on your own, it will take about one to two years.

Which of the following friends can use ch375 to port FATFS, f_mount (0, & fs); On stm32? This can be executed, but it always fails to open the file.

Oo
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.