Fatfs porting and use (establishing a file system on Spi_flash)

Source: Internet
Author: User

The importance of file systems for embedded systems is self-evident, with file system Management data and peripherals becoming much more convenient, while simplifying application development. Today, let's take a look at the Spi_flash file system to see how the Fatfs file system is ported and used.

The materials to be prepared are:

(1) Fatfs File System source code (click here to download the latest Fatfs source).

(2) Single-chip microcomputer platform one (larger memory better).

(3) Spi_flash chip one (such as: w25q32).

Fatfs is a universal embedded file system, for different platforms support very good, large to hard disk, U disk, memory card, small to Spi_flash chip even the internal flash can be used Fatfs MCU. Today we build a file system on a 4M size Spi_flash (w25q32), the master controller is the Freescale K60 microcontroller. Before you do the file system transplant, you need to spi_flash the operation of the driver, can read and write Spi_flash on it.

Step 0.1: Download the latest Fatfs source code, current version: R0.13.

Step 0.2 Unzip the newly downloaded FATFS source code to see what files are in it. As shown in the following illustration, the red box is the necessary file for porting Fatfs, and the file in the blue box is an optional migration file.

DISKIO.C Diskio.h is a driver interface related to memory read and write control, such as Spi_flash's read-write function interface, which should be mapped to this side. Required files

Ff.h and ff.h are the core files of Fatfs, the files that must be

Ffconf.h is a fatfs configuration file that is used to crop fatfs, the required file

Integer.h is the data type definition used by FATFS to be compatible with different word length CPUs, required files

FFSYSTEM.C are some examples of use in a platform with operating systems, optional files

FFUNICODE.C is the Universal code encoding file, the file is mainly large array definition, if you need to let the file name support Chinese need this document, this file will make the code space dramatically larger, optional file


Step 0.3 This FATFS transplant does not use the operating system, the file system supports Chinese path and name, so need FF.C, ff.h, Ffconf.h, DISKIO.C, Diskio.h, The six files ffunicode.c and Integer.h are added to the project. The following figure:



Step 1.0 modifies the Ffconf.h file to trim our Fatfs, using the macro switch to remove unused functionality to streamline the file system. Want to know the function of each macro. (Click here)

Note: As we support the Chinese path and the name of the transplant, so this to set this macro #define FF_CODE_PAGE936/*936 Representative Simplified Chinese * *

Step 1.1 modifies the DISKIO.C to map our memory read/write control interface as follows:

