Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: ST_INTERFACES X_NUCLEO_COMMON
Dependents: Ultrasound_And_IMU
Fork of X_NUCLEO_6180XA1 by
Diff: Components/VL6180X/vl6180x_class.cpp
- Revision:
- 8:f943a1fca15f
- Parent:
- 7:2dc81120c917
- Child:
- 9:9cbc1736f148
diff -r 2dc81120c917 -r f943a1fca15f Components/VL6180X/vl6180x_class.cpp
--- a/Components/VL6180X/vl6180x_class.cpp Fri Sep 25 12:12:51 2015 +0200
+++ b/Components/VL6180X/vl6180x_class.cpp Tue Sep 29 14:50:15 2015 +0200
@@ -37,40 +37,83 @@
/* Includes */
#include "vl6180x_class.h"
+
+
+#ifndef VL6180x_RANGE_STATUS_ERRSTRING
+#warning "VL6180x_RANGE_STATUS_ERRSTRING not defined ?"
+/* TODO you may remove or comment these #warning and keep the default below to keep compatibility
+ or update your vl6180x_platform.h file */
+/**
+ * force VL6180x_RANGE_STATUS_ERRSTRING to not supported when not part of any cfg file
+ */
+#define VL6180x_RANGE_STATUS_ERRSTRING 0
+#endif
+
+#ifndef VL6180X_SAFE_POLLING_ENTER
+#warning "VL6180X_SAFE_POLLING_ENTER not defined, likely old vl6180x_cfg.h file ?"
+/* TODO you may remove or comment these #warning and keep the default below to keep compatibility
+ or update your vl6180x_platform.h file */
+/**
+ * force VL6180X_SAFE_POLLING_ENTER to off when not in cfg file
+ */
+#define VL6180X_SAFE_POLLING_ENTER 0 /* off by default as in api 2.0 */
+#endif
+
+#ifndef VL6180X_LOG_ENABLE
+/**
+ * Force VL6180X_LOG_ENABLE to none as default
+ */
+#define VL6180X_LOG_ENABLE 0
+#endif
+
+#if VL6180x_RANGE_STATUS_ERRSTRING
+/**@def VL6180x_HAVE_RANGE_STATUS_ERRSTRING
+ * @brief is defined when @a #VL6180x_RANGE_STATUS_ERRSTRING is enable
+ */
+#define VL6180x_HAVE_RANGE_STATUS_ERRSTRING
+#endif
+
+
+/** @brief Get API version as "hex integer" 0xMMnnss
+ */
+#define VL6180x_ApiRevInt ((VL6180x_API_REV_MAJOR<<24)+(VL6180x_API_REV_MINOR<<16)+VL6180x_API_REV_SUB)
+
+/** Get API version as string for exe "2.1.12" "
+ */
+#define VL6180x_ApiRevStr VL6180X_STR(VL6180x_API_REV_MAJOR) "." VL6180X_STR(VL6180x_API_REV_MINOR) "." VL6180X_STR(VL6180x_API_REV_SUB)
+
+/** @defgroup api_init Init functions
+ * @brief API init functions
+ * @ingroup api_hl
+ * @{
+ */
+
/****************** define for i2c configuration *******************************/
-
-#define I2C_BUFFER_CONFIG 1
-#define VL6180x_I2C_USER_VAR
#define TEMP_BUF_SIZE 32
-#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
-
#define IsValidGPIOFunction(x) ((x)==GPIOx_SELECT_GPIO_INTERRUPT_OUTPUT || (x)==GPIOx_SELECT_OFF)
-
-
+/**
+ * @brief Clear All interrupt causes (als+range+error)
+ *
+ * @param dev The device
+ * @return 0 On success
+ */
+#define VL6180x_ClearAllInterrupt(dev) VL6180x_ClearInterrupt(dev, INTERRUPT_CLEAR_ERROR|INTERRUPT_CLEAR_RANGING|INTERRUPT_CLEAR_ALS)
+/**
+ * @brief Clear ALS interrupt
+ *
+ * @param dev The device
+ * @return 0 On success
+ */
+ #define VL6180x_AlsClearInterrupt(dev) VL6180x_ClearInterrupt(dev, INTERRUPT_CLEAR_ALS)
+/**
+ * @brief Clear range interrupt
+ *
+ * @param dev The device
+ * @return 0 On success
+ */
+#define VL6180x_RangeClearInterrupt(dev) VL6180x_ClearInterrupt(dev, INTERRUPT_CLEAR_RANGING)
/******************************************************************************/
/******************************* file api.c ***********************************/
@@ -99,11 +142,6 @@
/** default ambient tuning factor %x1000 */
#define DEF_AMBIENT_TUNING 80
-#if VL6180x_SINGLE_DEVICE_DRIVER
-extern struct VL6180xDevData_t SingleVL6180xDevData;
-#define VL6180xDevDataGet(dev, field) (SingleVL6180xDevData.field)
-#define VL6180xDevDataSet(dev, field, data) SingleVL6180xDevData.field=(data)
-#endif
#define LUXRES_FIX_PREC 8
#define GAIN_FIX_PREC 8 /* ! if not sme as LUX_PREC then :( adjust GetLux */
@@ -183,30 +221,6 @@
#define _SetUpscale(dev, Scaling ) VL6180xDevDataSet(dev, UpscaleFactor, Scaling)
#endif
-
-#if VL6180x_SINGLE_DEVICE_DRIVER
-/**
- * the unique driver data When single device driver is active
- */
-struct VL6180xDevData_t VL6180x_DEV_DATA_ATTR SingleVL6180xDevData={
- .EceFactorM = DEF_ECE_FACTOR_M,
- .EceFactorD = DEF_ECE_FACTOR_D,
-#ifdef VL6180x_HAVE_UPSCALE_DATA
- .UpscaleFactor = DEF_UPSCALE,
-#endif
-#ifdef VL6180x_HAVE_ALS_DATA
- .IntegrationPeriod = DEF_INT_PEFRIOD,
- .AlsGainCode = DEF_ALS_GAIN,
- .AlsScaler = DEF_ALS_SCALER,
-#endif
-#ifdef VL6180x_HAVE_DMAX_RANGING
- .DMaxEnable = DEF_DMAX_ENABLE,
-#endif
-};
-#endif /* VL6180x_SINGLE_DEVICE_DRIVER */
-
-
-
#define Fix7_2_KCPs(x) ((((uint32_t)(x))*1000)>>7)
@@ -364,7 +378,7 @@
int status;
LOG_FUNCTION_START("");
- status = VL6180x_WrByte(dev, I2C_SLAVE_DEVICE_ADDRESS, NewAddress/2);
+ status = VL6180x_WrByte(dev, I2C_SLAVE_DEVICE_ADDRESS, NewAddress);
if( status ){
VL6180x_ErrLog("new i2c addr Wr fail");
}
@@ -2469,41 +2483,30 @@
int VL6180X::VL6180x_WrByte(VL6180xDev_t dev, uint16_t index, uint8_t data)
{
int status;
- DECL_I2C_BUFFER
- VL6180x_I2C_USER_VAR
-
- status=VL6180x_I2CWrite(dev, index, &data,(uint8_t)3);
+
+ status=VL6180x_I2CWrite(dev.I2cAddr, index, &data,(uint8_t)1);
return status;
}
int VL6180X::VL6180x_WrWord(VL6180xDev_t dev, uint16_t index, uint16_t data)
{
int status;
- DECL_I2C_BUFFER
- VL6180x_I2C_USER_VAR
-
- status=VL6180x_I2CWrite(MyDeviceAddress, index, (uint8_t *)&data,(uint8_t)4);
+ status=VL6180x_I2CWrite(dev.I2cAddr, index, (uint8_t *)&data,(uint8_t)2);
return status;
}
int VL6180X::VL6180x_WrDWord(VL6180xDev_t dev, uint16_t index, uint32_t data)
{
- VL6180x_I2C_USER_VAR
- DECL_I2C_BUFFER
int status;
-
- status=VL6180x_I2CWrite(MyDeviceAddress, index, (uint8_t *)&data,(uint8_t)6);
+ status=VL6180x_I2CWrite(dev.I2cAddr, index, (uint8_t *)&data,(uint8_t)4);
return status;
}
int VL6180X::VL6180x_RdByte(VL6180xDev_t dev, uint16_t index, uint8_t *data)
{
- VL6180x_I2C_USER_VAR
int status;
uint8_t buffer;
- DECL_I2C_BUFFER
-
- status=VL6180x_I2CRead(dev, index, &buffer,1);
+ status=VL6180x_I2CRead(dev.I2cAddr, index, &buffer,1);
if( !status ){
*data=buffer;
}
@@ -2512,48 +2515,33 @@
int VL6180X::VL6180x_RdWord(VL6180xDev_t dev, uint16_t index, uint16_t *data)
{
- VL6180x_I2C_USER_VAR
int status;
- uint8_t *buffer;
- DECL_I2C_BUFFER
-
- status=VL6180x_I2CRead(MyDeviceAddress, index, 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];
- }
+ uint8_t buffer[2];
+ status=VL6180x_I2CRead(dev.I2cAddr, index, buffer, 2);
return status;
}
int VL6180X::VL6180x_RdDWord(VL6180xDev_t dev, uint16_t index, uint32_t *data)
{
- VL6180x_I2C_USER_VAR
int status;
- uint8_t *buffer;
- DECL_I2C_BUFFER
-
+ uint8_t buffer[4];
- status=VL6180x_I2CRead(MyDeviceAddress, index, 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]);
- }
+ status=VL6180x_I2CRead(dev.I2cAddr, index, buffer,4);
return status;
}
-int VL6180X::VL6180x_UpdateByte(VL6180xDev_t dev, uint16_t index, uint8_t AndData, uint8_t OrData){
- VL6180x_I2C_USER_VAR
+int VL6180X::VL6180x_UpdateByte(VL6180xDev_t dev, uint16_t index, uint8_t AndData, uint8_t OrData)
+{
int status;
- uint8_t *buffer;
- DECL_I2C_BUFFER
-
- status=VL6180x_I2CWrite(MyDeviceAddress, index, (uint8_t *)buffer,(uint8_t)2);
+ uint8_t buffer;
+
+ status=VL6180x_I2CWrite(dev.I2cAddr, index, (uint8_t *)buffer,(uint8_t)0);
if( !status ){
/* read data direct onto buffer */
- status=VL6180x_I2CRead(dev, index, &buffer[2],1);
+ status=VL6180x_I2CRead(dev.I2cAddr, index, &buffer,1);
if( !status ){
- buffer[2]=(buffer[2]&AndData)|OrData;
- status=VL6180x_I2CWrite(dev, index, buffer, (uint8_t)3);
+ buffer=(buffer & AndData)|OrData;
+ status=VL6180x_I2CWrite(dev.I2cAddr, index, &buffer, (uint8_t)1);
}
}
@@ -2567,26 +2555,26 @@
uint8_t tmp[TEMP_BUF_SIZE];
uint16_t myRegisterAddr = RegisterAddr;
uint16_t WriteDeviceAddr=0;
-
+ /* First, prepare 8 bits device address in 7bits i2ci format */
WriteDeviceAddr=DeviceAddr*2;
if(NumByteToWrite >= TEMP_BUF_SIZE) return -2;
- /* First, send 8 bits device address and 16 bits register address in BE format. Then, send data and STOP condition */
+ /* then prepare 16 bits register address in BE format. Then, send data and STOP condition */
tmp[0] = *(((uint8_t*)&myRegisterAddr)+1);
tmp[1] = (uint8_t)RegisterAddr;
- if(NumByteToWrite>1)
+ if(NumByteToWrite>1) /* swap data endianess */
{
for(i=0;i<NumByteToWrite;i++)
{
- tmp[NumByteToWrite+1-i]=pBuffer[i];
+ tmp[NumByteToWrite+sizeof(RegisterAddr)-i]=pBuffer[i];
}
}
else
{
- memcpy(tmp+2, pBuffer, NumByteToWrite);
+ memcpy(tmp+sizeof(RegisterAddr), pBuffer, NumByteToWrite);
}
- ret = dev_i2c.write(WriteDeviceAddr, (const char*)tmp, NumByteToWrite+sizeof(RegisterAddr), false);
+ ret = dev_i2c.write(DeviceAddr, (const char*)tmp, NumByteToWrite+sizeof(RegisterAddr), false);
if(ret) return -1;
return 0;
@@ -2594,7 +2582,8 @@
int VL6180X::VL6180x_I2CRead(uint8_t DeviceAddr, uint16_t RegisterAddr, uint8_t* pBuffer, uint16_t NumByteToRead)
{
- int ret;
+ int ret,i;
+ uint8_t tmp[TEMP_BUF_SIZE];
uint16_t myRegisterAddr = RegisterAddr;
uint16_t myRegisterAddrBE;
uint16_t ReadDeviceAddr=DeviceAddr;
@@ -2608,36 +2597,31 @@
if(!ret) {
ReadDeviceAddr|=0x001;
/* Read data, with STOP condition */
- ret = dev_i2c.read(ReadDeviceAddr, (char*)pBuffer, NumByteToRead, false);
+ ret = dev_i2c.read(ReadDeviceAddr, (char*)tmp, NumByteToRead, false);
+ }
+ if(ret) return -1;
+
+ if(NumByteToRead>1) /* swap data endianess */
+ {
+ for(i=0;i<NumByteToRead;i++)
+ {
+ pBuffer[i] = tmp[NumByteToRead-1-i];
+ }
}
-
- if(ret) return -1;
+ else
+ {
+ memcpy(pBuffer, tmp, NumByteToRead);
+ }
return 0;
}
-/****************** IO read funcitons *****************************************/
-
int VL6180X::VL6180X_ReadID()
{
- uint8_t rl_id;
-
- return VL6180X_IO_Read(&rl_id, IDENTIFICATION_MODEL_ID, 1);
+ uint8_t rd_id;
+ VL6180x_RdByte( DevData, IDENTIFICATION_MODEL_ID, &rd_id);
+ return rd_id;
}
-int VL6180X::VL6180X_IO_Read(uint8_t *pBuffer, uint8_t RegisterAddress, uint16_t NumByteToRead)
-{
- int status;
-
- status=VL6180x_I2CRead(MyDeviceAddress, RegisterAddress, pBuffer, NumByteToRead);
- if(status!=0)
- {
- return INVALID_PARAMS; // DA DEFINIRE IL TIPO DI ERRORE!!
- }
- return API_NO_ERROR; // DA DEFINIRE IL TIPO DI ERRORE!!
-}
-
-
-
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
