VL6180x_ATY.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. /**
  2. * @file VL6180x_ATY.c
  3. *
  4. * @param Project DEVICE_DRIVER_ATY_LIB
  5. *
  6. * @author ATY
  7. *
  8. * @copyright
  9. * - Copyright 2017 - 2026 MZ-ATY
  10. * - This code follows:
  11. * - MZ-ATY Various Contents Joint Statement -
  12. * <a href="https://mengze.top/MZ-ATY_VCJS">
  13. * https://mengze.top/MZ-ATY_VCJS</a>
  14. * - CC 4.0 BY-NC-SA -
  15. * <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
  16. * https://creativecommons.org/licenses/by-nc-sa/4.0/</a>
  17. * - Your use will be deemed to have accepted the terms of this statement.
  18. *
  19. * @brief functions of VL6180 for C platform
  20. *
  21. * @version
  22. * - 1_01_231228 > ATY
  23. * -# Preliminary version, first Release
  24. * - 1_01_240104 > ATY
  25. * -# base tested VL6180
  26. * - 1_01_240111 > ATY
  27. * -# add lock
  28. ********************************************************************************
  29. */
  30. #ifndef __VL6180x_ATY_C
  31. #define __VL6180x_ATY_C
  32. #include "VL6180x_ATY.h"
  33. #define VL6180x_ATY_TAG "\r\n[VL6180x_ATY] "
  34. /******************************* For user *************************************/
  35. /******************************************************************************/
  36. /**
  37. * @brief
  38. *
  39. * @param reg
  40. * @param data
  41. * @param dev
  42. * @return uint8_t
  43. */
  44. uint8_t VL6180x_WriteByte(uint16_t reg, uint8_t data, struct VL6180x_ATY_Dev* dev){
  45. uint8_t groupRegData[3], errCode = 0;
  46. __ATY_LOCK(dev);
  47. groupRegData[0] = (reg >> 8) & 0xFF; // MSB of register address
  48. groupRegData[1] = reg & 0xFF; // LSB of register address
  49. groupRegData[2] = data & 0xFF;
  50. errCode = dev->i2cProcess(dev->addr, groupRegData, 3, __ATY_RW_W);
  51. __ATY_UNLOCK(dev);
  52. return errCode;
  53. }
  54. /**
  55. * @brief
  56. *
  57. * @param reg
  58. * @param data
  59. * @param bytes num to write
  60. * @param dev
  61. * @return uint8_t
  62. */
  63. uint8_t VL6180x_WriteBytes(uint16_t reg, uint8_t* data, uint8_t bytes, struct VL6180x_ATY_Dev* dev){
  64. uint8_t groupRegData[3], i = 0, errCode = 0;
  65. __ATY_LOCK(dev);
  66. groupRegData[0] = (reg >> 8) & 0xFF; // MSB of register address
  67. groupRegData[1] = reg & 0xFF; // LSB of register address
  68. for(i = 0; i < bytes; i++)
  69. groupRegData[2 + i] = data[i] & 0xFF;
  70. errCode = dev->i2cProcess(dev->addr, groupRegData, 2 + bytes, __ATY_RW_W);
  71. __ATY_UNLOCK(dev);
  72. return errCode;
  73. }
  74. /**
  75. * @brief
  76. *
  77. * @param reg
  78. * @param data
  79. * @param dev
  80. * @return uint8_t
  81. */
  82. uint8_t VL6180x_ReadByte(uint16_t reg, uint8_t* data, struct VL6180x_ATY_Dev* dev){
  83. uint8_t groupReg[2], errCode = 0;
  84. __ATY_LOCK(dev);
  85. groupReg[0] = (reg >> 8) & 0xFF; // MSB of register address
  86. groupReg[1] = reg & 0xFF; // LSB of register address
  87. errCode = dev->i2cProcess(dev->addr, groupReg, 2, __ATY_RW_W);
  88. if(errCode){ __ATY_UNLOCK(dev); return errCode; }
  89. errCode = dev->i2cProcess(dev->addr, data, 1, __ATY_RW_R);
  90. __ATY_UNLOCK(dev);
  91. return errCode;
  92. }
  93. /**
  94. * @brief
  95. *
  96. * @param reg
  97. * @param data
  98. * @param bytes num to read
  99. * @param dev
  100. * @return uint8_t
  101. */
  102. uint8_t VL6180x_ReadBytes(uint16_t reg, uint8_t* data, uint8_t bytes, struct VL6180x_ATY_Dev* dev){
  103. uint8_t groupReg[2], errCode = 0;
  104. __ATY_LOCK(dev);
  105. groupReg[0] = (reg >> 8) & 0xFF; // MSB of register address
  106. groupReg[1] = reg & 0xFF; // LSB of register address
  107. errCode = dev->i2cProcess(dev->addr, groupReg, 2, __ATY_RW_W);
  108. if(errCode){ __ATY_UNLOCK(dev); return errCode; }
  109. errCode = dev->i2cProcess(dev->addr, data, bytes, __ATY_RW_R);
  110. __ATY_UNLOCK(dev);
  111. return errCode;
  112. }
  113. /**
  114. * @brief get ids
  115. *
  116. * @param id need 9 size array
  117. * @param dev device
  118. * @return uint8_t
  119. * @note B4 01 03 ......
  120. */
  121. uint8_t VL6180x_GetId(uint8_t* id, struct VL6180x_ATY_Dev* dev){
  122. uint8_t errCode = 0;
  123. errCode = 0x00 & VL6180x_ReadByte(VL6180x_REG_IDENTIFICATION_MODEL_ID, &id[0], dev);
  124. errCode = 0x10 & VL6180x_ReadByte(VL6180x_REG_IDENTIFICATION_MODEL_REV_MAJOR, &id[1], dev);
  125. errCode = 0x20 & VL6180x_ReadByte(VL6180x_REG_IDENTIFICATION_MODEL_REV_MINOR, &id[2], dev);
  126. errCode = 0x30 & VL6180x_ReadByte(VL6180x_REG_IDENTIFICATION_MODULE_REV_MAJOR, &id[3], dev);
  127. errCode = 0x40 & VL6180x_ReadByte(VL6180x_REG_IDENTIFICATION_MODULE_REV_MINOR, &id[4], dev);
  128. errCode = 0x50 & VL6180x_ReadByte(VL6180x_REG_IDENTIFICATION_DATE_HI, &id[5], dev);
  129. errCode = 0x60 & VL6180x_ReadByte(VL6180x_REG_IDENTIFICATION_DATE_LO, &id[6], dev);
  130. errCode = 0x70 & VL6180x_ReadBytes(VL6180x_REG_IDENTIFICATION_TIME, &id[7], 2, dev);
  131. printf_ATY_D("%sID: %02X %02X %02X %02X %02X %02X %02X %02X %02X\r\n",
  132. VL6180x_ATY_TAG, id[0], id[1], id[2], id[3], id[4], id[5], id[6], id[7], id[8]);
  133. if(id[0] != 0xB4){ return 0x81; }
  134. return errCode;
  135. }
  136. /**
  137. * @brief write new addr
  138. *
  139. * @param newAddr
  140. * @param dev
  141. * @note not real tested
  142. * @return uint8_t
  143. */
  144. uint8_t VL6180x_SetAddress(uint8_t newAddr, struct VL6180x_ATY_Dev* dev){
  145. if(VL6180x_WriteByte(VL6180x_REG_SLAVE_DEVICE_ADDRESS, newAddr & 0x7F, dev) == 1)
  146. return 1;
  147. dev->addr = newAddr;
  148. return 0;
  149. }
  150. /**
  151. * @brief get offset
  152. *
  153. * @param offset
  154. * @param dev
  155. * @return uint8_t
  156. */
  157. uint8_t VL6180x_GetOffset(uint8_t* offset, struct VL6180x_ATY_Dev* dev){
  158. return VL6180x_ReadByte(VL6180x_REG_SYSRANGE_PART_TO_PART_RANGE_OFFSET, offset, dev);
  159. }
  160. /**
  161. * @brief set offset
  162. *
  163. * @param offset
  164. * @param dev
  165. * @return uint8_t
  166. */
  167. uint8_t VL6180x_SetOffset(uint8_t offset, struct VL6180x_ATY_Dev* dev){
  168. return VL6180x_WriteByte(VL6180x_REG_SYSRANGE_PART_TO_PART_RANGE_OFFSET, offset, dev);
  169. }
  170. /**
  171. * @brief
  172. *
  173. * @param dev
  174. * @return uint8_t
  175. */
  176. uint8_t VL6180x_Config(struct VL6180x_ATY_Dev* dev){
  177. uint8_t errCode = 0;
  178. // Mandatory : private registers
  179. errCode = 0x10 & VL6180x_WriteByte(0x0207, 0x01, dev);
  180. errCode = 0x10 & VL6180x_WriteByte(0x0208, 0x01, dev);
  181. errCode = 0x10 & VL6180x_WriteByte(0x0096, 0x00, dev);
  182. errCode = 0x10 & VL6180x_WriteByte(0x0097, 0xfd, dev);
  183. errCode = 0x10 & VL6180x_WriteByte(0x00e3, 0x01, dev);
  184. errCode = 0x10 & VL6180x_WriteByte(0x00e4, 0x03, dev);
  185. errCode = 0x10 & VL6180x_WriteByte(0x00e5, 0x02, dev);
  186. errCode = 0x10 & VL6180x_WriteByte(0x00e6, 0x01, dev);
  187. errCode = 0x10 & VL6180x_WriteByte(0x00e7, 0x03, dev);
  188. errCode = 0x10 & VL6180x_WriteByte(0x00f5, 0x02, dev);
  189. errCode = 0x10 & VL6180x_WriteByte(0x00d9, 0x05, dev);
  190. errCode = 0x10 & VL6180x_WriteByte(0x00db, 0xce, dev);
  191. errCode = 0x10 & VL6180x_WriteByte(0x00dc, 0x03, dev);
  192. errCode = 0x10 & VL6180x_WriteByte(0x00dd, 0xf8, dev);
  193. errCode = 0x10 & VL6180x_WriteByte(0x009f, 0x00, dev);
  194. errCode = 0x10 & VL6180x_WriteByte(0x00a3, 0x3c, dev);
  195. errCode = 0x10 & VL6180x_WriteByte(0x00b7, 0x00, dev);
  196. errCode = 0x10 & VL6180x_WriteByte(0x00bb, 0x3c, dev);
  197. errCode = 0x10 & VL6180x_WriteByte(0x00b2, 0x09, dev);
  198. errCode = 0x10 & VL6180x_WriteByte(0x00ca, 0x09, dev);
  199. errCode = 0x10 & VL6180x_WriteByte(0x0198, 0x01, dev);
  200. errCode = 0x10 & VL6180x_WriteByte(0x01b0, 0x17, dev);
  201. errCode = 0x10 & VL6180x_WriteByte(0x01ad, 0x00, dev);
  202. errCode = 0x10 & VL6180x_WriteByte(0x00ff, 0x05, dev);
  203. errCode = 0x10 & VL6180x_WriteByte(0x0100, 0x05, dev);
  204. errCode = 0x10 & VL6180x_WriteByte(0x0199, 0x05, dev);
  205. errCode = 0x10 & VL6180x_WriteByte(0x01a6, 0x1b, dev);
  206. errCode = 0x10 & VL6180x_WriteByte(0x01ac, 0x3e, dev);
  207. errCode = 0x10 & VL6180x_WriteByte(0x01a7, 0x1f, dev);
  208. errCode = 0x10 & VL6180x_WriteByte(0x0030, 0x00, dev);
  209. // Recommended : Public registers - See data sheet for more detail
  210. errCode = 0x20 & VL6180x_WriteByte(0x0011, 0x10, dev); // Enables polling for "New Sample ready" when measurement completes
  211. errCode = 0x20 & VL6180x_WriteByte(0x010a, 0x30, dev); // Set the averaging sample period (compromise between lower noise and increased execution time)
  212. errCode = 0x20 & VL6180x_WriteByte(0x003f, 0x46, dev); // Sets the light and dark gain (upper nibble). Dark gain should not be changed.
  213. errCode = 0x20 & VL6180x_WriteByte(0x0031, 0xFF, dev); // sets the # of range measurements after which auto calibration of system is performed
  214. errCode = 0x20 & VL6180x_WriteByte(0x0041, 0x63, dev); // Set ALS integration time to 100ms
  215. errCode = 0x20 & VL6180x_WriteByte(0x002e, 0x01, dev); // perform a single temperature calibration of the ranging sensor
  216. // Optional: Public registers - See data sheet for more detail
  217. errCode = 0x30 & VL6180x_WriteByte(0x001b, 0x09, dev); // Set default ranging inter-measurement period to 100ms
  218. errCode = 0x30 & VL6180x_WriteByte(0x003e, 0x31, dev); // Set default ALS inter-measurement period to 500ms
  219. errCode = 0x30 & VL6180x_WriteByte(0x0014, 0x24, dev); // Configures interrupt on "New Sample Ready threshold event"
  220. return errCode;
  221. }
  222. /**
  223. * @brief initializes I2C interface, checks that VL6180x is found and resets chip
  224. *
  225. * @param dev
  226. * @return uint8_t
  227. */
  228. uint8_t VL6180x_Init(struct VL6180x_ATY_Dev* dev){
  229. uint8_t reset;
  230. if(VL6180x_ReadByte(VL6180x_REG_SYSTEM_FRESH_OUT_OF_RESET, &reset, dev) == 1)
  231. return 1;
  232. // check to see has it be Initialised already
  233. if(reset == 1){
  234. if(VL6180x_Config(dev))
  235. return 3;
  236. //change fresh out of set status to 0
  237. if(VL6180x_WriteByte(VL6180x_REG_SYSTEM_FRESH_OUT_OF_RESET, 0x00, dev) == 1)
  238. return 2;
  239. }
  240. VL6180x_GetId(dev->id, dev);
  241. return 0;
  242. }
  243. /**
  244. * @brief
  245. *
  246. * @return uint8_t 1 for ready
  247. */
  248. uint8_t VL6180x_IsDeviceReady(struct VL6180x_ATY_Dev* dev){
  249. uint8_t byte = 0;
  250. if(VL6180x_ReadByte(VL6180x_REG_RESULT_INTERRUPT_STATUS_GPIO, &byte, dev) == 1)
  251. return 1;
  252. if((byte & 0x01))
  253. return 0;
  254. return 2;
  255. }
  256. /**
  257. * @brief
  258. *
  259. * @return uint8_t 1 for ready
  260. */
  261. uint8_t VL6180x_WaitDeviceReady(struct VL6180x_ATY_Dev* dev){
  262. uint16_t errCount = 0;
  263. while(VL6180x_IsDeviceReady(dev) != 0){
  264. errCount++;
  265. if(errCount > 100) return 2;
  266. }
  267. return 1;
  268. }
  269. /**
  270. * @brief
  271. *
  272. * @return uint8_t 1 for complete
  273. */
  274. uint8_t VL6180x_IsRangeComplete(struct VL6180x_ATY_Dev* dev){
  275. uint8_t status = 0;
  276. if(VL6180x_ReadByte(VL6180x_REG_RESULT_INTERRUPT_STATUS_GPIO, &status, dev) == 1)
  277. return 1;
  278. status &= 0x07;
  279. if((status & 0x04))
  280. return 1;
  281. return 2;
  282. }
  283. /**
  284. * @brief
  285. *
  286. * @return uint8_t 1 for complete
  287. */
  288. uint8_t VL6180x_WaitRangeComplete(struct VL6180x_ATY_Dev* dev){
  289. uint16_t errCount = 0;
  290. while(VL6180x_IsRangeComplete(dev) != 0){
  291. errCount++;
  292. if(errCount > 100) return 2;
  293. }
  294. return 1;
  295. }
  296. /**
  297. * @brief
  298. *
  299. * @param dev
  300. * @return uint8_t
  301. */
  302. uint8_t VL6180x_StartRange(struct VL6180x_ATY_Dev* dev){
  303. return VL6180x_WriteByte(VL6180x_REG_SYSRANGE_START, 0x01, dev);
  304. }
  305. /**
  306. * @brief
  307. *
  308. * @param dev
  309. * @return uint8_t
  310. * @note Return results of read reqyest also clears out the interrupt
  311. Be sure to check the return of {@link readRangeStatus} to before using
  312. the return value!
  313. */
  314. uint8_t VL6180x_ReadRangeResult(uint8_t* range, struct VL6180x_ATY_Dev* dev){
  315. if(VL6180x_ReadByte(VL6180x_REG_RESULT_RANGE_VAL, range, dev) == 1)
  316. return 1;
  317. printf_ATY_D("%sRange: %d\r\n", VL6180x_ATY_TAG, *range);
  318. return 0;
  319. }
  320. /**
  321. * @brief
  322. *
  323. * @param dev
  324. * @return uint8_t
  325. */
  326. uint8_t VL6180x_ClearInterrupts(struct VL6180x_ATY_Dev* dev){
  327. return VL6180x_WriteByte(VL6180x_REG_SYSTEM_INTERRUPT_CLEAR, 0x07, dev);
  328. }
  329. /**
  330. * @brief
  331. *
  332. * @param range
  333. * @param dev
  334. * @return uint8_t distance in millimeters if valid
  335. * @note tested in 100ms cycle with uart printf in stm32f103c8t6 72MHz(limited by uart)
  336. */
  337. uint8_t VL6180x_MeasureRangeOnce(uint8_t* range, struct VL6180x_ATY_Dev* dev){
  338. uint8_t errCode = 0;
  339. errCode = 0x00 & VL6180x_WaitDeviceReady(dev); // slower
  340. errCode = 0x10 & VL6180x_StartRange(dev);
  341. errCode = 0x20 & VL6180x_WaitRangeComplete(dev);
  342. errCode = 0x30 & VL6180x_ReadRangeResult(range, dev);
  343. errCode = 0x40 & VL6180x_ClearInterrupts(dev);
  344. return errCode;
  345. }
  346. /**
  347. * @brief
  348. *
  349. * @param range
  350. * @param dev
  351. * @return uint8_t distance in millimeters if valid
  352. * @note tested in 50ms cycle with uart printf in stm32f103c8t6 72MHz(limited by uart)
  353. */
  354. uint8_t VL6180x_MeasureRangeOnceFast(uint8_t* range, struct VL6180x_ATY_Dev* dev){
  355. uint8_t errCode = 0;
  356. // errCode = 0x00 & VL6180x_WaitDeviceReady(dev);
  357. errCode = 0x10 & VL6180x_StartRange(dev);
  358. // errCode = 0x10 & VL6180x_StartRangeContinuous(10, dev);
  359. errCode = 0x20 & VL6180x_WaitRangeComplete(dev);
  360. errCode = 0x30 & VL6180x_ReadRangeResult(range, dev);
  361. errCode = 0x40 & VL6180x_ClearInterrupts(dev);
  362. return errCode;
  363. }
  364. /**
  365. * @brief request ranging success/error message (retreive after ranging)
  366. *
  367. * @param value
  368. * @param dev
  369. * @return uint8_t one of possible VL6180x_ERROR_* values
  370. */
  371. uint8_t VL6180x_ReadRangeStatus(uint8_t* status, struct VL6180x_ATY_Dev* dev){
  372. if(VL6180x_ReadByte(VL6180x_REG_RESULT_RANGE_STATUS, status, dev) == 1)
  373. return 1;
  374. return 0;
  375. }
  376. /**
  377. * @brief start continuous ranging
  378. *
  379. * @param period_msOptional Period between ranges in ms.
  380. Values will be rounded down to 10ms units with minimum of 10ms.
  381. Default is 50
  382. * @param dev
  383. * @return uint8_t
  384. */
  385. uint8_t VL6180x_StartRangeContinuous(uint16_t period_ms, struct VL6180x_ATY_Dev* dev){
  386. uint8_t period_reg = 0;
  387. if(period_ms > 10){
  388. if(period_ms < 2550)
  389. period_reg = (period_ms / 10) - 1;
  390. else
  391. period_reg = 254;
  392. }
  393. // Set ranging inter-measurement
  394. if(VL6180x_WriteByte(SYSRANGE__INTERMEASUREMENT_PERIOD, period_reg, dev) == 1)
  395. return 1;
  396. // Start a continuous range measurement
  397. if(VL6180x_WriteByte(VL6180x_REG_SYSRANGE_START, 0x03, dev) == 1)
  398. return 1;
  399. return 0;
  400. }
  401. /**
  402. * @brief stop continuous range operation
  403. *
  404. * @param dev
  405. * @return uint8_t
  406. * @note stop the continuous range operation, by setting the range register
  407. back to 1, Page 7 of appication notes
  408. */
  409. uint8_t VL6180x_StopRangeContinuous(struct VL6180x_ATY_Dev* dev){
  410. return VL6180x_WriteByte(VL6180x_REG_SYSRANGE_START, 0x01, dev);
  411. }
  412. /**
  413. * @brief single shot lux measurement
  414. *
  415. * @param gain gain setting, one of VL6180x_ALS_GAIN_*
  416. * @param lux
  417. * @param dev
  418. * @return uint8_t
  419. * @note only for VL6180X
  420. */
  421. uint8_t VL6180x_ReadLux(uint8_t gain, float* lux, struct VL6180x_ATY_Dev* dev){
  422. uint16_t errCount = 0;
  423. uint8_t byte = 0;
  424. if(VL6180x_ReadByte(VL6180x_REG_SYSTEM_INTERRUPT_CONFIG, &byte, dev) == 1)
  425. return 1;
  426. byte &= ~0x38;
  427. byte |= (0x4 << 3); // IRQ on ALS ready
  428. if(VL6180x_WriteByte(VL6180x_REG_SYSTEM_INTERRUPT_CONFIG, byte, dev) == 1)
  429. return 2;
  430. // 100 ms integration period
  431. if(VL6180x_WriteByte(VL6180x_REG_SYSALS_INTEGRATION_PERIOD_HI, 0, dev) == 1)
  432. return 3;
  433. if(VL6180x_WriteByte(VL6180x_REG_SYSALS_INTEGRATION_PERIOD_LO, 100, dev) == 1)
  434. return 4;
  435. // analog gain
  436. if(gain > VL6180x_ALS_GAIN_40){
  437. gain = VL6180x_ALS_GAIN_40;
  438. }
  439. if(VL6180x_WriteByte(VL6180x_REG_SYSALS_ANALOGUE_GAIN, 0x40 | gain, dev) == 1)
  440. return 5;
  441. // start ALS
  442. if(VL6180x_WriteByte(VL6180x_REG_SYSALS_START, 0x01, dev) == 1)
  443. return 6;
  444. if(VL6180x_ReadByte(VL6180x_REG_RESULT_INTERRUPT_STATUS_GPIO, &byte, dev) == 1)
  445. return 7;
  446. // Poll until "New Sample Ready threshold event" is set
  447. while(4 != ((byte >> 3) & 0x7)){
  448. if(VL6180x_ReadByte(VL6180x_REG_RESULT_INTERRUPT_STATUS_GPIO, &byte, dev) == 1)
  449. return 8;
  450. errCount++;
  451. if(errCount > 1000) return 9;
  452. }
  453. // read lux!
  454. uint8_t luxBytes[2] = {0};
  455. if(VL6180x_ReadBytes(VL6180x_REG_RESULT_ALS_VAL, luxBytes, 2, dev) == 1)
  456. return 10;
  457. *lux = ((luxBytes[0] << 8) | luxBytes[1]);
  458. // clear interrupt
  459. if(VL6180x_WriteByte(VL6180x_REG_SYSTEM_INTERRUPT_CLEAR, 0x07, dev) == 1)
  460. return 11;
  461. *lux *= 0.32; // calibrated count/lux
  462. switch(gain){
  463. case VL6180x_ALS_GAIN_1:
  464. break;
  465. case VL6180x_ALS_GAIN_1_25:
  466. *lux /= 1.25;
  467. break;
  468. case VL6180x_ALS_GAIN_1_67:
  469. *lux /= 1.67;
  470. break;
  471. case VL6180x_ALS_GAIN_2_5:
  472. *lux /= 2.5;
  473. break;
  474. case VL6180x_ALS_GAIN_5:
  475. *lux /= 5;
  476. break;
  477. case VL6180x_ALS_GAIN_10:
  478. *lux /= 10;
  479. break;
  480. case VL6180x_ALS_GAIN_20:
  481. *lux /= 20;
  482. break;
  483. case VL6180x_ALS_GAIN_40:
  484. *lux /= 40;
  485. break;
  486. }
  487. *lux *= 100;
  488. *lux /= 100; // integration time in ms
  489. return 0;
  490. }
  491. #endif /* __VL6180x_ATY_C */
  492. /************************************ etc *************************************/
  493. /* init
  494. // VL6180x ---------------------------------------------------------------------
  495. #include "VL6180x_ATY.h"
  496. uint8_t VL6180x_1_I2C_Soft(uint8_t addr, uint8_t* data_t, uint8_t len, uint8_t rw){
  497. if(rw == __ATY_RW_W)
  498. return I2C_Write(addr, data_t, len, &HW_I2C_ATY_Dev_1);
  499. else if(rw == __ATY_RW_R)
  500. return I2C_Read(addr, data_t, len, &HW_I2C_ATY_Dev_1);
  501. }
  502. uint8_t VL6180x_1_I2C(uint8_t addr, uint8_t* data_t, uint8_t len, uint8_t rw){
  503. if(rw == __ATY_RW_W)
  504. return (uint8_t)HAL_I2C_Master_Transmit(&hi2c1, (addr << 1 | 0), data_t, len, 1000);
  505. else if(rw == __ATY_RW_R)
  506. return (uint8_t)HAL_I2C_Master_Receive(&hi2c1, (addr << 1 | 1), data_t, len, 1000);
  507. return 1;
  508. }
  509. uint8_t VL6180x_1_ATY_id[9] = {0};
  510. struct VL6180x_ATY_Dev VL6180x_ATY_t_1 = {
  511. .addr = VL6180x_ADDRESS,
  512. .id = VL6180x_1_ATY_id,
  513. .i2cProcess = VL6180x_1_I2C,
  514. .lock = __ATY_UNLOCKED
  515. };
  516. */
  517. /* use
  518. VL6180x_Init(&VL6180x_ATY_t_1);
  519. VL6180x_GetId(idGroup, &VL6180x_ATY_t_1);
  520. VL6180x_SetAddress(0x29, &VL6180x_ATY_t_1);
  521. VL6180x_GetOffset(&offset, &VL6180x_ATY_t_1);
  522. VL6180x_SetOffset(0x0D, &VL6180x_ATY_t_1);
  523. VL6180x_MeasureRangeOnce(&range, &VL6180x_ATY_t_1);
  524. VL6180x_MeasureRangeOnceFast(&range, &VL6180x_ATY_t_1);
  525. // only for VL6180X
  526. VL6180x_ReadLux(VL6180x_ALS_GAIN_40, &lux, &VL6180x_ATY_t_1);
  527. */
  528. /******************************************************************************/
  529. /******************************** End Of File *********************************/