vl6180x_i2c.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * $Date: 2015-01-08 14:30:24 +0100 (Thu, 08 Jan 2015) $
  3. * $Revision: 2039 $
  4. */
  5. /**
  6. * @file vl6180x_i2c.h
  7. *
  8. * @brief CCI interface to "raw i2c" translation layer
  9. */
  10. #ifndef VL6180_I2C_H_
  11. #define VL6180_I2C_H_
  12. #include "vl6180x_platform.h"
  13. /**
  14. * @defgroup cci_i2c CCI to RAW I2C translation layer
  15. *
  16. * This optional tranlation layer is implemented in __platform/cci-i2c__ directory. If user uses this translation layer for his platform, only @a VL6180x_I2CRead() and
  17. * @a VL6180x_I2CWrite() functions need to be implemented. Also, some code adaption (via macro) is required for multi-threading and for multiple device support.
  18. *
  19. * File vl6180x_i2c.c implements device register access via raw i2c access. If the targeted application and platform has no multi-thread, no multi-cpu and uses single
  20. * device, then nothing else is required than the 2 mandatory function : @a VL6180x_I2CRead() and @a VL6180x_I2CWrite().\n
  21. * In other cases, review and customize @a VL6180x_GetI2CAccess() and @a VL6180x_DoneI2CAccess() functions as well as @a #VL6180x_I2C_USER_VAR macro. This should be enough
  22. * to conform to a wide range of platform OS and application requirements .\n
  23. *
  24. * If your configured i2c for per device buffer via @a #I2C_BUFFER_CONFIG == 2, you must implement @a VL6180x_GetI2cBuffer()
  25. *
  26. * __I2C Port sample__ \n
  27. * A __linux kernel__ port need a "long flags" var for its spin_lock in all functions. the following code example declares a spin lock "lock" in the custom device structure. \n
  28. * @code
  29. struct MyVL6180Dev_t {
  30. struct VL6180xDevData_t StData;
  31. ...
  32. spinlock_t i2c_lock;
  33. };
  34. typedef struct MyVL6180Dev_t *VL6180xDev_t;
  35. #define VL6180x_I2C_USER_VAR unsigned long flags;
  36. #define GetI2CAccess(dev) spin_lock_irqsave(dev->i2c_lock, flags)
  37. #define DoneI2CAccess(dev) spin_unlock_irqrestore(dev->i2c_lock,flags)
  38. @endcode
  39. * __POSIX pthread__ application porting could be as follows :\n
  40. * @code
  41. struct MyVL6180Dev_t {
  42. struct VL6180xDevData_t StData;
  43. ...
  44. pthread_mutex_t *lock;
  45. };
  46. typedef struct MyVL6180Dev_t *VL6180xDev_t;
  47. #define VL6180x_I2C_USER_VAR //no need
  48. #define VL6180x_GetI2CAccess(dev) pthread_mutex_lock(dev->lock)
  49. #define VL6180x_DoneI2CAcces(dev) pthread_mutex_unlock(dev->lock)
  50. * @endcode
  51. */
  52. /**
  53. * @def I2C_BUFFER_CONFIG
  54. *
  55. * @brief Configure device register I2C access
  56. *
  57. * @li 0 : one GLOBAL buffer \n
  58. * Use one global buffer of MAX_I2C_XFER_SIZE byte in data space \n
  59. * This solution is not multi-device compliant nor multi-thread cpu safe \n
  60. * It can be the best option for small 8/16 bit MCU without stack and limited ram (STM8s, 80C51 ...)
  61. *
  62. * @li 1 : ON_STACK/local \n
  63. * Use local variable (on stack) buffer \n
  64. * This solution is multi-thread with use of i2c resource lock or mutex see @a VL6180x_GetI2CAccess() \n
  65. *
  66. * @li 2 : User defined \n
  67. * Per device potentially dynamic allocated. Requires @a VL6180x_GetI2cBuffer() to be implemented.
  68. * @ingroup Configuration
  69. */
  70. #define I2C_BUFFER_CONFIG 1
  71. /**
  72. * @brief Write data buffer to VL6180x device via i2c
  73. * @param dev The device to write to
  74. * @param buff The data buffer
  75. * @param len The length of the transaction in byte
  76. * @return 0 on success
  77. * @ingroup cci_i2c
  78. */
  79. int VL6180x_I2CWrite(VL6180xDev_t dev, uint8_t *buff, uint8_t len);
  80. /**
  81. *
  82. * @brief Read data buffer from VL6180x device via i2c
  83. * @param dev The device to read from
  84. * @param buff The data buffer to fill
  85. * @param len The length of the transaction in byte
  86. * @return 0 on success
  87. * @ingroup cci_i2c
  88. */
  89. int VL6180x_I2CRead(VL6180xDev_t dev, uint8_t *buff, uint8_t len);
  90. /**
  91. * @brief Declare any required variables used by i2c lock (@a VL6180x_DoneI2CAccess() and @a VL6180x_GetI2CAccess())
  92. * and buffer access : @a VL6180x_GetI2cBuffer()
  93. *
  94. * @ingroup cci_i2c
  95. */
  96. #define VL6180x_I2C_USER_VAR
  97. /**
  98. * @brief Acquire lock or mutex for access to i2c data buffer and bus.\n
  99. * Delete the default VL6180x_GetI2CAccess 'do-nothing' macro below if you decide to implement this function.
  100. *
  101. * This function is used to perform i2c bus level and multiple access locking required for multi thread/proccess system.\n
  102. * Multiple access (read and update) will lock once and do multiple basic i2c rd/wr to complete the overall transfer.\n
  103. * When no locking is needed this can be a void macro.\n
  104. *
  105. * @param dev the device
  106. * @ingroup cci_i2c
  107. */
  108. void VL6180x_GetI2CAccess(VL6180xDev_t dev);
  109. /**
  110. * @def VL6180x_GetI2CAccess
  111. * @brief Default 'do-nothing' macro for @a VL6180x_GetI2CAccess(). Delete if used.
  112. * @ingroup cci_i2c
  113. */
  114. #define VL6180x_GetI2CAccess(dev) (void)0 /* TODO delete if function used */
  115. /**
  116. * @brief Release acquired lock or mutex for i2c access.\n
  117. * Delete default VL6180x_DoneI2CAccess 'do-nothing' macro below if implementing that function.
  118. *
  119. * This function is used to release the acquired lock.
  120. * @param dev The device
  121. * @ingroup cci_i2c
  122. */
  123. void VL6180x_DoneI2CAccess(VL6180xDev_t dev);
  124. /** @def VL6180x_DoneI2CAcces
  125. * @brief Default 'do-nothing' macro for @a VL6180x_DoneI2CAcces(). Delete if used.
  126. * @ingroup cci_i2c
  127. */
  128. #define VL6180x_DoneI2CAcces(dev) (void)0 /*TODO delete if function used */
  129. /**
  130. * @brief Provided data buffer for i2c access for at least n_byte.
  131. *
  132. * You must implement it when i2c @a #I2C_BUFFER_CONFIG is set to 2 (User defined).\n
  133. * This is used used in the context of #VL6180x_I2C_USER_VAR
  134. *
  135. * @param dev The device
  136. * @param n_byte Minimal number of byte
  137. * @return The buffer (cannot fail return not checked)
  138. * @ingroup cci_i2c
  139. */
  140. uint8_t *VL6180x_GetI2cBuffer(VL6180xDev_t dev, int n_byte);
  141. #if I2C_BUFFER_CONFIG == 2
  142. #error /* TODO add your macro of code here for VL6180x_GetI2cBuffer */
  143. #endif
  144. #endif /* VL6180_I2C_H_ */