Making it work

This commit is contained in:
jazzpi 2022-11-12 21:09:57 +01:00
parent 6bfe7c9fc8
commit 40e895313f
15 changed files with 5583 additions and 32 deletions

View File

@ -6,6 +6,11 @@ SourceFiles=../Core/Src/main.c;../Core/Src/stm32l4xx_it.c;../Core/Src/stm32l4xx_
HeaderPath=../Drivers/STM32L4xx_HAL_Driver/Inc;../Drivers/STM32L4xx_HAL_Driver/Inc/Legacy;../Drivers/CMSIS/Device/ST/STM32L4xx/Include;../Drivers/CMSIS/Include;../Core/Inc; HeaderPath=../Drivers/STM32L4xx_HAL_Driver/Inc;../Drivers/STM32L4xx_HAL_Driver/Inc/Legacy;../Drivers/CMSIS/Device/ST/STM32L4xx/Include;../Drivers/CMSIS/Include;../Core/Inc;
CDefines=USE_HAL_DRIVER;STM32L476xx;USE_HAL_DRIVER;USE_HAL_DRIVER; CDefines=USE_HAL_DRIVER;STM32L476xx;USE_HAL_DRIVER;USE_HAL_DRIVER;
[PreviousUsedMakefileFiles]
SourceFiles=Core/Src/main.c;Core/Src/stm32l4xx_it.c;Core/Src/stm32l4xx_hal_msp.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_i2c.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_i2c_ex.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_rcc.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_rcc_ex.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_flash.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_flash_ex.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_flash_ramfunc.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_gpio.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_dma.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_dma_ex.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_pwr.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_pwr_ex.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_cortex.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_exti.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_tim.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_tim_ex.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_uart.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_uart_ex.c;Drivers/CMSIS/Device/ST/STM32L4xx/Source/Templates/system_stm32l4xx.c;Core/Src/system_stm32l4xx.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_i2c.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_i2c_ex.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_rcc.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_rcc_ex.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_flash.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_flash_ex.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_flash_ramfunc.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_gpio.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_dma.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_dma_ex.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_pwr.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_pwr_ex.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_cortex.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_exti.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_tim.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_tim_ex.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_uart.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_uart_ex.c;Drivers/CMSIS/Device/ST/STM32L4xx/Source/Templates/system_stm32l4xx.c;Core/Src/system_stm32l4xx.c;;;
HeaderPath=Drivers/STM32L4xx_HAL_Driver/Inc;Drivers/STM32L4xx_HAL_Driver/Inc/Legacy;Drivers/CMSIS/Device/ST/STM32L4xx/Include;Drivers/CMSIS/Include;Core/Inc;
CDefines=USE_HAL_DRIVER;STM32L476xx;USE_HAL_DRIVER;USE_HAL_DRIVER;
[PreviousGenFiles] [PreviousGenFiles]
AdvancedFolderStructure=true AdvancedFolderStructure=true
HeaderFileListSize=3 HeaderFileListSize=3
@ -23,8 +28,3 @@ SourceFolderListSize=1
SourcePath#0=../Core/Src SourcePath#0=../Core/Src
SourceFiles=; SourceFiles=;
[PreviousUsedMakefileFiles]
SourceFiles=Core/Src/main.c;Core/Src/stm32l4xx_it.c;Core/Src/stm32l4xx_hal_msp.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_i2c.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_i2c_ex.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_rcc.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_rcc_ex.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_flash.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_flash_ex.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_flash_ramfunc.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_gpio.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_dma.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_dma_ex.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_pwr.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_pwr_ex.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_cortex.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_exti.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_tim.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_tim_ex.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_uart.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_uart_ex.c;Drivers/CMSIS/Device/ST/STM32L4xx/Source/Templates/system_stm32l4xx.c;Core/Src/system_stm32l4xx.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_i2c.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_i2c_ex.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_rcc.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_rcc_ex.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_flash.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_flash_ex.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_flash_ramfunc.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_gpio.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_dma.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_dma_ex.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_pwr.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_pwr_ex.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_cortex.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_exti.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_tim.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_tim_ex.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_uart.c;Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_uart_ex.c;Drivers/CMSIS/Device/ST/STM32L4xx/Source/Templates/system_stm32l4xx.c;Core/Src/system_stm32l4xx.c;;;
HeaderPath=Drivers/STM32L4xx_HAL_Driver/Inc;Drivers/STM32L4xx_HAL_Driver/Inc/Legacy;Drivers/CMSIS/Device/ST/STM32L4xx/Include;Drivers/CMSIS/Include;Core/Inc;
CDefines=USE_HAL_DRIVER;STM32L476xx;USE_HAL_DRIVER;USE_HAL_DRIVER;

View File

