且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-09-18 11:54:07

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


  1. [Serializable]  
  2.         private class FlashDatas  
  3.         {  
  4.             //调入数据  
  5.             public static byte[] Load(uint index)  
  6.             {  
  7.                 ExtendedWeakReference ewr = ExtendedWeakReference.RecoverOrCreate(  
  8.                         typeof(FlashDatas),                       //类型,任意类都可以,其名称起到一个索引作用  
  9.                         index,                                    //ID号,这个数据比较有用,不同ID号代表不同数据  
  10.                         ExtendedWeakReference.c_SurviveBoot);//该标志和.c_SurviveBoot 区别不大  
  11.                 return ewr.Target as byte[];  
  12.             }          
  13.    
  14.             //保存数据  
  15.             public static void Save(uint index, byte[] data)  
  16.             {  
  17.                 ExtendedWeakReference ewr = ExtendedWeakReference.RecoverOrCreate(typeof(FlashDatas), index, ExtendedWeakReference.c_SurviveBoot);  
  18.                 ewr.Target = data;  
  19.             }  
  20.     }  

上面的代码就是ExtendedWeakReference类的具体使用,从代码可以看出我们无法直接对存储器进行读写,只能通过保存类的方式对数据进行存储,至于该数据存放到何处,那是无从得知的。
我最初的想法是定义一个类,类中定义一个大数组,不过实际调试发现,该数组不能太大,超过几十K就会出现内存溢出。幸好该对象可以是字节数组,所以我产生了另一个想法,每次保存一个512字节大小的字节数组,相当于磁盘上的一个扇区,以此为存取的最小单位,实现FAT文件系统。
在我博客上曾写了一篇关于FAT文件系统的文章,如《FAT文件系统几点释疑》(http://yfsoft.blog.51cto.com/1635641/324291),我们知道要实现FAT16系统一般至少需要4M存储空间,实现FAT32一般需要256M空间以上。所以我简单的做了一个程序,在实际硬件中测试一下存取1024*4个512字节的数组内存是否可行。
测试代码如下:
       


  1. private const uint SectorSize=512;   //扇区大小  
  2.         private const uint SecPerClus = 4;   //一个簇包含的扇区数  
  3.         public static void Main()  
  4.         {   
  5.             Debug.Print("Start");  
  6.             for (uint i = 0; i < 512; i++)    //1024*4  
  7.             {  
  8.                 byte[] bytData = new byte[SectorSize];  
  9.                 bytData[0] = (byte)(i % 256);  
  10.                 bytData[bytData.Length - 1] = bytData[0];  
  11.                 FlashDatas.Save(i, bytData);  
  12.                Debug.Print(i.ToString() + " Save " + bytData[0].ToString() + " " + bytData[bytData.Length - 1].ToString());  
  13.    
  14.                 //byte[] bytData = FlashDatas.Load(i);  
  15.                 //if (bytData == null)  
  16.                 //{  
  17.                 //    Debug.Print(i.ToString() + " Load Error");  
  18.                 //    break;  
  19.                 //}  
  20.                 //else  
  21.                 //{  
  22.                 //    Debug.Print(i.ToString() + " Load " + bytData[0].ToString() + " " + bytData[bytData.Length - 1].ToString());  
  23.                 //}  
  24.             }  
  25.             Debug.Print("Exit");  
  26.  }  
  27.      

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


  1. ------------------------------------------------------------------------  
  2. //内存大小0x10000*16 = 1024*1024 也就是1M空间。  
  3. public class FlashManager : MemoryManagerBase  
  4. {  
  5.     // Fields 存储空间已经写死,就是1M  
  6.     private FlashSector[] _flashSectors = new FlashSector[] {   
  7. new FlashSector(0x10000, FlashSectorUsage.Log, FlashSectorPartition.Start),  
  8. new FlashSector(0x10000, FlashSectorUsage.Log, FlashSectorPartition.None),   
  9. new FlashSector(0x10000, FlashSectorUsage.Log, FlashSectorPartition.None),   
  10. new FlashSector(0x10000, FlashSectorUsage.Log, FlashSectorPartition.None),   
  11. new FlashSector(0x10000, FlashSectorUsage.Log, FlashSectorPartition.None),   
  12. new FlashSector(0x10000, FlashSectorUsage.Log, FlashSectorPartition.None),   
  13. new FlashSector(0x10000, FlashSectorUsage.Log, FlashSectorPartition.None),   
  14. new FlashSector(0x10000, FlashSectorUsage.Log, FlashSectorPartition.End),   
  15.    
  16. new FlashSector(0x10000, FlashSectorUsage.Log, FlashSectorPartition.Start),   
  17. new FlashSector(0x10000, FlashSectorUsage.Log, FlashSectorPartition.None),   
  18. new FlashSector(0x10000, FlashSectorUsage.Log, FlashSectorPartition.None),   
  19. new FlashSector(0x10000, FlashSectorUsage.Log, FlashSectorPartition.None),   
  20. new FlashSector(0x10000, FlashSectorUsage.StorageA, FlashSectorPartition.None),  
  21. new FlashSector(0x10000, FlashSectorUsage.StorageA, FlashSectorPartition.None),   
  22. new FlashSector(0x10000, FlashSectorUsage.StorageB, FlashSectorPartition.None),   
  23. new FlashSector(0x10000, FlashSectorUsage.StorageB, FlashSectorPartition.End) };  
  24.    
  25. ....  
  26. }  
  27.    
  28. //分配内存  
  29. internal override void AllocateMemory()  
  30. {  
  31.         this.ValidateFlashSectorsInternal();  
  32.         uint num = 0;  
  33.         for (int i = 0; i < this._flashSectors.Length; i++)  
  34.         {  
  35.             num += this._flashSectors[i].Length;  
  36.         }  
  37.         base._size = num;  
  38.         base.AllocateMemory();    //分配内存 ... ...  
  39.         for (int j = 0; j < base._size; j++)  
  40.         {  
  41.             base._memory[j] = 0xff;  
  42.         }  
  43.         this.InitializeFlashSectorsInternal();  
  44. }  
  45.    
  46. //分配内存  
  47. internal virtual void AllocateMemory()  
  48. {  
  49.         this._memory = new byte[this._size];  
  50.         this._handle = GCHandle.Alloc(this._memory, 3);  
  51. }  

此外模拟器在运行结束时,不能保证执行重载的UninitializeComponent函数,所以无法保存内存的数据,代码如下。
 


  1. /// <summary>  
  2.         /// Called by the emulator after all components were setup and registered  
  3.         /// </summary>  
  4.         public override void InitializeComponent()  
  5.         {  
  6.             base.InitializeComponent();   
  7.             _form = new YFEmulatorForm(this.Emulator);  
  8.             _form.OnInitializeComponent();  
  9.    
  10.             //Launch the UI thread.  
  11.             Thread uiThread = new Thread(RunForm);  
  12.             uiThread.SetApartmentState(ApartmentState.STA);  
  13.             uiThread.Start();  
  14.    
  15.             //读Flash数据  
  16.             EmulatorFlashPersistance.Load(this);  
  17.    
  18.             //必须添加这句,否则不会执行UninitializeComponent方法  
  19.             Application.DoEvents();     //这是我添的  
  20.         }  
  21.           
  22.          
  23.         /// <summary>  
  24.         /// Called by the emulator after the program exits  
  25.         /// </summary>  
  26.         public override void UninitializeComponent()    //这个函数不能保证会运行  
  27.         {  
  28.             //保存数据  
  29.             EmulatorFlashPersistance.Save(this);        //所以无法保存最后的结果  
  30.             Application.DoEvents(); //保存数据   
  31.    
  32.             base.UninitializeComponent();  
  33.             //When the Micro Framework is shutting down, inform the the WinForm application   
  34.             //to exit as well.  
  35.             Application.Exit();  
  36.         }  

目前该工作的开展对我来说,实在是一个不小的挑战。由于国内研究.Net Micro Framework不多,不仅没有人进行深层次的讨论,也少见相关资料,所以.Net Micro Framework推广真是任重而道远啊。
附记:在我上两篇关于串口部署的文章又有了新的进展,最近花了300多元购买了Moxa的UPort1110 USB转串口设备(一般的杂牌子的设备大约几十元一个)还真不错,在我笔记本上终于可以直接通过串口对.Net Micro Framework进行调试了。

 

 








本文转自yefanqiu51CTO博客,原文链接:http://blog.51cto.com/yfsoft/321282,如需转载请自行联系原作者