I have been working with I2C protocol to create some code for the RA8875 (see for I2C from page 67) using a Tiva board (TM4C123, I2C chapter from page 997), and I was able to paint in the display or writing text, but I am struggling when I want to read a value from a register.
For what I could read about I2C, I understand that the process should be as it follows:
- Send a write command, specifying the adress of the register
- Send a read command, so the board reads the data from the register
The code I use for I2C read is the following:
unsigned char lcd_read(unsigned char data,short I2C)
{
unsigned char response;
switch(I2C)
{
case I2C0:
{
while(I2C0_MCS_R&I2C_MCS_BUSBSY);
I2C0_MSA_R |=0x0E; //Specify the slave address of the touch screen and that the next operation is a write command
I2C0_MDR_R=data; // Register address
I2C0_MCS_R=0x03; // (START, RUN)
if(I2C0_MCS_R&I2C_MCS_ERROR) //Check the ERROR bit in the I2CMCS register to confirm the transmit was acknowledged.
{
}
I2C0_MSA_R |=0x0F; //Specify the slave address of the touch screen and that the next operation is a read
I2C0_MCS_R=0x07; // (STOP, START, RUN)
while(I2C0_MCS_R&I2C_MCS_BUSBSY); //Wait until the transmiI2Con completes by polling the I2CMCS register's BUSBstart_Y bit until it has been cleared.
response = I2C0_MDR_R&0xFF;
if(I2C0_MCS_R&I2C_MCS_ERROR) //Check the ERROR bit in the I2CMCS register to confirm the transmit was acknowledged.
{
}
break;
}
}
return response;
}
Another thing that happened to me is that what I can see in the registers (I use Code Composer Studio for coding and debugging) do not correspond to the data obtained using a protocol analyzer (i.e. content of register I2C_MDR). Probably this could be a separate question but, what can be the reason of it?
Edit: The following images show how data is transmitted. The read data should be 0x81 instead of 0x00:
I2C write to register:
I2C read from register using the code above:
Thanks for your help.