Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
vl53l1_platform.c
Go to the documentation of this file.
1 
2 /*
3 * This file is part of VL53L1 Platform
4 *
5 * Copyright (c) 2016, STMicroelectronics - All Rights Reserved
6 *
7 * License terms: BSD 3-clause "New" or "Revised" License.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright notice, this
13 * list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
18 *
19 * 3. Neither the name of the copyright holder nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *
34 */
35 
36 #include "vl53l1_platform.h"
37 #include <string.h>
38 #include <time.h>
39 #include <math.h>
40 
41 #include <assert.h>
42 
44 {
45  assert(2 + count <= I2C_BUF_LEN);
46  dev->i2c_trans.buf[0] = (index & 0xFF00) >> 8; // MSB first
47  dev->i2c_trans.buf[1] = (index & 0x00FF);
48  memcpy((uint8_t *) dev->i2c_trans.buf + 2, pdata, count);
49  return !i2c_blocking_transmit(dev->i2c_p, &dev->i2c_trans,
50  dev->i2c_trans.slave_addr, 2 + count);
51 }
52 
54 {
55  assert(count <= I2C_BUF_LEN);
56  dev->i2c_trans.buf[0] = (index & 0xFF00) >> 8; // MSB first
57  dev->i2c_trans.buf[1] = (index & 0x00FF);
58  int8_t ret = !i2c_blocking_transceive(dev->i2c_p, &dev->i2c_trans,
59  dev->i2c_trans.slave_addr, 2, count);
60  memcpy(pdata, (uint8_t *) dev->i2c_trans.buf, count);
61  return ret;
62 }
63 
65 {
66  return VL53L1_WriteMulti(dev, index, &data, 1);
67 }
68 
70 {
71  uint8_t data_u8[] = {
72  (data & 0xFF00) >> 8,
73  (data & 0x00FF)
74  };
75  return VL53L1_WriteMulti(dev, index, data_u8, 2);
76 }
77 
79 {
80  uint8_t data_u8[] = {
81  (data & 0xFF000000) >> 24,
82  (data & 0x00FF0000) >> 16,
83  (data & 0x0000FF00) >> 8,
84  (data & 0x000000FF)
85  };
86  return VL53L1_WriteMulti(dev, index, data_u8, 4);
87 }
88 
90 {
91  return VL53L1_ReadMulti(dev, index, data, 1);
92 }
93 
95 {
96  uint8_t data_u8[2];
97  int8_t ret = VL53L1_ReadMulti(dev, index, data_u8, 2);
98  *data = (data_u8[0] << 8) | data_u8[1];
99  return ret;
100 }
101 
103 {
104  uint8_t data_u8[4];
105  int8_t ret = VL53L1_ReadMulti(dev, index, data_u8, 4);
106  *data = (data_u8[0] << 24) |
107  (data_u8[1] << 16) |
108  (data_u8[2] << 8) |
109  (data_u8[3]);
110  return ret;
111 }
bool i2c_blocking_transceive(struct i2c_periph *p, struct i2c_transaction *t, uint8_t s_addr, uint8_t len_w, uint16_t len_r)
Submit a write/read transaction and wait for it to complete.
Definition: i2c.c:403
#define I2C_BUF_LEN
I2C buffer length.
Definition: i2c.h:84
bool i2c_blocking_transmit(struct i2c_periph *p, struct i2c_transaction *t, uint8_t s_addr, uint8_t len)
Submit a write only transaction and wait for it to complete.
Definition: i2c.c:359
static const struct usb_device_descriptor dev
Definition: usb_ser_hw.c:74
int8_t VL53L1_WrWord(VL53L1_DEV dev, uint16_t index, uint16_t data)
VL53L1_WrWord() definition.
int8_t VL53L1_WriteMulti(VL53L1_DEV dev, uint16_t index, uint8_t *pdata, uint32_t count)
VL53L1_WriteMulti() definition.
int8_t VL53L1_WrByte(VL53L1_DEV dev, uint16_t index, uint8_t data)
VL53L1_WrByte() definition.
int8_t VL53L1_RdWord(VL53L1_DEV dev, uint16_t index, uint16_t *data)
VL53L1_RdWord() definition.
int8_t VL53L1_WrDWord(VL53L1_DEV dev, uint16_t index, uint32_t data)
VL53L1_WrDWord() definition.
int8_t VL53L1_ReadMulti(VL53L1_DEV dev, uint16_t index, uint8_t *pdata, uint32_t count)
VL53L1_ReadMulti() definition.
int8_t VL53L1_RdDWord(VL53L1_DEV dev, uint16_t index, uint32_t *data)
VL53L1_RdDWord() definition.
int8_t VL53L1_RdByte(VL53L1_DEV dev, uint16_t index, uint8_t *data)
VL53L1_RdByte() definition.
Those platform functions are platform dependent and have to be implemented by the user.
unsigned short uint16_t
Typedef defining 16 bit unsigned short type.
Definition: vl53l1_types.h:88
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
Definition: vl53l1_types.h:78
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.
Definition: vl53l1_types.h:98
signed char int8_t
Typedef defining 8 bit char type.
Definition: vl53l1_types.h:103