|
| 1 | +/* |
| 2 | + * Copyright (c) 2019 Aaron Tsui <[email protected]> |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | + |
| 8 | +#include "vl53l1_platform.h" |
| 9 | + |
| 10 | +#include <sensor.h> |
| 11 | +#include <kernel.h> |
| 12 | +#include <device.h> |
| 13 | +#include <init.h> |
| 14 | +#include <i2c.h> |
| 15 | +#include <logging/log.h> |
| 16 | + |
| 17 | +#define LOG_LEVEL CONFIG_SENSOR_LOG_LEVEL |
| 18 | +LOG_MODULE_DECLARE(VL53L1X); |
| 19 | + |
| 20 | +VL53L1_Error VL53L1_WriteMulti(VL53L1_DEV Dev, uint16_t index, uint8_t *pdata, |
| 21 | + uint32_t count) |
| 22 | +{ |
| 23 | + VL53L1_Error Status = VL53L1_ERROR_NONE; |
| 24 | + int32_t status_int = 0; |
| 25 | + uint8_t buf[count + 2]; |
| 26 | + |
| 27 | + buf[0] = index >> 8; |
| 28 | + buf[1] = index & 0xFF; |
| 29 | + memcpy(&buf[2], pdata, count); |
| 30 | + |
| 31 | + status_int = i2c_write(Dev->i2c, buf, count + 2, Dev->I2cDevAddr); |
| 32 | + |
| 33 | + if (status_int < 0) { |
| 34 | + Status = VL53L1_ERROR_CONTROL_INTERFACE; |
| 35 | + LOG_ERR("Failed to write"); |
| 36 | + } |
| 37 | + |
| 38 | + return Status; |
| 39 | +} |
| 40 | + |
| 41 | +VL53L1_Error VL53L1_ReadMulti(VL53L1_DEV Dev, uint16_t index, uint8_t *pdata, |
| 42 | + uint32_t count) |
| 43 | +{ |
| 44 | + VL53L1_Error Status = VL53L1_ERROR_NONE; |
| 45 | + int32_t status_int; |
| 46 | + u8_t buf[2]; |
| 47 | + |
| 48 | + buf[0] = index >> 8; |
| 49 | + buf[1] = index & 0xFF; |
| 50 | + |
| 51 | + status_int = |
| 52 | + i2c_write_read(Dev->i2c, Dev->I2cDevAddr, buf, 2, pdata, count); |
| 53 | + if (status_int < 0) { |
| 54 | + LOG_ERR("Failed to read"); |
| 55 | + return -EIO; |
| 56 | + } |
| 57 | + |
| 58 | + return Status; |
| 59 | +} |
| 60 | + |
| 61 | +VL53L1_Error VL53L1_WrByte(VL53L1_DEV Dev, uint16_t index, uint8_t data) |
| 62 | +{ |
| 63 | + VL53L1_Error Status = VL53L1_ERROR_NONE; |
| 64 | + int32_t status_int; |
| 65 | + u8_t buf[3]; |
| 66 | + |
| 67 | + buf[0] = index >> 8; |
| 68 | + buf[1] = index & 0xFF; |
| 69 | + buf[2] = data; |
| 70 | + |
| 71 | + status_int = i2c_write(Dev->i2c, buf, 3, Dev->I2cDevAddr); |
| 72 | + |
| 73 | + if (status_int < 0) { |
| 74 | + Status = VL53L1_ERROR_CONTROL_INTERFACE; |
| 75 | + LOG_ERR("i2c_reg_write_byte failed (%d)", Status); |
| 76 | + } |
| 77 | + |
| 78 | + return Status; |
| 79 | +} |
| 80 | + |
| 81 | +VL53L1_Error VL53L1_WrWord(VL53L1_DEV Dev, uint16_t index, uint16_t data) |
| 82 | +{ |
| 83 | + VL53L1_Error Status = VL53L1_ERROR_NONE; |
| 84 | + int32_t status_int; |
| 85 | + uint8_t buf[4]; |
| 86 | + |
| 87 | + buf[0] = index >> 8; |
| 88 | + buf[1] = index & 0x00FF; |
| 89 | + buf[2] = data >> 8; |
| 90 | + buf[3] = data & 0x00FF; |
| 91 | + |
| 92 | + status_int = i2c_write(Dev->i2c, buf, 4, Dev->I2cDevAddr); |
| 93 | + if (status_int < 0) { |
| 94 | + Status = VL53L1_ERROR_CONTROL_INTERFACE; |
| 95 | + LOG_ERR("i2c_write failed (%d)", Status); |
| 96 | + } |
| 97 | + |
| 98 | + return Status; |
| 99 | +} |
| 100 | + |
| 101 | +VL53L1_Error VL53L1_WrDWord(VL53L1_DEV Dev, uint16_t index, uint32_t data) |
| 102 | +{ |
| 103 | + VL53L1_Error Status = VL53L1_ERROR_NONE; |
| 104 | + int32_t status_int; |
| 105 | + uint8_t buf[6]; |
| 106 | + |
| 107 | + buf[0] = index >> 8; |
| 108 | + buf[1] = index & 0x00FF; |
| 109 | + buf[2] = (data >> 24) & 0xFF; |
| 110 | + buf[3] = (data >> 16) & 0xFF; |
| 111 | + buf[4] = (data >> 8) & 0xFF; |
| 112 | + buf[5] = (data >> 0) & 0xFF; |
| 113 | + |
| 114 | + status_int = i2c_write(Dev->i2c, buf, 6, Dev->I2cDevAddr); |
| 115 | + if (status_int < 0) { |
| 116 | + Status = VL53L1_ERROR_CONTROL_INTERFACE; |
| 117 | + LOG_ERR("i2c_write failed (%d)", Status); |
| 118 | + } |
| 119 | + |
| 120 | + return Status; |
| 121 | +} |
| 122 | + |
| 123 | +VL53L1_Error VL53L1_UpdateByte(VL53L1_DEV Dev, uint16_t index, uint8_t AndData, |
| 124 | + uint8_t OrData) |
| 125 | +{ |
| 126 | + VL53L1_Error Status = VL53L1_ERROR_NONE; |
| 127 | + int32_t status_int; |
| 128 | + uint8_t deviceAddress; |
| 129 | + uint8_t data; |
| 130 | + |
| 131 | + deviceAddress = Dev->I2cDevAddr; |
| 132 | + |
| 133 | + status_int = VL53L1_RdByte(Dev, index, &data); |
| 134 | + if (status_int < 0) { |
| 135 | + Status = VL53L1_ERROR_CONTROL_INTERFACE; |
| 136 | + LOG_ERR("VL53L1_RdByte failed (%d)", Status); |
| 137 | + } |
| 138 | + |
| 139 | + if (Status == VL53L1_ERROR_NONE) { |
| 140 | + data = (data & AndData) | OrData; |
| 141 | + status_int = VL53L1_WrByte(Dev, index, data); |
| 142 | + if (status_int != 0) { |
| 143 | + Status = VL53L1_ERROR_CONTROL_INTERFACE; |
| 144 | + LOG_DBG("VL53L1_WrByte failed.(%d)", Status); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + return Status; |
| 149 | +} |
| 150 | + |
| 151 | +VL53L1_Error VL53L1_RdByte(VL53L1_DEV Dev, uint16_t index, uint8_t *data) |
| 152 | +{ |
| 153 | + VL53L1_Error Status = VL53L1_ERROR_NONE; |
| 154 | + int32_t status_int; |
| 155 | + uint8_t buf[2]; |
| 156 | + |
| 157 | + buf[0] = index >> 8; |
| 158 | + buf[1] = index & 0xFF; |
| 159 | + |
| 160 | + status_int = i2c_write_read(Dev->i2c, Dev->I2cDevAddr, buf, 2, buf, 1); |
| 161 | + if (status_int < 0) { |
| 162 | + Status = VL53L1_ERROR_CONTROL_INTERFACE; |
| 163 | + LOG_ERR("i2c_reg_read_byte failed (%d)", Status); |
| 164 | + } |
| 165 | + *data = buf[0]; |
| 166 | + |
| 167 | + return Status; |
| 168 | +} |
| 169 | + |
| 170 | +VL53L1_Error VL53L1_RdWord(VL53L1_DEV Dev, uint16_t index, uint16_t *data) |
| 171 | +{ |
| 172 | + VL53L1_Error Status = VL53L1_ERROR_NONE; |
| 173 | + int32_t status_int; |
| 174 | + uint8_t buf[2]; |
| 175 | + |
| 176 | + buf[0] = index >> 8; |
| 177 | + buf[1] = index & 0xFF; |
| 178 | + |
| 179 | + status_int = i2c_write_read(Dev->i2c, Dev->I2cDevAddr, buf, 2, buf, 2); |
| 180 | + if (status_int < 0) { |
| 181 | + LOG_ERR("i2c_write_read failed"); |
| 182 | + return -EIO; |
| 183 | + } |
| 184 | + *data = ((uint16_t)buf[0] << 8) + (uint16_t)buf[1]; |
| 185 | + |
| 186 | + return Status; |
| 187 | +} |
| 188 | + |
| 189 | +VL53L1_Error VL53L1_RdDWord(VL53L1_DEV Dev, uint16_t index, uint32_t *data) |
| 190 | +{ |
| 191 | + VL53L1_Error Status = VL53L1_ERROR_NONE; |
| 192 | + int32_t status_int; |
| 193 | + u8_t buf[4]; |
| 194 | + |
| 195 | + buf[0] = index >> 8; |
| 196 | + buf[1] = index & 0xFF; |
| 197 | + |
| 198 | + status_int = i2c_write_read(Dev->i2c, Dev->I2cDevAddr, buf, 2, buf, 4); |
| 199 | + if (status_int < 0) { |
| 200 | + LOG_ERR("i2c_write_read failed"); |
| 201 | + return -EIO; |
| 202 | + } |
| 203 | + *data = ((uint32_t)buf[0] << 24) + ((uint32_t)buf[1] << 16) + |
| 204 | + ((uint32_t)buf[2] << 8) + (uint32_t)buf[3]; |
| 205 | + |
| 206 | + return Status; |
| 207 | +} |
| 208 | + |
| 209 | +VL53L1_Error VL53L1_GetTickCount(uint32_t *ptick_count_ms) |
| 210 | +{ |
| 211 | + VL53L1_Error status = VL53L1_ERROR_NONE; |
| 212 | + *ptick_count_ms = k_cycle_get_32(); |
| 213 | + return status; |
| 214 | +} |
| 215 | + |
| 216 | +VL53L1_Error VL53L1_GetTimerFrequency(int32_t *ptimer_freq_hz) |
| 217 | +{ |
| 218 | + VL53L1_Error status = VL53L1_ERROR_NONE; |
| 219 | + return status; |
| 220 | +} |
| 221 | + |
| 222 | +VL53L1_Error VL53L1_WaitMs(VL53L1_Dev_t *pdev, int32_t wait_ms) |
| 223 | +{ |
| 224 | + VL53L1_Error status = VL53L1_ERROR_NONE; |
| 225 | + k_sleep(wait_ms); |
| 226 | + return status; |
| 227 | +} |
| 228 | + |
| 229 | +VL53L1_Error VL53L1_WaitUs(VL53L1_Dev_t *pdev, int32_t wait_us) |
| 230 | +{ |
| 231 | + VL53L1_Error status = VL53L1_ERROR_NONE; |
| 232 | + k_busy_wait(wait_us); |
| 233 | + return status; |
| 234 | +} |
| 235 | + |
| 236 | +VL53L1_Error VL53L1_WaitValueMaskEx(VL53L1_Dev_t *pdev, uint32_t timeout_ms, |
| 237 | + uint16_t index, uint8_t value, uint8_t mask, |
| 238 | + uint32_t poll_delay_ms) |
| 239 | +{ |
| 240 | + VL53L1_Error status = VL53L1_ERROR_NONE; |
| 241 | + return status; |
| 242 | +} |
0 commit comments