Click here to Skip to main content
15,888,020 members
Articles / Mobile Apps

TCP/IP Stack, FAT16 System on a Microcontroller

Rate me:
Please Sign up or sign in to vote.
4.53/5 (10 votes)
31 Jul 2011GPL35 min read 43.4K   2.1K   36  
TCP/IP Stack, FAT16 System on a Microcontroller
/*
 * myfat.h
 *
 *  Created on: 29/07/2011
 *      Author: lyl
 */

#ifndef MYFAT_H_
#define MYFAT_H_

#define FAT_INIT_OK							0
#define FAT_INIT_MEDIA_ACCESS_ERROR			(-1)
#define FAT_INIT_INVALID_SECTOR_SIZE		(-2)
#define FAT_INIT_INVALID_SIGNATURE			(-3)
#define FAT_INIT_ENDIAN_ERROR				(-4)
#define FAT_INIT_WRONG_FILESYS_TYPE			(-5)
#define FAT_INIT_WRONG_PARTITION_TYPE		(-6)

#define FAT_SECTOR_SIZE						512
#define SIGNATURE_VALUE						0xAA55
#define FAT_DIR_ENTRY_SIZE					32
// Max filename Length 
#define FATFS_MAX_LONG_FILENAME				260

#define FAT_DIR_ENTRIES_PER_SECTOR			(FAT_SECTOR_SIZE / FAT_DIR_ENTRY_SIZE)

#define FAT32_LAST_CLUSTER		0xFFFFFFFF
#define FAT32_INVALID_CLUSTER	0xFFFFFFFF

#define FILE_ATTR_READ_ONLY   	0x01
#define FILE_ATTR_HIDDEN 		0x02
#define FILE_ATTR_SYSTEM 		0x04
#define FILE_ATTR_SYSHID		0x06
#define FILE_ATTR_VOLUME_ID 	0x08
#define FILE_ATTR_DIRECTORY		0x10
#define FILE_ATTR_ARCHIVE  		0x20
#define FILE_ATTR_LFN_TEXT		0x0F
#define FILE_HEADER_BLANK		0x00
#define FILE_HEADER_DELETED		0xE5
#define FILE_TYPE_DIR			0x10
#define FILE_TYPE_FILE			0x20

typedef enum 
{
	FAT_TYPE_16,
	FAT_TYPE_32
} FAT_TYPE;

typedef struct{
	BYTE	boot[446];
}__attribute__((packed)) BOOT_CODE;

typedef struct{
	BYTE	jmp[3];
	BYTE	oem_name[8];
	WORD	bytes_per_sector;
	BYTE	sectors_per_cluster;
	WORD	reserved_sectors;
	BYTE	number_of_fats;
	WORD	number_of_root_entries;
	WORD	total_sectors_low;
	BYTE	media_descriptor;
	WORD	number_of_sectors_per_fat;
} __attribute__((packed)) BOOT_SECTOR_DETAIL;

typedef struct{
	union{
		BYTE data[FAT_SECTOR_SIZE];
		BOOT_SECTOR_DETAIL detail;
	} value;
} __attribute__((packed)) BOOT_SECTOR;

typedef struct{
	BYTE	boot_desc;
	BYTE	first_partition[3];
	BYTE	file_system_desc;
	BYTE	last_partition[3];
	DWORD	num_sectors_before_first;
	DWORD	num_sectors_in_partition;
}__attribute__((packed)) PARTITION_ENTRY;

typedef struct{
	BOOT_CODE boot_section;
	PARTITION_ENTRY entry[4];
	WORD	  boot_signature;//0x55aa
}__attribute__((packed)) MBR;

typedef struct {
	BYTE Name[11];
	BYTE Attr;
	BYTE NTRes;
	BYTE CrtTimeTenth;
	BYTE CrtTime[2];
	BYTE CrtDate[2];
	BYTE LstAccDate[2];
	WORD first_cluster_hi;
	BYTE WrtTime[2];
	BYTE WrtDate[2];
	WORD first_cluster_low;
	DWORD file_size;
} __attribute__((packed)) FAT_FILE_ENTRY;

//MY OWN PROGRAM RUNNING STRUCTURES
struct _SECTOR_BUFFER;

typedef struct _SECTOR_BUFFER
{
	BYTE		buffer[FAT_SECTOR_SIZE];
	DWORD		address; 
	BYTE		dirty;

	struct _SECTOR_BUFFER  *next;
} SECTOR_BUFFER;

typedef struct
{
	BYTE					sectors_per_cluster;
	DWORD					cluster_begin_lba;
	DWORD					rootdir_first_cluster;
	DWORD					rootdir_first_sector;
	DWORD					rootdir_sectors;
	DWORD					fat_begin_lba;
	WORD					fs_info_sector;
	DWORD					lba_begin;
	DWORD					fat_sectors;
	DWORD					next_free_cluster;
	WORD					root_entry_count;
	WORD					reserved_sectors;
	BYTE					num_of_fats;
	FAT_TYPE				fat_type;

	SECTOR_BUFFER			current_sector;
} FAT_SYSTEM;


typedef struct 
{
	DWORD					sector;
	DWORD					cluster;
	unsigned char			offset;
} DIR_STATUS;

typedef struct
{
	char					filename[FATFS_MAX_LONG_FILENAME];
	BOOL					is_dir;
	DWORD					cluster;
	DWORD					size;
} FAT_DIR_ENTRY;

#endif /* MYFAT_H_ */

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Software Developer
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions