在WinCE下,應(yīng)用程序直接讀/寫(xiě)/擦除flash設(shè)備的方法
先做個(gè)簡(jiǎn)單的介紹。WinCE支持Flash設(shè)備,一般指Nandflash或者是NORFlash,采用的架構(gòu)一般是FAL+FMD架構(gòu),我們實(shí)現(xiàn)FMD相關(guān)的接口函數(shù),F(xiàn)lash的驅(qū)動(dòng)就算完成了。當(dāng)WinCE啟動(dòng)以后,我們能夠看到Flash設(shè)備的磁盤(pán)。我們可以操作磁盤(pán)上面的文件,但是不能直接操作flash設(shè)備,對(duì)Flash設(shè)備的操作無(wú)非就是:讀,寫(xiě),擦除,讀ID。
現(xiàn)在開(kāi)始介紹實(shí)現(xiàn)的方法。我們?nèi)绻朐趹?yīng)用程序中直接調(diào)用FMD中的FMD_ReadSector(..),F(xiàn)MD_WriteSector(..),F(xiàn)MD_EraseBlock(..)是不太現(xiàn)實(shí)的。這里再補(bǔ)充一下,這三個(gè)函數(shù)分別是Flash的讀扇區(qū),寫(xiě)扇區(qū),擦除塊的函數(shù)。好像有點(diǎn)羅嗦了。但是我們可以在應(yīng)用程序中調(diào)用到FMD_OEMIoControl(..)函數(shù),這個(gè)是可以做到的。所以我們需要改一下Flash設(shè)備的驅(qū)動(dòng)程序,也就是改Flash設(shè)備驅(qū)動(dòng)中的FMD_OEMIoControl(..)這個(gè)函數(shù)。我的改動(dòng)如下:
BOOL FMD_OEMIoControl(DWORD dwIoControlCode, PBYTE pInBuf,DWORDnInBufSize, PBYTE pOutBuf, DWORD nOutBufSize,PDWORDpBytesReturned)
PFMDInterface pInterface = (PFMDInterface)pOutBuf;
RETAILMSG(1, (TEXT(FMD_OEMIoControl: control code is0x%xrn),dwIoControlCode));
switch(dwIoControlCode)
case IOCTL_FMD_GET_INTERFACE:
if (!pOutBuf nOutBufSize sizeof(FMDInterface))
DEBUGMSG(1, (TEXT(FMD_OEMIoControl: IOCTL_FMD_GET_INTERFACEbadmeter(s).rn)));
return(FALSE);
pInterface->cbSize = sizeof(FMDInterface);
pInterface->pInit = FMD_Init;
pInterface->pDeInit = FMD_Deinit;
pInterface->pGetInfo = FMD_GetInfo;
pInterface->pGetInfoEx = NULL; //FMD_GetInfoEx;
pInterface->pGetBlockStatus = FMD_GetBlockStatus;
pInterface->pSetBlockStatus = FMD_SetBlockStatus;
pInterface->pReadSector = FMD_ReadSector;
pInterface->pWriteSector = FMD_WriteSector;
pInterface->pEraseBlock = FMD_EraseBlock;
pInterface->pPowerUp = FMD_PowerUp;
pInterface->pPowerDown = FMD_PowerDown;
pInterface->pGetPhysSectorAddr = NULL;
pInterface->pOEMIoControl = FMD_OEMIoControl;
break;
case 0xff123456:
FMD_ReadSector(..);//調(diào)用讀Sector函數(shù)
break;
case 0xff654321:
FMD_WriteSector(..);//調(diào)用寫(xiě)Sector函數(shù)
break;
case 0xff123457:
FMD_EraseBlock(..);//調(diào)用擦除Block函數(shù)
break;
default:
DEBUGMSG(1, (TEXT(FMD_OEMIoControl: unrecognizedIOCTL(0x%x).rn), dwIoControlCode));
return(FALSE);
return(TRUE);
在FMD_OEMIoControl(..)函數(shù)里面增加了3個(gè)case,這3個(gè)case里面調(diào)用了讀/寫(xiě)/擦除函數(shù)。至于Case的值,我是隨便定義的。這樣Flash設(shè)備的驅(qū)動(dòng)部分就改完了。
在改完Flash驅(qū)動(dòng)以后,我下面會(huì)提供兩種方法,每一種方法都和Flash設(shè)備的注冊(cè)表配置有關(guān):
1. 以Nandflash為例,當(dāng)然對(duì)于NORFlash來(lái)說(shuō)大同小異,注冊(cè)表配置如下:
[HKEY_LOCAL_MACHINEDriversBuiltInNANDFlash]
Dll=ep94xxnandflash.dll
Prefix=DSK
Order=dword:4
;Ioctl=dword:4
Profile=NSFlash
IClass=A4E7EDDA-E575-4252-9D6B-4195D48BB865
; Override names in default profile
[HKEY_LOCAL_MACHINESystemStorageManagerProfilesNSFlash]
Name=Ep94xx NAND Flash
Folder=NANDFlash
PartitionDriver=MSPart.dll
AutoMount=dword:1
AutoPart=dword:1
AutoFormat=dword:1
[HKEY_LOCAL_MACHINESystemStorageManagerProfilesNSFlashFATFS]
EnableCache=dword:1
CacheSize=dword:1000
MountBootable=dword:1
Flags=dword:00000024
CheckForFormat=dword:1
評(píng)論