⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simpleffs.h

📁 一个简单的flash文件系统
💻 H
字号:
#ifndef SIMPLE_FS_H
#define SIMPLE_FS_H

/*
 *
 */

/*
 *
 */
#define NUMOPENFILES 8


#define FLASHWRERR 2
#define ILLEGALPACKET 3


//added by minye_li
#define kSector2Addr	getBeginofthePageAddr
#define atfProgram		Program2byte
#define	atfSectorErase  EraseSector
#define delay			OSTimeDly

                                    //we use TRUE as success
#define FLASH_SUCCESS					1  //(-1)

//end added

#define RETFREE         0
#define RETDELETED      1
#define RETUSED         2

#define sectortoaddress(sector)     getBeginofthePageAddr(sector+FFS_START_SECTOR)

//#define USE_FLASH_AS_	SWAP
#define  BYTE_WRITEBACK 1
#define INFOLENGTH		2048	 //SECTORSIZE=INFOLENGTH+MAXPACKETS*PACKETLENGTH
#define PACKETLENGTH	256
#define PAYLOAD 		250 		//PACKETLENGTH=PAYLOAD+6
//#define NUMPACKETS 	(SECTORSIZE-INFOLENGTH)/256
#define NUMPACKETS		248 		//from 1..248
#define SECTORLENGTH	(NUMPACKETS*PAYLOAD)   //62000=248*250
#define USEDPACKET		0xAA
#define INFOENTRY 		8//valid file name length
#define MAXERASE 		30000
#define MAXCACHE 		128


typedef struct {
	char name[INFOENTRY+1];
	int startsector;
	int startpacket;
	int busy;
	char data[PACKETLENGTH];
	int sector;
	int oldsector;
	int packet;
	int oldpacket;
	int length;
	int firsttime;
	int write;
	int packetdataptr;
	int totallength;
	int append;
	int bin_file;//1// 1this file is bin file add by bryan
	int bin_file_info_id;
	int bin_file_dataptr;
	int	bin_file_willflush_datalen;
} filedesc;


typedef struct {
	char name[INFOENTRY+1];
	int startsector;
	int startpacket;
	char data[PACKETLENGTH];
	int packet;
	int nextpacket;
	int packet_file_order;
	int length;
	int totallength;
	int firsttime;
	int iswrite;
	long next;
	long before;
}SWAP_CACHE;


typedef struct {
	int id;
	char name[INFOENTRY+1];
	int file_length;
	int filled_data_length;
} BIN_FILE_INFO_INKEYSRAM;

/*
 * filename: file name No more than 8 character.
 * attribute: r, w, not support rw
 * return: -1 failed
 *         >=0 file handler
 */
int kopen(char *filename,char *attribute);
int kopen4java(char *filename, int nameOffset, char *attribute);

int kread(int filed, unsigned char *data, int length);
int kread4java(int filed, unsigned char *data, int offset, int length);

/*
 * filed: file handler: get it from kopen
 * data: pointer to the data block
 * length: the length of the data to write
 * return total success written data length
 * Caller must check whether the written data length is the same as the length in the parameters
 */
int kwrite(int filed, unsigned char *data, int length); 
int kwrite4java(int filed, unsigned char *data, int offset, int length,unsigned char flag);


int kseek(int filed, int length, unsigned char whence);
int kseek4java(int filed, int length, unsigned char flag);

int kclose(int filed);

/*
 * format the file system.
 * all data will be lost.
 * format times will be recorded
 */
int init_file_system(void);
/*
 * I think this should be inner function, but it is used as API.
 * Because the kopen is unable to create file, this API must be called
 * befor using kxxx API.
 * filename: valid file name
 * attribute: "a"-- append
 *            "f"-- force 
 *            "a" or "f" is no difference in current implementation.
 * length: space to be reserve for the file.
 * return >0 success
 *        -1 not enought space
 */
int create_file(char *filename,char attribute,unsigned int length);
int createfile4java(char *filename, int offset, char attribute,unsigned int length);

/*
 * filename: valid file name
 * string:   C string, must have \0
 * This API is not good, it should be changed to 
 * char* filename, char* buff, int bufflength
 * It could only append the space which had already reserved for the file
 * return how many bytes is written.
 */
int append_to_file(char *filename, char *string);
int appendfile4java(char* filename, int nameoffset, char* string, int stringoffset, int datalength);

/*
 * return -1: not exists
 *         1: exists
 */
int file_exists(char *filename);
int file_exists4java(char *filename, int offset);

/*
 * Have not been test yet
 */
int read_app_file(char *filename, char *data, int start, int length);

/*
 * get currnet free space
 */
int get_avaible_space(void);

/**
 * 1: success
 * -1: failed
 */
int del_file(char *filename);
int del_file4java(char *filename, int offset);

/**
 * This will call flash file GC internally.
 * return int: which section has been cleaned up
 */
int cleanup_sector(void);

/*
 * Mark the data stamp
 */
static int write_data(int filed); 
/*
 * filename: valid file name
 * sector: the sector contains file
 * return: packet id(from 1, ..)
 * this function could not be failed now
 * packet id=MAXPACKETS+1 means failed to find
 */
static int find_packet(char *filename,int sector);

/*
 * return sector
 *        MAXSECTORS means did not find the file
 */
static int find_file(char *filename); 

/*
 * delete file from the file system.
 */
static void delete_file(char *filename,int sector); 

/*
 *  retchar: RETFREE, RETDELETED,RETUSED
 *  sector: sector id
 */
static int number_of_packets(int retchar,int sector);

static long get_erase_count(int sector_num);

static int my_consolidate(void);

static void fillcache(void);

int copy_file(char *dstfile,char *srcfile) ;
#if 0
    /*
     * Have not been test yet
     * It is no use i think
     */
    int modify(int filed, char *data);
    
    /*
     * Not in use now
     * It could be deleted in future.
     */
    int find_deleted(char *filename); 
    int FindClearedSector(void); 
#endif
/*
 * Low level FLASH operater API 
 * 
 */
 

//#define read_flash(addr)    ((int)((*(unsigned char*)(addr))&0xff))
//int read_flash(long addr);

//#define read_flash_long(addr)    ((int)((*(unsigned int*)(addr))&0xffffffff))
//int read_flash_long(long addr);


int Program1byte( unsigned char* pAddr, unsigned char unData );

int evenodd_prog(long addr, char data);

int emb_program(long addr, unsigned char data);

unsigned int getBeginofthePageAddr(const int page);

static int format_sect(unsigned char sector_num);
static void kill_sect(unsigned char sector_num);
static int insert_bin_file_info(char *binfilename);
static int get_bin_file_info_id(char *binfilename);

int check_fdisk(void);
int get_file_num(void);
int get_file_list(char *p,int buflen);
int get_file_len(char *filename);


/*
 * Diagnose function
 */
int allclosed(void);
int openfiles(void);
int directory(void); /*I did not understand now*/
void resetfile(void);
void erasedcnts(void);
int EraseSector(unsigned int page);

/*
 * Test functions
 * 
 */
void initfile(void); /*for test*/
void test (void);
int consolidate(void);

/**
 *@return 0 - flash file system is OK.
 *		1 - not OK.
 */
int check_fdisk(void);

#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -