AnsweredAssumed Answered

MQX i2C driver on Kinetis K70. Blocked if device not connected

Question asked by arnogir on Sep 23, 2015
Latest reply on Sep 28, 2015 by isaacavila

Hello every body.

 

I'm using an I2C bus to communicate with a peripheral.

 

All work corerclty, but I tested when the peripheral is not connected. For my Application I should detect it and signal a problem on this device.

 

But after test, the write funtion never return:

 

Bellow is my code, I'm calling DV_Touch_i2cWrite:

 

 



uint8_t DV_Touch_iI2cAck(void) 

{

  uint8_t   ReturnValue;

  uint32_t  Param;

  

  ReturnValue = ioctl(DV_Touch_I2cComponent, IO_IOCTL_FLUSH_OUTPUT, &Param);



  if (Param)

  {

    /* NACk received, send STOP */

    ReturnValue = ioctl(DV_Touch_I2cComponent, IO_IOCTL_I2C_STOP, NULL);

  }

  

  return(ReturnValue);

}

  



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

** Function name: DV_Touch_iI2cWrite

** Description: Send a sequuence to Writing to the device

** Parameter: Addr of the starting write register

** Parameter: Buffer: data to write

** Parameter: BufferSize: Number of byte to write 

** Return value: Number of written Byte;

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

uint16_t DV_Touch_iI2cWrite(uint8_t Addr, uint8_t * Buffer, uint16_t BufferSize) 

{

  uint16_t  ReturnValue = 0;

  uint8_t   Error;

  uint32_t  Param;

  

  

  

  /* I2C bus address */

  Param = DV_Touch_EASY_I2C_ADDR;

  Error = ioctl(DV_Touch_I2cComponent, IO_IOCTL_I2C_SET_DESTINATION_ADDRESS, &Param); 

  if (I2C_OK != Error)

  {

    return(0);

  }



  /* Send a START and send I2C bus address */

  fwrite(DV_Touch_Buffer, 1, 0, DV_Touch_I2cComponent);

  

  /* Check ack (device exists) */

  Error = DV_Touch_iI2cAck();

  if (I2C_OK != Error)

  {

    /* Bad Ack or NACK, then stop stransmission */

    return(0);

  }



  /* Send memory address */ 

  DV_Touch_Buffer[0] = Addr;

  do 

  {

    ReturnValue = fwrite(DV_Touch_Buffer, 1, 1, DV_Touch_I2cComponent);

  } while (ReturnValue < 1);



          i2c

  /* Send Data */

  ReturnValue = 0;

  do

  {

    ReturnValue += fwrite(&Buffer[ReturnValue], 1, BufferSize - ReturnValue, DV_Touch_I2cComponent);

  } while (ReturnValue < BufferSize);



  

  ReturnValue = ioctl(DV_Touch_I2cComponent, IO_IOCTL_FLUSH_OUTPUT, &Param);



  if (Param)

  {

    /* NACk received, send STOP */

    ReturnValue = ioctl(DV_Touch_I2cComponent, IO_IOCTL_I2C_STOP, NULL);

  }

  

  /* Wait for completion */

  Error = fflush(DV_Touch_I2cComponent);

  if (I2C_OK != Error)

  {

    ReturnValue = 0;

  }

  /* Stop I2C transfer  */

  Error = ioctl(DV_Touch_I2cComponent, IO_IOCTL_I2C_STOP, NULL);

  if (I2C_OK != Error)

  {

    ReturnValue = 0;

  }


  

  return(ReturnValue);

}

So when device not connected, the write on line 113 return one time with return value set to 0. But the second time, fwrite nev er return, teh task is in pause.

It is strange the Ack check not detect the problem because in my case, Ack is not done by device.

 

Is any body have an Idea of where come from my problem?

Thank

 

Note: MQX 4.1

Outcomes