<html><head><title>FAT16-32SourceV101.zip fat.c</title>
<LINK href="/inc/read_style.css" type=text/css rel=stylesheet></head>
<body>
<p><a href=http://www.pudn.com>www.pudn.com</a> > <a href="http://www.pudn.com/downloads32/sourcecode/windows/detail103069.html">FAT16-32SourceV101.zip</a> > fat.c</p><!-- saved from http://www.pudn.com -->
<script src="/inc/gg_read1.js"></script><BR>
<pre name="code" class="c">
//*****************************************************************************
//
// Title : FAT16/32 file system driver for ATMEL AVR
// Authors : Angelo Bannack, Giordano Bruno Wolaniuk
// File Name : 'fat.c'
// Date : April 26, 2004
// Version : 1.00
// Target MCU : Atmel AVR Series
// Editor Tabs : 4
//
// NOTE: This code is based on original library "FAT16/32 file system driver"
// from Pascal Stang.
//
// The authors in no way will be responsible for damages that you coul'd be using this code.
// Use this code at your own risk.
//
// This code is distributed under the GNU Public License
// which can be found at http://www.gnu.org/licenses/gpl.txt
//
//*****************************************************************************
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <string.h>
#include "ata.h"
#include "fat.h"
#include "fatconf.h"
#include "fattime.h"
// globals
unsigned char *SectorBuffer = (unsigned char *) SECTOR_BUFFER_ADDR; // Sector Buffer
unsigned char *FatCache = (unsigned char *) FAT_CACHE_ADDR; // Fat Buffer
char VolLabel[12]; // Volume Label
struct partrecord PartInfo; // Partition Information
TFILE File; // File Information
unsigned long FSInfo; // File System Information
unsigned char Fat32Enabled; // Indicates if is FAT32 or FAT16
unsigned long FirstDataSector; // First Data Sector Address
unsigned int SectorsPerCluster; // Number of Sectors per Cluster
unsigned long FirstFATSector; // First FAT Sector Address
unsigned long FirstFAT2Sector; // First FAT2 Sector Address
unsigned long FirstDirCluster; // First Directory (Data) Cluster Address
unsigned long FatInCache = 0xFFFFFFFF; // Address of the FAT Cluster in FatCache
unsigned long FatSectors; // Number of FAT Sectors
unsigned long currentDirCluster; // Actual Dir Cluster Number
unsigned long NumClusters; // ATA Dispositive Cluster Numbers
unsigned long SectorInCache = 0xFFFFFFFF; // Address of the Sector Cluster in SectorBuffer
//*****************************************************************************
// Function: fatClustToSect
// Parameters: cluster
// Returns: sector
//
// Description: Converts Sectors in Clusters
//*****************************************************************************
unsigned long fatClustToSect(unsigned long clust)
{
if (clust == 0)
clust=2;
return ((clust-2) * SectorsPerCluster) + FirstDataSector;
}
//*****************************************************************************
// Function: fatSectToClust
// Parameters: sector
// Returns: cluster
//
// Description: Converts Clusters in Sectors
//*****************************************************************************
unsigned long fatSectToClust(unsigned long sect)
{
return (((sect - FirstDataSector) / SectorsPerCluster) + 2);
}
//*****************************************************************************
// Function: fatInit
// Parameters: none
// Returns: TRUE if a FAT drive was found, FALSE otherwise
//
// Description: Get FAT info from ATA dispositive and initialize internal variables
//*****************************************************************************
unsigned char fatInit(void)
{
struct bpb710 *bpb;
// read partition table
ataReadSectors(DRIVE0, 0, SectorBuffer, &SectorInCache);
// map first partition record
// save partition information to global PartInfo
PartInfo = *((struct partrecord *) ((struct partsector *) SectorBuffer)->psPart);
// Read the Partition BootSector
// **first sector of partition in PartInfo.prStartLBA
ataReadSectors( DRIVE0, PartInfo.prStartLBA, SectorBuffer, &SectorInCache );
bpb = (struct bpb710 *) ((struct bootsector710 *) SectorBuffer)->bsBPB;
// setup global disk constants
FirstDataSector = PartInfo.prStartLBA;
if(bpb->bpbFATsecs)
{
// bpbFATsecs is non-zero and is therefore valid
FirstDataSector += bpb->bpbResSectors + bpb->bpbFATs * bpb->bpbFATsecs;
}
else
{
// bpbFATsecs is zero, real value is in bpbBigFATsecs
FirstDataSector += bpb->bpbResSectors + bpb->bpbFATs * bpb->bpbBigFATsecs;
}
SectorsPerCluster = bpb->bpbSecPerClust;
FirstFATSector = bpb->bpbResSectors + PartInfo.prStartLBA;
FatSectors = bpb->bpbBigFATsecs;
NumClusters = ataGetSizeInSectors()/(bpb->bpbSecPerClust);
// initialize Volume Label
memcpy(&VolLabel, bpb->bpbVolLabel, 11);
VolLabel[11]='\0';
switch (PartInfo.prPartType)
{
case PART_TYPE_DOSFAT16:
case PART_TYPE_FAT16:
case PART_TYPE_FAT16LBA:
// first directory cluster is 2 by default (clusters range 2->big)
FirstDirCluster = CLUST_FIRST;
// push data sector pointer to end of root directory area
//FirstDataSector += (bpb->bpbRootDirEnts)/DIRENTRIES_PER_SECTOR;
Fat32Enabled = FALSE;
break;
case PART_TYPE_FAT32LBA:
case PART_TYPE_FAT32:
// bpbRootClust field exists in FAT32 bpb710, but not in lesser bpb's
FirstDirCluster = bpb->bpbRootClust;
// push data sector pointer to end of root directory area
// need this? FirstDataSector += (bpb->bpbRootDirEnts)/DIRENTRIES_PER_SECTOR;
Fat32Enabled = TRUE;
break;
default:
//Found: No Partition!
return FALSE;
break;
}
if (Fat32Enabled)
FirstFAT2Sector=FirstFATSector+bpb->bpbBigFATsecs;
else
FirstFAT2Sector=FirstFATSector+bpb->bpbFATsecs;
FSInfo=bpb->bpbFSInfo+PartInfo.prStartLBA;
currentDirCluster= FirstDirCluster;
return TRUE;
}
//*****************************************************************************
// Function: fatNextCluster
// Parameters: cluster
// Returns: the next cluster
//
// Description: Find next cluster in the FAT chain
//*****************************************************************************
unsigned long fatNextCluster(unsigned long cluster)
{
unsigned long nextCluster;
unsigned long fatMask;
unsigned long fatOffset;
unsigned long sector;
unsigned int offset;
// get fat offset in bytes
if(Fat32Enabled)
{
// four FAT bytes (32 bits) for every cluster
fatOffset = cluster << 2;
// set the FAT bit mask
fatMask = FAT32_MASK;
}
else
{
// two FAT bytes (16 bits) for every cluster
fatOffset = cluster << 1;
// set the FAT bit mask
fatMask = FAT16_MASK;
}
// calculate the FAT sector that we're interested in
sector = FirstFATSector + (fatOffset / BYTES_PER_SECTOR);
// calculate offset of the our entry within that FAT sector
offset = fatOffset % BYTES_PER_SECTOR;
// if we don't already have this FAT chunk loaded, go get it
ataReadSectors( DRIVE0, sector, (unsigned char*)FAT_CACHE_ADDR, &FatInCache);
// read the nextCluster value
nextCluster = (*((unsigned long*) &((char*)FAT_CACHE_ADDR)[offset])) & fatMask;
// check to see if we're at the end of the chain
if (nextCluster == (CLUST_EOFE & fatMask))
nextCluster = 0;
return nextCluster;
}
//*****************************************************************************
// Function: fatClusterSize
// Parameters: none
// Returns: the size of a cluster in sectors
//
// Description: return the number of sectors in a disk cluster
//***********************************************************
没有合适的资源?快使用搜索试试~ 我知道了~
与ata结合的fat12 fat16 fat32的文件系统实现,代码完整!-FAT12,FAT16,FAT32 file sys...
共11个文件
htm:10个
zip:1个
5星 · 超过95%的资源 需积分: 9 23 下载量 10 浏览量
2009-03-18
12:38:56
上传
评论
收藏 317KB RAR 举报
温馨提示
与ata结合的fat12 fat16 fat32的文件系统实现,代码完整!-FAT12,FAT16,FAT32 file system with ATA
资源推荐
资源详情
资源评论
收起资源包目录
ata结合的fat12 fat16 fat32的文件系统实现,代码完整!-FAT12,FAT16,FAT32 file system with ATA.rar (11个子文件)
与ata结合的fat12 fat16 fat32的文件系统实现,代码完整!-FAT12,FAT16,FAT32 file system with ATA
fat.h__.htm 14KB
fattime.h__.htm 3KB
main.c__.htm 14KB
global.h__.htm 2KB
ataconf.h__.htm 3KB
ata.c__.htm 20KB
ata.h__.htm 7KB
ff006.zip 292KB
fatconf.h__.htm 2KB
fattime.c__.htm 2KB
fat.c__.htm 49KB
共 11 条
- 1
资源评论
- wuhen1142012-12-12很好,很有帮助
- pu_xx2012-03-31代码很详细,不过只有和FAT有关的代码,没有和ATA相关的代码
zui130
- 粉丝: 9
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功