vl6180x_port_i2c.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * vl6180x_port_i2c.c
  3. *
  4. * Created on: Oct 22, 2014
  5. * Author: Teresa Tao
  6. */
  7. #include "vl6180x_i2c.h"
  8. #include <linux/i2c.h>
  9. #if 0
  10. #define I2C_M_WR 0x00
  11. static struct i2c_client *pclient=NULL;
  12. void i2c_setclient(struct i2c_client *client)
  13. {
  14. pclient = client;
  15. }
  16. struct i2c_client* i2c_getclient()
  17. {
  18. return pclient;
  19. }
  20. /** int VL6180x_I2CWrite(VL6180xDev_t dev, void *buff, uint8_t len);
  21. * @brief Write data buffer to VL6180x device via i2c
  22. * @param dev The device to write to
  23. * @param buff The data buffer
  24. * @param len The length of the transaction in byte
  25. * @return 0 on success
  26. */
  27. int VL6180x_I2CWrite(VL6180xDev_t dev, uint8_t *buff, uint8_t len)
  28. {
  29. struct i2c_msg msg[1];
  30. int err=0;
  31. msg[0].addr = pclient->addr;
  32. msg[0].flags = I2C_M_WR;
  33. msg[0].buf= buff;
  34. msg[0].len=len;
  35. err = i2c_transfer(pclient->adapter,msg,1); //return the actual messages transfer
  36. if(err != 1)
  37. {
  38. pr_err("%s: i2c_transfer err:%d, addr:0x%x, reg:0x%x\n", __func__, err, pclient->addr,
  39. (buff[0]<<8|buff[1]));
  40. return -1;
  41. }
  42. return 0;
  43. }
  44. /** int VL6180x_I2CRead(VL6180xDev_t dev, void *buff, uint8_t len);
  45. * @brief Read data buffer from VL6180x device via i2c
  46. * @param dev The device to read from
  47. * @param buff The data buffer to fill
  48. * @param len The length of the transaction in byte
  49. * @return transaction status
  50. */
  51. int VL6180x_I2CRead(VL6180xDev_t dev, uint8_t *buff, uint8_t len)
  52. {
  53. struct i2c_msg msg[1];
  54. int err=0;
  55. msg[0].addr = pclient->addr;
  56. msg[0].flags = I2C_M_RD|pclient->flags;
  57. msg[0].buf= buff;
  58. msg[0].len=len;
  59. err = i2c_transfer(pclient->adapter,&msg[0],1); //return the actual mesage transfer
  60. if(err != 1)
  61. {
  62. pr_err("%s: Read i2c_transfer err:%d, addr:0x%x\n", __func__, err, pclient->addr);
  63. return -1;
  64. }
  65. return 0;
  66. }
  67. #endif