Revision 4dba9195

View differences:

drivers/VL53L1X/v1/alld_VL53L1X.c
1
#include <alld_VL53L1X.h>
2
//#include "vl53l1_platform.h"
3
#include <vl53l1_api.h>
4
// #include "vl53l1_platform_log.h"
5

  
6

  
7
// #include "stm32xxx_hal.h"
8
#include <string.h>
9

  
10
apalExitStatus_t hello_world(void){
11
    return APAL_STATUS_OK;
12
}
13

  
14
/**
15
 * @brief Set the GPIO1 to a specific state.
16
 * @param[in]   ledp        The LED driver object.
17
 * @param[in]   led_state   The state to set the LED to.
18
 * @return                  An indicator whether the call was successful.
19
 */
20
apalExitStatus_t vl53l1x_GPIO1_set(const VL53L1XDriver* const vlx, const gpio_lld_state_t state)
21
{
22
  apalDbgAssert(vlx != NULL);
23
  
24
  return apalControlGpioSet(vlx->xshut, (state == GPIO_LLD_STATE_ON) ? APAL_GPIO_ON : APAL_GPIO_OFF);
25
}
26

  
27

  
28

  
29

  
30

  
31

  
32

  
33
// #define I2C_TIME_OUT_BASE   10
34
// #define I2C_TIME_OUT_BYTE   1
35

  
36
// #ifdef VL53L1_LOG_ENABLE
37
// #define trace_print(level, ...) VL53L1_trace_print_module_function(VL53L1_TRACE_MODULE_PLATFORM, level, VL53L1_TRACE_FUNCTION_NONE, ##__VA_ARGS__)
38
// #define trace_i2c(...) VL53L1_trace_print_module_function(VL53L1_TRACE_MODULE_NONE, VL53L1_TRACE_LEVEL_NONE, VL53L1_TRACE_FUNCTION_I2C, ##__VA_ARGS__)
39
// #endif
40

  
41
// #ifndef HAL_I2C_MODULE_ENABLED
42
// #warning "HAL I2C module must be enable "
43
// #endif
44

  
45
//extern I2C_HandleTypeDef hi2c1;
46
//#define VL53L0X_pI2cHandle    (&hi2c1)
47

  
48
/* when not customized by application define dummy one */
49
// #ifndef VL53L1_GetI2cBus
50
/** This macro can be overloaded by user to enforce i2c sharing in RTOS context
51
 */
52
// #   define VL53L1_GetI2cBus(...) (void)0
53
// #endif
54

  
55
// #ifndef VL53L1_PutI2cBus
56
/** This macro can be overloaded by user to enforce i2c sharing in RTOS context
57
 */
58
// #   define VL53L1_PutI2cBus(...) (void)0
59
// #endif
60

  
61
#define I2C_TIME_OUT_BASE   10
62
#define I2C_TIME_OUT_BYTE   1
63

  
64
uint8_t _I2CBuffer[256] = { 0 };
65
uint8_t rx_buf[256];
66
uint8_t tx_buf[256];
67

  
68
int _I2CWrite(VL53L1_DEV Dev, uint8_t *pdata, uint32_t count) {
69
    int status = 0;
70
    int i2c_time_out = 1000 * (I2C_TIME_OUT_BASE + count * I2C_TIME_OUT_BYTE);
71

  
72
    apalExitStatus_t status2 = apalI2CMasterTransmit(Dev->vl53l1x.i2cd, Dev->vl53l1x.addr, pdata, count, rx_buf, 0, 100);
73
    return status;
74
}
75

  
76
int _I2CRead(VL53L1_DEV Dev, uint8_t *pdata, uint32_t count) {
77
   int status = 0;
78
//   apalI2CMasterReceive(Dev->vl53l1x->i2cd, Dev->vl53l1x->addr, uint8_t* const rxbuf, const size_t rxbytes, const apalTime_t timeout)
79

  
80
   return status;
81
}
82

  
83
// TODO: Needs to be defined
84

  
85
VL53L1_Error VL53L1_WriteMulti(VL53L1_DEV Dev, uint16_t index, uint8_t *pdata, uint32_t count) {
86
    VL53L1_Error Status = VL53L1_ERROR_NONE;
87

  
88
    return Status;
89
}
90

  
91
// the ranging_sensor_comms.dll will take care of the page selection
92
VL53L1_Error VL53L1_ReadMulti(VL53L1_DEV Dev, uint16_t index, uint8_t *pdata, uint32_t count) {
93
    VL53L1_Error Status = VL53L1_ERROR_NONE;
94
    return Status;
95
}
96

  
97
VL53L1_Error VL53L1_WrByte(VL53L1_DEV Dev, uint16_t index, uint8_t data) {
98
    VL53L1_Error Status = VL53L1_ERROR_NONE;
99
    // printf("Starting to write byte: %X\n", data);
100
     int32_t status_int;
101

  
102
    _I2CBuffer[0] = index>>8;
103
    _I2CBuffer[1] = index&0xFF;
104
    _I2CBuffer[2] = data;
105

  
106
    _I2CWrite(Dev, _I2CBuffer, 3);
107

  
108
    return Status;
109
}
110

  
111
VL53L1_Error VL53L1_WrWord(VL53L1_DEV Dev, uint16_t index, uint16_t data) {
112
    VL53L1_Error Status = VL53L1_ERROR_NONE;
113
    return Status;
114
}
115

  
116
VL53L1_Error VL53L1_WrDWord(VL53L1_DEV Dev, uint16_t index, uint32_t data) {
117
    VL53L1_Error Status = VL53L1_ERROR_NONE;
118
    return Status;
119
}
120

  
121
VL53L1_Error VL53L1_UpdateByte(VL53L1_DEV Dev, uint16_t index, uint8_t AndData, uint8_t OrData) {
122
    VL53L1_Error Status = VL53L1_ERROR_NONE;
123
    return Status;
124
}
125

  
126
VL53L1_Error VL53L1_RdByte(VL53L1_DEV Dev, uint16_t index, uint8_t *data) {
127
    VL53L1_Error Status = VL53L1_ERROR_NONE;
128
    
129
    uint8_t indx[2] = { index >> 8, index & 0xFF };
130
    apalExitStatus_t status2 = apalI2CMasterTransmit(Dev->vl53l1x.i2cd, Dev->vl53l1x.addr, indx, 2, data, 1, 0);
131

  
132
    return Status;
133
}
134

  
135
VL53L1_Error VL53L1_RdWord(VL53L1_DEV Dev, uint16_t index, uint16_t *data) {
136
    VL53L1_Error Status = VL53L1_ERROR_NONE;
137
    return Status;
138
}
139

  
140
VL53L1_Error VL53L1_RdDWord(VL53L1_DEV Dev, uint16_t index, uint32_t *data) {
141
    VL53L1_Error Status = VL53L1_ERROR_NONE;
142
    return Status;
143
}
144

  
145
VL53L1_Error VL53L1_GetTickCount(
146
	uint32_t *ptick_count_ms)
147
{
148
	VL53L1_Error status  = VL53L1_ERROR_NONE;
149
	return status;
150
}
151

  
152
//#define trace_print(level, ...) \
153
//	_LOG_TRACE_PRINT(VL53L1_TRACE_MODULE_PLATFORM, \
154
//	level, VL53L1_TRACE_FUNCTION_NONE, ##__VA_ARGS__)
155

  
156
//#define trace_i2c(...) \
157
//	_LOG_TRACE_PRINT(VL53L1_TRACE_MODULE_NONE, \
158
//	VL53L1_TRACE_LEVEL_NONE, VL53L1_TRACE_FUNCTION_I2C, ##__VA_ARGS__)
159

  
160
VL53L1_Error VL53L1_GetTimerFrequency(int32_t *ptimer_freq_hz)
161
{
162
	VL53L1_Error status  = VL53L1_ERROR_NONE;
163
	return status;
164
}
165

  
166
VL53L1_Error VL53L1_WaitMs(VL53L1_Dev_t *pdev, int32_t wait_ms){
167
	VL53L1_Error status  = VL53L1_ERROR_NONE;
168
	return status;
169
}
170

  
171
VL53L1_Error VL53L1_WaitUs(VL53L1_Dev_t *pdev, int32_t wait_us){
172
	VL53L1_Error status  = VL53L1_ERROR_NONE;
173
	return status;
174
}
175

  
176
VL53L1_Error VL53L1_WaitValueMaskEx(
177
	VL53L1_Dev_t *pdev,
178
	uint32_t      timeout_ms,
179
	uint16_t      index,
180
	uint8_t       value,
181
	uint8_t       mask,
182
	uint32_t      poll_delay_ms)
