AnsweredAssumed Answered

I2C read function issues

Question asked by Kas Lewis on Nov 24, 2014
Latest reply on Oct 20, 2015 by vignesh balaji

Is there anyone at Freescale available to help with the I2C operation for the KEA-64. I am having issues reading back the response from an I2C device, There seems to be an issue having the KEA-64 provide the clock signal for the sensor device to replay with the requested data. This is the main issue preventing my proposed demo project from going live, any help would be very much appreciated.

 

Thanks

Kas

 

P.s. I have attached my code for a read from address 0x4B register 0x0B, there is just no clock for the devices response from that register.

 

 

#include "derivative.h" /* include peripheral declarations */

 

#define MWSR      0x00  /* Master write  */

#define MRSW      0x01  /* Master read */

 

#define i2c_Start()            I2C0_C1     |= 0x10;\

                                          I2C0_C1     |= I2C_C1_MST_MASK

 

#define i2c_write_byte(data)   I2C0_D = data

 

#define i2c_Wait()             while((I2C0_S & I2C_S_IICIF_MASK)==0) {} \

                                 I2C0_S |= I2C_S_IICIF_MASK;

 

#define i2c_Stop()             I2C0_C1  &= ~I2C_C1_MST_MASK;\

                               I2C0_C1  &= ~I2C_C1_TX_MASK

 

unsigned char MasterTransmission;

/*******************************************************************/

/*!

* Pause Routine

*/

void Pause(void){

    int n;

    for(n=1;n<50;n++) {

      asm("nop");

    }

}

/***********************************************************************************************

*

* @brief    Uart_SendChar - Send a single byte on Uart1

* @param    byte to send

* @return   none

*

************************************************************************************************/ 

void Uart_SendChar(uint8_t send)

{

  while((UART2_S1&UART_S1_TDRE_MASK)==0);

  (void)UART2_S1;

  UART2_D=send;

}

 

void writeRegister(unsigned char slaveID, unsigned char registerAddress, unsigned char data)

{

  MasterTransmission = MWSR;

  slaveID = slaveID << 1;

  slaveID |= MasterTransmission;

  i2c_Start();

  i2c_write_byte(slaveID);

  i2c_Wait();

  I2C0_D = registerAddress;

  i2c_Wait();

  I2C0_D = data;

  i2c_Wait();

  i2c_Stop();

}

 

void Clk_Init()

{

  ICS_C1|=ICS_C1_IRCLKEN_MASK; /* Enable the internal reference clock*/

  ICS_C3= 0x50; /* Reference clock frequency = 39.0625 KHz*/

  while(!(ICS_S & ICS_S_LOCK_MASK));   /* Wait for PLL lock, now running at 40 MHz (1024 * 39.0625Khz) */

    ICS_C2|=ICS_C2_BDIV(1)  ; /*BDIV=2, Bus clock = 20 MHz*/

  ICS_S |= ICS_S_LOCK_MASK ; /* Clear Loss of lock sticky bit */

}

 

void UART_Init()

{

  SIM_SCGC |=  SIM_SCGC_UART2_MASK;

  UART2_BDL= 128;

  UART2_C1 = 0;

  UART2_C2 |= UART_C2_TE_MASK;

  UART2_C2 |= UART_C2_RE_MASK;

  UART2_C2 |= UART_C2_RIE_MASK;

}

 

void i2cInit(){

  //I2C setup

  SIM_SCGC |= SIM_SCGC_I2C_MASK;

  SIM_PINSEL |= SIM_PINSEL_I2C0PS_MASK;

    I2C0_F = 0x00;

    I2C0_C1 = I2C_C1_IICEN_MASK | I2C_C1_MST_MASK | I2C_C1_IICIE_MASK;

}

 

int main(void)

{

  long int delay;

  unsigned char i;

  unsigned char replay;

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

 

  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;

 

  I2C0_D = 0x96;

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

  //Wait for data to transmit

  }

  I2C0_S |= I2C_S_IICIF_MASK;

  I2C0_D = 0x0B;

 

 

  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;

  I2C0_D = 0x97;

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

  //Wait for data to transmit

  }

  I2C0_S |= I2C_S_IICIF_MASK;

 

    I2C0_C1 &= !I2C_C1_TX_MASK;

 

  I2C0_C1 |= I2C_C1_TXAK_MASK;

 

  replay = I2C0_D;

 

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

  //Wait for data to transmit

  }

  while(1) {  

    for (i = 0; string[i] != '\0'; i++){

    Uart_SendChar(string[i]);

    }

  

  

    for(delay = 0; delay < 99999; delay++);

  }

 

return 0;

}

Outcomes