以下是MSDN中的說法:
The Windows CE NDIS implementation does not support direct memory access (DMA). Do not use the NdisSetupDmaTransfer, NdisCompleteDmaTransfer, NdisMRegisterDmaChannel, NdisMDeregisterDmaChannel, NdisMAllocateMapRegisters, NdisMFreeMapRegisters, NdisMReadDmaCounter, and NdisGetSharedDataAlignment functions.
Use the NdisMAllocateSharedMemory and NdisMFreeSharedMemory functions for PCI bus master DMA transfers.
具體如何實現DMA呢?
在microsoft windows CE device driver kit中有一節是:Implementing DMA for NDIS Miniport Drivers
專門提到了這個問題,當前ce還沒有自己的DMA機制,然而開發人員可以在他們自己的小端口卡驅動程序中實現相同的功能,但是寫的太簡單了
Implementing DMA for NDIS Miniport Drivers
DMA is important for efficient networking because it enables the layers in
the networking architecture to share data without first copying that data
two categories of DMA: slave DMA and busmaster DMAB
Slave DMA
Slave DMA is appropriate for OEMs because it requires a block of memory that
is pre-allocated in the device memory map of the Windows CE–based platform.
Miniport drivers can map that block of physical memory to the driver’virtual
memory space, and then use the VirtualAlloc and VirtualCopy functions to move
data in and out of that space
busmaster DMA
To receive data, the driver allocates a shared memory block, transfers the
data to buffers within that block, and uses NDIS functions to indicate that a
packet has arrived. To send data, the driver uses the LockPages and
UnlockPages functions to map the virtual memory to device memory, informs the
NIC of the addresses of the data, and instructs the NIC to send the data.
This method can be faster for larger data block sizes.
后來發MAIL給一位曾經遇到此問題的朋友,回復如下:
事實上在使用DMA是的關鍵是得到一個連續的物理地址空間,然后將該空間的物理地址放入DMA控制器的源地址或者目的地址寄存器中(根據你使用DMA的方向決定);
然后等待DMA中斷;
由于DMA使用的是物理地址但是在wince中只能使用虛擬地址,所以在DMA中斷產生之后如需分析數據請使用虛擬地址;
特別提示參考一下的文章:
DMA的使用
1、 芯片DMA的使用要點:
AK3224芯片的DMA使用中,RAM的地址作為DMA傳輸的目標地址、源地址,必須要4字節對齊。而且DMA的操作長度以內的RAM地址,必須連續。
不過在使用中發現:Nandflash驅動中RAM地址作為目標地址時,只需要2字節對齊。RAM地址作為源地址可不需對齊。(其他情況需要逐一驗證)
2、 wince中的DMA使用:
根據DMA一次操作的RAM地址必須連續的特性,在驅動DMA使用時,我們需要確保虛擬地址映射的物理地址是連續的。有3個途徑:
1:數據區地址是由應用層或者其他進程、線程傳入的,驅動并不知道其虛擬地址對應的物理地址是否一直連續。
由于wince的內存申請,是以4K字節為一個頁,一段數據的內存申請可能跨越多個頁。因此,只要數據區長度大于1字節,就有可能其物理地址是跨越的、不連續的。為了確保DMA操作,我們必須查詢這段數據區在RAM上的物理分布。
首先,得到數據區所在的虛擬頁: VirPageStart = (ULONG)pSourceBuffer & 0xFFFFF000;
其次,得到數據區在頁內的偏移地址 :offset = (ULONG)pSourceBuffer & 0x0FFF;
計算數據區是否跨越頁段
if(offset + NumberOfBytes > 4096)
PageSize = WCE_UNIFORM_SIZE - offset; //整個數據跨越此頁,則DMA傳輸需要分多個部分,一次一個頁段的傳
else
PageSize = NumberOfBytes; //數據區沒有跨越頁
由得到的頁地址,查詢映射的物理地址。
if(!LockPages((LPVOID)VirPageStart, 4096, &TransAddr, LOCKFLAG_READ))
{
//異常處理
}
UnlockPages((LPVOID)VirPageStart, 4096);
得到了映射的物理地址TransAddr后,根據RAM是目標地址還是源地址,做進一步的處理。
假設一個數據區作為DMA源地址,大小為9K。在虛擬地址首頁的偏移為4K。那么它必然跨越3個頁段。
首先查詢第一頁的物理地址發送,第一個頁的2K數據。然后查詢第二頁的物理地址,發送4K數據。最后查詢第三頁的物理地址,發送3K數據。
2:數據區的申請可以使用AllocPhysMem函數申請。
LPVOID AllocPhysMem(
DWORD cbSize, 參數1:數據區大小
DWORD fdwProtect, 參數2:保護標記
DWORD dwAlignmentMask, 參數3:0(default system)
DWORD dwFlags, 參數4:0(Reserved for future use)
PULONG pPhysicalAddress 參數5:得到數據區對應的物理地址
);
AllocPhysMem函數返回值為指向申請后的虛擬地址指針。
如:pSerialHead->RxBufferInfo.RxCharBuffer = //alloc physical memory
AllocPhysMem(pSerialHead->RxBufferInfo.Length + 16, PAGE_READWRITE, 0, 0, &RX_PhyAddr);
由于此函數必定申請到一片連續的物理地址,因此pSerialHead->RxBufferInfo.RxCharBuffer的使用不再需要查詢是否跨越多個頁段。
但是,AllocPhysMem函數申請的物理地址可能會跨越多個RAM CHIP。因此,在使用1片以上RAM芯片的系統中,依然需要查詢是否跨越CHIP。
AllocPhysMem函數使用后,需要使用FreePhysMem函數進行釋放。
3:數據區可以在系統config.bib文件中,預先定義好一片連續、不跨越CHIP的RAM空間。
如下,系統保留了虛擬地址0x80024000開始,大小為0x3000的一段RAM。
SER_DMA 80024000 00003000 RESERVED
那么驅動DMA使用中,不再需要對這段內存,進行任何的查詢動作。我們只需要在進程空間中做映射即可。
pSerialHead->RxBufferInfo.RxCharBuffer = VirtualAlloc(0, RX_PhySize, MEM_RESERVE, PAGE_NOACCESS);
if (pSerialHead->RxBufferInfo.RxCharBuffer == NULL)
{
DEBUGMSG(ZONE_ERROR, (TEXT("COM_Init:: VirtualAlloc failed!\r\n")));
return(NULL);
}
else
{
if (!VirtualCopy((PVOID)pSerialHead->RxBufferInfo.RxCharBuffer, (PVOID)(RX_PhyAddr),
RX_PhySize, (PAGE_READWRITE | PAGE_NOCACHE)))
{
DEBUGMSG(ZONE_ERROR, (TEXT("COM_Init:: VirtualCopy failed!\r\n")));
return(NULL);
}
}
上面這段程序中,先使用函數VirtualAlloc,在進程空間中申請一段保留的虛擬地址空間。然后使用VirtualCopy,把需要使用的物理地址空間,映射到已經申請好的虛擬地址上。使用完畢,必須使用函數VirtualFree進行釋放。
LPVOID VirtualAlloc(
LPVOID lpAddress,
DWORD dwSize,
DWORD flAllocationType,
DWORD flProtect
);
BOOL VirtualCopy(
LPVOID lpvDest,
LPVOID lpvSrc,
DWORD cbSize,
DWORD fdwProtect
);
BOOL VirtualFree(
LPVOID lpAddress,
DWORD dwSize,
DWORD dwFreeType
);