183
{
184
	VL53L1_Error status  = VL53L1_ERROR_NONE;
185
	return status;
186
}
187

  
188

  
189

  
drivers/VL53L1X/v1/alld_VL53L1X.h
1
/**
2
 * @file    alld_VL53L1X.h
3
 * @brief   Touch sensor macros and structures.
4
 *
5
 * @addtogroup lld_touch
6
 * @{
7
 */
8

  
9
#ifndef AMIROLLD_VL53L1X_H
10
#define AMIROLLD_VL53L1X_H
11

  
12
#include <amiro-lld.h>
13
#include <alld_VL53L1X_types.h>
14
#include <vl53l1_api.h>
15
/******************************************************************************/
16
/* CONSTANTS                                                                  */
17
/******************************************************************************/
18
// 400kHz
19
#define VL53L1X_LLD_I2C_MAXFREQUENCY 400000
20

  
21
/* Default I2C address */
22
/* #define VL53L1X_LLD_I2C_ADDR_DEFAULT      0x52 */
23
#define VL53L1X_LLD_I2C_ADDR_DEFAULT           0x29
24

  
25
#define VL53L1X_LLD_BOOTDURATION     1200
26

  
27
/******************************************************************************/
28
/* SETTINGS                                                                   */
29
/******************************************************************************/
30

  
31
/******************************************************************************/
32
/* CHECKS                                                                     */
33
/******************************************************************************/
34

  
35
/******************************************************************************/
36
/* DATA STRUCTURES AND TYPES                                                  */
37
/******************************************************************************/
38

  
39
/**
40
 * @brief The state of the GPIOs.
41
 */
42
typedef enum {
43
  GPIO_LLD_STATE_OFF = 0x00,   /**< 'on' state of the LED */
44
  GPIO_LLD_STATE_ON  = 0x01,   /**< 'off' state of the LED */
45
} gpio_lld_state_t;
46

  
47
/******************************************************************************/
48
/* MACROS                                                                     */
49
/******************************************************************************/
50

  
51
/******************************************************************************/
52
/* EXTERN DECLARATIONS                                                        */
53
/******************************************************************************/
54

  
55
#ifdef __cplusplus
56
extern "C" {
57
#endif
58
  apalExitStatus_t hello_world(void);
59
  apalExitStatus_t vl53l1x_GPIO1_set(const VL53L1XDriver* const vlx, const gpio_lld_state_t state);
60

  
61
#ifdef __cplusplus
62
}
63
#endif
64

  
65

  
66
/******************************************************************************/
67
/* INLINE FUNCTIONS                                                           */
68
/******************************************************************************/
69

  
70

  
71

  
72
#endif /* AMIROLLD_VL53L1X_H */
drivers/VL53L1X/v1/alld_VL53L1X.mk
1
################################################################################
2
# AMiRo-LLD is a compilation of low-level hardware drivers for the Autonomous  #
3
# Mini Robot (AMiRo) platform.                                                 #
4
# Copyright (C) 2016..2019  Thomas Schöpping et al.                            #
5
#                                                                              #
6
# This program is free software: you can redistribute it and/or modify         #
7
# it under the terms of the GNU General Public License as published by         #
8
# the Free Software Foundation, either version 3 of the License, or            #
9
# (at your option) any later version.                                          #
10
#                                                                              #
11
# This program is distributed in the hope that it will be useful,              #
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of               #
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                #
14
# GNU General Public License for more details.                                 #
15
#                                                                              #
16
# You should have received a copy of the GNU General Public License            #
17
# along with this program.  If not, see <http://www.gnu.org/licenses/>.        #
18
#                                                                              #
19
# This research/work was supported by the Cluster of Excellence Cognitive      #
20
# Interaction Technology 'CITEC' (EXC 277) at Bielefeld University, which is   #
21
# funded by the German Research Foundation (DFG).                              #
22
################################################################################
23

  
24

  
25

  
26
# path to this directory
27
AMIROLLD_ST_VL53L1X := $(dir $(lastword $(MAKEFILE_LIST)))
28

  
29
# include paths
30
AMIROLLD_INC += $(AMIROLLD_ST_VL53L1X) \
31
				$(AMIROLLD_ST_VL53L1X)/api/core \
32
				$(AMIROLLD_ST_VL53L1X)/api/platform \
33

  
34
# $(info    AMIROLLD_INC is $(AMIROLLD_INC))
35

  
36
# C source files
37
AMIROLLD_CSRC += $(AMIROLLD_ST_VL53L1X)alld_VL53L1X.c \
38
				 $(AMIROLLD_ST_VL53L1X)api/core/vl53l1_api_calibration.c \
39
				 $(AMIROLLD_ST_VL53L1X)api/core/vl53l1_api_core.c \
40
				 $(AMIROLLD_ST_VL53L1X)api/core/vl53l1_api_debug.c \
41
				 $(AMIROLLD_ST_VL53L1X)api/core/vl53l1_api_preset_modes.c \
42
				 $(AMIROLLD_ST_VL53L1X)api/core/vl53l1_api_strings.c \
43
				 $(AMIROLLD_ST_VL53L1X)api/core/vl53l1_api.c \
44
				 $(AMIROLLD_ST_VL53L1X)api/core/vl53l1_core_support.c \
45
				 $(AMIROLLD_ST_VL53L1X)api/core/vl53l1_core.c \
46
				 $(AMIROLLD_ST_VL53L1X)api/core/vl53l1_error_strings.c \
47
				 $(AMIROLLD_ST_VL53L1X)api/core/vl53l1_register_funcs.c \
48
				 $(AMIROLLD_ST_VL53L1X)api/core/vl53l1_silicon_core.c \
49
				 $(AMIROLLD_ST_VL53L1X)api/core/vl53l1_wait.c \
50

  
51
# $(info    AMIROLLD_CSRC is $(AMIROLLD_CSRC))
drivers/VL53L1X/v1/alld_VL53L1X_types.h
1
#ifndef AMIROLLD_VL53L1X_TYPES_H
2
#define AMIROLLD_VL53L1X_TYPES_H
3
#include <amiro-lld.h>
4

  
5
/**
6
 * @brief The VL53L1X driver struct
7
 */
8
typedef struct {
9
  apalI2CDriver_t* i2cd;
10
  apalI2Caddr_t addr;       /**< @brief The I2C Driver.     */
11
  const apalControlGpio_t* gpio1;
12
  const apalControlGpio_t* xshut;
13
} VL53L1XDriver;
14

  
15
#endif
drivers/VL53L1X/v1/api/core/vl53l1_api.c
1

  
2
/*
3
* Copyright (c) 2017, STMicroelectronics - All Rights Reserved
4
*
5
* This file is part of VL53L1 Core and is dual licensed,
6
* either 'STMicroelectronics
7
* Proprietary license'
8
* or 'BSD 3-clause "New" or "Revised" License' , at your option.
9
*
10
********************************************************************************
11
*
12
* 'STMicroelectronics Proprietary license'
13
*
14
********************************************************************************
15
*
16
* License terms: STMicroelectronics Proprietary in accordance with licensing
17
* terms at www.st.com/sla0081
18
*
19
* STMicroelectronics confidential
20
* Reproduction and Communication of this document is strictly prohibited unless
21
* specifically authorized in writing by STMicroelectronics.
22
*
23
*
24
********************************************************************************
25
*
26
* Alternatively, VL53L1 Core may be distributed under the terms of
27
* 'BSD 3-clause "New" or "Revised" License', in which case the following
28
* provisions apply instead of the ones mentioned above :
29
*
30
********************************************************************************
31
*
32
* License terms: BSD 3-clause "New" or "Revised" License.
33
*
34
* Redistribution and use in source and binary forms, with or without
35
* modification, are permitted provided that the following conditions are met:
36
*
37
* 1. Redistributions of source code must retain the above copyright notice, this
38
* list of conditions and the following disclaimer.
39
*
40
* 2. Redistributions in binary form must reproduce the above copyright notice,
41
* this list of conditions and the following disclaimer in the documentation
42
* and/or other materials provided with the distribution.
43
*
44
* 3. Neither the name of the copyright holder nor the names of its contributors
45
* may be used to endorse or promote products derived from this software
46
* without specific prior written permission.
47
*
48
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
49
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
51
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
52
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
54
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
55
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
56
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
57
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
58
*
59
*
60
********************************************************************************
61
*
62
*/
63
#include "vl53l1_api.h"
64
#include "vl53l1_api_strings.h"
65
#include "vl53l1_register_settings.h"
66
#include "vl53l1_register_funcs.h"
67
#include "vl53l1_core.h"
68
#include "vl53l1_api_calibration.h"
69
#include "vl53l1_wait.h"
70
#include "vl53l1_preset_setup.h"
71
#include "vl53l1_api_debug.h"
72
#include "vl53l1_api_core.h"
73

  
74
/* Check for minimum user zone requested by Xtalk calibration */
75
/* no need for VL53L1_MAX_USER_ZONES check, set 5 to pass the test */
76
#define ZONE_CHECK 5
77

  
78
#if ZONE_CHECK < 5
79
#error Must define at least 5 zones in MAX_USER_ZONES constant
80
#endif
81

  
82

  
83
#ifdef VL53L1_NOCALIB
84
#define OFFSET_CALIB_EMPTY
85
#endif
86

  
87
#ifndef VL53L1_NOCALIB
88
#define OFFSET_CALIB
89
#endif
90

  
91
#define LOG_FUNCTION_START(fmt, ...) \
92
	_LOG_FUNCTION_START(VL53L1_TRACE_MODULE_API, fmt, ##__VA_ARGS__)
93
#define LOG_FUNCTION_END(status, ...) \
94
	_LOG_FUNCTION_END(VL53L1_TRACE_MODULE_API, status, ##__VA_ARGS__)
95
#define LOG_FUNCTION_END_FMT(status, fmt, ...) \
96
	_LOG_FUNCTION_END_FMT(VL53L1_TRACE_MODULE_API, status, \
97
			fmt, ##__VA_ARGS__)
98

  
99
#ifdef VL53L1_LOG_ENABLE
100
#define trace_print(level, ...) trace_print_module_function(\
101
		VL53L1_TRACE_MODULE_API, level, VL53L1_TRACE_FUNCTION_NONE, \
102
		##__VA_ARGS__)
103
#endif
104

  
105
#ifndef MIN
106
#define MIN(v1, v2) ((v1) < (v2) ? (v1) : (v2))
107
#endif
108
#ifndef MAX
109
#define MAX(v1, v2) ((v1) < (v2) ? (v2) : (v1))
110
#endif
111

  
112
#define DMAX_REFLECTANCE_IDX 2
113
/* Use Dmax index 2 because it's the 50% reflectance case by default */
114

  
115
/* Following LOWPOWER_AUTO figures have been measured observing vcsel
116
 * emissions on an actual device
117
 */
118
#define LOWPOWER_AUTO_VHV_LOOP_DURATION_US 245
119
#define LOWPOWER_AUTO_OVERHEAD_BEFORE_A_RANGING 1448
120
#define LOWPOWER_AUTO_OVERHEAD_BETWEEN_A_B_RANGING 2100
121

  
122
#define FDA_MAX_TIMING_BUDGET_US 550000
123
/* Maximum timing budget allowed codex #456189*/
124

  
125

  
126
/* local static utilities functions */
127

  
128
/* Bare Driver Tuning parameter table indexed with VL53L1_Tuning_t */
129
static int32_t BDTable[VL53L1_TUNING_MAX_TUNABLE_KEY] = {
130
		TUNING_VERSION,
131
		TUNING_PROXY_MIN,
132
		TUNING_SINGLE_TARGET_XTALK_TARGET_DISTANCE_MM,
133
		TUNING_SINGLE_TARGET_XTALK_SAMPLE_NUMBER,
134
		TUNING_MIN_AMBIENT_DMAX_VALID,
135
		TUNING_MAX_SIMPLE_OFFSET_CALIBRATION_SAMPLE_NUMBER,
136
		TUNING_XTALK_FULL_ROI_TARGET_DISTANCE_MM,
137
		TUNING_SIMPLE_OFFSET_CALIBRATION_REPEAT
138
};
139

  
140

  
141
#define VL53L1_NVM_POWER_UP_DELAY_US             50
142
#define VL53L1_NVM_READ_TRIGGER_DELAY_US          5
143

  
144
static VL53L1_Error VL53L1_nvm_enable(
145
	VL53L1_DEV      Dev,
146
	uint16_t        nvm_ctrl_pulse_width,
147
	int32_t         nvm_power_up_delay_us)
148
{
149
	/*
150
	 * Sequence below enables NVM for reading
151
	 *
152
	 *  - Enable power force
153
	 *  - Disable firmware
154
	 *  - Power up NVM
155
	 *  - Wait for 50us while the NVM powers up
156
	 *  - Configure for reading and set the pulse width (16-bit)
157
	 */
158

  
159
	VL53L1_Error status = VL53L1_ERROR_NONE;
160

  
161
	LOG_FUNCTION_START("");
162

  
163

  
164
	/* Disable Firmware */
165

  
166
	if (status == VL53L1_ERROR_NONE) /*lint !e774 always true*/
167
		status = VL53L1_disable_firmware(Dev);
168

  
169

  
170
	/* Enable Power Force  */
171

  
172
	if (status == VL53L1_ERROR_NONE)
173
		status = VL53L1_enable_powerforce(Dev);
174

  
175
	/* Wait the required time for the regulators, bandgap,
176
	 * oscillator to wake up and settle
177
	 */
178

  
179
	if (status == VL53L1_ERROR_NONE)
180
		status = VL53L1_WaitUs(
181
					Dev,
182
					VL53L1_ENABLE_POWERFORCE_SETTLING_TIME_US);
183

  
184
	/*  Power up NVM */
185

  
186
	if (status == VL53L1_ERROR_NONE)
187
		status = VL53L1_WrByte(
188
					Dev,
189
					VL53L1_RANGING_CORE__NVM_CTRL__PDN,
190
					0x01);
191

  
192
	/* Enable NVM Clock */
193

  
194
	if (status == VL53L1_ERROR_NONE)
195
		status = VL53L1_WrByte(
196
					Dev,
197
					VL53L1_RANGING_CORE__CLK_CTRL1,
198
					0x05);
199

  
200
	/* Wait the required time for NVM to power up*/
201

  
202
	if (status == VL53L1_ERROR_NONE)
203
		status = VL53L1_WaitUs(
204
					Dev,
205
					nvm_power_up_delay_us);
206

  
207
	/* Select read mode and set control pulse width */
208

  
209
	if (status == VL53L1_ERROR_NONE)
210
		status = VL53L1_WrByte(
211
					Dev,
212
					VL53L1_RANGING_CORE__NVM_CTRL__MODE,
213
					0x01);
214

  
215
	if (status == VL53L1_ERROR_NONE)
216
		status = VL53L1_WrWord(
217
					Dev,
218
					VL53L1_RANGING_CORE__NVM_CTRL__PULSE_WIDTH_MSB,
219
					nvm_ctrl_pulse_width);
220

  
221
	LOG_FUNCTION_END(status);
222

  
223
	return status;
224

  
225
}
226

  
227

  
228
static VL53L1_Error VL53L1_nvm_read(
229
	VL53L1_DEV    Dev,
230
	uint8_t       start_address,
231
	uint8_t       count,
232
	uint8_t      *pdata)
233
{
234
	/*
235
	 * Sequence per 32-bit NVM read access:
236
	 *
237
	 * - Set up the 5-bit (0-127) NVM Address
238
	 * - Trigger the read of the NVM data by toggling NVM_CTRL__READN
239
	 * - Read the NVM data - 4 bytes wide read/write interface
240
	 * - Increment data byte pointer by 4 ready for the next loop
241
	 */
242

  
243
	VL53L1_Error status   = VL53L1_ERROR_NONE;
244
	uint8_t      nvm_addr = 0;
245

  
246
	LOG_FUNCTION_START("");
247

  
248
	for (nvm_addr = start_address; nvm_addr < (start_address+count) ; nvm_addr++) {
249

  
250
		/* Step 1 : set address */
251

  
252
		if (status == VL53L1_ERROR_NONE)
253
			status = VL53L1_WrByte(
254
						Dev,
255
						VL53L1_RANGING_CORE__NVM_CTRL__ADDR,
256
						nvm_addr);
257

  
258
		/* Step 2 : trigger reading of data */
259

  
260
		if (status == VL53L1_ERROR_NONE)
261
			status = VL53L1_WrByte(
262
						Dev,
263
						VL53L1_RANGING_CORE__NVM_CTRL__READN,
264
						0x00);
265

  
266
		/* Step 3 : wait the required time */
267

  
268
		if (status == VL53L1_ERROR_NONE)
269
			status = VL53L1_WaitUs(
270
						Dev,
271
						VL53L1_NVM_READ_TRIGGER_DELAY_US);
272

  
273
		if (status == VL53L1_ERROR_NONE)
274
			status = VL53L1_WrByte(
275
						Dev,
276
						VL53L1_RANGING_CORE__NVM_CTRL__READN,
277
						0x01);
278

  
279
		/* Step 3 : read 4-byte wide data register */
280
		if (status == VL53L1_ERROR_NONE)
281
			status = VL53L1_ReadMulti(
282
						Dev,
283
						VL53L1_RANGING_CORE__NVM_CTRL__DATAOUT_MMM,
284
						pdata,
285
						4);
286

  
287
		/* Step 4 : increment byte buffer pointer */
288

  
289
		pdata = pdata + 4;
290

  
291

  
292
	}
293

  
294
	LOG_FUNCTION_END(status);
295

  
296
	return status;
297
}
298

  
299
static VL53L1_Error VL53L1_nvm_disable(
300
	VL53L1_DEV    Dev)
301
{
302
	/*
303
	 * Power down NVM (OTP) to extend lifetime
304
	 */
305

  
306
	VL53L1_Error status = VL53L1_ERROR_NONE;
307

  
308
	LOG_FUNCTION_START("");
309

  
310
	if (status == VL53L1_ERROR_NONE) /*lint !e774 always true*/
311
		status = VL53L1_WrByte(
312
					Dev,
313
					VL53L1_RANGING_CORE__NVM_CTRL__READN,
314
					0x01);
315

  
316
	/* Power down NVM */
317

  
318
	if (status == VL53L1_ERROR_NONE)
319
		status = VL53L1_WrByte(
320
					Dev,
321
					VL53L1_RANGING_CORE__NVM_CTRL__PDN,
322
					0x00);
323

  
324
	/* Keep power force enabled */
325

  
326
	if (status == VL53L1_ERROR_NONE)
327
		status = VL53L1_disable_powerforce(Dev);
328

  
329
	/* (Re)Enable Firmware */
330

  
331
	if (status == VL53L1_ERROR_NONE)
332
		status = VL53L1_enable_firmware(Dev);
333

  
334
	LOG_FUNCTION_END(status);
335

  
336
	return status;
337

  
338
}
339

  
340
static VL53L1_Error VL53L1_read_nvm_raw_data(
341
	VL53L1_DEV     Dev,
342
	uint8_t        start_address,
343
	uint8_t        count,
344
	uint8_t       *pnvm_raw_data)
345
{
346

  
347
	/*
348
	 * Reads ALL 512 bytes of NVM data
349
	 */
350

  
351
	VL53L1_Error status = VL53L1_ERROR_NONE;
352

  
353
	LOG_FUNCTION_START("");
354

  
355
	/*
356
	 *   Enable NVM and set control pulse width
357
	 */
358

  
359
	if (status == VL53L1_ERROR_NONE) /*lint !e774 always true*/
360
		status = VL53L1_nvm_enable(
361
					Dev,
362
					0x0004,
363
					VL53L1_NVM_POWER_UP_DELAY_US);
364

  
365
	/*
366
	 *  Read the raw NVM data
367
	 *        - currently all of 128 * 4 bytes = 512 bytes are read
368
	 */
369

  
370
	if (status == VL53L1_ERROR_NONE)
371
		status = VL53L1_nvm_read(
372
			Dev,
373
			start_address,
374
			count,
375
			pnvm_raw_data);
376

  
377
	/*
378
	 *   Disable NVM
379
	 */
380

  
381
	if (status == VL53L1_ERROR_NONE)
382
		status = VL53L1_nvm_disable(Dev);
383

  
384
	LOG_FUNCTION_END(status);
385

  
386
	return status;
387

  
388
}
389

  
390
static VL53L1_Error SingleTargetXTalkCalibration(VL53L1_DEV Dev)
391
{
392
	VL53L1_Error Status = VL53L1_ERROR_NONE;
393

  
394
	uint32_t sum_ranging = 0;
395
	uint32_t sum_spads = 0;
396
	FixPoint1616_t sum_signalRate = 0;
397
	FixPoint1616_t total_count = 0;
398
	uint8_t xtalk_meas = 0;
399
	uint8_t xtalk_measmax =
400
		BDTable[VL53L1_TUNING_SINGLE_TARGET_XTALK_SAMPLE_NUMBER];
401
	VL53L1_RangingMeasurementData_t RMData;
402
	FixPoint1616_t xTalkStoredMeanSignalRate;
403
	FixPoint1616_t xTalkStoredMeanRange;
404
	FixPoint1616_t xTalkStoredMeanRtnSpads;
405
	uint32_t xTalkStoredMeanRtnSpadsAsInt;
406
	uint32_t xTalkCalDistanceAsInt;
407
	FixPoint1616_t XTalkCompensationRateMegaCps;
408
	uint32_t signalXTalkTotalPerSpad;
409
	VL53L1_PresetModes PresetMode;
410
	VL53L1_CalibrationData_t  CalibrationData;
411
	VL53L1_CustomerNvmManaged_t *pC;
412

  
413

  
414
	LOG_FUNCTION_START("");
415

  
416
	/* check if the following are selected
417
	 * VL53L1_PRESETMODE_AUTONOMOUS,
418
	 * VL53L1_PRESETMODE_LOWPOWER_AUTONOMOUS
419
	 * VL53L1_PRESETMODE_LITE_RANGING
420
	 */
421
	PresetMode = VL53L1DevDataGet(Dev, CurrentParameters.PresetMode);
422

  
423
	if ((PresetMode != VL53L1_PRESETMODE_AUTONOMOUS) &&
424
		(PresetMode != VL53L1_PRESETMODE_LOWPOWER_AUTONOMOUS) &&
425
		(PresetMode != VL53L1_PRESETMODE_LITE_RANGING)) {
426
		Status = VL53L1_ERROR_MODE_NOT_SUPPORTED;
427
		goto ENDFUNC;
428
	}
429

  
430
	/* disable crosstalk calibration */
431
	Status = VL53L1_disable_xtalk_compensation(Dev);
432
	CHECK_ERROR_GO_ENDFUNC;
433

  
434
	Status = VL53L1_StartMeasurement(Dev);
435
	CHECK_ERROR_GO_ENDFUNC;
436

  
437
	sum_ranging = 0;
438
	sum_spads = 0;
439
	sum_signalRate = 0;
440
	total_count = 0;
441
	for (xtalk_meas = 0; xtalk_meas < xtalk_measmax; xtalk_meas++) {
442
		VL53L1_WaitMeasurementDataReady(Dev);
443
		VL53L1_GetRangingMeasurementData(Dev, &RMData);
444
		VL53L1_ClearInterruptAndStartMeasurement(Dev);
445
		if (RMData.RangeStatus == VL53L1_RANGESTATUS_RANGE_VALID) {
446
			sum_ranging += RMData.RangeMilliMeter;
447
			sum_signalRate += RMData.SignalRateRtnMegaCps;
448
			sum_spads += RMData.EffectiveSpadRtnCount / 256;
449
			total_count++;
450
		}
451
	}
452
	Status = VL53L1_StopMeasurement(Dev);
453

  
454
	if (total_count > 0) {
455
		/* FixPoint1616_t / uint16_t = FixPoint1616_t */
456
		xTalkStoredMeanSignalRate = sum_signalRate / total_count;
457
		xTalkStoredMeanRange = (FixPoint1616_t)(sum_ranging << 16);
458
		xTalkStoredMeanRange /= total_count;
459
		xTalkStoredMeanRtnSpads = (FixPoint1616_t)(sum_spads << 16);
460
		xTalkStoredMeanRtnSpads /= total_count;
461

  
462
		/* Round Mean Spads to Whole Number.
463
		 * Typically the calculated mean SPAD count is a whole number
464
		 * or very close to a whole
465
		 * number, therefore any truncation will not result in a
466
		 * significant loss in accuracy.
467
		 * Also, for a grey target at a typical distance of around
468
		 * 400mm, around 220 SPADs will
469
		 * be enabled, therefore, any truncation will result in a loss
470
		 * of accuracy of less than
471
		 * 0.5%.
472
		 */
473
		xTalkStoredMeanRtnSpadsAsInt = (xTalkStoredMeanRtnSpads +
474
			0x8000) >> 16;
475

  
476
		/* Round Cal Distance to Whole Number.
477
		 * Note that the cal distance is in mm, therefore no resolution
478
		 * is lost.
479
		 */
480
		 xTalkCalDistanceAsInt = ((uint32_t)BDTable[
481
			VL53L1_TUNING_SINGLE_TARGET_XTALK_TARGET_DISTANCE_MM]);
482
		if (xTalkStoredMeanRtnSpadsAsInt == 0 ||
483
		xTalkCalDistanceAsInt == 0 ||
484
		xTalkStoredMeanRange >= (xTalkCalDistanceAsInt << 16)) {
485
			XTalkCompensationRateMegaCps = 0;
486
		} else {
487
			/* Apply division by mean spad count early in the
488
			 * calculation to keep the numbers small.
489
			 * This ensures we can maintain a 32bit calculation.
490
			 * Fixed1616 / int := Fixed1616
491
			 */
492
			signalXTalkTotalPerSpad = (xTalkStoredMeanSignalRate) /
493
				xTalkStoredMeanRtnSpadsAsInt;
494

  
495
			/* Complete the calculation for total Signal XTalk per
496
			 * SPAD
497
			 * Fixed1616 * (Fixed1616 - Fixed1616/int) :=
498
			 * (2^16 * Fixed1616)
499
			 */
500
			signalXTalkTotalPerSpad *= (((uint32_t)1 << 16) -
501
				(xTalkStoredMeanRange / xTalkCalDistanceAsInt));
502

  
503
			/* Round from 2^16 * Fixed1616, to Fixed1616. */
504
			XTalkCompensationRateMegaCps = (signalXTalkTotalPerSpad
505
				+ 0x8000) >> 16;
506
		}
507

  
508

  
509
		Status = VL53L1_GetCalibrationData(Dev, &CalibrationData);
510
		CHECK_ERROR_GO_ENDFUNC;
511

  
512
		pC = &CalibrationData.customer;
513

  
514
		pC->algo__crosstalk_compensation_plane_offset_kcps =
515
			(uint32_t)(1000 * ((XTalkCompensationRateMegaCps  +
516
				((uint32_t)1<<6)) >> (16-9)));
517

  
518
		Status = VL53L1_SetCalibrationData(Dev, &CalibrationData);
519
		CHECK_ERROR_GO_ENDFUNC;
520

  
521
		Status = VL53L1_enable_xtalk_compensation(Dev);
522

  
523
	} else
524
		/* return error because no valid data found */
525
		Status = VL53L1_ERROR_XTALK_EXTRACTION_NO_SAMPLE_FAIL;
526

  
527
ENDFUNC:
528
	LOG_FUNCTION_END(Status);
529
	return Status;
530

  
531
}
532

  
533
/* Check Rectangle in user's coordinate system:
534
 *	15	TL(x,y) o-----*
535
 *   ^			|     |
536
 *   |			*-----o BR(x,y)
537
 *   0------------------------- >15
538
 *   check Rectangle definition conforms to the (0,15,15) coordinate system
539
 *   with a minimum of 4x4 size
540
 */
541
static VL53L1_Error CheckValidRectRoi(VL53L1_UserRoi_t ROI)
542
{
543
	VL53L1_Error Status = VL53L1_ERROR_NONE;
544

  
545
	LOG_FUNCTION_START("");
546

  
547
	/* Negative check are not necessary because value is unsigned */
548
	if ((ROI.TopLeftX > 15) || (ROI.TopLeftY > 15) ||
549
		(ROI.BotRightX > 15) || (ROI.BotRightY > 15))
550
		Status = VL53L1_ERROR_INVALID_PARAMS;
551

  
552
	if ((ROI.TopLeftX > ROI.BotRightX) || (ROI.TopLeftY < ROI.BotRightY))
553
		Status = VL53L1_ERROR_INVALID_PARAMS;
554

  
555
	LOG_FUNCTION_END(Status);
556
	return Status;
557
}
558

  
559
static VL53L1_GPIO_Interrupt_Mode ConvertModeToLLD(VL53L1_Error *pStatus,
560
		VL53L1_ThresholdMode CrossMode)
561
{
562
	VL53L1_GPIO_Interrupt_Mode Mode;
563

  
564
	switch (CrossMode) {
565
	case VL53L1_THRESHOLD_CROSSED_LOW:
566
		Mode = VL53L1_GPIOINTMODE_LEVEL_LOW;
567
		break;
568
	case VL53L1_THRESHOLD_CROSSED_HIGH:
569
		Mode = VL53L1_GPIOINTMODE_LEVEL_HIGH;
570
		break;
571
	case VL53L1_THRESHOLD_OUT_OF_WINDOW:
572
		Mode = VL53L1_GPIOINTMODE_OUT_OF_WINDOW;
573
		break;
574
	case VL53L1_THRESHOLD_IN_WINDOW:
575
		Mode = VL53L1_GPIOINTMODE_IN_WINDOW;
576
		break;
577
	default:
578
		/* define Mode to avoid warning but actual value doesn't mind */
579
		Mode = VL53L1_GPIOINTMODE_LEVEL_HIGH;
580
		*pStatus = VL53L1_ERROR_INVALID_PARAMS;
581
	}
582
	return Mode;
583
}
584

  
585
static VL53L1_ThresholdMode ConvertModeFromLLD(VL53L1_Error *pStatus,
586
		VL53L1_GPIO_Interrupt_Mode CrossMode)
587
{
588
	VL53L1_ThresholdMode Mode;
589

  
590
	switch (CrossMode) {
591
	case VL53L1_GPIOINTMODE_LEVEL_LOW:
592
		Mode = VL53L1_THRESHOLD_CROSSED_LOW;
593
		break;
594
	case VL53L1_GPIOINTMODE_LEVEL_HIGH:
595
		Mode = VL53L1_THRESHOLD_CROSSED_HIGH;
596
		break;
597
	case VL53L1_GPIOINTMODE_OUT_OF_WINDOW:
598
		Mode = VL53L1_THRESHOLD_OUT_OF_WINDOW;
599
		break;
600
	case VL53L1_GPIOINTMODE_IN_WINDOW:
601
		Mode = VL53L1_THRESHOLD_IN_WINDOW;
602
		break;
603
	default:
604
		/* define Mode to avoid warning but actual value doesn't mind */
605
		Mode = VL53L1_THRESHOLD_CROSSED_HIGH;
606
		*pStatus = VL53L1_ERROR_UNDEFINED;
607
	}
608
	return Mode;
609
}
610

  
611
/* Group PAL General Functions */
612

  
613
VL53L1_Error VL53L1_GetVersion(VL53L1_Version_t *pVersion)
614
{
615
	VL53L1_Error Status = VL53L1_ERROR_NONE;
616

  
617
	LOG_FUNCTION_START("");
618

  
619
	pVersion->major = VL53L1_IMPLEMENTATION_VER_MAJOR;
620
	pVersion->minor = VL53L1_IMPLEMENTATION_VER_MINOR;
621
	pVersion->build = VL53L1_IMPLEMENTATION_VER_SUB;
622

  
623
	pVersion->revision = VL53L1_IMPLEMENTATION_VER_REVISION;
624

  
625
	LOG_FUNCTION_END(Status);
626
	return Status;
627
}
628

  
629
VL53L1_Error VL53L1_GetProductRevision(VL53L1_DEV Dev,
630
	uint8_t *pProductRevisionMajor, uint8_t *pProductRevisionMinor)
631
{
632
	VL53L1_Error Status = VL53L1_ERROR_NONE;
633
	uint8_t revision_id;
634
	VL53L1_LLDriverData_t   *pLLData;
635

  
636
	LOG_FUNCTION_START("");
637

  
638
	pLLData =  VL53L1DevStructGetLLDriverHandle(Dev);
639
	revision_id = pLLData->nvm_copy_data.identification__revision_id;
640
	*pProductRevisionMajor = 1;
641
	*pProductRevisionMinor = (revision_id & 0xF0) >> 4;
642

  
643
	LOG_FUNCTION_END(Status);
644
	return Status;
645

  
646
}
647

  
648
VL53L1_Error VL53L1_GetDeviceInfo(VL53L1_DEV Dev,
649
	VL53L1_DeviceInfo_t *pVL53L1_DeviceInfo)
650
{
651
	VL53L1_Error Status = VL53L1_ERROR_NONE;
652
	uint8_t revision_id;
653
	VL53L1_LLDriverData_t   *pLLData;
654

  
655
	LOG_FUNCTION_START("");
656

  
657
	pLLData =  VL53L1DevStructGetLLDriverHandle(Dev);
658

  
659
	strncpy(pVL53L1_DeviceInfo->ProductId, "",
660
			VL53L1_DEVINFO_STRLEN-1);
661
	pVL53L1_DeviceInfo->ProductType =
662
			pLLData->nvm_copy_data.identification__module_type;
663

  
664
	revision_id = pLLData->nvm_copy_data.identification__revision_id;
665
	pVL53L1_DeviceInfo->ProductRevisionMajor = 1;
666
	pVL53L1_DeviceInfo->ProductRevisionMinor = (revision_id & 0xF0) >> 4;
667

  
668
#ifndef VL53L1_USE_EMPTY_STRING
669
	if (pVL53L1_DeviceInfo->ProductRevisionMinor == 0)
670
		strncpy(pVL53L1_DeviceInfo->Name,
671
				VL53L1_STRING_DEVICE_INFO_NAME0,
672
				VL53L1_DEVINFO_STRLEN-1);
673
	else
674
		strncpy(pVL53L1_DeviceInfo->Name,
675
				VL53L1_STRING_DEVICE_INFO_NAME1,
676
				VL53L1_DEVINFO_STRLEN-1);
677
	strncpy(pVL53L1_DeviceInfo->Type,
678
			VL53L1_STRING_DEVICE_INFO_TYPE,
679
			VL53L1_DEVINFO_STRLEN-1);
680
#else
681
	pVL53L1_DeviceInfo->Name[0] = 0;
682
	pVL53L1_DeviceInfo->Type[0] = 0;
683
#endif
684

  
685
	LOG_FUNCTION_END(Status);
686
	return Status;
687
}
688

  
689

  
690
VL53L1_Error VL53L1_GetRangeStatusString(uint8_t RangeStatus,
691
	char *pRangeStatusString)
692
{
693
	VL53L1_Error Status = VL53L1_ERROR_NONE;
694

  
695
	LOG_FUNCTION_START("");
696

  
697
	Status = VL53L1_get_range_status_string(RangeStatus,
698
		pRangeStatusString);
699

  
700
	LOG_FUNCTION_END(Status);
701
	return Status;
702
}
703

  
704
VL53L1_Error VL53L1_GetPalErrorString(VL53L1_Error PalErrorCode,
705
	char *pPalErrorString)
706
{
707
	VL53L1_Error Status = VL53L1_ERROR_NONE;
708

  
709
	LOG_FUNCTION_START("");
710

  
711
	Status = VL53L1_get_pal_error_string(PalErrorCode, pPalErrorString);
712

  
713
	LOG_FUNCTION_END(Status);
714
	return Status;
715
}
716

  
717
VL53L1_Error VL53L1_GetPalStateString(VL53L1_State PalStateCode,
718
	char *pPalStateString)
719
{
720
	VL53L1_Error Status = VL53L1_ERROR_NONE;
721

  
722
	LOG_FUNCTION_START("");
723

  
724
	Status = VL53L1_get_pal_state_string(PalStateCode, pPalStateString);
725

  
726
	LOG_FUNCTION_END(Status);
727
	return Status;
728
}
729

  
730
VL53L1_Error VL53L1_GetPalState(VL53L1_DEV Dev, VL53L1_State *pPalState)
731
{
732
	VL53L1_Error Status = VL53L1_ERROR_NONE;
733

  
734
	LOG_FUNCTION_START("");
735

  
736
	*pPalState = VL53L1DevDataGet(Dev, PalState);
737

  
738
	LOG_FUNCTION_END(Status);
739
	return Status;
740
}
741

  
742
/* End Group PAL General Functions */
743

  
744
/* Group PAL Init Functions */
745
VL53L1_Error VL53L1_SetDeviceAddress(VL53L1_DEV Dev, uint8_t DeviceAddress)
746
{
747
	VL53L1_Error Status = VL53L1_ERROR_NONE;
748

  
749
	LOG_FUNCTION_START("");
750

  
751
	Status = VL53L1_WrByte(Dev, VL53L1_I2C_SLAVE__DEVICE_ADDRESS,
752
			DeviceAddress / 2);
753

  
754
	LOG_FUNCTION_END(Status);
755
	return Status;
756
}
757

  
758
VL53L1_Error VL53L1_DataInit(VL53L1_DEV Dev)
759
{
760
	VL53L1_Error Status = VL53L1_ERROR_NONE;
761
	uint8_t i;
762

  
763
	LOG_FUNCTION_START("");
764

  
765
	/* 2V8 power mode selection codex 447463 */
766
#ifdef USE_I2C_2V8
767
	Status = VL53L1_RdByte(Dev, VL53L1_PAD_I2C_HV__EXTSUP_CONFIG, &i);
768
	if (Status == VL53L1_ERROR_NONE) {
769
		i = (i & 0xfe) | 0x01;
770
		Status = VL53L1_WrByte(Dev, VL53L1_PAD_I2C_HV__EXTSUP_CONFIG,
771
				i);
772
	}
773
#endif
774

  
775
	if (Status == VL53L1_ERROR_NONE)
776
		Status = VL53L1_data_init(Dev, 1);
777

  
778
	if (Status == VL53L1_ERROR_NONE) {
779
		VL53L1DevDataSet(Dev, PalState, VL53L1_STATE_WAIT_STATICINIT);
780
		VL53L1DevDataSet(Dev, CurrentParameters.PresetMode,
781
				VL53L1_PRESETMODE_LOWPOWER_AUTONOMOUS);
782
	}
783

  
784
	/* Enable all check */
785
	for (i = 0; i < VL53L1_CHECKENABLE_NUMBER_OF_CHECKS; i++) {
786
		if (Status == VL53L1_ERROR_NONE)
787
			Status |= VL53L1_SetLimitCheckEnable(Dev, i, 1);
788
		else
789
			break;
790

  
791
	}
792

  
793
	/* Limit default values */
794
	if (Status == VL53L1_ERROR_NONE) {
795
		Status = VL53L1_SetLimitCheckValue(Dev,
796
			VL53L1_CHECKENABLE_SIGMA_FINAL_RANGE,
797
				(FixPoint1616_t)(18 * 65536));
798
	}
799
	if (Status == VL53L1_ERROR_NONE) {
800
		Status = VL53L1_SetLimitCheckValue(Dev,
801
			VL53L1_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE,
802
				(FixPoint1616_t)(25 * 65536 / 100));
803
				/* 0.25 * 65536 */
804
	}
805

  
806
	LOG_FUNCTION_END(Status);
807
	return Status;
808
}
809

  
810

  
811
VL53L1_Error VL53L1_StaticInit(VL53L1_DEV Dev)
812
{
813
	VL53L1_Error Status = VL53L1_ERROR_NONE;
814
	uint8_t  measurement_mode;
815

  
816
	LOG_FUNCTION_START("");
817

  
818
	VL53L1DevDataSet(Dev, PalState, VL53L1_STATE_IDLE);
819

  
820
	measurement_mode  = VL53L1_DEVICEMEASUREMENTMODE_BACKTOBACK;
821
	VL53L1DevDataSet(Dev, LLData.measurement_mode, measurement_mode);
822

  
823
	VL53L1DevDataSet(Dev, CurrentParameters.NewDistanceMode,
824
			VL53L1_DISTANCEMODE_LONG);
825

  
826
	VL53L1DevDataSet(Dev, CurrentParameters.InternalDistanceMode,
827
			VL53L1_DISTANCEMODE_LONG);
828

  
829
	VL53L1DevDataSet(Dev, CurrentParameters.DistanceMode,
830
			VL53L1_DISTANCEMODE_LONG);
831

  
832
	/* ticket 472728 fix */
833
	Status = VL53L1_SetPresetMode(Dev,
834
			VL53L1_PRESETMODE_LOWPOWER_AUTONOMOUS);
835
	/* end of ticket 472728 fix */
836
	LOG_FUNCTION_END(Status);
837
	return Status;
838
}
839

  
840
VL53L1_Error VL53L1_WaitDeviceBooted(VL53L1_DEV Dev)
841
{
842
	VL53L1_Error Status = VL53L1_ERROR_NONE;
843

  
844
	LOG_FUNCTION_START("");
845

  
846
	Status = VL53L1_poll_for_boot_completion(Dev,
847
			VL53L1_BOOT_COMPLETION_POLLING_TIMEOUT_MS);
848

  
849
	LOG_FUNCTION_END(Status);
850
	return Status;
851
}
852

  
853
/* End Group PAL Init Functions */
854

  
855
/* Group PAL Parameters Functions */
856
static VL53L1_Error ComputeDevicePresetMode(
857
		VL53L1_PresetModes PresetMode,
858
		VL53L1_DistanceModes DistanceMode,
859
		VL53L1_DevicePresetModes *pDevicePresetMode)
860
{
861
	VL53L1_Error Status = VL53L1_ERROR_NONE;
862

  
863
	uint8_t DistIdx;
864
	VL53L1_DevicePresetModes LightModes[3] = {
865
		VL53L1_DEVICEPRESETMODE_STANDARD_RANGING_SHORT_RANGE,
866
		VL53L1_DEVICEPRESETMODE_STANDARD_RANGING,
867
		VL53L1_DEVICEPRESETMODE_STANDARD_RANGING_LONG_RANGE};
868

  
869

  
870
	VL53L1_DevicePresetModes TimedModes[3] = {
871
		VL53L1_DEVICEPRESETMODE_TIMED_RANGING_SHORT_RANGE,
872
		VL53L1_DEVICEPRESETMODE_TIMED_RANGING,
873
		VL53L1_DEVICEPRESETMODE_TIMED_RANGING_LONG_RANGE};
874

  
875
	VL53L1_DevicePresetModes LowPowerTimedModes[3] = {
876
		VL53L1_DEVICEPRESETMODE_LOWPOWERAUTO_SHORT_RANGE,
877
		VL53L1_DEVICEPRESETMODE_LOWPOWERAUTO_MEDIUM_RANGE,
878
		VL53L1_DEVICEPRESETMODE_LOWPOWERAUTO_LONG_RANGE};
879

  
880
	*pDevicePresetMode = VL53L1_DEVICEPRESETMODE_STANDARD_RANGING;
881

  
882
	switch (DistanceMode) {
883
	case VL53L1_DISTANCEMODE_SHORT:
884
		DistIdx = 0;
885
		break;
886
	case VL53L1_DISTANCEMODE_MEDIUM:
887
		DistIdx = 1;
888
		break;
889
	default:
890
		DistIdx = 2;
891
	}
892

  
893
	switch (PresetMode) {
894
	case VL53L1_PRESETMODE_LITE_RANGING:
895
		*pDevicePresetMode = LightModes[DistIdx];
896
		break;
897

  
898

  
899
	case VL53L1_PRESETMODE_AUTONOMOUS:
900
		*pDevicePresetMode = TimedModes[DistIdx];
901
		break;
902

  
903
	case VL53L1_PRESETMODE_LOWPOWER_AUTONOMOUS:
904
		*pDevicePresetMode = LowPowerTimedModes[DistIdx];
905
		break;
906

  
907
	default:
908
		/* Unsupported mode */
909
		Status = VL53L1_ERROR_MODE_NOT_SUPPORTED;
910
	}
911

  
912
	return Status;
913
}
914

  
915
static VL53L1_Error SetPresetMode(VL53L1_DEV Dev,
916
		VL53L1_PresetModes PresetMode,
917
		VL53L1_DistanceModes DistanceMode,
918
		uint32_t inter_measurement_period_ms)
919
{
920
	VL53L1_Error Status = VL53L1_ERROR_NONE;
921
	VL53L1_DevicePresetModes   device_preset_mode;
922
	uint8_t measurement_mode;
923
	uint16_t dss_config__target_total_rate_mcps;
924
	uint32_t phasecal_config_timeout_us;
925
	uint32_t mm_config_timeout_us;
926
	uint32_t lld_range_config_timeout_us;
927

  
928
	LOG_FUNCTION_START("%d", (int)PresetMode);
929

  
930
	if ((PresetMode == VL53L1_PRESETMODE_AUTONOMOUS) ||
931
		(PresetMode == VL53L1_PRESETMODE_LOWPOWER_AUTONOMOUS))
932
		measurement_mode  = VL53L1_DEVICEMEASUREMENTMODE_TIMED;
933
	else
934
		measurement_mode  = VL53L1_DEVICEMEASUREMENTMODE_BACKTOBACK;
935

  
936

  
937
	Status = ComputeDevicePresetMode(PresetMode, DistanceMode,
938
			&device_preset_mode);
939

  
940
	if (Status == VL53L1_ERROR_NONE)
941
		Status =  VL53L1_get_preset_mode_timing_cfg(Dev,
942
				device_preset_mode,
943
				&dss_config__target_total_rate_mcps,
944
				&phasecal_config_timeout_us,
945
				&mm_config_timeout_us,
946
				&lld_range_config_timeout_us);
947

  
948
	if (Status == VL53L1_ERROR_NONE)
949
		Status = VL53L1_set_preset_mode(
950
				Dev,
951
				device_preset_mode,
952
				dss_config__target_total_rate_mcps,
953
				phasecal_config_timeout_us,
954
				mm_config_timeout_us,
955
				lld_range_config_timeout_us,
956
				inter_measurement_period_ms);
957

  
958
	if (Status == VL53L1_ERROR_NONE)
959
		VL53L1DevDataSet(Dev, LLData.measurement_mode, measurement_mode);
960

  
961
	if (Status == VL53L1_ERROR_NONE)
962
		VL53L1DevDataSet(Dev, CurrentParameters.PresetMode, PresetMode);
963

  
964
	LOG_FUNCTION_END(Status);
965
	return Status;
966
}
967

  
968
VL53L1_Error VL53L1_SetPresetMode(VL53L1_DEV Dev, VL53L1_PresetModes PresetMode)
969
{
970
	VL53L1_Error Status = VL53L1_ERROR_NONE;
971
	VL53L1_DistanceModes DistanceMode = VL53L1_DISTANCEMODE_LONG;
972

  
973
	LOG_FUNCTION_START("%d", (int)PresetMode);
974

  
975
	Status = SetPresetMode(Dev,
976
			PresetMode,
977
			DistanceMode,
978
			1000);
979

  
980
	if (Status == VL53L1_ERROR_NONE) {
981
		VL53L1DevDataSet(Dev, CurrentParameters.InternalDistanceMode,
982
				DistanceMode);
983

  
984
		VL53L1DevDataSet(Dev, CurrentParameters.NewDistanceMode,
985
				DistanceMode);
986

  
987
		if ((PresetMode == VL53L1_PRESETMODE_LITE_RANGING) ||
988
			(PresetMode == VL53L1_PRESETMODE_AUTONOMOUS) ||
989
			(PresetMode == VL53L1_PRESETMODE_LOWPOWER_AUTONOMOUS))
990
			Status = VL53L1_SetMeasurementTimingBudgetMicroSeconds(
991
				Dev, 41000);
992
		else
993
			/* Set default timing budget to 30Hz (33.33 ms)*/
994
			Status = VL53L1_SetMeasurementTimingBudgetMicroSeconds(
995
				Dev, 33333);
996
	}
997

  
998
	if (Status == VL53L1_ERROR_NONE) {
999
		/* Set default intermeasurement period to 1000 ms */
1000
		Status = VL53L1_SetInterMeasurementPeriodMilliSeconds(Dev,
1001
				1000);
1002
	}
1003

  
1004
	LOG_FUNCTION_END(Status);
1005
	return Status;
1006
}
1007

  
1008

  
1009
VL53L1_Error VL53L1_GetPresetMode(VL53L1_DEV Dev,
1010
	VL53L1_PresetModes *pPresetMode)
1011
{
1012
	VL53L1_Error Status = VL53L1_ERROR_NONE;
1013

  
1014
	LOG_FUNCTION_START("");
1015

  
1016
	*pPresetMode = VL53L1DevDataGet(Dev, CurrentParameters.PresetMode);
1017

  
1018
	LOG_FUNCTION_END(Status);
1019
	return Status;
1020
}
1021

  
1022
VL53L1_Error VL53L1_SetDistanceMode(VL53L1_DEV Dev,
1023
		VL53L1_DistanceModes DistanceMode)
1024
{
1025
	VL53L1_Error Status = VL53L1_ERROR_NONE;
1026
	VL53L1_PresetModes PresetMode;
1027
	VL53L1_DistanceModes InternalDistanceMode;
1028
	uint32_t inter_measurement_period_ms;
1029
	uint32_t TimingBudget;
1030
	uint32_t MmTimeoutUs;
1031
	uint32_t PhaseCalTimeoutUs;
1032
	VL53L1_user_zone_t user_zone;
1033

  
1034
	LOG_FUNCTION_START("%d", (int)DistanceMode);
1035

  
1036
	PresetMode = VL53L1DevDataGet(Dev, CurrentParameters.PresetMode);
1037

  
1038
	/* when the distance mode is valid:
1039
	 * Manual Mode: all modes
1040
	 * AUTO AUTO_LITE : LITE_RANGING, RANGING
1041
	 */
1042

  
1043
	if ((DistanceMode != VL53L1_DISTANCEMODE_SHORT) &&
1044
		(DistanceMode != VL53L1_DISTANCEMODE_MEDIUM) &&
1045
		(DistanceMode != VL53L1_DISTANCEMODE_LONG))
1046
		return VL53L1_ERROR_INVALID_PARAMS;
1047

  
1048
	/* The internal distance mode is limited to Short, Medium or
1049
	 * long only
1050
	*/
1051
	if (Status == VL53L1_ERROR_NONE) {
1052
		if ((DistanceMode == VL53L1_DISTANCEMODE_SHORT) ||
1053
			(DistanceMode == VL53L1_DISTANCEMODE_MEDIUM))
1054
			InternalDistanceMode = DistanceMode;
1055
		else /* (DistanceMode == VL53L1_DISTANCEMODE_LONG) */
1056
			InternalDistanceMode = VL53L1_DISTANCEMODE_LONG;
1057
	}
1058

  
1059
	if (Status == VL53L1_ERROR_NONE)
1060
		Status = VL53L1_get_user_zone(Dev, &user_zone);
1061

  
1062
	inter_measurement_period_ms =  VL53L1DevDataGet(Dev,
1063
				LLData.inter_measurement_period_ms);
1064

  
1065
	if (Status == VL53L1_ERROR_NONE)
1066
		Status = VL53L1_get_timeouts_us(Dev, &PhaseCalTimeoutUs,
1067
			&MmTimeoutUs, &TimingBudget);
1068

  
1069
	if (Status == VL53L1_ERROR_NONE)
1070
		Status = SetPresetMode(Dev,
1071
				PresetMode,
1072
				InternalDistanceMode,
1073
				inter_measurement_period_ms);
1074

  
1075
	if (Status == VL53L1_ERROR_NONE) {
1076
		VL53L1DevDataSet(Dev, CurrentParameters.InternalDistanceMode,
1077
				InternalDistanceMode);
1078
		VL53L1DevDataSet(Dev, CurrentParameters.NewDistanceMode,
1079
				InternalDistanceMode);
1080
		VL53L1DevDataSet(Dev, CurrentParameters.DistanceMode,
1081
				DistanceMode);
1082
	}
1083

  
1084
	if (Status == VL53L1_ERROR_NONE) {
1085
		Status = VL53L1_set_timeouts_us(Dev, PhaseCalTimeoutUs,
1086
			MmTimeoutUs, TimingBudget);
1087

  
1088
		if (Status == VL53L1_ERROR_NONE)
1089
			VL53L1DevDataSet(Dev, LLData.range_config_timeout_us,
1090
				TimingBudget);
1091
	}
1092

  
1093
	if (Status == VL53L1_ERROR_NONE)
1094
		Status = VL53L1_set_user_zone(Dev, &user_zone);
1095

  
1096
	LOG_FUNCTION_END(Status);
1097
	return Status;
1098
}
1099

  
1100
VL53L1_Error VL53L1_GetDistanceMode(VL53L1_DEV Dev,
1101
	VL53L1_DistanceModes *pDistanceMode)
1102
{
1103
	VL53L1_Error Status = VL53L1_ERROR_NONE;
1104

  
1105
	LOG_FUNCTION_START("");
1106

  
1107
	*pDistanceMode = VL53L1DevDataGet(Dev, CurrentParameters.DistanceMode);
1108

  
1109
	LOG_FUNCTION_END(Status);
1110
	return Status;
1111
}
1112

  
1113

  
1114

  
1115

  
1116
VL53L1_Error VL53L1_SetMeasurementTimingBudgetMicroSeconds(VL53L1_DEV Dev,
1117
	uint32_t MeasurementTimingBudgetMicroSeconds)
1118
{
1119
	VL53L1_Error Status = VL53L1_ERROR_NONE;
1120
	uint8_t Mm1Enabled;
1121
	uint8_t Mm2Enabled;
1122
	uint32_t TimingGuard;
1123
	uint32_t divisor;
1124
	uint32_t TimingBudget;
1125
	uint32_t MmTimeoutUs;
1126
	VL53L1_PresetModes PresetMode;
1127
	uint32_t PhaseCalTimeoutUs;
1128
	uint32_t vhv;
1129
	int32_t vhv_loops;
1130
	uint32_t FDAMaxTimingBudgetUs = FDA_MAX_TIMING_BUDGET_US;
1131

  
1132
	LOG_FUNCTION_START("");
1133

  
1134
	/* Timing budget is limited to 10 seconds */
1135
	if (MeasurementTimingBudgetMicroSeconds > 10000000)
1136
		Status = VL53L1_ERROR_INVALID_PARAMS;
1137

  
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff