I2C on twr-k70f120n

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

I2C on twr-k70f120n

959 Views
ankurchauhan
Contributor I

Hii,

I'm using twr-k70f120(master) development kit and trying to read the data from sensor(slave).

I configured the I2C0 in receive mode and write the I2C0_D register with the slave address in read mode as shown below:

I2C0_C1 = I2C_C1_IICEN_MASK;

I2C0_C1 &= (~I2C_C1_TX_MASK);

  I2C0_C1 |= I2C_C1_MST_MASK;

   I2C0_D = 0x93;

  

  while ( !(I2C0_S & (1<<1)) );

  I2C0_S = I2C0_S | (1 << 1);

  

  readData = I2C0_D;

  

  while ( !(I2C0_S & (1<<1)) );

  I2C0_S = I2C0_S | (1 << 1);

  

   I2C0_C1 = I2C0_C1 | (1 << 2);

  

   I2C0_D = 0x93;

   while ( !(I2C0_S & (1<<1)) );

   I2C0_S = I2C0_S | (1 << 1);

  

   I2C0_C1 |= I2C_C1_TXAK_MASK;

  

   readData_1 = I2C0_D;

  

  while ( !(I2C0_S & (1<<1)) );

  I2C0_S = I2C0_S | (1 << 1);

  readData_2 = I2C0_D;

  I2C0_C1 &= ~I2C_C1_MST_MASK;

m doing repetitive reading bcoz single reading giving output zero and most of the time its getting stuck in while condition..

0 Kudos
8 Replies

640 Views
Jorge_Gonzalez
NXP Employee
NXP Employee

Hello Ankur Chauhan:

In the Slave Address cycle the TX bit must always be '1'. This is specified in the Reference Manual:

pastedImage_0.png

Maybe you are confused by the "Read/Write" mode of the I2C protocol specification. Although both things are related when developing the code, the TX bit is inherent to the I2C module and indicates the transfer direction, while the "Read[1]/Write[0]" mode is specified by the least significant bit in the first byte of the I2C frame (address byte).

I hope it is clear.


Regards!,
Jorge Gonzalez

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos

640 Views
ankurchauhan
Contributor I

Thanks jorge:

I tried your suggestion and after editing

I2C0_C1 |= I2C_C1_TX_MASK
I2C0_C1 |= I2C_C1_MST_MASK;
  I2C0_D = 0x93;
 
while ( !(I2C0_S & (1<<1)) );
I2C0_S = I2C0_S | (1 << 1);
readData = I2C0_D;
while ( !(I2C0_S & (1<<1)) );
I2C0_S = I2C0_S | (1 << 1);
   I2C0_C1 = I2C0_C1 | (1 << 2);
//I2C0_C1 &= (~I2C_C1_TX_MASK);
 
  I2C0_D = 0x93;
  while ( !(I2C0_S & (1<<1)) );
  I2C0_S = I2C0_S | (1 << 1);
 
  I2C0_C1 &= (~I2C_C1_TX_MASK);
  I2C0_C1 |= I2C_C1_TXAK_MASK;
 
  readData_1 = I2C0_D;
I2C0_C1 &= ~I2C_C1_MST_MASK;

but this code is getting stuck in second while loop ,but if i removed second while  its running only once  b'coz i'm checking bus status in the beginning its showing it's busy so executing only once and readData and readData_1 values is the slave address. Is it compulsory that Master has to write to I2C0_D register with write bit once and then do repetitive start and read the slave??

Regards

Ankur Chauahn

0 Kudos

640 Views
Jorge_Gonzalez
NXP Employee
NXP Employee

Hello Ankur:

Please check the datasheet of the sensor to see the I2C control frames it is expecting. Using or not repeated start depends on the sensor. i.e. if you need to change from a write command (R/W = 0) to a read command (R/W =1) or the other way around without releasing the bus, then yes, repeated start is required. In that case the address byte is sent twice within the transfer, but with different R/W bit.

Now, about your code, it is normal that in readData and readData_1 you get the slave address. You are missing a dummy read from the I2Cx_D register after sending the slave address. The dummy read is required to trigger the SCL clock signal and receive a byte, but the valid data will be the next after transfer is complete. For example the first part in your code should be something like this:

I2C0_C1 |= I2C_C1_TX_MASK;

I2C0_C1 |= I2C_C1_MST_MASK;

I2C0_D = 0x93;

while ( !(I2C0_S & (1<<1)) );

I2C0_S = I2C0_S | (1 << 1);

(void)I2C0_D;     // Dummy read

while ( !(I2C0_S & (1<<1)) );

I2C0_S = I2C0_S | (1 << 1);

readData = I2C0_D;

Please also give a check to the figure Typical I2C Interrupt Routine in the K70 Reference Manual. The flow chart is for an interrupt routine, but it is exactly the same, just with the difference that you are polling the flag instead of using interrupts.

Regards!

Jorge Gonzalez

0 Kudos

640 Views
ankurchauhan
Contributor I

Thanks jorge :

I change the code and now its working also :

I2C0_C1 |= I2C_C1_TX_MASK;

  I2C0_C1 |= I2C_C1_MST_MASK;

   I2C0_D = 0x92;

  

  while ( !(I2C0_S & (1<<1)) );

  I2C0_S = I2C0_S | (1 << 1);

  

  I2C0_D = 0x49;

  

  while ( !(I2C0_S & (1<<1)) );

  I2C0_S = I2C0_S | (1 << 1);

  

     I2C0_C1 = I2C0_C1 | (1 << 2);

     I2C0_D = 0x93;

   while ( !(I2C0_S & (1<<1)) );

   I2C0_S = I2C0_S | (1 << 1);

  

   I2C0_C1 &= (~I2C_C1_TX_MASK);

   I2C0_C1 &= ~I2C_C1_TXAK_MASK; // again read without restart

  

   readData_1 = I2C0_D;

   while ( !(I2C0_S & (1<<1)) );

  I2C0_S = I2C0_S | (1 << 1);

  

  I2C0_C1 |= I2C_C1_TXAK_MASK;

  readData_3 = I2C0_D; //ch

   while ( !(I2C0_S & (1<<1)) );

  I2C0_S = I2C0_S | (1 << 1);

  

  

  I2C0_C1 &= ~I2C_C1_MST_MASK;

  readData_2 = I2C0_D;

in this m doing multiple reads without restart but i'm able to do this only two times if m trying to do it more times values are not correct.

I have to read the data 6 times is it possible without restart??

Regards

Ankur

0 Kudos

640 Views
Jorge_Gonzalez
NXP Employee
NXP Employee

Hello Ankur:

Your use of TXAK seems incorrect. This bit is used for the master to respond with ACK or NAK after the next received byte from the slave. Usually you should set it just before receiving the last byte. Please give it a check.

And yes, you can read as many bytes as required, not only 2, but the reading process depends on your sensor.

Regards!

Jorge Gonzalez

0 Kudos

640 Views
ankurchauhan
Contributor I

Thanks Jorge :

But when i'm trying to read the same register multiple times without sending  Stop bit ,values are not correct. Can u send some sample code of multiple reading without sending stop bit for K70.

Regards

Ankur

0 Kudos

640 Views
Jorge_Gonzalez
NXP Employee
NXP Employee

Hello Ankur:

Multiple reading without STOP is done by just reading from the data register (I2Cx_D) multiple times, waiting for the IICIF and clearing it between each read.

I guess you are confused with your slave's documentation. Please give it a check or tell me the part number so I can give you some hint.

Regards!

Jorge Gonzalez

0 Kudos

640 Views
ankurchauhan
Contributor I

Hello Jorge:

I have one doubt like I have to use  LCD using ebi interface ,is MQX reqireded for this or barebored is enough??.

Regards

Ankur

0 Kudos