---vl6180dr

This commit is contained in:
Mysteo91
2023-07-31 10:42:03 +03:00
parent c6549ebd6f
commit 0b2cdbc8ba
24 changed files with 7378 additions and 1 deletions

View File

@@ -0,0 +1,139 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : custom_bus.h
* @brief : header file for the BSP BUS IO driver
******************************************************************************
* @attention
*
* Copyright (c) 2022 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef CUSTOM_BUS_H
#define CUSTOM_BUS_H
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/
#include "custom_conf.h"
#include "custom_errno.h"
/** @addtogroup BSP
* @{
*/
/** @addtogroup CUSTOM
* @{
*/
/** @defgroup CUSTOM_BUS CUSTOM BUS
* @{
*/
/** @defgroup CUSTOM_BUS_Exported_Constants CUSTOM BUS Exported Constants
* @{
*/
#define BUS_I2C1_INSTANCE I2C1
#define BUS_I2C1_SCL_GPIO_PORT GPIOB
#define BUS_I2C1_SCL_GPIO_AF GPIO_AF4_I2C1
#define BUS_I2C1_SCL_GPIO_CLK_ENABLE() __HAL_RCC_GPIOB_CLK_ENABLE()
#define BUS_I2C1_SCL_GPIO_CLK_DISABLE() __HAL_RCC_GPIOB_CLK_DISABLE()
#define BUS_I2C1_SCL_GPIO_PIN GPIO_PIN_8
#define BUS_I2C1_SDA_GPIO_PIN GPIO_PIN_9
#define BUS_I2C1_SDA_GPIO_CLK_DISABLE() __HAL_RCC_GPIOB_CLK_DISABLE()
#define BUS_I2C1_SDA_GPIO_PORT GPIOB
#define BUS_I2C1_SDA_GPIO_AF GPIO_AF4_I2C1
#define BUS_I2C1_SDA_GPIO_CLK_ENABLE() __HAL_RCC_GPIOB_CLK_ENABLE()
#ifndef BUS_I2C1_POLL_TIMEOUT
#define BUS_I2C1_POLL_TIMEOUT 0x1000U
#endif
/* I2C1 Frequeny in Hz */
#ifndef BUS_I2C1_FREQUENCY
#define BUS_I2C1_FREQUENCY 1000000U /* Frequency of I2Cn = 100 KHz*/
#endif
/**
* @}
*/
/** @defgroup CUSTOM_BUS_Private_Types CUSTOM BUS Private types
* @{
*/
#if (USE_HAL_I2C_REGISTER_CALLBACKS == 1U)
typedef struct
{
pI2C_CallbackTypeDef pMspInitCb;
pI2C_CallbackTypeDef pMspDeInitCb;
}BSP_I2C_Cb_t;
#endif /* (USE_HAL_I2C_REGISTER_CALLBACKS == 1U) */
/**
* @}
*/
/** @defgroup CUSTOM_LOW_LEVEL_Exported_Variables LOW LEVEL Exported Constants
* @{
*/
extern I2C_HandleTypeDef hi2c1;
/**
* @}
*/
/** @addtogroup CUSTOM_BUS_Exported_Functions
* @{
*/
/* BUS IO driver over I2C Peripheral */
HAL_StatusTypeDef MX_I2C1_Init(I2C_HandleTypeDef* hi2c);
int32_t BSP_I2C1_Init(void);
int32_t BSP_I2C1_DeInit(void);
int32_t BSP_I2C1_IsReady(uint16_t DevAddr, uint32_t Trials);
int32_t BSP_I2C1_WriteReg(uint16_t Addr, uint16_t Reg, uint8_t *pData, uint16_t Length);
int32_t BSP_I2C1_ReadReg(uint16_t Addr, uint16_t Reg, uint8_t *pData, uint16_t Length);
int32_t BSP_I2C1_WriteReg16(uint16_t Addr, uint16_t Reg, uint8_t *pData, uint16_t Length);
int32_t BSP_I2C1_ReadReg16(uint16_t Addr, uint16_t Reg, uint8_t *pData, uint16_t Length);
int32_t BSP_I2C1_Send(uint16_t DevAddr, uint8_t *pData, uint16_t Length);
int32_t BSP_I2C1_Recv(uint16_t DevAddr, uint8_t *pData, uint16_t Length);
int32_t BSP_I2C1_SendRecv(uint16_t DevAddr, uint8_t *pTxdata, uint8_t *pRxdata, uint16_t Length);
#if (USE_HAL_I2C_REGISTER_CALLBACKS == 1U)
int32_t BSP_I2C1_RegisterDefaultMspCallbacks (void);
int32_t BSP_I2C1_RegisterMspCallbacks (BSP_I2C_Cb_t *Callbacks);
#endif /* (USE_HAL_I2C_REGISTER_CALLBACKS == 1U) */
int32_t BSP_GetTick(void);
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif /* CUSTOM_BUS_H */

View File

@@ -0,0 +1,82 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : custom_conf.h
* @brief : Configuration file
******************************************************************************
* @attention
*
* Copyright (c) 2022 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef CUSTOM_CONF_H
#define CUSTOM_CONF_H
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/
/** @addtogroup BSP
* @{
*/
/** @addtogroup CUSTOM
* @{
*/
/** @defgroup CUSTOM_CONFIG Config
* @{
*/
/** @defgroup CUSTOM_CONFIG_Exported_Constants
* @{
*/
/* COM Feature define */
#define USE_BSP_COM_FEATURE 1U
/* COM define */
#define USE_COM_LOG 1U
/* IRQ priorities */
#define BSP_BUTTON_USER_IT_PRIORITY 15U
/* I2C1 Frequeny in Hz */
#define BUS_I2C1_FREQUENCY 100000U /* Frequency of I2C1 = 100 KHz*/
/* SPI1 Baud rate in bps */
#define BUS_SPI1_BAUDRATE 16000000U /* baud rate of SPIn = 16 Mbps */
/* UART1 Baud rate in bps */
#define BUS_UART1_BAUDRATE 9600U /* baud rate of UARTn = 9600 baud */
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif /* CUSTOM_CONF_H */

View File

@@ -0,0 +1,58 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : custom_errno.h
* @brief : Error Code
******************************************************************************
* @attention
*
* Copyright (c) 2022 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef CUSTOM_ERRNO_H
#define CUSTOM_ERRNO_H
#ifdef __cplusplus
extern "C" {
#endif
/* BSP Common Error codes */
#define BSP_ERROR_NONE 0
#define BSP_ERROR_NO_INIT -1
#define BSP_ERROR_WRONG_PARAM -2
#define BSP_ERROR_BUSY -3
#define BSP_ERROR_PERIPH_FAILURE -4
#define BSP_ERROR_COMPONENT_FAILURE -5
#define BSP_ERROR_UNKNOWN_FAILURE -6
#define BSP_ERROR_UNKNOWN_COMPONENT -7
#define BSP_ERROR_BUS_FAILURE -8
#define BSP_ERROR_CLOCK_FAILURE -9
#define BSP_ERROR_MSP_FAILURE -10
#define BSP_ERROR_FEATURE_NOT_SUPPORTED -11
/* BSP BUS error codes */
#define BSP_ERROR_BUS_TRANSACTION_FAILURE -100
#define BSP_ERROR_BUS_ARBITRATION_LOSS -101
#define BSP_ERROR_BUS_ACKNOWLEDGE_FAILURE -102
#define BSP_ERROR_BUS_PROTOCOL_FAILURE -103
#define BSP_ERROR_BUS_MODE_FAULT -104
#define BSP_ERROR_BUS_FRAME_ERROR -105
#define BSP_ERROR_BUS_CRC_ERROR -106
#define BSP_ERROR_BUS_DMA_FAILURE -107
#ifdef __cplusplus
}
#endif
#endif /*CUSTOM_ERRNO_H */

View File

