.Net Micro Framework研究—FAT文件系统实现探索

由于目前.Net Micro Framework并不支持P/Invoke功能(也无法像WinCE一样开发流式驱动),所以在底层在驱动层面用C直接对存储器(Flash)进行文件系统开发是行不通的。幸好.Net Micro Framework提供了ExtendedWeakReference类,其中赋值Target对象可以把数据存放到存储器上(Flash)。
[Serializable]
privateclass
FlashDatas
{
//调入数据

publicstaticbyte[]Load(uint index)
{
ExtendedWeakReferenceewr
=
ExtendedWeakReference.RecoverOrCreate(
typeof(FlashDatas),//类型,任意类都可以,其名称起到一个索引作用

index,//ID号,这个数据比较有用,不同ID号代表不同数据
ExtendedWeakReference.c_SurviveBoot);//该标志和.c_SurviveBoot区别不大
returnewr.Targetasbyte [];
}

//保存数据

publicstaticvoidSave(uintindex,byte []data)
{
ExtendedWeakReferenceewr
=ExtendedWeakReference.RecoverOrCreate(typeof
(FlashDatas),index,ExtendedWeakReference.c_SurviveBoot);
ewr.Target
=
data;
}
}
上面的代码就是ExtendedWeakReference类的具体使用,从代码可以看出我们无法直接对存储器进行读写,只能通过保存类的方式对数据进行存储,至于该数据存放到何处,那是无从得知的。
我最初的想法是定义一个类,类中定义一个大数组,不过实际调试发现,该数组不能太大,超过几十K就会出现内存溢出。幸好该对象可以是字节数组,所以我产生了另一个想法,每次保存一个512字节大小的字节数组,相当于磁盘上的一个扇区,以此为存取的最小单位,实现FAT文件系统。
在我博客上曾写了一篇关于FAT文件系统的文章,如《FAT文件系统几点释疑》( http://blog.youkuaiyun.com/yefanqiu/archive/2008/03/13/2176340.aspx ),我们知道要实现FAT16系统一般至少需要4M存储空间,实现FAT32一般需要256M空间以上。所以我简单的做了一个程序,在实际硬件中测试一下存取1024*4个512字节的数组内存是否可行。
测试代码如下:
privateconstuintSectorSize=512;//扇区大小
privateconstuintSecPerClus=4;//一个簇包含的扇区数
publicstaticvoid Main()
{
Debug.Print(
"Start"
);
for(uinti=0;i<512;i++)//1024*4

{
byte[]bytData=newbyte
[SectorSize];
bytData[
0]=(byte)(i%256
);
bytData[bytData.Length
-1]=bytData[0
];
FlashDatas.Save(i,bytData);
Debug.Print(i.ToString()
+"Save"+bytData[0].ToString()+""+bytData[bytData.Length-1
].ToString());

//
byte[]bytData=FlashDatas.Load(i);
//
if(bytData==null)
//
{
//
Debug.Print(i.ToString()+"LoadError");
//
break;
//
}
//
else
//
{
//
Debug.Print(i.ToString()+"Load"+bytData[0].ToString()+""+bytData[bytData.Length-1].ToString());
//}

}
Debug.Print(
"Exit"
);
}
让我失望的是,Digi的开发板存储个数一旦超过128个就会出现读失败,新拿来的iPac-9302开发板要好一些,512个之内读写没有什么问题,超过这个数就会出现和Digi开发板一样的问题。需要说明的时,在使用读写的过程中如果不断电,读写都会成功的。一但断电重新读取,读就会失败。(当然在我测试过程中出现了各种各样不同的现象,如只能成功读取前几个)。
杜伟当初还想直接支持FAT32系统呢,目前恐怕FAT16的支持都很困难了,如果实现FAT12系统就有点不值当了。不过杜伟建议说模拟器也支持数据存储功能,所以先在模拟器中实现该功能。
没有想到,模拟器存储器最大存储仅支持1M,开始我还以为我配置参数不当呢,后来反编译了模拟器相关的核心代码,发现1M在代码中就已经写死了,相关内容如下。
反编译 Microsoft.SPOT.Emulator.dll,下面是关键代码
------------------------------------------------------------------------
//内存大小0x10000*16 = 1024*1024也就是1M空间。
publicclass FlashManager:MemoryManagerBase
{
//Fields存储空间已经写死,就是1M

privateFlashSector[]_flashSectors=new FlashSector[]{
newFlashSector(0x10000
,FlashSectorUsage.Log,FlashSectorPartition.Start),
newFlashSector(0x10000
,FlashSectorUsage.Log,FlashSectorPartition.None),
newFlashSector(0x10000
,FlashSectorUsage.Log,FlashSectorPartition.None),
newFlashSector(0x10000
,FlashSectorUsage.Log,FlashSectorPartition.None),
newFlashSector(0x10000
,FlashSectorUsage.Log,FlashSectorPartition.None),
newFlashSector(0x10000
,FlashSectorUsage.Log,FlashSectorPartition.None),
newFlashSector(0x10000
,FlashSectorUsage.Log,FlashSectorPartition.None),
newFlashSector(0x10000
,FlashSectorUsage.Log,FlashSectorPartition.End),

newFlashSector(0x10000
,FlashSectorUsage.Log,FlashSectorPartition.Start),
newFlashSector(0x10000
,FlashSectorUsage.Log,FlashSectorPartition.None),
newFlashSector(0x10000
,FlashSectorUsage.Log,FlashSectorPartition.None),
newFlashSector(0x10000
,FlashSectorUsage.Log,FlashSectorPartition.None),
newFlashSector(0x10000
,FlashSectorUsage.StorageA,FlashSectorPartition.None),
newFlashSector(0x10000
,FlashSectorUsage.StorageA,FlashSectorPartition.None),
newFlashSector(0x10000
,FlashSectorUsage.StorageB,FlashSectorPartition.None),
newFlashSector(0x10000
,FlashSectorUsage.StorageB,FlashSectorPartition.End)};

....
}

//分配内存

internaloverridevoid AllocateMemory()
{
this
.ValidateFlashSectorsInternal();
uintnum=0
;
for(inti=0;i<this._flashSectors.Length;i++
)
{
num
+=this
._flashSectors[i].Length;
}
base._size=
num;
base.AllocateMemory();//分配内存......

for(intj=0;j<base._size;j++ )
{
base._memory[j]=0xff
;
}
this
.InitializeFlashSectorsInternal();
}

//分配内存

internalvirtualvoid AllocateMemory()
{
this._memory=newbyte[this
._size];
this._handle=GCHandle.Alloc(this._memory,3
);
}
此外模拟器在运行结束时,不能保证执行重载的UninitializeComponent函数,所以无法保存内存的数据,代码如下。
///<summary>
/// Calledbytheemulatorafterallcomponentsweresetupandregistered
///</summary>

publicoverridevoid InitializeComponent()
{
base
.InitializeComponent();
_form
=newYFEmulatorForm(this
.Emulator);
_form.OnInitializeComponent();

//LaunchtheUIthread.

ThreaduiThread=new Thread(RunForm);
uiThread.SetApartmentState(ApartmentState.STA);
uiThread.Start();

//读Flash数据

EmulatorFlashPersistance.Load(this );

//必须添加这句,否则不会执行UninitializeComponent方法

Application.DoEvents();//这是我添的
}


///<summary>

/// Calledbytheemulatoraftertheprogramexits
///</summary>

publicoverridevoidUninitializeComponent()//这个函数不能保证会运行
{
//保存数据

EmulatorFlashPersistance.Save(this);//所以无法保存最后的结果
Application.DoEvents();//保存数据

base
.UninitializeComponent();
//
WhentheMicroFrameworkisshuttingdown,informthetheWinFormapplication
//toexitaswell.

Application.Exit();
}
目前该工作的开展对我来说,实在是一个不小的挑战。由于国内研究.Net Micro Framework不多,不仅没有人进行深层次的讨论,也少见相关资料,所以.Net Micro Framework推广真是任重而道远啊。

附记:在我上两篇关于串口部署的文章又有了新的进展,最近花了300多元购买了MoxaUPort1110 USB转串口设备(一般的杂牌子的设备大约几十元一个)还真不错,在我笔记本上终于可以直接通过串口对.Net Micro Framework进行调试了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值