📄 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, Motorola Inc. All Rights Reserved
//
//------------------------------------------------------------------------------
//
// Copyright (C) 2004, Freescale Semiconductor, Inc. All Rights Reserved
// THIS SOURCE CODE IS CONFIDENTIAL AND PROPRIETARY AND MAY NOT
// BE USED OR DISTRIBUTED WITHOUT THE WRITTEN PERMISSION OF
// FREESCALE SEMICONDUCTOR, INC.
//
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//
// 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.
//
//------------------------------------------------------------------------------
#include <windows.h>
#include <Devload.h>
#include <windev.h>
#include "mxarm11.h"
#include "i2cbus.h"
#include "i2cclass.h"
//------------------------------------------------------------------------------
// 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
//------------------------------------------------------------------------------
// I2C_IOControl Critical Section
CRITICAL_SECTION gcsI2CIOControlLock;
//------------------------------------------------------------------------------
// 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("SPI_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;
}
// 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));
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")));
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)
{
// 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!
}
//-----------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -