AnsweredAssumed Answered

Multiple I2C reads with no restart

Question asked by Kas Lewis on Dec 12, 2014
Latest reply on Dec 26, 2014 by Kas Lewis

With help from the Freescale team I was able to get a single register read working perfectly, my code is below. I was wondering what would need to be changed/added to make it a multiregister read. Think of the MPL3115A pressure registers, looking to read 3 or 4 registers without needing to start a new read cycle for each register. I have been able to get two registers but no stop bit at the end. and help would be much appreciated.

 

Kas

 

Sorry for the lack of formatting, but when I try use the C code insert it messes up the code completely

/***********************SINGLE REGISTER READ*****/

int main(void)

{

  long int delay;

  unsigned char i;

  unsigned char result;

  char string[] = "My name is Bob C. Marley \r\n\r\n\0";

 

  Clk_Init();

  UART_Init();

 

  SIM_SCGC |= SIM_SCGC_I2C_MASK;

  SIM_PINSEL |= SIM_PINSEL_I2C0PS_MASK;

  I2C0_F = 0x11;

  I2C0_C1 |= I2C_C1_MST_MASK | I2C_C1_TX_MASK | I2C_C1_IICEN_MASK;

 

/**/ //Start

  I2C0_C1 |= I2C_C1_MST_MASK | I2C_C1_TX_MASK;

 

/**/ //Write device to read from

  I2C0_D = 0x96;//0xC0;

  while ((I2C0_S & I2C_S_IICIF_MASK) == 0){

  //Wait for data to transmit

  }

  I2C0_S |= I2C_S_IICIF_MASK;

 

/**/ //Write register to read from

  I2C0_D = 0x0B;//0x0C;

  while ((I2C0_S & I2C_S_IICIF_MASK) == 0){

  //Wait for data to transmit

  }

  I2C0_S |= I2C_S_IICIF_MASK;

 

/**/ //Restart

  I2C0_C1 |= I2C_C1_RSTA_MASK;

 

/**/ //Read from device

  I2C0_D = 0x97;//0xC1;

  while ((I2C0_S & I2C_S_IICIF_MASK) == 0){

  //Wait for data to transmit

  }

  I2C0_S |= I2C_S_IICIF_MASK;

 

/**/ //RX mode

    I2C0_C1 &= ~I2C_C1_TX_MASK;

/**/ //Turn off its ACK

    I2C0_C1 |= I2C_C1_TXAK_MASK;

//    for (i = 0; i < 5000; i++){}

//   

    result = I2C0_D;

  while ((I2C0_S & I2C_S_IICIF_MASK) == 0){

  //Wait for data to transmit

  }

  I2C0_S |= I2C_S_IICIF_MASK;

 

  I2C0_C1 &= ~I2C_C1_MST_MASK;

    result = I2C0_D;

  for(i = 0; i < 40; i++){

  asm("nop");

  }

  }

 

 

/********Multiple Register Read with no stop bit*************************/

int main(void)

{

  long int delay;

  unsigned char i;

  unsigned char result;

  char string[] = "My name is Bob C. Marley \r\n\r\n\0";

 

  Clk_Init();

  UART_Init();

 

  SIM_SCGC |= SIM_SCGC_I2C_MASK;

  SIM_PINSEL |= SIM_PINSEL_I2C0PS_MASK;

  I2C0_F = 0x11;

  I2C0_C1 |= I2C_C1_MST_MASK | I2C_C1_TX_MASK | I2C_C1_IICEN_MASK;

 

/**/ //Start

  I2C0_C1 |= I2C_C1_MST_MASK | I2C_C1_TX_MASK;

 

/**/ //Write device to read from

  I2C0_D = 0x96;//0xC0;

  while ((I2C0_S & I2C_S_IICIF_MASK) == 0){

  //Wait for data to transmit

  }

  I2C0_S |= I2C_S_IICIF_MASK;

 

/**/ //Write register to read from

  I2C0_D = 0x01;//0x0C;

  while ((I2C0_S & I2C_S_IICIF_MASK) == 0){

  //Wait for data to transmit

  }

  I2C0_S |= I2C_S_IICIF_MASK;

 

/**/ //Restart

  I2C0_C1 |= I2C_C1_RSTA_MASK;

 

/**/ //Read from device

  I2C0_D = 0x97;//0xC1;

  while ((I2C0_S & I2C_S_IICIF_MASK) == 0){

  //Wait for data to transmit

  }

  I2C0_S |= I2C_S_IICIF_MASK;

/**/ //RX mode

    I2C0_C1 &= ~I2C_C1_TX_MASK;

 

    I2C0_C1 &= ~I2C_C1_TXAK_MASK;

 

    result = I2C0_D;

  while ((I2C0_S & I2C_S_IICIF_MASK) == 0){

  //Wait for data to transmit

  }

/**/ //Turn off its ACK

  I2C0_C1 |= I2C_C1_TXAK_MASK;I2C0_C1 |= I2C_C1_TXAK_MASK;

  result = I2C0_D;

  while ((I2C0_S & I2C_S_IICIF_MASK) == 0){

  //Wait for data to transmit

  }

  I2C0_S |= I2C_S_IICIF_MASK;

  I2C0_C1 &= ~I2C_C1_MST_MASK;

    result = I2C0_D;

  for(i = 0; i < 40; i++){

  asm("nop");

  }

  return 0;

}

Outcomes