vl53l1_core.h 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  1. /*
  2. * Copyright (c) 2017, STMicroelectronics - All Rights Reserved
  3. *
  4. * This file is part of VL53L1 Core and is dual licensed,
  5. * either 'STMicroelectronics
  6. * Proprietary license'
  7. * or 'BSD 3-clause "New" or "Revised" License' , at your option.
  8. *
  9. ********************************************************************************
  10. *
  11. * 'STMicroelectronics Proprietary license'
  12. *
  13. ********************************************************************************
  14. *
  15. * License terms: STMicroelectronics Proprietary in accordance with licensing
  16. * terms at www.st.com/sla0081
  17. *
  18. * STMicroelectronics confidential
  19. * Reproduction and Communication of this document is strictly prohibited unless
  20. * specifically authorized in writing by STMicroelectronics.
  21. *
  22. *
  23. ********************************************************************************
  24. *
  25. * Alternatively, VL53L1 Core may be distributed under the terms of
  26. * 'BSD 3-clause "New" or "Revised" License', in which case the following
  27. * provisions apply instead of the ones mentioned above :
  28. *
  29. ********************************************************************************
  30. *
  31. * License terms: BSD 3-clause "New" or "Revised" License.
  32. *
  33. * Redistribution and use in source and binary forms, with or without
  34. * modification, are permitted provided that the following conditions are met:
  35. *
  36. * 1. Redistributions of source code must retain the above copyright notice, this
  37. * list of conditions and the following disclaimer.
  38. *
  39. * 2. Redistributions in binary form must reproduce the above copyright notice,
  40. * this list of conditions and the following disclaimer in the documentation
  41. * and/or other materials provided with the distribution.
  42. *
  43. * 3. Neither the name of the copyright holder nor the names of its contributors
  44. * may be used to endorse or promote products derived from this software
  45. * without specific prior written permission.
  46. *
  47. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  48. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  49. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  50. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  51. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  52. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  53. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  54. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  55. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  56. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  57. *
  58. *
  59. ********************************************************************************
  60. *
  61. */
  62. /**
  63. * @file vl53l1_core.h
  64. *
  65. * @brief EwokPlus25 core function definitions
  66. */
  67. #ifndef _VL53L1_CORE_H_
  68. #define _VL53L1_CORE_H_
  69. #include "vl53l1_platform.h"
  70. #include "vl53l1_core_support.h"
  71. #ifdef __cplusplus
  72. extern "C" {
  73. #endif
  74. /**
  75. * @brief Initialise version info in pdev
  76. *
  77. * @param[out] Dev : Device handle
  78. */
  79. void VL53L1_init_version(
  80. VL53L1_DEV Dev);
  81. /**
  82. * @brief Initialise LL Driver State
  83. *
  84. * @param[out] Dev : Device handle
  85. * @param[in] ll_state : Device state
  86. */
  87. void VL53L1_init_ll_driver_state(
  88. VL53L1_DEV Dev,
  89. VL53L1_DeviceState ll_state);
  90. /**
  91. * @brief Update LL Driver Read State
  92. *
  93. * @param[out] Dev : Device handle
  94. *
  95. * @return VL53L1_ERROR_NONE Success
  96. * @return "Other error code" See ::VL53L1_Error
  97. */
  98. VL53L1_Error VL53L1_update_ll_driver_rd_state(
  99. VL53L1_DEV Dev);
  100. /**
  101. * @brief Checks if the LL Driver Read state and expected stream count
  102. * matches the state and stream count received from the device
  103. *
  104. * @param[out] Dev : Device handle
  105. *
  106. * @return VL53L1_ERROR_NONE Success
  107. * @return "Other error code" See ::VL53L1_Error
  108. */
  109. VL53L1_Error VL53L1_check_ll_driver_rd_state(
  110. VL53L1_DEV Dev);
  111. /**
  112. * @brief Update LL Driver Configuration State
  113. *
  114. * @param[out] Dev : Device handle
  115. *
  116. * @return VL53L1_ERROR_NONE Success
  117. * @return "Other error code" See ::VL53L1_Error
  118. */
  119. VL53L1_Error VL53L1_update_ll_driver_cfg_state(
  120. VL53L1_DEV Dev);
  121. /**
  122. * @brief Convenience function to copy return SPAD enables to buffer
  123. *
  124. * @param[in ] pdata : pointer to VL53L1_nvm_copy_data_t
  125. * @param[out] pbuffer : pointer to buffer
  126. */
  127. void VL53L1_copy_rtn_good_spads_to_buffer(
  128. VL53L1_nvm_copy_data_t *pdata,
  129. uint8_t *pbuffer);
  130. /**
  131. * @brief Initialise system results structure to all ones
  132. *
  133. * This mimics what the device firmware does the the results registers
  134. * at the start of each range
  135. *
  136. * @param[out] pdata : pointer to VL53L1_system_results_t
  137. */
  138. void VL53L1_init_system_results(
  139. VL53L1_system_results_t *pdata);
  140. /**
  141. * @brief Initialise zone dynamic config DSS control values
  142. *
  143. * @param[in] Dev : Device handler
  144. */
  145. void V53L1_init_zone_dss_configs(
  146. VL53L1_DEV Dev);
  147. /**
  148. * @brief Encodes a uint16_t register value into an I2C write buffer
  149. *
  150. * The register is encoded MS byte first is the buffer i.e as per the device register map
  151. *
  152. * @param[in] ip_value : input uint16_t value
  153. * @param[in] count : register size of in bytes (1, 2, 3 or 4)
  154. * @param[out] pbuffer : uint8_t pointer to the I2C write buffer
  155. */
  156. void VL53L1_i2c_encode_uint16_t(
  157. uint16_t ip_value,
  158. uint16_t count,
  159. uint8_t *pbuffer);
  160. /**
  161. * @brief Decodes a uint16_t register value from an I2C read buffer
  162. *
  163. * The I2C read buffer is assumed to be MS byte first i.e. matching the device register map
  164. *
  165. * @param[in] count : register size of in bytes (1, 2, 3 or 4)
  166. * @param[in] pbuffer : uint8_t pointer to the I2C read buffer
  167. *
  168. * @return value : decoded uint16_t value
  169. */
  170. uint16_t VL53L1_i2c_decode_uint16_t(
  171. uint16_t count,
  172. uint8_t *pbuffer);
  173. /**
  174. * @brief Encodes a int16_t register value into an I2C write buffer
  175. *
  176. * The register is encoded MS byte first is the buffer i.e as per the device register map
  177. *
  178. * @param[in] ip_value : input int16_t value
  179. * @param[in] count : register size of in bytes (1, 2, 3 or 4)
  180. * @param[out] pbuffer : uint8_t pointer to the I2C write buffer
  181. */
  182. void VL53L1_i2c_encode_int16_t(
  183. int16_t ip_value,
  184. uint16_t count,
  185. uint8_t *pbuffer);
  186. /**
  187. * @brief Decodes a int16_t register value from an I2C read buffer
  188. *
  189. * The I2C read buffer is assumed to be MS byte first i.e. matching the device register map
  190. *
  191. * @param[in] count : register size of in bytes (1, 2, 3 or 4)
  192. * @param[in] pbuffer : uint8_t pointer to the I2C read buffer
  193. *
  194. * @return value : decoded int16_t value
  195. */
  196. int16_t VL53L1_i2c_decode_int16_t(
  197. uint16_t count,
  198. uint8_t *pbuffer);
  199. /**
  200. * @brief Encodes a uint32_t register value into an I2C write buffer
  201. *
  202. * The register is encoded MS byte first is the buffer i.e as per the device register map
  203. *
  204. * @param[in] ip_value : input uint32_t value
  205. * @param[in] count : register size of in bytes (1, 2, 3 or 4)
  206. * @param[out] pbuffer : uint8_t pointer to the I2C write buffer
  207. */
  208. void VL53L1_i2c_encode_uint32_t(
  209. uint32_t ip_value,
  210. uint16_t count,
  211. uint8_t *pbuffer);
  212. /**
  213. * @brief Decodes a uint32_t register value from an I2C read buffer
  214. *
  215. * The I2C read buffer is assumed to be MS byte first i.e. matching the device register map
  216. *
  217. * @param[in] count : register size of in bytes (1, 2, 3 or 4)
  218. * @param[in] pbuffer : uint8_t pointer to the I2C read buffer
  219. *
  220. * @return value : decoded uint32_t value
  221. */
  222. uint32_t VL53L1_i2c_decode_uint32_t(
  223. uint16_t count,
  224. uint8_t *pbuffer);
  225. /**
  226. * @brief Decodes an integer register value from an I2C read buffer
  227. *
  228. * The I2C read buffer is assumed to be MS byte first i.e. matching the device register map
  229. *
  230. * @param[in] count : integer size of in bytes (1, 2, 3 or 4)
  231. * @param[in] pbuffer : uint8_t pointer to the I2C read buffer
  232. * @param[in] bit_mask : bit mask to apply
  233. * @param[in] down_shift : down shift to apply
  234. * @param[in] offset : offset to apply after the down shift
  235. *
  236. * @return value : decoded integer value
  237. */
  238. uint32_t VL53L1_i2c_decode_with_mask(
  239. uint16_t count,
  240. uint8_t *pbuffer,
  241. uint32_t bit_mask,
  242. uint32_t down_shift,
  243. uint32_t offset);
  244. /**
  245. * @brief Encodes a int32_t register value into an I2C write buffer
  246. *
  247. * The register is encoded MS byte first is the buffer i.e as per the device register map
  248. *
  249. * @param[in] ip_value : input int32_t value
  250. * @param[in] count : register size of in bytes (1, 2, 3 or 4)
  251. * @param[out] pbuffer : uint8_t pointer to the I2C write buffer
  252. */
  253. void VL53L1_i2c_encode_int32_t(
  254. int32_t ip_value,
  255. uint16_t count,
  256. uint8_t *pbuffer);
  257. /**
  258. * @brief Decodes a int32_t register value from an I2C read buffer
  259. *
  260. * The I2C read buffer is assumed to be MS byte first i.e. matching the device register map
  261. *
  262. * @param[in] count : register size of in bytes (1, 2, 3 or 4)
  263. * @param[in] pbuffer : uint8_t pointer to the I2C read buffer
  264. *
  265. * @return value : decoded int32_t value
  266. */
  267. int32_t VL53L1_i2c_decode_int32_t(
  268. uint16_t count,
  269. uint8_t *pbuffer);
  270. /**
  271. * @brief Triggers the start of the provided test_mode.
  272. *
  273. * @param[in] Dev : Device handle
  274. * @param[in] test_mode__ctrl : VL53L1_TEST_MODE__CTRL register value
  275. *
  276. * @return VL53L1_ERROR_NONE Success
  277. * @return "Other error code" See ::VL53L1_Error
  278. */
  279. #ifndef VL53L1_NOCALIB
  280. VL53L1_Error VL53L1_start_test(
  281. VL53L1_DEV Dev,
  282. uint8_t test_mode__ctrl);
  283. #endif
  284. /**
  285. * @brief Set firmware enable register
  286. *
  287. * Wrapper for setting power force register state
  288. * Also updates pdev->sys_ctrl.firmware__enable
  289. *
  290. * @param[in] Dev : Device handle
  291. * @param[in] value : register value
  292. *
  293. * @return VL53L1_ERROR_NONE Success
  294. * @return "Other error code" See ::VL53L1_Error
  295. */
  296. VL53L1_Error VL53L1_set_firmware_enable_register(
  297. VL53L1_DEV Dev,
  298. uint8_t value);
  299. /**
  300. * @brief Enables MCU firmware
  301. *
  302. * @param[in] Dev : Device handle
  303. *
  304. * @return VL53L1_ERROR_NONE Success
  305. * @return "Other error code" See ::VL53L1_Error
  306. */
  307. VL53L1_Error VL53L1_enable_firmware(
  308. VL53L1_DEV Dev);
  309. /**
  310. * @brief Disables MCU firmware
  311. *
  312. * This required to write to MCU G02, G01 registers and access the patch RAM
  313. *
  314. * @param[in] Dev : Device handle
  315. *
  316. * @return VL53L1_ERROR_NONE Success
  317. * @return "Other error code" See ::VL53L1_Error
  318. */
  319. VL53L1_Error VL53L1_disable_firmware(
  320. VL53L1_DEV Dev);
  321. /**
  322. * @brief Set power force register
  323. *
  324. * Wrapper for setting power force register state
  325. * Also updates pdev->sys_ctrl.power_management__go1_power_force
  326. *
  327. * @param[in] Dev : Device handle
  328. * @param[in] value : register value
  329. *
  330. * @return VL53L1_ERROR_NONE Success
  331. * @return "Other error code" See ::VL53L1_Error
  332. */
  333. VL53L1_Error VL53L1_set_powerforce_register(
  334. VL53L1_DEV Dev,
  335. uint8_t value);
  336. /**
  337. * @brief Enables power force
  338. *
  339. * This prevents the G01 and patch RAM from powering down between
  340. * ranges to enable reading of G01 and patch RAM data
  341. *
  342. * Calls VL53L1_set_powerforce_register()
  343. *
  344. * @param[in] Dev : Device handle
  345. *
  346. * @return VL53L1_ERROR_NONE Success
  347. * @return "Other error code" See ::VL53L1_Error
  348. */
  349. VL53L1_Error VL53L1_enable_powerforce(
  350. VL53L1_DEV Dev);
  351. /**
  352. * @brief Disables power force
  353. *
  354. * @param[in] Dev : Device handle
  355. *
  356. * Disable power force
  357. *
  358. * Calls VL53L1_set_powerforce_register()
  359. *
  360. * @return VL53L1_ERROR_NONE Success
  361. * @return "Other error code" See ::VL53L1_Error
  362. */
  363. VL53L1_Error VL53L1_disable_powerforce(
  364. VL53L1_DEV Dev);
  365. /**
  366. * @brief Clears Ranging Interrupt
  367. *
  368. * @param[in] Dev : Device handle
  369. *
  370. * @return VL53L1_ERROR_NONE Success
  371. * @return "Other error code" See ::VL53L1_Error
  372. */
  373. VL53L1_Error VL53L1_clear_interrupt(
  374. VL53L1_DEV Dev);
  375. /**
  376. * @brief Forces shadow stream count to zero
  377. *
  378. * @param[in] Dev : Device handle
  379. *
  380. * @return VL53L1_ERROR_NONE Success
  381. * @return "Other error code" See ::VL53L1_Error
  382. */
  383. #ifdef VL53L1_DEBUG
  384. VL53L1_Error VL53L1_force_shadow_stream_count_to_zero(
  385. VL53L1_DEV Dev);
  386. #endif
  387. /**
  388. * @brief Calculates the macro period in [us] for the input
  389. * fast_osc_frequency and VCSEL period register value
  390. *
  391. * Processing steps:
  392. *
  393. * 1. PLL period from fast_osc_frequency
  394. * 2. VCSEL period
  395. * 3. Macro period
  396. *
  397. * @param[in] fast_osc_frequency : fast oscillator frequency in 4.12 MHz format
  398. * @param[in] vcsel_period : VCSEL period register value
  399. *
  400. * @return macro_period_us : macro period in [us] 12.12 format
  401. */
  402. uint32_t VL53L1_calc_macro_period_us(
  403. uint16_t fast_osc_frequency,
  404. uint8_t vcsel_period);
  405. /**
  406. * @brief Calculates the Xtalk Range Ignore Threshold
  407. * rate per spad in 3.13Mcps
  408. *
  409. * This is done by calculating the worst case xtalk seen on the array
  410. * and applying a user specified fractional multiplier.
  411. *
  412. * @param[in] central_rate : xtalk central rate in 9.9u Kcps format
  413. * @param[in] x_gradient : xtalk xgradient rate in 4.11s Kcps format
  414. * @param[in] y_gradient : xtalk ygradient rate in 4.11s Kcps format
  415. * @param[in] rate_mult : rate multiplier in 2.5 fractional format
  416. *
  417. * @return range_ignore_threshold_kcps : rate per spad in mcps 3.13
  418. */
  419. uint16_t VL53L1_calc_range_ignore_threshold(
  420. uint32_t central_rate,
  421. int16_t x_gradient,
  422. int16_t y_gradient,
  423. uint8_t rate_mult);
  424. /**
  425. * @brief Calculates the timeout value in macro period based on the input
  426. * timeout period in milliseconds and the macro period in us
  427. *
  428. * @param[in] timeout_us : timeout period in microseconds
  429. * @param[in] macro_period_us : macro period in microseconds 12.12
  430. *
  431. * @return timeout_mclks : timeout in macro periods
  432. */
  433. uint32_t VL53L1_calc_timeout_mclks(
  434. uint32_t timeout_us,
  435. uint32_t macro_period_us);
  436. /**
  437. * @brief Calculates the encoded timeout register value based on the input
  438. * timeout period in milliseconds and the macro period in us
  439. *
  440. * @param[in] timeout_us : timeout period in microseconds
  441. * @param[in] macro_period_us : macro period in microseconds 12.12
  442. *
  443. * @return timeout_encoded : encoded timeout register value
  444. */
  445. uint16_t VL53L1_calc_encoded_timeout(
  446. uint32_t timeout_us,
  447. uint32_t macro_period_us);
  448. /**
  449. * @brief Calculates the timeout in us based on the input
  450. * timeout im macro periods value and the macro period in us
  451. *
  452. * @param[in] timeout_mclks : timeout im macro periods
  453. * @param[in] macro_period_us : macro period in microseconds 12.12
  454. *
  455. * @return timeout_us : encoded timeout register value
  456. */
  457. uint32_t VL53L1_calc_timeout_us(
  458. uint32_t timeout_mclks,
  459. uint32_t macro_period_us);
  460. /**
  461. * @brief Calculates the decoded timeout in us based on the input
  462. * encoded timeout register value and the macro period in us
  463. *
  464. * @param[in] timeout_encoded : encoded timeout register value
  465. * @param[in] macro_period_us : macro period in microseconds 12.12
  466. *
  467. * @return timeout_us : encoded timeout register value
  468. */
  469. uint32_t VL53L1_calc_decoded_timeout_us(
  470. uint16_t timeout_encoded,
  471. uint32_t macro_period_us);
  472. /**
  473. * @brief Encode timeout in (LSByte * 2^MSByte) + 1 register format.
  474. *
  475. * @param[in] timeout_mclks : uint32_t timeout value (macro periods)
  476. *
  477. * @return encoded_timeout : 16-bit encoded value
  478. */
  479. uint16_t VL53L1_encode_timeout(
  480. uint32_t timeout_mclks);
  481. /**
  482. * @brief Decode 16-bit timeout register value.
  483. *
  484. * @param[in] encoded_timeout : 16-bit timeout register value
  485. *
  486. * @return timeout_macro_clks : uint32_t decoded value
  487. *
  488. */
  489. uint32_t VL53L1_decode_timeout(
  490. uint16_t encoded_timeout);
  491. /**
  492. * @brief Converts the input MM and range timeouts in [us]
  493. * into the appropriate register values
  494. *
  495. * Must also be run after the VCSEL period settings are changed
  496. *
  497. * @param[in] phasecal_config_timeout_us : requested Phase Cal Timeout e.g. 1000us
  498. * @param[in] mm_config_timeout_us : requested MM Timeout e.g. 2000us
  499. * @param[in] range_config_timeout_us : requested Ranging Timeout e.g 10000us
  500. * @param[in] fast_osc_frequency : fast oscillator frequency in 4.12 MHz format
  501. * @param[out] pgeneral : general data struct
  502. * @param[out] ptiming : timing data struct with input vcsel period data
  503. *
  504. * @return VL53L1_ERROR_NONE Success
  505. * @return "Other error code" See ::VL53L1_Error
  506. */
  507. VL53L1_Error VL53L1_calc_timeout_register_values(
  508. uint32_t phasecal_config_timeout_us,
  509. uint32_t mm_config_timeout_us,
  510. uint32_t range_config_timeout_us,
  511. uint16_t fast_osc_frequency,
  512. VL53L1_general_config_t *pgeneral,
  513. VL53L1_timing_config_t *ptiming);
  514. /**
  515. * @brief Encodes the real period in PLL clocks into the register value
  516. *
  517. * @param[in] vcsel_period_pclks : 8-bit value
  518. *
  519. * @return vcsel_period_reg : 8-bit encoded value
  520. *
  521. */
  522. uint8_t VL53L1_encode_vcsel_period(
  523. uint8_t vcsel_period_pclks);
  524. /**
  525. * @brief Decodes an unsigned integer from a uint8_t buffer 16-bit, 24-bit or 32-bit integers.
  526. *
  527. * Assumes MS Byte first
  528. *
  529. * @param[in] pbuffer : pointer to start of integer uint8_t data
  530. * @param[in] no_of_bytes : size of integer in bytes
  531. *
  532. * @return decoded_value : decoded integer value
  533. *
  534. */
  535. uint32_t VL53L1_decode_unsigned_integer(
  536. uint8_t *pbuffer,
  537. uint8_t no_of_bytes);
  538. /**
  539. * @brief Encodes an unsigned integer into a uint8_t buffer MS byte first
  540. *
  541. * @param[in] ip_value : input unsigned integer
  542. * @param[in] no_of_bytes : size of integer storage in bytes
  543. * @param[out] pbuffer : pointer to output buffer
  544. *
  545. */
  546. void VL53L1_encode_unsigned_integer(
  547. uint32_t ip_value,
  548. uint8_t no_of_bytes,
  549. uint8_t *pbuffer);
  550. /**
  551. * @brief Get the SPAD number, byte index (0-31) and bit index (0-7) for
  552. *
  553. * Takes the map index (0 - 255) and calculated the SPAD number, byte index
  554. * within the SPAD enable byte array and but position within the SPAD enable
  555. * byte
  556. *
  557. *
  558. * @param[in] spad_number : spad number
  559. * @param[out] pbyte_index : pointer to output 0-31 byte index for SPAD enables
  560. * @param[out] pbit_index : pointer to output 0-7 bit index
  561. * @param[out] pbit_mask : pointer to output bit mask for the byte
  562. *
  563. */
  564. void VL53L1_spad_number_to_byte_bit_index(
  565. uint8_t spad_number,
  566. uint8_t *pbyte_index,
  567. uint8_t *pbit_index,
  568. uint8_t *pbit_mask);
  569. /**
  570. * @brief Encodes a (col,row) coord value into ByteIndex.BitIndex format
  571. *
  572. *
  573. * @param[in] row : Row
  574. * @param[in] col : Column
  575. * @param[out] pspad_number : Encoded Coord in Byte.Bit format
  576. *
  577. */
  578. void VL53L1_encode_row_col(
  579. uint8_t row,
  580. uint8_t col,
  581. uint8_t *pspad_number);
  582. /**
  583. * @brief Decodes encoded zone size format into width and height values
  584. *
  585. * @param[in] encoded_xy_size : Encoded zone size
  586. * @param[out] pwidth : Decoded zone width
  587. * @param[out] pheight : Decoded zone height
  588. *
  589. */
  590. void VL53L1_decode_zone_size(
  591. uint8_t encoded_xy_size,
  592. uint8_t *pwidth,
  593. uint8_t *pheight);
  594. /**
  595. * @brief Encodes a zone width & height into encoded size format
  596. *
  597. * @param[in] width : Zone Width
  598. * @param[in] height : Zone height
  599. * @param[out] pencoded_xy_size : Encoded zone size
  600. *
  601. */
  602. void VL53L1_encode_zone_size(
  603. uint8_t width,
  604. uint8_t height,
  605. uint8_t *pencoded_xy_size);
  606. /**
  607. * @brief Decodes encoded zone info into min/max limits
  608. *
  609. * Note both the lower left and upper right coordinated lie within
  610. * the zone (inclusive)
  611. *
  612. * @param[in] encoded_xy_centre : Encoded zone centre (spad number)
  613. * @param[in] encoded_xy_size : Encoded zone size
  614. * @param[out] px_ll : Decoded zone lower left x coord
  615. * @param[out] py_ll : Decoded zone lower left y coord
  616. * @param[out] px_ur : Decoded zone upper right x coord
  617. * @param[out] py_ur : Decoded zone upper right y coord
  618. */
  619. void VL53L1_decode_zone_limits(
  620. uint8_t encoded_xy_centre,
  621. uint8_t encoded_xy_size,
  622. int16_t *px_ll,
  623. int16_t *py_ll,
  624. int16_t *px_ur,
  625. int16_t *py_ur);
  626. /**
  627. * @brief Returns > 0 if input (row,col) location is an apertured SPAD
  628. *
  629. * @param[in] row : Row
  630. * @param[in] col : Column
  631. *
  632. * @return is_aperture : if > 0 the location is an apertured SPAD
  633. */
  634. uint8_t VL53L1_is_aperture_location(
  635. uint8_t row,
  636. uint8_t col);
  637. /**
  638. * @brief Calculates the effective SPAD counts for the MM inner and outer
  639. * regions based on the input MM ROI, Zone info and return good
  640. * SPAD map
  641. *
  642. * @param[in] encoded_mm_roi_centre : Encoded MM ROI centre - spad number
  643. * @param[in] encoded_mm_roi_size : Encoded MM ROI size
  644. * @param[in] encoded_zone_centre : Encoded Zone centre - spad number
  645. * @param[in] encoded_zone_size : Encoded Zone size
  646. * @param[in] pgood_spads : Return array good SPAD map (32 bytes)
  647. * @param[in] aperture_attenuation : Aperture attenuation value, Format 8.8
  648. * @param[out] pmm_inner_effective_spads : MM Inner effective SPADs, Format 8.8
  649. * @param[out] pmm_outer_effective_spads : MM Outer effective SPADs, Format 8.8
  650. *
  651. */
  652. void VL53L1_calc_mm_effective_spads(
  653. uint8_t encoded_mm_roi_centre,
  654. uint8_t encoded_mm_roi_size,
  655. uint8_t encoded_zone_centre,
  656. uint8_t encoded_zone_size,
  657. uint8_t *pgood_spads,
  658. uint16_t aperture_attenuation,
  659. uint16_t *pmm_inner_effective_spads,
  660. uint16_t *pmm_outer_effective_spads);
  661. /**
  662. * @brief Function to save dynamic config data per zone at init and start range
  663. *
  664. * @param[in] Dev : Pointer to data structure
  665. *
  666. *
  667. * @return VL53L1_ERROR_NONE Success
  668. *
  669. */
  670. VL53L1_Error VL53L1_save_cfg_data(
  671. VL53L1_DEV Dev);
  672. /**
  673. * @brief Encodes VL53L1_GPIO_interrupt_config_t structure to FW register format
  674. *
  675. * @param[in] pintconf : pointer to gpio interrupt config structure
  676. * @return The encoded system__interrupt_config_gpio byte
  677. */
  678. uint8_t VL53L1_encode_GPIO_interrupt_config(
  679. VL53L1_GPIO_interrupt_config_t *pintconf);
  680. /**
  681. * @brief Decodes FW register to VL53L1_GPIO_interrupt_config_t structure
  682. *
  683. * @param[in] system__interrupt_config : interrupt config register byte
  684. * @return The decoded structure
  685. */
  686. VL53L1_GPIO_interrupt_config_t VL53L1_decode_GPIO_interrupt_config(
  687. uint8_t system__interrupt_config);
  688. /**
  689. * @brief SET GPIO distance threshold
  690. *
  691. * @param[in] Dev : Device Handle
  692. * @param[in] threshold_high : High distance threshold in mm
  693. * @param[in] threshold_low : Low distance threshold in mm
  694. */
  695. VL53L1_Error VL53L1_set_GPIO_distance_threshold(
  696. VL53L1_DEV Dev,
  697. uint16_t threshold_high,
  698. uint16_t threshold_low);
  699. /**
  700. * @brief SET GPIO rate threshold
  701. *
  702. * @param[in] Dev : Device Handle
  703. * @param[in] threshold_high : High rate threshold in 9.7 Mcps
  704. * @param[in] threshold_low : Low rate threshold in 9.7 Mcps
  705. */
  706. VL53L1_Error VL53L1_set_GPIO_rate_threshold(
  707. VL53L1_DEV Dev,
  708. uint16_t threshold_high,
  709. uint16_t threshold_low);
  710. /**
  711. * @brief SET GPIO thresholds from structure. Sets both rate and distance
  712. * thresholds
  713. *
  714. * @param[in] Dev : Device Handle
  715. * @param[in] pintconf : Pointer to structure
  716. */
  717. VL53L1_Error VL53L1_set_GPIO_thresholds_from_struct(
  718. VL53L1_DEV Dev,
  719. VL53L1_GPIO_interrupt_config_t *pintconf);
  720. /**
  721. * @brief Set Ref SPAD Characterisation Config
  722. *
  723. * Initialises the timeout and VCSEL period for the Reference
  724. * SPAD Characterisation test mode.
  725. *
  726. * Register Mapping:
  727. *
  728. * - timeout -> VL53L1_PHASECAL_CONFIG__TIMEOUT_MACROP
  729. * - vcsel_period -> VL53L1_RANGE_CONFIG__VCSEL_PERIOD_A \n
  730. * -> VL53L1_SD_CONFIG__WOI_SD0 \n
  731. * -> VL53L1_SD_CONFIG__WOI_SD1
  732. * - total_rate_target_mcps -> VL53L1_REF_SPAD_CHAR__TOTAL_RATE_TARGET_MCPS
  733. * - max_count_rate_rtn_limit_mcps -> VL53L1_RANGE_CONFIG__SIGMA_THRESH
  734. * - min_count_rate_rtn_limit_mcps -> VL53L1_SRANGE_CONFIG__SIGMA_THRESH
  735. *
  736. * @param[in] Dev : Device handle
  737. * @param[in] vcsel_period_a : VCSEL period A register value
  738. * @param[in] phasecal_timeout_us : requested PhaseCal Timeout in [us]
  739. * e.g 1000us
  740. * @param[in] total_rate_target_mcps : Target reference rate [Mcps] 9.7 format
  741. * @param[in] max_count_rate_rtn_limit_mcps : Max rate final check limit [Mcps] 9.7 format
  742. * @param[in] min_count_rate_rtn_limit_mcps : Min rate final check limit [Mcps] 9.7 format
  743. * @param[in] fast_osc_frequency : Fast Osc Frequency (4.12) format
  744. *
  745. * @return VL53L1_ERROR_NONE Success
  746. * @return "Other error code" See ::VL53L1_Error
  747. */
  748. #ifndef VL53L1_NOCALIB
  749. VL53L1_Error VL53L1_set_ref_spad_char_config(
  750. VL53L1_DEV Dev,
  751. uint8_t vcsel_period_a,
  752. uint32_t phasecal_timeout_us,
  753. uint16_t total_rate_target_mcps,
  754. uint16_t max_count_rate_rtn_limit_mcps,
  755. uint16_t min_count_rate_rtn_limit_mcps,
  756. uint16_t fast_osc_frequency);
  757. #endif
  758. /**
  759. * @brief Applies SSC (SPAD Self Check) configuration to device.
  760. *
  761. * Prior to calling this function it is assumed both the Fast Osc
  762. * and VCSEL have already been trimmed and the register values set.
  763. *
  764. * Internally the required timeout in macro periods is calculated
  765. * from the input VCSEL period, fast_osc_frequency and requested
  766. * timeout in microseconds.
  767. *
  768. * Register Mapping:
  769. *
  770. * - rate_select -> VL53L1_NVM_BIST__CTRL
  771. * - timeout -> VL53L1_RANGE_CONFIG__TIMEOUT_MACROP_ B_HI & _LO
  772. * - vcsel_period -> VL53L1_RANGE_CONFIG__VCSEL_PERIOD_B \n
  773. * -> VL53L1_SD_CONFIG__WOI_SD0 \n
  774. * -> VL53L1_SD_CONFIG__WOI_SD1
  775. * - vcsel_start -> VL53L1_CAL_CONFIG__VCSEL_START
  776. * - vcsel_width -> VL53L1_GLOBAL_CONFIG__VCSEL_WIDTH
  777. * - ssc_limit_mcps -> VL53L1_RANGE_CONFIG__SIGMA_THRESH
  778. *
  779. * ssc_rate_limit_mcps format:
  780. *
  781. * - 1.15 for DCR/LCR test modes with VCSEL off
  782. * - 9.7 LCR test mode with VCSEL on
  783. *
  784. * The configuration is set to the device via a 5-byte multi byte write.
  785. *
  786. * @param[in] Dev : Device handle
  787. * @param[in] pssc_cfg : pointer to VL53L1_ssc_config_t
  788. * @param[in] fast_osc_frequency : Fast Osc Frequency (4.12) format
  789. *
  790. * @return VL53L1_ERROR_NONE Success
  791. * @return "Other error code" See ::VL53L1_Error
  792. */
  793. #ifndef VL53L1_NOCALIB
  794. VL53L1_Error VL53L1_set_ssc_config(
  795. VL53L1_DEV Dev,
  796. VL53L1_ssc_config_t *pssc_cfg,
  797. uint16_t fast_osc_frequency);
  798. #endif
  799. /**
  800. * @brief Gets the 256 return array SSC rates from the Patch RAM
  801. *
  802. * Each SSC rate is 1.15 or 9.7 dependent on test run.
  803. * Total of 512 bytes to read!
  804. *
  805. * ssc_rate_mcps buffer format:
  806. *
  807. * - 1.15 for DCR/LCR test modes with VCSEL off (ambient)
  808. * - 9.7 LCR test mode with VCSEL on (which rate?)
  809. *
  810. * Assumes that power force has already been enabled
  811. *
  812. * Function disables firmware at start to allow patch RAM to be read
  813. * and then enables the firmware before returning
  814. *
  815. * The order of the rates is in SPAD number order (increasing)
  816. *
  817. * @param[in] Dev : Device handle
  818. * @param[out] pspad_rates : pointer to VL53L1_spad_rate_data_t
  819. *
  820. * @return VL53L1_ERROR_NONE Success
  821. * @return "Other error code" See ::VL53L1_Error
  822. */
  823. #ifndef VL53L1_NOCALIB
  824. VL53L1_Error VL53L1_get_spad_rate_data(
  825. VL53L1_DEV Dev,
  826. VL53L1_spad_rate_data_t *pspad_rates);
  827. #endif
  828. /**
  829. * @brief Function to add signed margin to the
  830. * xtalk plane offset value, dealing with signed and unsigned
  831. * value mixing
  832. *
  833. * Clips output to 0 if negative
  834. * Clips to max possible value of 9.9 Kcps if exceeds this limitation
  835. *
  836. * @param[in] plane_offset_kcps : xtalk plane offset (9.9 format) Kcps
  837. * @param[in] margin_offset_kcps : margin offset signed (6.9 format) Kcps
  838. *
  839. * @return plane_offset_with_margin in Kcps (7.9 format)
  840. */
  841. uint32_t VL53L1_calc_crosstalk_plane_offset_with_margin(
  842. uint32_t plane_offset_kcps,
  843. int16_t margin_offset_kcps);
  844. /**
  845. * @brief Initialize the Low Power Auto data structure
  846. *
  847. * Patch_LowPowerAutoMode
  848. *
  849. * @param[in] Dev : Device Handle
  850. *
  851. * @return VL53L1_ERROR_NONE Success
  852. * @return "Other error code" See ::VL53L1_Error
  853. */
  854. VL53L1_Error VL53L1_low_power_auto_data_init(
  855. VL53L1_DEV Dev
  856. );
  857. /**
  858. * @brief Reset internal state but leave low_power_auto mode intact
  859. *
  860. * Patch_LowPowerAutoMode
  861. *
  862. * @param[in] Dev : Device Handle
  863. *
  864. * @return VL53L1_ERROR_NONE Success
  865. * @return "Other error code" See ::VL53L1_Error
  866. */
  867. VL53L1_Error VL53L1_low_power_auto_data_stop_range(
  868. VL53L1_DEV Dev
  869. );
  870. /**
  871. * @brief Initialize the config strcutures when low power auto preset modes are
  872. * selected
  873. *
  874. * Patch_LowPowerAutoMode
  875. *
  876. * @param[in] pgeneral : pointer to the general config structure
  877. * @param[in] pdynamic : pointer to the dynamic config structure
  878. * @param[in] plpadata : pointer to the low power auto config structure
  879. *
  880. * @return VL53L1_ERROR_NONE Success
  881. * @return "Other error code" See ::VL53L1_Error
  882. */
  883. VL53L1_Error VL53L1_config_low_power_auto_mode(
  884. VL53L1_general_config_t *pgeneral,
  885. VL53L1_dynamic_config_t *pdynamic,
  886. VL53L1_low_power_auto_data_t *plpadata
  887. );
  888. /**
  889. * @brief Setup ranges after the first one in low power auto mode by turning
  890. * off FW calibration steps and programming static values
  891. *
  892. * Patch_LowPowerAutoMode
  893. *
  894. * @param[out] Dev : Device handle
  895. *
  896. * @return VL53L1_ERROR_NONE Success
  897. * @return "Other error code" See ::VL53L1_Error
  898. */
  899. VL53L1_Error VL53L1_low_power_auto_setup_manual_calibration(
  900. VL53L1_DEV Dev);
  901. /**
  902. * @brief Do a DSS calculation and update manual config
  903. *
  904. * Patch_LowPowerAutoMode
  905. *
  906. * @param[out] Dev : Device handle
  907. *
  908. * @return VL53L1_ERROR_NONE Success
  909. * @return "Other error code" See ::VL53L1_Error
  910. */
  911. VL53L1_Error VL53L1_low_power_auto_update_DSS(
  912. VL53L1_DEV Dev);
  913. #ifdef __cplusplus
  914. }
  915. #endif
  916. #endif /* _VL53L1_API_CORE_H_ */