📄 cspiclass.cpp
字号:
// [in] Pointer to buffer from which data will be read.
//
// Returns:
// Returns data pointed to by specified buffer, promoted to UINT32.
//
//-----------------------------------------------------------------------------
UINT32 cspiClass::CspiBufRd8(LPVOID pBuf)
{
UINT8 *p;
p = (UINT8 *) pBuf;
return *p;
}
//-----------------------------------------------------------------------------
//
// Function: CspiBufRd16
//
// This function is used to access a buffer as an array of 16-bit (UINT16)
// values and read data from the specified location.
//
// Parameters:
// pBuf
// [in] Pointer to buffer from which data will be read.
//
// Returns:
// Returns data pointed to by specified buffer, promoted to UINT32.
//
//-----------------------------------------------------------------------------
UINT32 cspiClass::CspiBufRd16(LPVOID pBuf)
{
UINT16 *p;
p = (UINT16 *) pBuf;
return *p;
}
//-----------------------------------------------------------------------------
//
// Function: CspiBufRd32
//
// This function is used to access a buffer as an array of 32-bit (UINT32)
// values and read data from the specified location.
//
// Parameters:
// pBuf
// [in] Pointer to buffer from which data will be read.
//
// Returns:
// Returns data pointed to by specified buffer.
//
//-----------------------------------------------------------------------------
UINT32 cspiClass::CspiBufRd32(LPVOID pBuf)
{
UINT32 *p;
p = (UINT32 *) pBuf;
return *p;
}
//-----------------------------------------------------------------------------
//
// Function: CspiBufWrt8
//
// This function is used to access a buffer as an array of 8-bit (UINT8)
// values and writes data to the specified buffer location.
//
// Parameters:
// pBuf
// [in] Pointer to buffer to which data will be written.
//
// data
// [in] Data to be written demoted to UINT8.
//
// Returns:
// None.
//
//-----------------------------------------------------------------------------
void cspiClass::CspiBufWrt8(LPVOID pBuf, UINT32 data)
{
UINT8 *p;
p = (UINT8 *) pBuf;
*p = (UINT8) data;
}
//-----------------------------------------------------------------------------
//
// Function: CspiBufWrt16
//
// This function is used to access a buffer as an array of 16-bit (UINT16)
// values and writes data to the specified buffer location.
//
// Parameters:
// pBuf
// [in] Pointer to buffer to which data will be written.
//
// data
// [in] Data to be written demoted to UINT16.
//
// Returns:
// None.
//
//-----------------------------------------------------------------------------
void cspiClass::CspiBufWrt16(LPVOID pBuf, UINT32 data)
{
UINT16 *p;
p = (UINT16 *) pBuf;
*p = (UINT16) data;
}
//-----------------------------------------------------------------------------
//
// Function: CspiBufWrt32
//
// This function is used to access a buffer as an array of 32-bit (UINT32)
// values and writes data to the specified buffer location.
//
// Parameters:
// pBuf
// [in] Pointer to buffer to which data will be written.
//
// data
// [in] Data to be written.
//
// Returns:
// None.
//
//-----------------------------------------------------------------------------
void cspiClass::CspiBufWrt32(LPVOID pBuf, UINT32 data)
{
UINT32 *p;
p = (UINT32 *) pBuf;
*p = data;
}
// ----------------------------------------------------------------------------
// Function: MoveDMABuffer
// Move data from s/g buffer to DMA buffer or vice versa
//
// Parameters:
// pBuf
// Destination or Source buffer according to bReceive.
//
// dwLen
// Length of the buffer to be copied.
//
// bReceive
// Used to indicate Receive or Transmit.
//
// Returns:
// None.
//
// ----------------------------------------------------------------------------
VOID cspiClass::MoveDMABuffer(LPVOID pBuf, DWORD dwLen, BOOL bReceive)
{
DEBUGMSG(ZONE_FUNCTION, (_T("Cspi: MoveDMABuffer(%d)+\n"), dwLen));
// Map address and check for security violation
LPBYTE pBuffer = (LPBYTE)MapCallerPtr(pBuf, dwLen);
if (pBuf != NULL && pBuffer == NULL)
{
// security violation
DEBUGMSG(ZONE_ERROR, (TEXT("Cspi: MoveDMABuffer Failed to map pointer to caller\r\n")));
return;
}
if (bReceive)
memcpy(pBuffer, pVirtDMABufferAddr + CSPI_RECV_OFFSET, dwLen);
else
memcpy(pVirtDMABufferAddr + CSPI_TXMT_OFFSET, pBuffer, dwLen);
}
//------------------------------------------------------------------------------
//
// Function: MapDMABuffers
//
// Allocate and map DMA buffer
//
// Parameters:
// None
//
// Returns:
// Returns TRUE if successful, otherwise returns FALSE.
//
//------------------------------------------------------------------------------
BOOL cspiClass::MapDMABuffers(void)
{
DMA_ADAPTER_OBJECT Adapter;
DEBUGMSG(ZONE_FUNCTION,(_T("Cspi::MapDMABuffers+\r\n")));
pVirtDMABufferAddr = NULL;
memset(&Adapter, 0, sizeof(DMA_ADAPTER_OBJECT));
Adapter.InterfaceType = Internal;
Adapter.ObjectSize = sizeof(DMA_ADAPTER_OBJECT);
// Allocate a block of virtual memory (physically contiguous) for the DMA buffers.
pVirtDMABufferAddr = (PBYTE)HalAllocateCommonBuffer(&Adapter, (CSPI_SDMA_BUFFER_SIZE)
, &(PhysDMABufferAddr), FALSE);
if (pVirtDMABufferAddr == NULL)
{
DEBUGMSG(ZONE_ERROR, (TEXT("Cspi: MapDMABuffers() - Failed to allocate DMA buffer.\r\n")));
return(FALSE);
}
DEBUGMSG(ZONE_FUNCTION,(_T("Cspi::MapDMABuffers-\r\n")));
return(TRUE);
}
//------------------------------------------------------------------------------
//
// Function: UnmapDMABuffers
//
// This function unmaps the DMA buffers previously mapped with the
// MapDMABuffers function.
//
// Parameters:
// None
//
// Returns:
// Returns TRUE if successful, otherwise returns FALSE.
//
//------------------------------------------------------------------------------
BOOL cspiClass::UnmapDMABuffers(void)
{
DEBUGMSG(ZONE_FUNCTION,(_T("Cspi::UnmapDMABuffers+\r\n")));
if(pVirtDMABufferAddr)
{
// Logical address parameter is ignored
PhysDMABufferAddr.QuadPart = 0;
HalFreeCommonBuffer(NULL, 0, PhysDMABufferAddr, (PVOID)pVirtDMABufferAddr, FALSE);
}
return TRUE;
}
//------------------------------------------------------------------------------
//
// Function: DeinitChannelDMA
//
// This function deinitializes the DMA channel for output.
//
// Parameters:
// None
//
// Returns:
// Returns TRUE if successful, otherwise returns FALSE.
//
//------------------------------------------------------------------------------
BOOL cspiClass::DeinitChannelDMA(void)
{
if (m_dmaChanCspiRx != 0)
{
DDKSdmaCloseChan(m_dmaChanCspiRx);
m_dmaChanCspiRx = 0;
}
if (m_dmaChanCspiTx != 0)
{
DDKSdmaCloseChan(m_dmaChanCspiTx);
m_dmaChanCspiTx = 0;
}
return TRUE;
}
//------------------------------------------------------------------------------
//
// Function: InitChannelDMA
//
// This function initializes the DMA channel for output.
//
// Parameters:
// Index
// CSPI device Index
// Returns:
// Returns TRUE if successful, otherwise returns FALSE.
//
//------------------------------------------------------------------------------
BOOL cspiClass::InitChannelDMA(UINT32 Index)
{
BOOL rc = FALSE;
DEBUGMSG(ZONE_FUNCTION,(_T("Cspi::InitChannelDMA+\r\n")));
// Check if DMA buffer has been allocated
if (!PhysDMABufferAddr.LowPart || !pVirtDMABufferAddr)
{
DEBUGMSG(ZONE_ERROR, (_T("ERROR:InitChannelDMA: Invalid DMA buffer physical address.\r\n")));
goto cleanUp;
}
// Open virtual DMA channels
m_dmaChanCspiRx = DDKSdmaOpenChan(m_dmaReqRx, BSPCspiGetChannelPriority(), NULL, CspCSPIGetIRQ(Index));
DEBUGMSG(ZONE_DMA,(_T("Channel Allocated(Rx) : %d\r\n"),m_dmaChanCspiRx));
if (!m_dmaChanCspiRx)
{
DEBUGMSG(ZONE_ERROR, (_T("ERROR:InitChannelDMA(Rx): SdmaOpenChan for input failed.\r\n")));
goto cleanUp;
}
// Allocate DMA chain buffer
if (!DDKSdmaAllocChain(m_dmaChanCspiRx, CSPI_MAX_DESC_COUNT_RX))
{
DEBUGMSG(ZONE_ERROR, (_T("ERROR:InitChannelDMA(Rx): DDKSdmaAllocChain for input failed.\r\n")));
goto cleanUp;
}
// Initialize the chain and set the watermark level
if (!DDKSdmaInitChain(m_dmaChanCspiRx, CSPI_DMA_WATERMARK_RX))
{
DEBUGMSG(ZONE_ERROR, (_T("ERROR:InitChannelDMA(Rx): DDKSdmaInitChain failed.\r\n")));
goto cleanUp;
}
// Open virtual DMA channels
m_dmaChanCspiTx = DDKSdmaOpenChan(m_dmaReqTx, BSPCspiGetChannelPriority(), NULL, CspCSPIGetIRQ(Index));
DEBUGMSG(ZONE_DMA,(_T("Channel Allocated(Tx) : %d\r\n"),m_dmaChanCspiTx));
if (!m_dmaChanCspiTx)
{
DEBUGMSG(ZONE_ERROR, (_T("ERROR:InitChannelDMA(Tx): SdmaOpenChan(Rx) for input failed.\r\n")));
goto cleanUp;
}
// Allocate DMA chain buffer
if (!DDKSdmaAllocChain(m_dmaChanCspiTx, CSPI_MAX_DESC_COUNT_TX))
{
DEBUGMSG(ZONE_ERROR, (_T("ERROR:InitChannelDMA(Tx): DDKSdmaAllocChain for input failed.\r\n")));
goto cleanUp;
}
// Initialize the chain and set the watermark level
if (!DDKSdmaInitChain(m_dmaChanCspiTx, CSPI_DMA_WATERMARK_TX))
{
DEBUGMSG(ZONE_ERROR, (_T("ERROR:InitChannelDMA(Tx): DDKSdmaInitChain failed.\r\n")));
goto cleanUp;
}
rc = TRUE;
cleanUp:
if (!rc)
{
DeinitChannelDMA();
}
DEBUGMSG(ZONE_DMA,(_T("Cspi::InitChannelDMA-\r\n")));
return rc;
}
//------------------------------------------------------------------------------
//
// Function: InitDMA
//
// Performs DMA channel intialization
//
// Parameters:
// Index
// CSPI device Index
//
// Returns:
// Returns TRUE if successful, otherwise returns FALSE.
//
//------------------------------------------------------------------------------
BOOL cspiClass::InitCspiDMA(UINT32 Index)
{
DEBUGMSG(ZONE_FUNCTION, (TEXT("Cspi: InitDMA+\r\n")));
m_dmaReqTx = CspCSPIGetDmaReqTx(Index) ;
m_dmaReqRx = CspCSPIGetDmaReqRx(Index) ;
// Map the DMA buffers into driver's virtual address space
if (!MapDMABuffers())
{
DEBUGMSG(ZONE_ERROR, (TEXT("Cspi:InitDMA() - Failed to map DMA buffers.\r\n")));
return FALSE;
}
// Initialize the output DMA
if (!InitChannelDMA(Index))
{
DEBUGMSG(ZONE_ERROR, (TEXT("Cspi:InitDMA() - Failed to initialize output DMA.\r\n")));
return FALSE;
}
DEBUGMSG(ZONE_FUNCTION, (TEXT("Cspi::InitDMA-\r\n")));
return TRUE ;
}
//------------------------------------------------------------------------------
//
// Function: DeInitDMA
//
// Performs deintialization of DMA
//
// Parameters:
// None
//
// Returns:
// Returns TRUE if successful, otherwise returns FALSE.
//
//------------------------------------------------------------------------------
BOOL cspiClass::DeInitCspiDMA(void)
{
if(!DeinitChannelDMA())
{
DEBUGMSG(ZONE_ERROR, (TEXT("Cspi:DeinitChannelDMA() - Failed to deinitialize DMA.\r\n")));
return FALSE;
}
if(!UnmapDMABuffers())
{
DEBUGMSG(ZONE_ERROR, (TEXT("Cspi:UnmapDMABuffers() - Failed to Unmap DMA buffers\r\n")));
return FALSE;
}
return TRUE ;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -