⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 i2ctest.cpp

📁 Microsoft WinCE 6.0 BSP FINAL release source code for use with the i.MX27ADS TO2 WCE600_FINAL_MX27_S
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//      The two byte content stored in byReg.
//
//-----------------------------------------------------------------------------

WORD I2CReadTwoByte(HANDLE hI2C, BYTE byAddr, BYTE byReg, LPINT lpiResult)
{
    I2C_TRANSFER_BLOCK I2CXferBlock;
    I2C_PACKET I2CPacket[2];
    BYTE byOutData;
    BYTE byInData[2];

    byOutData = byReg;

    I2CPacket[0].pbyBuf = (PBYTE) &byOutData;
    I2CPacket[0].wLen = sizeof(byOutData);

    I2CPacket[0].byRW = I2C_RW_WRITE;
    I2CPacket[0].byAddr = byAddr;
    I2CPacket[0].lpiResult = lpiResult;


    I2CPacket[1].pbyBuf = (PBYTE) &byInData;
    I2CPacket[1].wLen = sizeof(byInData);

    I2CPacket[1].byRW = I2C_RW_READ;
    I2CPacket[1].byAddr = byAddr;
    I2CPacket[1].lpiResult = lpiResult;

    I2CXferBlock.pI2CPackets = I2CPacket;
    I2CXferBlock.iNumPackets = 2;

    I2C_MACRO_TRANSFER(hI2C, &I2CXferBlock);

    return ((0xFF & byInData[1]) | (((WORD) byInData[0]) << 8));
}

//------------------------------------------------------------------------------
//
// Function: CheckBytesRead
//
// This function compares a buffer of bytes read from the I2C
// to a buffer containing the expected result values, and returns
// an error if the values do not match up.  This method also
// outputs the bytes read and bytes expected.
//
// Parameters:
//      lpbyReadBuf
//           [in] pointer to buffer containing bytes read.
//
//      lpbyCheckBuf
//           [in] pointer to buffer containing expected result.
//
//      uiLen
//           [in] number of bytes to compare.
//
// Returns:
//      TPR_PASS if the compared buffers are identical
//      TPR_FAIL if the compared buffers do not match up
//
//------------------------------------------------------------------------------
DWORD CheckBytesRead(LPBYTE lpbyReadBuf, LPBYTE lpbyCheckBuf, UINT32 uiLen)
{
    UINT32 i;
    DWORD dwRet = TPR_PASS;

    for (i = 0; i < uiLen; i++)
    {
        g_pKato->Log(I2C_ZONE_INFO, TEXT("I2CTest.cpp:CheckBytesRead(%s): Byte #%x read: %x, Correct Byte: %x\r\n"), 
					I2CbusNr==1?CAM_I2C_PORT:USB_I2C_PORT,i, lpbyReadBuf[i], lpbyCheckBuf[i]);
        if (lpbyReadBuf[i] != lpbyCheckBuf[i])
        {
            g_pKato->Log(I2C_ZONE_ERROR, TEXT("I2CTest.cpp:CheckBytesRead(%s): Read wrong byte!\r\n"),
						I2CbusNr==1?CAM_I2C_PORT:USB_I2C_PORT);
            dwRet = TPR_FAIL;
        }
    }

    return dwRet;
}

//------------------------------------------------------------------------------
//
// Function: CheckWordsRead
//
// This function compares a buffer of words read from the I2C
// to a buffer containing the expected result values, and returns
// an error if the values do not match up.  This method also
// outputs the words read and words expected.
//
// Parameters:
//      lpwReadBuf
//           [in] pointer to buffer containing words read.
//
//      lpwCheckBuf
//           [in] pointer to buffer containing expected result.
//
//      uiLen
//           [in] number of words to compare.
//
// Returns:
//      TPR_PASS if the compared buffers are identical
//      TPR_FAIL if the compared buffers do not match up
//
//------------------------------------------------------------------------------
DWORD CheckWordsRead(LPWORD lpwReadBuf, LPWORD lpwCheckBuf, UINT32 uiLen)
{
    UINT32 i;
    DWORD dwRet = TPR_PASS;

    for (i = 0; i < uiLen; i++)
    {
        g_pKato->Log(I2C_ZONE_INFO, TEXT("I2CTest.cpp:CheckWordsRead(%s): Word #%x read: %x, Correct Word: %x\r\n"), 
					I2CbusNr==1?CAM_I2C_PORT:USB_I2C_PORT,i, lpwReadBuf[i], lpwCheckBuf[i]);

        RETAILMSG(TRUE, (TEXT("I2CTest.cpp:CheckWordsRead(%s): Word #%x read: %x, Correct Word: %x\r\n"), I2CbusNr==1?CAM_I2C_PORT:USB_I2C_PORT,i, lpwReadBuf[i], lpwCheckBuf[i]));
    if (lpwReadBuf[i] != lpwCheckBuf[i])
        {
            g_pKato->Log(I2C_ZONE_ERROR, TEXT("I2CTest.cpp:CheckWordsRead(%s): Read wrong word!\r\n"),I2CbusNr==1?CAM_I2C_PORT:USB_I2C_PORT);
        RETAILMSG(TRUE, (TEXT("I2CTest.cpp:CheckWordsRead(%s): Read wrong word!\r\n"),I2CbusNr==1?CAM_I2C_PORT:USB_I2C_PORT));
            dwRet = TPR_FAIL;
        }
    }

    return dwRet;
}

//------------------------------------------------------------------------------
//
// Function: I2CResetTest
//
// This function tests the I2C software reset.
//
// Parameters:
//      uiMsg
//           [in] Ignored.
//
//      tpParam
//           [in] Ignored.
//
//      lpFTE
//           [in] Ignored.
//
// Returns:
//      Specifies if the test passed (TPR_PASS), failed (TPR_FAIL), or was
//      skipped (TPR_SKIP).
//
//------------------------------------------------------------------------------
TESTPROCAPI I2CResetTest(UINT uMsg, TPPARAM tpParam, LPFUNCTION_TABLE_ENTRY lpFTE)
{
    // The shell doesn't necessarily want us to execute the test. Make sure
    // first.
    if(uMsg != TPM_EXECUTE)
    {
        return TPR_NOT_HANDLED;
    }

    g_pKato->Log(LOG_COMMENT, TEXT("I2CTest.cpp:I2CResetTest(%s) +\r\n"),I2CbusNr==1?CAM_I2C_PORT:USB_I2C_PORT);

    // Issue the IOCTL to reset I2C
    if (!I2C_MACRO_RESET(hI2C))
    {
        //g_pKato->Log(I2C_ZONE_ERROR, TEXT("I2CTest.cpp:I2CResetTest(): I2C_IOCTL_RESET failed!\r\n"));
        return TPR_FAIL;
    }

    g_pKato->Log(LOG_COMMENT, TEXT("I2CTest.cpp:I2CResetTest(%s) -\r\n"),I2CbusNr==1?CAM_I2C_PORT:USB_I2C_PORT);

    return TPR_PASS;
}

//------------------------------------------------------------------------------
//
// Function: I2CReadWriteTest
//
// This function attempts to perform a series of reads and writes
// on registers in the USB onboard transceiver via the I2C Interface.
//
// Parameters:
//      uiMsg
//           [in] Ignored.
//
//      tpParam
//           [in] Ignored.
//
//      lpFTE
//           [in] Ignored.
//
// Returns:
//      Specifies if the test passed (TPR_PASS), failed (TPR_FAIL), or was
//      skipped (TPR_SKIP).
//
//------------------------------------------------------------------------------
TESTPROCAPI I2CReadWriteTest(UINT uMsg, TPPARAM tpParam, LPFUNCTION_TABLE_ENTRY lpFTE)
{
    BYTE byCorrectResults[3] = {0x2c, 0x4, 0x2};
    BYTE byReadResults[3];
    INT iResult;
    DWORD dwResult;

    // The shell doesn't necessarily want us to execute the test. Make sure
    // first.
    if(uMsg != TPM_EXECUTE)
    {
        return TPR_NOT_HANDLED;
    }

    BYTE bySlaveAddr = 0x2D; // USB slave address

    g_pKato->Log(LOG_COMMENT, TEXT("I2CTest.cpp:I2CReadWriteTest(%s) +\r\n"),I2CbusNr==1?CAM_I2C_PORT:USB_I2C_PORT);

    // Write 0x20 to USB register 0x6
    I2CWriteOneByte(hI2C, bySlaveAddr, 0x06, 0x20, &iResult);

    // Write 0x4 to USB register 0x4
    I2CWriteOneByte(hI2C, bySlaveAddr, 0x04, 0x04, &iResult);

    // Write 0x4 to USB register 0x13
    I2CWriteOneByte(hI2C, bySlaveAddr, 0x13, 0x04, &iResult);

    // Write 0x2 to USB register 0x12
    I2CWriteOneByte(hI2C, bySlaveAddr, 0x12, 0x02, &iResult);

    // Now read back results

    // Read from USB register 0x6
    byReadResults[0] = I2CReadOneByte(hI2C, bySlaveAddr, 0x06, &iResult);
    // If an error occurs, test failed!
    if (iResult != I2C_NO_ERROR)
    {
        g_pKato->Log(I2C_ZONE_ERROR, TEXT("I2CTest:I2CReadWriteTest(%s) Reading Register Failed! Register=0x06 Err=0x%x \r\n"), I2CbusNr==1?CAM_I2C_PORT:USB_I2C_PORT,iResult);
        return TPR_FAIL;
    }

    // Read from USB register 0x4
    byReadResults[1] = I2CReadOneByte(hI2C, bySlaveAddr, 0x04, &iResult);
    // If an error occurs, test failed!
    if (iResult != I2C_NO_ERROR)
    {
        g_pKato->Log(I2C_ZONE_ERROR, TEXT("I2CTest:I2CReadWriteTest(%s) Reading Register Failed! Register=0x04 Err=0x%x \r\n"), I2CbusNr==1?CAM_I2C_PORT:USB_I2C_PORT,iResult);
        return TPR_FAIL;
    }

    // Read from USB register 0x13
    byReadResults[2] = I2CReadOneByte(hI2C, bySlaveAddr, 0x13, &iResult);
    // If an error occurs, test failed!
    if (iResult != I2C_NO_ERROR)
    {
        g_pKato->Log(I2C_ZONE_ERROR, TEXT("I2CTest:I2CReadWriteTest(%s) Reading Register Failed! Register=0x13 Err=0x%x \r\n"), I2CbusNr==1?CAM_I2C_PORT:USB_I2C_PORT,iResult);
        return TPR_FAIL;
    }

    g_pKato->Log(I2C_ZONE_INFO, TEXT("I2CTest.cpp:I2CReadWriteTest(%s) Comparing Register Values! \r\n"),I2CbusNr==1?CAM_I2C_PORT:USB_I2C_PORT);

    // Compare the results
    dwResult = CheckBytesRead(byReadResults, byCorrectResults, sizeof(byReadResults));

    g_pKato->Log(LOG_COMMENT, TEXT("I2CTest.cpp:I2CReadWriteTest(%s) -\r\n"),I2CbusNr==1?CAM_I2C_PORT:USB_I2C_PORT);

    return dwResult;
}

////////////////////////////////////////////////////////////////////////////////

/*********************************************************************
 END OF FILE
*********************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -