I2C Writing protocol:如何写入一个寄存器?

I2C Writing protocol: how to write in a register?

我正在使用 FT4222 设备在 I2c 中对 MAX77651 进行编程。我希望能够写入寄存器。

我能够读取寄存器,所以我知道我没有任何硬件问题。要读取一个寄存器,我是这样进行的:我首先发送我想读取的寄存器,然后我只读取里面的内容。

要写入,我正在尝试将寄存器发送到我想写入的位置,然后我发送我想放入寄存器的数据。但是当我在此之后通过阅读进行检查时,我没有得到预期的结果。

这是我尝试编写和阅读的程序部分:

uint8 resultat=0x11;
uint8 *p_resultat=&resultat;
int chiffre = 8;
uint16 bytesToWrite2 = 1;
uint16 bytesWritten2=1;
uint8 valeur= 0x06;     // Adress of register
uint8 *p_valeur=&valeur;    
uint16         slaveAddr = 0x48;

// ***** reading test of 0x06 : expected value : 0xFF
FT4222_I2CMaster_Write(ftHandle,slaveAddr,p_valeur,
bytesToWrite2,&bytesWritten); 

chiffre = FT4222_I2CMaster_Read(ftHandle,slaveAddr,p_resultat,1, &bytesRead);
printf("The content of the register %02X is : %02X \n " , valeur , resultat);
printf("reading success if : %d = 0 \n " , chiffre);

//********** Writting test to the register 0x40 
***********************************************//

valeur = 0x40;

FT4222_I2CMaster_Write(ftHandle,slaveAddr,p_valeur,
bytesToWrite2,&bytesWritten);

valeur = 0x1F;

FT4222_I2CMaster_Write(ftHandle,slaveAddr,p_valeur,
bytesToWrite2,&bytesWritten);

//*************** Reading of the register 0x40 
*******************************************//

valeur= 0x40;           

FT4222_I2CMaster_Write(ftHandle,slaveAddr,p_valeur,
bytesToWrite2,&bytesWritten); 

chiffre = FT4222_I2CMaster_Read(ftHandle,
slaveAddr,p_resultat,1, &bytesRead);

printf("The content of the register %02X 
is : %02X \n " , valeur , resultat);

printf("reading success if : %d = 0 \n " , chiffre);

该代码应该在寄存器 0x​​40 中显示 1F,但事实并非如此,这是我得到的:

The content of the register 06 is : FF 
reading success if : 0 = 0 
The content of the register 40 is : 00 
reading success if : 0 = 0 

我的问题很简单,我应该怎么做以及按什么顺序正确写入寄存器?

Ps: 我没有放完整的代码,因为它主要是关于 FT4222 的配置,包括,与解决我的问题无关。

请参阅 MAX77651 datasheet 第 76 页的 "Writing to a Single Register" 部分。

The write byte protocol is as follows:

  • The master sends a start command (S).
  • The master sends the 7-bit slave address followed by a write bit (R/W = 0).
  • The addressed slave asserts an acknowledge (A) by pulling SDA low.
  • The master sends an 8-bit register pointer.
  • The slave acknowledges the register pointer.
  • The master sends a data byte.
  • The slave updates with the new data
  • The slave acknowledges or not acknowledges the data byte. The next rising edge on SDA loads the data byte into its target register and the data becomes active.
  • The master sends a stop condition (P) or a repeated start condition (Sr). Issuing a P ensures that the bus input filters are set for 1MHz or slower operation. Issuing an Sr leaves the bus input filters in their current state.

您正试图通过连续两次调用 FT4222_I2CMaster_Write() 来写入内部寄存器。您期望第一次写入选择寄存器地址,第二次写入加载数据。但事实并非如此。每次调用 FT4222_I2CMaster_Write() 都会发送一个 START 和 STOP,因此您的连续调用将在寄存器地址和数据之间产生一个 STOP/START。相反,您应该调用 FT4222_I2CMaster_Write() 并提供寄存器地址和数据。也许是这样的。

uint8_t buf[2];
buf[0] = register_addr;
buf[1] = data;
FT4222_I2CMaster_Write(ftHandle, slaveAddr, buf, 2, &bytesWritten);

顺便说一下,你的解读可能也不正确。请参见 MAX77651 数据表的 "Reading from a Single Register" 部分。您可能需要通过调用 FT4222_I2CMaster_WriteEx() 然后调用 FT4222_I2CMaster_ReadEx() 来实现寄存器读取,如 User Guide For LibFT4222.

第 32 页的 "I²C combined message support" 示例所示