📄 i2c_io.cpp
字号:
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your
// install media.
//
//------------------------------------------------------------------------------
//
// Copyright (C) 2004-2008, 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
//
//------------------------------------------------------------------------------
//
// File: i2c_io.cpp
//
// This module provides a stream interface for the I2C bus
// driver. Client drivers can use the stream interface to
// configure and exchange data with the I2C peripheral.
//
//------------------------------------------------------------------------------
#pragma warning(push)
#pragma warning(disable: 4115 4201 4204 4214)
#include <windows.h>
#include <Devload.h>
#include <windev.h>
#pragma warning(pop)
#include "socarm_types.h"
#include "mx27_i2c.h"
#include "i2cbus.h"
#include "i2cclass.h"
#pragma warning(push)
#pragma warning(disable: 4512)
#include "marshal.hpp" //helper classes to marshal/alloc embedded/async buffer
#pragma warning(pop)
//------------------------------------------------------------------------------
// External Functions
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// External Variables
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// Defines
//------------------------------------------------------------------------------
#define REG_DEVINDEX_VAL_NAME L"Index"
#define I2C_DEVINDEX_MAX_VAL 3
#define I2C_DEVINDEX_MIN_VAL 1
#define I2C_DEVINDEX_DEFAULT_VAL 1
#ifdef DEBUG
DBGPARAM dpCurSettings = {
TEXT("i2c"), {
TEXT("Init"),TEXT("Deinit"),TEXT("Open"),TEXT("Close"),
TEXT("IOCtl"),TEXT("Thread"),TEXT(""),TEXT(""),
TEXT(""),TEXT(""),TEXT(""),TEXT(""),
TEXT(""),TEXT("Function"),TEXT("Warning"),TEXT("Error") },
(ZONEMASK_WARN | ZONEMASK_ERROR)
};
#endif // DEBUG
//------------------------------------------------------------------------------
// Types
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// Global Variables
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// Local Variables
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// Local Functions
//------------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//
// Function: I2C_Init
//
// The Device Manager calls this function as a result of a call to the
// ActivateDevice() function.
//
// Parameters:
// pContext
// [in] Pointer to a string containing the registry path to the
// active key for the stream interface driver.
//
// Returns:
// Returns a handle to the device context created if successful. Returns
// zero if not successful.
//
//-----------------------------------------------------------------------------
DWORD I2C_Init(LPCTSTR pContext)
{
UINT32 error;
HKEY hKey;
DWORD dwIndex, dwSize;
DEBUGMSG (ZONE_INIT|ZONE_FUNCTION, (TEXT("I2C_Init +\r\n")));
// try to open active device registry key for this context
hKey = OpenDeviceKey(pContext);
if (hKey == NULL)
{
DEBUGMSG(ZONE_ERROR, (TEXT("I2C_Init: OpenDeviceKey failed!!!\r\n")));
return 0;
}
// try to load I2C index from registry data
dwSize = sizeof(DWORD);
error = RegQueryValueEx(
hKey, // handle to currently open key
REG_DEVINDEX_VAL_NAME, // string containing value to query
NULL, // reserved, set to NULL
NULL, // type not required, set to NULL
(LPBYTE)(&dwIndex), // pointer to buffer receiving value
&dwSize); // pointer to buffer size
// close handle to open key
RegCloseKey(hKey);
// check for errors during RegQueryValueEx
if (error != ERROR_SUCCESS)
{
DEBUGMSG(ZONE_ERROR, (TEXT("I2C_Init: RegQueryValueEx failed!!!\r\n")));
return 0;
}
// Construct the I2C Module Class
I2CClass* pI2C = new I2CClass(dwIndex);
// Managed to create the class?
if (pI2C == NULL)
{
return NULL;
}
PREFAST_SUPPRESS(28197, "I2C class own this slave buffer handle, allocate outside the class so that customize this buffer size");
I2CSLAVEBUF *pSlave = new I2CSLAVEBUF;
if (pSlave == NULL)
{
delete pI2C;
return NULL;
}
pSlave->iBufSize=1;
pSlave->byBuf[0]=(BYTE)0;
pI2C->SetSlaveBuf(pSlave);
// If class construction not successful?
if (pI2C->IsLastActionOK() != TRUE)
{
// Dispose the instance
DEBUGMSG (ZONE_INIT|ZONE_ERROR, (TEXT("I2C_Init: I2C Class Failed! Err=%d\r\n"), pI2C->GetLastResult()));
delete pI2C;
return NULL;
}
DEBUGMSG (ZONE_INIT|ZONE_FUNCTION, (TEXT("I2C_Init - hDev=0x%x\r\n"), pI2C));
// Otherwise return the created instance
return (DWORD) pI2C;
}
//-----------------------------------------------------------------------------
//
// Function: I2C_Deinit
//
// This function uninitializes a device.
//
// Parameters:
// hDeviceContext
// [in] Handle to the device context.
//
// Returns:
// TRUE indicates success. FALSE indicates failure.
//
//-----------------------------------------------------------------------------
BOOL I2C_Deinit(DWORD hDeviceContext)
{
I2CClass * pI2C = (I2CClass*) hDeviceContext;
DEBUGMSG (ZONE_DEINIT|ZONE_FUNCTION, (TEXT("I2C_Deinit +DeviceContext=0x%x\r\n"),hDeviceContext));
if (pI2C != NULL)
{
delete pI2C;
}
DEBUGMSG (ZONE_DEINIT|ZONE_FUNCTION, (TEXT("I2C_Deinit -\r\n")));
return TRUE;
}
//-----------------------------------------------------------------------------
//
// Function: I2C_Open
//
// This function opens a device for reading, writing, or both.
//
// Parameters:
// hDeviceContext
// [in] Handle to the device context. The XXX_Init function creates
// and returns this handle.
// AccessCode
// [in] Access code for the device. The access is a combination of
// read and write access from CreateFile.
// ShareMode
// [in] File share mode of the device. The share mode is a
// combination of read and write access sharing from CreateFile.
//
// Returns:
// This function returns a handle that identifies the open context of
// the device to the calling application.
//
//-----------------------------------------------------------------------------
DWORD I2C_Open(DWORD hDeviceContext, DWORD AccessCode, DWORD ShareMode)
{
DEBUGMSG (ZONE_OPEN|ZONE_FUNCTION, (TEXT("I2C_Open +hDeviceContext=0x%x\r\n"),hDeviceContext));
// Remove-W4: Warning C4100 workaround
UNREFERENCED_PARAMETER(AccessCode);
UNREFERENCED_PARAMETER(ShareMode);
DEBUGMSG (ZONE_OPEN|ZONE_FUNCTION, (TEXT("I2C_Open -\r\n")));
// Open is meaningless!
return hDeviceContext;
}
//-----------------------------------------------------------------------------
//
// Function: I2C_Close
//
// This function opens a device for reading, writing, or both.
//
// Parameters:
// hOpenContext
// [in] Handle returned by the XXX_Open function, used to identify
// the open context of the device.
//
// Returns:
// TRUE indicates success. FALSE indicates failure.
//
//-----------------------------------------------------------------------------
BOOL I2C_Close(DWORD hOpenContext)
{
DEBUGMSG (ZONE_CLOSE|ZONE_FUNCTION, (TEXT("I2C_Close +\r\n")));
// Remove-W4: Warning C4100 workaround
UNREFERENCED_PARAMETER(hOpenContext);
DEBUGMSG (ZONE_CLOSE|ZONE_FUNCTION, (TEXT("I2C_Close -\r\n")));
// Close is meaningless!
return TRUE;
}
//-----------------------------------------------------------------------------
//
// Function: I2C_PowerDown
//
// This function suspends power to the device. It is useful only with
// devices that can power down under software control.
//
// Parameters:
// hDeviceContext
// [in] Handle to the device context.
//
// Returns:
// None.
//
//-----------------------------------------------------------------------------
void I2C_PowerDown(DWORD hDeviceContext)
{
// Remove-W4: Warning C4100 workaround
UNREFERENCED_PARAMETER(hDeviceContext);
// Not implemented!
}
//-----------------------------------------------------------------------------
//
// Function: I2C_PowerUp
//
// This function restores power to a device.
//
// Parameters:
// hDeviceContext
// [in] Handle to the device context.
//
// Returns:
// None.
//
//-----------------------------------------------------------------------------
void I2C_PowerUp(void)
{
// Not implemented!
}
//-----------------------------------------------------------------------------
//
// 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)
{
// Remove-W4: Warning C4100 workaround
UNREFERENCED_PARAMETER(hOpenContext);
UNREFERENCED_PARAMETER(pBuffer);
UNREFERENCED_PARAMETER(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)
{
// Remove-W4: Warning C4100 workaround
UNREFERENCED_PARAMETER(Handle);
UNREFERENCED_PARAMETER(pBuffer);
UNREFERENCED_PARAMETER(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)
{
// Remove-W4: Warning C4100 workaround
UNREFERENCED_PARAMETER(hOpenContext);
UNREFERENCED_PARAMETER(Amount);
UNREFERENCED_PARAMETER(Type);
// Seeking is meaningless!
return (DWORD)-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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -