详解I2CTransfer

The 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.


BOOL I2CTransfer(

HANDLE hDev,

PI2C_TRANSFER_BLOCK pI2CTransferBlock);


Parameters:


hDev I2C device handle retrieved from CreateFile()

pI2CTransferBlock

pI2CPackets [in] Pointer to an array of packets to be transferred sequentially

iNumPackets [in] Number of packets pointed to by pI2CPackets (the number of packets to be

transferred)

Return Values Returns TRUE or FALSE, if the result is TRUE, the operation is successful.

I have defined it as so: 

[DllImport("i2csdk.DLL")]

public static extern bool I2CTransfer(IntPtr manejador, I2C_TRANSFER_BLOCK miBloque);


And the trnsfer function definitions are as follow:

I2C_PACKET

This structure contains the information needed to write or read data using an I2C port.

typedef struct {

BYTE byAddr;

BYTE byRW;

PBYTE pbyBuf;

WORD wLen;

LPINT lpiResult;

} I2C_PACKET, *PI2C_PACKET;


Members

byAddr 7-bit slave address that specifies the target I2C device to or from which data is read

or written

byRW Determines whether the packet is a read or a write packet. Set to I2C_RW_READ

for reading and I2C_RW_WRITE for writing. Set to I2C_POLLING_MODE to

force polling mode for transfer.

pbyBuf Pointer to a buffer of bytes. For a read operation, this is the buffer into which data

is read. For a write operation, this buffer contains the data to write to the target

device.

wLen If the operation is a read, wLen specifies the number of bytes to read into pbyBuf.

If the operation is a write, wLen specifies the number of bytes to write from

pbyBuf.

lpiResult Pointer to an int that contains the return code from the transfer operation

I2C_TRANSFER_BLOCK


This structure contains an array of packets to be transferred using an I2C port.


typedef struct {

I2C_PACKET *pI2CPackets;

INT32 iNumPackets;

} I2C_TRANSFER_BLOCK, *PI2C_TRANSFER_BLOCK;


Members

pI2CPackets Pointer to an array of I2C_PACKET objects

iNumPackets Number of I2C_PACKET objects pointed to by pI2CPackets

### i2ctransfer 命令用法 `i2ctransfer` 是 Linux 下用于通过 I2C 总线与设备交互的一个工具命令。它允许用户发送和接收基于 I2C 协议的数据帧,通常被用来测试或调试 I2C 设备。 #### 基本语法 以下是 `i2ctransfer` 的基本语法结构: ```bash i2ctransfer [-f] [-y bus_number] format_string ... ``` - `-f`: 强制模式,忽略错误继续执行。 - `-y bus_number`: 指定使用的 I2C 总线编号。 - `format_string`: 定义传输的具体格式字符串,描述读写操作的细节。 #### 格式字符串详解 `format_string` 是由一系列字符组成的字符串,定义了具体的 I2C 数据交换方式: - **r**: 表示从指定寄存器地址开始连续读取字节。 - **w**: 表示向指定寄存器地址写入数据。 - 数字: 如果紧跟在 r 或 w 后面,则表示读/写的字节数量。 - 方括号中的十六进制数 `[value]`: 表示具体要写入的数值。 ##### 示例解析 1. 写入单个字节到设备寄存器: ```bash i2ctransfer -y 1 w1 [0x5A] ``` 这条命令会向 I2C 总线 1 上的目标设备写入一个字节 `0x5A`[^2]。 2. 从设备寄存器中读取两个字节: ```bash i2ctransfer -y 1 r2 ``` 此命令将从目标设备读取两个字节的数据,并将其显示在终端上。 3. 组合写入和读取操作: ```bash i2ctransfer -y 1 w1 [0x10] r4 ``` 首先向目标设备写入一个字节 `0x10`,随后从中读取四个字节的数据。 #### 返回值解释 成功时返回状态码为 0;如果发生错误则返回非零值。常见的错误原因可能包括但不限于:设备未响应、总线冲突或其他硬件异常。 --- ### i2ctransfer API 使用说明 除了作为独立命令行工具之外,在某些情况下开发者也可能希望直接调用底层库函数来实现更复杂的控制逻辑。此时可以考虑使用 C/C++ 编程语言配合 libi2c 库完成类似功能开发。 下面是一个简单的例子展示如何利用该方法构建自定义应用程序接口(API): ```c #include <stdio.h> #include <stdint.h> #include <sys/ioctl.h> #include <linux/i2c-dev.h> #include <fcntl.h> #include <unistd.h> int main() { const char *filename = "/dev/i2c-1"; // 替换为你实际使用的I2C适配器路径 int file; if ((file = open(filename, O_RDWR)) < 0) { perror("Failed to open the i2c bus"); return 1; } uint8_t addr = 0x4E; // 更改为你的I2C设备地址 if (ioctl(file, I2C_SLAVE, addr) < 0) { perror("Failed to acquire bus access and/or talk to slave."); return 1; } __u8 reg = 0x07; // 寄存器地址 __s32 res; /* 发送指令 */ if (write(file, &reg, 1) != 1){ perror("Write failed.\n"); return 1; } /* 接收反馈 */ char buf[4]; if(read(file,buf,sizeof(buf))!=sizeof(buf)){ perror("Read failed\n"); return 1; }else{ printf("Data received:%X%X%X%X \n",buf[0],buf[1],buf[2],buf[3]); } close(file); return 0; } ``` 此代码片段展示了打开特定 I2C 设备文件节点、设置目标从机地址以及进行基础读写操作的过程[^1]。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值