@ -1,21 +1,21 @@
/* USER CODE BEGIN Header */ /* USER CODE BEGIN Header */
/** /**
****************************************************************************** ******************************************************************************
* @file : main.h * @file : main.h
* @brief : Header for main.c file. * @brief : Header for main.c file.
* This file contains the common defines of the application. * This file contains the common defines of the application.
****************************************************************************** ******************************************************************************
* @attention * @attention
* *
* Copyright (c) 2022 STMicroelectronics. * Copyright (c) 2022 STMicroelectronics.
* All rights reserved. * All rights reserved.
* *
* This software is licensed under terms that can be found in the LICENSE file * This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component. * in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS. * If no LICENSE file comes with this software, it is provided AS-IS.
* *
****************************************************************************** ******************************************************************************
*/ */
/* USER CODE END Header */ /* USER CODE END Header */
/* Define to prevent recursive inclusion -------------------------------------*/ /* Define to prevent recursive inclusion -------------------------------------*/
@ -53,7 +53,7 @@ extern "C" {
void Error_Handler(void); void Error_Handler(void);
/* USER CODE BEGIN EFP */ /* USER CODE BEGIN EFP */
extern volatile uint16_t range;
/* USER CODE END EFP */ /* USER CODE END EFP */
/* Private defines -----------------------------------------------------------*/ /* Private defines -----------------------------------------------------------*/

8
Core/Inc/vl6180x_glue.h Normal file
View File

@ -0,0 +1,8 @@
#ifndef _INC_VL6180X_GLUE_H
#define _INC_VL6180X_GLUE_H
#include "stm32l4xx_hal_i2c.h"
void vl6180x_glue_init(I2C_HandleTypeDef *handle_i2c);
#endif // _INC_VL6180X_GLUE_H

1019
Core/Lib/Inc/vl6180x_api.h Normal file

File diff suppressed because it is too large Load Diff

119
Core/Lib/Inc/vl6180x_cfg.h Normal file
View File

@ -0,0 +1,119 @@
/*******************************************************************************
Copyright © 2015, STMicroelectronics International N.V.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of STMicroelectronics nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
NON-INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS ARE DISCLAIMED.
IN NO EVENT SHALL STMICROELECTRONICS INTERNATIONAL N.V. BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
********************************************************************************/
/*
* $Date: 2015-07-07 10:05:35 +0200 (Tue, 07 Jul 2015) $
* $Revision: 2438 $
*/
/**
* @file VL6180x_cfg.h
*
* Proximity and ALS configuration
*/
#ifndef VL6180x_CFG_H_
#define VL6180x_CFG_H_
/** @defgroup api_config Configuration
* @brief API static configuration
*/
/** @ingroup api_config
* @{*/
/**
* @def VL6180x_UPSCALE_SUPPORT
* @brief Configure up-scale capabilities and default up-scale factor for ranging operations
*
* @li 1 : Fixed scaling by 1 (no up-scaling support)
* @li 2 : Fixed scaling by 2
* @li 3 : Fixed scaling by 3
* @li -1 -2 -3 : Run time programmable through @a VL6180x_UpscaleSetScaling(). Default scaling factore is -VL6180x_UPSCALE_SUPPORT \n
*/
#define VL6180x_UPSCALE_SUPPORT -1
/**
* @def VL6180x_ALS_SUPPORT
* @brief Enable ALS support
*
* Set to 0 if ALS is not used in application. This can help reducing code size if it is a concern.
*/
#define VL6180x_ALS_SUPPORT 1
/**
* @def VL6180x_HAVE_DMAX_RANGING
* @brief Enable DMax calculation for ranging applications.
*
* When set to 1, __Dmax__ is returned by API typically when @a VL6180x_RangePollMeasurement() high level
* function is called (this is returned in @a VL6180x_RangeData_t structure).
* __Dmax__ is an estimation of the maximum distance (in mm) the product can report a valid distance of a 17% target for
* the current ambient light conditions (__Dmax__ decreases when ambient light increases). __Dmax__ should be used only
* when the product is not able to return a valid distance (no object or object is too far from the ranging sensor).
* Typically, this is done by checking the __errorStatus__ field of the @a VL6180x_RangeData_t structure returned by
* the @a VL6180x_RangePollMeasurement() function.
* You may refer to ::RangeError_u to get full list of supported error codes.
* @warning Dmax is estimated for a 17% grey target. If the real target has a reflectance lower than 17%, report Dmax could be over-estimated
*/
#define VL6180x_HAVE_DMAX_RANGING 1
/**
* @def VL6180x_WRAP_AROUND_FILTER_SUPPORT
* @brief Enable wrap around filter (WAF) feature
*
* In specific conditions, when targeting a mirror or a very reflective metal, a __wrap around__ effect can occur internally to the
* ranging product which results in returning a wrong distance (under-estimated). Goal of the WAF is to detect this wrap arround effect
* and to filter it by returning a non-valid distance : __errorStatus__ set to 16 (see ::RangeError_u)
* @warning Wrap-around filter can not be used when device is running in continuous mode
*
* @li 0 : Filter is not supported, no filtering code is included in API
* @li 1 : Filter is supported and active by default
* @li -1 : Filter is supported but is not active by default @a VL6180x_FilterSetState() can turn it on and off at any time
*/
#define VL6180x_WRAP_AROUND_FILTER_SUPPORT 1
/**
* @def VL6180x_EXTENDED_RANGE
* @brief Enable extended ranging support
*
* Device that do not formally support extended ranging should only be used with a scaling factor of 1.
* Correct operation with scaling factor other than 1 (>200mm ) is not granted by ST.
*/
#define VL6180x_EXTENDED_RANGE 0
#if (VL6180x_EXTENDED_RANGE) && (VL6180x_ALS_SUPPORT)
#warning "Als support should be OFF for extended range"
#endif
#endif
/** @} */ // end of api_config
/* VL6180x_CFG_H_ */

773
Core/Lib/Inc/vl6180x_def.h Normal file
View File

@ -0,0 +1,773 @@
/*******************************************************************************
Copyright © 2015, STMicroelectronics International N.V.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of STMicroelectronics nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
NON-INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS ARE DISCLAIMED.
IN NO EVENT SHALL STMICROELECTRONICS INTERNATIONAL N.V. BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
********************************************************************************/
/*
* $Date: 2018-07-04 16:49:57 +0200 (Wed, 04 Jul 2018) $
* $Revision: 2768 $
*/
/**
* @file VL6180x_def.h
*
* @brief Type definitions for vl6180x api.
*
*/
#ifndef _VL6180x_DEF
#define _VL6180x_DEF
/** API major version */
#define VL6180x_API_REV_MAJOR 3
/** API minor version */
#define VL6180x_API_REV_MINOR 2
/** API sub version */
#define VL6180x_API_REV_SUB 2
#define VL6180X_STR_HELPER(x) #x
#define VL6180X_STR(x) VL6180X_STR_HELPER(x)
#include "vl6180x_cfg.h"
#include "vl6180x_types.h"
/*
* check configuration macro raise error or warning and suggest a default value
*/
#ifndef VL6180x_UPSCALE_SUPPORT
#error "VL6180x_UPSCALE_SUPPORT not defined"
/* TODO you must define value for upscale support in your vl6180x_cfg.h */
#endif
#ifndef VL6180x_ALS_SUPPORT
#error "VL6180x_ALS_SUPPORT not defined"
/* TODO you must define VL6180x_ALS_SUPPORT with a value in your vl6180x_cfg.h set to 0 do disable*/
#endif
#ifndef VL6180x_HAVE_DMAX_RANGING
#error "VL6180x_HAVE_DMAX_RANGING not defined"
/* TODO you may remove or comment these #error and keep the default below or update your vl6180x_cfg.h .h file */
/**
* force VL6180x_HAVE_DMAX_RANGING to not supported when not part of cfg file
*/
#define VL6180x_HAVE_DMAX_RANGING 0
#endif
#ifndef VL6180x_EXTENDED_RANGE
#define VL6180x_EXTENDED_RANGE 0
#endif
#ifndef VL6180x_WRAP_AROUND_FILTER_SUPPORT
#error "VL6180x_WRAP_AROUND_FILTER_SUPPORT not defined ?"
/* TODO you may remove or comment these #error and keep the default below or update vl6180x_cfg.h file */
/**
* force VL6180x_WRAP_AROUND_FILTER_SUPPORT to not supported when not part of cfg file
*/
#define VL6180x_WRAP_AROUND_FILTER_SUPPORT 0
#endif
#ifndef VL6180x_HAVE_MULTI_READ
# define VL6180x_HAVE_MULTI_READ 0
#endif
/**
* Force VL6180x_CACHED_REG to default 0 when not defined
*/
#ifndef VL6180x_CACHED_REG
# define VL6180x_CACHED_REG 0
#else
# define VL6180x_FIRST_CACHED_INDEX 0x04D
# define VL6180x_LAST_CACHED_INDEX (VL6180x_FIRST_CACHED_INDEX+55)
# define VL6180x_CACHED_REG_CNT (VL6180x_LAST_CACHED_INDEX-VL6180x_FIRST_CACHED_INDEX+1)
#endif
/****************************************
* PRIVATE define do not edit
****************************************/
/** Maximal buffer size ever use in i2c */
#define VL6180x_MAX_I2C_XFER_SIZE 8 /* At present time it 6 byte max but that can change */
#if VL6180x_UPSCALE_SUPPORT < 0
/**
* @def VL6180x_HAVE_UPSCALE_DATA
* @brief is defined if device data structure has data so when user configurable up-scale is active
*/
#define VL6180x_HAVE_UPSCALE_DATA /* have data only for user configurable up-scale config */
#endif
#if VL6180x_WRAP_AROUND_FILTER_SUPPORT
/**
* @def VL6180x_HAVE_WRAP_AROUND_DATA
* @brief is defined if device data structure has filter data so when active in cfg file
*/
#define VL6180x_HAVE_WRAP_AROUND_DATA
#endif
#if VL6180x_ALS_SUPPORT != 0
/**
* @def VL6180x_HAVE_ALS_DATA
* @brief is defined when als data are include in device data structure so when als suport if configured
*/
#define VL6180x_HAVE_ALS_DATA
#endif
#if VL6180x_WRAP_AROUND_FILTER_SUPPORT || VL6180x_HAVE_DMAX_RANGING
#define VL6180x_HAVE_RATE_DATA
#endif
/** Error and warning code returned by API
*
* negative value are true error mostly fatal\n
* positive value are warning most of time it's ok to continue\n
*/
enum VL6180x_ErrCode_t {
API_NO_ERROR = 0,
CALIBRATION_WARNING = 1, /*!< warning invalid calibration data may be in used \a VL6180x_InitData() \a VL6180x_GetOffsetCalibrationData \a VL6180x_SetOffsetCalibrationData*/
MIN_CLIPED = 2, /*!< warning parameter passed was clipped to min before to be applied */
NOT_GUARANTEED = 3, /*!< Correct operation is not guaranteed typically using extended ranging on vl6180x */
API_ERROR = -1, /*!< Unqualified error */
INVALID_PARAMS = -2, /*!< parameter passed is invalid or out of range */
NOT_SUPPORTED = -3, /*!< function is not supported in current mode or configuration */
RANGE_ERROR = -4, /*!< device report a ranging error interrupt status */
TIME_OUT = -5, /*!< aborted due to time out */
};
/**
* Filtered result data structure range data is to be used
*/
typedef struct RangeFilterResult_tag {
uint16_t range_mm; /*!< Filtered ranging value */
uint16_t rawRange_mm; /*!< raw range value (scaled) */
uint32_t filterError; /*!< current filter error code */
} RangeFilterResult_t;
/**
* "small" unsigned data type used in filter
*
* if data space saving is not a concern it can be change to platform native unsigned int
*/
typedef uint32_t FilterType1_t;
/**
* @def FILTER_NBOF_SAMPLES
* @brief sample history len used for wrap around filtering
*/
#define FILTER_NBOF_SAMPLES 10
/**
* Wrap around filter internal data
*/
struct FilterData_t {
uint32_t MeasurementIndex; /*!< current measurement index */
uint32_t MeasurementsSinceLastFlush; /*!< Number of measurements done since last time buffer has been flushed */
uint16_t LastTrueRange[FILTER_NBOF_SAMPLES]; /*!< filtered/corrected distance history */
uint32_t LastReturnRates[FILTER_NBOF_SAMPLES]; /*!< Return rate history */
uint16_t StdFilteredReads; /*!< internal use */
FilterType1_t Default_ZeroVal; /*!< internal use */
FilterType1_t Default_VAVGVal; /*!< internal use */
FilterType1_t NoDelay_ZeroVal; /*!< internal use */
FilterType1_t NoDelay_VAVGVal; /*!< internal use */
FilterType1_t Previous_VAVGDiff; /*!< internal use */
uint32_t FilteringOnGoingConsecutiveStates; /*!< internal use */
uint32_t filterError; /*!< current filter error code */
};
#if VL6180x_HAVE_DMAX_RANGING
typedef int32_t DMaxFix_t;
struct DMaxData_t {
uint32_t ambTuningWindowFactor_K; /*!< internal algo tuning (*1000) */
DMaxFix_t retSignalAt400mm; /*!< intermediate dmax computation value caching @a #SYSRANGE_CROSSTALK_COMPENSATION_RATE and private reg 0x02A */
/* int32_t RegB8; */ /*!< register 0xB8 cached to speed reduce i2c traffic for dmax computation */
/* place all word data below to optimize struct packing */
/* int32_t minSignalNeeded; */ /*!< optimized computation intermediate base on register cached value */
int32_t snrLimit_K; /*!< cached and optimized computation intermediate from @a #SYSRANGE_MAX_AMBIENT_LEVEL_MULT */
uint16_t ClipSnrLimit; /*!< Max value for snr limit */
/* place all byte data below to optimize packing */
/* uint8_t MaxConvTime; */ /*!< cached max convergence time @a #SYSRANGE_MAX_CONVERGENCE_TIME*/
};
#endif
struct RangeIgnoreData_t {
uint16_t ValidHeight;
uint16_t IgnoreThreshold;
uint8_t Enabled;
};
/**
* @struct VL6180xDevData_t
*
* @brief Per VL6180x device St private data structure \n
* End user should never access any of these field directly
*
* These must never access directly but only via VL6180xDev/SetData(dev, field) macro
*/
struct VL6180xDevData_t {
uint32_t Part2PartAmbNVM; /*!< backed up NVM value */
uint32_t XTalkCompRate_KCps; /*! Cached XTlak Compensation Rate */
uint16_t EceFactorM; /*!< Ece Factor M numerator */
uint16_t EceFactorD; /*!< Ece Factor D denominator*/
struct RangeIgnoreData_t RangeIgnore;
#ifdef VL6180x_HAVE_ALS_DATA
uint16_t IntegrationPeriod; /*!< cached als Integration period avoid slow read from device at each measure */
uint16_t AlsGainCode; /*!< cached Als gain avoid slow read from device at each measure */
uint16_t AlsScaler; /*!< cached Als scaler avoid slow read from device at each measure */
#endif
#ifdef VL6180x_HAVE_UPSCALE_DATA
uint8_t UpscaleFactor; /*!< up-scaling factor*/
#endif
#ifdef VL6180x_HAVE_WRAP_AROUND_DATA
uint8_t WrapAroundFilterActive; /*!< Filter on/off */
struct FilterData_t FilterData; /*!< Filter internal data state history ... */
#endif
#if VL6180x_CACHED_REG
uint8_t CacheFilled; /*!< Set if valid data got fetched use to control when to fill up register cache */
uint8_t CachedRegs[VL6180x_CACHED_REG_CNT]; /*!< Cache register storage */
#endif
#if VL6180x_HAVE_DMAX_RANGING
struct DMaxData_t DMaxData;
uint8_t DMaxEnable;
#endif
int8_t Part2PartOffsetNVM; /*!< backed up NVM value */
};
/**
* @struct VL6180x_RangeData_t
* @brief Range and any optional measurement data.
*/
typedef struct {
int32_t range_mm; /*!< range distance in mm. */
int32_t signalRate_mcps; /*!< signal rate (MCPS)\n these is a 9.7 fix point value, which is effectively a measure of target reflectance.*/
uint32_t errorStatus; /*!< Error status of the current measurement. \n see @a ::RangeError_u @a VL6180x_GetRangeStatusErrString() */
#ifdef VL6180x_HAVE_RATE_DATA
uint32_t rtnAmbRate; /*!< Return Ambient rate in KCount per sec related to \a RESULT_RANGE_RETURN_AMB_COUNT */
uint32_t rtnRate; /*!< Return rate in KCount per sec related to \a RESULT_RANGE_RETURN_SIGNAL_COUNT */
uint32_t rtnConvTime; /*!< Return Convergence time \a RESULT_RANGE_RETURN_CONV_TIME */
uint32_t refConvTime; /*!< Reference convergence time \a RESULT_RANGE_REFERENCE_CONV_TIME */
#endif
#if VL6180x_HAVE_DMAX_RANGING
uint32_t DMax; /*!< DMax when applicable */
#endif
#ifdef VL6180x_HAVE_WRAP_AROUND_DATA
RangeFilterResult_t FilteredData; /*!< Filter result main range_mm is updated */
#endif
} VL6180x_RangeData_t;
/** use where fix point 9.7 bit values are expected
*
* given a floating point value f it's .7 bit point is (int)(f*(1<<7))*/
typedef uint16_t FixPoint97_t;
/** lux data type */
typedef uint32_t lux_t;
/**
* @brief This data type defines als measurement data.
*/
typedef struct VL6180x_AlsData_st {
lux_t lux; /**< Light measurement (Lux) */
uint32_t errorStatus; /**< Error status of the current measurement. \n
* No Error := 0. \n
* Refer to product sheets for other error codes. */
} VL6180x_AlsData_t;
/**
* @brief Range status Error code
*
* @a VL6180x_GetRangeStatusErrString() if configured ( @a #VL6180x_RANGE_STATUS_ERRSTRING )
* related to register @a #RESULT_RANGE_STATUS and additional post processing
*/
typedef enum {
NoError = 0, /*!< 0 0b0000 NoError */
VCSEL_Continuity_Test, /*!< 1 0b0001 VCSEL_Continuity_Test */
VCSEL_Watchdog_Test, /*!< 2 0b0010 VCSEL_Watchdog_Test */
VCSEL_Watchdog, /*!< 3 0b0011 VCSEL_Watchdog */
PLL1_Lock, /*!< 4 0b0100 PLL1_Lock */
PLL2_Lock, /*!< 5 0b0101 PLL2_Lock */
Early_Convergence_Estimate,/*!< 6 0b0110 Early_Convergence_Estimate */
Max_Convergence, /*!< 7 0b0111 Max_Convergence */
No_Target_Ignore, /*!< 8 0b1000 No_Target_Ignore */
Not_used_9, /*!< 9 0b1001 Not_used */
Not_used_10, /*!< 10 0b1010 Not_used_ */
Max_Signal_To_Noise_Ratio, /*!< 11 0b1011 Max_Signal_To_Noise_Ratio*/
Raw_Ranging_Algo_Underflow,/*!< 12 0b1100 Raw_Ranging_Algo_Underflow*/
Raw_Ranging_Algo_Overflow, /*!< 13 0b1101 Raw_Ranging_Algo_Overflow */
Ranging_Algo_Underflow, /*!< 14 0b1110 Ranging_Algo_Underflow */
Ranging_Algo_Overflow, /*!< 15 0b1111 Ranging_Algo_Overflow */
/* code below are addition for API/software side they are not hardware*/
RangingFiltered = 0x10, /*!< 16 0b10000 filtered by post processing*/
DataNotReady = 0x12, /*!< 18 0b10011 New data sample not ready */
} RangeError_u;
/** @defgroup device_regdef Device registers & masks definitions
* @brief Device registers and masks definitions
*/
/** @ingroup device_regdef
* @{*/
/**
* The device model ID
*/
#define IDENTIFICATION_MODEL_ID 0x000
/**
* Revision identifier of the Device for major change.
*/
#define IDENTIFICATION_MODULE_REV_MAJOR 0x003
/**
* Revision identifier of the Device for minor change.
*/
#define IDENTIFICATION_MODULE_REV_MINOR 0x004
/**
* @def SYSTEM_MODE_GPIO0
* @brief Configures polarity and select which function gpio 0 serves.
* Gpio0 is chip enable at power up ! Be aware of all h/w implication of turning it to output.
* Same definition as #SYSTEM_MODE_GPIO1
* @ingroup device_regdef
*/
#define SYSTEM_MODE_GPIO0 0x010
/**
* @def SYSTEM_MODE_GPIO1
* @brief Configures polarity and select what als or ranging functionality gpio pin serves.
*
* Function can be #GPIOx_SELECT_OFF #GPIOx_SELECT_GPIO_INTERRUPT_OUTPUT.\n
* Same definition apply to register GPIO0 that is used as chip enable at power up.
* @ingroup device_regdef
*/
#define SYSTEM_MODE_GPIO1 0x011
/** gpio pad POLARITY mask in #SYSTEM_MODE_GPIO1 (and/or 0) write 1 to set active high polarity (positive edge) */
#define GPIOx_POLARITY_SELECT_MASK 0x20
/** gpio pad Function select shift in #SYSTEM_MODE_GPIO1 or 0 */
#define GPIOx_FUNCTIONALITY_SELECT_SHIFT 1
/** gpio pad Function select mask in #SYSTEM_MODE_GPIO1 or 0 */
#define GPIOx_FUNCTIONALITY_SELECT_MASK (0xF<<GPIOx_FUNCTIONALITY_SELECT_SHIFT)
/** select no interrupt in #SYSTEM_MODE_GPIO1 pad is put in Hi-Z*/
#define GPIOx_SELECT_OFF 0x00
/** select gpiox as interrupt output in #SYSTEM_MODE_GPIO1 */
#define GPIOx_SELECT_GPIO_INTERRUPT_OUTPUT 0x08
/** select range as source for interrupt on in #SYSTEM_MODE_GPIO1 */
#define GPIOx_MODE_SELECT_RANGING 0x00
/** select als as source for interrupt on in #SYSTEM_MODE_GPIO1 */
#define GPIOx_MODE_SELECT_ALS 0x01
/**
* @def SYSTEM_INTERRUPT_CONFIG_GPIO
*
* @brief Configure Als and Ranging interrupt reporting
*
* Possible values for Range and ALS are\n
*
* #CONFIG_GPIO_INTERRUPT_DISABLED\n
* #CONFIG_GPIO_INTERRUPT_LEVEL_LOW\n
* #CONFIG_GPIO_INTERRUPT_LEVEL_HIGH\n
* #CONFIG_GPIO_INTERRUPT_OUT_OF_WINDOW\n
* #CONFIG_GPIO_INTERRUPT_NEW_SAMPLE_READY\n
* Apply respective rang/als shift and mask \n
* #CONFIG_GPIO_RANGE_SHIFT and full reg mask #CONFIG_GPIO_RANGE_MASK\n
* #CONFIG_GPIO_ALS_SHIFT and full reg mask #CONFIG_GPIO_ALS_MASK\n
*
* \sa GPIO use for interrupt #SYSTEM_MODE_GPIO0 or #SYSTEM_MODE_GPIO1\n
* @ingroup device_regdef
*/
#define SYSTEM_INTERRUPT_CONFIG_GPIO 0x014
/** RANGE bits shift in #SYSTEM_INTERRUPT_CONFIG_GPIO */
#define CONFIG_GPIO_RANGE_SHIFT 0
/** RANGE bits mask in #SYSTEM_INTERRUPT_CONFIG_GPIO (unshifted)*/
#define CONFIG_GPIO_RANGE_MASK (0x7<<CONFIG_GPIO_RANGE_SHIFT)
/** ALS bits shift in #SYSTEM_INTERRUPT_CONFIG_GPIO */
#define CONFIG_GPIO_ALS_SHIFT 3
/** ALS bits mask in #SYSTEM_INTERRUPT_CONFIG_GPIO (unshifted)*/
#define CONFIG_GPIO_ALS_MASK (0x7<<CONFIG_GPIO_ALS_SHIFT)
/** interrupt is disabled */
#define CONFIG_GPIO_INTERRUPT_DISABLED 0x00
/** trigger when value < low threshold */
#define CONFIG_GPIO_INTERRUPT_LEVEL_LOW 0x01
/** trigger when value < low threshold */
#define CONFIG_GPIO_INTERRUPT_LEVEL_HIGH 0x02
/** trigger when outside range defined by high low threshold */
#define CONFIG_GPIO_INTERRUPT_OUT_OF_WINDOW 0x03
/** trigger when new sample are ready */
#define CONFIG_GPIO_INTERRUPT_NEW_SAMPLE_READY 0x04
/**
* @def SYSTEM_INTERRUPT_CLEAR
* @brief Writing to this register will clear interrupt source
*
* Use or combination of any #INTERRUPT_CLEAR_RANGING , #INTERRUPT_CLEAR_ALS , #INTERRUPT_CLEAR_ERROR
* @ingroup device_regdef
*/
#define SYSTEM_INTERRUPT_CLEAR 0x015
/** clear ranging interrupt in write to #SYSTEM_INTERRUPT_CLEAR */
#define INTERRUPT_CLEAR_RANGING 0x01
/** clear als interrupt in write to #SYSTEM_INTERRUPT_CLEAR */
#define INTERRUPT_CLEAR_ALS 0x02
/** clear error interrupt in write to #SYSTEM_INTERRUPT_CLEAR */
#define INTERRUPT_CLEAR_ERROR 0x04
/** After power up or reset this register will start reading 1 when device is ready */
#define SYSTEM_FRESH_OUT_OF_RESET 0x016
/**
* @def SYSTEM_GROUPED_PARAMETER_HOLD
* @brief Writing 1/0 activate/deactivate safe host update of multiple register in critical group \n
* rather use \a VL6180x_SetGroupParamHold()
*
* The critical register group is made of: \n
* #SYSTEM_INTERRUPT_CONFIG_GPIO \n
* #SYSRANGE_THRESH_HIGH \n
* #SYSRANGE_THRESH_LOW \n
* #SYSALS_INTEGRATION_PERIOD \n
* #SYSALS_ANALOGUE_GAIN \n
* #SYSALS_THRESH_HIGH \n
* #SYSALS_THRESH_LOW
* @ingroup device_regdef
*/
#define SYSTEM_GROUPED_PARAMETER_HOLD 0x017
/**
* @def SYSRANGE_START
* @brief Start/stop and set operating range mode
*
* Write Combination of #MODE_START_STOP and #MODE_CONTINUOUS to select and start desired operation.
*
* @ingroup device_regdef
*/
#define SYSRANGE_START 0x018
/** mask existing bit in #SYSRANGE_START*/
#define SYSRANGE_START_MODE_MASK 0x03
/** bit 0 in #SYSRANGE_START write 1 toggle state in continuous mode and arm next shot in single shot mode */
#define MODE_START_STOP 0x01
/** bit 1 write 1 in #SYSRANGE_START set continuous operation mode */
#define MODE_CONTINUOUS 0x02
/** bit 1 write 0 in #SYSRANGE_START set single shot mode */
#define MODE_SINGLESHOT 0x00
/**
* @def SYSRANGE_THRESH_HIGH
* High level range threshold (must be scaled)
* @ingroup device_regdef
*/
#define SYSRANGE_THRESH_HIGH 0x019
/**
* @def SYSRANGE_THRESH_LOW
* Low level range threshold (must be scaled)
* @ingroup device_regdef
*/
#define SYSRANGE_THRESH_LOW 0x01A
/**
* @def SYSRANGE_INTERMEASUREMENT_PERIOD
* @brief Continuous mode intermeasurement delay \a VL6180x_RangeSetInterMeasPeriod()
*
* Time delay between measurements in Ranging continuous mode.\n
* Range 0-254 (0 = 10ms).\n Step size = 10ms.
*
* @ingroup device_regdef
*/
#define SYSRANGE_INTERMEASUREMENT_PERIOD 0x01B
/**
* @brief Maximum time to run measurement in Ranging modes.
* Range 1 - 63 ms (1 code = 1 ms);
*
* Measurement aborted when limit reached to aid power reduction.\
* For example, 0x01 = 1ms, 0x0a = 10ms.\
* Note: Effective max_convergence_time depends on readout_averaging_sample_period setting.
*
* @ingroup device_regdef
*/
#define SYSRANGE_MAX_CONVERGENCE_TIME 0x01C
/**@brief Cross talk compensation rate
* @warning never write register directly use @a VL6180x_SetXTalkCompensationRate()
* refer to manual for calibration procedure and computation
* @ingroup device_regdef
*/
#define SYSRANGE_CROSSTALK_COMPENSATION_RATE 0x01E
/**
* @brief Minimum range value in mm to qualify for crosstalk compensation
*/
#define SYSRANGE_CROSSTALK_VALID_HEIGHT 0x021
#define SYSRANGE_EARLY_CONVERGENCE_ESTIMATE 0x022
#define SYSRANGE_PART_TO_PART_RANGE_OFFSET 0x024
/**
* @brief range ignore valid height
* @warning do not program directly use @a VL6180x_RangeIgnoreConfigure() and @a VL6180x_RangeIgnoreSetEnable()
*/
#define SYSRANGE_RANGE_IGNORE_VALID_HEIGHT 0x025
/**
* @brief range ignore threshold
* @warning do not program directly use @a VL6180x_RangeIgnoreConfigure() and @a VL6180x_RangeIgnoreSetEnable()
*/
#define SYSRANGE_RANGE_IGNORE_THRESHOLD 0x026
#define SYSRANGE_EMITTER_BLOCK_THRESHOLD 0x028
#define SYSRANGE_MAX_AMBIENT_LEVEL_THRESH 0x02A
#define SYSRANGE_MAX_AMBIENT_LEVEL_MULT 0x02C
/** @brief various Enable check enable register
* @a VL6180x_RangeSetEceState()
* @a VL6180x_RangeIgnoreConfigure() and @a VL6180x_RangeIgnoreSetEnable()
*/
#define SYSRANGE_RANGE_CHECK_ENABLES 0x02D
#define RANGE_CHECK_ECE_ENABLE_MASK 0x01
#define RANGE_CHECK_RANGE_ENABLE_MASK 0x02
#define RANGE_CHECK_SNR_ENABLE 0x10
#define SYSRANGE_VHV_RECALIBRATE 0x02E
#define SYSRANGE_VHV_REPEAT_RATE 0x031
/**
* @def SYSALS_START
* @brief Start/stop and set operating als mode
*
* same bit definition as range \a #SYSRANGE_START \n
*/
#define SYSALS_START 0x038
/** ALS low Threshold high */
#define SYSALS_THRESH_HIGH 0x03A
/** ALS low Threshold low */
#define SYSALS_THRESH_LOW 0x03C
/** ALS intermeasurement period */
#define SYSALS_INTERMEASUREMENT_PERIOD 0x03E
/**
* @warning or value with 0x40 when writing to these register*/
#define SYSALS_ANALOGUE_GAIN 0x03F
/** ALS integration period */
#define SYSALS_INTEGRATION_PERIOD 0x040
/**
* @brief Result range status
*
* Hold the various range interrupt flags and error Specific error codes
*/
#define RESULT_RANGE_STATUS 0x04D
/** Device ready for new command bit 0*/
#define RANGE_DEVICE_READY_MASK 0x01
/** mask for error status covers bits [7:4] in #RESULT_RANGE_STATUS @a ::RangeError_u */
#define RANGE_ERROR_CODE_MASK 0xF0 /* */
/** range error bit position in #RESULT_RANGE_STATUS */
#define RANGE_ERROR_CODE_SHIFT 4
/**
* @def RESULT_ALS_STATUS
* @brief Result als status \n
* Hold the various als interrupt flags and Specific error codes
*/
#define RESULT_ALS_STATUS 0x4E
/** Device ready for new command bit 0*/
#define ALS_DEVICE_READY_MASK 0x01
/**
* @def RESULT_ALS_VAL
* @brief 16 Bit ALS count output value.
*
* Lux value depends on Gain and integration settings and calibrated lux/count setting
* \a VL6180x_AlsGetLux() \a VL6180x_AlsGetMeasurement()
*/
#define RESULT_ALS_VAL 0x50
/**
* @def FW_ALS_RESULT_SCALER
* @brief Als scaler register Bits [3:0] analogue gain 1 to 16x
* these register content is cached by API in \a VL6180xDevData_t::AlsScaler
* for lux computation acceleration
*/
#define FW_ALS_RESULT_SCALER 0x120
/**
* these union can be use as a generic bit field type for map #RESULT_INTERRUPT_STATUS_GPIO register
* @ingroup device_regdef
*/
typedef union IntrStatus_u {
uint8_t val; /*!< raw 8 bit register value*/
struct {
unsigned Range:3; /*!< Range status one of :\n \a #RES_INT_STAT_GPIO_LOW_LEVEL_THRESHOLD \n \a #RES_INT_STAT_GPIO_HIGH_LEVEL_THRESHOLD \n \a #RES_INT_STAT_GPIO_OUT_OF_WINDOW \n \a #RES_INT_STAT_GPIO_NEW_SAMPLE_READY */
unsigned Als:3; /*!< Als status one of: \n \a #RES_INT_STAT_GPIO_LOW_LEVEL_THRESHOLD \n \a #RES_INT_STAT_GPIO_HIGH_LEVEL_THRESHOLD \n \a #RES_INT_STAT_GPIO_OUT_OF_WINDOW \n \a #RES_INT_STAT_GPIO_NEW_SAMPLE_READY */
unsigned Error:2; /*!< Error status of: \n \a #RES_INT_ERROR_LASER_SAFETY \n \a #RES_INT_ERROR_PLL */
} status; /*!< interrupt status as bit field */
} IntrStatus_t;
/**
* @def RESULT_INTERRUPT_STATUS_GPIO
* @brief System interrupt status report selected interrupt for als and ranging
*
* These register can be polled even if no gpio pins is active\n
* What reported is selected by \a #SYSTEM_INTERRUPT_CONFIG_GPIO \n
* Range mask with \a #RES_INT_RANGE_MASK and shit by \a #RES_INT_RANGE_SHIFT
* Als mask with \a #RES_INT_ALS_MASK and shit by \a #RES_INT_ALS_SHIFT
* Result value express condition (or combination?)
* \a #RES_INT_STAT_GPIO_LOW_LEVEL_THRESHOLD \n
* \a #RES_INT_STAT_GPIO_HIGH_LEVEL_THRESHOLD \n
* \a #RES_INT_STAT_GPIO_OUT_OF_WINDOW \n
* \a #RES_INT_STAT_GPIO_NEW_SAMPLE_READY
*
* @ingroup device_regdef
*/
#define RESULT_INTERRUPT_STATUS_GPIO 0x4F
/** ranging interrupt 1st bit position in #RESULT_INTERRUPT_STATUS_GPIO */
#define RES_INT_RANGE_SHIFT 0
/** ALS interrupt 1st bit position in #RESULT_INTERRUPT_STATUS_GPIO */
#define RES_INT_ALS_SHIFT 3
/** interrupt bit position in #RESULT_INTERRUPT_STATUS_GPIO */
#define RES_INT_ERROR_SHIFT 6
/** Ranging interrupt mask in #RESULT_INTERRUPT_STATUS_GPIO (prior to shift) \sa IntrStatus_t */
#define RES_INT_RANGE_MASK (0x7<<RES_INT_RANGE_SHIFT)
/** als interrupt mask in #RESULT_INTERRUPT_STATUS_GPIO (prior to shift) \sa IntrStatus_t */
#define RES_INT_ALS_MASK (0x7<<RES_INT_ALS_SHIFT)
/** low threshold condition in #RESULT_INTERRUPT_STATUS_GPIO for */
#define RES_INT_STAT_GPIO_LOW_LEVEL_THRESHOLD 0x01
/** high threshold condition in #RESULT_INTERRUPT_STATUS_GPIO for ALs or Rage*/
#define RES_INT_STAT_GPIO_HIGH_LEVEL_THRESHOLD 0x02
/** out of window condition in #RESULT_INTERRUPT_STATUS_GPIO */
#define RES_INT_STAT_GPIO_OUT_OF_WINDOW 0x03
/** new sample ready in #RESULT_INTERRUPT_STATUS_GPIO */
#define RES_INT_STAT_GPIO_NEW_SAMPLE_READY 0x04
/** error in #RESULT_INTERRUPT_STATUS_GPIO */
#define RES_INT_ERROR_MASK (0x3<<RES_INT_ERROR_SHIFT)
/** laser safety error on #RES_INT_ERROR_MASK of #RESULT_INTERRUPT_STATUS_GPIO */
#define RES_INT_ERROR_LASER_SAFETY 1
/** pll 1 or 2 error on #RES_INT_ERROR_MASK of #RESULT_INTERRUPT_STATUS_GPIO*/
#define RES_INT_ERROR_PLL 2
/**
* Final range result value presented to the user for use. Unit is in mm.
*/
#define RESULT_RANGE_VAL 0x062
/**
* Raw Range result value with offset applied (no cross talk compensation applied). Unit is in mm.
*/
#define RESULT_RANGE_RAW 0x064
/**
* @brief Sensor count rate of signal returns correlated to IR emitter.
*
* Computed from RETURN_SIGNAL_COUNT / RETURN_CONV_TIME. Mcps 9.7 format
*/
#define RESULT_RANGE_SIGNAL_RATE 0x066
/**
* @brief Return signal count
*
* Sensor count output value attributed to signal correlated to IR emitter on the Return array.
*/
#define RESULT_RANGE_RETURN_SIGNAL_COUNT 0x06C
/**
* @brief Reference signal count
*
* sensor count output value attributed to signal correlated to IR emitter on the Reference array.
*/
#define RESULT_RANGE_REFERENCE_SIGNAL_COUNT 0x070
/**
* @brief Return ambient count
*
* sensor count output value attributed to uncorrelated ambient signal on the Return array.
* Must be multiplied by 6 if used to calculate the ambient to signal threshold
*/
#define RESULT_RANGE_RETURN_AMB_COUNT 0x074
/**
* @brief Reference ambient count
*
* Sensor count output value attributed to uncorrelated ambient signal on the Reference array.
*/
#define RESULT_RANGE_REFERENCE_AMB_COUNT 0x078
/**
* sensor count output value attributed to signal on the Return array.
*/
#define RESULT_RANGE_RETURN_CONV_TIME 0x07C
/**
* sensor count output value attributed to signal on the Reference array.
*/
#define RESULT_RANGE_REFERENCE_CONV_TIME 0x080
/**
* @def RANGE_SCALER
* @brief RANGE scaling register
*
* Never should user write directly onto that register directly \a VL6180x_UpscaleSetScaling()
*/
#define RANGE_SCALER 0x096
/**
* @def READOUT_AVERAGING_SAMPLE_PERIOD
* @brief Readout averaging sample period register
*
*
* The internal readout averaging sample period can be adjusted from 0 to 255.
* Increasing the sampling period decreases noise but also reduces the effective
* max convergence time and increases power consumption
* Each unit sample period corresponds to around 64.5 μs additional processing time.
* The recommended setting is 48 which equates to around 4.3 ms.
*
* see datasheet for more detail
*/
#define READOUT_AVERAGING_SAMPLE_PERIOD 0x10A
/**
* @def I2C_SLAVE_DEVICE_ADDRESS
* User programmable I2C address (7-bit). Device address can be re-designated after power-up.
* @warning What programmed in the register 7-0 are bit 8-1 of i2c address on bus (bit 0 is rd/wr)
* so what prohamd is commonly whar ergfer as adrerss /2
* @sa VL6180x_SetI2CAddress()
*/
#define I2C_SLAVE_DEVICE_ADDRESS 0x212
#endif /* _VL6180x_DEF */

166
Core/Lib/Inc/vl6180x_i2c.h Normal file
View File

@ -0,0 +1,166 @@
/*
* $Date: 2015-01-08 14:30:24 +0100 (Thu, 08 Jan 2015) $
* $Revision: 2039 $
*/
/**
* @file vl6180x_i2c.h
*
* @brief CCI interface to "raw i2c" translation layer
*/
#ifndef VL6180_I2C_H_
#define VL6180_I2C_H_
#include "vl6180x_platform.h"
/**
* @defgroup cci_i2c CCI to RAW I2C translation layer
*
* This optional tranlation layer is implemented in __platform/cci-i2c__ directory. If user uses this translation layer for his platform, only @a VL6180x_I2CRead() and
* @a VL6180x_I2CWrite() functions need to be implemented. Also, some code adaption (via macro) is required for multi-threading and for multiple device support.
*
* File vl6180x_i2c.c implements device register access via raw i2c access. If the targeted application and platform has no multi-thread, no multi-cpu and uses single
* device, then nothing else is required than the 2 mandatory function : @a VL6180x_I2CRead() and @a VL6180x_I2CWrite().\n
* In other cases, review and customize @a VL6180x_GetI2CAccess() and @a VL6180x_DoneI2CAccess() functions as well as @a #VL6180x_I2C_USER_VAR macro. This should be enough
* to conform to a wide range of platform OS and application requirements .\n
*
* If your configured i2c for per device buffer via @a #I2C_BUFFER_CONFIG == 2, you must implement @a VL6180x_GetI2cBuffer()
*
* __I2C Port sample__ \n
* A __linux kernel__ port need a "long flags" var for its spin_lock in all functions. the following code example declares a spin lock "lock" in the custom device structure. \n
* @code
struct MyVL6180Dev_t {
struct VL6180xDevData_t StData;
...
spinlock_t i2c_lock;
};
typedef struct MyVL6180Dev_t *VL6180xDev_t;
#define VL6180x_I2C_USER_VAR unsigned long flags;
#define GetI2CAccess(dev) spin_lock_irqsave(dev->i2c_lock, flags)
#define DoneI2CAccess(dev) spin_unlock_irqrestore(dev->i2c_lock,flags)
@endcode
* __POSIX pthread__ application porting could be as follows :\n
* @code
struct MyVL6180Dev_t {
struct VL6180xDevData_t StData;
...
pthread_mutex_t *lock;
};
typedef struct MyVL6180Dev_t *VL6180xDev_t;
#define VL6180x_I2C_USER_VAR //no need
#define VL6180x_GetI2CAccess(dev) pthread_mutex_lock(dev->lock)
#define VL6180x_DoneI2CAcces(dev) pthread_mutex_unlock(dev->lock)
* @endcode
*/
/**
* @def I2C_BUFFER_CONFIG
*
* @brief Configure device register I2C access
*
* @li 0 : one GLOBAL buffer \n
* Use one global buffer of MAX_I2C_XFER_SIZE byte in data space \n
* This solution is not multi-device compliant nor multi-thread cpu safe \n
* It can be the best option for small 8/16 bit MCU without stack and limited ram (STM8s, 80C51 ...)
*
* @li 1 : ON_STACK/local \n
* Use local variable (on stack) buffer \n
* This solution is multi-thread with use of i2c resource lock or mutex see @a VL6180x_GetI2CAccess() \n
*
* @li 2 : User defined \n
* Per device potentially dynamic allocated. Requires @a VL6180x_GetI2cBuffer() to be implemented.
* @ingroup Configuration
*/
#define I2C_BUFFER_CONFIG 1
/**
* @brief Write data buffer to VL6180x device via i2c
* @param dev The device to write to
* @param buff The data buffer
* @param len The length of the transaction in byte
* @return 0 on success
* @ingroup cci_i2c
*/
int VL6180x_I2CWrite(VL6180xDev_t dev, uint8_t *buff, uint8_t len);
/**
*
* @brief Read data buffer from VL6180x device via i2c
* @param dev The device to read from
* @param buff The data buffer to fill
* @param len The length of the transaction in byte
* @return 0 on success
* @ingroup cci_i2c
*/
int VL6180x_I2CRead(VL6180xDev_t dev, uint8_t *buff, uint8_t len);
/**
* @brief Declare any required variables used by i2c lock (@a VL6180x_DoneI2CAccess() and @a VL6180x_GetI2CAccess())
* and buffer access : @a VL6180x_GetI2cBuffer()
*
* @ingroup cci_i2c
*/
#define VL6180x_I2C_USER_VAR
/**
* @brief Acquire lock or mutex for access to i2c data buffer and bus.\n
* Delete the default VL6180x_GetI2CAccess 'do-nothing' macro below if you decide to implement this function.
*
* This function is used to perform i2c bus level and multiple access locking required for multi thread/proccess system.\n
* Multiple access (read and update) will lock once and do multiple basic i2c rd/wr to complete the overall transfer.\n
* When no locking is needed this can be a void macro.\n
*
* @param dev the device
* @ingroup cci_i2c
*/
void VL6180x_GetI2CAccess(VL6180xDev_t dev);
/**
* @def VL6180x_GetI2CAccess
* @brief Default 'do-nothing' macro for @a VL6180x_GetI2CAccess(). Delete if used.
* @ingroup cci_i2c
*/
#define VL6180x_GetI2CAccess(dev) (void)0 /* TODO delete if function used */
/**
* @brief Release acquired lock or mutex for i2c access.\n
* Delete default VL6180x_DoneI2CAccess 'do-nothing' macro below if implementing that function.
*
* This function is used to release the acquired lock.
* @param dev The device
* @ingroup cci_i2c
*/
void VL6180x_DoneI2CAccess(VL6180xDev_t dev);
/** @def VL6180x_DoneI2CAcces
* @brief Default 'do-nothing' macro for @a VL6180x_DoneI2CAcces(). Delete if used.
* @ingroup cci_i2c
*/
#define VL6180x_DoneI2CAcces(dev) (void)0 /*TODO delete if function used */
/**
* @brief Provided data buffer for i2c access for at least n_byte.
*
* You must implement it when i2c @a #I2C_BUFFER_CONFIG is set to 2 (User defined).\n
* This is used used in the context of #VL6180x_I2C_USER_VAR
*
* @param dev The device
* @param n_byte Minimal number of byte
* @return The buffer (cannot fail return not checked)
* @ingroup cci_i2c
*/
uint8_t *VL6180x_GetI2cBuffer(VL6180xDev_t dev, int n_byte);
#if I2C_BUFFER_CONFIG == 2
#error /* TODO add your macro of code here for VL6180x_GetI2cBuffer */
#endif
#endif /* VL6180_I2C_H_ */

View File

@ -0,0 +1,312 @@
/*******************************************************************************
Copyright © 2014, STMicroelectronics International N.V.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of STMicroelectronics nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
NON-INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS ARE DISCLAIMED.
IN NO EVENT SHALL STMICROELECTRONICS INTERNATIONAL N.V. BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
********************************************************************************/
/*
* $Date: 2015-07-07 17:33:18 +0200 (Tue, 07 Jul 2015) $
* $Revision: 2441 $
*/
#ifndef VL6180x_PLATFORM
#define VL6180x_PLATFORM
/* this is a typical ansi and posix example with multithread and i2c lock concern */
#include <unistd.h>
#include <pthread.h>
/**
* @file vl6180x_platform.h
*
* @brief All end user OS/platform/application porting
*/
/** @defgroup api_platform Platform
* @brief Platform-dependent code
*/
/** @ingroup api_platform
* @{*/
/**
* @brief For user convenience to place or give any required data attribute
* to the built-in single device instance \n
* Useful only when Configuration @a #VL6180x_SINGLE_DEVICE_DRIVER is active
*
* @ingroup api_platform
*/
#define VL6180x_DEV_DATA_ATTR
/**
* @def ROMABLE_DATA
* @brief API Read only data that can be place in rom/flash are declared with that extra keyword
*
* For user convenience, use compiler specific attribute or keyword to place all read-only data in required data area. \n
* For example using gcc section :
* @code
* #define ROMABLE_DATA __attribute__ ((section ("user_rom")))
* // you may need to edit your link script file to place user_rom section in flash/rom memory
* @endcode
*
* @ingroup api_platform
*/
#define ROMABLE_DATA
/* #define ROMABLE_DATA __attribute__ ((section ("user_rom"))) */
/**
* @def VL6180x_RANGE_STATUS_ERRSTRING
* @brief Activate error code translation into string.
* TODO: michel to apdate
* @ingroup api_platform
*/
#define VL6180x_RANGE_STATUS_ERRSTRING 1
/**
* @def VL6180x_SINGLE_DEVICE_DRIVER
* @brief Enable lightweight single vl6180x device driver.
*
* Value __1__ => Single device capable.
* Configure optimized API for single device driver with static data and minimal use of ref pointer. \n
* Limited to single device driver or application in non multi thread/core environment. \n
*
* Value __0__ => Multiple device capable. User must review "device" structure and type in vl6180x_platform.h files.
* @ingroup api_platform
*/
#define VL6180x_SINGLE_DEVICE_DRIVER 1
/**
* @def VL6180X_SAFE_POLLING_ENTER
* @brief Ensure safe polling method when set
*
* Polling for a condition can be hazardous and result in infinite looping if any previous interrupt status
* condition is not cleared. \n
* Setting these flags enforce error clearing on start of polling method to avoid it.
* the drawback are : \n
* @li extra use-less i2c bus usage and traffic
* @li potentially slower measure rate.
* If application ensures interrupt get cleared on mode or interrupt configuration change
* then keep option disabled. \n
* To be safe set these option to 1
* @ingroup api_platform
*/
#define VL6180X_SAFE_POLLING_ENTER 0
/**
* @def VL6180x_HAVE_MULTI_READ
* @brief Enable I2C multi read support
*
* When set to 1, multi read operations are done (when necessary) by the API functions (mainly WAF) to access a bunch of registers
* instead of individual ones (for speed increase). This requires the @a VL6180x_RdMulti() function to be implemented.
*/
#define VL6180x_HAVE_MULTI_READ 1
/**
* @def VL6180x_CACHED_REG
* @brief Enable Cached Register mode
*
* In addition to the multi read mode (#VL6180x_HAVE_MULTI_READ set to 1), this mode implements an advanced register access mode to speed-up
* ranging measurements by reading all results registers in one shot (using multi read operation). All post-processing operations (like WAF)
* are done by accessing the cached registers instead of doing individual register access.
* @warning It is mandatory to set #VL6180x_HAVE_MULTI_READ to 1 to benefit from this advanced mode
*/
#define VL6180x_CACHED_REG 1
/**
* @brief Enable start/end logging facilities. It can generates traces log to help problem tracking analysis and solving
*
* Requires porting @a #LOG_FUNCTION_START, @a #LOG_FUNCTION_END, @a #LOG_FUNCTION_END_FMT
* @ingroup api_platform
*/
#define VL6180X_LOG_ENABLE 0
#include "vl6180x_def.h"
//TODO: modify this
#if VL6180x_SINGLE_DEVICE_DRIVER
/**
* @typedef VL6180xDev_t
* @brief Generic VL6180x device type that does link between API and platform abstraction layer
*
* @ingroup api_platform
*/
/* //![device_type_int] */
typedef uint8_t VL6180xDev_t; /* simplest single device example "dev" is the i2c device address in the platform AL*/
/* //![device_type_int] */
#else /* VL6180x_SINGLE_DEVICE_DRIVER */
/*! [device_type_multi] */
struct MyDev_t {
struct VL6180xDevData_t Data; /*!< embed ST VL6180 Dev data as "Data"*/
//TODO ADD HERE any extra device data
/*!< user specific field */
int i2c_bus_num; /*!< i2c bus number user specific field */
int i2c_dev_addr; /*!< i2c devcie address user specific field */
mutex_t dev_lock ; /*!< mutex user specific field */
int i2c_file; /*!< sample i2c file handle */
};
typedef struct MyDev_t *VL6180xDev_t;
/**
* @def VL6180xDevDataGet
* @brief Get ST private structure @a VL6180xDevData_t data access (needed only in multi devices configuration)
*
* It may be used and a real data "ref" not just as "get" for sub-structure item
* like VL6180xDevDataGet(FilterData.field)[i] or VL6180xDevDataGet(FilterData.MeasurementIndex)++
* @param dev The device
* @param field ST structure filed name
* @ingroup api_platform
*/
#define VL6180xDevDataGet(dev, field) (dev->Data.field)
/**
* @def VL6180xDevDataSet(dev, field, data)
* @brief Set ST private structure @a VL6180xDevData_t data field (needed only in multi devices configuration)
* @param dev The device
* @param field ST structure field name
* @param data Data to be set
* @ingroup api_platform
*/
#define VL6180xDevDataSet(dev, field, data) (dev->Data.field)=(data)
/*! [device_type_multi] */
#endif /* #else VL6180x_SINGLE_DEVICE_DRIVER */
/**
* @brief execute delay in all polling api calls : @a VL6180x_RangePollMeasurement() and @a VL6180x_AlsPollMeasurement()
*
* A typical multi-thread or RTOs implementation is to sleep the task for some 5ms (with 100Hz max rate faster polling is not needed).
* if nothing specific is needed, you can define it as an empty/void macro
* @code
* #define VL6180x_PollDelay(...) (void)0
* @endcode
* @param dev The device
* @ingroup api_platform
*/
void VL6180x_PollDelay(VL6180xDev_t dev); /* usualy best implemanted a a real fucntion */
/** @def VL6180x_PollDelay
* @brief Default value : does nothing. Macro to be deleted it you implement a real function
* @ingroup api_platform
*/
#define VL6180x_PollDelay(dev) (void)0
#if VL6180X_LOG_ENABLE
#include <sys/time.h>
#include <stdio.h>
extern FILE * log_file;
#define trace_printf fprinf
#define LOG_GET_TIME() clock()
/**
* @brief Log function start.
*
* @param fmt Text and printf formating for any extra argument.\n
* It can be " " when there is nothing else than the function entry to log.
* @param ... (\__VA_ARGS__) extra argument if any, but it can be none.
*
* @note Formatting a string can take considerable amount of cycles. Typically "sprinting" 4 int arguments and function name take 5-10K on common 32 bit µC\n
* It depends on implementation. The tracing is very intrusive and resources consuming .
*
* Example of time logging using printf and LOG_GET_TIME() macro \n
* @code
* #define LOG_FUNCTION_START(fmt, ... ) printf("beg %s @%d\t" fmt "\n", __func__, LOG_GET_TIME(), ##__VA_ARGS__)
* @endcode
* @ingroup api_platform
*/
#define LOG_FUNCTION_START(fmt, ... ) \
fprintf(log_file, "VL61080 beg %s start @%d\t" fmt "\n", __FUNCTION__, LOG_GET_TIME(), ##__VA_ARGS__)
/**
* @brief Logging function end with status.
*
* @param status final status (%d)
*
* Example of time logging using printf and LOG_GET_TIME() macro \n
* @code
* #define LOG_FUNCTION_END(status) printf("end %s @%d %d\n", __func__, LOG_GET_TIME(), (int)status)
* @endcode
* @ingroup api_platform
*/
#define LOG_FUNCTION_END(status)\
fprintf(log_file, "VL61080 end %s @%d %d\n", __FUNCTION__, LOG_GET_TIME(), (int)status)
/**
* @brief Log function end along with extra optional formated arguments.
*
* @param status final status (%d)
* @param fmt printf format and string
* @param ... (\__VA_ARGS__) extra argument for printf or va_args type function
*
* Example of time logging using printf and LOG_GET_TIME() macro. \n
* @code
* #define LOG_FUNCTION_END_FMT(status, fmt, ... ) printf("End %s @%d %d\t"fmt"\n" , __func__, LOG_GET_TIME(), (int)status,##__VA_ARGS__)
* @endcode
* @ingroup api_platform
*/
#define LOG_FUNCTION_END_FMT(status, fmt, ... )\
fprintf(log_file, "End %s @%d %d\t"fmt"\n" , __FUNCTION__, LOG_GET_TIME(), (int)status,##__VA_ARGS__)
/**
* @brief Log error raised in API
*
* Implementation may abort execution but it's not an API requirement
*
* @param fmt text and printf like format list
* @param ... (\__VA_ARGS__) optional variable to be formated
*
* Example of error logging using printf and LOG_GET_TIME() macro \n
* @code
* #define VL6180x_ErrLog(msg, ... ) printf("Err in %s line %d @%dt" msg "\n" , __func__, __LINE__, LOG_GET_TIME(), msg ,##__VA_ARGS__)
* @endcode
* @ingroup api_platform
*/
#define VL6180x_ErrLog( fmt, ...) fprintf(stderr, "VL6180x_ErrLog %s" fmt "\n", __func__, ##__VA_ARGS__)
#else /* VL6180X_LOG_ENABLE no logging */
#define LOG_FUNCTION_START(...) (void)0
#define LOG_FUNCTION_END(...) (void)0
#define LOG_FUNCTION_END_FMT(...) (void)0
#define VL6180x_ErrLog(... ) (void)0
#endif /* else */
#endif /* VL6180x_PLATFORM */

View File

@ -0,0 +1,104 @@
/*******************************************************************************
Copyright © 2014, STMicroelectronics International N.V.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of STMicroelectronics nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
NON-INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS ARE DISCLAIMED.
IN NO EVENT SHALL STMICROELECTRONICS INTERNATIONAL N.V. BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
********************************************************************************/
/*
* $Date: 2015-01-08 14:30:24 +0100 (Thu, 08 Jan 2015) $
* $Revision: 2039 $
*/
/**
* @file vl6180x_types.h
* @brief vl6180x types definition
*/
#ifndef VL6180x_TYPES_H_
#define VL6180x_TYPES_H_
/** @defgroup porting_type Basic type definition
* @ingroup porting
*
* @brief file vl6180x_types.h file holds basic type definitions that may require porting
*
* Contains type that must be defined for the platform\n
* When target platform and compiler provide stdint.h and stddef.h, it is enough to include it.\n
* If stdint.h is not available review and adapt all signed and unsigned 8/16/32 bits basic types. \n
* If stddef.h is not available review and adapt NULL definition .
*/
#include <stdint.h>
#include <stddef.h>
#ifndef NULL
#error "TODO review NULL definition or add required include "
#endif
#if ! defined(STDINT_H) && !defined(_GCC_STDINT_H) &&!defined(__STDINT_DECLS) && !defined(_GCC_WRAP_STDINT_H)
#pragma message("Please review type definition of STDINT define for your platform and add to list above ")
/*
* target platform do not provide stdint or use a different #define than above
* to avoid seeing the message below addapt the #define list above or implement
* all type and delete these pragma
*/
/** \ingroup porting_type
* @{
*/
/** @brief Typedef defining 32 bit unsigned int type.\n
* The developer should modify this to suit the platform being deployed.
*/
typedef unsigned int uint32_t;
/** @brief Typedef defining 32 bit int type.\n
* The developer should modify this to suit the platform being deployed.
*/
typedef int int32_t;
/** @brief Typedef defining 16 bit unsigned short type.\n
* The developer should modify this to suit the platform being deployed.
*/
typedef unsigned short uint16_t;
/** @brief Typedef defining 16 bit short type.\n
* The developer should modify this to suit the platform being deployed.
*/
typedef short int16_t;
/** @brief Typedef defining 8 bit unsigned char type.\n
* The developer should modify this to suit the platform being deployed.
*/
typedef unsigned char uint8_t;
/** @brief Typedef defining 8 bit char type.\n
* The developer should modify this to suit the platform being deployed.
*/
typedef signed char int8_t;
/** @} */
#endif /* _STDINT_H */
#endif /* VL6180x_TYPES_H_ */

2761
Core/Lib/Src/vl6180x_api.c Normal file

File diff suppressed because it is too large Load Diff

242
Core/Lib/Src/vl6180x_i2c.c Normal file
View File

@ -0,0 +1,242 @@
/*******************************************************************************
Copyright <EFBFBD> 2014, STMicroelectronics International N.V.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of STMicroelectronics nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
NON-INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS ARE DISCLAIMED.
IN NO EVENT SHALL STMICROELECTRONICS INTERNATIONAL N.V. BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
********************************************************************************/
/*
* $Date: 2015-07-06 15:44:31 +0200 (Mon, 06 Jul 2015) $
* $Revision: 2430 $
*/
/**
* @file vl6180x_i2c.c
*
* Copyright (C) 2014 ST MicroElectronics
*
* provide variable word size byte/Word/dword VL6180x register access via i2c
*
*/
#include "vl6180x_i2c.h"
#ifndef I2C_BUFFER_CONFIG
#error "I2C_BUFFER_CONFIG not defined"
/* TODO you must define value for I2C_BUFFER_CONFIG in configuration or
* platform h */
#endif
#if I2C_BUFFER_CONFIG == 0
/* GLOBAL config buffer */
uint8_t i2c_global_buffer[VL6180x_MAX_I2C_XFER_SIZE];
#define DECL_I2C_BUFFER
#define VL6180x_GetI2cBuffer(dev, n_byte) i2c_global_buffer
#elif I2C_BUFFER_CONFIG == 1
/* ON STACK */
#define DECL_I2C_BUFFER uint8_t LocBuffer[VL6180x_MAX_I2C_XFER_SIZE];
#define VL6180x_GetI2cBuffer(dev, n_byte) LocBuffer
#elif I2C_BUFFER_CONFIG == 2
/* user define buffer type declare DECL_I2C_BUFFER as access via
* VL6180x_GetI2cBuffer */
#define DECL_I2C_BUFFER
#else
#error "invalid I2C_BUFFER_CONFIG "
#endif
int VL6180x_WrByte(VL6180xDev_t dev, uint16_t index, uint8_t data) {
int status;
uint8_t *buffer;
DECL_I2C_BUFFER
VL6180x_I2C_USER_VAR
VL6180x_GetI2CAccess(dev);
buffer = VL6180x_GetI2cBuffer(dev, 3);
buffer[0] = index >> 8;
buffer[1] = index & 0xFF;
buffer[2] = data;
status = VL6180x_I2CWrite(dev, buffer, (uint8_t)3);
VL6180x_DoneI2CAcces(dev);
return status;
}
int VL6180x_WrWord(VL6180xDev_t dev, uint16_t index, uint16_t data) {
int status;
DECL_I2C_BUFFER
uint8_t *buffer;
VL6180x_I2C_USER_VAR
VL6180x_GetI2CAccess(dev);
buffer = VL6180x_GetI2cBuffer(dev, 4);
buffer[0] = index >> 8;
buffer[1] = index & 0xFF;
buffer[2] = data >> 8;
buffer[3] = data & 0xFF;
status = VL6180x_I2CWrite(dev, buffer, (uint8_t)4);
VL6180x_DoneI2CAcces(dev);
return status;
}
int VL6180x_WrDWord(VL6180xDev_t dev, uint16_t index, uint32_t data) {
VL6180x_I2C_USER_VAR DECL_I2C_BUFFER int status;
uint8_t *buffer;
VL6180x_GetI2CAccess(dev);
buffer = VL6180x_GetI2cBuffer(dev, 6);
buffer[0] = index >> 8;
buffer[1] = index & 0xFF;
buffer[2] = data >> 24;
buffer[3] = (data >> 16) & 0xFF;
buffer[4] = (data >> 8) & 0xFF;
;
buffer[5] = data & 0xFF;
status = VL6180x_I2CWrite(dev, buffer, (uint8_t)6);
VL6180x_DoneI2CAcces(dev);
return status;
}
int VL6180x_UpdateByte(VL6180xDev_t dev, uint16_t index, uint8_t AndData,
uint8_t OrData) {
VL6180x_I2C_USER_VAR int status;
uint8_t *buffer;
DECL_I2C_BUFFER
VL6180x_GetI2CAccess(dev);
buffer = VL6180x_GetI2cBuffer(dev, 3);
buffer[0] = index >> 8;
buffer[1] = index & 0xFF;
status = VL6180x_I2CWrite(dev, (uint8_t *)buffer, (uint8_t)2);
if (!status) {
/* read data direct onto buffer */
status = VL6180x_I2CRead(dev, &buffer[2], 1);
if (!status) {
buffer[2] = (buffer[2] & AndData) | OrData;
status = VL6180x_I2CWrite(dev, buffer, (uint8_t)3);
}
}
VL6180x_DoneI2CAcces(dev);
return status;
}
int VL6180x_RdByte(VL6180xDev_t dev, uint16_t index, uint8_t *data) {
VL6180x_I2C_USER_VAR int status;
uint8_t *buffer;
DECL_I2C_BUFFER
VL6180x_GetI2CAccess(dev);
buffer = VL6180x_GetI2cBuffer(dev, 2);
buffer[0] = index >> 8;
buffer[1] = index & 0xFF;
status = VL6180x_I2CWrite(dev, buffer, (uint8_t)2);
if (!status) {
status = VL6180x_I2CRead(dev, buffer, 1);
if (!status) {
*data = buffer[0];
}
}
VL6180x_DoneI2CAcces(dev);
return status;
}
int VL6180x_RdWord(VL6180xDev_t dev, uint16_t index, uint16_t *data) {
VL6180x_I2C_USER_VAR int status;
uint8_t *buffer;
DECL_I2C_BUFFER
VL6180x_GetI2CAccess(dev);
buffer = VL6180x_GetI2cBuffer(dev, 2);
buffer[0] = index >> 8;
buffer[1] = index & 0xFF;
status = VL6180x_I2CWrite(dev, buffer, (uint8_t)2);
if (!status) {
status = VL6180x_I2CRead(dev, buffer, 2);
if (!status) {
/* VL6180x register are Big endian if cpu is be direct read direct into
* *data is possible */
*data = ((uint16_t)buffer[0] << 8) | (uint16_t)buffer[1];
}
}
VL6180x_DoneI2CAcces(dev);
return status;
}
int VL6180x_RdDWord(VL6180xDev_t dev, uint16_t index, uint32_t *data) {
VL6180x_I2C_USER_VAR int status;
uint8_t *buffer;
DECL_I2C_BUFFER
VL6180x_GetI2CAccess(dev);
buffer = VL6180x_GetI2cBuffer(dev, 4);
buffer[0] = index >> 8;
buffer[1] = index & 0xFF;
status = VL6180x_I2CWrite(dev, (uint8_t *)buffer, (uint8_t)2);
if (!status) {
status = VL6180x_I2CRead(dev, buffer, 4);
if (!status) {
/* VL6180x register are Big endian if cpu is be direct read direct into
* data is possible */
*data = ((uint32_t)buffer[0] << 24) | ((uint32_t)buffer[1] << 16) |
((uint32_t)buffer[2] << 8) | ((uint32_t)buffer[3]);
}
}
VL6180x_DoneI2CAcces(dev);
return status;
}
int VL6180x_RdMulti(VL6180xDev_t dev, uint16_t index, uint8_t *data,
int nData) {
VL6180x_I2C_USER_VAR int status;
uint8_t *buffer;
DECL_I2C_BUFFER
VL6180x_GetI2CAccess(dev);
buffer = VL6180x_GetI2cBuffer(dev, 2);
buffer[0] = index >> 8;
buffer[1] = index & 0xFF;
status = VL6180x_I2CWrite(dev, (uint8_t *)buffer, (uint8_t)2);
if (!status) {
status = VL6180x_I2CRead(dev, data, nData);
}
VL6180x_DoneI2CAcces(dev);
return status;
}

View File

@ -18,11 +18,12 @@
/* USER CODE END Header */ /* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/ /* Includes ------------------------------------------------------------------*/
#include "main.h" #include "main.h"
#include "stm32l4xx_hal.h"
#include "stm32l4xx_hal_i2c.h"
/* Private includes ----------------------------------------------------------*/ /* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */ /* USER CODE BEGIN Includes */
#include "stm32l4xx_hal.h"
#include "vl6180x_api.h"
#include "vl6180x_glue.h"
/* USER CODE END Includes */ /* USER CODE END Includes */
@ -70,6 +71,7 @@ static void MX_USART2_UART_Init(void);
/* USER CODE BEGIN 0 */ /* USER CODE BEGIN 0 */
volatile uint16_t range = 0; volatile uint16_t range = 0;
#if false
void tof_write_reg(uint16_t reg_addr, uint32_t data, int len_bytes) { void tof_write_reg(uint16_t reg_addr, uint32_t data, int len_bytes) {
uint8_t buf[len_bytes + 2]; uint8_t buf[len_bytes + 2];
buf[0] = reg_addr >> 8; buf[0] = reg_addr >> 8;
@ -170,12 +172,18 @@ void tof_init(void) {
// Ready threshold event // Ready threshold event
tof_write_reg(VL6180X_REG_SYSTEM_FRESH_OUT_OF_RESET, 0, 1); tof_write_reg(VL6180X_REG_SYSTEM_FRESH_OUT_OF_RESET, 0, 1);
HAL_Delay(10);
if (tof_read_reg(VL6180X_REG_SYSTEM_FRESH_OUT_OF_RESET, 1) & 1) {
Error_Handler();
}
} }
// tof_write_reg(VL6180X_REG_SYSTEM_INTERRUPT_CONFIG_GPIO, 4, 1); // tof_write_reg(VL6180X_REG_SYSTEM_INTERRUPT_CONFIG_GPIO, 4, 1);
} }
uint8_t tof_range_poll(void) { uint8_t tof_range_poll(void) {
if (tof_read_reg(VL6180X_REG_RESULT_RANGE_STATUS, 1) & 1) { uint8_t status = tof_read_reg(VL6180X_REG_RESULT_RANGE_STATUS, 1);
uint8_t fresh = tof_read_reg(VL6180X_REG_SYSTEM_FRESH_OUT_OF_RESET, 1);
if (!(status & 1) || (fresh & 1)) {
// Not ready // Not ready
Error_Handler(); Error_Handler();
} }
@ -205,6 +213,7 @@ uint8_t tof_range_poll(void) {
return range; return range;
} }
#endif
/* USER CODE END 0 */ /* USER CODE END 0 */
/** /**
@ -237,19 +246,28 @@ int main(void) {
MX_GPIO_Init(); MX_GPIO_Init();
MX_I2C1_Init(); MX_I2C1_Init();
MX_USART2_UART_Init(); MX_USART2_UART_Init();
HAL_Delay(100);
tof_init();
HAL_Delay(100);
/* USER CODE BEGIN 2 */ /* USER CODE BEGIN 2 */
HAL_Delay(10);
vl6180x_glue_init(&hi2c1);
VL6180xDev_t my_dev;
VL6180x_RangeData_t my_range;
VL6180x_InitData(my_dev);
VL6180x_Prepare(my_dev);
/* USER CODE END 2 */ /* USER CODE END 2 */
/* Infinite loop */ /* Infinite loop */
/* USER CODE BEGIN WHILE */ /* USER CODE BEGIN WHILE */
while (1) { while (1) {
/* USER CODE END WHILE */ /* USER CODE END WHILE */
/* USER CODE BEGIN 3 */ /* USER CODE BEGIN 3 */
range = tof_range_poll(); VL6180x_RangePollMeasurement(my_dev, &my_range);
if (my_range.errorStatus == 0) {
range = my_range.range_mm;
} else {
HAL_Delay(100);
}
} }
/* USER CODE END 3 */ /* USER CODE END 3 */
} }

25
Core/Src/vl6180x_glue.c Normal file
View File

@ -0,0 +1,25 @@
#include "main.h"
#include "stm32l4xx_hal_i2c.h"
#include "vl6180x_i2c.h"
#define VL6180X_ADDR (0x29 << 1)
I2C_HandleTypeDef *handle;
void vl6180x_glue_init(I2C_HandleTypeDef *handle_i2c) { handle = handle_i2c; }
int VL6180x_I2CWrite(VL6180xDev_t dev, uint8_t *buff, uint8_t len) {
if (HAL_I2C_Master_Transmit(handle, VL6180X_ADDR, buff, len, 100) != HAL_OK) {
Error_Handler();
}
return 0;
}
int VL6180x_I2CRead(VL6180xDev_t dev, uint8_t *buff, uint8_t len) {
if (HAL_I2C_Master_Receive(handle, VL6180X_ADDR | 1, buff, len, 100) != 0) {
Error_Handler();
}
return 0;
}

View File

@ -1,5 +1,5 @@
########################################################################################################################## ##########################################################################################################################
# File automatically-generated by tool: [projectgenerator] version: [3.17.1] date: [Sat Nov 12 12:53:14 CET 2022] # File automatically-generated by tool: [projectgenerator] version: [3.17.1] date: [Sat Nov 12 16:28:05 CET 2022]
########################################################################################################################## ##########################################################################################################################
# ------------------------------------------------ # ------------------------------------------------
@ -58,7 +58,7 @@ Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_tim.c \
Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_tim_ex.c \ Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_tim_ex.c \
Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_uart.c \ Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_uart.c \
Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_uart_ex.c \ Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_uart_ex.c \
Core/Src/system_stm32l4xx.c Core/Src/system_stm32l4xx.c
# ASM sources # ASM sources
ASM_SOURCES = \ ASM_SOURCES = \
@ -191,4 +191,4 @@ clean:
####################################### #######################################
-include $(wildcard $(BUILD_DIR)/*.d) -include $(wildcard $(BUILD_DIR)/*.d)
# *** EOF *** # *** EOF ***

View File

@ -36,10 +36,13 @@ BUILD_DIR = build
###################################### ######################################
# C sources # C sources
C_SOURCES = \ C_SOURCES = \
Core/Lib/Src/vl6180x_api.c \
Core/Lib/Src/vl6180x_i2c.c \
Core/Src/main.c \ Core/Src/main.c \
Core/Src/stm32l4xx_hal_msp.c \ Core/Src/stm32l4xx_hal_msp.c \
Core/Src/stm32l4xx_it.c \ Core/Src/stm32l4xx_it.c \
Core/Src/system_stm32l4xx.c \ Core/Src/system_stm32l4xx.c \
Core/Src/vl6180x_glue.c \
Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal.c \ Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal.c \
Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_cortex.c \ Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_cortex.c \
Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_dma.c \ Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_dma.c \
@ -131,6 +134,7 @@ AS_INCLUDES = \
# C includes # C includes
C_INCLUDES = \ C_INCLUDES = \
-ICore/Inc \ -ICore/Inc \
-ICore/Lib/Inc \
-IDrivers/CMSIS/Device/ST/STM32L4xx/Include \ -IDrivers/CMSIS/Device/ST/STM32L4xx/Include \
-IDrivers/CMSIS/Include \ -IDrivers/CMSIS/Include \
-IDrivers/STM32L4xx_HAL_Driver/Inc \ -IDrivers/STM32L4xx_HAL_Driver/Inc \