I am programming the MAX77651 in I2c using the FT4222 device. I want to be able to write in a register.
I am able to read registers so i know i dont have any hardware problems. To read in a register i proceed like this: i first send the register i want to read then i just read what is inside.
To write i am trying to send the register where i want to write then i send the data i want to put in the register. But when i check by reading after this i dont get the expected result.
Here is the part of the programm where i try to write and read:
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);
That code should show me 1F in the register 0x40 but it's not the case, here is what i get:
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
My Question is simple, what should i do and in which order, to write properly in a register ?
Ps: I did'nt put my whole code because it is mainly configuration about FT4222 and includes, nothing relevant to solve my problem.
See the "Writing to a Single Register" section on page 76 of the MAX77651 datasheet.
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.
You are attempting to write to an internal register with two consecutive calls to FT4222_I2CMaster_Write()
. You are expecting that the first write selects the register address and the second write loads the data. But that's not how it works. Each call to FT4222_I2CMaster_Write()
sends a START and STOP so your consecutive calls will result in a STOP/START between the register address and the data. Instead you should make one call to FT4222_I2CMaster_Write()
and provide both the register address and the data. Maybe something like this.
uint8_t buf[2];
buf[0] = register_addr;
buf[1] = data;
FT4222_I2CMaster_Write(ftHandle, slaveAddr, buf, 2, &bytesWritten);
By the way, your reads may not be correct either. See the "Reading from a Single Register" section of the MAX77651 datasheet. You may need to implement a register read with calls to FT4222_I2CMaster_WriteEx()
followed by FT4222_I2CMaster_ReadEx()
as shown in the "I²C combined message support" example on page 32 of the User Guide For LibFT4222.