📄 i2csdk.cpp
字号:
sizeof(BOOL), // out buffer size
NULL, // pointer to number of bytes returned
NULL)) // ignored (=NULL)
{
DEBUGMSG(ZONE_ERROR,
(TEXT("%s: I2C_IOCTL_IS_SLAVE failed!\r\n"), __WFUNCTION__));
return FALSE;
}
I2C_FUNCTION_EXIT();
return TRUE;
}
//-----------------------------------------------------------------------------
//
// Function: I2CGetClockRate
//
// This function will retrieve the clock rate divisor. Note that the value
// is not the absolute peripheral clock frequency. The value retrieved should
// be compared against the I2C specifications to obtain the true frequency.
//
// Parameters:
// hI2C
// [in] The I2C device handle retrieved from I2COpenHandle().
//
// pwClkRate
// [out] The pointer of WORD variable that retrieves divisor index.
// Refer to I2C specification to obtain the true clock frequency.
//
// Returns:
// Return TRUE or FALSE. If the result is TRUE, the operation is
// successful.
//
//-----------------------------------------------------------------------------
BOOL I2CGetClockRate(HANDLE hI2C, PWORD pwClkRate)
{
I2C_FUNCTION_ENTRY();
if (!DeviceIoControl(hI2C, // file handle to the driver
I2C_IOCTL_GET_CLOCK_RATE, // I/O control code
NULL, // in buffer
0, // in buffer size
pwClkRate, // out buffer
sizeof(WORD), // out buffer size
NULL, // pointer to number of bytes returned
NULL)) // ignored (=NULL)
{
DEBUGMSG(ZONE_ERROR,
(TEXT("%s: I2C_IOCTL_GET_CLOCK_RATE failed!\r\n"), __WFUNCTION__));
return FALSE;
}
I2C_FUNCTION_EXIT();
return TRUE;
}
//-----------------------------------------------------------------------------
//
// Function: I2CSetClockRate
//
// This function will initialize the I2C device with the given clock rate. Note
// that this function does not expect to receive the absolute peripheral clock
// frequency. Rather, it will be expecting the clock rate divisor index stated
// in the I2C specification. If absolute clock frequency must be used, please
// use the function I2CSetFrequency().
//
// Parameters:
// hI2C
// [in] The I2C device handle retrieved from I2COpenHandle().
//
// wClkRate
// [in] Contains the divisor index. Refer to I2C specification to
// obtain the true clock frequency.
//
// Returns:
// Return TRUE or FALSE. If the result is TRUE, the operation is
// successful.
//
//-----------------------------------------------------------------------------
BOOL I2CSetClockRate(HANDLE hI2C, WORD wClkRate)
{
I2C_FUNCTION_ENTRY();
if (!DeviceIoControl(hI2C, // file handle to the driver
I2C_IOCTL_SET_CLOCK_RATE, // I/O control code
&wClkRate, // in buffer
sizeof(WORD), // 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_CLOCK_RATE failed!\r\n"), __WFUNCTION__));
return FALSE;
}
I2C_FUNCTION_EXIT();
return TRUE;
}
//-----------------------------------------------------------------------------
//
// Function: I2CSetFrequency
//
// This function will estimate the nearest clock rate acceptable for
// I2C device and initialize the I2C device to use the estimated clock
// rate divisor. If the estimated clock rate divisor index is required,
// please refer to the macro I2CGetClockRate to determine the estimated index.
//
// Parameters:
// hI2C
// [in] The I2C device handle retrieved from I2COpenHandle().
//
// dwFreq
// [in] The desired frequency.
//
// Returns:
// Return TRUE or FALSE. If the result is TRUE, the operation is
// successful.
//
//-----------------------------------------------------------------------------
BOOL I2CSetFrequency(HANDLE hI2C, DWORD dwFreq)
{
I2C_FUNCTION_ENTRY();
if (!DeviceIoControl(hI2C, // file handle to the driver
I2C_IOCTL_SET_FREQUENCY, // I/O control code
&dwFreq, // in buffer
sizeof(DWORD), // 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_FREQUENCY failed!\r\n"), __WFUNCTION__));
return FALSE;
}
I2C_FUNCTION_EXIT();
return TRUE;
}
//-----------------------------------------------------------------------------
//
// Function: I2CSetSelfAddr
//
// This function will initialize the I2C device with the given address.
// The device will be expected to respond when any master within the
// I2C bus wish to proceed with any transfer.
//
// Note that this function will have no effect if the I2C device is
// in Master mode.
//
// Parameters:
// hI2C
// [in] The I2C device handle retrieved from I2COpenHandle().
//
// bySelfAddr
// [in] The expected I2C device address. The valid range of address
// is [0x00, 0x7F].
//
// Returns:
// Return TRUE or FALSE. If the result is TRUE, the operation is
// successful.
//
//-----------------------------------------------------------------------------
BOOL I2CSetSelfAddr(HANDLE hI2C, BYTE bySelfAddr)
{
I2C_FUNCTION_ENTRY();
if (!DeviceIoControl(hI2C, // file handle to the driver
I2C_IOCTL_SET_SELF_ADDR, // I/O control code
&bySelfAddr, // in buffer
sizeof(BYTE), // 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_SELF_ADDR failed!\r\n"), __WFUNCTION__));
return FALSE;
}
I2C_FUNCTION_EXIT();
return TRUE;
}
//-----------------------------------------------------------------------------
//
// Function: I2CGetSelfAddr
//
// This function will retrieve the address of the I2C device.
//
// Note that this function is only meaningful if it is currently in Slave mode.
//
// Parameters:
// hI2C
// [in] The I2C device handle retrieved from I2COpenHandle().
//
// pbySelfAddr
// [out] The pointer to BYTE variable that retrieves I2C device address.
// The valid range of address is [0x00, 0x7F].
//
// Returns:
// Return TRUE or FALSE. If the result is TRUE, the operation is
// successful.
//
//-----------------------------------------------------------------------------
BOOL I2CGetSelfAddr(HANDLE hI2C, PBYTE pbySelfAddr)
{
I2C_FUNCTION_ENTRY();
if (!DeviceIoControl(hI2C, // file handle to the driver
I2C_IOCTL_GET_SELF_ADDR, // I/O control code
NULL, // in buffer
0, // in buffer size
pbySelfAddr, // out buffer
sizeof(BYTE), // out buffer size
NULL, // pointer to number of bytes returned
NULL)) // ignored (=NULL)
{
DEBUGMSG(ZONE_ERROR,
(TEXT("%s: I2C_IOCTL_GET_SELF_ADRR failed!\r\n"), __WFUNCTION__));
return FALSE;
}
I2C_FUNCTION_EXIT();
return TRUE;
}
//-----------------------------------------------------------------------------
//
// Function: I2CTransfer
//
// This function performs one or more I2C read or write operations.
// pI2CTransferBlock contains a pointer to the first of an array of I2C
// packets to be processed by the I2C. All the required information for the
// I2C operations should be contained in the array elements of pI2CPackets.
//
// Parameters:
// hI2C
// [in] The I2C device handle retrieved from I2COpenHandle().
//
// pI2CTransferBlock
// [in] The pointer of I2C_TRANSFER_BLOCK structure type.
// The fields of I2C_TRANSFER_BLOCK are described below:
// pI2CPackets - Pointer to the I2C Packets to transfer.
// iNumPackets - Number of packets in pI2CPackets array.
//
// Returns:
// Return TRUE or FALSE. If the result is TRUE, the operation is
// successful.
//
//-----------------------------------------------------------------------------
BOOL I2CTransfer(HANDLE hI2C, PI2C_TRANSFER_BLOCK pI2CTransferBlock)
{
I2C_FUNCTION_ENTRY();
if (!DeviceIoControl(hI2C, // file handle to the driver
I2C_IOCTL_TRANSFER, // I/O control code
pI2CTransferBlock, // in buffer
sizeof(I2C_TRANSFER_BLOCK), // 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_TRANSFER failed!\r\n"), __WFUNCTION__));
return FALSE;
}
I2C_FUNCTION_EXIT();
return TRUE;
}
//-----------------------------------------------------------------------------
//
// Function: I2CReset
//
// This function performs a hardware reset. Note that the I2C driver will still
// maintain all the current information of the device, which includes all the
// initialized addresses.
//
// 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 I2CReset(HANDLE hI2C)
{
I2C_FUNCTION_ENTRY();
if (!DeviceIoControl(hI2C, // file handle to the driver
I2C_IOCTL_RESET, // 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_RESET failed!\r\n"), __WFUNCTION__));
return FALSE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -