/*******************************************************************************
Copyright � 2016, 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.
******************************************************************************/
#include "vl53l0x_api.h"
#include "vl53l0x_tuning.h"
#include "vl53l0x_interrupt_threshold_settings.h"
#include "vl53l0x_api_core.h"
#include "vl53l0x_api_calibration.h"
#ifndef __KERNEL__
#include <stdlib.h>
#endif
#define LOG_FUNCTION_START(fmt, ...) \
_LOG_FUNCTION_START(TRACE_MODULE_API, fmt, ##__VA_ARGS__)
#define LOG_FUNCTION_END(status, ...) \
_LOG_FUNCTION_END(TRACE_MODULE_API, status, ##__VA_ARGS__)
#define LOG_FUNCTION_END_FMT(status, fmt, ...) \
_LOG_FUNCTION_END_FMT(TRACE_MODULE_API, status, fmt, ##__VA_ARGS__)
#ifdef VL53L0X_LOG_ENABLE
#define trace_print(level, ...) trace_print_module_function(TRACE_MODULE_API, \
level, TRACE_FUNCTION_NONE, ##__VA_ARGS__)
#endif
/* Group PAL General Functions */
VL53L0X_Error VL53L0X_GetVersion(VL53L0X_Version_t *pVersion)
{
VL53L0X_Error Status = VL53L0X_ERROR_NONE;
LOG_FUNCTION_START("");
pVersion->major = VL53L0X_IMPLEMENTATION_VER_MAJOR;
pVersion->minor = VL53L0X_IMPLEMENTATION_VER_MINOR;
pVersion->build = VL53L0X_IMPLEMENTATION_VER_SUB;
pVersion->revision = VL53L0X_IMPLEMENTATION_VER_REVISION;
LOG_FUNCTION_END(Status);
return Status;
}
VL53L0X_Error VL53L0X_GetPalSpecVersion(VL53L0X_Version_t *pPalSpecVersion)
{
VL53L0X_Error Status = VL53L0X_ERROR_NONE;
LOG_FUNCTION_START("");
pPalSpecVersion->major = VL53L0X_SPECIFICATION_VER_MAJOR;
pPalSpecVersion->minor = VL53L0X_SPECIFICATION_VER_MINOR;
pPalSpecVersion->build = VL53L0X_SPECIFICATION_VER_SUB;
pPalSpecVersion->revision = VL53L0X_SPECIFICATION_VER_REVISION;
LOG_FUNCTION_END(Status);
return Status;
}
VL53L0X_Error VL53L0X_GetProductRevision(VL53L0X_DEV Dev,
uint8_t *pProductRevisionMajor, uint8_t *pProductRevisionMinor)
{
VL53L0X_Error Status = VL53L0X_ERROR_NONE;
uint8_t revision_id;
LOG_FUNCTION_START("");
Status = VL53L0X_RdByte(Dev, VL53L0X_REG_IDENTIFICATION_REVISION_ID,
&revision_id);
*pProductRevisionMajor = 1;
*pProductRevisionMinor = (revision_id & 0xF0) >> 4;
LOG_FUNCTION_END(Status);
return Status;
}
VL53L0X_Error VL53L0X_GetDeviceInfo(VL53L0X_DEV Dev,
VL53L0X_DeviceInfo_t *pVL53L0X_DeviceInfo)
{
VL53L0X_Error Status = VL53L0X_ERROR_NONE;
LOG_FUNCTION_START("");
Status = VL53L0X_get_device_info(Dev, pVL53L0X_DeviceInfo);
LOG_FUNCTION_END(Status);
return Status;
}
VL53L0X_Error VL53L0X_GetDeviceErrorStatus(VL53L0X_DEV Dev,
VL53L0X_DeviceError *pDeviceErrorStatus)
{
VL53L0X_Error Status = VL53L0X_ERROR_NONE;
uint8_t RangeStatus;
LOG_FUNCTION_START("");
Status = VL53L0X_RdByte(Dev, VL53L0X_REG_RESULT_RANGE_STATUS,
&RangeStatus);
*pDeviceErrorStatus = (VL53L0X_DeviceError)((RangeStatus & 0x78) >> 3);
LOG_FUNCTION_END(Status);
return Status;
}
VL53L0X_Error VL53L0X_GetDeviceErrorString(VL53L0X_DeviceError ErrorCode,
char *pDeviceErrorString)
{
VL53L0X_Error Status = VL53L0X_ERROR_NONE;
LOG_FUNCTION_START("");
Status = VL53L0X_get_device_error_string(ErrorCode, pDeviceErrorString);
LOG_FUNCTION_END(Status);
return Status;
}
VL53L0X_Error VL53L0X_GetRangeStatusString(uint8_t RangeStatus,
char *pRangeStatusString)
{
VL53L0X_Error Status = VL53L0X_ERROR_NONE;
LOG_FUNCTION_START("");
Status = VL53L0X_get_range_status_string(RangeStatus,
pRangeStatusString);
LOG_FUNCTION_END(Status);
return Status;
}
VL53L0X_Error VL53L0X_GetPalErrorString(VL53L0X_Error PalErrorCode,
char *pPalErrorString)
{
VL53L0X_Error Status = VL53L0X_ERROR_NONE;
LOG_FUNCTION_START("");
Status = VL53L0X_get_pal_error_string(PalErrorCode, pPalErrorString);
LOG_FUNCTION_END(Status);
return Status;
}
VL53L0X_Error VL53L0X_GetPalStateString(VL53L0X_State PalStateCode,
char *pPalStateString)
{
VL53L0X_Error Status = VL53L0X_ERROR_NONE;
LOG_FUNCTION_START("");
Status = VL53L0X_get_pal_state_string(PalStateCode, pPalStateString);
LOG_FUNCTION_END(Status);
return Status;
}
VL53L0X_Error VL53L0X_GetPalState(VL53L0X_DEV Dev, VL53L0X_State *pPalState)
{
VL53L0X_Error Status = VL53L0X_ERROR_NONE;
LOG_FUNCTION_START("");
*pPalState = PALDevDataGet(Dev, PalState);
LOG_FUNCTION_END(Status);
return Status;
}
VL53L0X_Error VL53L0X_SetPowerMode(VL53L0X_DEV Dev, VL53L0X_PowerModes PowerMode)
{
VL53L0X_Error Status = VL53L0X_ERROR_NONE;
LOG_FUNCTION_START("");
/* Only level1 of Power mode exists */
if ((PowerMode != VL53L0X_POWERMODE_STANDBY_LEVEL1)
&& (PowerMode != VL53L0X_POWERMODE_IDLE_LEVEL1)) {
Status = VL53L0X_ERROR_MODE_NOT_SUPPORTED;
} else if (PowerMode == VL53L0X_POWERMODE_STANDBY_LEVEL1) {
/* set the standby level1 of power mode */
Status = VL53L0X_WrByte(Dev, 0x80, 0x00);
if (Status == VL53L0X_ERROR_NONE) {
/* Set PAL State to standby */
PALDevDataSet(Dev, PalState, VL53L0X_STATE_STANDBY);
PALDevDataSet(Dev, PowerMode,
VL53L0X_POWERMODE_STANDBY_LEVEL1);
}
} else {
/* VL53L0X_POWERMODE_IDLE_LEVEL1 */
Status = VL53L0X_WrByte(Dev, 0x80, 0x00);
if (Status == VL53L0X_ERROR_NONE)
Status = VL53L0X_StaticInit(Dev);
if (Status == VL53L0X_ERROR_NONE)
PALDevDataSet(Dev, PowerMode,
VL53L0X_POWERMODE_IDLE_LEVEL1);
}
LOG_FUNCTION_END(Status);
return Status;
}
VL53L0X_Error VL53L0X_GetPowerMode(VL53L0X_DEV Dev, VL53L0X_PowerModes *pPowerMode)
{
VL53L0X_Error Status = VL53L0X_ERROR_NONE;
uint8_t Byte;
LOG_FUNCTION_START("");
/* Only level1 of Power mode exists */
Status = VL53L0X_RdByte(Dev, 0x80, &Byte);
if (Status == VL53L0X_ERROR_NONE) {
if (Byte == 1) {
PALDevDataSet(Dev, PowerMode,
VL53L0X_POWERMODE_IDLE_LEVEL1);
} else {
PALDevDataSet(Dev, PowerMode,
VL53L0X_POWERMODE_STANDBY_LEVEL1);
}
}
LOG_FUNCTION_END(Status);
return Status;
}
VL53L0X_Error VL53L0X_SetOffsetCalibrationDataMicroMeter(VL53L0X_DEV Dev,
int32_t OffsetCalibrationDataMicroMeter)
{
VL53L0X_Error Status = VL53L0X_ERROR_NONE;
LOG_FUNCTION_START("");
Status = VL53L0X_set_offset_calibration_data_micro_meter(Dev,
OffsetCalibrationDataMicroMeter);
LOG_FUNCTION_END(Status);
return Status;
}
VL53L0X_Error VL53L0X_GetOffsetCalibrationDataMicroMeter(VL53L0X_DEV