---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);