且构网

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

【玩转.Net MF – 01】Flash远程读写

更新时间:2022-10-12 09:19:57

目前在PC远程访问设备Flash,也就是部署TinyCLR和下载应用程序。在以前写的《NandFlash驱动开发》文章,我们知道Flash被分为六个区,典型的结构如下(以Sam9261_ek开发板为例):

const BlockRange g_K9F2G_8_BlockStatus[] =

{

    { BlockRange::BLOCKTYPE_BOOTSTRAP ,  0,  1 },

    { BlockRange::BLOCKTYPE_CONFIG    ,  2,  2 },

    { BlockRange::BLOCKTYPE_CODE      ,  3, 24 },

    { BlockRange::BLOCKTYPE_DEPLOYMENT, 25, 29 },

    { BlockRange::BLOCKTYPE_DEPLOYMENT, 30, 34 },

    { BlockRange::BLOCKTYPE_DEPLOYMENT, 35, 39 },

    { BlockRange::BLOCKTYPE_DEPLOYMENT, 40, 48 },

    { BlockRange::BLOCKTYPE_STORAGE_A , 49, 49 },

    { BlockRange::BLOCKTYPE_STORAGE_B , 50, 50 },   

    {BlockRange::BLOCKTYPE_FILESYSTEM, 51, FLASH_BLOCK_COUNT - 1 }

};

我们能否直接读写该Flash上的所有区呢?

实现这个功能的好处是易见的,我们再也没有必要为了下载一个应用程序而启动相对庞大的VS2008,再也不受必须打开MF工程才能下载的限制。在我们开发Ti DM355开发板就遇到类似问题,我们给异地开发板提供者演示相关程序功能时,必须要求对方安装VS2008,还必须发送我们的项目源码,否则就无法在另外的开发板上进行演示。

