📄 i2cbus.h
字号:
// wClkRate
// [out] 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.
//
//-----------------------------------------------------------------------------
#define I2C_MACRO_GET_CLOCK_RATE(hDev, wClkRate) \
(DeviceIoControl(hDev, I2C_IOCTL_GET_CLOCK_RATE, NULL, 0, (PBYTE) &wClkRate, sizeof(wClkRate), NULL, NULL))
//-----------------------------------------------------------------------------
//
// Macro: I2C_MACRO_SET_CLOCK_RATE
//
// This macro will initialize the I2C device with the given clock rate. Note
// that this macro 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 macro I2C_MACRO_SET_FREQUENCY.
//
// Parameters:
// hDev
// [in] The I2C device handle retrieved from CreateFile().
// 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.
//
//-----------------------------------------------------------------------------
#define I2C_MACRO_SET_CLOCK_RATE(hDev, wClkRate) \
(DeviceIoControl(hDev, I2C_IOCTL_SET_CLOCK_RATE, (PBYTE) &wClkRate, sizeof(wClkRate), NULL, 0, NULL, NULL))
//-----------------------------------------------------------------------------
//
// Macro: I2C_MACRO_SET_FREQUENCY
//
// This macro 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
// I2C_MACRO_GET_CLOCK_RATE to determine the estimated index.
//
// Parameters:
// hDev
// [in] The I2C device handle retrieved from CreateFile().
// dwFreq
// [in] The desired frequency.
//
// Returns:
// Return TRUE or FALSE. If the result is TRUE, the operation is
// successful.
//
//-----------------------------------------------------------------------------
#define I2C_MACRO_SET_FREQUENCY(hDev, dwFreq) \
(DeviceIoControl(hDev, I2C_IOCTL_SET_FREQUENCY, (PBYTE) &dwFreq, sizeof(dwFreq), NULL, 0, NULL, NULL))
//-----------------------------------------------------------------------------
//
// Macro: I2C_MACRO_SET_SELF_ADDR
//
// This macro 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 macro will have no effect if the
// I2C device is in Master mode.
//
// Parameters:
// hDev
// [in] The I2C device handle retrieved from CreateFile().
// 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.
//
//-----------------------------------------------------------------------------
#define I2C_MACRO_SET_SELF_ADDR(hDev, bySelfAddr) \
(DeviceIoControl(hDev, I2C_IOCTL_SET_SELF_ADDR, (PBYTE) &bySelfAddr, sizeof(bySelfAddr), NULL, 0, NULL, NULL))
//-----------------------------------------------------------------------------
//
// Macro: I2C_MACRO_GET_SELF_ADDR
//
// This macro will retrieve the address of the I2C device. Note that this macro
// is only meaningful if it is currently in Slave mode.
//
// Parameters:
// hDev
// [in] The I2C device handle retrieved from CreateFile().
// bySelfAddr
// [out] The current 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.
//
//-----------------------------------------------------------------------------
#define I2C_MACRO_GET_SELF_ADRR(hDev, bySelfAddr) \
(DeviceIoControl(hDev, I2C_IOCTL_GET_SELF_ADRR, NULL, 0, (PBYTE) &bySelfAddr, sizeof(bySelfAddr), NULL, NULL))
//-----------------------------------------------------------------------------
//
// Macro: I2C_MACRO_TRANSFER
//
// This macro performs one or more I2C read or write operations. pPackets 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 pPackets.
//
// Parameters:
// hDev
// [in] The I2C device handle retrieved from CreateFile().
// pI2CTransferBlock
// [in] I2C_TRANSFER_BLOCK structure type.
// The fields of I2C_TRANSFER_BLOCK are described below:
// pI2CPackets[in] Holds the pointer to the I2C Packets
// to transfer.
// iNumPackets[in] Number of packets in pI2CPackets array.
//
// Returns:
// Return TRUE or FALSE. If the result is TRUE, the operation is
// successful.
//
//-----------------------------------------------------------------------------
#define I2C_MACRO_TRANSFER(hDev, pI2CTransferBlock) \
(DeviceIoControl(hDev, I2C_IOCTL_TRANSFER, (PBYTE) pI2CTransferBlock, sizeof(I2C_TRANSFER_BLOCK), NULL, 0, NULL, NULL))
//-----------------------------------------------------------------------------
//
// Macro: I2C_MACRO_RESET
//
// This macro perform 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:
// hDev
// [in] The I2C device handle retrieved from CreateFile().
//
// Returns:
// Return TRUE or FALSE. If the result is TRUE, the operation is
// successful.
//
//-----------------------------------------------------------------------------
#define I2C_MACRO_RESET(hDev) \
(DeviceIoControl(hDev, I2C_IOCTL_RESET, NULL, 0, NULL, 0, NULL, NULL))
//------------------------------------------------------------------------------
// Types
//------------------------------------------------------------------------------
// I2C Packet
typedef struct
{
BYTE byAddr; // I2C slave device address for this I2C operation
BYTE byRW; // Read = I2C_READ or Write = I2C_WRITE
PBYTE pbyBuf; // Message Buffer
WORD wLen; // Message Buffer Length
LPINT lpiResult; // Contains the result of last operation
} I2C_PACKET, *PI2C_PACKET;
// I2C Transfer Block
typedef struct
{
I2C_PACKET *pI2CPackets;
INT32 iNumPackets;
} I2C_TRANSFER_BLOCK, *PI2C_TRANSFER_BLOCK;
//------------------------------------------------------------------------------
// Functions
//------------------------------------------------------------------------------
HANDLE I2COpenHandle(LPCWSTR lpDevName);
BOOL I2CCloseHandle(HANDLE hI2C);
BOOL I2CSetSlaveMode(HANDLE hI2C);
BOOL I2CSetMasterMode(HANDLE hI2C);
BOOL I2CIsMaster(HANDLE hI2C, PBOOL pbIsMaster);
BOOL I2CIsSlave(HANDLE hI2C, PBOOL pbIsSlave);
BOOL I2CGetClockRate(HANDLE hI2C, PWORD pwClkRate);
BOOL I2CSetClockRate(HANDLE hI2C, WORD wClkRate);
BOOL I2CSetFrequency(HANDLE hI2C, DWORD dwFreq);
BOOL I2CSetSelfAddr(HANDLE hI2C, BYTE bySelfAddr);
BOOL I2CGetSelfAddr(HANDLE hI2C, PBYTE pbySelfAddr);
BOOL I2CTransfer(HANDLE hI2C, PI2C_TRANSFER_BLOCK pI2CTransferBlock);
BOOL I2CReset(HANDLE hI2C);
BOOL I2CEnableSlave(HANDLE hI2C);
BOOL I2CDisableSlave(HANDLE hI2C);
BOOL I2CGetSlaveSize(HANDLE hI2C, PDWORD pdwSize);
BOOL I2CSetSlaveSize(HANDLE hI2C, DWORD dwSize);
BOOL I2CGetSlaveText(HANDLE hI2C, PBYTE pbyTextBuf, DWORD dwBufSize, PDWORD pdwTextLen);
BOOL I2CSetSlaveText(HANDLE hI2C, PBYTE pbyTextBuf, DWORD dwTextLen);
#ifdef __cplusplus
}
#endif
#endif // __I2CBUS_H__
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -