vl53l1_wait.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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_wait.c
  64. *
  65. * @brief EwokPlus25 low level Driver wait function definition
  66. */
  67. #include "vl53l1_ll_def.h"
  68. #include "vl53l1_ll_device.h"
  69. #include "vl53l1_platform.h"
  70. #include "vl53l1_core.h"
  71. #include "vl53l1_silicon_core.h"
  72. #include "vl53l1_wait.h"
  73. #include "vl53l1_register_settings.h"
  74. #define LOG_FUNCTION_START(fmt, ...) \
  75. _LOG_FUNCTION_START(VL53L1_TRACE_MODULE_CORE, fmt, ##__VA_ARGS__)
  76. #define LOG_FUNCTION_END(status, ...) \
  77. _LOG_FUNCTION_END(VL53L1_TRACE_MODULE_CORE, status, ##__VA_ARGS__)
  78. #define LOG_FUNCTION_END_FMT(status, fmt, ...) \
  79. _LOG_FUNCTION_END_FMT(VL53L1_TRACE_MODULE_CORE, status, \
  80. fmt, ##__VA_ARGS__)
  81. VL53L1_Error VL53L1_wait_for_boot_completion(
  82. VL53L1_DEV Dev)
  83. {
  84. /* Waits for firmware boot to finish
  85. */
  86. VL53L1_Error status = VL53L1_ERROR_NONE;
  87. VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
  88. uint8_t fw_ready = 0;
  89. LOG_FUNCTION_START("");
  90. if (pdev->wait_method == VL53L1_WAIT_METHOD_BLOCKING) {
  91. /* blocking version */
  92. status =
  93. VL53L1_poll_for_boot_completion(
  94. Dev,
  95. VL53L1_BOOT_COMPLETION_POLLING_TIMEOUT_MS);
  96. } else {
  97. /* implement non blocking version below */
  98. fw_ready = 0;
  99. while (fw_ready == 0x00 && status == VL53L1_ERROR_NONE) {
  100. status = VL53L1_is_boot_complete(
  101. Dev,
  102. &fw_ready);
  103. if (status == VL53L1_ERROR_NONE) {
  104. status = VL53L1_WaitMs(
  105. Dev,
  106. VL53L1_POLLING_DELAY_MS);
  107. }
  108. }
  109. }
  110. LOG_FUNCTION_END(status);
  111. return status;
  112. }
  113. VL53L1_Error VL53L1_wait_for_firmware_ready(
  114. VL53L1_DEV Dev)
  115. {
  116. /* If in timed mode or single shot then check firmware is ready
  117. * before sending handshake
  118. */
  119. VL53L1_Error status = VL53L1_ERROR_NONE;
  120. VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
  121. uint8_t fw_ready = 0;
  122. uint8_t mode_start = 0;
  123. LOG_FUNCTION_START("");
  124. /* Filter out tje measure mode part of the mode
  125. * start register
  126. */
  127. mode_start =
  128. pdev->sys_ctrl.system__mode_start &
  129. VL53L1_DEVICEMEASUREMENTMODE_MODE_MASK;
  130. /*
  131. * conditional wait for firmware ready
  132. * only waits for timed and single shot modes
  133. */
  134. if ((mode_start == VL53L1_DEVICEMEASUREMENTMODE_TIMED) ||
  135. (mode_start == VL53L1_DEVICEMEASUREMENTMODE_SINGLESHOT)) {
  136. if (pdev->wait_method == VL53L1_WAIT_METHOD_BLOCKING) {
  137. /* blocking version */
  138. status =
  139. VL53L1_poll_for_firmware_ready(
  140. Dev,
  141. VL53L1_RANGE_COMPLETION_POLLING_TIMEOUT_MS);
  142. } else {
  143. /* implement non blocking version below */
  144. fw_ready = 0;
  145. while (fw_ready == 0x00 && status == VL53L1_ERROR_NONE) {
  146. status = VL53L1_is_firmware_ready(
  147. Dev,
  148. &fw_ready);
  149. if (status == VL53L1_ERROR_NONE) {
  150. status = VL53L1_WaitMs(
  151. Dev,
  152. VL53L1_POLLING_DELAY_MS);
  153. }
  154. }
  155. }
  156. }
  157. LOG_FUNCTION_END(status);
  158. return status;
  159. }
  160. VL53L1_Error VL53L1_wait_for_range_completion(
  161. VL53L1_DEV Dev)
  162. {
  163. /* Wrapper function for waiting for range completion
  164. */
  165. VL53L1_Error status = VL53L1_ERROR_NONE;
  166. VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
  167. uint8_t data_ready = 0;
  168. LOG_FUNCTION_START("");
  169. if (pdev->wait_method == VL53L1_WAIT_METHOD_BLOCKING) {
  170. /* blocking version */
  171. status =
  172. VL53L1_poll_for_range_completion(
  173. Dev,
  174. VL53L1_RANGE_COMPLETION_POLLING_TIMEOUT_MS);
  175. } else {
  176. /* implement non blocking version below */
  177. data_ready = 0;
  178. while (data_ready == 0x00 && status == VL53L1_ERROR_NONE) {
  179. status = VL53L1_is_new_data_ready(
  180. Dev,
  181. &data_ready);
  182. if (status == VL53L1_ERROR_NONE) {
  183. status = VL53L1_WaitMs(
  184. Dev,
  185. VL53L1_POLLING_DELAY_MS);
  186. }
  187. }
  188. }
  189. LOG_FUNCTION_END(status);
  190. return status;
  191. }
  192. VL53L1_Error VL53L1_wait_for_test_completion(
  193. VL53L1_DEV Dev)
  194. {
  195. /* Wrapper function for waiting for test mode completion
  196. */
  197. VL53L1_Error status = VL53L1_ERROR_NONE;
  198. VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
  199. uint8_t data_ready = 0;
  200. LOG_FUNCTION_START("");
  201. if (pdev->wait_method == VL53L1_WAIT_METHOD_BLOCKING) {
  202. /* blocking version */
  203. status =
  204. VL53L1_poll_for_range_completion(
  205. Dev,
  206. VL53L1_TEST_COMPLETION_POLLING_TIMEOUT_MS);
  207. } else {
  208. /* implement non blocking version below */
  209. data_ready = 0;
  210. while (data_ready == 0x00 && status == VL53L1_ERROR_NONE) {
  211. status = VL53L1_is_new_data_ready(
  212. Dev,
  213. &data_ready);
  214. if (status == VL53L1_ERROR_NONE) {
  215. status = VL53L1_WaitMs(
  216. Dev,
  217. VL53L1_POLLING_DELAY_MS);
  218. }
  219. }
  220. }
  221. LOG_FUNCTION_END(status);
  222. return status;
  223. }
  224. VL53L1_Error VL53L1_is_boot_complete(
  225. VL53L1_DEV Dev,
  226. uint8_t *pready)
  227. {
  228. /**
  229. * Determines if the firmware finished booting by reading
  230. * bit 0 of firmware__system_status register
  231. */
  232. VL53L1_Error status = VL53L1_ERROR_NONE;
  233. uint8_t firmware__system_status = 0;
  234. LOG_FUNCTION_START("");
  235. /* read current range interrupt state */
  236. status =
  237. VL53L1_RdByte(
  238. Dev,
  239. VL53L1_FIRMWARE__SYSTEM_STATUS,
  240. &firmware__system_status);
  241. /* set *pready = 1 if new range data ready complete
  242. * zero otherwise
  243. */
  244. if ((firmware__system_status & 0x01) == 0x01) {
  245. *pready = 0x01;
  246. VL53L1_init_ll_driver_state(
  247. Dev,
  248. VL53L1_DEVICESTATE_SW_STANDBY);
  249. } else {
  250. *pready = 0x00;
  251. VL53L1_init_ll_driver_state(
  252. Dev,
  253. VL53L1_DEVICESTATE_FW_COLDBOOT);
  254. }
  255. LOG_FUNCTION_END(status);
  256. return status;
  257. }
  258. VL53L1_Error VL53L1_is_firmware_ready(
  259. VL53L1_DEV Dev,
  260. uint8_t *pready)
  261. {
  262. /**
  263. * Determines if the firmware is ready to range
  264. */
  265. VL53L1_Error status = VL53L1_ERROR_NONE;
  266. VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
  267. LOG_FUNCTION_START("");
  268. status = VL53L1_is_firmware_ready_silicon(
  269. Dev,
  270. pready);
  271. pdev->fw_ready = *pready;
  272. LOG_FUNCTION_END(status);
  273. return status;
  274. }
  275. VL53L1_Error VL53L1_is_new_data_ready(
  276. VL53L1_DEV Dev,
  277. uint8_t *pready)
  278. {
  279. /**
  280. * Determines if new range data is ready by reading bit 0 of
  281. * VL53L1_GPIO__TIO_HV_STATUS to determine the current state
  282. * of output interrupt pin
  283. */
  284. VL53L1_Error status = VL53L1_ERROR_NONE;
  285. VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
  286. uint8_t gpio__mux_active_high_hv = 0;
  287. uint8_t gpio__tio_hv_status = 0;
  288. uint8_t interrupt_ready = 0;
  289. LOG_FUNCTION_START("");
  290. gpio__mux_active_high_hv =
  291. pdev->stat_cfg.gpio_hv_mux__ctrl &
  292. VL53L1_DEVICEINTERRUPTLEVEL_ACTIVE_MASK;
  293. if (gpio__mux_active_high_hv == VL53L1_DEVICEINTERRUPTLEVEL_ACTIVE_HIGH)
  294. interrupt_ready = 0x01;
  295. else
  296. interrupt_ready = 0x00;
  297. /* read current range interrupt state */
  298. status = VL53L1_RdByte(
  299. Dev,
  300. VL53L1_GPIO__TIO_HV_STATUS,
  301. &gpio__tio_hv_status);
  302. /* set *pready = 1 if new range data ready complete zero otherwise */
  303. if ((gpio__tio_hv_status & 0x01) == interrupt_ready)
  304. *pready = 0x01;
  305. else
  306. *pready = 0x00;
  307. LOG_FUNCTION_END(status);
  308. return status;
  309. }
  310. VL53L1_Error VL53L1_poll_for_boot_completion(
  311. VL53L1_DEV Dev,
  312. uint32_t timeout_ms)
  313. {
  314. /**
  315. * Polls the bit 0 of the FIRMWARE__SYSTEM_STATUS register to see if
  316. * the firmware is ready.
  317. */
  318. VL53L1_Error status = VL53L1_ERROR_NONE;
  319. LOG_FUNCTION_START("");
  320. /* after reset for the firmware blocks I2C access while
  321. * it copies the NVM data into the G02 host register banks
  322. * The host must wait the required time to allow the copy
  323. * to complete before attempting to read the firmware status
  324. */
  325. status = VL53L1_WaitUs(
  326. Dev,
  327. VL53L1_FIRMWARE_BOOT_TIME_US);
  328. if (status == VL53L1_ERROR_NONE)
  329. status =
  330. VL53L1_WaitValueMaskEx(
  331. Dev,
  332. timeout_ms,
  333. VL53L1_FIRMWARE__SYSTEM_STATUS,
  334. 0x01,
  335. 0x01,
  336. VL53L1_POLLING_DELAY_MS);
  337. if (status == VL53L1_ERROR_NONE)
  338. VL53L1_init_ll_driver_state(Dev, VL53L1_DEVICESTATE_SW_STANDBY);
  339. LOG_FUNCTION_END(status);
  340. return status;
  341. }
  342. VL53L1_Error VL53L1_poll_for_firmware_ready(
  343. VL53L1_DEV Dev,
  344. uint32_t timeout_ms)
  345. {
  346. /**
  347. * Polls the bit 0 of the FIRMWARE__SYSTEM_STATUS register to see if
  348. * the firmware is ready.
  349. */
  350. VL53L1_Error status = VL53L1_ERROR_NONE;
  351. VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
  352. uint32_t start_time_ms = 0;
  353. uint32_t current_time_ms = 0;
  354. int32_t poll_delay_ms = VL53L1_POLLING_DELAY_MS;
  355. uint8_t fw_ready = 0;
  356. /* calculate time limit in absolute time */
  357. VL53L1_GetTickCount(&start_time_ms); /*lint !e534 ignoring return*/
  358. pdev->fw_ready_poll_duration_ms = 0;
  359. /* wait until firmware is ready, timeout reached on error occurred */
  360. while ((status == VL53L1_ERROR_NONE) &&
  361. (pdev->fw_ready_poll_duration_ms < timeout_ms) &&
  362. (fw_ready == 0)) {
  363. status = VL53L1_is_firmware_ready(
  364. Dev,
  365. &fw_ready);
  366. if (status == VL53L1_ERROR_NONE &&
  367. fw_ready == 0 &&
  368. poll_delay_ms > 0) {
  369. status = VL53L1_WaitMs(
  370. Dev,
  371. poll_delay_ms);
  372. }
  373. /*
  374. * Update polling time (Compare difference rather than
  375. * absolute to negate 32bit wrap around issue)
  376. */
  377. VL53L1_GetTickCount(&current_time_ms); /*lint !e534 ignoring return*/
  378. pdev->fw_ready_poll_duration_ms =
  379. current_time_ms - start_time_ms;
  380. }
  381. if (fw_ready == 0 && status == VL53L1_ERROR_NONE)
  382. status = VL53L1_ERROR_TIME_OUT;
  383. LOG_FUNCTION_END(status);
  384. return status;
  385. }
  386. VL53L1_Error VL53L1_poll_for_range_completion(
  387. VL53L1_DEV Dev,
  388. uint32_t timeout_ms)
  389. {
  390. /**
  391. * Polls bit 0 of VL53L1_GPIO__TIO_HV_STATUS to determine
  392. * the state of output interrupt pin
  393. *
  394. * Interrupt may be either active high or active low. Use active_high to
  395. * select the required level check
  396. */
  397. VL53L1_Error status = VL53L1_ERROR_NONE;
  398. VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
  399. uint8_t gpio__mux_active_high_hv = 0;
  400. uint8_t interrupt_ready = 0;
  401. LOG_FUNCTION_START("");
  402. gpio__mux_active_high_hv =
  403. pdev->stat_cfg.gpio_hv_mux__ctrl &
  404. VL53L1_DEVICEINTERRUPTLEVEL_ACTIVE_MASK;
  405. if (gpio__mux_active_high_hv == VL53L1_DEVICEINTERRUPTLEVEL_ACTIVE_HIGH)
  406. interrupt_ready = 0x01;
  407. else
  408. interrupt_ready = 0x00;
  409. status =
  410. VL53L1_WaitValueMaskEx(
  411. Dev,
  412. timeout_ms,
  413. VL53L1_GPIO__TIO_HV_STATUS,
  414. interrupt_ready,
  415. 0x01,
  416. VL53L1_POLLING_DELAY_MS);
  417. LOG_FUNCTION_END(status);
  418. return status;
  419. }