仔细研究了一下MFDeploy程序(这是典型的C#程序,在Vista和Windows7上可直接运行,在WinXP及以前的系统上需要安装.Net Framework运行时),发现可以为其开发一个插件来实现我们所要求的功能。

MFDeploy程序可以通过三种方式来访问.Net MF设备,串口、网口和USB,并且可以把TinyCLR部署到设备上去(需要开发板运行TinyBooter),也可以清空应用程序区,所以我们只要把这部分功能给扩展一下就可以了。

插件类必须继承于MFPlugInMenuItem类,相关代码如下:

public class PlugInHandle : MFPlugInMenuItem

     {

        public override string Name { get { return "Read/Write Flash"; } }

        public override void OnAction(IMFDeployForm form, MFDevice device)

        {

            if (form == null || device == null) return;

            (new frmRWFlash(form, device)).ShowDialog();

        }

}

其中由宿主传递过来的form和device非常重要,form就是针对MFDeploy主窗体,主要提供DumpToOutput函数,把消息显示到信息区,而device则提供和设备通信的相关函数,如Ping、Deploy、Erase、Execute等。

插件实现的第一步,要读写Flash区,首先要获取Flash的内存映像表,通过如下的代码就可以获取:

_DBG.WireProtocol.Commands.Monitor_FlashSectorMap.Reply reply = engine.GetFlashSectorMap();

if (reply != null)

{

    for (int i = 0; i < reply.m_map.Length; i++)

    {

        _DBG.WireProtocol.Commands.Monitor_FlashSectorMap.FlashSectorData fsd = reply.m_map[i];

        string usage = "";

        switch (fsd.m_flags & _DBG.WireProtocol.Commands.Monitor_FlashSectorMap.c_MEMORY_USAGE_MASK)

        {

            case _DBG.WireProtocol.Commands.Monitor_FlashSectorMap.c_MEMORY_USAGE_APPLICATION:

                usage = "Application";

                break;

            case _DBG.WireProtocol.Commands.Monitor_FlashSectorMap.c_MEMORY_USAGE_BOOTSTRAP:

                usage = "Bootstrap";

                break;

            case _DBG.WireProtocol.Commands.Monitor_FlashSectorMap.c_MEMORY_USAGE_CODE:

                usage = "Code";

                break;

            case _DBG.WireProtocol.Commands.Monitor_FlashSectorMap.c_MEMORY_USAGE_CONFIG:

                usage = "Configuration";

                break;

            case _DBG.WireProtocol.Commands.Monitor_FlashSectorMap.c_MEMORY_USAGE_DEPLOYMENT:

                usage = "Deployment";

                break;

            case _DBG.WireProtocol.Commands.Monitor_FlashSectorMap.c_MEMORY_USAGE_JITTER:

                usage = "Jitter";

                break;

            case _DBG.WireProtocol.Commands.Monitor_FlashSectorMap.c_MEMORY_USAGE_FS:

                usage = "File System";

                break;

            case _DBG.WireProtocol.Commands.Monitor_FlashSectorMap.c_MEMORY_USAGE_RESERVED:

                usage = "Reserved";

                break;

            case _DBG.WireProtocol.Commands.Monitor_FlashSectorMap.c_MEMORY_USAGE_STORAGE_A:

                usage = "Storage";

                break;

            case _DBG.WireProtocol.Commands.Monitor_FlashSectorMap.c_MEMORY_USAGE_STORAGE_B:

                usage = "Storage";

                break;

            case 0xA0:

                usage = "Custom";

                break;

        }

        FlashMap.Add(FlashMaps, usage, fsd.m_address, fsd.m_size);

  }

}

第二步就是读写Flash的扇区数据,可以通过如下代码实现:

bool ret = engine.ReadMemory(addr, buflen, out bytData);

bool ret = engine.WriteMemory(addr, FileIndex.GetBytes(), 0, (int)FileIndex.HeadSize);

但是仅仅实现以上两步还不行,因为TinyCLR代码会限制一些Flash的读写范围,所以我们还得需要修改TinyCLR的代码。

在/CLR/Debugger目录下的Debugger.cpp文件有一个函数CheckPermission,我们修改一下它的代码,让它直接返回true就可以了,这样我们就可以对Flash任何区都可以读写了。

bool CLR_DBG_Debugger::CheckPermission( ByteAddress address, int mode )

{

    NATIVE_PROFILE_CLR_DEBUGGER();

    bool   hasPermission = false;

    UINT32 regionIndex, rangeIndex;

 

    m_deploymentStorageDevice->FindRegionFromAddress( address, regionIndex, rangeIndex );

    const BlockRange& range = m_deploymentStorageDevice->GetDeviceInfo()->Regions[ regionIndex ].BlockRanges[ rangeIndex ];

 

    return true;

    /*

    switch(mode)

    {

        case AccessMemory_Check:

            hasPermission = true;

            break;

        case AccessMemory_Read:

            switch(range.RangeType)

            {

                case BlockRange::BLOCKTYPE_CONFIG:         // fall through

                case BlockRange::BLOCKTYPE_DIRTYBIT:       // fall through

                case BlockRange::BLOCKTYPE_DEPLOYMENT:     // fall through

                case BlockRange::BLOCKTYPE_FILESYSTEM:     // fall through

                case BlockRange::BLOCKTYPE_STORAGE_A:      // fall through

                case BlockRange::BLOCKTYPE_STORAGE_B:

 

                    hasPermission = true;

                    break;

            }

            break;

        case AccessMemory_Write:

            if(range.IsDeployment())

            {

                hasPermission = true;

            }

            break;

        case AccessMemory_Erase:

            switch(range.RangeType)

            {

                case BlockRange::BLOCKTYPE_DEPLOYMENT:   // fall through

                case BlockRange::BLOCKTYPE_FILESYSTEM:   // fall through

                case BlockRange::BLOCKTYPE_STORAGE_A:    // fall through

                case BlockRange::BLOCKTYPE_STORAGE_B:

                    hasPermission = true;

                    break;

            }

            break;

        default:

            hasPermission = false;

            break;

    }

 

    return hasPermission;*/

}

编译好插件,把它拷贝到MFDeploy.exe文件所在的子目录PlugIn,启动MFDeploy.exe程序,在Plug-in菜单下就会有我们的插件菜单。

这样就可以对Flash上的任何区都可以进行读写了。并且针对Deployment区,不仅可以下载.Net Micro Framework应用程序,也可以单击“Run”按钮,运行当前下载的程序(这样就不用重启TinyCLR了)。

针对File System区,我考虑实现一个类似VS2008远程工具“远程文件查看器”,这样PC就会更方便和.Net MF设备进行交互了。如果大家看过我以前写的《LCD驱动开发》,屏幕上显示的位图,就是通过该插件下载的。