/*-----------------------------------------------------------------------*/* Low level disk I/O module skeleton for FatFs (C) ChaN, */*-----------------------------------------------------------------------*/* If a work ing Storage Control module is available, it should be */*/* attached to the FatFs via a glue function rather than M   Odifying it. */* This is a example of glue functions to attach various exsisting */* Storage Control modules to the FatFs MoD       Ule with a defined API. */*-----------------------------------------------------------------------*/#include "diskio.h"/* FatFs Lower Layer API */#include "w25qxx.h" #include "debug.h" #define DISKIO_DEBUG 1 #if defined diskio_debug&& (diskio_debu G) #define DISKIO_PRINTF uart_printf #else #define DISKIO_PRINTF (a,...) #endif/* Definitions of physical drive number F or each drive */#define SPI_FLASH 0/* Example:map Ramdisk to physical drive 0 */*-----------------------------------------------------------------------*/* Get Drive Status */*-----------------------------------------------------------------------*/dstatus Disk_status (BYTE pdrv/* Phy Sical Drive Nmuber to identify the drive */) {if (pdrv = = Spi_flash) {return RES_OK;//return OK directly} else {Diski O_PRINTF ("!!!
		Disk_status err\r\n ");
	return res_parerr;                                                    }}/*-----------------------------------------------------------------------*/* inidialize a drive */*-----------------------------------------------------------------------*/Dstatus Disk_initialize (BYTE pdrv/* Physical drive Nmuber to identify the drive * *) {if (pdrv = = Spi_flash) {w25qxx_
	Init ();//Initialize SPI Flash return RES_OK; } else {diskio_printf ("!!!
		Disk_initialize err\r\n ");
	return res_parerr; }}/*-----------------------------------------------------------------------*/* ReaD Sector (s) *//*--------------------------------------------------		---------------------*/Dresult Disk_read (Byte pdrv,/* Physical drive Nmuber to identify the drive */byte *buff, 
/* Data buffer to store read Data */DWORD sector,/* Start sector in LBA */UINT Count/* Number of sectors to read */
	) {Dresult res;
	uart_printf ("Disk_read---sector:%d,count:%d\r\n", sector,count);
	if (pdrv = = Spi_flash) {res = Fs_spiflash_read (buff,sector,count);//spi FLASH read interface, note function parameter type consistency return res; } else {diskio_printf ("!!!
		Disk_read err\r\n ");
	return res_parerr;                                                       }}/*-----------------------------------------------------------------------*/* Write Sector (s) */*-----------------------------------------------------------------------*/Dresult Disk_write (Byte pdrv,/* Physical drive Nmuber to identify the drive * * Const BYTE *buff,/* Data to be WrittEn */DWORD sector,/* Start sector in LBA */UINT Count/* Number of sectors to write */) {Dresult res; if (pdrv = = Spi_flash) {res = Fs_spiflash_write ((uint8_t *) buff,sector,count),//spi FLASH's write interface, note function parameter type consistency return res
	; } else {diskio_printf ("!!!
		Disk_write err\r\n ");
	return res_parerr;                                               }}/*-----------------------------------------------------------------------*/* Miscellaneous Functions */*-----------------------------------------------------------------------*/Dresult Disk_ioctl (Byte pdrv,/* Physical drive Nmuber (0 ...) */BYTE cmd,/* Control code */void *buff/Buffer to send/

		 Receive control Data */) {if (pdrv = = Spi_flash) {switch (cmd) {case Ctrl_sync:return RES_OK;
			 /* Number of Sectors 1024*1024*1024 =4 (MB) */Case Get_sector_count: * (DWORD *) buff = 1024;//w25q32 has 1024 sectors with a size of 4k bytes

		 return RES_OK; /* Sector size */Case get_sector_size: * (WORD *) buff = 4096;//SPI Flash sector size is 4K Bytes return RES_OK;
			 /* Block Size */Case get_block_size: * (DWORD *) buff = 1;
			 
		 return RES_OK;
		 Default:return Res_parerr; }} else {diskio_printf ("!!!
		Disk_ioctl err\r\n ");
	return res_parerr; }
}
DWORD Get_fattime (void)
{
	DWORD time;
	
	/* Return current TIMESTAMP *///Add time gain, if necessary, bring RTC data here
	
	return 0;
}
There are so many files to modify, and the other files are used directly without changes.

step2.0 Write a test function below to measure the success of our transplant.

void Fs_test (void) {FATFS fs;            /* Filesystem Object */FIL FIL;  /* File Object */Fresult res;            /* API Result code */UINT BW; /* Bytes written */BYTE WORK[FF_MAX_SS];
	/* Work area (larger are better for processing time) */BYTE mm[50];
	
	UINT i;
	uart_printf ("File system Test start: \ r \ n"); /* Format File system */Res = F_MKFS ("0:", fm_any, 0, work, sizeof work),//"0:" is a volume label, from #define SPI_FLASH 0 if (res) {UART_PRI
		NTF ("File system format failed. \ r \ n");
	return;
	} else {uart_printf ("File system format succeeded. \ r \ n");
	}/* Mount file System */Res = F_mount (&fs, "0:", 0);
	if (res) {uart_printf ("File system mount failed. \ r \ n");
	} else {uart_printf ("file system mount succeeded. \ r \ n");
	}/* Create a file as new */Res = F_open (&fil, "0:/test files. txt", fa_create_new|fa_write|fa_read);
	if (res) {uart_printf ("Open file failed. \ r \ n");
	} else {uart_printf ("open file succeeded. \ r \ n");
	}/* Write a message */Res = F_write (&fil, "hello,world!", N, &BW);
	uart_printf ("Res write:%d\r\n", res);
		if (bw = = 12) {uart_printf ("Write file successfully!\r\n");
	} else {uart_printf ("Write file failed!\r\n");
	} res = f_size (&fil);
	uart_printf ("File size:%d bytes.\r\n", res);
	memset (mm,0x0,50);
	F_lseek (&fil,0);
	res = F_read (&fil,mm,12,&i);
		if (res = = FR_OK) {uart_printf ("read file successfully!\r\n");
	uart_printf ("read data length:%d bytes.\r\n", i);
	} else {uart_printf ("read file failed!\r\n");
	} uart_printf ("read the following data: \ r \ n");
	Buff_print ((char *) mm,12);
	/* Close The file */F_close (&FIL);
	/* unmount File System */f_mount (0, "0:", 0); uart_printf ("File system test completed. \ r \ n");
step2.1 Congratulations ... Look, we made it, enjoy the convenience of the file system.

Said the more obvious, we have questions or suggestions welcome message. Over

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.