@@ -0,0 +1,257 @@
//
// Created by Professional on 7/31/2023.
//
#include "stdint-gcc.h"
#include "vl6180_def.h"
#include "stm32g0xx_hal.h"
#include "vl6180_api.h"
#include "main.h"
#include "i2c.h"
#define max_scale 1
uint8_t changeScale = 0;
#define ALLOW_DISABLE_WAF_FROM_BLUE_BUTTON 1
#define theVL6180Dev 0x52 // what we use as "API device
#define def_i2c_time_out 100
#if VL6180_HAVE_DMAX_RANGING
#define DMaxDispTime 0 /* Set to 1000 to display Dmax during 1 sec when no target is detected */
#else
#define DMaxDispTime 0
#endif
#define OutORangeDispfTime 800
#define g_TickCnt uwTick
enum runmode_t{
RunRangePoll=0,
RunAlsPoll,
InitErr,
ScaleSwap,
WaitForReset,
AlrmStart,
AlrmRun,
FromSwitch,
};
char buffer[10];
struct state_t {
int OutofRAnge:1;
int AutoScale:1;
int FilterEn:1;
uint8_t mode;
int8_t ScaleSwapCnt;
uint8_t InitScale;
uint8_t CurAlrm;
uint8_t AlrmFired; /* just used to keep display at least min time */
}State;
uint32_t TimeStarted; /* various display and mode delay starting time */
VL6180_RangeData_t Range; /* Range measurmeent */
int alpha =(int)(0.85*(1<<16)); /* range distance running average cofs */
uint16_t range; /* range average distance */
#define AutoThreshHigh 80 /*auto scale high thresh => AutoThreshHigh * max_raneg => scale ++ */
#define AutoThreshLow 33 /*auto scale low thresh => AutoThreshHigh * max_raneg => scale ++ */
#define ErrRangeDispTime 0 /* Set to 800 ms to display error code when no target os detected */
#if ErrRangeDispTime == 0
/* supress Warning[Pe186]: pointless comparison of unsigned integer with zero */
# ifdef __ARMCC_VERSION /* ARM/KEIL */
# pragma diag_suppress 186
# endif /* _ARMCC_VERSION */
# ifdef __ICCARM__ /* IAR */
# pragma diag_suppress=Pe186
# endif /* _ARMCC_VERSION */
#endif
#define AlrmDispTime 800
#define AlarmKeepDispTime 250 /* alarm message retain time after it fires */
volatile int IntrFired=0;
extern volatile uint32_t SensorsEnabled;
void AbortErr( const char * msg ){
State.mode= WaitForReset;
}
void DoScalingSwap(int scaling){
State.mode = ScaleSwap;
TimeStarted=g_TickCnt;
}
uint8_t allowIT = 0;
void vl6180_init (void)
{
allowIT = 0;
HAL_GPIO_WritePin(CS_PROXIMITY_GPIO_Port, CS_PROXIMITY_Pin, GPIO_PIN_RESET);
HAL_Delay(200);
HAL_GPIO_WritePin(CS_PROXIMITY_GPIO_Port, CS_PROXIMITY_Pin, GPIO_PIN_SET);
HAL_Delay(200);
VL6180_WaitDeviceBooted(theVL6180Dev);
VL6180_InitData(theVL6180Dev);
allowIT = 1;
HAL_GPIO_WritePin(CS_PROXIMITY_GPIO_Port, CS_PROXIMITY_Pin, GPIO_PIN_RESET);
HAL_Delay(200);
HAL_GPIO_WritePin(CS_PROXIMITY_GPIO_Port, CS_PROXIMITY_Pin, GPIO_PIN_SET);
HAL_Delay(200);
VL6180_WaitDeviceBooted(theVL6180Dev);
VL6180_InitData(theVL6180Dev);
State.InitScale=VL6180_UpscaleGetScaling(theVL6180Dev);
State.FilterEn=VL6180_FilterGetState(theVL6180Dev);
VL6180_Prepare(theVL6180Dev);
VL6180_RangeSetMaxConvergenceTime(theVL6180Dev, 20);
VL6180_RangeSetInterMeasPeriod(theVL6180Dev, 25);
//State.InitScale=VL6180_UpscaleGetScaling(theVL6180Dev);
/* Enable Dmax calculation only if value is displayed (to save computation power) */
//VL6180_DMaxSetState(theVL6180Dev, DMaxDispTime>0);
VL6180_ClearAllInterrupt(theVL6180Dev);
VL6180_RangeStartContinuousMode(theVL6180Dev);
}
int VL6180_I2CRead(VL6180Dev_t addr, uint8_t *buff, uint8_t len){
int status;
status = HAL_I2C_Master_Receive(&hi2c2, addr, buff, len , def_i2c_time_out);
if( status ){
MX_I2C2_Init();
}
return status;
}
int VL6180_I2CWrite(VL6180Dev_t addr, uint8_t *buff, uint8_t len){
int status;
status = HAL_I2C_Master_Transmit(&hi2c2, addr, buff, len , def_i2c_time_out);
if( status ){
MX_I2C2_Init();
}
return status;
}
void RangeState(void) {
HAL_GPIO_TogglePin(LED2_GPIO_PORT, LED2_Pin);
int status;
uint16_t hlimit;
uint8_t scaling;
VL6180_RangeGetMeasurement(theVL6180Dev, &Range );
scaling = VL6180_UpscaleGetScaling(theVL6180Dev);
if (Range.errorStatus) {
AbortErr("Er r");
return;
}
hlimit = VL6180_GetUpperLimit(theVL6180Dev);
if (Range.range_mm >= (hlimit * AutoThreshHigh) / 100 && scaling < 3 && State.AutoScale) {
VL6180_UpscaleSetScaling(theVL6180Dev, scaling + 1);
}
if (Range.range_mm < (hlimit * AutoThreshLow) / 100 && scaling > 1 && State.AutoScale) {
VL6180_UpscaleSetScaling(theVL6180Dev, scaling - 1);
}
if (Range.errorStatus) {
/* no valid ranging*/
if (State.OutofRAnge) {
#if VL6180_HAVE_DMAX_RANGING
if (g_TickCnt - TimeStarted >= ErrRangeDispTime && g_TickCnt - TimeStarted < ErrRangeDispTime + DMaxDispTime ){
sprintf(buffer, "d%3d", (int)Range.DMax);
}
else
#endif
if(g_TickCnt - TimeStarted < ErrRangeDispTime )
{
sprintf(buffer, "rE%2d", (int) Range.errorStatus);
}
else{
State.OutofRAnge=0; /* back to out of range display */
TimeStarted=g_TickCnt;
}
}
else {
int FilterEn;
#if VL6180_WRAP_AROUND_FILTER_SUPPORT
FilterEn = VL6180_FilterGetState(theVL6180Dev);
#else
#endif
if( g_TickCnt - TimeStarted > OutORangeDispfTime ) {
State.OutofRAnge = 1;
TimeStarted = g_TickCnt;
}
}
}
else {
State.OutofRAnge = 0;
TimeStarted = g_TickCnt;
range = (range * alpha + Range.range_mm * ((1 << 16) - alpha)) >> 16;
sprintf(buffer, "r%3d", (int) range);
if (State.AutoScale) {
if (scaling == 1) {
buffer[0] = '_';
}
else
if (scaling == 2)
buffer[0] = '=';
else
buffer[0] = '~';
}
}
if (changeScale == 1) {
changeScale = 0;
TimeStarted = g_TickCnt;
State.ScaleSwapCnt++;
if (State.ScaleSwapCnt % (max_scale + 1) == max_scale) {
State.AutoScale = 1;
scaling = max_scale;
}
else {
#if ALLOW_DISABLE_WAF_FROM_BLUE_BUTTON
/* togle filtering every time we roll over all scaling(pass by autoscale) */
if (State.AutoScale)
State.FilterEn = !State.FilterEn;
#endif
State.AutoScale = 0;
scaling = State.InitScale + (State.ScaleSwapCnt % max_scale);
if (scaling > max_scale)
scaling = scaling - (max_scale);
}
status = VL6180_UpscaleSetScaling(theVL6180Dev, scaling);
if (status<0) {
AbortErr("ErUp");
State.mode = InitErr;
}
else {
/* do not check status may fail when filter support not active */
VL6180_FilterSetState(theVL6180Dev, State.FilterEn);
DoScalingSwap(scaling);
}
}
}
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
if (GPIO_Pin == GPIO_PIN_0)
{
RangeState();
if (Range.errorStatus == 0)
{
__NOP();
}
VL6180_ClearAllInterrupt(theVL6180Dev);
}
else
{
}
}
/* USER CODE END 4 */

