[英]Variable sized i2c reads Raspberry
我正在嘗試通過 i2c 將 A71CH 與 raspberry PI 3 連接起來,設備需要重復啟動,並且當設備發送的第一個字節發出讀取請求時,始終是整個消息的長度。 當我嘗試讀取時,我想讀取第一個字節,而不是讀取固定大小的消息,然后在接收到第一個字節指示的一定數量的字節后向從站發送 NACK 信號。 我曾經遵循代碼但無法獲得我預期的結果,因為它只讀取一個字節而不是發送 NACK 信號,如下所示。
struct i2c_rdwr_ioctl_data packets;
struct i2c_msg messages[2];
int r = 0;
int i = 0;
if (bus != I2C_BUS_0) // change if bus 0 is not the correct bus
{
printf("axI2CWriteRead on wrong bus %x (addr %x)\n", bus, addr);
}
messages[0].addr = axSmDevice_addr;
messages[0].flags = 0;
messages[0].len = txLen;
messages[0].buf = pTx;
// NOTE:
// By setting the 'I2C_M_RECV_LEN' bit in 'messages[1].flags' one ensures
// the I2C Block Read feature is used.
messages[1].addr = axSmDevice_addr;
messages[1].flags = I2C_M_RD | I2C_M_RECV_LEN|I2C_M_IGNORE_NAK;
messages[1].len = 256;
messages[1].buf = pRx;
messages[1].buf[0] = 1;
// NOTE:
// By passing the two message structures via the packets structure as
// a parameter to the ioctl call one ensures a Repeated Start is triggered.
packets.msgs = messages;
packets.nmsgs = 2;
// Send the request to the kernel and get the result back
r = ioctl(axSmDevice, I2C_RDWR, &packets);
有什么方法可以讓我進行可變大小的 i2c 讀取嗎? 我該怎么做才能讓它發揮作用? 謝謝你看。
Raspbery 不支持 SMBUS 塊讀取,解決此問題的唯一方法是在 GPIO 引腳上進行 bitbanging。 正如@Ian Abbott 上面提到的,我設法通過檢查接收到的消息的第一個字節並隨后更新讀取長度來修改 bbI2CZip 函數以滿足我的需要。
我對 rpi3 有類似的問題。 我想從從設備上的寄存器中准確讀取 32 字節的數據,但i2c_smbus_read_block_data()
返回 -71 和 errno 71 EPROTO
。
解決方案是使用i2c_smbus_read_i2c_block_data()
而不是i2c_smbus_read_block_data()
。
/* Until kernel 2.6.22, the length is hardcoded to 32 bytes. If you
ask for less than 32 bytes, your code will only work with kernels
2.6.23 and later. */
extern __s32 i2c_smbus_read_i2c_block_data(int file, __u8 command, __u8 length,
__u8 *values);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.