K60 I2C Multiread

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

K60 I2C Multiread

Jump to solution
1,107 Views
broccolee
Contributor III

I'm using the TWR-K60N512, with PK60DN512Z VMD10 chip to read an IMU: Pololu - MinIMU-9 Gyro, Accelerometer, and Compass (L3G4200D and LSM303DLM Carrier)  via I2C.

I am able to perform single read operation with the example I2C driver for kinetis. I'm trying to take the advantage of the address auto-incrementing feature of the sensor, but I can't seem do a multiread operation. I based my code from these two sources: https://community.nxp.com/thread/337809#465606 , and this very useful application note http://cache.nxp.com/files/sensors/doc/app_note/AN4481.pdf,  though I think it has errors too.

Here's my modification of the example code:

void LSM303DLM_A_MultiReadRegister(unsigned char u8RegisterAddress, int buffer, unsigned char *result)

{

  int i = buffer, j;

  /* Send Slave Address */ //SAD + W

  IIC_StartTransmission(SlaveID_A,MWSR);

  i2c_Wait(); // SAK

  /* Write Register Address */ //SUB

  I2C0_D = u8RegisterAddress;

  i2c_Wait(); // SAK

  /* Do a repeated start */ // SR

  I2C0_C1 |= I2C_C1_RSTA_MASK;

  /* Send Slave Address */ //SAD + R

  I2C0_D = (ACCEL_ADDRESS << 1) | 0x01; //read address

  i2c_Wait(); // SAK

  /* Put in Rx Mode */

  I2C0_C1 &= (~I2C_C1_TX_MASK);

  //I2C0_C1 |= I2C_C1_TXAK_MASK; // Commented this out

// dummy read

  *result = I2C0_D ;

  for (j=0; j<5000; j++){};

  i2c_Wait();

/************ MODIFICATION BEGINS HERE, EVERYTHING ELSE IS SAME AS EXAMPLE CODE *********/

  while(buffer > 2)

  {

   *result = I2C0_D ;

   i2c_Wait();

      result++;

   buffer--;

   Pause();

  }

 

  I2C0_C1 |= I2C_C1_TXAK_MASK;

  *result = I2C0_D ;

  i2c_Wait();

  result++;

  i2c_Stop();

  *(result) = I2C0_D ;

  Pause();

}

It returns random values for the first accelerometer measurement and constants for the remaining measurements. Did I get the sequence wrong somewhere?

Thanks.

0 Kudos
Reply
1 Solution
963 Views
broccolee
Contributor III

Nevermind, it works now. I assumed the I2C_C1_TXAK bit was already cleared so I didn't clear that when switching the module to receiving mode. I cleared it, and it works.

View solution in original post

0 Kudos
Reply
1 Reply
964 Views
broccolee
Contributor III

Nevermind, it works now. I assumed the I2C_C1_TXAK bit was already cleared so I didn't clear that when switching the module to receiving mode. I cleared it, and it works.

0 Kudos
Reply