View File

@@ -0,0 +1,9 @@
//
// Created by Professional on 7/31/2023.
//
#ifndef MYPROJECT_VL6180_APP_H
#define MYPROJECT_VL6180_APP_H
#endif //MYPROJECT_VL6180_APP_H
void vl6180_init (void);

View File

@@ -0,0 +1,92 @@
/*******************************************************************************
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.
********************************************************************************/
/*
* vl6180_appcfg.h
*
*/
#ifndef VL6180_APPCFG_H_
#define VL6180_APPCFG_H_
/**
* @def VL6180_SINGLE_DEVICE_DRIVER
* @brief enable lightweight single vl6180 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 porting files
* @ingroup Configuration
*/
#define VL6180_SINGLE_DEVICE_DRIVER 1
/**
* @def VL6180_RANGE_STATUS_ERRSTRING
* @brief when define include range status Error string and related
*
* The string table lookup require some space in read only area
* @ingroup Configuration
*/
#define VL6180_RANGE_STATUS_ERRSTRING 1
/**
* @def VL6180_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 ensure interrupt get clear on mode or interrupt configuration change
* then keep option disabled. \n
* To be safe set these option to 1
* @ingroup Configuration
*/
#define VL6180_SAFE_POLLING_ENTER 0
/**
* @brief Enable function start/end logging
*
* requires porting @a #LOG_FUNCTION_START @a #LOG_FUNCTION_END @a #LOG_FUNCTION_END_FMT
* @ingroup Configuration
*/
#define VL6180_LOG_ENABLE 0
#endif /* VL6180_APPCFG_H_ */

