📄 i2csdk.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) 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: i2csdk.cpp
//
// The implementation of I2C driver SDK.
//
//-----------------------------------------------------------------------------
#pragma warning(push)
#pragma warning(disable: 4115 4201 4204 4214)
#include <windows.h>
#include "ceddk.h"
#pragma warning(pop)
#include "socarm_macros.h"
#include "i2cbus.h"
//-----------------------------------------------------------------------------
// External Functions
//-----------------------------------------------------------------------------
// External Variables
//-----------------------------------------------------------------------------
// Defines
#define I2C_FUNCTION_ENTRY() \
DEBUGMSG(ZONE_FUNCTION, (TEXT("++%s\r\n"), __WFUNCTION__))
#define I2C_FUNCTION_EXIT() \
DEBUGMSG(ZONE_FUNCTION, (TEXT("--%s\r\n"), __WFUNCTION__))
#ifdef DEBUG
// Debug zone bit positions
#define ZONEID_INIT 0
#define ZONEID_INFO 12
#define ZONEID_FUNCTION 13
#define ZONEID_WARN 14
#define ZONEID_ERROR 15
// Debug zone masks
#define ZONEMASK_INIT (1<<ZONEID_INIT)
#define ZONEMASK_INFO (1<<ZONEID_INFO)
#define ZONEMASK_FUNCTION (1<<ZONEID_FUNCTION)
#define ZONEMASK_WARN (1<<ZONEID_WARN)
#define ZONEMASK_ERROR (1<<ZONEID_ERROR)
// Debug zone args to DEBUGMSG
#define ZONE_INIT DEBUGZONE(ZONEID_INIT)
#define ZONE_INFO DEBUGZONE(ZONEID_INFO)
#define ZONE_FUNCTION DEBUGZONE(ZONEID_FUNCTION)
#define ZONE_WARN DEBUGZONE(ZONEID_WARN)
#define ZONE_ERROR DEBUGZONE(ZONEID_ERROR)
#endif
//-----------------------------------------------------------------------------
// Types
//-----------------------------------------------------------------------------
// Global Variables
#ifdef DEBUG
extern DBGPARAM dpCurSettings =
{
_T("I2C"),
{
_T("Init"), _T(""), _T(""), _T(""),
_T(""), _T(""), _T(""), _T(""),
_T(""),_T(""),_T(""),_T(""),
_T("Info"),_T("Function"),_T("Warnings"),_T("Errors")
},
ZONEMASK_ERROR | ZONEMASK_WARN | ZONEMASK_INFO
};
#endif
//-----------------------------------------------------------------------------
// Local Variables
//-----------------------------------------------------------------------------
// Local Functions
//------------------------------------------------------------------------------
//
// Function: I2COpenHandle
//
// This method creates a handle to the I2C stream driver.
//
// Parameters:
// None
//
// Returns:
// Handle to I2C driver, which is set in this method.
// Returns INVALID_HANDLE_VALUE if failure.
//
//------------------------------------------------------------------------------
HANDLE I2COpenHandle(LPCWSTR lpDevName)
{
HANDLE hI2C;
I2C_FUNCTION_ENTRY();
hI2C = CreateFile(lpDevName, // name of device
GENERIC_READ|GENERIC_WRITE, // desired access
FILE_SHARE_READ|FILE_SHARE_WRITE, // sharing mode
NULL, // security attributes (ignored)
OPEN_EXISTING, // creation disposition
FILE_FLAG_RANDOM_ACCESS, // flags/attributes
NULL); // template file (ignored)
// if we failed to get handle to I2C
if (hI2C == INVALID_HANDLE_VALUE)
{
DEBUGMSG(ZONE_ERROR,
(TEXT("%s: CreateFile I2C failed!\r\n"), __WFUNCTION__));
return hI2C;
}
I2C_FUNCTION_EXIT();
return hI2C;
}
//------------------------------------------------------------------------------
//
// Function: I2CCloseHandle
//
// This method closes a handle to the I2C stream driver.
//
// Parameters:
// hI2C
// [in/out] Handle to close.
//
// Returns:
// TRUE if success.
// FALSE if failure.
//
//------------------------------------------------------------------------------
BOOL I2CCloseHandle(HANDLE hI2C)
{
I2C_FUNCTION_ENTRY();
// if we don't have handle to I2C bus driver
if (hI2C != NULL)
{
if (!CloseHandle(hI2C))
{
DEBUGMSG(ZONE_ERROR,
(TEXT("%s: CloseHandle failed!\r\n"), __WFUNCTION__));
return FALSE;
}
}
I2C_FUNCTION_EXIT();
return TRUE;
}
//-----------------------------------------------------------------------------
//
// Function: I2CSetSlaveMode
//
// This function sets the I2C device in slave mode.
//
// Parameters:
// hI2C
// [in] The I2C device handle retrieved from I2COpenHandle().
//
// Returns:
// Return TRUE or FALSE. If the result is TRUE, the operation is
// successful.
//
//-----------------------------------------------------------------------------
BOOL I2CSetSlaveMode(HANDLE hI2C)
{
I2C_FUNCTION_ENTRY();
if (!DeviceIoControl(hI2C, // file handle to the driver
I2C_IOCTL_SET_SLAVE_MODE, // I/O control code
NULL, // in buffer
0, // in buffer size
NULL, // out buffer
0, // out buffer size
NULL, // pointer to number of bytes returned
NULL)) // ignored (=NULL)
{
DEBUGMSG(ZONE_ERROR,
(TEXT("%s: I2C_IOCTL_SET_SLAVE_MODE failed!\r\n"), __WFUNCTION__));
return FALSE;
}
I2C_FUNCTION_EXIT();
return TRUE;
}
//-----------------------------------------------------------------------------
//
// Function: I2CSetMasterMode
//
// This function sets the I2C device in master mode.
//
// Parameters:
// hI2C
// [in] The I2C device handle retrieved from I2COpenHandle().
//
// Returns:
// Return TRUE or FALSE. If the result is TRUE, the operation is
// successful.
//
//-----------------------------------------------------------------------------
BOOL I2CSetMasterMode(HANDLE hI2C)
{
I2C_FUNCTION_ENTRY();
if (!DeviceIoControl(hI2C, // file handle to the driver
I2C_IOCTL_SET_MASTER_MODE, // I/O control code
NULL, // in buffer
0, // in buffer size
NULL, // out buffer
0, // out buffer size
NULL, // pointer to number of bytes returned
NULL)) // ignored (=NULL)
{
DEBUGMSG(ZONE_ERROR,
(TEXT("%s: I2C_IOCTL_SET_MASTER_MODE failed!\r\n"), __WFUNCTION__));
return FALSE;
}
I2C_FUNCTION_EXIT();
return TRUE;
}
//-----------------------------------------------------------------------------
//
// Function: I2CIsMaster
//
// This function determines whether the I2C is currently in Master mode.
//
// Parameters:
// hI2C
// [in] The I2C device handle retrieved from I2COpenHandle().
//
// pbIsMaster
// [out] TRUE if the I2C device is in Master mode.
//
// Returns:
// Return TRUE or FALSE. If the result is TRUE, the operation is
// successful.
//
//-----------------------------------------------------------------------------
BOOL I2CIsMaster(HANDLE hI2C, PBOOL pbIsMaster)
{
I2C_FUNCTION_ENTRY();
if (!DeviceIoControl(hI2C, // file handle to the driver
I2C_IOCTL_IS_MASTER, // I/O control code
NULL, // in buffer
0, // in buffer size
pbIsMaster, // out buffer
sizeof(BOOL), // out buffer size
NULL, // pointer to number of bytes returned
NULL)) // ignored (=NULL)
{
DEBUGMSG(ZONE_ERROR,
(TEXT("%s: I2C_IOCTL_IS_MASTER failed!\r\n"), __WFUNCTION__));
return FALSE;
}
I2C_FUNCTION_EXIT();
return TRUE;
}
//-----------------------------------------------------------------------------
//
// Function: I2CIsSlave
//
// This function determines whether the I2C is currently in Slave mode.
//
// Parameters:
// hI2C
// [in] The I2C device handle retrieved from I2COpenHandle().
//
// pbIsSlave
// [out] The pointer to BOOL variable that indicates
// if the I2C device is in Slave mode.
//
// Returns:
// Return TRUE or FALSE. If the result is TRUE, the operation is
// successful.
//
//-----------------------------------------------------------------------------
BOOL I2CIsSlave(HANDLE hI2C, PBOOL pbIsSlave)
{
I2C_FUNCTION_ENTRY();
if (!DeviceIoControl(hI2C, // file handle to the driver
I2C_IOCTL_IS_SLAVE, // I/O control code
NULL, // in buffer
0, // in buffer size
pbIsSlave, // out buffer
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -