📄 nfc.cpp
字号:
//------------------------------------------------------------------------------
// Copyright (C) 2006, Freescale Semiconductor, Inc. All Rights Reserved.
// THIS SOURCE CODE, AND ITS USE AND DISTRIBUTION, IS SUBJECT TO THE TERMS
// AND CONDITIONS OF THE APPLICABLE LICENSE AGREEMENT
//------------------------------------------------------------------------------
#include "bsp.h"
//-----------------------------------------------------------------------------
// External Functions
//-----------------------------------------------------------------------------
// External Variables
extern "C" PCSP_NANDFC_REGS g_pNFC;
//-----------------------------------------------------------------------------
// Defines
//-----------------------------------------------------------------------------
// Types
//-----------------------------------------------------------------------------
// Global Variables
//-----------------------------------------------------------------------------
// Local Variables
static DWORD g_dwNfcSysIntr;
static HANDLE g_hNfcIntrEvent;
static HANDLE g_hNfcWaitEvent;
static HANDLE g_hNfcIntrServThread;
//-----------------------------------------------------------------------------
// Local Functions
//-----------------------------------------------------------------------------
//
// Function: NFCIntrServThread
//
// This is the interrupt service thread for NANDFC interrupts.
//
// Parameters:
// lpParam
// [in] Thread data passed to the function using the
// lpParameter parameter of the CreateThread function. Not used.
//
// Returns:
// Returns thread exit code.
//
//-----------------------------------------------------------------------------
static DWORD WINAPI NFCIntrServThread (LPVOID lpParam)
{
DWORD rc = TRUE;
CeSetThreadPriority(GetCurrentThread(), 100);
while(TRUE)
{
if(WaitForSingleObject(g_hNfcIntrEvent, INFINITE) == WAIT_OBJECT_0)
{
// Signal the thread waiting for NANDFC
SetEvent(g_hNfcWaitEvent);
}
else
{
// Abnormal signal
rc = FALSE;
break;
}
}
return rc;
}
//-----------------------------------------------------------------------------
//
// Function: NFCAlloc
//
// This function maps the NFC peripheral, configures interupts, and
// configures NFC DMA support.
//
// Parameters:
// None.
//
// Returns:
// Returns TRUE on success. Returns FALSE on failure.
//
//-----------------------------------------------------------------------------
BOOL NFCAlloc(VOID)
{
DWORD irq;
PHYSICAL_ADDRESS phyAddr;
BOOL rc = FALSE;
// Map peripheral physical address to virtual address
phyAddr.QuadPart = CSP_BASE_REG_PA_NANDFC;
g_pNFC = (PCSP_NANDFC_REGS) MmMapIoSpace(phyAddr, sizeof(CSP_NANDFC_REGS),
FALSE);
// Check if virtual mapping failed
if (g_pNFC == NULL)
{
ERRORMSG(1, (_T("NFCAlloc: MmMapIoSpace failed!\r\n")));
}
// Translate IRQ into SYSINTR
irq = IRQ_NFC;
if (!KernelIoControl(IOCTL_HAL_REQUEST_SYSINTR, &irq, sizeof(irq),
&g_dwNfcSysIntr, sizeof(g_dwNfcSysIntr), NULL))
{
ERRORMSG(TRUE, (_T("IOCTL_HAL_REQUEST_SYSINTR failed for IRQ_NANDFC!\r\n")));
goto cleanUp;
}
// Create event for signaling threads waiting on NFC
g_hNfcWaitEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
if (!g_hNfcWaitEvent)
{
ERRORMSG(TRUE, (_T("CreateEvent failed for NANDFC wait event!\r\n")));
goto cleanUp;
}
// Create event for IST signaling
g_hNfcIntrEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
if (!g_hNfcIntrEvent)
{
ERRORMSG(TRUE, (_T("CreateEvent failed for NANDFC IST event!\r\n")));
goto cleanUp;
}
// Register interrupt
if (!InterruptInitialize(g_dwNfcSysIntr, g_hNfcIntrEvent, NULL, 0))
{
ERRORMSG(TRUE, (_T("InterruptInitialize failed for NANDFC SYSINTR!\r\n")));
goto cleanUp;
}
// Create IST for DVFC interrupts
g_hNfcIntrServThread = CreateThread(NULL, 0, NFCIntrServThread, NULL, 0, NULL);
if (!g_hNfcIntrServThread)
{
ERRORMSG(TRUE, (_T("CreateThread failed for NANDFC IST!\r\n")));
goto cleanUp;
}
rc = TRUE;
cleanUp:
return rc;
}
//-----------------------------------------------------------------------------
//
// Function: NFCWait
//
// This functions waits for the pending NANDFC operation to complete.
//
// Parameters:
// bPoll
// [in] - Set TRUE to poll for completion. Set FALSE to use NFC
// interrupt to signal completion.
//
// Returns:
// None.
//
//-----------------------------------------------------------------------------
VOID NFCWait(BOOL bPoll)
{
// bPoll = TRUE;
// If polling for completion
if (bPoll)
{
// Poll NANDFC interrupt bit
while (!(INREG16(&g_pNFC->NAND_FLASH_CONFIG2) & CSP_BITFMASK(NANDFC_NAND_FLASH_CONFIG2_INT)))
{
Sleep(0);
}
// Clear the NANDFC interrupt
CLRREG16(&g_pNFC->NAND_FLASH_CONFIG2,
CSP_BITFMASK(NANDFC_NAND_FLASH_CONFIG2_INT));
}
else
{
// Unmask NANDFC interrupt
INSREG16BF(&g_pNFC->NAND_FLASH_CONFIG1,
NANDFC_NAND_FLASH_CONFIG1_INT_MSK,
NANDFC_NAND_FLASH_CONFIG1_INT_MSK_UNMASK);
// Wait for NANDFC IST to signal us
WaitForSingleObject(g_hNfcWaitEvent, INFINITE);
// Clear the NANDFC interrupt
CLRREG16(&g_pNFC->NAND_FLASH_CONFIG2,
CSP_BITFMASK(NANDFC_NAND_FLASH_CONFIG2_INT));
// Mask NANDFC interrupt
INSREG16BF(&g_pNFC->NAND_FLASH_CONFIG1,
NANDFC_NAND_FLASH_CONFIG1_INT_MSK,
NANDFC_NAND_FLASH_CONFIG1_INT_MSK_MASK);
// Renable the NANDFC interrupt
InterruptDone(g_dwNfcSysIntr);
}
}
//-----------------------------------------------------------------------------
//
// Function: NFCSetClock
//
// This enables/disable clocks for the NANDFC.
//
// Parameters:
// None.
//
// Returns:
// None.
//
//-----------------------------------------------------------------------------
BOOL NFCSetClock(BOOL bEnabled)
{
BOOL rc;
if (bEnabled)
{
rc = DDKClockSetGatingMode(DDK_CLOCK_GATE_INDEX_NFG_BAUD, DDK_CLOCK_GATE_MODE_ENABLE);
}
else
{
rc = DDKClockSetGatingMode(DDK_CLOCK_GATE_INDEX_NFG_BAUD, DDK_CLOCK_GATE_MODE_DISABLE);
}
return rc;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -