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

📄 i2c_io.cpp

📁 freescale i.mx31 BSP CE5.0全部源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//
// Function: I2C_Read
//
// This function reads data from the device identified by the open 
//      context.
//
// Parameters:
//      hOpenContext 
//          [in] Handle to the open context of the device. The XXX_Open 
//                function creates and returns this identifier.
//      pBuffer 
//         [out] Pointer to the buffer that stores the data read from the 
//                 device. This buffer should be at least Count bytes long. 
//      Count 
//          [in] Number of bytes to read from the device into pBuffer. 
//
// Returns:  
//      Returns zero to indicate end-of-file. Returns -1 to indicate an 
//      error. Returns the number of bytes read to indicate success.
//
//-----------------------------------------------------------------------------
DWORD I2C_Read(DWORD hOpenContext, LPVOID pBuffer, DWORD Count)
{
    // Nothing to read
    return 0;
}


//-----------------------------------------------------------------------------
//
// Function: I2C_Write
//
// This function writes data to the device.
//
// Parameters:
//      hOpenContext 
//          [in] Handle to the open context of the device. The XXX_Open 
//                function creates and returns this identifier.
//      pBuffer 
//         [out] Pointer to the buffer that contains the data to write. 
//      Count 
//          [in] Number of bytes to read from the device into pBuffer. 
//
// Returns:  
//      The number of bytes written indicates success. A value of -1 indicates 
//      failure.
//
//-----------------------------------------------------------------------------
DWORD I2C_Write(DWORD Handle, LPCVOID pBuffer, DWORD dwNumBytes)
{
    // Nothing to write
    return 0;
}


//-----------------------------------------------------------------------------
//
// Function: I2C_Seek
//
// This function moves the data pointer in the device.
//
// Parameters:
//      hOpenContext 
//          [in] Handle to the open context of the device. The XXX_Open 
//                function creates and returns this identifier.
//      Amount 
//         [in] Number of bytes to move the data pointer in the device. 
//               A positive value moves the data pointer toward the end of the 
//               file, and a negative value moves it toward the beginning.
//      Type 
//         [in] Starting point for the data pointer. 
//
// Returns:  
//      The new data pointer for the device indicates success. A value of -1 
//      indicates failure.
//
//-----------------------------------------------------------------------------
DWORD I2C_Seek(DWORD hOpenContext, long Amount, WORD Type)
{
    // Seeking is meaningless!
    return -1;
}


//-----------------------------------------------------------------------------
//
// Function: I2C_IOControl
//
// This function sends a command to a device.
//
// Parameters:
//      hOpenContext 
//          [in] Handle to the open context of the device. The XXX_Open 
//                function creates and returns this identifier.
//      dwCode 
//          [in] I/O control operation to perform. These codes are 
//                device-specific and are usually exposed to developers through 
//                a header file.
//      pBufIn 
//          [in] Pointer to the buffer containing data to transfer to the 
//                device. 
//      dwLenIn 
//         [in] Number of bytes of data in the buffer specified for pBufIn.
//
//      pBufOut 
//          [out] Pointer to the buffer used to transfer the output data 
//                  from the device.
//      dwLenOut 
//          [in] Maximum number of bytes in the buffer specified by pBufOut.
//
//      pdwActualOut 
//          [out] Pointer to the DWORD buffer that this function uses to 
//                  return the actual number of bytes received from the device.
//
// Returns:  
//      The new data pointer for the device indicates success. A value of -1 
//      indicates failure.
//
//-----------------------------------------------------------------------------
BOOL I2C_IOControl(DWORD hOpenContext, DWORD dwCode, PBYTE pBufIn, 
                   DWORD dwLenIn, PBYTE pBufOut, DWORD dwLenOut,
                   PDWORD pdwActualOut)
{
    BOOL bRet = FALSE;
    
    // hOpenContext is a pointer to I2CClass instance!
    I2CClass* pI2C = (I2CClass*) hOpenContext;

    DEBUGMSG (ZONE_IOCTL|ZONE_FUNCTION, (TEXT("I2C_IOControl: +hOpenContext=0x%x\r\n"),hOpenContext));

    if (pI2C != NULL)
    {
        switch (dwCode)
        {
            case I2C_IOCTL_SET_SLAVE_MODE:
            {
                DEBUGMSG (ZONE_IOCTL|ZONE_FUNCTION, (TEXT("I2C_IOControl:SET_SLAVE_MODE +\r\n")));
                pI2C->SetMode(I2C_SLAVE_MODE);
                bRet = TRUE;
                DEBUGMSG (ZONE_IOCTL|ZONE_FUNCTION, (TEXT("I2C_IOControl:SET_SLAVE_MODE -\r\n")));
                break;
            }
            case I2C_IOCTL_SET_MASTER_MODE:
            {
                DEBUGMSG (ZONE_IOCTL|ZONE_FUNCTION, (TEXT("I2C_IOControl:SET_MASTER_MODE +\r\n")));
                pI2C->SetMode(I2C_MASTER_MODE);
                bRet = TRUE;
                DEBUGMSG (ZONE_IOCTL|ZONE_FUNCTION, (TEXT("I2C_IOControl:SET_MASTER_MODE +\r\n")));
                break;
            }
            case I2C_IOCTL_IS_MASTER:
            {
                if (dwLenOut != sizeof(BOOL))
                    return FALSE;

                DEBUGMSG (ZONE_IOCTL|ZONE_FUNCTION, (TEXT("I2C_IOControl:IS_MASTER +\r\n")));
                PBOOL pbIsMaster = (PBOOL) MapCallerPtr(pBufOut, sizeof(BOOL));
                if (pI2C->GetMode() == I2C_MASTER_MODE)
                    *pbIsMaster = TRUE;
                else
                    *pbIsMaster = FALSE;
                bRet = TRUE;
                DEBUGMSG (ZONE_IOCTL|ZONE_FUNCTION, (TEXT("I2C_IOControl:IS_MASTER - Val=0x%x\r\n"), *pbIsMaster));
                break;
            }
            case I2C_IOCTL_IS_SLAVE:
            {
                if (dwLenOut != sizeof(BOOL))
                    return FALSE;

                DEBUGMSG (ZONE_IOCTL|ZONE_FUNCTION, (TEXT("I2C_IOControl:IS_SLAVE +\r\n")));
                PBOOL pbIsSlave = (PBOOL) MapCallerPtr(pBufOut, sizeof(BOOL));
                if (pI2C->GetMode() == I2C_SLAVE_MODE)
                    *pbIsSlave = TRUE;
                else
                    *pbIsSlave = FALSE;
                bRet = TRUE;
                DEBUGMSG (ZONE_IOCTL|ZONE_FUNCTION, (TEXT("I2C_IOControl:IS_SLAVE - Val=0x%x\r\n"), *pbIsSlave));
                break;
            }
            case I2C_IOCTL_GET_CLOCK_RATE:
            {
                if (dwLenOut != sizeof(WORD))
                    return FALSE;


                DEBUGMSG (ZONE_IOCTL|ZONE_FUNCTION, (TEXT("I2C_IOControl:GET_CLOCK_RATE +\r\n")));
                PWORD pwClkRate = (PWORD) MapCallerPtr(pBufOut, sizeof(WORD));
                *pwClkRate = pI2C->GetClockRateDivider();
                bRet = TRUE;
                DEBUGMSG (ZONE_IOCTL|ZONE_FUNCTION, (TEXT("I2C_IOControl:GET_CLOCK_RATE - Val=0x%x\r\n"), *pwClkRate));
                break;
            }
            case I2C_IOCTL_SET_CLOCK_RATE:
            {
                if (dwLenIn != sizeof(WORD))
                    return FALSE;

                PWORD pwClkRate = (PWORD) MapCallerPtr(pBufIn, sizeof(WORD));
                DEBUGMSG (ZONE_IOCTL|ZONE_FUNCTION, (TEXT("I2C_IOControl:SET_CLOCK_RATE + ValIn=0x%x\r\n"), *pwClkRate));
                pI2C->SetClockRateDivider(*pwClkRate);
                bRet = TRUE;
                break;
            }
            case I2C_IOCTL_SET_FREQUENCY:
            {
                if (dwLenIn != sizeof(DWORD))
                    return FALSE;

                PDWORD pdwFrequency = (PDWORD) MapCallerPtr(pBufIn, sizeof(DWORD));
                DEBUGMSG (ZONE_IOCTL|ZONE_FUNCTION, (TEXT("I2C_IOControl:SET_FREQUENCY + ValIn=0x%x\r\n"), *pdwFrequency));     
                WORD wClkRate = BSPCalculateClkRateDiv(*pdwFrequency);
                pI2C->SetClockRateDivider(wClkRate);
                bRet = TRUE;
                break;
            }
            case I2C_IOCTL_SET_SELF_ADDR:
            {
                if (dwLenIn != sizeof(BYTE))
                    return FALSE;

                PBYTE pbySelfAddr = (PBYTE) MapCallerPtr(pBufIn, sizeof(BYTE));
                DEBUGMSG (ZONE_IOCTL|ZONE_FUNCTION, (TEXT("I2C_IOControl:SET_SELF_ADDR + ValIn=0x%x\r\n"), *pbySelfAddr));
                pI2C->SetSelfAddress(*pbySelfAddr);
                bRet = TRUE;
                break;
            }
            case I2C_IOCTL_GET_SELF_ADDR:
            {
                if (dwLenOut != sizeof(BYTE))
                    return FALSE;

                DEBUGMSG (ZONE_IOCTL|ZONE_FUNCTION, (TEXT("I2C_IOControl:GET_SELF_ADDR +\r\n")));
                PBYTE pbySelfAddr = (PBYTE) MapCallerPtr(pBufOut, sizeof(BYTE));
                *pbySelfAddr = pI2C->GetSelfAddress();
                bRet = TRUE;
                DEBUGMSG (ZONE_IOCTL|ZONE_FUNCTION, (TEXT("I2C_IOControl:GET_SELF_ADDR - Val=0x%x\r\n"), *pbySelfAddr));
                break;
            }
            case I2C_IOCTL_TRANSFER:
            {
                I2C_TRANSFER_BLOCK *pXferBlock = (I2C_TRANSFER_BLOCK *) pBufIn;
                I2C_PACKET *pPackets = (I2C_PACKET *) MapCallerPtr(pXferBlock->pI2CPackets, sizeof(I2C_PACKET) * pXferBlock->iNumPackets);

                // Map pointers for each packet in the array
                for (int i = 0; i < pXferBlock->iNumPackets; i++)
                {
                    pPackets[i].pbyBuf = (PBYTE) MapCallerPtr(pPackets[i].pbyBuf, pPackets[i].wLen);
                }

                bRet = pI2C->ProcessPackets(pPackets, pXferBlock->iNumPackets);
                DEBUGMSG (ZONE_IOCTL|ZONE_FUNCTION, (TEXT("I2C_IOControl:I2C_IOCTL_TRANSFER -\r\n")));
                break;
            }
            case I2C_IOCTL_RESET:
            {
                DEBUGMSG (ZONE_IOCTL|ZONE_FUNCTION, (TEXT("I2C_IOControl:RESET +\r\n")));

                pI2C->Reset();

                bRet = TRUE;
                DEBUGMSG (ZONE_IOCTL|ZONE_FUNCTION, (TEXT("I2C_IOControl:RESET -\r\n")));
                break;
            }
            default:
            {
                bRet = FALSE;
                break;
            }
        }
                
    }

    DEBUGMSG (ZONE_IOCTL|ZONE_FUNCTION, (TEXT("I2C_IOControl -\r\n")));

    return bRet;
}

BOOL WINAPI I2C_DllEntry(HANDLE hInstDll, DWORD dwReason, LPVOID lpvReserved)
{
    switch (dwReason) {
        case DLL_PROCESS_ATTACH:
            DEBUGREGISTER((HINSTANCE) hInstDll);
            DEBUGMSG(ZONE_FUNCTION, (TEXT("I2C_DllEntry: DLL_PROCESS_ATTACH lpvReserved(0x%x)\r\n"),lpvReserved));
            DisableThreadLibraryCalls((HMODULE) hInstDll);
            break;
         
        case DLL_PROCESS_DETACH:
            DEBUGMSG(ZONE_FUNCTION, (TEXT("I2C_DllEntry: DLL_PROCESS_DETACH lpvReserved(0x%x)\r\n"),lpvReserved));
            break;
    }
    // return TRUE for success
    return TRUE;
}

⌨️ 快捷键说明

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