View File

@@ -0,0 +1,241 @@
/*******************************************************************************
Copyright © 2019, 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.
********************************************************************************/
/**
* @file vl6180_i2c.c
*
* Copyright (C) 2019 ST MicroElectronics
*
* provide variable word size byte/Word/dword VL6180 register access via i2c
*
*/
#include "vl6180_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[VL6180_MAX_I2C_XFER_SIZE];
#define DECL_I2C_BUFFER
#define VL6180_GetI2cBuffer(dev, n_byte) i2c_global_buffer
#elif I2C_BUFFER_CONFIG == 1
/* ON STACK */
#define DECL_I2C_BUFFER uint8_t LocBuffer[VL6180_MAX_I2C_XFER_SIZE];
#define VL6180_GetI2cBuffer(dev, n_byte) LocBuffer
#elif I2C_BUFFER_CONFIG == 2
/* user define buffer type declare DECL_I2C_BUFFER as access via VL6180_GetI2cBuffer */
#define DECL_I2C_BUFFER
#else
#error "invalid I2C_BUFFER_CONFIG "
#endif
int VL6180_WrByte(VL6180Dev_t dev, uint16_t index, uint8_t data){
int status;
uint8_t *buffer;
DECL_I2C_BUFFER
VL6180_I2C_USER_VAR
VL6180_GetI2CAccess(dev);
buffer=VL6180_GetI2cBuffer(dev,3);
buffer[0]=index>>8;
buffer[1]=index&0xFF;
buffer[2]=data;
status=VL6180_I2CWrite(dev, buffer,(uint8_t)3);
VL6180_DoneI2CAcces(dev);
return status;
}
int VL6180_WrWord(VL6180Dev_t dev, uint16_t index, uint16_t data){
int status;
DECL_I2C_BUFFER
uint8_t *buffer;
VL6180_I2C_USER_VAR
VL6180_GetI2CAccess(dev);
buffer=VL6180_GetI2cBuffer(dev,4);
buffer[0]=index>>8;
buffer[1]=index&0xFF;
buffer[2]=data>>8;
buffer[3]=data&0xFF;
status=VL6180_I2CWrite(dev, buffer,(uint8_t)4);
VL6180_DoneI2CAcces(dev);
return status;
}
int VL6180_WrDWord(VL6180Dev_t dev, uint16_t index, uint32_t data){
VL6180_I2C_USER_VAR
DECL_I2C_BUFFER
int status;
uint8_t *buffer;
VL6180_GetI2CAccess(dev);
buffer=VL6180_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=VL6180_I2CWrite(dev, buffer,(uint8_t)6);
VL6180_DoneI2CAcces(dev);
return status;
}
int VL6180_UpdateByte(VL6180Dev_t dev, uint16_t index, uint8_t AndData, uint8_t OrData){
VL6180_I2C_USER_VAR
int status;
uint8_t *buffer;
DECL_I2C_BUFFER
VL6180_GetI2CAccess(dev);
buffer=VL6180_GetI2cBuffer(dev,3);
buffer[0]=index>>8;
buffer[1]=index&0xFF;
status=VL6180_I2CWrite(dev, (uint8_t *)buffer,(uint8_t)2);
if( !status ){
/* read data direct onto buffer */
status=VL6180_I2CRead(dev, &buffer[2],1);
if( !status ){
buffer[2]=(buffer[2]&AndData)|OrData;
status=VL6180_I2CWrite(dev, buffer, (uint8_t)3);
}
}
VL6180_DoneI2CAcces(dev);
return status;
}
int VL6180_RdByte(VL6180Dev_t dev, uint16_t index, uint8_t *data){
VL6180_I2C_USER_VAR
int status;
uint8_t *buffer;
DECL_I2C_BUFFER
VL6180_GetI2CAccess(dev);
buffer=VL6180_GetI2cBuffer(dev,2);
buffer[0]=index>>8;
buffer[1]=index&0xFF;
status=VL6180_I2CWrite(dev, buffer, (uint8_t)2);
if( !status ){
status=VL6180_I2CRead(dev, buffer,1);
if( !status ){
*data=buffer[0];
}
}
VL6180_DoneI2CAcces(dev);
return status;
}
int VL6180_RdWord(VL6180Dev_t dev, uint16_t index, uint16_t *data){
VL6180_I2C_USER_VAR
int status;
uint8_t *buffer;
DECL_I2C_BUFFER
VL6180_GetI2CAccess(dev);
buffer=VL6180_GetI2cBuffer(dev,2);
buffer[0]=index>>8;
buffer[1]=index&0xFF;
status=VL6180_I2CWrite(dev, buffer, (uint8_t)2);
if( !status){
status=VL6180_I2CRead(dev, buffer,2);
if( !status ){
/* VL6180 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];
}
}
VL6180_DoneI2CAcces(dev);
return status;
}
int VL6180_RdDWord(VL6180Dev_t dev, uint16_t index, uint32_t *data){
VL6180_I2C_USER_VAR
int status;
uint8_t *buffer;
DECL_I2C_BUFFER
VL6180_GetI2CAccess(dev);
buffer=VL6180_GetI2cBuffer(dev,4);
buffer[0]=index>>8;
buffer[1]=index&0xFF;
status=VL6180_I2CWrite(dev, (uint8_t *) buffer, (uint8_t)2);
if( !status ){
status=VL6180_I2CRead(dev, buffer,4);
if( !status ){
/* VL6180 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]);
}
}
VL6180_DoneI2CAcces(dev);
return status;
}
int VL6180_RdMulti(VL6180Dev_t dev, uint16_t index, uint8_t *data, int nData){
VL6180_I2C_USER_VAR
int status;
uint8_t *buffer;
DECL_I2C_BUFFER
VL6180_GetI2CAccess(dev);
buffer=VL6180_GetI2cBuffer(dev,2);
buffer[0]=index>>8;
buffer[1]=index&0xFF;
status=VL6180_I2CWrite(dev, (uint8_t *) buffer, (uint8_t)2);
if( !status ){
status=VL6180_I2CRead(dev, data, nData);
}
VL6180_DoneI2CAcces(dev);
return status;
}

View File

@@ -0,0 +1,166 @@
/*
* $Date: 2015-01-08 14:30:24 +0100 (Thu, 08 Jan 2015) $
* $Revision: 2039 $
*/
/**
* @file vl6180_i2c.h
*
* @brief CCI interface to "raw i2c" translation layer
*/
#ifndef VL6180_I2C_H_
#define VL6180_I2C_H_
#include "vl6180_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 VL6180_I2CRead() and
* @a VL6180_I2CWrite() functions need to be implemented. Also, some code adaption (via macro) is required for multi-threading and for multiple device support.
*
* File vl6180_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 VL6180_I2CRead() and @a VL6180_I2CWrite().\n
* In other cases, review and customize @a VL6180_GetI2CAccess() and @a VL6180_DoneI2CAccess() functions as well as @a #VL6180_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 VL6180_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 VL6180DevData_t StData;
...
spinlock_t i2c_lock;
};
typedef struct MyVL6180Dev_t *VL6180Dev_t;
#define VL6180_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 VL6180DevData_t StData;
...
pthread_mutex_t *lock;
};
typedef struct MyVL6180Dev_t *VL6180Dev_t;
#define VL6180_I2C_USER_VAR //no need
#define VL6180_GetI2CAccess(dev) pthread_mutex_lock(dev->lock)
#define VL6180_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 VL6180_GetI2CAccess() \n
*
* @li 2 : User defined \n
* Per device potentially dynamic allocated. Requires @a VL6180_GetI2cBuffer() to be implemented.
* @ingroup Configuration
*/
#define I2C_BUFFER_CONFIG 1
/**
* @brief Write data buffer to VL6180 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 VL6180_I2CWrite(VL6180Dev_t dev, uint8_t *buff, uint8_t len);
/**
*
* @brief Read data buffer from VL6180 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 VL6180_I2CRead(VL6180Dev_t dev, uint8_t *buff, uint8_t len);
/**
* @brief Declare any required variables used by i2c lock (@a VL6180_DoneI2CAccess() and @a VL6180_GetI2CAccess())
* and buffer access : @a VL6180_GetI2cBuffer()
*
* @ingroup cci_i2c
*/
#define VL6180_I2C_USER_VAR
/**
* @brief Acquire lock or mutex for access to i2c data buffer and bus.\n
* Delete the default VL6180_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 VL6180_GetI2CAccess(VL6180Dev_t dev);
/**
* @def VL6180_GetI2CAccess
* @brief Default 'do-nothing' macro for @a VL6180_GetI2CAccess(). Delete if used.
* @ingroup cci_i2c
*/
#define VL6180_GetI2CAccess(dev) (void)0 /* TODO delete if function used */
/**
* @brief Release acquired lock or mutex for i2c access.\n
* Delete default VL6180_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 VL6180_DoneI2CAccess(VL6180Dev_t dev);
/** @def VL6180_DoneI2CAcces
* @brief Default 'do-nothing' macro for @a VL6180_DoneI2CAcces(). Delete if used.
* @ingroup cci_i2c
*/
#define VL6180_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 #VL6180_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 *VL6180_GetI2cBuffer(VL6180Dev_t dev, int n_byte);
#if I2C_BUFFER_CONFIG == 2
#error /* TODO add your macro of code here for VL6180_GetI2cBuffer */
#endif
#endif /* VL6180_I2C_H_ */

View File

@@ -0,0 +1,115 @@
/*******************************************************************************
Copyright © 2019, 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.
********************************************************************************/
/* vl6180_platform.h STM32 Nucelo F401 single device using generic cci-i2c
* trace via swo port some GnuArm eclipse toolset */
#ifndef VL6180_PLATFORM
#define VL6180_PLATFORM
#include "vl6180_appcfg.h"
#include "vl6180_def.h"
#define VL6180_DEV_DATA_ATTR
#define ROMABLE_DATA
/* #define ROMABLE_DATA __attribute__ ((section ("user_rom"))) */
#if VL6180_LOG_ENABLE
/* dot not include non ansi here trace was a case :( */
#ifdef TRACE
#include "diag/trace.h"
extern volatile uint32_t g_TickCnt;
#define LOG_GET_TIME() g_TickCnt
#else
/* these is nto stm32 vl6180 GNuArm eclpse build*/
#define trace_printf(...) (void)0
#define LOG_GET_TIME() (int)0 /* add your code here expect to be an integer native (%d) type value */
#endif
#define LOG_FUNCTION_START(fmt, ... ) \
trace_printf("beg %s start @%d\t" fmt "\n", __func__, LOG_GET_TIME(), ##__VA_ARGS__)
#define LOG_FUNCTION_END(status)\
trace_printf("end %s @%d %d\n", __func__, LOG_GET_TIME(), (int)status)
#define LOG_FUNCTION_END_FMT(status, fmt, ... )\
trace_printf("End %s @%d %d\t"fmt"\n" , __func__, LOG_GET_TIME(), (int)status, ##__VA_ARGS__)
#define VL6180_ErrLog(msg, ... )\
do{\
trace_printf("ERR in %s line %d\n" msg, __func__, __LINE__, ##__VA_ARGS__);\
}while(0)
#else /* VL6180_LOG_ENABLE no logging */
void OnErrLog(void);
#define LOG_FUNCTION_START(...) (void)0
#define LOG_FUNCTION_END(...) (void)0
#define LOG_FUNCTION_END_FMT(...) (void)0
#define VL6180_ErrLog(... ) OnErrLog()
#endif
#if VL6180_SINGLE_DEVICE_DRIVER
typedef uint8_t VL6180Dev_t;
#else /* VL6180_SINGLE_DEVICE_DRIVER */
struct MyVL6180Dev_t {
struct VL6180DevData_t Data;
#if I2C_BUFFER_CONFIG == 2
uint8_t i2c_buffer[VL6180_MAX_I2C_XFER_SIZE];
#define VL6180_GetI2cBuffer(dev, n) ((dev)->i2c_buffer)
#endif
};
typedef struct MyVL6180Dev_t *VL6180Dev_t;
#define VL6180DevDataGet(dev, field) (dev->Data.field)
#define VL6180DevDataSet(dev, field, data) (dev->Data.field)=(data)
#endif /* #else VL6180_SINGLE_DEVICE_DRIVER */
void VL6180_PollDelay(VL6180Dev_t dev);
void DISP_ExecLoopBody(void);
#define VL6180_PollDelay(dev) DISP_ExecLoopBody();
#endif /* VL6180_PLATFORM */

View File

@@ -0,0 +1,62 @@
/*******************************************************************************
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.
********************************************************************************/
#ifndef VL6180_TYPES_H_
#define VL6180_TYPES_H_
#include <stdint.h>
#include <stddef.h> /* these is for NULL */
#ifndef NULL
#error "review NULL definition or add required include "
#endif
#if !defined(STDINT_H) && !defined(_GCC_STDINT_H) && !defined(__STDINT_DECLS) && !defined(_STDINT) && !defined(_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
*/
typedef unsigned int uint32_t;
typedef int int32_t;
typedef unsigned short uint16_t;
typedef short int16_t;
typedef unsigned char uint8_t;
typedef signed char int8_t;
#endif /* _STDINT_H */
#endif /* VL6180_TYPES_H_ */