vl53l1_platform.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * @file vl53l1_platform.c
  3. * @brief 针对HAL库与模拟IIC的平台兼容文件
  4. * @details This is the detail description.
  5. * @author WMD
  6. * @date date
  7. * @version 0.1
  8. * @par Copyright (c):
  9. * WMD
  10. * @par 日志 2020年4月16日13:40:46
  11. *
  12. * License terms: BSD 3-clause "New" or "Revised" License.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions are met:
  16. *
  17. * 1. Redistributions of source code must retain the above copyright notice, this
  18. * list of conditions and the following disclaimer.
  19. *
  20. * 2. Redistributions in binary form must reproduce the above copyright notice,
  21. * this list of conditions and the following disclaimer in the documentation
  22. * and/or other materials provided with the distribution.
  23. *
  24. * 3. Neither the name of the copyright holder nor the names of its contributors
  25. * may be used to endorse or promote products derived from this software
  26. * without specific prior written permission.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  29. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  30. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  32. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  34. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  35. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  36. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  37. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. *
  40. ********************************************************************************
  41. *
  42. */
  43. #include "vl53l1_platform.h"
  44. #include "vl53l1_api.h"
  45. #include "IOI2C.h"
  46. #include <string.h>
  47. VL53L1_Error VL53L1_WriteMulti(VL53L1_DEV Dev, uint16_t index, uint8_t *pdata, uint32_t count) {
  48. if(IICwriteBytes(Dev->I2cDevAddr,index,count,pdata)==count)
  49. return VL53L1_ERROR_NONE;
  50. else return VL53L1_ERROR_CONTROL_INTERFACE;
  51. }
  52. // the ranging_sensor_comms.dll will take care of the page selection
  53. VL53L1_Error VL53L1_ReadMulti(VL53L1_DEV Dev, uint16_t index, uint8_t *pdata, uint32_t count) {
  54. if(IICreadBytes(Dev->I2cDevAddr,index,count,pdata)==count)
  55. return VL53L1_ERROR_NONE;
  56. else return VL53L1_ERROR_CONTROL_INTERFACE;
  57. }
  58. VL53L1_Error VL53L1_WrByte(VL53L1_DEV Dev, uint16_t index, uint8_t data) {
  59. if(IICwriteByte(Dev->I2cDevAddr,index,data))
  60. return VL53L1_ERROR_NONE;
  61. else return VL53L1_ERROR_CONTROL_INTERFACE;
  62. }
  63. VL53L1_Error VL53L1_WrWord(VL53L1_DEV Dev, uint16_t index, uint16_t data) {
  64. IICwriteByte(Dev->I2cDevAddr,index,data>>8);
  65. IICwriteByte(Dev->I2cDevAddr,index,data & 0x00ff);
  66. return VL53L1_ERROR_NONE;
  67. }
  68. VL53L1_Error VL53L1_WrDWord(VL53L1_DEV Dev, uint16_t index, uint32_t data) {
  69. IICwriteByte(Dev->I2cDevAddr,index,data>>24);
  70. IICwriteByte(Dev->I2cDevAddr,index,(data >> 16) & 0xff);
  71. IICwriteByte(Dev->I2cDevAddr,index,(data >> 8) & 0xff);
  72. IICwriteByte(Dev->I2cDevAddr,index,(data >> 0) & 0xff);
  73. return VL53L1_ERROR_NONE;
  74. }
  75. VL53L1_Error VL53L1_UpdateByte(VL53L1_DEV Dev, uint16_t index, uint8_t AndData, uint8_t OrData) {
  76. uint8_t data;
  77. VL53L1_Error states=VL53L1_ERROR_NONE;
  78. if(!IIC_ReadOneByte(Dev->I2cDevAddr,index,&data))
  79. {
  80. states=VL53L1_ERROR_CONTROL_INTERFACE;
  81. }
  82. data=(data & AndData) | OrData;
  83. if(!IICwriteByte(Dev->I2cDevAddr,index,data))
  84. {
  85. states=VL53L1_ERROR_CONTROL_INTERFACE;
  86. }
  87. return states;
  88. }
  89. VL53L1_Error VL53L1_RdByte(VL53L1_DEV Dev, uint16_t index, uint8_t *data) {
  90. if(IIC_ReadOneByte(Dev->I2cDevAddr,index,data))
  91. {
  92. return VL53L1_ERROR_NONE;
  93. }else return VL53L1_ERROR_CONTROL_INTERFACE;
  94. }
  95. VL53L1_Error VL53L1_RdWord(VL53L1_DEV Dev, uint16_t index, uint16_t *data) {
  96. uint8_t ret[2];
  97. IICreadBytes(Dev->I2cDevAddr,index,2,(uint8_t*)ret);
  98. *data=(ret[0]<<8) | ret[1];
  99. return VL53L1_ERROR_NONE;
  100. }
  101. VL53L1_Error VL53L1_RdDWord(VL53L1_DEV Dev, uint16_t index, uint32_t *data) {
  102. uint8_t ret[4];
  103. IICreadBytes(Dev->I2cDevAddr,index,4,(uint8_t*)ret);
  104. *data=(ret[0]<<24) | (ret[1]<<16) | (ret[2]<<8) | ret[3];
  105. return VL53L1_ERROR_NONE;
  106. }
  107. VL53L1_Error VL53L1_GetTickCount(
  108. uint32_t *ptick_count_ms)
  109. {
  110. *ptick_count_ms=HAL_GetTick();
  111. return VL53L1_ERROR_NONE;;
  112. }
  113. VL53L1_Error VL53L1_GetTimerFrequency(int32_t *ptimer_freq_hz)
  114. {
  115. *ptimer_freq_hz=10000000;
  116. return VL53L1_ERROR_NONE;;
  117. }
  118. VL53L1_Error VL53L1_WaitMs(VL53L1_Dev_t *pdev, int32_t wait_ms){
  119. HAL_Delay(wait_ms);
  120. return VL53L1_ERROR_NONE;;
  121. }
  122. VL53L1_Error VL53L1_WaitUs(VL53L1_Dev_t *pdev, int32_t wait_us){
  123. //这里使用不精确的循环延时代替,省去一个定时器
  124. wait_us=wait_us*72;
  125. while(wait_us)wait_us--;
  126. return VL53L1_ERROR_NONE;;
  127. }
  128. VL53L1_Error VL53L1_WaitValueMaskEx(
  129. VL53L1_Dev_t *pdev,
  130. uint32_t timeout_ms,
  131. uint16_t index,
  132. uint8_t value,
  133. uint8_t mask,
  134. uint32_t poll_delay_ms)
  135. {
  136. VL53L1_Error status = VL53L1_ERROR_NONE;;
  137. uint32_t polling_time_ms = 0;
  138. uint8_t byte_value = 0;
  139. uint8_t found = 0;
  140. /* wait until value is found, timeout reached on error occurred */
  141. while ((status == VL53L1_ERROR_NONE)
  142. && (polling_time_ms < timeout_ms)
  143. && (found == 0))
  144. {
  145. if (status == VL53L1_ERROR_NONE)
  146. status = VL53L1_RdByte(pdev, index, &byte_value);
  147. if ((byte_value & mask) == value)
  148. found = 1;
  149. if (status == VL53L1_ERROR_NONE && found == 0 && poll_delay_ms > 0)
  150. status = VL53L1_WaitMs(pdev, poll_delay_ms);
  151. /* Update polling time */
  152. polling_time_ms++;
  153. }
  154. if (found == 0 && status == VL53L1_ERROR_NONE)
  155. status = VL53L1_ERROR_TIME_OUT;
  156. return status;
  157. }