vl53l1_api.c 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909
  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. #include "vl53l1_api.h"
  63. #include "vl53l1_api_strings.h"
  64. #include "vl53l1_register_settings.h"
  65. #include "vl53l1_register_funcs.h"
  66. #include "vl53l1_core.h"
  67. #include "vl53l1_api_calibration.h"
  68. #include "vl53l1_wait.h"
  69. #include "vl53l1_preset_setup.h"
  70. #include "vl53l1_api_debug.h"
  71. #include "vl53l1_api_core.h"
  72. /* Check for minimum user zone requested by Xtalk calibration */
  73. /* no need for VL53L1_MAX_USER_ZONES check, set 5 to pass the test */
  74. #define ZONE_CHECK 5
  75. #if ZONE_CHECK < 5
  76. #error Must define at least 5 zones in MAX_USER_ZONES constant
  77. #endif
  78. #ifdef VL53L1_NOCALIB
  79. #define OFFSET_CALIB_EMPTY
  80. #endif
  81. #ifndef VL53L1_NOCALIB
  82. #define OFFSET_CALIB
  83. #endif
  84. #define LOG_FUNCTION_START(fmt, ...) \
  85. _LOG_FUNCTION_START(VL53L1_TRACE_MODULE_API, fmt, ##__VA_ARGS__)
  86. #define LOG_FUNCTION_END(status, ...) \
  87. _LOG_FUNCTION_END(VL53L1_TRACE_MODULE_API, status, ##__VA_ARGS__)
  88. #define LOG_FUNCTION_END_FMT(status, fmt, ...) \
  89. _LOG_FUNCTION_END_FMT(VL53L1_TRACE_MODULE_API, status, \
  90. fmt, ##__VA_ARGS__)
  91. #ifdef VL53L1_LOG_ENABLE
  92. #define trace_print(level, ...) trace_print_module_function(\
  93. VL53L1_TRACE_MODULE_API, level, VL53L1_TRACE_FUNCTION_NONE, \
  94. ##__VA_ARGS__)
  95. #endif
  96. #ifndef MIN
  97. #define MIN(v1, v2) ((v1) < (v2) ? (v1) : (v2))
  98. #endif
  99. #ifndef MAX
  100. #define MAX(v1, v2) ((v1) < (v2) ? (v2) : (v1))
  101. #endif
  102. #define DMAX_REFLECTANCE_IDX 2
  103. /* Use Dmax index 2 because it's the 50% reflectance case by default */
  104. /* Following LOWPOWER_AUTO figures have been measured observing vcsel
  105. * emissions on an actual device
  106. */
  107. #define LOWPOWER_AUTO_VHV_LOOP_DURATION_US 245
  108. #define LOWPOWER_AUTO_OVERHEAD_BEFORE_A_RANGING 1448
  109. #define LOWPOWER_AUTO_OVERHEAD_BETWEEN_A_B_RANGING 2100
  110. #define FDA_MAX_TIMING_BUDGET_US 550000
  111. /* Maximum timing budget allowed codex #456189*/
  112. /* local static utilities functions */
  113. /* Bare Driver Tuning parameter table indexed with VL53L1_Tuning_t */
  114. static int32_t BDTable[VL53L1_TUNING_MAX_TUNABLE_KEY] = {
  115. TUNING_VERSION,
  116. TUNING_PROXY_MIN,
  117. TUNING_SINGLE_TARGET_XTALK_TARGET_DISTANCE_MM,
  118. TUNING_SINGLE_TARGET_XTALK_SAMPLE_NUMBER,
  119. TUNING_MIN_AMBIENT_DMAX_VALID,
  120. TUNING_MAX_SIMPLE_OFFSET_CALIBRATION_SAMPLE_NUMBER,
  121. TUNING_XTALK_FULL_ROI_TARGET_DISTANCE_MM,
  122. TUNING_SIMPLE_OFFSET_CALIBRATION_REPEAT
  123. };
  124. #define VL53L1_NVM_POWER_UP_DELAY_US 50
  125. #define VL53L1_NVM_READ_TRIGGER_DELAY_US 5
  126. static VL53L1_Error VL53L1_nvm_enable(
  127. VL53L1_DEV Dev,
  128. uint16_t nvm_ctrl_pulse_width,
  129. int32_t nvm_power_up_delay_us)
  130. {
  131. /*
  132. * Sequence below enables NVM for reading
  133. *
  134. * - Enable power force
  135. * - Disable firmware
  136. * - Power up NVM
  137. * - Wait for 50us while the NVM powers up
  138. * - Configure for reading and set the pulse width (16-bit)
  139. */
  140. VL53L1_Error status = VL53L1_ERROR_NONE;
  141. LOG_FUNCTION_START("");
  142. /* Disable Firmware */
  143. if (status == VL53L1_ERROR_NONE) /*lint !e774 always true*/
  144. status = VL53L1_disable_firmware(Dev);
  145. /* Enable Power Force */
  146. if (status == VL53L1_ERROR_NONE)
  147. status = VL53L1_enable_powerforce(Dev);
  148. /* Wait the required time for the regulators, bandgap,
  149. * oscillator to wake up and settle
  150. */
  151. if (status == VL53L1_ERROR_NONE)
  152. status = VL53L1_WaitUs(
  153. Dev,
  154. VL53L1_ENABLE_POWERFORCE_SETTLING_TIME_US);
  155. /* Power up NVM */
  156. if (status == VL53L1_ERROR_NONE)
  157. status = VL53L1_WrByte(
  158. Dev,
  159. VL53L1_RANGING_CORE__NVM_CTRL__PDN,
  160. 0x01);
  161. /* Enable NVM Clock */
  162. if (status == VL53L1_ERROR_NONE)
  163. status = VL53L1_WrByte(
  164. Dev,
  165. VL53L1_RANGING_CORE__CLK_CTRL1,
  166. 0x05);
  167. /* Wait the required time for NVM to power up*/
  168. if (status == VL53L1_ERROR_NONE)
  169. status = VL53L1_WaitUs(
  170. Dev,
  171. nvm_power_up_delay_us);
  172. /* Select read mode and set control pulse width */
  173. if (status == VL53L1_ERROR_NONE)
  174. status = VL53L1_WrByte(
  175. Dev,
  176. VL53L1_RANGING_CORE__NVM_CTRL__MODE,
  177. 0x01);
  178. if (status == VL53L1_ERROR_NONE)
  179. status = VL53L1_WrWord(
  180. Dev,
  181. VL53L1_RANGING_CORE__NVM_CTRL__PULSE_WIDTH_MSB,
  182. nvm_ctrl_pulse_width);
  183. LOG_FUNCTION_END(status);
  184. return status;
  185. }
  186. static VL53L1_Error VL53L1_nvm_read(
  187. VL53L1_DEV Dev,
  188. uint8_t start_address,
  189. uint8_t count,
  190. uint8_t *pdata)
  191. {
  192. /*
  193. * Sequence per 32-bit NVM read access:
  194. *
  195. * - Set up the 5-bit (0-127) NVM Address
  196. * - Trigger the read of the NVM data by toggling NVM_CTRL__READN
  197. * - Read the NVM data - 4 bytes wide read/write interface
  198. * - Increment data byte pointer by 4 ready for the next loop
  199. */
  200. VL53L1_Error status = VL53L1_ERROR_NONE;
  201. uint8_t nvm_addr = 0;
  202. LOG_FUNCTION_START("");
  203. for (nvm_addr = start_address; nvm_addr < (start_address+count) ; nvm_addr++) {
  204. /* Step 1 : set address */
  205. if (status == VL53L1_ERROR_NONE)
  206. status = VL53L1_WrByte(
  207. Dev,
  208. VL53L1_RANGING_CORE__NVM_CTRL__ADDR,
  209. nvm_addr);
  210. /* Step 2 : trigger reading of data */
  211. if (status == VL53L1_ERROR_NONE)
  212. status = VL53L1_WrByte(
  213. Dev,
  214. VL53L1_RANGING_CORE__NVM_CTRL__READN,
  215. 0x00);
  216. /* Step 3 : wait the required time */
  217. if (status == VL53L1_ERROR_NONE)
  218. status = VL53L1_WaitUs(
  219. Dev,
  220. VL53L1_NVM_READ_TRIGGER_DELAY_US);
  221. if (status == VL53L1_ERROR_NONE)
  222. status = VL53L1_WrByte(
  223. Dev,
  224. VL53L1_RANGING_CORE__NVM_CTRL__READN,
  225. 0x01);
  226. /* Step 3 : read 4-byte wide data register */
  227. if (status == VL53L1_ERROR_NONE)
  228. status = VL53L1_ReadMulti(
  229. Dev,
  230. VL53L1_RANGING_CORE__NVM_CTRL__DATAOUT_MMM,
  231. pdata,
  232. 4);
  233. /* Step 4 : increment byte buffer pointer */
  234. pdata = pdata + 4;
  235. }
  236. LOG_FUNCTION_END(status);
  237. return status;
  238. }
  239. static VL53L1_Error VL53L1_nvm_disable(
  240. VL53L1_DEV Dev)
  241. {
  242. /*
  243. * Power down NVM (OTP) to extend lifetime
  244. */
  245. VL53L1_Error status = VL53L1_ERROR_NONE;
  246. LOG_FUNCTION_START("");
  247. if (status == VL53L1_ERROR_NONE) /*lint !e774 always true*/
  248. status = VL53L1_WrByte(
  249. Dev,
  250. VL53L1_RANGING_CORE__NVM_CTRL__READN,
  251. 0x01);
  252. /* Power down NVM */
  253. if (status == VL53L1_ERROR_NONE)
  254. status = VL53L1_WrByte(
  255. Dev,
  256. VL53L1_RANGING_CORE__NVM_CTRL__PDN,
  257. 0x00);
  258. /* Keep power force enabled */
  259. if (status == VL53L1_ERROR_NONE)
  260. status = VL53L1_disable_powerforce(Dev);
  261. /* (Re)Enable Firmware */
  262. if (status == VL53L1_ERROR_NONE)
  263. status = VL53L1_enable_firmware(Dev);
  264. LOG_FUNCTION_END(status);
  265. return status;
  266. }
  267. static VL53L1_Error VL53L1_read_nvm_raw_data(
  268. VL53L1_DEV Dev,
  269. uint8_t start_address,
  270. uint8_t count,
  271. uint8_t *pnvm_raw_data)
  272. {
  273. /*
  274. * Reads ALL 512 bytes of NVM data
  275. */
  276. VL53L1_Error status = VL53L1_ERROR_NONE;
  277. LOG_FUNCTION_START("");
  278. /*
  279. * Enable NVM and set control pulse width
  280. */
  281. if (status == VL53L1_ERROR_NONE) /*lint !e774 always true*/
  282. status = VL53L1_nvm_enable(
  283. Dev,
  284. 0x0004,
  285. VL53L1_NVM_POWER_UP_DELAY_US);
  286. /*
  287. * Read the raw NVM data
  288. * - currently all of 128 * 4 bytes = 512 bytes are read
  289. */
  290. if (status == VL53L1_ERROR_NONE)
  291. status = VL53L1_nvm_read(
  292. Dev,
  293. start_address,
  294. count,
  295. pnvm_raw_data);
  296. /*
  297. * Disable NVM
  298. */
  299. if (status == VL53L1_ERROR_NONE)
  300. status = VL53L1_nvm_disable(Dev);
  301. LOG_FUNCTION_END(status);
  302. return status;
  303. }
  304. static VL53L1_Error SingleTargetXTalkCalibration(VL53L1_DEV Dev)
  305. {
  306. VL53L1_Error Status = VL53L1_ERROR_NONE;
  307. uint32_t sum_ranging = 0;
  308. uint32_t sum_spads = 0;
  309. FixPoint1616_t sum_signalRate = 0;
  310. FixPoint1616_t total_count = 0;
  311. uint8_t xtalk_meas = 0;
  312. uint8_t xtalk_measmax =
  313. BDTable[VL53L1_TUNING_SINGLE_TARGET_XTALK_SAMPLE_NUMBER];
  314. VL53L1_RangingMeasurementData_t RMData;
  315. FixPoint1616_t xTalkStoredMeanSignalRate;
  316. FixPoint1616_t xTalkStoredMeanRange;
  317. FixPoint1616_t xTalkStoredMeanRtnSpads;
  318. uint32_t xTalkStoredMeanRtnSpadsAsInt;
  319. uint32_t xTalkCalDistanceAsInt;
  320. FixPoint1616_t XTalkCompensationRateMegaCps;
  321. uint32_t signalXTalkTotalPerSpad;
  322. VL53L1_PresetModes PresetMode;
  323. VL53L1_CalibrationData_t CalibrationData;
  324. VL53L1_CustomerNvmManaged_t *pC;
  325. LOG_FUNCTION_START("");
  326. /* check if the following are selected
  327. * VL53L1_PRESETMODE_AUTONOMOUS,
  328. * VL53L1_PRESETMODE_LOWPOWER_AUTONOMOUS
  329. * VL53L1_PRESETMODE_LITE_RANGING
  330. */
  331. PresetMode = VL53L1DevDataGet(Dev, CurrentParameters.PresetMode);
  332. if ((PresetMode != VL53L1_PRESETMODE_AUTONOMOUS) &&
  333. (PresetMode != VL53L1_PRESETMODE_LOWPOWER_AUTONOMOUS) &&
  334. (PresetMode != VL53L1_PRESETMODE_LITE_RANGING)) {
  335. Status = VL53L1_ERROR_MODE_NOT_SUPPORTED;
  336. goto ENDFUNC;
  337. }
  338. /* disable crosstalk calibration */
  339. Status = VL53L1_disable_xtalk_compensation(Dev);
  340. CHECK_ERROR_GO_ENDFUNC;
  341. Status = VL53L1_StartMeasurement(Dev);
  342. CHECK_ERROR_GO_ENDFUNC;
  343. sum_ranging = 0;
  344. sum_spads = 0;
  345. sum_signalRate = 0;
  346. total_count = 0;
  347. for (xtalk_meas = 0; xtalk_meas < xtalk_measmax; xtalk_meas++) {
  348. VL53L1_WaitMeasurementDataReady(Dev);
  349. VL53L1_GetRangingMeasurementData(Dev, &RMData);
  350. VL53L1_ClearInterruptAndStartMeasurement(Dev);
  351. if (RMData.RangeStatus == VL53L1_RANGESTATUS_RANGE_VALID) {
  352. sum_ranging += RMData.RangeMilliMeter;
  353. sum_signalRate += RMData.SignalRateRtnMegaCps;
  354. sum_spads += RMData.EffectiveSpadRtnCount / 256;
  355. total_count++;
  356. }
  357. }
  358. Status = VL53L1_StopMeasurement(Dev);
  359. if (total_count > 0) {
  360. /* FixPoint1616_t / uint16_t = FixPoint1616_t */
  361. xTalkStoredMeanSignalRate = sum_signalRate / total_count;
  362. xTalkStoredMeanRange = (FixPoint1616_t)(sum_ranging << 16);
  363. xTalkStoredMeanRange /= total_count;
  364. xTalkStoredMeanRtnSpads = (FixPoint1616_t)(sum_spads << 16);
  365. xTalkStoredMeanRtnSpads /= total_count;
  366. /* Round Mean Spads to Whole Number.
  367. * Typically the calculated mean SPAD count is a whole number
  368. * or very close to a whole
  369. * number, therefore any truncation will not result in a
  370. * significant loss in accuracy.
  371. * Also, for a grey target at a typical distance of around
  372. * 400mm, around 220 SPADs will
  373. * be enabled, therefore, any truncation will result in a loss
  374. * of accuracy of less than
  375. * 0.5%.
  376. */
  377. xTalkStoredMeanRtnSpadsAsInt = (xTalkStoredMeanRtnSpads +
  378. 0x8000) >> 16;
  379. /* Round Cal Distance to Whole Number.
  380. * Note that the cal distance is in mm, therefore no resolution
  381. * is lost.
  382. */
  383. xTalkCalDistanceAsInt = ((uint32_t)BDTable[
  384. VL53L1_TUNING_SINGLE_TARGET_XTALK_TARGET_DISTANCE_MM]);
  385. if (xTalkStoredMeanRtnSpadsAsInt == 0 ||
  386. xTalkCalDistanceAsInt == 0 ||
  387. xTalkStoredMeanRange >= (xTalkCalDistanceAsInt << 16)) {
  388. XTalkCompensationRateMegaCps = 0;
  389. } else {
  390. /* Apply division by mean spad count early in the
  391. * calculation to keep the numbers small.
  392. * This ensures we can maintain a 32bit calculation.
  393. * Fixed1616 / int := Fixed1616
  394. */
  395. signalXTalkTotalPerSpad = (xTalkStoredMeanSignalRate) /
  396. xTalkStoredMeanRtnSpadsAsInt;
  397. /* Complete the calculation for total Signal XTalk per
  398. * SPAD
  399. * Fixed1616 * (Fixed1616 - Fixed1616/int) :=
  400. * (2^16 * Fixed1616)
  401. */
  402. signalXTalkTotalPerSpad *= (((uint32_t)1 << 16) -
  403. (xTalkStoredMeanRange / xTalkCalDistanceAsInt));
  404. /* Round from 2^16 * Fixed1616, to Fixed1616. */
  405. XTalkCompensationRateMegaCps = (signalXTalkTotalPerSpad
  406. + 0x8000) >> 16;
  407. }
  408. Status = VL53L1_GetCalibrationData(Dev, &CalibrationData);
  409. CHECK_ERROR_GO_ENDFUNC;
  410. pC = &CalibrationData.customer;
  411. pC->algo__crosstalk_compensation_plane_offset_kcps =
  412. (uint32_t)(1000 * ((XTalkCompensationRateMegaCps +
  413. ((uint32_t)1<<6)) >> (16-9)));
  414. Status = VL53L1_SetCalibrationData(Dev, &CalibrationData);
  415. CHECK_ERROR_GO_ENDFUNC;
  416. Status = VL53L1_enable_xtalk_compensation(Dev);
  417. } else
  418. /* return error because no valid data found */
  419. Status = VL53L1_ERROR_XTALK_EXTRACTION_NO_SAMPLE_FAIL;
  420. ENDFUNC:
  421. LOG_FUNCTION_END(Status);
  422. return Status;
  423. }
  424. /* Check Rectangle in user's coordinate system:
  425. * 15 TL(x,y) o-----*
  426. * ^ | |
  427. * | *-----o BR(x,y)
  428. * 0------------------------- >15
  429. * check Rectangle definition conforms to the (0,15,15) coordinate system
  430. * with a minimum of 4x4 size
  431. */
  432. static VL53L1_Error CheckValidRectRoi(VL53L1_UserRoi_t ROI)
  433. {
  434. VL53L1_Error Status = VL53L1_ERROR_NONE;
  435. LOG_FUNCTION_START("");
  436. /* Negative check are not necessary because value is unsigned */
  437. if ((ROI.TopLeftX > 15) || (ROI.TopLeftY > 15) ||
  438. (ROI.BotRightX > 15) || (ROI.BotRightY > 15))
  439. Status = VL53L1_ERROR_INVALID_PARAMS;
  440. if ((ROI.TopLeftX > ROI.BotRightX) || (ROI.TopLeftY < ROI.BotRightY))
  441. Status = VL53L1_ERROR_INVALID_PARAMS;
  442. LOG_FUNCTION_END(Status);
  443. return Status;
  444. }
  445. static VL53L1_GPIO_Interrupt_Mode ConvertModeToLLD(VL53L1_Error *pStatus,
  446. VL53L1_ThresholdMode CrossMode)
  447. {
  448. VL53L1_GPIO_Interrupt_Mode Mode;
  449. switch (CrossMode) {
  450. case VL53L1_THRESHOLD_CROSSED_LOW:
  451. Mode = VL53L1_GPIOINTMODE_LEVEL_LOW;
  452. break;
  453. case VL53L1_THRESHOLD_CROSSED_HIGH:
  454. Mode = VL53L1_GPIOINTMODE_LEVEL_HIGH;
  455. break;
  456. case VL53L1_THRESHOLD_OUT_OF_WINDOW:
  457. Mode = VL53L1_GPIOINTMODE_OUT_OF_WINDOW;
  458. break;
  459. case VL53L1_THRESHOLD_IN_WINDOW:
  460. Mode = VL53L1_GPIOINTMODE_IN_WINDOW;
  461. break;
  462. default:
  463. /* define Mode to avoid warning but actual value doesn't mind */
  464. Mode = VL53L1_GPIOINTMODE_LEVEL_HIGH;
  465. *pStatus = VL53L1_ERROR_INVALID_PARAMS;
  466. }
  467. return Mode;
  468. }
  469. static VL53L1_ThresholdMode ConvertModeFromLLD(VL53L1_Error *pStatus,
  470. VL53L1_GPIO_Interrupt_Mode CrossMode)
  471. {
  472. VL53L1_ThresholdMode Mode;
  473. switch (CrossMode) {
  474. case VL53L1_GPIOINTMODE_LEVEL_LOW:
  475. Mode = VL53L1_THRESHOLD_CROSSED_LOW;
  476. break;
  477. case VL53L1_GPIOINTMODE_LEVEL_HIGH:
  478. Mode = VL53L1_THRESHOLD_CROSSED_HIGH;
  479. break;
  480. case VL53L1_GPIOINTMODE_OUT_OF_WINDOW:
  481. Mode = VL53L1_THRESHOLD_OUT_OF_WINDOW;
  482. break;
  483. case VL53L1_GPIOINTMODE_IN_WINDOW:
  484. Mode = VL53L1_THRESHOLD_IN_WINDOW;
  485. break;
  486. default:
  487. /* define Mode to avoid warning but actual value doesn't mind */
  488. Mode = VL53L1_THRESHOLD_CROSSED_HIGH;
  489. *pStatus = VL53L1_ERROR_UNDEFINED;
  490. }
  491. return Mode;
  492. }
  493. /* Group PAL General Functions */
  494. VL53L1_Error VL53L1_GetVersion(VL53L1_Version_t *pVersion)
  495. {
  496. VL53L1_Error Status = VL53L1_ERROR_NONE;
  497. LOG_FUNCTION_START("");
  498. pVersion->major = VL53L1_IMPLEMENTATION_VER_MAJOR;
  499. pVersion->minor = VL53L1_IMPLEMENTATION_VER_MINOR;
  500. pVersion->build = VL53L1_IMPLEMENTATION_VER_SUB;
  501. pVersion->revision = VL53L1_IMPLEMENTATION_VER_REVISION;
  502. LOG_FUNCTION_END(Status);
  503. return Status;
  504. }
  505. VL53L1_Error VL53L1_GetProductRevision(VL53L1_DEV Dev,
  506. uint8_t *pProductRevisionMajor, uint8_t *pProductRevisionMinor)
  507. {
  508. VL53L1_Error Status = VL53L1_ERROR_NONE;
  509. uint8_t revision_id;
  510. VL53L1_LLDriverData_t *pLLData;
  511. LOG_FUNCTION_START("");
  512. pLLData = VL53L1DevStructGetLLDriverHandle(Dev);
  513. revision_id = pLLData->nvm_copy_data.identification__revision_id;
  514. *pProductRevisionMajor = 1;
  515. *pProductRevisionMinor = (revision_id & 0xF0) >> 4;
  516. LOG_FUNCTION_END(Status);
  517. return Status;
  518. }
  519. VL53L1_Error VL53L1_GetDeviceInfo(VL53L1_DEV Dev,
  520. VL53L1_DeviceInfo_t *pVL53L1_DeviceInfo)
  521. {
  522. VL53L1_Error Status = VL53L1_ERROR_NONE;
  523. uint8_t revision_id;
  524. VL53L1_LLDriverData_t *pLLData;
  525. LOG_FUNCTION_START("");
  526. pLLData = VL53L1DevStructGetLLDriverHandle(Dev);
  527. strncpy(pVL53L1_DeviceInfo->ProductId, "",
  528. VL53L1_DEVINFO_STRLEN-1);
  529. pVL53L1_DeviceInfo->ProductType =
  530. pLLData->nvm_copy_data.identification__module_type;
  531. revision_id = pLLData->nvm_copy_data.identification__revision_id;
  532. pVL53L1_DeviceInfo->ProductRevisionMajor = 1;
  533. pVL53L1_DeviceInfo->ProductRevisionMinor = (revision_id & 0xF0) >> 4;
  534. #ifndef VL53L1_USE_EMPTY_STRING
  535. if (pVL53L1_DeviceInfo->ProductRevisionMinor == 0)
  536. strncpy(pVL53L1_DeviceInfo->Name,
  537. VL53L1_STRING_DEVICE_INFO_NAME0,
  538. VL53L1_DEVINFO_STRLEN-1);
  539. else
  540. strncpy(pVL53L1_DeviceInfo->Name,
  541. VL53L1_STRING_DEVICE_INFO_NAME1,
  542. VL53L1_DEVINFO_STRLEN-1);
  543. strncpy(pVL53L1_DeviceInfo->Type,
  544. VL53L1_STRING_DEVICE_INFO_TYPE,
  545. VL53L1_DEVINFO_STRLEN-1);
  546. #else
  547. pVL53L1_DeviceInfo->Name[0] = 0;
  548. pVL53L1_DeviceInfo->Type[0] = 0;
  549. #endif
  550. LOG_FUNCTION_END(Status);
  551. return Status;
  552. }
  553. VL53L1_Error VL53L1_GetRangeStatusString(uint8_t RangeStatus,
  554. char *pRangeStatusString)
  555. {
  556. VL53L1_Error Status = VL53L1_ERROR_NONE;
  557. LOG_FUNCTION_START("");
  558. Status = VL53L1_get_range_status_string(RangeStatus,
  559. pRangeStatusString);
  560. LOG_FUNCTION_END(Status);
  561. return Status;
  562. }
  563. VL53L1_Error VL53L1_GetPalErrorString(VL53L1_Error PalErrorCode,
  564. char *pPalErrorString)
  565. {
  566. VL53L1_Error Status = VL53L1_ERROR_NONE;
  567. LOG_FUNCTION_START("");
  568. Status = VL53L1_get_pal_error_string(PalErrorCode, pPalErrorString);
  569. LOG_FUNCTION_END(Status);
  570. return Status;
  571. }
  572. VL53L1_Error VL53L1_GetPalStateString(VL53L1_State PalStateCode,
  573. char *pPalStateString)
  574. {
  575. VL53L1_Error Status = VL53L1_ERROR_NONE;
  576. LOG_FUNCTION_START("");
  577. Status = VL53L1_get_pal_state_string(PalStateCode, pPalStateString);
  578. LOG_FUNCTION_END(Status);
  579. return Status;
  580. }
  581. VL53L1_Error VL53L1_GetPalState(VL53L1_DEV Dev, VL53L1_State *pPalState)
  582. {
  583. VL53L1_Error Status = VL53L1_ERROR_NONE;
  584. LOG_FUNCTION_START("");
  585. *pPalState = VL53L1DevDataGet(Dev, PalState);
  586. LOG_FUNCTION_END(Status);
  587. return Status;
  588. }
  589. /* End Group PAL General Functions */
  590. /* Group PAL Init Functions */
  591. VL53L1_Error VL53L1_SetDeviceAddress(VL53L1_DEV Dev, uint8_t DeviceAddress)
  592. {
  593. VL53L1_Error Status = VL53L1_ERROR_NONE;
  594. LOG_FUNCTION_START("");
  595. Status = VL53L1_WrByte(Dev, VL53L1_I2C_SLAVE__DEVICE_ADDRESS,
  596. DeviceAddress / 2);
  597. LOG_FUNCTION_END(Status);
  598. return Status;
  599. }
  600. VL53L1_Error VL53L1_DataInit(VL53L1_DEV Dev)
  601. {
  602. VL53L1_Error Status = VL53L1_ERROR_NONE;
  603. uint8_t i;
  604. LOG_FUNCTION_START("");
  605. /* 2V8 power mode selection codex 447463 */
  606. #ifdef USE_I2C_2V8
  607. Status = VL53L1_RdByte(Dev, VL53L1_PAD_I2C_HV__EXTSUP_CONFIG, &i);
  608. if (Status == VL53L1_ERROR_NONE) {
  609. i = (i & 0xfe) | 0x01;
  610. Status = VL53L1_WrByte(Dev, VL53L1_PAD_I2C_HV__EXTSUP_CONFIG,
  611. i);
  612. }
  613. #endif
  614. if (Status == VL53L1_ERROR_NONE)
  615. Status = VL53L1_data_init(Dev, 1);
  616. if (Status == VL53L1_ERROR_NONE) {
  617. VL53L1DevDataSet(Dev, PalState, VL53L1_STATE_WAIT_STATICINIT);
  618. VL53L1DevDataSet(Dev, CurrentParameters.PresetMode,
  619. VL53L1_PRESETMODE_LOWPOWER_AUTONOMOUS);
  620. }
  621. /* Enable all check */
  622. for (i = 0; i < VL53L1_CHECKENABLE_NUMBER_OF_CHECKS; i++) {
  623. if (Status == VL53L1_ERROR_NONE)
  624. Status |= VL53L1_SetLimitCheckEnable(Dev, i, 1);
  625. else
  626. break;
  627. }
  628. /* Limit default values */
  629. if (Status == VL53L1_ERROR_NONE) {
  630. Status = VL53L1_SetLimitCheckValue(Dev,
  631. VL53L1_CHECKENABLE_SIGMA_FINAL_RANGE,
  632. (FixPoint1616_t)(18 * 65536));
  633. }
  634. if (Status == VL53L1_ERROR_NONE) {
  635. Status = VL53L1_SetLimitCheckValue(Dev,
  636. VL53L1_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE,
  637. (FixPoint1616_t)(25 * 65536 / 100));
  638. /* 0.25 * 65536 */
  639. }
  640. LOG_FUNCTION_END(Status);
  641. return Status;
  642. }
  643. VL53L1_Error VL53L1_StaticInit(VL53L1_DEV Dev)
  644. {
  645. VL53L1_Error Status = VL53L1_ERROR_NONE;
  646. uint8_t measurement_mode;
  647. LOG_FUNCTION_START("");
  648. VL53L1DevDataSet(Dev, PalState, VL53L1_STATE_IDLE);
  649. measurement_mode = VL53L1_DEVICEMEASUREMENTMODE_BACKTOBACK;
  650. VL53L1DevDataSet(Dev, LLData.measurement_mode, measurement_mode);
  651. VL53L1DevDataSet(Dev, CurrentParameters.NewDistanceMode,
  652. VL53L1_DISTANCEMODE_LONG);
  653. VL53L1DevDataSet(Dev, CurrentParameters.InternalDistanceMode,
  654. VL53L1_DISTANCEMODE_LONG);
  655. VL53L1DevDataSet(Dev, CurrentParameters.DistanceMode,
  656. VL53L1_DISTANCEMODE_LONG);
  657. /* ticket 472728 fix */
  658. Status = VL53L1_SetPresetMode(Dev,
  659. VL53L1_PRESETMODE_LOWPOWER_AUTONOMOUS);
  660. /* end of ticket 472728 fix */
  661. LOG_FUNCTION_END(Status);
  662. return Status;
  663. }
  664. VL53L1_Error VL53L1_WaitDeviceBooted(VL53L1_DEV Dev)
  665. {
  666. VL53L1_Error Status = VL53L1_ERROR_NONE;
  667. LOG_FUNCTION_START("");
  668. Status = VL53L1_poll_for_boot_completion(Dev,
  669. VL53L1_BOOT_COMPLETION_POLLING_TIMEOUT_MS);
  670. LOG_FUNCTION_END(Status);
  671. return Status;
  672. }
  673. /* End Group PAL Init Functions */
  674. /* Group PAL Parameters Functions */
  675. static VL53L1_Error ComputeDevicePresetMode(
  676. VL53L1_PresetModes PresetMode,
  677. VL53L1_DistanceModes DistanceMode,
  678. VL53L1_DevicePresetModes *pDevicePresetMode)
  679. {
  680. VL53L1_Error Status = VL53L1_ERROR_NONE;
  681. uint8_t DistIdx;
  682. VL53L1_DevicePresetModes LightModes[3] = {
  683. VL53L1_DEVICEPRESETMODE_STANDARD_RANGING_SHORT_RANGE,
  684. VL53L1_DEVICEPRESETMODE_STANDARD_RANGING,
  685. VL53L1_DEVICEPRESETMODE_STANDARD_RANGING_LONG_RANGE};
  686. VL53L1_DevicePresetModes TimedModes[3] = {
  687. VL53L1_DEVICEPRESETMODE_TIMED_RANGING_SHORT_RANGE,
  688. VL53L1_DEVICEPRESETMODE_TIMED_RANGING,
  689. VL53L1_DEVICEPRESETMODE_TIMED_RANGING_LONG_RANGE};
  690. VL53L1_DevicePresetModes LowPowerTimedModes[3] = {
  691. VL53L1_DEVICEPRESETMODE_LOWPOWERAUTO_SHORT_RANGE,
  692. VL53L1_DEVICEPRESETMODE_LOWPOWERAUTO_MEDIUM_RANGE,
  693. VL53L1_DEVICEPRESETMODE_LOWPOWERAUTO_LONG_RANGE};
  694. *pDevicePresetMode = VL53L1_DEVICEPRESETMODE_STANDARD_RANGING;
  695. switch (DistanceMode) {
  696. case VL53L1_DISTANCEMODE_SHORT:
  697. DistIdx = 0;
  698. break;
  699. case VL53L1_DISTANCEMODE_MEDIUM:
  700. DistIdx = 1;
  701. break;
  702. default:
  703. DistIdx = 2;
  704. }
  705. switch (PresetMode) {
  706. case VL53L1_PRESETMODE_LITE_RANGING:
  707. *pDevicePresetMode = LightModes[DistIdx];
  708. break;
  709. case VL53L1_PRESETMODE_AUTONOMOUS:
  710. *pDevicePresetMode = TimedModes[DistIdx];
  711. break;
  712. case VL53L1_PRESETMODE_LOWPOWER_AUTONOMOUS:
  713. *pDevicePresetMode = LowPowerTimedModes[DistIdx];
  714. break;
  715. default:
  716. /* Unsupported mode */
  717. Status = VL53L1_ERROR_MODE_NOT_SUPPORTED;
  718. }
  719. return Status;
  720. }
  721. static VL53L1_Error SetPresetMode(VL53L1_DEV Dev,
  722. VL53L1_PresetModes PresetMode,
  723. VL53L1_DistanceModes DistanceMode,
  724. uint32_t inter_measurement_period_ms)
  725. {
  726. VL53L1_Error Status = VL53L1_ERROR_NONE;
  727. VL53L1_DevicePresetModes device_preset_mode;
  728. uint8_t measurement_mode;
  729. uint16_t dss_config__target_total_rate_mcps;
  730. uint32_t phasecal_config_timeout_us;
  731. uint32_t mm_config_timeout_us;
  732. uint32_t lld_range_config_timeout_us;
  733. LOG_FUNCTION_START("%d", (int)PresetMode);
  734. if ((PresetMode == VL53L1_PRESETMODE_AUTONOMOUS) ||
  735. (PresetMode == VL53L1_PRESETMODE_LOWPOWER_AUTONOMOUS))
  736. measurement_mode = VL53L1_DEVICEMEASUREMENTMODE_TIMED;
  737. else
  738. measurement_mode = VL53L1_DEVICEMEASUREMENTMODE_BACKTOBACK;
  739. Status = ComputeDevicePresetMode(PresetMode, DistanceMode,
  740. &device_preset_mode);
  741. if (Status == VL53L1_ERROR_NONE)
  742. Status = VL53L1_get_preset_mode_timing_cfg(Dev,
  743. device_preset_mode,
  744. &dss_config__target_total_rate_mcps,
  745. &phasecal_config_timeout_us,
  746. &mm_config_timeout_us,
  747. &lld_range_config_timeout_us);
  748. if (Status == VL53L1_ERROR_NONE)
  749. Status = VL53L1_set_preset_mode(
  750. Dev,
  751. device_preset_mode,
  752. dss_config__target_total_rate_mcps,
  753. phasecal_config_timeout_us,
  754. mm_config_timeout_us,
  755. lld_range_config_timeout_us,
  756. inter_measurement_period_ms);
  757. if (Status == VL53L1_ERROR_NONE)
  758. VL53L1DevDataSet(Dev, LLData.measurement_mode, measurement_mode);
  759. if (Status == VL53L1_ERROR_NONE)
  760. VL53L1DevDataSet(Dev, CurrentParameters.PresetMode, PresetMode);
  761. LOG_FUNCTION_END(Status);
  762. return Status;
  763. }
  764. VL53L1_Error VL53L1_SetPresetMode(VL53L1_DEV Dev, VL53L1_PresetModes PresetMode)
  765. {
  766. VL53L1_Error Status = VL53L1_ERROR_NONE;
  767. VL53L1_DistanceModes DistanceMode = VL53L1_DISTANCEMODE_LONG;
  768. LOG_FUNCTION_START("%d", (int)PresetMode);
  769. Status = SetPresetMode(Dev,
  770. PresetMode,
  771. DistanceMode,
  772. 1000);
  773. if (Status == VL53L1_ERROR_NONE) {
  774. VL53L1DevDataSet(Dev, CurrentParameters.InternalDistanceMode,
  775. DistanceMode);
  776. VL53L1DevDataSet(Dev, CurrentParameters.NewDistanceMode,
  777. DistanceMode);
  778. if ((PresetMode == VL53L1_PRESETMODE_LITE_RANGING) ||
  779. (PresetMode == VL53L1_PRESETMODE_AUTONOMOUS) ||
  780. (PresetMode == VL53L1_PRESETMODE_LOWPOWER_AUTONOMOUS))
  781. Status = VL53L1_SetMeasurementTimingBudgetMicroSeconds(
  782. Dev, 41000);
  783. else
  784. /* Set default timing budget to 30Hz (33.33 ms)*/
  785. Status = VL53L1_SetMeasurementTimingBudgetMicroSeconds(
  786. Dev, 33333);
  787. }
  788. if (Status == VL53L1_ERROR_NONE) {
  789. /* Set default intermeasurement period to 1000 ms */
  790. Status = VL53L1_SetInterMeasurementPeriodMilliSeconds(Dev,
  791. 1000);
  792. }
  793. LOG_FUNCTION_END(Status);
  794. return Status;
  795. }
  796. VL53L1_Error VL53L1_GetPresetMode(VL53L1_DEV Dev,
  797. VL53L1_PresetModes *pPresetMode)
  798. {
  799. VL53L1_Error Status = VL53L1_ERROR_NONE;
  800. LOG_FUNCTION_START("");
  801. *pPresetMode = VL53L1DevDataGet(Dev, CurrentParameters.PresetMode);
  802. LOG_FUNCTION_END(Status);
  803. return Status;
  804. }
  805. VL53L1_Error VL53L1_SetDistanceMode(VL53L1_DEV Dev,
  806. VL53L1_DistanceModes DistanceMode)
  807. {
  808. VL53L1_Error Status = VL53L1_ERROR_NONE;
  809. VL53L1_PresetModes PresetMode;
  810. VL53L1_DistanceModes InternalDistanceMode;
  811. uint32_t inter_measurement_period_ms;
  812. uint32_t TimingBudget;
  813. uint32_t MmTimeoutUs;
  814. uint32_t PhaseCalTimeoutUs;
  815. VL53L1_user_zone_t user_zone;
  816. LOG_FUNCTION_START("%d", (int)DistanceMode);
  817. PresetMode = VL53L1DevDataGet(Dev, CurrentParameters.PresetMode);
  818. /* when the distance mode is valid:
  819. * Manual Mode: all modes
  820. * AUTO AUTO_LITE : LITE_RANGING, RANGING
  821. */
  822. if ((DistanceMode != VL53L1_DISTANCEMODE_SHORT) &&
  823. (DistanceMode != VL53L1_DISTANCEMODE_MEDIUM) &&
  824. (DistanceMode != VL53L1_DISTANCEMODE_LONG))
  825. return VL53L1_ERROR_INVALID_PARAMS;
  826. /* The internal distance mode is limited to Short, Medium or
  827. * long only
  828. */
  829. if (Status == VL53L1_ERROR_NONE) {
  830. if ((DistanceMode == VL53L1_DISTANCEMODE_SHORT) ||
  831. (DistanceMode == VL53L1_DISTANCEMODE_MEDIUM))
  832. InternalDistanceMode = DistanceMode;
  833. else /* (DistanceMode == VL53L1_DISTANCEMODE_LONG) */
  834. InternalDistanceMode = VL53L1_DISTANCEMODE_LONG;
  835. }
  836. if (Status == VL53L1_ERROR_NONE)
  837. Status = VL53L1_get_user_zone(Dev, &user_zone);
  838. inter_measurement_period_ms = VL53L1DevDataGet(Dev,
  839. LLData.inter_measurement_period_ms);
  840. if (Status == VL53L1_ERROR_NONE)
  841. Status = VL53L1_get_timeouts_us(Dev, &PhaseCalTimeoutUs,
  842. &MmTimeoutUs, &TimingBudget);
  843. if (Status == VL53L1_ERROR_NONE)
  844. Status = SetPresetMode(Dev,
  845. PresetMode,
  846. InternalDistanceMode,
  847. inter_measurement_period_ms);
  848. if (Status == VL53L1_ERROR_NONE) {
  849. VL53L1DevDataSet(Dev, CurrentParameters.InternalDistanceMode,
  850. InternalDistanceMode);
  851. VL53L1DevDataSet(Dev, CurrentParameters.NewDistanceMode,
  852. InternalDistanceMode);
  853. VL53L1DevDataSet(Dev, CurrentParameters.DistanceMode,
  854. DistanceMode);
  855. }
  856. if (Status == VL53L1_ERROR_NONE) {
  857. Status = VL53L1_set_timeouts_us(Dev, PhaseCalTimeoutUs,
  858. MmTimeoutUs, TimingBudget);
  859. if (Status == VL53L1_ERROR_NONE)
  860. VL53L1DevDataSet(Dev, LLData.range_config_timeout_us,
  861. TimingBudget);
  862. }
  863. if (Status == VL53L1_ERROR_NONE)
  864. Status = VL53L1_set_user_zone(Dev, &user_zone);
  865. LOG_FUNCTION_END(Status);
  866. return Status;
  867. }
  868. VL53L1_Error VL53L1_GetDistanceMode(VL53L1_DEV Dev,
  869. VL53L1_DistanceModes *pDistanceMode)
  870. {
  871. VL53L1_Error Status = VL53L1_ERROR_NONE;
  872. LOG_FUNCTION_START("");
  873. *pDistanceMode = VL53L1DevDataGet(Dev, CurrentParameters.DistanceMode);
  874. LOG_FUNCTION_END(Status);
  875. return Status;
  876. }
  877. VL53L1_Error VL53L1_SetMeasurementTimingBudgetMicroSeconds(VL53L1_DEV Dev,
  878. uint32_t MeasurementTimingBudgetMicroSeconds)
  879. {
  880. VL53L1_Error Status = VL53L1_ERROR_NONE;
  881. uint8_t Mm1Enabled;
  882. uint8_t Mm2Enabled;
  883. uint32_t TimingGuard;
  884. uint32_t divisor;
  885. uint32_t TimingBudget;
  886. uint32_t MmTimeoutUs;
  887. VL53L1_PresetModes PresetMode;
  888. uint32_t PhaseCalTimeoutUs;
  889. uint32_t vhv;
  890. int32_t vhv_loops;
  891. uint32_t FDAMaxTimingBudgetUs = FDA_MAX_TIMING_BUDGET_US;
  892. LOG_FUNCTION_START("");
  893. /* Timing budget is limited to 10 seconds */
  894. if (MeasurementTimingBudgetMicroSeconds > 10000000)
  895. Status = VL53L1_ERROR_INVALID_PARAMS;
  896. if (Status == VL53L1_ERROR_NONE) {
  897. Status = VL53L1_GetSequenceStepEnable(Dev,
  898. VL53L1_SEQUENCESTEP_MM1, &Mm1Enabled);
  899. }
  900. if (Status == VL53L1_ERROR_NONE) {
  901. Status = VL53L1_GetSequenceStepEnable(Dev,
  902. VL53L1_SEQUENCESTEP_MM2, &Mm2Enabled);
  903. }
  904. if (Status == VL53L1_ERROR_NONE)
  905. Status = VL53L1_get_timeouts_us(Dev,
  906. &PhaseCalTimeoutUs,
  907. &MmTimeoutUs,
  908. &TimingBudget);
  909. if (Status == VL53L1_ERROR_NONE) {
  910. PresetMode = VL53L1DevDataGet(Dev, CurrentParameters.PresetMode);
  911. TimingGuard = 0;
  912. divisor = 1;
  913. switch (PresetMode) {
  914. case VL53L1_PRESETMODE_LITE_RANGING:
  915. if ((Mm1Enabled == 1) || (Mm2Enabled == 1))
  916. TimingGuard = 5000;
  917. else
  918. TimingGuard = 1000;
  919. break;
  920. case VL53L1_PRESETMODE_AUTONOMOUS:
  921. FDAMaxTimingBudgetUs *= 2;
  922. if ((Mm1Enabled == 1) || (Mm2Enabled == 1))
  923. TimingGuard = 26600;
  924. else
  925. TimingGuard = 21600;
  926. divisor = 2;
  927. break;
  928. case VL53L1_PRESETMODE_LOWPOWER_AUTONOMOUS:
  929. FDAMaxTimingBudgetUs *= 2;
  930. vhv = LOWPOWER_AUTO_VHV_LOOP_DURATION_US;
  931. VL53L1_get_tuning_parm(Dev,
  932. VL53L1_TUNINGPARM_LOWPOWERAUTO_VHV_LOOP_BOUND,
  933. &vhv_loops);
  934. if (vhv_loops > 0) {
  935. vhv += vhv_loops *
  936. LOWPOWER_AUTO_VHV_LOOP_DURATION_US;
  937. }
  938. TimingGuard = LOWPOWER_AUTO_OVERHEAD_BEFORE_A_RANGING +
  939. LOWPOWER_AUTO_OVERHEAD_BETWEEN_A_B_RANGING +
  940. vhv;
  941. divisor = 2;
  942. break;
  943. default:
  944. /* Unsupported mode */
  945. Status = VL53L1_ERROR_MODE_NOT_SUPPORTED;
  946. }
  947. if (MeasurementTimingBudgetMicroSeconds <= TimingGuard)
  948. Status = VL53L1_ERROR_INVALID_PARAMS;
  949. else {
  950. TimingBudget = (MeasurementTimingBudgetMicroSeconds
  951. - TimingGuard);
  952. }
  953. if (Status == VL53L1_ERROR_NONE) {
  954. if (TimingBudget > FDAMaxTimingBudgetUs)
  955. Status = VL53L1_ERROR_INVALID_PARAMS;
  956. else {
  957. TimingBudget /= divisor;
  958. Status = VL53L1_set_timeouts_us(
  959. Dev,
  960. PhaseCalTimeoutUs,
  961. MmTimeoutUs,
  962. TimingBudget);
  963. }
  964. if (Status == VL53L1_ERROR_NONE)
  965. VL53L1DevDataSet(Dev,
  966. LLData.range_config_timeout_us,
  967. TimingBudget);
  968. }
  969. }
  970. if (Status == VL53L1_ERROR_NONE) {
  971. VL53L1DevDataSet(Dev,
  972. CurrentParameters.MeasurementTimingBudgetMicroSeconds,
  973. MeasurementTimingBudgetMicroSeconds);
  974. }
  975. LOG_FUNCTION_END(Status);
  976. return Status;
  977. }
  978. VL53L1_Error VL53L1_GetMeasurementTimingBudgetMicroSeconds(VL53L1_DEV Dev,
  979. uint32_t *pMeasurementTimingBudgetMicroSeconds)
  980. {
  981. VL53L1_Error Status = VL53L1_ERROR_NONE;
  982. uint8_t Mm1Enabled = 0;
  983. uint8_t Mm2Enabled = 0;
  984. uint32_t MmTimeoutUs = 0;
  985. uint32_t RangeTimeoutUs = 0;
  986. uint32_t MeasTimingBdg = 0;
  987. uint32_t PhaseCalTimeoutUs = 0;
  988. VL53L1_PresetModes PresetMode;
  989. uint32_t TimingGuard;
  990. uint32_t vhv;
  991. int32_t vhv_loops;
  992. LOG_FUNCTION_START("");
  993. *pMeasurementTimingBudgetMicroSeconds = 0;
  994. if (Status == VL53L1_ERROR_NONE)
  995. Status = VL53L1_GetSequenceStepEnable(Dev,
  996. VL53L1_SEQUENCESTEP_MM1, &Mm1Enabled);
  997. if (Status == VL53L1_ERROR_NONE)
  998. Status = VL53L1_GetSequenceStepEnable(Dev,
  999. VL53L1_SEQUENCESTEP_MM2, &Mm2Enabled);
  1000. if (Status == VL53L1_ERROR_NONE)
  1001. Status = VL53L1_get_timeouts_us(Dev,
  1002. &PhaseCalTimeoutUs,
  1003. &MmTimeoutUs,
  1004. &RangeTimeoutUs);
  1005. if (Status == VL53L1_ERROR_NONE) {
  1006. PresetMode = VL53L1DevDataGet(Dev, CurrentParameters.PresetMode);
  1007. switch (PresetMode) {
  1008. case VL53L1_PRESETMODE_LITE_RANGING:
  1009. if ((Mm1Enabled == 1) || (Mm2Enabled == 1))
  1010. MeasTimingBdg = RangeTimeoutUs + 5000;
  1011. else
  1012. MeasTimingBdg = RangeTimeoutUs + 1000;
  1013. break;
  1014. case VL53L1_PRESETMODE_AUTONOMOUS:
  1015. if ((Mm1Enabled == 1) || (Mm2Enabled == 1))
  1016. MeasTimingBdg = 2 * RangeTimeoutUs + 26600;
  1017. else
  1018. MeasTimingBdg = 2 * RangeTimeoutUs + 21600;
  1019. break;
  1020. case VL53L1_PRESETMODE_LOWPOWER_AUTONOMOUS:
  1021. vhv = LOWPOWER_AUTO_VHV_LOOP_DURATION_US;
  1022. VL53L1_get_tuning_parm(Dev,
  1023. VL53L1_TUNINGPARM_LOWPOWERAUTO_VHV_LOOP_BOUND,
  1024. &vhv_loops);
  1025. if (vhv_loops > 0) {
  1026. vhv += vhv_loops *
  1027. LOWPOWER_AUTO_VHV_LOOP_DURATION_US;
  1028. }
  1029. TimingGuard = LOWPOWER_AUTO_OVERHEAD_BEFORE_A_RANGING +
  1030. LOWPOWER_AUTO_OVERHEAD_BETWEEN_A_B_RANGING +
  1031. vhv;
  1032. MeasTimingBdg = 2 * RangeTimeoutUs + TimingGuard;
  1033. break;
  1034. default:
  1035. /* Unsupported mode */
  1036. Status = VL53L1_ERROR_MODE_NOT_SUPPORTED;
  1037. }
  1038. }
  1039. if (Status == VL53L1_ERROR_NONE)
  1040. *pMeasurementTimingBudgetMicroSeconds = MeasTimingBdg;
  1041. LOG_FUNCTION_END(Status);
  1042. return Status;
  1043. }
  1044. VL53L1_Error VL53L1_SetInterMeasurementPeriodMilliSeconds(VL53L1_DEV Dev,
  1045. uint32_t InterMeasurementPeriodMilliSeconds)
  1046. {
  1047. VL53L1_Error Status = VL53L1_ERROR_NONE;
  1048. uint32_t adjustedIMP;
  1049. LOG_FUNCTION_START("");
  1050. /* Fix for Ticket 468205 actual measurement period shorter than set */
  1051. adjustedIMP = InterMeasurementPeriodMilliSeconds;
  1052. adjustedIMP += (adjustedIMP * 64) / 1000;
  1053. /* End of fix for Ticket 468205 */
  1054. Status = VL53L1_set_inter_measurement_period_ms(Dev,
  1055. adjustedIMP);
  1056. LOG_FUNCTION_END(Status);
  1057. return Status;
  1058. }
  1059. VL53L1_Error VL53L1_GetInterMeasurementPeriodMilliSeconds(VL53L1_DEV Dev,
  1060. uint32_t *pInterMeasurementPeriodMilliSeconds)
  1061. {
  1062. VL53L1_Error Status = VL53L1_ERROR_NONE;
  1063. uint32_t adjustedIMP;
  1064. LOG_FUNCTION_START("");
  1065. Status = VL53L1_get_inter_measurement_period_ms(Dev, &adjustedIMP);
  1066. /* Fix for Ticket 468205 actual measurement period shorter than set */
  1067. adjustedIMP -= (adjustedIMP * 64) / 1000;
  1068. *pInterMeasurementPeriodMilliSeconds = adjustedIMP;
  1069. /* End of fix for Ticket 468205 */
  1070. LOG_FUNCTION_END(Status);
  1071. return Status;
  1072. }
  1073. /* End Group PAL Parameters Functions */
  1074. /* Group Limit check Functions */
  1075. VL53L1_Error VL53L1_GetNumberOfLimitCheck(uint16_t *pNumberOfLimitCheck)
  1076. {
  1077. VL53L1_Error Status = VL53L1_ERROR_NONE;
  1078. LOG_FUNCTION_START("");
  1079. *pNumberOfLimitCheck = VL53L1_CHECKENABLE_NUMBER_OF_CHECKS;
  1080. LOG_FUNCTION_END(Status);
  1081. return Status;
  1082. }
  1083. VL53L1_Error VL53L1_GetLimitCheckInfo(uint16_t LimitCheckId,
  1084. char *pLimitCheckString)
  1085. {
  1086. VL53L1_Error Status = VL53L1_ERROR_NONE;
  1087. LOG_FUNCTION_START("");
  1088. Status = VL53L1_get_limit_check_info(LimitCheckId,
  1089. pLimitCheckString);
  1090. LOG_FUNCTION_END(Status);
  1091. return Status;
  1092. }
  1093. VL53L1_Error VL53L1_GetLimitCheckStatus(VL53L1_DEV Dev, uint16_t LimitCheckId,
  1094. uint8_t *pLimitCheckStatus)
  1095. {
  1096. VL53L1_Error Status = VL53L1_ERROR_NONE;
  1097. uint8_t Temp8;
  1098. LOG_FUNCTION_START("");
  1099. if (LimitCheckId >= VL53L1_CHECKENABLE_NUMBER_OF_CHECKS) {
  1100. Status = VL53L1_ERROR_INVALID_PARAMS;
  1101. } else {
  1102. VL53L1_GETARRAYPARAMETERFIELD(Dev, LimitChecksStatus,
  1103. LimitCheckId, Temp8);
  1104. *pLimitCheckStatus = Temp8;
  1105. }
  1106. LOG_FUNCTION_END(Status);
  1107. return Status;
  1108. }
  1109. static VL53L1_Error SetLimitValue(VL53L1_DEV Dev, uint16_t LimitCheckId,
  1110. FixPoint1616_t value)
  1111. {
  1112. VL53L1_Error Status = VL53L1_ERROR_NONE;
  1113. uint16_t tmpuint16; /* temporary variable */
  1114. LOG_FUNCTION_START("");
  1115. switch (LimitCheckId) {
  1116. case VL53L1_CHECKENABLE_SIGMA_FINAL_RANGE:
  1117. tmpuint16 = VL53L1_FIXPOINT1616TOFIXPOINT142(value);
  1118. VL53L1_set_lite_sigma_threshold(Dev, tmpuint16);
  1119. break;
  1120. case VL53L1_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE:
  1121. tmpuint16 = VL53L1_FIXPOINT1616TOFIXPOINT97(value);
  1122. VL53L1_set_lite_min_count_rate(Dev, tmpuint16);
  1123. break;
  1124. default:
  1125. Status = VL53L1_ERROR_INVALID_PARAMS;
  1126. }
  1127. LOG_FUNCTION_END(Status);
  1128. return Status;
  1129. }
  1130. VL53L1_Error VL53L1_SetLimitCheckEnable(VL53L1_DEV Dev, uint16_t LimitCheckId,
  1131. uint8_t LimitCheckEnable)
  1132. {
  1133. VL53L1_Error Status = VL53L1_ERROR_NONE;
  1134. FixPoint1616_t TempFix1616 = 0;
  1135. LOG_FUNCTION_START("");
  1136. if (LimitCheckId >= VL53L1_CHECKENABLE_NUMBER_OF_CHECKS) {
  1137. Status = VL53L1_ERROR_INVALID_PARAMS;
  1138. } else {
  1139. /* TempFix1616 contains either 0 or the limit value */
  1140. if (LimitCheckEnable == 0)
  1141. TempFix1616 = 0;
  1142. else
  1143. VL53L1_GETARRAYPARAMETERFIELD(Dev, LimitChecksValue,
  1144. LimitCheckId, TempFix1616);
  1145. Status = SetLimitValue(Dev, LimitCheckId, TempFix1616);
  1146. }
  1147. if (Status == VL53L1_ERROR_NONE)
  1148. VL53L1_SETARRAYPARAMETERFIELD(Dev,
  1149. LimitChecksEnable,
  1150. LimitCheckId,
  1151. ((LimitCheckEnable == 0) ? 0 : 1));
  1152. LOG_FUNCTION_END(Status);
  1153. return Status;
  1154. }
  1155. VL53L1_Error VL53L1_GetLimitCheckEnable(VL53L1_DEV Dev, uint16_t LimitCheckId,
  1156. uint8_t *pLimitCheckEnable)
  1157. {
  1158. VL53L1_Error Status = VL53L1_ERROR_NONE;
  1159. uint8_t Temp8;
  1160. LOG_FUNCTION_START("");
  1161. if (LimitCheckId >= VL53L1_CHECKENABLE_NUMBER_OF_CHECKS) {
  1162. Status = VL53L1_ERROR_INVALID_PARAMS;
  1163. *pLimitCheckEnable = 0;
  1164. } else {
  1165. VL53L1_GETARRAYPARAMETERFIELD(Dev, LimitChecksEnable,
  1166. LimitCheckId, Temp8);
  1167. *pLimitCheckEnable = Temp8;
  1168. }
  1169. LOG_FUNCTION_END(Status);
  1170. return Status;
  1171. }
  1172. VL53L1_Error VL53L1_SetLimitCheckValue(VL53L1_DEV Dev, uint16_t LimitCheckId,
  1173. FixPoint1616_t LimitCheckValue)
  1174. {
  1175. VL53L1_Error Status = VL53L1_ERROR_NONE;
  1176. uint8_t LimitChecksEnable;
  1177. LOG_FUNCTION_START("");
  1178. if (LimitCheckId >= VL53L1_CHECKENABLE_NUMBER_OF_CHECKS) {
  1179. Status = VL53L1_ERROR_INVALID_PARAMS;
  1180. } else {
  1181. VL53L1_GETARRAYPARAMETERFIELD(Dev, LimitChecksEnable,
  1182. LimitCheckId,
  1183. LimitChecksEnable);
  1184. if (LimitChecksEnable == 0) {
  1185. /* disabled write only internal value */
  1186. VL53L1_SETARRAYPARAMETERFIELD(Dev, LimitChecksValue,
  1187. LimitCheckId, LimitCheckValue);
  1188. } else {
  1189. Status = SetLimitValue(Dev, LimitCheckId,
  1190. LimitCheckValue);
  1191. if (Status == VL53L1_ERROR_NONE) {
  1192. VL53L1_SETARRAYPARAMETERFIELD(Dev,
  1193. LimitChecksValue,
  1194. LimitCheckId, LimitCheckValue);
  1195. }
  1196. }
  1197. }
  1198. LOG_FUNCTION_END(Status);
  1199. return Status;
  1200. }
  1201. VL53L1_Error VL53L1_GetLimitCheckValue(VL53L1_DEV Dev, uint16_t LimitCheckId,
  1202. FixPoint1616_t *pLimitCheckValue)
  1203. {
  1204. VL53L1_Error Status = VL53L1_ERROR_NONE;
  1205. uint16_t MinCountRate;
  1206. FixPoint1616_t TempFix1616;
  1207. uint16_t SigmaThresh;
  1208. LOG_FUNCTION_START("");
  1209. switch (LimitCheckId) {
  1210. case VL53L1_CHECKENABLE_SIGMA_FINAL_RANGE:
  1211. Status = VL53L1_get_lite_sigma_threshold(Dev, &SigmaThresh);
  1212. TempFix1616 = VL53L1_FIXPOINT142TOFIXPOINT1616(SigmaThresh);
  1213. break;
  1214. case VL53L1_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE:
  1215. Status = VL53L1_get_lite_min_count_rate(Dev, &MinCountRate);
  1216. TempFix1616 = VL53L1_FIXPOINT97TOFIXPOINT1616(MinCountRate);
  1217. break;
  1218. default:
  1219. Status = VL53L1_ERROR_INVALID_PARAMS;
  1220. }
  1221. if (Status == VL53L1_ERROR_NONE) {
  1222. if (TempFix1616 == 0) {
  1223. /* disabled: return value from memory */
  1224. VL53L1_GETARRAYPARAMETERFIELD(Dev,
  1225. LimitChecksValue, LimitCheckId,
  1226. TempFix1616);
  1227. *pLimitCheckValue = TempFix1616;
  1228. VL53L1_SETARRAYPARAMETERFIELD(Dev,
  1229. LimitChecksEnable, LimitCheckId, 0);
  1230. } else {
  1231. *pLimitCheckValue = TempFix1616;
  1232. VL53L1_SETARRAYPARAMETERFIELD(Dev,
  1233. LimitChecksValue, LimitCheckId,
  1234. TempFix1616);
  1235. VL53L1_SETARRAYPARAMETERFIELD(Dev,
  1236. LimitChecksEnable, LimitCheckId, 1);
  1237. }
  1238. }
  1239. LOG_FUNCTION_END(Status);
  1240. return Status;
  1241. }
  1242. VL53L1_Error VL53L1_GetLimitCheckCurrent(VL53L1_DEV Dev, uint16_t LimitCheckId,
  1243. FixPoint1616_t *pLimitCheckCurrent)
  1244. {
  1245. VL53L1_Error Status = VL53L1_ERROR_NONE;
  1246. FixPoint1616_t TempFix1616 = 0;
  1247. LOG_FUNCTION_START("");
  1248. if (LimitCheckId >= VL53L1_CHECKENABLE_NUMBER_OF_CHECKS) {
  1249. Status = VL53L1_ERROR_INVALID_PARAMS;
  1250. } else {
  1251. VL53L1_GETARRAYPARAMETERFIELD(Dev, LimitChecksCurrent,
  1252. LimitCheckId, TempFix1616);
  1253. *pLimitCheckCurrent = TempFix1616;
  1254. }
  1255. LOG_FUNCTION_END(Status);
  1256. return Status;
  1257. }
  1258. /* End Group Limit check Functions */
  1259. /* Group ROI Functions */
  1260. VL53L1_Error VL53L1_SetUserROI(VL53L1_DEV Dev,
  1261. VL53L1_UserRoi_t *pRoi)
  1262. {
  1263. VL53L1_Error Status = VL53L1_ERROR_NONE;
  1264. VL53L1_user_zone_t user_zone;
  1265. Status = CheckValidRectRoi(*pRoi);
  1266. if (Status != VL53L1_ERROR_NONE)
  1267. return VL53L1_ERROR_INVALID_PARAMS;
  1268. user_zone.x_centre = (pRoi->BotRightX + pRoi->TopLeftX + 1) / 2;
  1269. user_zone.y_centre = (pRoi->TopLeftY + pRoi->BotRightY + 1) / 2;
  1270. user_zone.width = (pRoi->BotRightX - pRoi->TopLeftX);
  1271. user_zone.height = (pRoi->TopLeftY - pRoi->BotRightY);
  1272. if ((user_zone.width < 3) || (user_zone.height < 3))
  1273. Status = VL53L1_ERROR_INVALID_PARAMS;
  1274. else
  1275. Status = VL53L1_set_user_zone(Dev, &user_zone);
  1276. LOG_FUNCTION_END(Status);
  1277. return Status;
  1278. }
  1279. VL53L1_Error VL53L1_GetUserROI(VL53L1_DEV Dev,
  1280. VL53L1_UserRoi_t *pRoi)
  1281. {
  1282. VL53L1_Error Status = VL53L1_ERROR_NONE;
  1283. VL53L1_user_zone_t user_zone;
  1284. Status = VL53L1_get_user_zone(Dev, &user_zone);
  1285. pRoi->TopLeftX = (2 * user_zone.x_centre - user_zone.width) >> 1;
  1286. pRoi->TopLeftY = (2 * user_zone.y_centre + user_zone.height) >> 1;
  1287. pRoi->BotRightX = (2 * user_zone.x_centre + user_zone.width) >> 1;
  1288. pRoi->BotRightY = (2 * user_zone.y_centre - user_zone.height) >> 1;
  1289. LOG_FUNCTION_END(Status);
  1290. return Status;
  1291. }
  1292. /* End Group ROI Functions */
  1293. /* Group Sequence Step Functions */
  1294. VL53L1_Error VL53L1_GetNumberOfSequenceSteps(VL53L1_DEV Dev,
  1295. uint8_t *pNumberOfSequenceSteps)
  1296. {
  1297. VL53L1_Error Status = VL53L1_ERROR_NONE;
  1298. SUPPRESS_UNUSED_WARNING(Dev);
  1299. LOG_FUNCTION_START("");
  1300. *pNumberOfSequenceSteps = VL53L1_SEQUENCESTEP_NUMBER_OF_ITEMS;
  1301. LOG_FUNCTION_END(Status);
  1302. return Status;
  1303. }
  1304. VL53L1_Error VL53L1_GetSequenceStepsInfo(VL53L1_SequenceStepId SequenceStepId,
  1305. char *pSequenceStepsString)
  1306. {
  1307. VL53L1_Error Status = VL53L1_ERROR_NONE;
  1308. LOG_FUNCTION_START("");
  1309. Status = VL53L1_get_sequence_steps_info(
  1310. SequenceStepId,
  1311. pSequenceStepsString);
  1312. LOG_FUNCTION_END(Status);
  1313. return Status;
  1314. }
  1315. VL53L1_Error VL53L1_SetSequenceStepEnable(VL53L1_DEV Dev,
  1316. VL53L1_SequenceStepId SequenceStepId, uint8_t SequenceStepEnabled)
  1317. {
  1318. VL53L1_Error Status = VL53L1_ERROR_NONE;
  1319. uint32_t MeasurementTimingBudgetMicroSeconds;
  1320. LOG_FUNCTION_START("");
  1321. /* the VL53L1_SequenceStepId correspond to the LLD
  1322. * VL53L1_DeviceSequenceConfig
  1323. */
  1324. Status = VL53L1_set_sequence_config_bit(Dev,
  1325. (VL53L1_DeviceSequenceConfig)SequenceStepId,
  1326. SequenceStepEnabled);
  1327. /* Apply New Setting */
  1328. if (Status == VL53L1_ERROR_NONE) {
  1329. /* Recalculate timing budget */
  1330. MeasurementTimingBudgetMicroSeconds = VL53L1DevDataGet(Dev,
  1331. CurrentParameters.MeasurementTimingBudgetMicroSeconds);
  1332. VL53L1_SetMeasurementTimingBudgetMicroSeconds(Dev,
  1333. MeasurementTimingBudgetMicroSeconds);
  1334. }
  1335. LOG_FUNCTION_END(Status);
  1336. return Status;
  1337. }
  1338. VL53L1_Error VL53L1_GetSequenceStepEnable(VL53L1_DEV Dev,
  1339. VL53L1_SequenceStepId SequenceStepId, uint8_t *pSequenceStepEnabled)
  1340. {
  1341. VL53L1_Error Status = VL53L1_ERROR_NONE;
  1342. LOG_FUNCTION_START("");
  1343. Status = VL53L1_get_sequence_config_bit(Dev,
  1344. (VL53L1_DeviceSequenceConfig)SequenceStepId,
  1345. pSequenceStepEnabled);
  1346. LOG_FUNCTION_END(Status);
  1347. return Status;
  1348. }
  1349. /* End Group Sequence Step Functions Functions */
  1350. /* Group PAL Measurement Functions */
  1351. VL53L1_Error VL53L1_StartMeasurement(VL53L1_DEV Dev)
  1352. {
  1353. #define TIMED_MODE_TIMING_GUARD_MILLISECONDS 4
  1354. VL53L1_Error Status = VL53L1_ERROR_NONE;
  1355. uint8_t DeviceMeasurementMode;
  1356. VL53L1_State CurrPalState;
  1357. VL53L1_Error lStatus;
  1358. uint32_t MTBus, IMPms;
  1359. LOG_FUNCTION_START("");
  1360. CurrPalState = VL53L1DevDataGet(Dev, PalState);
  1361. switch (CurrPalState) {
  1362. case VL53L1_STATE_IDLE:
  1363. Status = VL53L1_ERROR_NONE;
  1364. break;
  1365. case VL53L1_STATE_POWERDOWN:
  1366. case VL53L1_STATE_WAIT_STATICINIT:
  1367. case VL53L1_STATE_STANDBY:
  1368. case VL53L1_STATE_RUNNING:
  1369. case VL53L1_STATE_RESET:
  1370. case VL53L1_STATE_UNKNOWN:
  1371. case VL53L1_STATE_ERROR:
  1372. Status = VL53L1_ERROR_INVALID_COMMAND;
  1373. break;
  1374. default:
  1375. Status = VL53L1_ERROR_UNDEFINED;
  1376. }
  1377. DeviceMeasurementMode = VL53L1DevDataGet(Dev, LLData.measurement_mode);
  1378. /* Check timing configuration between timing budget and
  1379. * inter measurement period */
  1380. if ((Status == VL53L1_ERROR_NONE) &&
  1381. (DeviceMeasurementMode == VL53L1_DEVICEMEASUREMENTMODE_TIMED)) {
  1382. lStatus = VL53L1_GetMeasurementTimingBudgetMicroSeconds(Dev,
  1383. &MTBus);
  1384. /* convert timing budget in ms */
  1385. MTBus /= 1000;
  1386. lStatus = VL53L1_GetInterMeasurementPeriodMilliSeconds(Dev,
  1387. &IMPms);
  1388. /* trick to get rid of compiler "set but not used" warning */
  1389. SUPPRESS_UNUSED_WARNING(lStatus);
  1390. if (IMPms < MTBus + TIMED_MODE_TIMING_GUARD_MILLISECONDS)
  1391. Status = VL53L1_ERROR_INVALID_PARAMS;
  1392. }
  1393. if (Status == VL53L1_ERROR_NONE)
  1394. Status = VL53L1_init_and_start_range(
  1395. Dev,
  1396. DeviceMeasurementMode,
  1397. VL53L1_DEVICECONFIGLEVEL_FULL);
  1398. /* Set PAL State to Running */
  1399. if (Status == VL53L1_ERROR_NONE)
  1400. VL53L1DevDataSet(Dev, PalState, VL53L1_STATE_RUNNING);
  1401. LOG_FUNCTION_END(Status);
  1402. return Status;
  1403. }
  1404. VL53L1_Error VL53L1_StopMeasurement(VL53L1_DEV Dev)
  1405. {
  1406. VL53L1_Error Status = VL53L1_ERROR_NONE;
  1407. LOG_FUNCTION_START("");
  1408. Status = VL53L1_stop_range(Dev);
  1409. /* Set PAL State to Idle */
  1410. if (Status == VL53L1_ERROR_NONE)
  1411. VL53L1DevDataSet(Dev, PalState, VL53L1_STATE_IDLE);
  1412. LOG_FUNCTION_END(Status);
  1413. return Status;
  1414. }
  1415. static VL53L1_Error ChangePresetMode(VL53L1_DEV Dev)
  1416. {
  1417. VL53L1_Error Status = VL53L1_ERROR_NONE;
  1418. VL53L1_PresetModes PresetMode;
  1419. VL53L1_DistanceModes NewDistanceMode;
  1420. VL53L1_user_zone_t user_zone;
  1421. uint32_t TimingBudget;
  1422. uint32_t MmTimeoutUs;
  1423. uint32_t PhaseCalTimeoutUs;
  1424. uint8_t DeviceMeasurementMode;
  1425. uint32_t inter_measurement_period_ms;
  1426. LOG_FUNCTION_START("");
  1427. Status = VL53L1_get_user_zone(Dev, &user_zone);
  1428. /* Initialize variables fix ticket EwokP #475395 */
  1429. PresetMode = VL53L1DevDataGet(Dev,
  1430. CurrentParameters.PresetMode);
  1431. NewDistanceMode = VL53L1DevDataGet(Dev,
  1432. CurrentParameters.NewDistanceMode);
  1433. /* End of Initialize variables fix ticket EwokP #475395 */
  1434. if (Status == VL53L1_ERROR_NONE)
  1435. Status = VL53L1_get_timeouts_us(Dev, &PhaseCalTimeoutUs,
  1436. &MmTimeoutUs, &TimingBudget);
  1437. if (Status == VL53L1_ERROR_NONE)
  1438. Status = VL53L1_stop_range(Dev);
  1439. if (Status == VL53L1_ERROR_NONE)
  1440. Status = VL53L1_WaitUs(Dev, 500);
  1441. if (Status == VL53L1_ERROR_NONE) {
  1442. inter_measurement_period_ms = VL53L1DevDataGet(Dev,
  1443. LLData.inter_measurement_period_ms);
  1444. Status = SetPresetMode(Dev,
  1445. PresetMode,
  1446. NewDistanceMode,
  1447. inter_measurement_period_ms);
  1448. }
  1449. if (Status == VL53L1_ERROR_NONE) {
  1450. Status = VL53L1_set_timeouts_us(Dev, PhaseCalTimeoutUs,
  1451. MmTimeoutUs, TimingBudget);
  1452. if (Status == VL53L1_ERROR_NONE)
  1453. VL53L1DevDataSet(Dev, LLData.range_config_timeout_us,
  1454. TimingBudget);
  1455. }
  1456. if (Status == VL53L1_ERROR_NONE)
  1457. Status = VL53L1_set_user_zone(Dev, &user_zone);
  1458. if (Status == VL53L1_ERROR_NONE) {
  1459. DeviceMeasurementMode = VL53L1DevDataGet(Dev,
  1460. LLData.measurement_mode);
  1461. Status = VL53L1_init_and_start_range(
  1462. Dev,
  1463. DeviceMeasurementMode,
  1464. VL53L1_DEVICECONFIGLEVEL_FULL);
  1465. }
  1466. if (Status == VL53L1_ERROR_NONE)
  1467. VL53L1DevDataSet(Dev,
  1468. CurrentParameters.InternalDistanceMode,
  1469. NewDistanceMode);
  1470. LOG_FUNCTION_END(Status);
  1471. return Status;
  1472. }
  1473. VL53L1_Error VL53L1_ClearInterruptAndStartMeasurement(VL53L1_DEV Dev)
  1474. {
  1475. VL53L1_Error Status = VL53L1_ERROR_NONE;
  1476. uint8_t DeviceMeasurementMode;
  1477. VL53L1_DistanceModes InternalDistanceMode;
  1478. VL53L1_DistanceModes NewDistanceMode;
  1479. LOG_FUNCTION_START("");
  1480. DeviceMeasurementMode = VL53L1DevDataGet(Dev, LLData.measurement_mode);
  1481. InternalDistanceMode = VL53L1DevDataGet(Dev,
  1482. CurrentParameters.InternalDistanceMode);
  1483. NewDistanceMode = VL53L1DevDataGet(Dev,
  1484. CurrentParameters.NewDistanceMode);
  1485. if (NewDistanceMode != InternalDistanceMode)
  1486. Status = ChangePresetMode(Dev);
  1487. else
  1488. Status = VL53L1_clear_interrupt_and_enable_next_range(
  1489. Dev,
  1490. DeviceMeasurementMode);
  1491. LOG_FUNCTION_END(Status);
  1492. return Status;
  1493. }
  1494. VL53L1_Error VL53L1_GetMeasurementDataReady(VL53L1_DEV Dev,
  1495. uint8_t *pMeasurementDataReady)
  1496. {
  1497. VL53L1_Error Status = VL53L1_ERROR_NONE;
  1498. LOG_FUNCTION_START("");
  1499. Status = VL53L1_is_new_data_ready(Dev, pMeasurementDataReady);
  1500. LOG_FUNCTION_END(Status);
  1501. return Status;
  1502. }
  1503. VL53L1_Error VL53L1_WaitMeasurementDataReady(VL53L1_DEV Dev)
  1504. {
  1505. VL53L1_Error Status = VL53L1_ERROR_NONE;
  1506. LOG_FUNCTION_START("");
  1507. /* Note that the timeout is given by:
  1508. * VL53L1_RANGE_COMPLETION_POLLING_TIMEOUT_MS defined in def.h
  1509. */
  1510. Status = VL53L1_poll_for_range_completion(Dev,
  1511. VL53L1_RANGE_COMPLETION_POLLING_TIMEOUT_MS);
  1512. LOG_FUNCTION_END(Status);
  1513. return Status;
  1514. }
  1515. static uint8_t ComputeRQL(uint8_t active_results,
  1516. uint8_t FilteredRangeStatus,
  1517. VL53L1_range_data_t *presults_data)
  1518. {
  1519. int16_t SRL = 300;
  1520. uint16_t SRAS = 30;
  1521. FixPoint1616_t RAS;
  1522. FixPoint1616_t SRQL;
  1523. FixPoint1616_t GI = 7713587; /* 117.7 * 65536 */
  1524. FixPoint1616_t GGm = 3198157; /* 48.8 * 65536 */
  1525. FixPoint1616_t LRAP = 6554; /* 0.1 * 65536 */
  1526. FixPoint1616_t partial;
  1527. uint8_t finalvalue;
  1528. uint8_t returnvalue;
  1529. if (active_results == 0)
  1530. returnvalue = 0;
  1531. else if (FilteredRangeStatus == VL53L1_DEVICEERROR_PHASECONSISTENCY)
  1532. returnvalue = 50;
  1533. else {
  1534. if (presults_data->median_range_mm < SRL)
  1535. RAS = SRAS * 65536;
  1536. else
  1537. RAS = LRAP * presults_data->median_range_mm;
  1538. /* Fix1616 + (fix1616 * uint16_t / fix1616) * 65536 = fix1616 */
  1539. if (RAS != 0) {
  1540. partial = (GGm * presults_data->sigma_mm);
  1541. partial = partial + (RAS >> 1);
  1542. partial = partial / RAS;
  1543. partial = partial * 65536;
  1544. if (partial <= GI)
  1545. SRQL = GI - partial;
  1546. else
  1547. SRQL = 50 * 65536;
  1548. } else
  1549. SRQL = 100 * 65536;
  1550. finalvalue = (uint8_t)(SRQL >> 16);
  1551. returnvalue = MAX(50, MIN(100, finalvalue));
  1552. }
  1553. return returnvalue;
  1554. }
  1555. static uint8_t ConvertStatusLite(uint8_t FilteredRangeStatus)
  1556. {
  1557. uint8_t RangeStatus;
  1558. switch (FilteredRangeStatus) {
  1559. case VL53L1_DEVICEERROR_GPHSTREAMCOUNT0READY:
  1560. RangeStatus = VL53L1_RANGESTATUS_SYNCRONISATION_INT;
  1561. break;
  1562. case VL53L1_DEVICEERROR_RANGECOMPLETE_NO_WRAP_CHECK:
  1563. RangeStatus = VL53L1_RANGESTATUS_RANGE_VALID_NO_WRAP_CHECK_FAIL;
  1564. break;
  1565. case VL53L1_DEVICEERROR_RANGEPHASECHECK:
  1566. RangeStatus = VL53L1_RANGESTATUS_OUTOFBOUNDS_FAIL;
  1567. break;
  1568. case VL53L1_DEVICEERROR_MSRCNOTARGET:
  1569. RangeStatus = VL53L1_RANGESTATUS_SIGNAL_FAIL;
  1570. break;
  1571. case VL53L1_DEVICEERROR_SIGMATHRESHOLDCHECK:
  1572. RangeStatus = VL53L1_RANGESTATUS_SIGMA_FAIL;
  1573. break;
  1574. case VL53L1_DEVICEERROR_PHASECONSISTENCY:
  1575. RangeStatus = VL53L1_RANGESTATUS_WRAP_TARGET_FAIL;
  1576. break;
  1577. case VL53L1_DEVICEERROR_RANGEIGNORETHRESHOLD:
  1578. RangeStatus = VL53L1_RANGESTATUS_XTALK_SIGNAL_FAIL;
  1579. break;
  1580. case VL53L1_DEVICEERROR_MINCLIP:
  1581. RangeStatus = VL53L1_RANGESTATUS_RANGE_VALID_MIN_RANGE_CLIPPED;
  1582. break;
  1583. case VL53L1_DEVICEERROR_RANGECOMPLETE:
  1584. RangeStatus = VL53L1_RANGESTATUS_RANGE_VALID;
  1585. break;
  1586. default:
  1587. RangeStatus = VL53L1_RANGESTATUS_NONE;
  1588. }
  1589. return RangeStatus;
  1590. }
  1591. static VL53L1_Error SetSimpleData(VL53L1_DEV Dev,
  1592. uint8_t active_results, uint8_t device_status,
  1593. VL53L1_range_data_t *presults_data,
  1594. VL53L1_RangingMeasurementData_t *pRangeData)
  1595. {
  1596. VL53L1_Error Status = VL53L1_ERROR_NONE;
  1597. uint8_t FilteredRangeStatus;
  1598. uint8_t SigmaLimitflag;
  1599. uint8_t SignalLimitflag;
  1600. uint8_t Temp8Enable;
  1601. uint8_t Temp8;
  1602. FixPoint1616_t AmbientRate;
  1603. FixPoint1616_t SignalRate;
  1604. FixPoint1616_t TempFix1616;
  1605. FixPoint1616_t LimitCheckValue;
  1606. int16_t Range;
  1607. pRangeData->TimeStamp = presults_data->time_stamp;
  1608. FilteredRangeStatus = presults_data->range_status & 0x1F;
  1609. pRangeData->RangeQualityLevel = ComputeRQL(active_results,
  1610. FilteredRangeStatus,
  1611. presults_data);
  1612. SignalRate = VL53L1_FIXPOINT97TOFIXPOINT1616(
  1613. presults_data->peak_signal_count_rate_mcps);
  1614. pRangeData->SignalRateRtnMegaCps
  1615. = SignalRate;
  1616. AmbientRate = VL53L1_FIXPOINT97TOFIXPOINT1616(
  1617. presults_data->ambient_count_rate_mcps);
  1618. pRangeData->AmbientRateRtnMegaCps = AmbientRate;
  1619. pRangeData->EffectiveSpadRtnCount =
  1620. presults_data->actual_effective_spads;
  1621. TempFix1616 = VL53L1_FIXPOINT97TOFIXPOINT1616(
  1622. presults_data->sigma_mm);
  1623. pRangeData->SigmaMilliMeter = TempFix1616;
  1624. pRangeData->RangeMilliMeter = presults_data->median_range_mm;
  1625. pRangeData->RangeFractionalPart = 0;
  1626. /* Treat device error status first */
  1627. switch (device_status) {
  1628. case VL53L1_DEVICEERROR_MULTCLIPFAIL:
  1629. case VL53L1_DEVICEERROR_VCSELWATCHDOGTESTFAILURE:
  1630. case VL53L1_DEVICEERROR_VCSELCONTINUITYTESTFAILURE:
  1631. case VL53L1_DEVICEERROR_NOVHVVALUEFOUND:
  1632. pRangeData->RangeStatus = VL53L1_RANGESTATUS_HARDWARE_FAIL;
  1633. break;
  1634. case VL53L1_DEVICEERROR_USERROICLIP:
  1635. pRangeData->RangeStatus = VL53L1_RANGESTATUS_MIN_RANGE_FAIL;
  1636. break;
  1637. default:
  1638. pRangeData->RangeStatus = VL53L1_RANGESTATUS_RANGE_VALID;
  1639. }
  1640. /* Now deal with range status according to the ranging preset */
  1641. if (pRangeData->RangeStatus == VL53L1_RANGESTATUS_RANGE_VALID) {
  1642. pRangeData->RangeStatus =
  1643. ConvertStatusLite(FilteredRangeStatus);
  1644. }
  1645. /* Update current Limit Check */
  1646. TempFix1616 = VL53L1_FIXPOINT97TOFIXPOINT1616(
  1647. presults_data->sigma_mm);
  1648. VL53L1_SETARRAYPARAMETERFIELD(Dev,
  1649. LimitChecksCurrent, VL53L1_CHECKENABLE_SIGMA_FINAL_RANGE,
  1650. TempFix1616);
  1651. TempFix1616 = VL53L1_FIXPOINT97TOFIXPOINT1616(
  1652. presults_data->peak_signal_count_rate_mcps);
  1653. VL53L1_SETARRAYPARAMETERFIELD(Dev,
  1654. LimitChecksCurrent, VL53L1_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE,
  1655. TempFix1616);
  1656. /* Update Limit Check Status */
  1657. /* Sigma */
  1658. VL53L1_GetLimitCheckValue(Dev,
  1659. VL53L1_CHECKENABLE_SIGMA_FINAL_RANGE,
  1660. &LimitCheckValue);
  1661. SigmaLimitflag = (FilteredRangeStatus ==
  1662. VL53L1_DEVICEERROR_SIGMATHRESHOLDCHECK)
  1663. ? 1 : 0;
  1664. VL53L1_GetLimitCheckEnable(Dev,
  1665. VL53L1_CHECKENABLE_SIGMA_FINAL_RANGE,
  1666. &Temp8Enable);
  1667. Temp8 = ((Temp8Enable == 1) && (SigmaLimitflag == 1)) ? 1 : 0;
  1668. VL53L1_SETARRAYPARAMETERFIELD(Dev, LimitChecksStatus,
  1669. VL53L1_CHECKENABLE_SIGMA_FINAL_RANGE, Temp8);
  1670. /* Signal Rate */
  1671. VL53L1_GetLimitCheckValue(Dev,
  1672. VL53L1_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE,
  1673. &LimitCheckValue);
  1674. SignalLimitflag = (FilteredRangeStatus ==
  1675. VL53L1_DEVICEERROR_MSRCNOTARGET)
  1676. ? 1 : 0;
  1677. VL53L1_GetLimitCheckEnable(Dev,
  1678. VL53L1_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE,
  1679. &Temp8Enable);
  1680. Temp8 = ((Temp8Enable == 1) && (SignalLimitflag == 1)) ? 1 : 0;
  1681. VL53L1_SETARRAYPARAMETERFIELD(Dev, LimitChecksStatus,
  1682. VL53L1_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE, Temp8);
  1683. Range = pRangeData->RangeMilliMeter;
  1684. if ((pRangeData->RangeStatus == VL53L1_RANGESTATUS_RANGE_VALID) &&
  1685. (Range < 0)) {
  1686. if (Range < BDTable[VL53L1_TUNING_PROXY_MIN])
  1687. pRangeData->RangeStatus =
  1688. VL53L1_RANGESTATUS_RANGE_INVALID;
  1689. else
  1690. pRangeData->RangeMilliMeter = 0;
  1691. }
  1692. return Status;
  1693. }
  1694. VL53L1_Error VL53L1_GetRangingMeasurementData(VL53L1_DEV Dev,
  1695. VL53L1_RangingMeasurementData_t *pRangingMeasurementData)
  1696. {
  1697. VL53L1_Error Status = VL53L1_ERROR_NONE;
  1698. VL53L1_range_results_t results;
  1699. VL53L1_range_results_t *presults = &results;
  1700. VL53L1_range_data_t *presults_data;
  1701. LOG_FUNCTION_START("");
  1702. /* Clear Ranging Data */
  1703. memset(pRangingMeasurementData, 0xFF,
  1704. sizeof(VL53L1_RangingMeasurementData_t));
  1705. /* Get Ranging Data */
  1706. Status = VL53L1_get_device_results(
  1707. Dev,
  1708. VL53L1_DEVICERESULTSLEVEL_FULL,
  1709. presults);
  1710. if (Status == VL53L1_ERROR_NONE) {
  1711. pRangingMeasurementData->StreamCount = presults->stream_count;
  1712. /* in case of lite ranging or autonomous the following function
  1713. * returns index = 0
  1714. */
  1715. presults_data = &(presults->data[0]);
  1716. Status = SetSimpleData(Dev, 1,
  1717. presults->device_status,
  1718. presults_data,
  1719. pRangingMeasurementData);
  1720. }
  1721. LOG_FUNCTION_END(Status);
  1722. return Status;
  1723. }
  1724. /* End Group PAL Measurement Functions */
  1725. /* Group Calibration functions */
  1726. VL53L1_Error VL53L1_SetTuningParameter(VL53L1_DEV Dev,
  1727. uint16_t TuningParameterId, int32_t TuningParameterValue)
  1728. {
  1729. VL53L1_Error Status = VL53L1_ERROR_NONE;
  1730. LOG_FUNCTION_START("");
  1731. if (TuningParameterId >= 32768)
  1732. Status = VL53L1_set_tuning_parm(Dev,
  1733. TuningParameterId,
  1734. TuningParameterValue);
  1735. else {
  1736. if (TuningParameterId < VL53L1_TUNING_MAX_TUNABLE_KEY)
  1737. BDTable[TuningParameterId] = TuningParameterValue;
  1738. else
  1739. Status = VL53L1_ERROR_INVALID_PARAMS;
  1740. }
  1741. LOG_FUNCTION_END(Status);
  1742. return Status;
  1743. }
  1744. VL53L1_Error VL53L1_GetTuningParameter(VL53L1_DEV Dev,
  1745. uint16_t TuningParameterId, int32_t *pTuningParameterValue)
  1746. {
  1747. VL53L1_Error Status = VL53L1_ERROR_NONE;
  1748. LOG_FUNCTION_START("");
  1749. if (TuningParameterId >= 32768)
  1750. Status = VL53L1_get_tuning_parm(Dev,
  1751. TuningParameterId,
  1752. pTuningParameterValue);
  1753. else {
  1754. if (TuningParameterId < VL53L1_TUNING_MAX_TUNABLE_KEY)
  1755. *pTuningParameterValue = BDTable[TuningParameterId];
  1756. else
  1757. Status = VL53L1_ERROR_INVALID_PARAMS;
  1758. }
  1759. LOG_FUNCTION_END(Status);
  1760. return Status;
  1761. }
  1762. VL53L1_Error VL53L1_PerformRefSpadManagement(VL53L1_DEV Dev)
  1763. {
  1764. #ifdef VL53L1_NOCALIB
  1765. VL53L1_Error Status = VL53L1_ERROR_NOT_SUPPORTED;
  1766. SUPPRESS_UNUSED_WARNING(Dev);
  1767. LOG_FUNCTION_START("");
  1768. #else
  1769. VL53L1_Error Status = VL53L1_ERROR_NONE;
  1770. VL53L1_Error RawStatus;
  1771. uint8_t dcrbuffer[24];
  1772. uint8_t *comms_buffer;
  1773. uint8_t numloc[2] = {5,3};
  1774. VL53L1_LLDriverData_t *pdev;
  1775. VL53L1_customer_nvm_managed_t *pc;
  1776. VL53L1_PresetModes PresetMode;
  1777. LOG_FUNCTION_START("");
  1778. pdev = VL53L1DevStructGetLLDriverHandle(Dev);
  1779. pc = &pdev->customer;
  1780. if (Status == VL53L1_ERROR_NONE)
  1781. {
  1782. PresetMode = VL53L1DevDataGet(Dev, CurrentParameters.PresetMode);
  1783. Status = VL53L1_run_ref_spad_char(Dev, &RawStatus);
  1784. /* We discovered RefSpad mngt badly breaks some preset mode
  1785. * The WA is to apply again the current one
  1786. */
  1787. if (Status == VL53L1_ERROR_NONE)
  1788. Status = VL53L1_SetPresetMode(Dev, PresetMode);
  1789. }
  1790. if (Status == VL53L1_WARNING_REF_SPAD_CHAR_RATE_TOO_HIGH) {
  1791. /* Fix ticket #466282 RefSpad management error/warning -29
  1792. * force usage of location 3 and 5 refspads in registers
  1793. */
  1794. Status = VL53L1_read_nvm_raw_data(Dev,
  1795. (uint8_t)(0xA0 >> 2),
  1796. (uint8_t)(24 >> 2),
  1797. dcrbuffer);
  1798. if (Status == VL53L1_ERROR_NONE)
  1799. Status = VL53L1_WriteMulti( Dev,
  1800. VL53L1_REF_SPAD_MAN__NUM_REQUESTED_REF_SPADS,
  1801. numloc, 2);
  1802. if (Status == VL53L1_ERROR_NONE) {
  1803. pc->ref_spad_man__num_requested_ref_spads = numloc[0];
  1804. pc->ref_spad_man__ref_location = numloc[1];
  1805. }
  1806. if (Status == VL53L1_ERROR_NONE)
  1807. comms_buffer = &dcrbuffer[16];
  1808. /*
  1809. * update & copy reference SPAD enables to customer nvm managed
  1810. */
  1811. if (Status == VL53L1_ERROR_NONE)
  1812. Status = VL53L1_WriteMulti(Dev,
  1813. VL53L1_GLOBAL_CONFIG__SPAD_ENABLES_REF_0,
  1814. comms_buffer, 6);
  1815. if (Status == VL53L1_ERROR_NONE) {
  1816. pc->global_config__spad_enables_ref_0 = comms_buffer[0];
  1817. pc->global_config__spad_enables_ref_1 = comms_buffer[1];
  1818. pc->global_config__spad_enables_ref_2 = comms_buffer[2];
  1819. pc->global_config__spad_enables_ref_3 = comms_buffer[3];
  1820. pc->global_config__spad_enables_ref_4 = comms_buffer[4];
  1821. pc->global_config__spad_enables_ref_5 = comms_buffer[5];
  1822. }
  1823. /* End of fix ticket #466282 */
  1824. }
  1825. #endif
  1826. LOG_FUNCTION_END(Status);
  1827. return Status;
  1828. }
  1829. VL53L1_Error VL53L1_SetXTalkCompensationEnable(VL53L1_DEV Dev,
  1830. uint8_t XTalkCompensationEnable)
  1831. {
  1832. VL53L1_Error Status = VL53L1_ERROR_NONE;
  1833. LOG_FUNCTION_START("");
  1834. if (XTalkCompensationEnable == 0)
  1835. Status = VL53L1_disable_xtalk_compensation(Dev);
  1836. else
  1837. Status = VL53L1_enable_xtalk_compensation(Dev);
  1838. LOG_FUNCTION_END(Status);
  1839. return Status;
  1840. }
  1841. VL53L1_Error VL53L1_GetXTalkCompensationEnable(VL53L1_DEV Dev,
  1842. uint8_t *pXTalkCompensationEnable)
  1843. {
  1844. VL53L1_Error Status = VL53L1_ERROR_NONE;
  1845. LOG_FUNCTION_START("");
  1846. VL53L1_get_xtalk_compensation_enable(
  1847. Dev,
  1848. pXTalkCompensationEnable);
  1849. LOG_FUNCTION_END(Status);
  1850. return Status;
  1851. }
  1852. VL53L1_Error VL53L1_PerformSingleTargetXTalkCalibration(VL53L1_DEV Dev,
  1853. int32_t CalDistanceMilliMeter)
  1854. {
  1855. VL53L1_Error Status = VL53L1_ERROR_NONE;
  1856. LOG_FUNCTION_START("");
  1857. if (CalDistanceMilliMeter > 0) {
  1858. BDTable[VL53L1_TUNING_SINGLE_TARGET_XTALK_TARGET_DISTANCE_MM] =
  1859. CalDistanceMilliMeter;
  1860. Status = SingleTargetXTalkCalibration(Dev);
  1861. } else
  1862. Status = VL53L1_ERROR_INVALID_PARAMS;
  1863. LOG_FUNCTION_END(Status);
  1864. return Status;
  1865. }
  1866. VL53L1_Error VL53L1_SetOffsetCalibrationMode(VL53L1_DEV Dev,
  1867. VL53L1_OffsetCalibrationModes OffsetCalibrationMode)
  1868. {
  1869. VL53L1_Error Status = VL53L1_ERROR_NONE;
  1870. VL53L1_OffsetCalibrationMode offset_cal_mode;
  1871. LOG_FUNCTION_START("");
  1872. if (OffsetCalibrationMode == VL53L1_OFFSETCALIBRATIONMODE_STANDARD) {
  1873. offset_cal_mode = VL53L1_DEVICEPRESETMODE_STANDARD_RANGING;
  1874. } else if (OffsetCalibrationMode ==
  1875. VL53L1_OFFSETCALIBRATIONMODE_PRERANGE_ONLY) {
  1876. offset_cal_mode =
  1877. VL53L1_OFFSETCALIBRATIONMODE__MM1_MM2__STANDARD_PRE_RANGE_ONLY;
  1878. } else {
  1879. Status = VL53L1_ERROR_INVALID_PARAMS;
  1880. }
  1881. if (Status == VL53L1_ERROR_NONE)
  1882. Status = VL53L1_set_offset_calibration_mode(Dev,
  1883. offset_cal_mode);
  1884. LOG_FUNCTION_END(Status);
  1885. return Status;
  1886. }
  1887. #ifdef OFFSET_CALIB
  1888. VL53L1_Error VL53L1_PerformOffsetCalibration(VL53L1_DEV Dev,
  1889. int32_t CalDistanceMilliMeter)
  1890. {
  1891. VL53L1_Error Status = VL53L1_ERROR_NONE;
  1892. VL53L1_Error UnfilteredStatus;
  1893. VL53L1_OffsetCalibrationMode offset_cal_mode;
  1894. LOG_FUNCTION_START("");
  1895. if (Status == VL53L1_ERROR_NONE)
  1896. Status = VL53L1_get_offset_calibration_mode(Dev,
  1897. &offset_cal_mode);
  1898. if (Status != VL53L1_ERROR_NONE) {
  1899. LOG_FUNCTION_END(Status);
  1900. return Status;
  1901. }
  1902. if ((offset_cal_mode ==
  1903. VL53L1_OFFSETCALIBRATIONMODE__MM1_MM2__STANDARD) ||
  1904. (offset_cal_mode ==
  1905. VL53L1_OFFSETCALIBRATIONMODE__MM1_MM2__STANDARD_PRE_RANGE_ONLY
  1906. )) {
  1907. if (Status == VL53L1_ERROR_NONE)
  1908. Status = VL53L1_run_offset_calibration(
  1909. Dev,
  1910. (int16_t)CalDistanceMilliMeter,
  1911. &UnfilteredStatus);
  1912. } else {
  1913. Status = VL53L1_ERROR_INVALID_PARAMS;
  1914. }
  1915. LOG_FUNCTION_END(Status);
  1916. return Status;
  1917. }
  1918. #endif
  1919. #ifdef OFFSET_CALIB_EMPTY
  1920. VL53L1_Error VL53L1_PerformOffsetCalibration(VL53L1_DEV Dev,
  1921. int32_t CalDistanceMilliMeter)
  1922. {
  1923. VL53L1_Error Status = VL53L1_ERROR_NOT_SUPPORTED;
  1924. SUPPRESS_UNUSED_WARNING(Dev);
  1925. SUPPRESS_UNUSED_WARNING(CalDistanceMilliMeter);
  1926. return Status;
  1927. }
  1928. #endif
  1929. VL53L1_Error VL53L1_PerformOffsetSimpleCalibration(VL53L1_DEV Dev,
  1930. int32_t CalDistanceMilliMeter)
  1931. {
  1932. VL53L1_Error Status = VL53L1_ERROR_NONE;
  1933. int32_t sum_ranging;
  1934. uint8_t offset_meas;
  1935. int16_t Max, UnderMax, OverMax, Repeat;
  1936. int32_t total_count, inloopcount;
  1937. int32_t IncRounding;
  1938. int16_t meanDistance_mm;
  1939. int16_t offset;
  1940. VL53L1_RangingMeasurementData_t RangingMeasurementData;
  1941. VL53L1_LLDriverData_t *pdev;
  1942. uint8_t goodmeas;
  1943. LOG_FUNCTION_START("");
  1944. pdev = VL53L1DevStructGetLLDriverHandle(Dev);
  1945. /* Disable any offsets */
  1946. pdev->customer.algo__part_to_part_range_offset_mm = 0;
  1947. pdev->customer.mm_config__inner_offset_mm = 0;
  1948. pdev->customer.mm_config__outer_offset_mm = 0;
  1949. Repeat=BDTable[VL53L1_TUNING_SIMPLE_OFFSET_CALIBRATION_REPEAT];
  1950. Max=BDTable[VL53L1_TUNING_MAX_SIMPLE_OFFSET_CALIBRATION_SAMPLE_NUMBER];
  1951. UnderMax = 1 + (Max / 2);
  1952. OverMax = Max + (Max / 2);
  1953. sum_ranging = 0;
  1954. total_count = 0;
  1955. while ((Repeat > 0) && (Status == VL53L1_ERROR_NONE)) {
  1956. Status = VL53L1_StartMeasurement(Dev);
  1957. /* Very first ranging completion interrupt must be ignored */
  1958. if (Status == VL53L1_ERROR_NONE)
  1959. Status = VL53L1_WaitMeasurementDataReady(Dev);
  1960. if (Status == VL53L1_ERROR_NONE)
  1961. Status = VL53L1_GetRangingMeasurementData(Dev,
  1962. &RangingMeasurementData);
  1963. if (Status == VL53L1_ERROR_NONE)
  1964. Status = VL53L1_ClearInterruptAndStartMeasurement(Dev);
  1965. /* offset calibration main loop */
  1966. inloopcount = 0;
  1967. offset_meas = 0;
  1968. while ((Status == VL53L1_ERROR_NONE) && (inloopcount < Max) &&
  1969. (offset_meas < OverMax)) {
  1970. Status = VL53L1_WaitMeasurementDataReady(Dev);
  1971. if (Status == VL53L1_ERROR_NONE)
  1972. Status = VL53L1_GetRangingMeasurementData(Dev,
  1973. &RangingMeasurementData);
  1974. goodmeas = (RangingMeasurementData.RangeStatus ==
  1975. VL53L1_RANGESTATUS_RANGE_VALID);
  1976. if ((Status == VL53L1_ERROR_NONE) && goodmeas) {
  1977. sum_ranging = sum_ranging +
  1978. RangingMeasurementData.RangeMilliMeter;
  1979. inloopcount++;
  1980. }
  1981. if (Status == VL53L1_ERROR_NONE) {
  1982. Status = VL53L1_ClearInterruptAndStartMeasurement(
  1983. Dev);
  1984. }
  1985. offset_meas++;
  1986. }
  1987. total_count += inloopcount;
  1988. /* no enough valid values found */
  1989. if (inloopcount < UnderMax) {
  1990. Status = VL53L1_ERROR_OFFSET_CAL_NO_SAMPLE_FAIL;
  1991. }
  1992. VL53L1_StopMeasurement(Dev);
  1993. Repeat--;
  1994. }
  1995. /* check overflow (unlikely if target is near to the device) */
  1996. if ((sum_ranging < 0) ||
  1997. (sum_ranging > ((int32_t) total_count * 0xffff))) {
  1998. Status = VL53L1_WARNING_OFFSET_CAL_SIGMA_TOO_HIGH;
  1999. }
  2000. if ((Status == VL53L1_ERROR_NONE) && (total_count > 0)) {
  2001. IncRounding = total_count / 2;
  2002. meanDistance_mm = (int16_t)((sum_ranging + IncRounding)
  2003. / total_count);
  2004. offset = (int16_t)CalDistanceMilliMeter - meanDistance_mm;
  2005. pdev->customer.algo__part_to_part_range_offset_mm = 0;
  2006. pdev->customer.mm_config__inner_offset_mm = offset;
  2007. pdev->customer.mm_config__outer_offset_mm = offset;
  2008. Status = VL53L1_set_customer_nvm_managed(Dev,
  2009. &(pdev->customer));
  2010. }
  2011. LOG_FUNCTION_END(Status);
  2012. return Status;
  2013. }
  2014. VL53L1_Error VL53L1_SetCalibrationData(VL53L1_DEV Dev,
  2015. VL53L1_CalibrationData_t *pCalibrationData)
  2016. {
  2017. VL53L1_Error Status = VL53L1_ERROR_NONE;
  2018. VL53L1_CustomerNvmManaged_t *pC;
  2019. VL53L1_calibration_data_t cal_data;
  2020. uint32_t x;
  2021. LOG_FUNCTION_START("");
  2022. cal_data.struct_version = pCalibrationData->struct_version -
  2023. VL53L1_ADDITIONAL_CALIBRATION_DATA_STRUCT_VERSION;
  2024. /* memcpy(DEST, SRC, N) */
  2025. memcpy(
  2026. &(cal_data.add_off_cal_data),
  2027. &(pCalibrationData->add_off_cal_data),
  2028. sizeof(VL53L1_additional_offset_cal_data_t));
  2029. /* memcpy (DEST, SRC, N) */
  2030. memcpy(
  2031. &(cal_data.optical_centre),
  2032. &(pCalibrationData->optical_centre),
  2033. sizeof(VL53L1_optical_centre_t));
  2034. /* memcpy (DEST, SRC, N) */
  2035. memcpy(
  2036. &(cal_data.gain_cal),
  2037. &(pCalibrationData->gain_cal),
  2038. sizeof(VL53L1_gain_calibration_data_t));
  2039. /* memcpy (DEST, SRC, N) */
  2040. memcpy(
  2041. &(cal_data.cal_peak_rate_map),
  2042. &(pCalibrationData->cal_peak_rate_map),
  2043. sizeof(VL53L1_cal_peak_rate_map_t));
  2044. pC = &pCalibrationData->customer;
  2045. x = pC->algo__crosstalk_compensation_plane_offset_kcps;
  2046. cal_data.customer.algo__crosstalk_compensation_plane_offset_kcps =
  2047. (uint16_t)(x&0x0000FFFF);
  2048. cal_data.customer.global_config__spad_enables_ref_0 =
  2049. pC->global_config__spad_enables_ref_0;
  2050. cal_data.customer.global_config__spad_enables_ref_1 =
  2051. pC->global_config__spad_enables_ref_1;
  2052. cal_data.customer.global_config__spad_enables_ref_2 =
  2053. pC->global_config__spad_enables_ref_2;
  2054. cal_data.customer.global_config__spad_enables_ref_3 =
  2055. pC->global_config__spad_enables_ref_3;
  2056. cal_data.customer.global_config__spad_enables_ref_4 =
  2057. pC->global_config__spad_enables_ref_4;
  2058. cal_data.customer.global_config__spad_enables_ref_5 =
  2059. pC->global_config__spad_enables_ref_5;
  2060. cal_data.customer.global_config__ref_en_start_select =
  2061. pC->global_config__ref_en_start_select;
  2062. cal_data.customer.ref_spad_man__num_requested_ref_spads =
  2063. pC->ref_spad_man__num_requested_ref_spads;
  2064. cal_data.customer.ref_spad_man__ref_location =
  2065. pC->ref_spad_man__ref_location;
  2066. cal_data.customer.algo__crosstalk_compensation_x_plane_gradient_kcps =
  2067. pC->algo__crosstalk_compensation_x_plane_gradient_kcps;
  2068. cal_data.customer.algo__crosstalk_compensation_y_plane_gradient_kcps =
  2069. pC->algo__crosstalk_compensation_y_plane_gradient_kcps;
  2070. cal_data.customer.ref_spad_char__total_rate_target_mcps =
  2071. pC->ref_spad_char__total_rate_target_mcps;
  2072. cal_data.customer.algo__part_to_part_range_offset_mm =
  2073. pC->algo__part_to_part_range_offset_mm;
  2074. cal_data.customer.mm_config__inner_offset_mm =
  2075. pC->mm_config__inner_offset_mm;
  2076. cal_data.customer.mm_config__outer_offset_mm =
  2077. pC->mm_config__outer_offset_mm;
  2078. Status = VL53L1_set_part_to_part_data(Dev, &cal_data);
  2079. LOG_FUNCTION_END(Status);
  2080. return Status;
  2081. }
  2082. VL53L1_Error VL53L1_GetCalibrationData(VL53L1_DEV Dev,
  2083. VL53L1_CalibrationData_t *pCalibrationData)
  2084. {
  2085. VL53L1_Error Status = VL53L1_ERROR_NONE;
  2086. VL53L1_calibration_data_t cal_data;
  2087. VL53L1_CustomerNvmManaged_t *pC;
  2088. VL53L1_customer_nvm_managed_t *pC2;
  2089. LOG_FUNCTION_START("");
  2090. /* struct_version is filled inside get part to part function */
  2091. Status = VL53L1_get_part_to_part_data(Dev, &cal_data);
  2092. pCalibrationData->struct_version = cal_data.struct_version +
  2093. VL53L1_ADDITIONAL_CALIBRATION_DATA_STRUCT_VERSION;
  2094. /* memcpy(DEST, SRC, N) */
  2095. memcpy(
  2096. &(pCalibrationData->add_off_cal_data),
  2097. &(cal_data.add_off_cal_data),
  2098. sizeof(VL53L1_additional_offset_cal_data_t));
  2099. /* memcpy (DEST, SRC, N) */
  2100. memcpy(
  2101. &(pCalibrationData->optical_centre),
  2102. &(cal_data.optical_centre),
  2103. sizeof(VL53L1_optical_centre_t));
  2104. /* memcpy (DEST, SRC, N) */
  2105. memcpy(
  2106. &(pCalibrationData->gain_cal),
  2107. &(cal_data.gain_cal),
  2108. sizeof(VL53L1_gain_calibration_data_t));
  2109. /* memcpy (DEST, SRC, N) */
  2110. memcpy(
  2111. &(pCalibrationData->cal_peak_rate_map),
  2112. &(cal_data.cal_peak_rate_map),
  2113. sizeof(VL53L1_cal_peak_rate_map_t));
  2114. pC = &pCalibrationData->customer;
  2115. pC2 = &cal_data.customer;
  2116. pC->global_config__spad_enables_ref_0 =
  2117. pC2->global_config__spad_enables_ref_0;
  2118. pC->global_config__spad_enables_ref_1 =
  2119. pC2->global_config__spad_enables_ref_1;
  2120. pC->global_config__spad_enables_ref_2 =
  2121. pC2->global_config__spad_enables_ref_2;
  2122. pC->global_config__spad_enables_ref_3 =
  2123. pC2->global_config__spad_enables_ref_3;
  2124. pC->global_config__spad_enables_ref_4 =
  2125. pC2->global_config__spad_enables_ref_4;
  2126. pC->global_config__spad_enables_ref_5 =
  2127. pC2->global_config__spad_enables_ref_5;
  2128. pC->global_config__ref_en_start_select =
  2129. pC2->global_config__ref_en_start_select;
  2130. pC->ref_spad_man__num_requested_ref_spads =
  2131. pC2->ref_spad_man__num_requested_ref_spads;
  2132. pC->ref_spad_man__ref_location =
  2133. pC2->ref_spad_man__ref_location;
  2134. pC->algo__crosstalk_compensation_x_plane_gradient_kcps =
  2135. pC2->algo__crosstalk_compensation_x_plane_gradient_kcps;
  2136. pC->algo__crosstalk_compensation_y_plane_gradient_kcps =
  2137. pC2->algo__crosstalk_compensation_y_plane_gradient_kcps;
  2138. pC->ref_spad_char__total_rate_target_mcps =
  2139. pC2->ref_spad_char__total_rate_target_mcps;
  2140. pC->algo__part_to_part_range_offset_mm =
  2141. pC2->algo__part_to_part_range_offset_mm;
  2142. pC->mm_config__inner_offset_mm =
  2143. pC2->mm_config__inner_offset_mm;
  2144. pC->mm_config__outer_offset_mm =
  2145. pC2->mm_config__outer_offset_mm;
  2146. pC->algo__crosstalk_compensation_plane_offset_kcps =
  2147. (uint32_t)(
  2148. pC2->algo__crosstalk_compensation_plane_offset_kcps);
  2149. LOG_FUNCTION_END(Status);
  2150. return Status;
  2151. }
  2152. VL53L1_Error VL53L1_GetOpticalCenter(VL53L1_DEV Dev,
  2153. FixPoint1616_t *pOpticalCenterX,
  2154. FixPoint1616_t *pOpticalCenterY)
  2155. {
  2156. VL53L1_Error Status = VL53L1_ERROR_NONE;
  2157. VL53L1_calibration_data_t CalibrationData;
  2158. LOG_FUNCTION_START("");
  2159. *pOpticalCenterX = 0;
  2160. *pOpticalCenterY = 0;
  2161. Status = VL53L1_get_part_to_part_data(Dev, &CalibrationData);
  2162. if (Status == VL53L1_ERROR_NONE) {
  2163. *pOpticalCenterX = VL53L1_FIXPOINT44TOFIXPOINT1616(
  2164. CalibrationData.optical_centre.x_centre);
  2165. *pOpticalCenterY = VL53L1_FIXPOINT44TOFIXPOINT1616(
  2166. CalibrationData.optical_centre.y_centre);
  2167. }
  2168. LOG_FUNCTION_END(Status);
  2169. return Status;
  2170. }
  2171. /* END Group Calibration functions */
  2172. /* Group PAL detection triggered events Functions */
  2173. VL53L1_Error VL53L1_SetThresholdConfig(VL53L1_DEV Dev,
  2174. VL53L1_DetectionConfig_t *pConfig)
  2175. {
  2176. #define BADTHRESBOUNDS(T) \
  2177. (((T.CrossMode == VL53L1_THRESHOLD_OUT_OF_WINDOW) || \
  2178. (T.CrossMode == VL53L1_THRESHOLD_IN_WINDOW)) && (T.Low > T.High))
  2179. VL53L1_Error Status = VL53L1_ERROR_NONE;
  2180. VL53L1_GPIO_interrupt_config_t Cfg;
  2181. uint16_t g;
  2182. FixPoint1616_t gain, high1616, low1616;
  2183. VL53L1_LLDriverData_t *pdev;
  2184. LOG_FUNCTION_START("");
  2185. pdev = VL53L1DevStructGetLLDriverHandle(Dev);
  2186. Status = VL53L1_get_GPIO_interrupt_config(Dev, &Cfg);
  2187. if (Status == VL53L1_ERROR_NONE) {
  2188. if (pConfig->DetectionMode == VL53L1_DETECTION_NORMAL_RUN) {
  2189. Cfg.intr_new_measure_ready = 1;
  2190. Status = VL53L1_set_GPIO_interrupt_config_struct(Dev,
  2191. Cfg);
  2192. } else {
  2193. if (BADTHRESBOUNDS(pConfig->Distance))
  2194. Status = VL53L1_ERROR_INVALID_PARAMS;
  2195. if ((Status == VL53L1_ERROR_NONE) &&
  2196. (BADTHRESBOUNDS(pConfig->Rate)))
  2197. Status = VL53L1_ERROR_INVALID_PARAMS;
  2198. if (Status == VL53L1_ERROR_NONE) {
  2199. Cfg.intr_new_measure_ready = 0;
  2200. Cfg.intr_no_target = pConfig->IntrNoTarget;
  2201. /* fix ticket 466238
  2202. * Apply invert distance gain to thresholds */
  2203. g = pdev->gain_cal.standard_ranging_gain_factor;
  2204. /* gain is ufix 5.11, convert to 16.16 */
  2205. gain = (FixPoint1616_t) ((uint32_t)g << 5);
  2206. high1616 = (FixPoint1616_t) ((uint32_t)
  2207. pConfig->Distance.High << 16);
  2208. low1616 = (FixPoint1616_t) ((uint32_t)
  2209. pConfig->Distance.Low << 16);
  2210. /* +32768 to round the results*/
  2211. high1616 = (high1616 + 32768) / gain;
  2212. low1616 = (low1616 + 32768) / gain;
  2213. Cfg.threshold_distance_high = (uint16_t)
  2214. (high1616 & 0xFFFF);
  2215. Cfg.threshold_distance_low = (uint16_t)
  2216. (low1616 & 0xFFFF);
  2217. /* end fix ticket 466238 */
  2218. Cfg.threshold_rate_high =
  2219. VL53L1_FIXPOINT1616TOFIXPOINT97(
  2220. pConfig->Rate.High);
  2221. Cfg.threshold_rate_low =
  2222. VL53L1_FIXPOINT1616TOFIXPOINT97(
  2223. pConfig->Rate.Low);
  2224. Cfg.intr_mode_distance = ConvertModeToLLD(
  2225. &Status,
  2226. pConfig->Distance.CrossMode);
  2227. if (Status == VL53L1_ERROR_NONE)
  2228. Cfg.intr_mode_rate = ConvertModeToLLD(
  2229. &Status,
  2230. pConfig->Rate.CrossMode);
  2231. }
  2232. /* Refine thresholds combination now */
  2233. if (Status == VL53L1_ERROR_NONE) {
  2234. Cfg.intr_combined_mode = 1;
  2235. switch (pConfig->DetectionMode) {
  2236. case VL53L1_DETECTION_DISTANCE_ONLY:
  2237. Cfg.threshold_rate_high = 0;
  2238. Cfg.threshold_rate_low = 0;
  2239. break;
  2240. case VL53L1_DETECTION_RATE_ONLY:
  2241. Cfg.threshold_distance_high = 0;
  2242. Cfg.threshold_distance_low = 0;
  2243. break;
  2244. case VL53L1_DETECTION_DISTANCE_OR_RATE:
  2245. /* Nothing to do all is already
  2246. * in place
  2247. */
  2248. break;
  2249. case VL53L1_DETECTION_DISTANCE_AND_RATE:
  2250. Cfg.intr_combined_mode = 0;
  2251. break;
  2252. default:
  2253. Status = VL53L1_ERROR_INVALID_PARAMS;
  2254. }
  2255. }
  2256. if (Status == VL53L1_ERROR_NONE)
  2257. Status =
  2258. VL53L1_set_GPIO_interrupt_config_struct(Dev,
  2259. Cfg);
  2260. }
  2261. }
  2262. LOG_FUNCTION_END(Status);
  2263. return Status;
  2264. }
  2265. VL53L1_Error VL53L1_GetThresholdConfig(VL53L1_DEV Dev,
  2266. VL53L1_DetectionConfig_t *pConfig)
  2267. {
  2268. VL53L1_Error Status = VL53L1_ERROR_NONE;
  2269. VL53L1_GPIO_interrupt_config_t Cfg;
  2270. LOG_FUNCTION_START("");
  2271. Status = VL53L1_get_GPIO_interrupt_config(Dev, &Cfg);
  2272. if (Status != VL53L1_ERROR_NONE) {
  2273. LOG_FUNCTION_END(Status);
  2274. return Status;
  2275. }
  2276. pConfig->IntrNoTarget = Cfg.intr_no_target;
  2277. pConfig->Distance.High = Cfg.threshold_distance_high;
  2278. pConfig->Distance.Low = Cfg.threshold_distance_low;
  2279. pConfig->Rate.High =
  2280. VL53L1_FIXPOINT97TOFIXPOINT1616(
  2281. Cfg.threshold_rate_high);
  2282. pConfig->Rate.Low =
  2283. VL53L1_FIXPOINT97TOFIXPOINT1616(Cfg.threshold_rate_low);
  2284. pConfig->Distance.CrossMode =
  2285. ConvertModeFromLLD(&Status, Cfg.intr_mode_distance);
  2286. if (Status == VL53L1_ERROR_NONE)
  2287. pConfig->Rate.CrossMode =
  2288. ConvertModeFromLLD(&Status, Cfg.intr_mode_rate);
  2289. if (Cfg.intr_new_measure_ready == 1) {
  2290. pConfig->DetectionMode = VL53L1_DETECTION_NORMAL_RUN;
  2291. } else {
  2292. /* Refine thresholds combination now */
  2293. if (Status == VL53L1_ERROR_NONE) {
  2294. if (Cfg.intr_combined_mode == 0)
  2295. pConfig->DetectionMode =
  2296. VL53L1_DETECTION_DISTANCE_AND_RATE;
  2297. else {
  2298. if ((Cfg.threshold_distance_high == 0) &&
  2299. (Cfg.threshold_distance_low == 0))
  2300. pConfig->DetectionMode =
  2301. VL53L1_DETECTION_RATE_ONLY;
  2302. else if ((Cfg.threshold_rate_high == 0) &&
  2303. (Cfg.threshold_rate_low == 0))
  2304. pConfig->DetectionMode =
  2305. VL53L1_DETECTION_DISTANCE_ONLY;
  2306. else
  2307. pConfig->DetectionMode =
  2308. VL53L1_DETECTION_DISTANCE_OR_RATE;
  2309. }
  2310. }
  2311. }
  2312. LOG_FUNCTION_END(Status);
  2313. return Status;
  2314. }
  2315. /* End Group PAL IRQ Triggered events Functions */