Ravago Jones | 9c10b2a | 2023-02-06 12:41:59 -0800 | [diff] [blame^] | 1 | diff --git a/core/VL53L1X_api.c b/core/VL53L1X_api.c |
| 2 | index 572040d..7cdb719 100644 |
| 3 | --- a/core/VL53L1X_api.c |
| 4 | +++ b/core/VL53L1X_api.c |
| 5 | @@ -62,7 +62,7 @@ |
| 6 | * @brief Functions implementation |
| 7 | */ |
| 8 | |
| 9 | -#include "VL53L1X_api.h" |
| 10 | +#include "core/VL53L1X_api.h" |
| 11 | #include <string.h> |
| 12 | |
| 13 | #if 0 |
| 14 | @@ -665,7 +665,7 @@ VL53L1X_ERROR VL53L1X_GetOffset(uint16_t dev, int16_t *offset) |
| 15 | Temp = Temp>>5; |
| 16 | *offset = (int16_t)(Temp); |
| 17 | |
| 18 | - if(*offset > 1024) |
| 19 | + if(*offset > 1024) |
| 20 | { |
| 21 | *offset = *offset - 2048; |
| 22 | } |
| 23 | diff --git a/core/VL53L1X_api.h b/core/VL53L1X_api.h |
| 24 | index fc93e56..790ffcb 100644 |
| 25 | --- a/core/VL53L1X_api.h |
| 26 | +++ b/core/VL53L1X_api.h |
| 27 | @@ -68,7 +68,11 @@ |
| 28 | #ifndef _API_H_ |
| 29 | #define _API_H_ |
| 30 | |
| 31 | -#include "vl53l1_platform.h" |
| 32 | +#include "platform/vl53l1_platform.h" |
| 33 | + |
| 34 | +#ifdef __cplusplus |
| 35 | +extern "C" { |
| 36 | +#endif |
| 37 | |
| 38 | #define VL53L1X_IMPLEMENTATION_VER_MAJOR 3 |
| 39 | #define VL53L1X_IMPLEMENTATION_VER_MINOR 5 |
| 40 | @@ -390,4 +394,8 @@ VL53L1X_ERROR VL53L1X_GetSigmaThreshold(uint16_t dev, uint16_t *signal); |
| 41 | */ |
| 42 | VL53L1X_ERROR VL53L1X_StartTemperatureUpdate(uint16_t dev); |
| 43 | |
| 44 | +#ifdef __cplusplus |
| 45 | +} |
| 46 | +#endif |
| 47 | + |
| 48 | #endif |
| 49 | diff --git a/core/VL53L1X_calibration.c b/core/VL53L1X_calibration.c |
| 50 | index 0c58fb1..59381da 100644 |
| 51 | --- a/core/VL53L1X_calibration.c |
| 52 | +++ b/core/VL53L1X_calibration.c |
| 53 | @@ -63,8 +63,8 @@ |
| 54 | * @file vl53l1x_calibration.c |
| 55 | * @brief Calibration functions implementation |
| 56 | */ |
| 57 | -#include "VL53L1X_api.h" |
| 58 | -#include "VL53L1X_calibration.h" |
| 59 | +#include "core/VL53L1X_api.h" |
| 60 | +#include "core/VL53L1X_calibration.h" |
| 61 | |
| 62 | #define ALGO__PART_TO_PART_RANGE_OFFSET_MM 0x001E |
| 63 | #define MM_CONFIG__INNER_OFFSET_MM 0x0020 |
| 64 | diff --git a/core/VL53L1X_calibration.h b/core/VL53L1X_calibration.h |
| 65 | index c057722..eaa405a 100644 |
| 66 | --- a/core/VL53L1X_calibration.h |
| 67 | +++ b/core/VL53L1X_calibration.h |
| 68 | @@ -67,6 +67,12 @@ |
| 69 | #ifndef _CALIBRATION_H_ |
| 70 | #define _CALIBRATION_H_ |
| 71 | |
| 72 | +#include <stdint.h> |
| 73 | + |
| 74 | +#ifdef __cplusplus |
| 75 | +extern "C" { |
| 76 | +#endif |
| 77 | + |
| 78 | /** |
| 79 | * @brief This function performs the offset calibration.\n |
| 80 | * The function returns the offset value found and programs the offset compensation into the device. |
| 81 | @@ -90,4 +96,8 @@ int8_t VL53L1X_CalibrateOffset(uint16_t dev, uint16_t TargetDistInMm, int16_t *o |
| 82 | */ |
| 83 | int8_t VL53L1X_CalibrateXtalk(uint16_t dev, uint16_t TargetDistInMm, uint16_t *xtalk); |
| 84 | |
| 85 | +#ifdef __cplusplus |
| 86 | +} |
| 87 | +#endif |
| 88 | + |
| 89 | #endif |
| 90 | diff --git a/platform/vl53l1_platform.c b/platform/vl53l1_platform.c |
| 91 | index 4d17113..0cd0620 100644 |
| 92 | --- a/platform/vl53l1_platform.c |
| 93 | +++ b/platform/vl53l1_platform.c |
| 94 | @@ -1,75 +1,144 @@ |
| 95 |
|
| 96 | -/*
|
| 97 | -* This file is part of VL53L1 Platform
|
| 98 | -*
|
| 99 | -* Copyright (c) 2016, STMicroelectronics - All Rights Reserved
|
| 100 | -*
|
| 101 | -* License terms: BSD 3-clause "New" or "Revised" License.
|
| 102 | -*
|
| 103 | -* Redistribution and use in source and binary forms, with or without
|
| 104 | -* modification, are permitted provided that the following conditions are met:
|
| 105 | -*
|
| 106 | -* 1. Redistributions of source code must retain the above copyright notice, this
|
| 107 | -* list of conditions and the following disclaimer.
|
| 108 | -*
|
| 109 | -* 2. Redistributions in binary form must reproduce the above copyright notice,
|
| 110 | -* this list of conditions and the following disclaimer in the documentation
|
| 111 | -* and/or other materials provided with the distribution.
|
| 112 | -*
|
| 113 | -* 3. Neither the name of the copyright holder nor the names of its contributors
|
| 114 | -* may be used to endorse or promote products derived from this software
|
| 115 | -* without specific prior written permission.
|
| 116 | -*
|
| 117 | -* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
| 118 | -* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
| 119 | -* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
| 120 | -* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
| 121 | -* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
| 122 | -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
| 123 | -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
| 124 | -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
| 125 | -* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
| 126 | -* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
| 127 | -*
|
| 128 | +/*
|
| 129 | +* This file is part of VL53L1 Platform
|
| 130 | +*
|
| 131 | +* Copyright (c) 2016, STMicroelectronics - All Rights Reserved
|
| 132 | +*
|
| 133 | +* License terms: BSD 3-clause "New" or "Revised" License.
|
| 134 | +*
|
| 135 | +* Redistribution and use in source and binary forms, with or without
|
| 136 | +* modification, are permitted provided that the following conditions are met:
|
| 137 | +*
|
| 138 | +* 1. Redistributions of source code must retain the above copyright notice, this
|
| 139 | +* list of conditions and the following disclaimer.
|
| 140 | +*
|
| 141 | +* 2. Redistributions in binary form must reproduce the above copyright notice,
|
| 142 | +* this list of conditions and the following disclaimer in the documentation
|
| 143 | +* and/or other materials provided with the distribution.
|
| 144 | +*
|
| 145 | +* 3. Neither the name of the copyright holder nor the names of its contributors
|
| 146 | +* may be used to endorse or promote products derived from this software
|
| 147 | +* without specific prior written permission.
|
| 148 | +*
|
| 149 | +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
| 150 | +* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
| 151 | +* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
| 152 | +* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
| 153 | +* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
| 154 | +* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
| 155 | +* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
| 156 | +* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
| 157 | +* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
| 158 | +* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
| 159 | +*
|
| 160 | */
|
| 161 |
|
| 162 | -#include "vl53l1_platform.h"
|
| 163 | +#include "platform/vl53l1_platform.h"
|
| 164 | +#include <stdint.h>
|
| 165 | #include <string.h>
|
| 166 | #include <time.h>
|
| 167 | #include <math.h>
|
| 168 |
|
| 169 | -int8_t VL53L1_WriteMulti( uint16_t dev, uint16_t index, uint8_t *pdata, uint32_t count) {
|
| 170 | - return 0; // to be implemented
|
| 171 | +#include "hardware/i2c.h"
|
| 172 | +#include "pico/stdlib.h"
|
| 173 | +
|
| 174 | +static uint8_t
|
| 175 | + buffer[VL53L1X_I2C_BUF_SIZE + 2]; // R/W buffer for all transactions
|
| 176 | +
|
| 177 | +/* ----- Static helper functions ----- */
|
| 178 | +
|
| 179 | +// Read len bytes of data from i2c device with address dev into buff
|
| 180 | +// Expects register address to already be configured
|
| 181 | +// Returns 0 on success, -1 on failure
|
| 182 | +static int8_t Pico_I2CRead(i2c_inst_t *i2c_dev, uint16_t addr, uint8_t *buff,
|
| 183 | + uint8_t len) {
|
| 184 | + return (i2c_read_blocking(i2c_dev, addr, buff, len, false) == len) - 1;
|
| 185 | +}
|
| 186 | +
|
| 187 | +// Write len bytes of data to i2c device with address dev from buff
|
| 188 | +// Returns 0 on success, -1 on failure
|
| 189 | +static int8_t Pico_I2CWrite(i2c_inst_t *i2c_dev, uint16_t addr, uint8_t *buff,
|
| 190 | + uint8_t len) {
|
| 191 | + return (i2c_write_blocking(i2c_dev, addr, buff, len, false) == len) - 1;
|
| 192 | }
|
| 193 |
|
| 194 | -int8_t VL53L1_ReadMulti(uint16_t dev, uint16_t index, uint8_t *pdata, uint32_t count){
|
| 195 | - return 0; // to be implemented
|
| 196 | +int8_t VL53L1_WriteMulti(uint16_t dev, uint16_t index, uint8_t *pdata,
|
| 197 | + uint32_t count) {
|
| 198 | + if (count >= VL53L1X_I2C_BUF_SIZE) {
|
| 199 | + return -1;
|
| 200 | + }
|
| 201 | +
|
| 202 | + buffer[0] = 0xFF & (index >> 8);
|
| 203 | + buffer[1] = 0xFF & index;
|
| 204 | + memcpy(&buffer[2], pdata, count);
|
| 205 | +
|
| 206 | + i2c_inst_t *i2c_dev = i2c0;
|
| 207 | + if (dev == 1) {
|
| 208 | + i2c_dev = i2c0;
|
| 209 | + } else if (dev == 2) {
|
| 210 | + i2c_dev = i2c1;
|
| 211 | + }
|
| 212 | +
|
| 213 | + return Pico_I2CWrite(i2c_dev, VL53L1X_I2C_DEV_ADDR, buffer, (count + 2));
|
| 214 | +}
|
| 215 | +
|
| 216 | +int8_t VL53L1_ReadMulti(uint16_t dev, uint16_t index, uint8_t *pdata,
|
| 217 | + uint32_t count) {
|
| 218 | + if (count >= VL53L1X_I2C_BUF_SIZE) {
|
| 219 | + return -1;
|
| 220 | + }
|
| 221 | +
|
| 222 | + buffer[0] = 0xFF & (index >> 8);
|
| 223 | + buffer[1] = 0xFF & index;
|
| 224 | +
|
| 225 | + i2c_inst_t *i2c_dev = i2c0;
|
| 226 | + if (dev == 1) {
|
| 227 | + i2c_dev = i2c0;
|
| 228 | + } else if (dev == 2) {
|
| 229 | + i2c_dev = i2c1;
|
| 230 | + }
|
| 231 | +
|
| 232 | + int8_t result = Pico_I2CWrite(i2c_dev, VL53L1X_I2C_DEV_ADDR, buffer, 2);
|
| 233 | +
|
| 234 | + if (result == 0) {
|
| 235 | + pdata[0] = index;
|
| 236 | + result = Pico_I2CRead(i2c_dev, VL53L1X_I2C_DEV_ADDR, pdata, count);
|
| 237 | + }
|
| 238 | +
|
| 239 | + return result;
|
| 240 | }
|
| 241 |
|
| 242 | int8_t VL53L1_WrByte(uint16_t dev, uint16_t index, uint8_t data) {
|
| 243 | - return 0; // to be implemented
|
| 244 | + return VL53L1_WriteMulti(dev, index, &data, 1);
|
| 245 | }
|
| 246 |
|
| 247 | int8_t VL53L1_WrWord(uint16_t dev, uint16_t index, uint16_t data) {
|
| 248 | - return 0; // to be implemented
|
| 249 | + data = __htons(data);
|
| 250 | + return VL53L1_WriteMulti(dev, index, (uint8_t *)&data, 2);
|
| 251 | }
|
| 252 |
|
| 253 | int8_t VL53L1_WrDWord(uint16_t dev, uint16_t index, uint32_t data) {
|
| 254 | - return 0; // to be implemented
|
| 255 | + data = __htonl(data);
|
| 256 | + return VL53L1_WriteMulti(dev, index, (uint8_t *)&data, 4);
|
| 257 | }
|
| 258 |
|
| 259 | int8_t VL53L1_RdByte(uint16_t dev, uint16_t index, uint8_t *data) {
|
| 260 | - return 0; // to be implemented
|
| 261 | + return VL53L1_ReadMulti(dev, index, data, 1);
|
| 262 | }
|
| 263 |
|
| 264 | int8_t VL53L1_RdWord(uint16_t dev, uint16_t index, uint16_t *data) {
|
| 265 | - return 0; // to be implemented
|
| 266 | + int8_t result = VL53L1_ReadMulti(dev, index, (uint8_t *)data, 2);
|
| 267 | + *data = __ntohs(*data);
|
| 268 | + return result;
|
| 269 | }
|
| 270 |
|
| 271 | int8_t VL53L1_RdDWord(uint16_t dev, uint16_t index, uint32_t *data) {
|
| 272 | - return 0; // to be implemented
|
| 273 | + int8_t result = VL53L1_ReadMulti(dev, index, (uint8_t *)data, 4);
|
| 274 | + *data = __ntohl(*data);
|
| 275 | + return result;
|
| 276 | }
|
| 277 |
|
| 278 | -int8_t VL53L1_WaitMs(uint16_t dev, int32_t wait_ms){
|
| 279 | - return 0; // to be implemented
|
| 280 | +int8_t VL53L1_WaitMs(uint16_t dev, int32_t wait_ms) {
|
| 281 | + sleep_ms(wait_ms);
|
| 282 | + return 0;
|
| 283 | }
|
| 284 | diff --git a/platform/vl53l1_platform.h b/platform/vl53l1_platform.h |
| 285 | index 75bc4c2..e657ba6 100644 |
| 286 | --- a/platform/vl53l1_platform.h |
| 287 | +++ b/platform/vl53l1_platform.h |
| 288 | @@ -2,17 +2,20 @@ |
| 289 | * @file vl53l1_platform.h
|
| 290 | * @brief Those platform functions are platform dependent and have to be implemented by the user
|
| 291 | */
|
| 292 | -
|
| 293 | +
|
| 294 | #ifndef _VL53L1_PLATFORM_H_
|
| 295 | #define _VL53L1_PLATFORM_H_
|
| 296 |
|
| 297 | -#include "vl53l1_types.h"
|
| 298 | +#include "platform/vl53l1_types.h"
|
| 299 |
|
| 300 | #ifdef __cplusplus
|
| 301 | extern "C"
|
| 302 | {
|
| 303 | #endif
|
| 304 |
|
| 305 | +#define VL53L1X_I2C_BUF_SIZE 256
|
| 306 | +#define VL53L1X_I2C_DEV_ADDR 0x29
|
| 307 | +
|
| 308 | typedef struct {
|
| 309 | uint32_t dummy;
|
| 310 | } VL53L1_Dev_t;
|