vl6180x_api.h 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  1. /*******************************************************************************
  2. Copyright © 2015, STMicroelectronics International N.V.
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. * Neither the name of STMicroelectronics nor the
  12. names of its contributors may be used to endorse or promote products
  13. derived from this software without specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  15. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
  17. NON-INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS ARE DISCLAIMED.
  18. IN NO EVENT SHALL STMICROELECTRONICS INTERNATIONAL N.V. BE LIABLE FOR ANY
  19. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  22. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. ********************************************************************************/
  26. /*
  27. * @file VL6180x_api.h
  28. * $Date: 2015-09-07 16:41:32 +0200 (Mon, 07 Sep 2015) $
  29. * $Revision: 2503 $
  30. */
  31. #ifndef VL6180x_API_H_
  32. #define VL6180x_API_H_
  33. #include "vl6180x_platform.h"
  34. #ifdef __cplusplus
  35. extern "C" {
  36. #endif
  37. /** @defgroup api_ll API Low Level Functions
  38. * @brief API Low level functions
  39. */
  40. /** @defgroup api_hl API High Level Functions
  41. * @brief API High level functions
  42. */
  43. /*
  44. * Check and set default platform dependent configuration
  45. */
  46. #ifndef VL6180x_SINGLE_DEVICE_DRIVER
  47. #error "VL6180x_SINGLE_DEVICE_DRIVER not defined"
  48. /* TODO you may remove or comment these #error but it is best you update your vl6180x_platform.h file to define it*/
  49. #endif
  50. #ifndef VL6180x_RANGE_STATUS_ERRSTRING
  51. #warning "VL6180x_RANGE_STATUS_ERRSTRING not defined ?"
  52. /* TODO you may remove or comment these #warning and keep the default below to keep compatibility
  53. or update your vl6180x_platform.h file */
  54. /**
  55. * force VL6180x_RANGE_STATUS_ERRSTRING to not supported when not part of any cfg file
  56. */
  57. #define VL6180x_RANGE_STATUS_ERRSTRING 0
  58. #endif
  59. #ifndef VL6180X_SAFE_POLLING_ENTER
  60. #warning "VL6180X_SAFE_POLLING_ENTER not defined, likely old vl6180x_cfg.h file ?"
  61. /* TODO you may remove or comment these #warning and keep the default below to keep compatibility
  62. or update your vl6180x_platform.h file */
  63. /**
  64. * force VL6180X_SAFE_POLLING_ENTER to off when not in cfg file
  65. */
  66. #define VL6180X_SAFE_POLLING_ENTER 0 /* off by default as in api 2.0 */
  67. #endif
  68. #ifndef VL6180X_LOG_ENABLE
  69. /**
  70. * Force VL6180X_LOG_ENABLE to none as default
  71. */
  72. #define VL6180X_LOG_ENABLE 0
  73. #endif
  74. #if VL6180x_RANGE_STATUS_ERRSTRING
  75. /**@def VL6180x_HAVE_RANGE_STATUS_ERRSTRING
  76. * @brief is defined when @a #VL6180x_RANGE_STATUS_ERRSTRING is enable
  77. */
  78. #define VL6180x_HAVE_RANGE_STATUS_ERRSTRING
  79. #endif
  80. /** @brief Get API version as "hex integer" 0xMMnnss
  81. */
  82. #define VL6180x_ApiRevInt ((VL6180x_API_REV_MAJOR<<24)+(VL6180x_API_REV_MINOR<<16)+VL6180x_API_REV_SUB)
  83. /** Get API version as string for exe "2.1.12" "
  84. */
  85. #define VL6180x_ApiRevStr VL6180X_STR(VL6180x_API_REV_MAJOR) "." VL6180X_STR(VL6180x_API_REV_MINOR) "." VL6180X_STR(VL6180x_API_REV_SUB)
  86. /** @defgroup api_init Init functions
  87. * @brief API init functions
  88. * @ingroup api_hl
  89. * @{
  90. */
  91. /**
  92. * @brief Wait for device booted after chip enable (hardware standby)
  93. * @par Function Description
  94. * After Chip enable Application you can also simply wait at least 1ms to ensure device is ready
  95. * @warning After device chip enable (gpio0) de-asserted user must wait gpio1 to get asserted (hardware standby).
  96. * or wait at least 400usec prior to do any low level access or api call .
  97. *
  98. * This function implements polling for standby but you must ensure 400usec from chip enable passed\n
  99. * @warning Calling this function if device is not fresh out of reset will result in an indefinite loop\n
  100. *
  101. * @param dev The device
  102. * @return 0 on success
  103. */
  104. int VL6180x_WaitDeviceBooted(VL6180xDev_t dev);
  105. /**
  106. *
  107. * @brief One time device initialization
  108. *
  109. * To be called once and only once after device is brought out of reset (Chip enable) and booted see @a VL6180x_WaitDeviceBooted()
  110. *
  111. * @par Function Description
  112. * When not used after a fresh device "power up" or reset, it may return @a #CALIBRATION_WARNING
  113. * meaning wrong calibration data may have been fetched from device that can result in unpredictable and wrong ranging values\n
  114. *
  115. * @param dev The device
  116. * @return 0 on success, @a #CALIBRATION_WARNING if failed
  117. */
  118. int VL6180x_InitData(VL6180xDev_t dev);
  119. /**
  120. * @brief Configure GPIO1 function and set polarity.
  121. * @par Function Description
  122. * To be used prior to arm single shot measure or start continuous mode.
  123. *
  124. * The function uses @a VL6180x_SetupGPIOx() for setting gpio 1.
  125. * @warning changing polarity can generate a spurious interrupt on pins.
  126. * It sets an interrupt flags condition that must be cleared to avoid polling hangs. \n
  127. * It is safe to run VL6180x_ClearAllInterrupt() just after.
  128. *
  129. * @param dev The device
  130. * @param IntFunction The interrupt functionality to use one of :\n
  131. * @a #GPIOx_SELECT_OFF \n
  132. * @a #GPIOx_SELECT_GPIO_INTERRUPT_OUTPUT
  133. * @param ActiveHigh The interrupt line polarity see ::IntrPol_e
  134. * use @a #INTR_POL_LOW (falling edge) or @a #INTR_POL_HIGH (rising edge)
  135. * @return 0 on success
  136. */
  137. int VL6180x_SetupGPIO1(VL6180xDev_t dev, uint8_t IntFunction, int ActiveHigh);
  138. /**
  139. * @brief Prepare device for operation
  140. * @par Function Description
  141. * Does static initialization and reprogram common default settings \n
  142. * Device is prepared for new measure, ready single shot ranging or ALS typical polling operation\n
  143. * After prepare user can : \n
  144. * @li Call other API function to set other settings\n
  145. * @li Configure the interrupt pins, etc... \n
  146. * @li Then start ranging or ALS operations in single shot or continuous mode
  147. *
  148. * @param dev The device
  149. * @return 0 on success
  150. */
  151. int VL6180x_Prepare(VL6180xDev_t dev);
  152. /** @} */
  153. /** @defgroup api_hl_range Ranging functions
  154. * @brief Ranging functions
  155. * @ingroup api_hl
  156. * @{
  157. */
  158. /**
  159. * @brief Start continuous ranging mode
  160. *
  161. * @details End user should ensure device is in idle state and not already running
  162. */
  163. int VL6180x_RangeStartContinuousMode(VL6180xDev_t dev);
  164. /**
  165. * @brief Start single shot ranging measure
  166. *
  167. * @details End user should ensure device is in idle state and not already running
  168. */
  169. int VL6180x_RangeStartSingleShot(VL6180xDev_t dev);
  170. /**
  171. * @brief Set maximum convergence time
  172. *
  173. * @par Function Description
  174. * Setting a low convergence time can impact maximal detectable distance.
  175. * Refer to VL6180x Datasheet Table 7 : Typical range convergence time.
  176. * A typical value for up to x3 scaling is 50 ms
  177. *
  178. * @param dev
  179. * @param MaxConTime_msec
  180. * @return 0 on success. <0 on error. >0 for calibration warning status
  181. */
  182. int VL6180x_RangeSetMaxConvergenceTime(VL6180xDev_t dev, uint8_t MaxConTime_msec);
  183. /**
  184. * @brief Single shot Range measurement in polling mode.
  185. *
  186. * @par Function Description
  187. * Kick off a new single shot range then wait for ready to retrieve it by polling interrupt status \n
  188. * Ranging must be prepared by a first call to @a VL6180x_Prepare() and it is safer to clear very first poll call \n
  189. * This function reference VL6180x_PollDelay(dev) porting macro/call on each polling loop,
  190. * but PollDelay(dev) may never be called if measure in ready on first poll loop \n
  191. * Should not be use in continuous mode operation as it will stop it and cause stop/start misbehaviour \n
  192. * \n This function clears Range Interrupt status , but not error one. For that uses @a VL6180x_ClearErrorInterrupt() \n
  193. * This range error is not related VL6180x_RangeData_t::errorStatus that refer measure status \n
  194. *
  195. * @param dev The device
  196. * @param pRangeData Will be populated with the result ranging data @a VL6180x_RangeData_t
  197. * @return 0 on success , @a #RANGE_ERROR if device reports an error case in it status (not cleared) use
  198. *
  199. * \sa ::VL6180x_RangeData_t
  200. */
  201. int VL6180x_RangePollMeasurement(VL6180xDev_t dev, VL6180x_RangeData_t *pRangeData);
  202. /**
  203. * @brief Check for measure readiness and get it if ready
  204. *
  205. * @par Function Description
  206. * Using this function is an alternative to @a VL6180x_RangePollMeasurement() to avoid polling operation. This is suitable for applications
  207. * where host CPU is triggered on a interrupt (not from VL6180X) to perform ranging operation. In this scenario, we assume that the very first ranging
  208. * operation is triggered by a call to @a VL6180x_RangeStartSingleShot(). Then, host CPU regularly calls @a VL6180x_RangeGetMeasurementIfReady() to
  209. * get a distance measure if ready. In case the distance is not ready, host may get it at the next call.\n
  210. *
  211. * @warning
  212. * This function does not re-start a new measurement : this is up to the host CPU to do it.\n
  213. * This function clears Range Interrupt for measure ready , but not error interrupts. For that, uses @a VL6180x_ClearErrorInterrupt() \n
  214. *
  215. * @param dev The device
  216. * @param pRangeData Will be populated with the result ranging data if available
  217. * @return 0 on success and <0 in case of error. Please check pRangeData.errorStatus to check is new measurement is ready or not.
  218. */
  219. int VL6180x_RangeGetMeasurementIfReady(VL6180xDev_t dev, VL6180x_RangeData_t *pRangeData);
  220. /**
  221. * @brief Retrieve range measurements set from device
  222. *
  223. * @par Function Description
  224. * The measurement is made of range_mm status and error code @a VL6180x_RangeData_t \n
  225. * Based on configuration selected extra measures are included.
  226. *
  227. * @warning should not be used in continuous if wrap around filter is active \n
  228. * Does not perform any wait nor check for result availability or validity.
  229. *\sa VL6180x_RangeGetResult for "range only" measurement
  230. *
  231. * @param dev The device
  232. * @param pRangeData Pointer to the data structure to fill up
  233. * @return 0 on success
  234. */
  235. int VL6180x_RangeGetMeasurement(VL6180xDev_t dev, VL6180x_RangeData_t *pRangeData);
  236. /**
  237. * @brief Get ranging result and only that
  238. *
  239. * @par Function Description
  240. * Unlike @a VL6180x_RangeGetMeasurement() this function only retrieves the range in millimeter \n
  241. * It does any required up-scale translation\n
  242. * It can be called after success status polling or in interrupt mode \n
  243. * @warning these function is not doing wrap around filtering \n
  244. * This function doesn't perform any data ready check!
  245. *
  246. * @param dev The device
  247. * @param pRange_mm Pointer to range distance
  248. * @return 0 on success
  249. */
  250. int VL6180x_RangeGetResult(VL6180xDev_t dev, int32_t *pRange_mm);
  251. /**
  252. * @brief Configure ranging interrupt reported to application
  253. *
  254. * @param dev The device
  255. * @param ConfigGpioInt Select ranging report\n select one (and only one) of:\n
  256. * @a #CONFIG_GPIO_INTERRUPT_DISABLED \n
  257. * @a #CONFIG_GPIO_INTERRUPT_LEVEL_LOW \n
  258. * @a #CONFIG_GPIO_INTERRUPT_LEVEL_HIGH \n
  259. * @a #CONFIG_GPIO_INTERRUPT_OUT_OF_WINDOW \n
  260. * @a #CONFIG_GPIO_INTERRUPT_NEW_SAMPLE_READY
  261. * @return 0 on success
  262. */
  263. int VL6180x_RangeConfigInterrupt(VL6180xDev_t dev, uint8_t ConfigGpioInt);
  264. /**
  265. * @brief Clear range interrupt
  266. *
  267. * @param dev The device
  268. * @return 0 On success
  269. */
  270. #define VL6180x_RangeClearInterrupt(dev) VL6180x_ClearInterrupt(dev, INTERRUPT_CLEAR_RANGING)
  271. /**
  272. * @brief Return ranging error interrupt status
  273. *
  274. * @par Function Description
  275. * Appropriate Interrupt report must have been selected first by @a VL6180x_RangeConfigInterrupt() or @a VL6180x_Prepare() \n
  276. *
  277. * Can be used in polling loop to wait for a given ranging event or in interrupt to read the trigger \n
  278. * Events triggers are : \n
  279. * @a #RES_INT_STAT_GPIO_LOW_LEVEL_THRESHOLD \n
  280. * @a #RES_INT_STAT_GPIO_HIGH_LEVEL_THRESHOLD \n
  281. * @a #RES_INT_STAT_GPIO_OUT_OF_WINDOW \n (RES_INT_STAT_GPIO_LOW_LEVEL_THRESHOLD|RES_INT_STAT_GPIO_HIGH_LEVEL_THRESHOLD)
  282. * @a #RES_INT_STAT_GPIO_NEW_SAMPLE_READY \n
  283. *
  284. * @sa IntrStatus_t
  285. * @param dev The device
  286. * @param pIntStatus Pointer to status variable to update
  287. * @return 0 on success
  288. */
  289. int VL6180x_RangeGetInterruptStatus(VL6180xDev_t dev, uint8_t *pIntStatus);
  290. #if VL6180x_RANGE_STATUS_ERRSTRING
  291. extern const char *ROMABLE_DATA VL6180x_RangeStatusErrString[];
  292. /**
  293. * @brief Human readable error string for range error status
  294. *
  295. * @param RangeErrCode The error code as stored on @a VL6180x_RangeData_t::errorStatus
  296. * @return error string , NULL for invalid RangeErrCode
  297. * @sa ::RangeError_u
  298. */
  299. const char *VL6180x_RangeGetStatusErrString(uint8_t RangeErrCode);
  300. #else
  301. #define VL6180x_RangeGetStatusErrString(...) NULL
  302. #endif
  303. /** @} */
  304. #if VL6180x_ALS_SUPPORT
  305. /** @defgroup api_hl_als ALS functions
  306. * @brief ALS functions
  307. * @ingroup api_hl
  308. * @{
  309. */
  310. /**
  311. * @brief Run a single ALS measurement in single shot polling mode
  312. *
  313. * @par Function Description
  314. * Kick off a new single shot ALS then wait new measurement ready to retrieve it ( polling system interrupt status register for als) \n
  315. * ALS must be prepared by a first call to @a VL6180x_Prepare() \n
  316. * \n Should not be used in continuous or interrupt mode it will break it and create hazard in start/stop \n
  317. *
  318. * @param dev The device
  319. * @param pAlsData Als data structure to fill up @a VL6180x_AlsData_t
  320. * @return 0 on success
  321. */
  322. int VL6180x_AlsPollMeasurement(VL6180xDev_t dev, VL6180x_AlsData_t *pAlsData);
  323. /**
  324. * @brief Get actual ALS measurement
  325. *
  326. * @par Function Description
  327. * Can be called after success status polling or in interrupt mode to retrieve ALS measurement from device \n
  328. * This function doesn't perform any data ready check !
  329. *
  330. * @param dev The device
  331. * @param pAlsData Pointer to measurement struct @a VL6180x_AlsData_t
  332. * @return 0 on success
  333. */
  334. int VL6180x_AlsGetMeasurement(VL6180xDev_t dev, VL6180x_AlsData_t *pAlsData);
  335. /**
  336. * @brief Configure ALS interrupts provide to application
  337. *
  338. * @param dev The Device
  339. * @param ConfigGpioInt Select one (and only one) of : \n
  340. * @a #CONFIG_GPIO_INTERRUPT_DISABLED \n
  341. * @a #CONFIG_GPIO_INTERRUPT_LEVEL_LOW \n
  342. * @a #CONFIG_GPIO_INTERRUPT_LEVEL_HIGH \n
  343. * @a #CONFIG_GPIO_INTERRUPT_OUT_OF_WINDOW \n
  344. * @a #CONFIG_GPIO_INTERRUPT_NEW_SAMPLE_READY
  345. * @return 0 on success may return #INVALID_PARAMS for invalid mode
  346. */
  347. int VL6180x_AlsConfigInterrupt(VL6180xDev_t dev, uint8_t ConfigGpioInt);
  348. /**
  349. * @brief Set ALS integration period
  350. *
  351. * @param dev The device
  352. * @param period_ms Integration period in msec. Value in between 50 to 100 msec is recommended\n
  353. * @return 0 on success
  354. */
  355. int VL6180x_AlsSetIntegrationPeriod(VL6180xDev_t dev, uint16_t period_ms);
  356. /**
  357. * @brief Set ALS "inter-measurement period"
  358. *
  359. * @par Function Description
  360. * The so call data-sheet "inter measurement period" is actually an extra inter-measurement delay
  361. *
  362. * @param dev The device
  363. * @param intermeasurement_period_ms Inter measurement time in milli second\n
  364. * @warning applied value is clipped to 2550 ms\n
  365. * @return 0 on success if value is
  366. */
  367. int VL6180x_AlsSetInterMeasurementPeriod(VL6180xDev_t dev, uint16_t intermeasurement_period_ms);
  368. /**
  369. * @brief Set ALS analog gain code
  370. *
  371. * @par Function Description
  372. * ALS gain code value programmed in @a SYSALS_ANALOGUE_GAIN .
  373. * @param dev The device
  374. * @param gain Gain code see datasheet or AlsGainLookUp for real value. Value is clipped to 7.
  375. * @return 0 on success
  376. */
  377. int VL6180x_AlsSetAnalogueGain(VL6180xDev_t dev, uint8_t gain);
  378. /**
  379. * @brief Set thresholds for ALS continuous mode
  380. * @warning Threshold are raw device value not lux!
  381. *
  382. * @par Function Description
  383. * Basically value programmed in @a SYSALS_THRESH_LOW and @a SYSALS_THRESH_HIGH registers
  384. * @param dev The device
  385. * @param low ALS low raw threshold for @a SYSALS_THRESH_LOW
  386. * @param high ALS high raw threshold for @a SYSALS_THRESH_HIGH
  387. * @return 0 on success
  388. */
  389. int VL6180x_AlsSetThresholds(VL6180xDev_t dev, uint8_t low, uint8_t high);
  390. /**
  391. * @brief Clear ALS interrupt
  392. *
  393. * @param dev The device
  394. * @return 0 On success
  395. */
  396. #define VL6180x_AlsClearInterrupt(dev) VL6180x_ClearInterrupt(dev, INTERRUPT_CLEAR_ALS)
  397. /**
  398. * Read ALS interrupt status
  399. * @param dev Device
  400. * @param pIntStatus Pointer to status
  401. * @return 0 on success
  402. */
  403. int VL6180x_AlsGetInterruptStatus(VL6180xDev_t dev, uint8_t *pIntStatus);
  404. /** @} */
  405. #endif
  406. /** @defgroup api_ll_init Init functions
  407. * @brief Init functions
  408. * @ingroup api_ll
  409. * @{
  410. */
  411. /**
  412. * @brief Low level ranging and ALS register static settings (you should call @a VL6180x_Prepare() function instead)
  413. *
  414. * @param dev
  415. * @return 0 on success
  416. */
  417. int VL6180x_StaticInit(VL6180xDev_t dev);
  418. /** @} */
  419. /** @defgroup api_ll_range Ranging functions
  420. * @brief Ranging Low Level functions
  421. * @ingroup api_ll
  422. * @{
  423. */
  424. /**
  425. * @brief Wait for device to be ready (before a new ranging command can be issued by application)
  426. * @param dev The device
  427. * @param MaxLoop Max Number of i2c polling loop see @a #msec_2_i2cloop
  428. * @return 0 on success. <0 when fail \n
  429. * @ref VL6180x_ErrCode_t::TIME_OUT for time out \n
  430. * @ref VL6180x_ErrCode_t::INVALID_PARAMS if MaxLop<1
  431. */
  432. int VL6180x_RangeWaitDeviceReady(VL6180xDev_t dev, int MaxLoop);
  433. /**
  434. * @brief Program Inter measurement period (used only in continuous mode)
  435. *
  436. * @par Function Description
  437. * When trying to set too long time, it returns #INVALID_PARAMS
  438. *
  439. * @param dev The device
  440. * @param InterMeasTime_msec Requires inter-measurement time in msec
  441. * @return 0 on success
  442. */
  443. int VL6180x_RangeSetInterMeasPeriod(VL6180xDev_t dev, uint32_t InterMeasTime_msec);
  444. /**
  445. * @brief Set device ranging scaling factor
  446. *
  447. * @par Function Description
  448. * The ranging scaling factor is applied on the raw distance measured by the device to increase operating ranging at the price of the precision.
  449. * Changing the scaling factor when device is not in f/w standby state (free running) is not safe.
  450. * It can be source of spurious interrupt, wrongly scaled range etc ...
  451. * @warning __This function doesns't update high/low threshold and other programmed settings linked to scaling factor__.
  452. * To ensure proper operation, threshold and scaling changes should be done following this procedure: \n
  453. * @li Set Group hold : @a VL6180x_SetGroupParamHold() \n
  454. * @li Get Threshold @a VL6180x_RangeGetThresholds() \n
  455. * @li Change scaling : @a VL6180x_UpscaleSetScaling() \n
  456. * @li Set Threshold : @a VL6180x_RangeSetThresholds() \n
  457. * @li Unset Group Hold : @a VL6180x_SetGroupParamHold()
  458. *
  459. * @param dev The device
  460. * @param scaling Scaling factor to apply (1,2 or 3)
  461. * @return 0 on success when up-scale support is not configured it fail for any
  462. * scaling than the one statically configured.
  463. */
  464. int VL6180x_UpscaleSetScaling(VL6180xDev_t dev, uint8_t scaling);
  465. /**
  466. * @brief Get current ranging scaling factor
  467. *
  468. * @param dev The device
  469. * @return The current scaling factor
  470. */
  471. int VL6180x_UpscaleGetScaling(VL6180xDev_t dev);
  472. /**
  473. * @brief Give filtered state (wrap-around filter) of a range measurement
  474. * @param pRangeData Range measurement data
  475. * @return 0 means measure was not filtered, when not 0 range from device got filtered by filter post processing
  476. */
  477. #define VL6180x_RangeIsFilteredMeasurement(pRangeData) ((pRangeData)->errorStatus == RangingFiltered)
  478. /**
  479. * @brief Get the maximal distance for actual scaling
  480. * @par Function Description
  481. * Do not use prior to @a VL6180x_Prepare() or at least @a VL6180x_InitData()
  482. *
  483. * Any range value more than the value returned by this function is to be considered as "no target detected"
  484. * or "no target in detectable range"
  485. * @warning The maximal distance depends on the scaling
  486. *
  487. * @param dev The device
  488. * @return The maximal range limit for actual mode and scaling
  489. */
  490. uint16_t VL6180x_GetUpperLimit(VL6180xDev_t dev);
  491. /**
  492. * @brief Apply low and high ranging thresholds that are considered only in continuous mode
  493. *
  494. * @par Function Description
  495. * This function programs low and high ranging thresholds that are considered in continuous mode :
  496. * interrupt will be raised only when an object is detected at a distance inside this [low:high] range.
  497. * The function takes care of applying current scaling factor if any.\n
  498. * To be safe, in continuous operation, thresholds must be changed under "group parameter hold" cover.
  499. * Group hold can be activated/deactivated directly in the function or externally (then set 0)
  500. * using /a VL6180x_SetGroupParamHold() function.
  501. *
  502. * @param dev The device
  503. * @param low Low threshold in mm
  504. * @param high High threshold in mm
  505. * @param SafeHold Use of group parameters hold to surround threshold programming.
  506. * @return 0 On success
  507. */
  508. int VL6180x_RangeSetThresholds(VL6180xDev_t dev, uint16_t low, uint16_t high, int SafeHold);
  509. /**
  510. * @brief Get scaled high and low threshold from device
  511. *
  512. * @par Function Description
  513. * Due to scaling factor, the returned value may be different from what has been programmed first (precision lost).
  514. * For instance VL6180x_RangeSetThresholds(dev,11,22) with scale 3
  515. * will read back 9 ((11/3)x3) and 21 ((22/3)x3).
  516. * @param dev The device
  517. * @param low scaled low Threshold ptr can be NULL if not needed
  518. * @param high scaled High Threshold ptr can be NULL if not needed
  519. * @return 0 on success, return value is undefined if both low and high are NULL
  520. * @warning return value is undefined if both low and high are NULL
  521. */
  522. int VL6180x_RangeGetThresholds(VL6180xDev_t dev, uint16_t *low, uint16_t *high);
  523. /**
  524. * @brief Set ranging raw thresholds (scaling not considered so not recommended to use it)
  525. *
  526. * @param dev The device
  527. * @param low raw low threshold set to raw register
  528. * @param high raw high threshold set to raw register
  529. * @return 0 on success
  530. */
  531. int VL6180x_RangeSetRawThresholds(VL6180xDev_t dev, uint8_t low, uint8_t high);
  532. /**
  533. * @brief Set Early Convergence Estimate ratio
  534. * @par Function Description
  535. * For more information on ECE check datasheet
  536. * @warning May return a calibration warning in some use cases
  537. *
  538. * @param dev The device
  539. * @param FactorM ECE factor M in M/D
  540. * @param FactorD ECE factor D in M/D
  541. * @return 0 on success. <0 on error. >0 on warning
  542. */
  543. int VL6180x_RangeSetEceFactor(VL6180xDev_t dev, uint16_t FactorM, uint16_t FactorD);
  544. /**
  545. * @brief Set Early Convergence Estimate state (See #SYSRANGE_RANGE_CHECK_ENABLES register)
  546. * @param dev The device
  547. * @param enable State to be set 0=disabled, otherwise enabled
  548. * @return 0 on success
  549. */
  550. int VL6180x_RangeSetEceState(VL6180xDev_t dev, int enable);
  551. /**
  552. * @brief Set activation state of the wrap around filter
  553. * @param dev The device
  554. * @param state New activation state (0=off, otherwise on)
  555. * @return 0 on success
  556. */
  557. int VL6180x_FilterSetState(VL6180xDev_t dev, int state);
  558. /**
  559. * Get activation state of the wrap around filter
  560. * @param dev The device
  561. * @return Filter enabled or not, when filter is not supported it always returns 0S
  562. */
  563. int VL6180x_FilterGetState(VL6180xDev_t dev);
  564. /**
  565. * @brief Set activation state of DMax computation
  566. * @param dev The device
  567. * @param state New activation state (0=off, otherwise on)
  568. * @return 0 on success
  569. */
  570. int VL6180x_DMaxSetState(VL6180xDev_t dev, int state);
  571. /**
  572. * Get activation state of DMax computation
  573. * @param dev The device
  574. * @return Filter enabled or not, when filter is not supported it always returns 0S
  575. */
  576. int VL6180x_DMaxGetState(VL6180xDev_t dev);
  577. /**
  578. * @brief Set ranging mode and start/stop measure (use high level functions instead : @a VL6180x_RangeStartSingleShot() or @a VL6180x_RangeStartContinuousMode())
  579. *
  580. * @par Function Description
  581. * When used outside scope of known polling single shot stopped state, \n
  582. * user must ensure the device state is "idle" before to issue a new command.
  583. *
  584. * @param dev The device
  585. * @param mode A combination of working mode (#MODE_SINGLESHOT or #MODE_CONTINUOUS) and start/stop condition (#MODE_START_STOP) \n
  586. * @return 0 on success
  587. */
  588. int VL6180x_RangeSetSystemMode(VL6180xDev_t dev, uint8_t mode);
  589. /**
  590. * @brief Enable/disable range ignore feature
  591. *
  592. * @par Function Description
  593. * Enable range ignore feature to ensure that the device does not range on the cover glass because of cross-talk.
  594. * @a VL6180x_RangeIgnoreConfigure() should be run first to configure feature prior to enable it.
  595. *
  596. * @param dev The Device
  597. * @param EnableState Feature state to set 0= off else =on
  598. * @return 0 on success
  599. */
  600. int VL6180x_RangeIgnoreSetEnable(VL6180xDev_t dev, int EnableState);
  601. /**
  602. * @brief Configure Range ignore feature
  603. *
  604. * @par Function Description
  605. * When return signal rate is below the IgnoreThreshold and return distance is below the ValidHeight, the distance will be ignored
  606. * @warning It is recommended to enable range ignore feature and configure it only when device is in stop or idle state
  607. * @warning Once this function is called, next call to @a VL6180x_InitData() function without reseting the device will result in wrong ranging operation
  608. * @param dev The Device
  609. * @param ValidHeight_mm Valid height in mm (unscaled ie not raw value before scaling)
  610. * @param IgnoreThreshold Ignore threshold in fixpoint 9.7 MegaCount/sec
  611. * @return
  612. */
  613. int VL6180x_RangeIgnoreConfigure(VL6180xDev_t dev, uint16_t ValidHeight_mm, uint16_t IgnoreThreshold);
  614. /** @} */
  615. /** @defgroup api_ll_range_calibration Ranging calibration functions
  616. * @brief Ranging calibration functions
  617. * @ingroup api_ll
  618. * @{
  619. */
  620. /**
  621. * @brief Get part to part calibration offset
  622. *
  623. * @par Function Description
  624. * Should only be used after a successful call to @a VL6180x_InitData to backup device nvm value
  625. *
  626. * @param dev The device
  627. * @return part to part calibration offset from device
  628. */
  629. int8_t VL6180x_GetOffsetCalibrationData(VL6180xDev_t dev);
  630. /**
  631. * Set or over-write part to part calibration offset and apply it immediately
  632. * \sa VL6180x_InitData(), VL6180x_GetOffsetCalibrationData()
  633. * @param dev The device
  634. * @param offset Offset
  635. * @return 0 on success
  636. */
  637. int VL6180x_SetOffsetCalibrationData(VL6180xDev_t dev, int8_t offset);
  638. /**
  639. * @brief Set Cross talk compensation rate
  640. *
  641. * @par Function Description
  642. * It programs register @a #SYSRANGE_CROSSTALK_COMPENSATION_RATE
  643. *
  644. * @param dev The device
  645. * @param Rate Compensation rate (9.7 fix point) see datasheet for details
  646. * @return 0 on success
  647. */
  648. int VL6180x_SetXTalkCompensationRate(VL6180xDev_t dev, FixPoint97_t Rate);
  649. /** @} */
  650. #if VL6180x_ALS_SUPPORT
  651. /** @defgroup api_ll_als ALS functions
  652. * @brief ALS functions
  653. * @ingroup api_ll
  654. * @{
  655. */
  656. /**
  657. * @brief Wait for device to be ready for new als operation or max pollign loop (time out)
  658. * @param dev The device
  659. * @param MaxLoop Max Number of i2c polling loop see @a #msec_2_i2cloop
  660. * @return 0 on success. <0 when @a VL6180x_ErrCode_t::TIME_OUT if timed out
  661. */
  662. int VL6180x_AlsWaitDeviceReady(VL6180xDev_t dev, int MaxLoop);
  663. /**
  664. * @brief Set ALS system mode and start/stop measure
  665. *
  666. * @warning When used outside after single shot polling, \n
  667. * User must ensure the device state is ready before issuing a new command (using @a VL6180x_AlsWaitDeviceReady()). \n
  668. * Non respect of this, can cause loss of interrupt or device hanging.
  669. *
  670. * @param dev The device
  671. * @param mode A combination of working mode (#MODE_SINGLESHOT or #MODE_CONTINUOUS) and start condition (#MODE_START_STOP) \n
  672. * @return 0 on success
  673. */
  674. int VL6180x_AlsSetSystemMode(VL6180xDev_t dev, uint8_t mode);
  675. /** @} */
  676. #endif
  677. /** @defgroup api_ll_misc Misc functions
  678. * @brief Misc functions
  679. * @ingroup api_ll
  680. * @{
  681. */
  682. /**
  683. * Set Group parameter Hold state
  684. *
  685. * @par Function Description
  686. * Group parameter holds @a #SYSTEM_GROUPED_PARAMETER_HOLD enable safe update (non atomic across multiple measure) by host
  687. * \n The critical register group is composed of: \n
  688. * #SYSTEM_INTERRUPT_CONFIG_GPIO \n
  689. * #SYSRANGE_THRESH_HIGH \n
  690. * #SYSRANGE_THRESH_LOW \n
  691. * #SYSALS_INTEGRATION_PERIOD \n
  692. * #SYSALS_ANALOGUE_GAIN \n
  693. * #SYSALS_THRESH_HIGH \n
  694. * #SYSALS_THRESH_LOW
  695. *
  696. *
  697. * @param dev The device
  698. * @param Hold Group parameter Hold state to be set (on/off)
  699. * @return 0 on success
  700. */
  701. int VL6180x_SetGroupParamHold(VL6180xDev_t dev, int Hold);
  702. /**
  703. * @brief Set new device i2c address
  704. *
  705. * After completion the device will answer to the new address programmed.
  706. *
  707. * @sa AN4478: Using multiple VL6180X's in a single design
  708. * @param dev The device
  709. * @param NewAddr The new i2c address (8 bits)
  710. * @return 0 on success
  711. */
  712. int VL6180x_SetI2CAddress(VL6180xDev_t dev, uint8_t NewAddr);
  713. /**
  714. * @brief Fully configure gpio 0/1 pin : polarity and functionality
  715. *
  716. * @param dev The device
  717. * @param pin gpio pin 0 or 1
  718. * @param IntFunction Pin functionality : either #GPIOx_SELECT_OFF or #GPIOx_SELECT_GPIO_INTERRUPT_OUTPUT (refer to #SYSTEM_MODE_GPIO1 register definition)
  719. * @param ActiveHigh Set active high polarity, or active low see @a ::IntrPol_e
  720. * @return 0 on success
  721. */
  722. int VL6180x_SetupGPIOx(VL6180xDev_t dev, int pin, uint8_t IntFunction, int ActiveHigh);
  723. /**
  724. * @brief Set interrupt pin polarity for the given GPIO
  725. *
  726. * @param dev The device
  727. * @param pin Pin 0 or 1
  728. * @param active_high select active high or low polarity using @ref IntrPol_e
  729. * @return 0 on success
  730. */
  731. int VL6180x_SetGPIOxPolarity(VL6180xDev_t dev, int pin, int active_high);
  732. /**
  733. * Select interrupt functionality for the given GPIO
  734. *
  735. * @par Function Description
  736. * Functionality refer to @a SYSTEM_MODE_GPIO0
  737. *
  738. * @param dev The device
  739. * @param pin Pin to configure 0 or 1 (gpio0 or gpio1)\nNote that gpio0 is chip enable at power up !
  740. * @param functionality Pin functionality : either #GPIOx_SELECT_OFF or #GPIOx_SELECT_GPIO_INTERRUPT_OUTPUT (refer to #SYSTEM_MODE_GPIO1 register definition)
  741. * @return 0 on success
  742. */
  743. int VL6180x_SetGPIOxFunctionality(VL6180xDev_t dev, int pin, uint8_t functionality);
  744. /**
  745. * #brief Disable and turn to Hi-Z gpio output pin
  746. *
  747. * @param dev The device
  748. * @param pin The pin number to disable 0 or 1
  749. * @return 0 on success
  750. */
  751. int VL6180x_DisableGPIOxOut(VL6180xDev_t dev, int pin);
  752. /**
  753. * @def msec_2_i2cloop
  754. * @brief Number of I2C polling loop (an 8 bit register) to run for maximal wait time.
  755. *
  756. * @par Function Description
  757. * When polling via I2C the overall time is mainly the I2C transaction time because it is a slow bus
  758. * one 8 bit register poll on I2C bus timing is shown below: \n
  759. * start + addr_w(a) + 2x8bit index(a) + stop + start + addr_rd(a) + 1x8bit data_rd(a) + stop \n
  760. * 1 8 1 2*(8+1) 1 1 8 1 8 1 1 \n
  761. * so 49 serial bits
  762. *
  763. * @param time_ms Time to wait in milli second 10
  764. * @param i2c_khz I2C bus frequencies in KHz for instance 400
  765. * @return The number of loops (at least 1)
  766. */
  767. #define msec_2_i2cloop(time_ms, i2c_khz) (((time_ms) * (i2c_khz) / 49) + 1)
  768. /** @} */
  769. /**
  770. * polarity use in @a VL6180x_SetupGPIOx() , @a VL6180x_SetupGPIO1()
  771. */
  772. typedef enum {
  773. INTR_POL_LOW = 0, /*!< set active low polarity best setup for falling edge */
  774. INTR_POL_HIGH = 1, /*!< set active high polarity best setup for rising edge */
  775. } IntrPol_e;
  776. /** @defgroup api_ll_intr Interrupts management functions
  777. * @brief Interrupts management functions
  778. * @ingroup api_ll
  779. * @{
  780. */
  781. /**
  782. * @brief Get all interrupts cause
  783. *
  784. * @param dev The device
  785. * @param status Ptr to interrupt status. You can use @a IntrStatus_t::val
  786. * @return 0 on success
  787. */
  788. int VL6180x_GetInterruptStatus(VL6180xDev_t dev, uint8_t *status);
  789. /**
  790. * @brief Clear given system interrupt condition
  791. *
  792. * @par Function Description
  793. * Clear given interrupt cause by writing into register #SYSTEM_INTERRUPT_CLEAR register.
  794. * @param dev The device
  795. * @param IntClear Which interrupt source to clear. Use any combinations of #INTERRUPT_CLEAR_RANGING , #INTERRUPT_CLEAR_ALS , #INTERRUPT_CLEAR_ERROR.
  796. * @return 0 On success
  797. */
  798. int VL6180x_ClearInterrupt(VL6180xDev_t dev, uint8_t IntClear);
  799. /**
  800. * @brief Clear error interrupt
  801. *
  802. * @param dev The device
  803. * @return 0 On success
  804. */
  805. #define VL6180x_ClearErrorInterrupt(dev) VL6180x_ClearInterrupt(dev, INTERRUPT_CLEAR_ERROR)
  806. /**
  807. * @brief Clear All interrupt causes (als+range+error)
  808. *
  809. * @param dev The device
  810. * @return 0 On success
  811. */
  812. #define VL6180x_ClearAllInterrupt(dev) VL6180x_ClearInterrupt(dev, INTERRUPT_CLEAR_ERROR|INTERRUPT_CLEAR_RANGING|INTERRUPT_CLEAR_ALS)
  813. /** @} */
  814. /** @defgroup api_reg API Register access functions
  815. * @brief Registers access functions called by API core functions
  816. * @ingroup api_ll
  817. * @{
  818. */
  819. /**
  820. * Write VL6180x single byte register
  821. * @param dev The device
  822. * @param index The register index
  823. * @param data 8 bit register data
  824. * @return success
  825. */
  826. int VL6180x_WrByte(VL6180xDev_t dev, uint16_t index, uint8_t data);
  827. /**
  828. * Thread safe VL6180x Update (rd/modify/write) single byte register
  829. *
  830. * Final_reg = (Initial_reg & and_data) |or_data
  831. *
  832. * @param dev The device
  833. * @param index The register index
  834. * @param AndData 8 bit and data
  835. * @param OrData 8 bit or data
  836. * @return 0 on success
  837. */
  838. int VL6180x_UpdateByte(VL6180xDev_t dev, uint16_t index, uint8_t AndData, uint8_t OrData);
  839. /**
  840. * Write VL6180x word register
  841. * @param dev The device
  842. * @param index The register index
  843. * @param data 16 bit register data
  844. * @return 0 on success
  845. */
  846. int VL6180x_WrWord(VL6180xDev_t dev, uint16_t index, uint16_t data);
  847. /**
  848. * Write VL6180x double word (4 byte) register
  849. * @param dev The device
  850. * @param index The register index
  851. * @param data 32 bit register data
  852. * @return 0 on success
  853. */
  854. int VL6180x_WrDWord(VL6180xDev_t dev, uint16_t index, uint32_t data);
  855. /**
  856. * Read VL6180x single byte register
  857. * @param dev The device
  858. * @param index The register index
  859. * @param data pointer to 8 bit data
  860. * @return 0 on success
  861. */
  862. int VL6180x_RdByte(VL6180xDev_t dev, uint16_t index, uint8_t *data);
  863. /**
  864. * Read VL6180x word (2byte) register
  865. * @param dev The device
  866. * @param index The register index
  867. * @param data pointer to 16 bit data
  868. * @return 0 on success
  869. */
  870. int VL6180x_RdWord(VL6180xDev_t dev, uint16_t index, uint16_t *data);
  871. /**
  872. * Read VL6180x dword (4byte) register
  873. * @param dev The device
  874. * @param index The register index
  875. * @param data pointer to 32 bit data
  876. * @return 0 on success
  877. */
  878. int VL6180x_RdDWord(VL6180xDev_t dev, uint16_t index, uint32_t *data);
  879. /**
  880. * Read VL6180x multiple bytes
  881. * @note required only if #VL6180x_HAVE_MULTI_READ is set
  882. * @param dev The device
  883. * @param index The register index
  884. * @param data pointer to 8 bit data
  885. * @param nData number of data bytes to read
  886. * @return 0 on success
  887. */
  888. int VL6180x_RdMulti(VL6180xDev_t dev, uint16_t index, uint8_t *data, int nData);
  889. /** @} */
  890. #ifdef __cplusplus
  891. }
  892. #endif
  893. #endif /* VL6180x_API_H_ */