vl6180x_api.h 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  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: 2018-07-04 16:49:57 +0200 (Wed, 04 Jul 2018) $
  29. * $Revision: 2768 $
  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. * VL6180x_PollDelay() is platform depending, the user has to implement this function accordingly \n
  193. * 1 ms of wait is the recommended value to avoid excessive I2C requests for data ready check loop \n
  194. * \n This function clears Range Interrupt status , but not error one. For that uses @a VL6180x_ClearErrorInterrupt() \n
  195. * This range error is not related VL6180x_RangeData_t::errorStatus that refer measure status \n
  196. *
  197. * @param dev The device
  198. * @param pRangeData Will be populated with the result ranging data @a VL6180x_RangeData_t
  199. * @return 0 on success , @a #RANGE_ERROR if device reports an error case in it status (not cleared) use
  200. *
  201. * \sa ::VL6180x_RangeData_t
  202. */
  203. int VL6180x_RangePollMeasurement(VL6180xDev_t dev, VL6180x_RangeData_t *pRangeData);
  204. /**
  205. * @brief Check for measure readiness and get it if ready
  206. *
  207. * @par Function Description
  208. * Using this function is an alternative to @a VL6180x_RangePollMeasurement() to avoid polling operation. This is suitable for applications
  209. * 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
  210. * operation is triggered by a call to @a VL6180x_RangeStartSingleShot(). Then, host CPU regularly calls @a VL6180x_RangeGetMeasurementIfReady() to
  211. * get a distance measure if ready. In case the distance is not ready, host may get it at the next call.\n
  212. *
  213. * @warning
  214. * This function does not re-start a new measurement : this is up to the host CPU to do it.\n
  215. * This function clears Range Interrupt for measure ready , but not error interrupts. For that, uses @a VL6180x_ClearErrorInterrupt() \n
  216. *
  217. * @param dev The device
  218. * @param pRangeData Will be populated with the result ranging data if available
  219. * @return 0 on success and <0 in case of error. Please check pRangeData.errorStatus to check is new measurement is ready or not.
  220. */
  221. int VL6180x_RangeGetMeasurementIfReady(VL6180xDev_t dev, VL6180x_RangeData_t *pRangeData);
  222. /**
  223. * @brief Retrieve range measurements set from device
  224. *
  225. * @par Function Description
  226. * The measurement is made of range_mm status and error code @a VL6180x_RangeData_t \n
  227. * Based on configuration selected extra measures are included.
  228. *
  229. * @warning should not be used in continuous if wrap around filter is active \n
  230. * Does not perform any wait nor check for result availability or validity.
  231. *\sa VL6180x_RangeGetResult for "range only" measurement
  232. *
  233. * @param dev The device
  234. * @param pRangeData Pointer to the data structure to fill up
  235. * @return 0 on success
  236. */
  237. int VL6180x_RangeGetMeasurement(VL6180xDev_t dev, VL6180x_RangeData_t *pRangeData);
  238. /**
  239. * @brief Get ranging result and only that
  240. *
  241. * @par Function Description
  242. * Unlike @a VL6180x_RangeGetMeasurement() this function only retrieves the range in millimeter \n
  243. * It does any required up-scale translation\n
  244. * It can be called after success status polling or in interrupt mode \n
  245. * @warning these function is not doing wrap around filtering \n
  246. * This function doesn't perform any data ready check!
  247. *
  248. * @param dev The device
  249. * @param pRange_mm Pointer to range distance
  250. * @return 0 on success
  251. */
  252. int VL6180x_RangeGetResult(VL6180xDev_t dev, int32_t *pRange_mm);
  253. /**
  254. * @brief Configure ranging interrupt reported to application
  255. *
  256. * @param dev The device
  257. * @param ConfigGpioInt Select ranging report\n select one (and only one) of:\n
  258. * @a #CONFIG_GPIO_INTERRUPT_DISABLED \n
  259. * @a #CONFIG_GPIO_INTERRUPT_LEVEL_LOW \n
  260. * @a #CONFIG_GPIO_INTERRUPT_LEVEL_HIGH \n
  261. * @a #CONFIG_GPIO_INTERRUPT_OUT_OF_WINDOW \n
  262. * @a #CONFIG_GPIO_INTERRUPT_NEW_SAMPLE_READY
  263. * @return 0 on success
  264. */
  265. int VL6180x_RangeConfigInterrupt(VL6180xDev_t dev, uint8_t ConfigGpioInt);
  266. /**
  267. * @brief Clear range interrupt
  268. *
  269. * @param dev The device
  270. * @return 0 On success
  271. */
  272. #define VL6180x_RangeClearInterrupt(dev) VL6180x_ClearInterrupt(dev, INTERRUPT_CLEAR_RANGING)
  273. /**
  274. * @brief Return ranging error interrupt status
  275. *
  276. * @par Function Description
  277. * Appropriate Interrupt report must have been selected first by @a VL6180x_RangeConfigInterrupt() or @a VL6180x_Prepare() \n
  278. *
  279. * Can be used in polling loop to wait for a given ranging event or in interrupt to read the trigger \n
  280. * Events triggers are : \n
  281. * @a #RES_INT_STAT_GPIO_LOW_LEVEL_THRESHOLD \n
  282. * @a #RES_INT_STAT_GPIO_HIGH_LEVEL_THRESHOLD \n
  283. * @a #RES_INT_STAT_GPIO_OUT_OF_WINDOW \n (RES_INT_STAT_GPIO_LOW_LEVEL_THRESHOLD|RES_INT_STAT_GPIO_HIGH_LEVEL_THRESHOLD)
  284. * @a #RES_INT_STAT_GPIO_NEW_SAMPLE_READY \n
  285. *
  286. * @sa IntrStatus_t
  287. * @param dev The device
  288. * @param pIntStatus Pointer to status variable to update
  289. * @return 0 on success
  290. */
  291. int VL6180x_RangeGetInterruptStatus(VL6180xDev_t dev, uint8_t *pIntStatus);
  292. #if VL6180x_RANGE_STATUS_ERRSTRING
  293. extern const char *ROMABLE_DATA VL6180x_RangeStatusErrString[];
  294. /**
  295. * @brief Human readable error string for range error status
  296. *
  297. * @param RangeErrCode The error code as stored on @a VL6180x_RangeData_t::errorStatus
  298. * @return error string , NULL for invalid RangeErrCode
  299. * @sa ::RangeError_u
  300. */
  301. const char *VL6180x_RangeGetStatusErrString(uint8_t RangeErrCode);
  302. #else
  303. #define VL6180x_RangeGetStatusErrString(...) NULL
  304. #endif
  305. /** @} */
  306. #if VL6180x_ALS_SUPPORT
  307. /** @defgroup api_hl_als ALS functions
  308. * @brief ALS functions
  309. * @ingroup api_hl
  310. * @{
  311. */
  312. /**
  313. * @brief Run a single ALS measurement in single shot polling mode
  314. *
  315. * @par Function Description
  316. * Kick off a new single shot ALS then wait new measurement ready to retrieve it ( polling system interrupt status register for als) \n
  317. * ALS must be prepared by a first call to @a VL6180x_Prepare() \n
  318. * \n Should not be used in continuous or interrupt mode it will break it and create hazard in start/stop \n
  319. *
  320. * @param dev The device
  321. * @param pAlsData Als data structure to fill up @a VL6180x_AlsData_t
  322. * @return 0 on success
  323. */
  324. int VL6180x_AlsPollMeasurement(VL6180xDev_t dev, VL6180x_AlsData_t *pAlsData);
  325. /**
  326. * @brief Get actual ALS measurement
  327. *
  328. * @par Function Description
  329. * Can be called after success status polling or in interrupt mode to retrieve ALS measurement from device \n
  330. * This function doesn't perform any data ready check !
  331. *
  332. * @param dev The device
  333. * @param pAlsData Pointer to measurement struct @a VL6180x_AlsData_t
  334. * @return 0 on success
  335. */
  336. int VL6180x_AlsGetMeasurement(VL6180xDev_t dev, VL6180x_AlsData_t *pAlsData);
  337. /**
  338. * @brief Configure ALS interrupts provide to application
  339. *
  340. * @param dev The Device
  341. * @param ConfigGpioInt Select one (and only one) of : \n
  342. * @a #CONFIG_GPIO_INTERRUPT_DISABLED \n
  343. * @a #CONFIG_GPIO_INTERRUPT_LEVEL_LOW \n
  344. * @a #CONFIG_GPIO_INTERRUPT_LEVEL_HIGH \n
  345. * @a #CONFIG_GPIO_INTERRUPT_OUT_OF_WINDOW \n
  346. * @a #CONFIG_GPIO_INTERRUPT_NEW_SAMPLE_READY
  347. * @return 0 on success may return #INVALID_PARAMS for invalid mode
  348. */
  349. int VL6180x_AlsConfigInterrupt(VL6180xDev_t dev, uint8_t ConfigGpioInt);
  350. /**
  351. * @brief Set ALS integration period
  352. *
  353. * @param dev The device
  354. * @param period_ms Integration period in msec. Value in between 50 to 100 msec is recommended\n
  355. * @return 0 on success
  356. */
  357. int VL6180x_AlsSetIntegrationPeriod(VL6180xDev_t dev, uint16_t period_ms);
  358. /**
  359. * @brief Set ALS "inter-measurement period"
  360. *
  361. * @par Function Description
  362. * The so call data-sheet "inter measurement period" is actually an extra inter-measurement delay
  363. *
  364. * @param dev The device
  365. * @param intermeasurement_period_ms Inter measurement time in milli second\n
  366. * @warning applied value is clipped to 2550 ms\n
  367. * @return 0 on success if value is
  368. */
  369. int VL6180x_AlsSetInterMeasurementPeriod(VL6180xDev_t dev, uint16_t intermeasurement_period_ms);
  370. /**
  371. * @brief Set ALS analog gain code
  372. *
  373. * @par Function Description
  374. * ALS gain code value programmed in @a SYSALS_ANALOGUE_GAIN .
  375. * @param dev The device
  376. * @param gain Gain code see datasheet or AlsGainLookUp for real value. Value is clipped to 7.
  377. * @return 0 on success
  378. */
  379. int VL6180x_AlsSetAnalogueGain(VL6180xDev_t dev, uint8_t gain);
  380. /**
  381. * @brief Set thresholds for ALS continuous mode
  382. * @warning Threshold are raw device value not lux!
  383. *
  384. * @par Function Description
  385. * Basically value programmed in @a SYSALS_THRESH_LOW and @a SYSALS_THRESH_HIGH registers
  386. * @param dev The device
  387. * @param low ALS low raw threshold for @a SYSALS_THRESH_LOW
  388. * @param high ALS high raw threshold for @a SYSALS_THRESH_HIGH
  389. * @return 0 on success
  390. */
  391. int VL6180x_AlsSetThresholds(VL6180xDev_t dev, uint8_t low, uint8_t high);
  392. /**
  393. * @brief Clear ALS interrupt
  394. *
  395. * @param dev The device
  396. * @return 0 On success
  397. */
  398. #define VL6180x_AlsClearInterrupt(dev) VL6180x_ClearInterrupt(dev, INTERRUPT_CLEAR_ALS)
  399. /**
  400. * Read ALS interrupt status
  401. * @param dev Device
  402. * @param pIntStatus Pointer to status
  403. * @return 0 on success
  404. */
  405. int VL6180x_AlsGetInterruptStatus(VL6180xDev_t dev, uint8_t *pIntStatus);
  406. /** @} */
  407. #endif
  408. /** @defgroup api_ll_init Init functions
  409. * @brief Init functions
  410. * @ingroup api_ll
  411. * @{
  412. */
  413. /**
  414. * @brief Low level ranging and ALS register static settings (you should call @a VL6180x_Prepare() function instead)
  415. *
  416. * @param dev
  417. * @return 0 on success
  418. */
  419. int VL6180x_StaticInit(VL6180xDev_t dev);
  420. /** @} */
  421. /** @defgroup api_ll_range Ranging functions
  422. * @brief Ranging Low Level functions
  423. * @ingroup api_ll
  424. * @{
  425. */
  426. /**
  427. * @brief Wait for device to be ready (before a new ranging command can be issued by application)
  428. * @param dev The device
  429. * @param MaxLoop Max Number of i2c polling loop see @a #msec_2_i2cloop
  430. * @return 0 on success. <0 when fail \n
  431. * @ref VL6180x_ErrCode_t::TIME_OUT for time out \n
  432. * @ref VL6180x_ErrCode_t::INVALID_PARAMS if MaxLop<1
  433. */
  434. int VL6180x_RangeWaitDeviceReady(VL6180xDev_t dev, int MaxLoop);
  435. /**
  436. * @brief Program Inter measurement period (used only in continuous mode)
  437. *
  438. * @par Function Description
  439. * When trying to set too long time, it returns #INVALID_PARAMS
  440. *
  441. * @param dev The device
  442. * @param InterMeasTime_msec Requires inter-measurement time in msec
  443. * @return 0 on success
  444. */
  445. int VL6180x_RangeSetInterMeasPeriod(VL6180xDev_t dev, uint32_t InterMeasTime_msec);
  446. /**
  447. * @brief Set device ranging scaling factor
  448. *
  449. * @par Function Description
  450. * The ranging scaling factor is applied on the raw distance measured by the device to increase operating ranging at the price of the precision.
  451. * Changing the scaling factor when device is not in f/w standby state (free running) is not safe.
  452. * It can be source of spurious interrupt, wrongly scaled range etc ...
  453. * @warning __This function doesns't update high/low threshold and other programmed settings linked to scaling factor__.
  454. * To ensure proper operation, threshold and scaling changes should be done following this procedure: \n
  455. * @li Set Group hold : @a VL6180x_SetGroupParamHold() \n
  456. * @li Get Threshold @a VL6180x_RangeGetThresholds() \n
  457. * @li Change scaling : @a VL6180x_UpscaleSetScaling() \n
  458. * @li Set Threshold : @a VL6180x_RangeSetThresholds() \n
  459. * @li Unset Group Hold : @a VL6180x_SetGroupParamHold()
  460. *
  461. * @param dev The device
  462. * @param scaling Scaling factor to apply (1,2 or 3)
  463. * @return 0 on success when up-scale support is not configured it fail for any
  464. * scaling than the one statically configured.
  465. */
  466. int VL6180x_UpscaleSetScaling(VL6180xDev_t dev, uint8_t scaling);
  467. /**
  468. * @brief Get current ranging scaling factor
  469. *
  470. * @param dev The device
  471. * @return The current scaling factor
  472. */
  473. int VL6180x_UpscaleGetScaling(VL6180xDev_t dev);
  474. /**
  475. * @brief Give filtered state (wrap-around filter) of a range measurement
  476. * @param pRangeData Range measurement data
  477. * @return 0 means measure was not filtered, when not 0 range from device got filtered by filter post processing
  478. */
  479. #define VL6180x_RangeIsFilteredMeasurement(pRangeData) ((pRangeData)->errorStatus == RangingFiltered)
  480. /**
  481. * @brief Get the maximal distance for actual scaling
  482. * @par Function Description
  483. * Do not use prior to @a VL6180x_Prepare() or at least @a VL6180x_InitData()
  484. *
  485. * Any range value more than the value returned by this function is to be considered as "no target detected"
  486. * or "no target in detectable range"
  487. * @warning The maximal distance depends on the scaling
  488. *
  489. * @param dev The device
  490. * @return The maximal range limit for actual mode and scaling
  491. */
  492. uint16_t VL6180x_GetUpperLimit(VL6180xDev_t dev);
  493. /**
  494. * @brief Apply low and high ranging thresholds that are considered only in continuous mode
  495. *
  496. * @par Function Description
  497. * This function programs low and high ranging thresholds that are considered in continuous mode :
  498. * interrupt will be raised only when an object is detected at a distance inside this [low:high] range.
  499. * The function takes care of applying current scaling factor if any.\n
  500. * To be safe, in continuous operation, thresholds must be changed under "group parameter hold" cover.
  501. * Group hold can be activated/deactivated directly in the function or externally (then set 0)
  502. * using /a VL6180x_SetGroupParamHold() function.
  503. *
  504. * @param dev The device
  505. * @param low Low threshold in mm
  506. * @param high High threshold in mm
  507. * @param SafeHold Use of group parameters hold to surround threshold programming.
  508. * @return 0 On success
  509. */
  510. int VL6180x_RangeSetThresholds(VL6180xDev_t dev, uint16_t low, uint16_t high, int SafeHold);
  511. /**
  512. * @brief Get scaled high and low threshold from device
  513. *
  514. * @par Function Description
  515. * Due to scaling factor, the returned value may be different from what has been programmed first (precision lost).
  516. * For instance VL6180x_RangeSetThresholds(dev,11,22) with scale 3
  517. * will read back 9 ((11/3)x3) and 21 ((22/3)x3).
  518. * @param dev The device
  519. * @param low scaled low Threshold ptr can be NULL if not needed
  520. * @param high scaled High Threshold ptr can be NULL if not needed
  521. * @return 0 on success, return value is undefined if both low and high are NULL
  522. * @warning return value is undefined if both low and high are NULL
  523. */
  524. int VL6180x_RangeGetThresholds(VL6180xDev_t dev, uint16_t *low, uint16_t *high);
  525. /**
  526. * @brief Set ranging raw thresholds (scaling not considered so not recommended to use it)
  527. *
  528. * @param dev The device
  529. * @param low raw low threshold set to raw register
  530. * @param high raw high threshold set to raw register
  531. * @return 0 on success
  532. */
  533. int VL6180x_RangeSetRawThresholds(VL6180xDev_t dev, uint8_t low, uint8_t high);
  534. /**
  535. * @brief Set Early Convergence Estimate ratio
  536. * @par Function Description
  537. * For more information on ECE check datasheet
  538. * @warning May return a calibration warning in some use cases
  539. *
  540. * @param dev The device
  541. * @param FactorM ECE factor M in M/D
  542. * @param FactorD ECE factor D in M/D
  543. * @return 0 on success. <0 on error. >0 on warning
  544. */
  545. int VL6180x_RangeSetEceFactor(VL6180xDev_t dev, uint16_t FactorM, uint16_t FactorD);
  546. /**
  547. * @brief Set Early Convergence Estimate state (See #SYSRANGE_RANGE_CHECK_ENABLES register)
  548. * @param dev The device
  549. * @param enable State to be set 0=disabled, otherwise enabled
  550. * @return 0 on success
  551. */
  552. int VL6180x_RangeSetEceState(VL6180xDev_t dev, int enable);
  553. /**
  554. * @brief Set activation state of the wrap around filter
  555. * @param dev The device
  556. * @param state New activation state (0=off, otherwise on)
  557. * @return 0 on success
  558. */
  559. int VL6180x_FilterSetState(VL6180xDev_t dev, int state);
  560. /**
  561. * Get activation state of the wrap around filter
  562. * @param dev The device
  563. * @return Filter enabled or not, when filter is not supported it always returns 0S
  564. */
  565. int VL6180x_FilterGetState(VL6180xDev_t dev);
  566. /**
  567. * @brief Set activation state of DMax computation
  568. * @param dev The device
  569. * @param state New activation state (0=off, otherwise on)
  570. * @return 0 on success
  571. */
  572. int VL6180x_DMaxSetState(VL6180xDev_t dev, int state);
  573. /**
  574. * Get activation state of DMax computation
  575. * @param dev The device
  576. * @return Filter enabled or not, when filter is not supported it always returns 0S
  577. */
  578. int VL6180x_DMaxGetState(VL6180xDev_t dev);
  579. /**
  580. * @brief Set ranging mode and start/stop measure (use high level functions instead : @a VL6180x_RangeStartSingleShot() or @a VL6180x_RangeStartContinuousMode())
  581. *
  582. * @par Function Description
  583. * When used outside scope of known polling single shot stopped state, \n
  584. * user must ensure the device state is "idle" before to issue a new command.
  585. *
  586. * @param dev The device
  587. * @param mode A combination of working mode (#MODE_SINGLESHOT or #MODE_CONTINUOUS) and start/stop condition (#MODE_START_STOP) \n
  588. * @return 0 on success
  589. */
  590. int VL6180x_RangeSetSystemMode(VL6180xDev_t dev, uint8_t mode);
  591. /**
  592. * @brief Enable/disable range ignore feature
  593. *
  594. * @par Function Description
  595. * Enable range ignore feature to ensure that the device does not range on the cover glass because of cross-talk.
  596. * @a VL6180x_RangeIgnoreConfigure() should be run first to configure feature prior to enable it.
  597. *
  598. * @param dev The Device
  599. * @param EnableState Feature state to set 0= off else =on
  600. * @return 0 on success
  601. */
  602. int VL6180x_RangeIgnoreSetEnable(VL6180xDev_t dev, int EnableState);
  603. /**
  604. * @brief Configure Range ignore feature
  605. *
  606. * @par Function Description
  607. * When return signal rate is below the IgnoreThreshold and return distance is below the ValidHeight, the distance will be ignored
  608. * @warning It is recommended to enable range ignore feature and configure it only when device is in stop or idle state
  609. * @warning Once this function is called, next call to @a VL6180x_InitData() function without reseting the device will result in wrong ranging operation
  610. * @param dev The Device
  611. * @param ValidHeight_mm Valid height in mm (unscaled ie not raw value before scaling)
  612. * @param IgnoreThreshold Ignore threshold in fixpoint 9.7 MegaCount/sec
  613. * @return
  614. */
  615. int VL6180x_RangeIgnoreConfigure(VL6180xDev_t dev, uint16_t ValidHeight_mm, uint16_t IgnoreThreshold);
  616. /** @} */
  617. /** @defgroup api_ll_range_calibration Ranging calibration functions
  618. * @brief Ranging calibration functions
  619. * @ingroup api_ll
  620. * @{
  621. */
  622. /**
  623. * @brief Get part to part calibration offset
  624. *
  625. * @par Function Description
  626. * Should only be used after a successful call to @a VL6180x_InitData to backup device nvm value
  627. *
  628. * @param dev The device
  629. * @return part to part calibration offset from device
  630. */
  631. int8_t VL6180x_GetOffsetCalibrationData(VL6180xDev_t dev);
  632. /**
  633. * Set or over-write part to part calibration offset and apply it immediately
  634. * \sa VL6180x_InitData(), VL6180x_GetOffsetCalibrationData()
  635. * @param dev The device
  636. * @param offset Offset
  637. * @return 0 on success
  638. */
  639. int VL6180x_SetOffsetCalibrationData(VL6180xDev_t dev, int8_t offset);
  640. /**
  641. * @brief Set Cross talk compensation rate
  642. *
  643. * @par Function Description
  644. * It programs register @a #SYSRANGE_CROSSTALK_COMPENSATION_RATE
  645. *
  646. * @param dev The device
  647. * @param Rate Compensation rate (9.7 fix point) see datasheet for details
  648. * @return 0 on success
  649. */
  650. int VL6180x_SetXTalkCompensationRate(VL6180xDev_t dev, FixPoint97_t Rate);
  651. /** @} */
  652. #if VL6180x_ALS_SUPPORT
  653. /** @defgroup api_ll_als ALS functions
  654. * @brief ALS functions
  655. * @ingroup api_ll
  656. * @{
  657. */
  658. /**
  659. * @brief Wait for device to be ready for new als operation or max pollign loop (time out)
  660. * @param dev The device
  661. * @param MaxLoop Max Number of i2c polling loop see @a #msec_2_i2cloop
  662. * @return 0 on success. <0 when @a VL6180x_ErrCode_t::TIME_OUT if timed out
  663. */
  664. int VL6180x_AlsWaitDeviceReady(VL6180xDev_t dev, int MaxLoop);
  665. /**
  666. * @brief Set ALS system mode and start/stop measure
  667. *
  668. * @warning When used outside after single shot polling, \n
  669. * User must ensure the device state is ready before issuing a new command (using @a VL6180x_AlsWaitDeviceReady()). \n
  670. * Non respect of this, can cause loss of interrupt or device hanging.
  671. *
  672. * @param dev The device
  673. * @param mode A combination of working mode (#MODE_SINGLESHOT or #MODE_CONTINUOUS) and start condition (#MODE_START_STOP) \n
  674. * @return 0 on success
  675. */
  676. int VL6180x_AlsSetSystemMode(VL6180xDev_t dev, uint8_t mode);
  677. /** @} */
  678. #endif
  679. /** @defgroup api_ll_misc Misc functions
  680. * @brief Misc functions
  681. * @ingroup api_ll
  682. * @{
  683. */
  684. /**
  685. * Set Group parameter Hold state
  686. *
  687. * @par Function Description
  688. * Group parameter holds @a #SYSTEM_GROUPED_PARAMETER_HOLD enable safe update (non atomic across multiple measure) by host
  689. * \n The critical register group is composed of: \n
  690. * #SYSTEM_INTERRUPT_CONFIG_GPIO \n
  691. * #SYSRANGE_THRESH_HIGH \n
  692. * #SYSRANGE_THRESH_LOW \n
  693. * #SYSALS_INTEGRATION_PERIOD \n
  694. * #SYSALS_ANALOGUE_GAIN \n
  695. * #SYSALS_THRESH_HIGH \n
  696. * #SYSALS_THRESH_LOW
  697. *
  698. *
  699. * @param dev The device
  700. * @param Hold Group parameter Hold state to be set (on/off)
  701. * @return 0 on success
  702. */
  703. int VL6180x_SetGroupParamHold(VL6180xDev_t dev, int Hold);
  704. /**
  705. * @brief Set new device i2c address
  706. *
  707. * After completion the device will answer to the new address programmed.
  708. *
  709. * @sa AN4478: Using multiple VL6180X's in a single design
  710. * @param dev The device
  711. * @param NewAddr The new i2c address (8 bits)
  712. * @return 0 on success
  713. */
  714. int VL6180x_SetI2CAddress(VL6180xDev_t dev, uint8_t NewAddr);
  715. /**
  716. * @brief Fully configure gpio 0/1 pin : polarity and functionality
  717. *
  718. * @param dev The device
  719. * @param pin gpio pin 0 or 1
  720. * @param IntFunction Pin functionality : either #GPIOx_SELECT_OFF or #GPIOx_SELECT_GPIO_INTERRUPT_OUTPUT (refer to #SYSTEM_MODE_GPIO1 register definition)
  721. * @param ActiveHigh Set active high polarity, or active low see @a ::IntrPol_e
  722. * @return 0 on success
  723. */
  724. int VL6180x_SetupGPIOx(VL6180xDev_t dev, int pin, uint8_t IntFunction, int ActiveHigh);
  725. /**
  726. * @brief Set interrupt pin polarity for the given GPIO
  727. *
  728. * @param dev The device
  729. * @param pin Pin 0 or 1
  730. * @param active_high select active high or low polarity using @ref IntrPol_e
  731. * @return 0 on success
  732. */
  733. int VL6180x_SetGPIOxPolarity(VL6180xDev_t dev, int pin, int active_high);
  734. /**
  735. * Select interrupt functionality for the given GPIO
  736. *
  737. * @par Function Description
  738. * Functionality refer to @a SYSTEM_MODE_GPIO0
  739. *
  740. * @param dev The device
  741. * @param pin Pin to configure 0 or 1 (gpio0 or gpio1)\nNote that gpio0 is chip enable at power up !
  742. * @param functionality Pin functionality : either #GPIOx_SELECT_OFF or #GPIOx_SELECT_GPIO_INTERRUPT_OUTPUT (refer to #SYSTEM_MODE_GPIO1 register definition)
  743. * @return 0 on success
  744. */
  745. int VL6180x_SetGPIOxFunctionality(VL6180xDev_t dev, int pin, uint8_t functionality);
  746. /**
  747. * #brief Disable and turn to Hi-Z gpio output pin
  748. *
  749. * @param dev The device
  750. * @param pin The pin number to disable 0 or 1
  751. * @return 0 on success
  752. */
  753. int VL6180x_DisableGPIOxOut(VL6180xDev_t dev, int pin);
  754. /**
  755. * @def msec_2_i2cloop
  756. * @brief Number of I2C polling loop (an 8 bit register) to run for maximal wait time.
  757. *
  758. * @par Function Description
  759. * When polling via I2C the overall time is mainly the I2C transaction time because it is a slow bus
  760. * one 8 bit register poll on I2C bus timing is shown below: \n
  761. * start + addr_w(a) + 2x8bit index(a) + stop + start + addr_rd(a) + 1x8bit data_rd(a) + stop \n
  762. * 1 8 1 2*(8+1) 1 1 8 1 8 1 1 \n
  763. * so 49 serial bits
  764. *
  765. * @param time_ms Time to wait in milli second 10
  766. * @param i2c_khz I2C bus frequencies in KHz for instance 400
  767. * @return The number of loops (at least 1)
  768. */
  769. #define msec_2_i2cloop(time_ms, i2c_khz) (((time_ms) * (i2c_khz) / 49) + 1)
  770. /** @} */
  771. /**
  772. * polarity use in @a VL6180x_SetupGPIOx() , @a VL6180x_SetupGPIO1()
  773. */
  774. typedef enum {
  775. INTR_POL_LOW = 0, /*!< set active low polarity best setup for falling edge */
  776. INTR_POL_HIGH = 1, /*!< set active high polarity best setup for rising edge */
  777. } IntrPol_e;
  778. /** @defgroup api_ll_intr Interrupts management functions
  779. * @brief Interrupts management functions
  780. * @ingroup api_ll
  781. * @{
  782. */
  783. /**
  784. * @brief Get all interrupts cause
  785. *
  786. * @param dev The device
  787. * @param status Ptr to interrupt status. You can use @a IntrStatus_t::val
  788. * @return 0 on success
  789. */
  790. int VL6180x_GetInterruptStatus(VL6180xDev_t dev, uint8_t *status);
  791. /**
  792. * @brief Clear given system interrupt condition
  793. *
  794. * @par Function Description
  795. * Clear given interrupt cause by writing into register #SYSTEM_INTERRUPT_CLEAR register.
  796. * @param dev The device
  797. * @param IntClear Which interrupt source to clear. Use any combinations of #INTERRUPT_CLEAR_RANGING , #INTERRUPT_CLEAR_ALS , #INTERRUPT_CLEAR_ERROR.
  798. * @return 0 On success
  799. */
  800. int VL6180x_ClearInterrupt(VL6180xDev_t dev, uint8_t IntClear);
  801. /**
  802. * @brief Clear error interrupt
  803. *
  804. * @param dev The device
  805. * @return 0 On success
  806. */
  807. #define VL6180x_ClearErrorInterrupt(dev) VL6180x_ClearInterrupt(dev, INTERRUPT_CLEAR_ERROR)
  808. /**
  809. * @brief Clear All interrupt causes (als+range+error)
  810. *
  811. * @param dev The device
  812. * @return 0 On success
  813. */
  814. #define VL6180x_ClearAllInterrupt(dev) VL6180x_ClearInterrupt(dev, INTERRUPT_CLEAR_ERROR|INTERRUPT_CLEAR_RANGING|INTERRUPT_CLEAR_ALS)
  815. /** @} */
  816. /** @defgroup api_reg API Register access functions
  817. * @brief Registers access functions called by API core functions
  818. * @ingroup api_ll
  819. * @{
  820. */
  821. /**
  822. * Write VL6180x single byte register
  823. * @param dev The device
  824. * @param index The register index
  825. * @param data 8 bit register data
  826. * @return success
  827. */
  828. int VL6180x_WrByte(VL6180xDev_t dev, uint16_t index, uint8_t data);
  829. /**
  830. * Thread safe VL6180x Update (rd/modify/write) single byte register
  831. *
  832. * Final_reg = (Initial_reg & and_data) |or_data
  833. *
  834. * @param dev The device
  835. * @param index The register index
  836. * @param AndData 8 bit and data
  837. * @param OrData 8 bit or data
  838. * @return 0 on success
  839. */
  840. int VL6180x_UpdateByte(VL6180xDev_t dev, uint16_t index, uint8_t AndData, uint8_t OrData);
  841. /**
  842. * Write VL6180x word register
  843. * @param dev The device
  844. * @param index The register index
  845. * @param data 16 bit register data
  846. * @return 0 on success
  847. */
  848. int VL6180x_WrWord(VL6180xDev_t dev, uint16_t index, uint16_t data);
  849. /**
  850. * Write VL6180x double word (4 byte) register
  851. * @param dev The device
  852. * @param index The register index
  853. * @param data 32 bit register data
  854. * @return 0 on success
  855. */
  856. int VL6180x_WrDWord(VL6180xDev_t dev, uint16_t index, uint32_t data);
  857. /**
  858. * Read VL6180x single byte register
  859. * @param dev The device
  860. * @param index The register index
  861. * @param data pointer to 8 bit data
  862. * @return 0 on success
  863. */
  864. int VL6180x_RdByte(VL6180xDev_t dev, uint16_t index, uint8_t *data);
  865. /**
  866. * Read VL6180x word (2byte) register
  867. * @param dev The device
  868. * @param index The register index
  869. * @param data pointer to 16 bit data
  870. * @return 0 on success
  871. */
  872. int VL6180x_RdWord(VL6180xDev_t dev, uint16_t index, uint16_t *data);
  873. /**
  874. * Read VL6180x dword (4byte) register
  875. * @param dev The device
  876. * @param index The register index
  877. * @param data pointer to 32 bit data
  878. * @return 0 on success
  879. */
  880. int VL6180x_RdDWord(VL6180xDev_t dev, uint16_t index, uint32_t *data);
  881. /**
  882. * Read VL6180x multiple bytes
  883. * @note required only if #VL6180x_HAVE_MULTI_READ is set
  884. * @param dev The device
  885. * @param index The register index
  886. * @param data pointer to 8 bit data
  887. * @param nData number of data bytes to read
  888. * @return 0 on success
  889. */
  890. int VL6180x_RdMulti(VL6180xDev_t dev, uint16_t index, uint8_t *data, int nData);
  891. /** @} */
  892. #ifdef __cplusplus
  893. }
  894. #endif
  895. #endif /* VL6180x_API_H_ */