2020-09-28 05:14 AM
Ultimately, I would like to know where I2C commands are described in order to re-write my own I2C R/W function according to my hardware. First I woud like to play with the entire API to get familiar with it. The file ranging_sensor_comms.h is not included in the STM32 53L3A2 expansion board software. The vl53lx_platform.c is completely re-written,and the project only uses this file frome the API and none of the other files. Thank you for your help
Solved! Go to Solution.
2020-11-20 09:05 AM
UPDATE : the answer was dead simple : I forgot to shift the I2C adress like this :
status = (int8_t)bus_i2c_ReadData(DevAddr>>1, pdata, pdev->i2c_slave_address + index, count);
here is my final code for those who have the issue aswell :
VL53LX_Error VL53LX_ReadMulti(VL53LX_Dev_t *pdev, uint16_t index, uint8_t *pdata, uint32_t count)
{
VL53LX_Error status = VL53LX_ERROR_NONE;
uint8_t DevAddr = pdev->I2CDevAddr;
int32_t status_int;
uint8_t I2C_buff[2] = {0};
if (count>=VL53LX_COMMS_BUFFER_SIZE)
{
status = VL53LX_ERROR_INVALID_PARAMS;
}
if (count > (VL53LX_MAX_STRING_LENGTH_PLT))
{
return NRF_ERROR_INVALID_LENGTH;
}
status_int = bus_i2c_ReadData(DevAddr>>1, pdata, index, count);
if (status_int != 0)
status = VL53LX_ERROR_CONTROL_INTERFACE;
return status;
}
2020-09-28 07:53 AM
As delivered the API does everything you need right down to where the code sends the I2C commands. We generally don't assume we know which processor you are running on, so the platform.c and platform.h are left to you.
But clearly someone left the PC test code in there.
Platform.c and platform.h should have been empty prototypes.
I'm going to guess - and it's a big guess that you have an STM32.
If so the following platform.c and should work.
The post gets long, so I'm going to put the .h file in the next post
//?????#include "hal.h"
#include "vl53l0x_platform.h"
#include "vl53l0x_api.h"
#include "stm32xxx_hal.h"
#include <string.h>
#define I2C_TIME_OUT_BASE 10
#define I2C_TIME_OUT_BYTE 1
#define VL53L0X_OsDelay(...) HAL_Delay(2)
#ifndef HAL_I2C_MODULE_ENABLED
#warning "HAL I2C module must be enable "
#endif
//extern I2C_HandleTypeDef hi2c1;
//#define VL53L0X_pI2cHandle (&hi2c1)
/* when not customized by application define dummy one */
#ifndef VL53L0X_GetI2cBus
/** This macro can be overloaded by user to enforce i2c sharing in RTOS context
*/
# define VL53L0X_GetI2cBus(...) (void)0
#endif
#ifndef VL53L0X_PutI2cBus
/** This macro can be overloaded by user to enforce i2c sharing in RTOS context
*/
# define VL53L0X_PutI2cBus(...) (void)0
#endif
#ifndef VL53L0X_OsDelay
# define VL53L0X_OsDelay(...) (void)0
#endif
uint8_t _I2CBuffer[64];
int _I2CWrite(VL53L0X_DEV Dev, uint8_t *pdata, uint32_t count) {
int status;
int i2c_time_out = I2C_TIME_OUT_BASE+ count* I2C_TIME_OUT_BYTE;
status = HAL_I2C_Master_Transmit(Dev->I2cHandle, Dev->I2cDevAddr, pdata, count, i2c_time_out);
if (status) {
//VL6180x_ErrLog("I2C error 0x%x %d len", dev->I2cAddr, len);
//XNUCLEO6180XA1_I2C1_Init(&hi2c1);
}
return status;
}
int _I2CRead(VL53L0X_DEV Dev, uint8_t *pdata, uint32_t count) {
int status;
int i2c_time_out = I2C_TIME_OUT_BASE+ count* I2C_TIME_OUT_BYTE;
status = HAL_I2C_Master_Receive(Dev->I2cHandle, Dev->I2cDevAddr|1, pdata, count, i2c_time_out);
if (status) {
//VL6180x_ErrLog("I2C error 0x%x %d len", dev->I2cAddr, len);
//XNUCLEO6180XA1_I2C1_Init(&hi2c1);
}
return status;
}
// the ranging_sensor_comms.dll will take care of the page selection
VL53L0X_Error VL53L0X_WriteMulti(VL53L0X_DEV Dev, uint8_t index, uint8_t *pdata, uint32_t count) {
int status_int;
VL53L0X_Error Status = VL53L0X_ERROR_NONE;
if (count > sizeof(_I2CBuffer) - 1) {
return VL53L0X_ERROR_INVALID_PARAMS;
}
_I2CBuffer[0] = index;
memcpy(&_I2CBuffer[1], pdata, count);
VL53L0X_GetI2cBus();
status_int = _I2CWrite(Dev, _I2CBuffer, count + 1);
if (status_int != 0) {
Status = VL53L0X_ERROR_CONTROL_INTERFACE;
}
VL53L0X_PutI2cBus();
return Status;
}
// the ranging_sensor_comms.dll will take care of the page selection
VL53L0X_Error VL53L0X_ReadMulti(VL53L0X_DEV Dev, uint8_t index, uint8_t *pdata, uint32_t count) {
VL53L0X_Error Status = VL53L0X_ERROR_NONE;
int32_t status_int;
VL53L0X_GetI2cBus();
status_int = _I2CWrite(Dev, &index, 1);
if (status_int != 0) {
Status = VL53L0X_ERROR_CONTROL_INTERFACE;
goto done;
}
status_int = _I2CRead(Dev, pdata, count);
if (status_int != 0) {
Status = VL53L0X_ERROR_CONTROL_INTERFACE;
}
done:
VL53L0X_PutI2cBus();
return Status;
}
VL53L0X_Error VL53L0X_WrByte(VL53L0X_DEV Dev, uint8_t index, uint8_t data) {
VL53L0X_Error Status = VL53L0X_ERROR_NONE;
int32_t status_int;
_I2CBuffer[0] = index;
_I2CBuffer[1] = data;
VL53L0X_GetI2cBus();
status_int = _I2CWrite(Dev, _I2CBuffer, 2);
if (status_int != 0) {
Status = VL53L0X_ERROR_CONTROL_INTERFACE;
}
VL53L0X_PutI2cBus();
return Status;
}
VL53L0X_Error VL53L0X_WrWord(VL53L0X_DEV Dev, uint8_t index, uint16_t data) {
VL53L0X_Error Status = VL53L0X_ERROR_NONE;
int32_t status_int;
_I2CBuffer[0] = index;
_I2CBuffer[1] = data >> 8;
_I2CBuffer[2] = data & 0x00FF;
VL53L0X_GetI2cBus();
status_int = _I2CWrite(Dev, _I2CBuffer, 3);
if (status_int != 0) {
Status = VL53L0X_ERROR_CONTROL_INTERFACE;
}
VL53L0X_PutI2cBus();
return Status;
}
VL53L0X_Error VL53L0X_WrDWord(VL53L0X_DEV Dev, uint8_t index, uint32_t data) {
VL53L0X_Error Status = VL53L0X_ERROR_NONE;
int32_t status_int;
_I2CBuffer[0] = index;
_I2CBuffer[1] = (data >> 24) & 0xFF;
_I2CBuffer[2] = (data >> 16) & 0xFF;
_I2CBuffer[3] = (data >> 8) & 0xFF;
_I2CBuffer[4] = (data >> 0 ) & 0xFF;
VL53L0X_GetI2cBus();
status_int = _I2CWrite(Dev, _I2CBuffer, 5);
if (status_int != 0) {
Status = VL53L0X_ERROR_CONTROL_INTERFACE;
}
VL53L0X_PutI2cBus();
return Status;
}
VL53L0X_Error VL53L0X_UpdateByte(VL53L0X_DEV Dev, uint8_t index, uint8_t AndData, uint8_t OrData) {
VL53L0X_Error Status = VL53L0X_ERROR_NONE;
uint8_t data;
Status = VL53L0X_RdByte(Dev, index, &data);
if (Status) {
goto done;
}
data = (data & AndData) | OrData;
Status = VL53L0X_WrByte(Dev, index, data);
done:
return Status;
}
VL53L0X_Error VL53L0X_RdByte(VL53L0X_DEV Dev, uint8_t index, uint8_t *data) {
VL53L0X_Error Status = VL53L0X_ERROR_NONE;
int32_t status_int;
VL53L0X_GetI2cBus();
status_int = _I2CWrite(Dev, &index, 1);
if( status_int ){
Status = VL53L0X_ERROR_CONTROL_INTERFACE;
goto done;
}
status_int = _I2CRead(Dev, data, 1);
if (status_int != 0) {
Status = VL53L0X_ERROR_CONTROL_INTERFACE;
}
done:
VL53L0X_PutI2cBus();
return Status;
}
VL53L0X_Error VL53L0X_RdWord(VL53L0X_DEV Dev, uint8_t index, uint16_t *data) {
VL53L0X_Error Status = VL53L0X_ERROR_NONE;
int32_t status_int;
VL53L0X_GetI2cBus();
status_int = _I2CWrite(Dev, &index, 1);
if( status_int ){
Status = VL53L0X_ERROR_CONTROL_INTERFACE;
goto done;
}
status_int = _I2CRead(Dev, _I2CBuffer, 2);
if (status_int != 0) {
Status = VL53L0X_ERROR_CONTROL_INTERFACE;
goto done;
}
*data = ((uint16_t)_I2CBuffer[0]<<8) + (uint16_t)_I2CBuffer[1];
done:
VL53L0X_PutI2cBus();
return Status;
}
VL53L0X_Error VL53L0X_RdDWord(VL53L0X_DEV Dev, uint8_t index, uint32_t *data) {
VL53L0X_Error Status = VL53L0X_ERROR_NONE;
int32_t status_int;
VL53L0X_GetI2cBus();
status_int = _I2CWrite(Dev, &index, 1);
if (status_int != 0) {
Status = VL53L0X_ERROR_CONTROL_INTERFACE;
goto done;
}
status_int = _I2CRead(Dev, _I2CBuffer, 4);
if (status_int != 0) {
Status = VL53L0X_ERROR_CONTROL_INTERFACE;
goto done;
}
*data = ((uint32_t)_I2CBuffer[0]<<24) + ((uint32_t)_I2CBuffer[1]<<16) + ((uint32_t)_I2CBuffer[2]<<8) + (uint32_t)_I2CBuffer[3];
done:
VL53L0X_PutI2cBus();
return Status;
}
VL53L0X_Error VL53L0X_PollingDelay(VL53L0X_DEV Dev) {
VL53L0X_Error status = VL53L0X_ERROR_NONE;
// do nothing
VL53L0X_OsDelay();
return status;
}
//end of file
2020-09-28 07:55 AM
And here is the .h file:
Note that the L0X, or L1X can be changed depending on which sensor you have.
/*******************************************************************************
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.
********************************************************************************/
#ifndef _VL53L0X_PLATFORM_H_
#define _VL53L0X_PLATFORM_H_
#include "vl53l0x_def.h"
#include "vl53l0x_platform_log.h"
#ifdef __cplusplus
extern "C" {
#endif
#include "stm32xxx_hal.h"
#if TRACE_UART
#define trace_printf uart_printf
#endif
/**
* @file vl53l0x_platform.h
*
* @brief All end user OS/platform/application porting
*/
/**
* @defgroup VL53L0X_platform_group VL53L0X Platform Functions
* @brief VL53L0X Platform Functions
* @{
*/
/**
* @struct VL53L0X_Dev_t
* @brief Generic PAL device type that does link between API and platform abstraction layer
*
*/
typedef struct {
VL53L0X_DevData_t Data; /*!< embed ST Ewok Dev data as "Data"*/
/*!< user specific field */
I2C_HandleTypeDef *I2cHandle;
uint8_t I2cDevAddr;
char DevLetter;
int Id;
int Present;
int Enabled;
int Ready;
uint8_t comms_type;
uint16_t comms_speed_khz;
int LeakyRange;
int LeakyFirst;
uint8_t RangeStatus;
uint8_t PreviousRangeStatus;
FixPoint1616_t SignalRateRtnMegaCps;
uint16_t EffectiveSpadRtnCount;
uint32_t StartTime;
} VL53L0X_Dev_t;
/**
* @brief Declare the device Handle as a pointer of the structure @a VL53L0X_Dev_t.
*
*/
typedef VL53L0X_Dev_t* VL53L0X_DEV;
/**
* @def PALDevDataGet
* @brief Get ST private structure @a VL53L0X_DevData_t data access
*
* @param Dev Device Handle
* @param field ST structure field name
* It maybe used and as real data "ref" not just as "get" for sub-structure item
* like PALDevDataGet(FilterData.field)[i] or PALDevDataGet(FilterData.MeasurementIndex)++
*/
#define PALDevDataGet(Dev, field) (Dev->Data.field)
/**
* @def PALDevDataSet(Dev, field, data)
* @brief Set ST private structure @a VL53L0X_DevData_t data field
* @param Dev Device Handle
* @param field ST structure field name
* @param data Data to be set
*/
#define PALDevDataSet(Dev, field, data) (Dev->Data.field)=(data)
/**
* @defgroup VL53L0X_registerAccess_group PAL Register Access Functions
* @brief PAL Register Access Functions
* @{
*/
/**
* Lock comms interface to serialize all commands to a shared I2C interface for a specific device
* @param Dev Device Handle
* @return VL53L0X_ERROR_NONE Success
* @return "Other error code" See ::VL53L0X_Error
*/
VL53L0X_Error VL53L0X_LockSequenceAccess(VL53L0X_DEV Dev);
/**
* Unlock comms interface to serialize all commands to a shared I2C interface for a specific device
* @param Dev Device Handle
* @return VL53L0X_ERROR_NONE Success
* @return "Other error code" See ::VL53L0X_Error
*/
VL53L0X_Error VL53L0X_UnlockSequenceAccess(VL53L0X_DEV Dev);
/**
* Writes the supplied byte buffer to the device
* @param Dev Device Handle
* @param index The register index
* @param pdata Pointer to uint8_t buffer containing the data to be written
* @param count Number of bytes in the supplied byte buffer
* @return VL53L0X_ERROR_NONE Success
* @return "Other error code" See ::VL53L0X_Error
*/
VL53L0X_Error VL53L0X_WriteMulti(VL53L0X_DEV Dev, uint8_t index, uint8_t *pdata, uint32_t count);
/**
* Reads the requested number of bytes from the device
* @param Dev Device Handle
* @param index The register index
* @param pdata Pointer to the uint8_t buffer to store read data
* @param count Number of uint8_t's to read
* @return VL53L0X_ERROR_NONE Success
* @return "Other error code" See ::VL53L0X_Error
*/
VL53L0X_Error VL53L0X_ReadMulti(VL53L0X_DEV Dev, uint8_t index, uint8_t *pdata, uint32_t count);
/**
* Write single byte register
* @param Dev Device Handle
* @param index The register index
* @param data 8 bit register data
* @return VL53L0X_ERROR_NONE Success
* @return "Other error code" See ::VL53L0X_Error
*/
VL53L0X_Error VL53L0X_WrByte(VL53L0X_DEV Dev, uint8_t index, uint8_t data);
/**
* Write word register
* @param Dev Device Handle
* @param index The register index
* @param data 16 bit register data
* @return VL53L0X_ERROR_NONE Success
* @return "Other error code" See ::VL53L0X_Error
*/
VL53L0X_Error VL53L0X_WrWord(VL53L0X_DEV Dev, uint8_t index, uint16_t data);
/**
* Write double word (4 byte) register
* @param Dev Device Handle
* @param index The register index
* @param data 32 bit register data
* @return VL53L0X_ERROR_NONE Success
* @return "Other error code" See ::VL53L0X_Error
*/
VL53L0X_Error VL53L0X_WrDWord(VL53L0X_DEV Dev, uint8_t index, uint32_t data);
/**
* Read single byte register
* @param Dev Device Handle
* @param index The register index
* @param data pointer to 8 bit data
* @return VL53L0X_ERROR_NONE Success
* @return "Other error code" See ::VL53L0X_Error
*/
VL53L0X_Error VL53L0X_RdByte(VL53L0X_DEV Dev, uint8_t index, uint8_t *data);
/**
* Read word (2byte) register
* @param Dev Device Handle
* @param index The register index
* @param data pointer to 16 bit data
* @return VL53L0X_ERROR_NONE Success
* @return "Other error code" See ::VL53L0X_Error
*/
VL53L0X_Error VL53L0X_RdWord(VL53L0X_DEV Dev, uint8_t index, uint16_t *data);
/**
* Read dword (4byte) register
* @param Dev Device Handle
* @param index The register index
* @param data pointer to 32 bit data
* @return VL53L0X_ERROR_NONE Success
* @return "Other error code" See ::VL53L0X_Error
*/
VL53L0X_Error VL53L0X_RdDWord(VL53L0X_DEV Dev, uint8_t index, uint32_t *data);
/**
* Threat safe Update (read/modify/write) single byte register
*
* Final_reg = (Initial_reg & and_data) |or_data
*
* @param Dev Device Handle
* @param index The register index
* @param AndData 8 bit and data
* @param OrData 8 bit or data
* @return VL53L0X_ERROR_NONE Success
* @return "Other error code" See ::VL53L0X_Error
*/
VL53L0X_Error VL53L0X_UpdateByte(VL53L0X_DEV Dev, uint8_t index, uint8_t AndData, uint8_t OrData);
/** @} end of VL53L0X_registerAccess_group */
/**
* @brief execute delay in all polling API call
*
* 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 need you can define it as an empty/void macro
* @code
* #define VL53L0X_PollingDelay(...) (void)0
* @endcode
* @param Dev Device Handle
* @return VL53L0X_ERROR_NONE Success
* @return "Other error code" See ::VL53L0X_Error
*/
VL53L0X_Error VL53L0X_PollingDelay(VL53L0X_DEV Dev); /* usually best implemented as a real function */
/** @} end of VL53L0X_platform_group */
#define VL53L0X_COPYSTRING(str, ...) strcpy(str, ##__VA_ARGS__)
#ifdef __cplusplus
}
#endif
#endif /* _VL53L0X_PLATFORM_H_ */
2020-09-28 08:30 AM
Hello, thank you for your answer! Unfortunately, it is not an STM32 for the moment ;) thank you anyway for the code. There is STM32F401RE code for 53L3A2 expansion boards I may use.
The API I have is taken from here
If I understand well, I have to completely custom my platform.c and platform.h, by putting my chip related I2C functions within each functions in plateform.c, right? Thank you for your help!
2020-09-29 07:37 AM
Exactly.
and to help verify you got it right, consider including the following code.
(Remove it once you get the test to pass.)
It verifies the long I2C long I/O operations and the 'byte endianness' of the operations.
2020-09-29 07:38 AM
Hi,
Actually the stsw-img015 you downloaded is the VL53L3CX driver package including the driver itself and the example. To run the example you need IAR, KEIL or STM32CudeIDE (freeware downloadable from st.com ). Your understanding is correct, you have to write your platform.c where you will implement the I2C functions according to your hardware. The platform.c in the example code was written for the X-NUCLEO-53L3A2 and the NucleoF401RE.
2020-09-29 08:18 AM
Thank you for the snippet, it is great! I'll try it when my functions will be done
2020-10-21 07:07 AM
Hi everyone, I am back to this project after few times.
I want to test with nRF microcontrollers and I am struggling to adapt platform.c with my I2C functions. Here is my main.c : (very simple)
#include "hardware.h"
#include "vl53lx_api.h"
#include "bus_i2c_drv.h"
#include "debug_uart_nrf52.h"
#include "nrf_delay.h"
VL53LX_Dev_t dev;
VL53LX_DEV Dev = &dev;
Dev->I2cDevAddr = 0x52;
VL53LX_Error status=0;
volatile int IntCount;
int main(void)
{
uint8_t byteData;
uint16_t wordData;
VL53LX_MultiRangingData_t MultiRangingData;
VL53LX_MultiRangingData_t *pMultiRangingData = &MultiRangingData;
uint8_t NewDataReady=0;
int no_of_object_found=0,j;
// Initialize.
hardware_IO_init();
debug_uart_init();
hardware_set_pin(PIN_PWR_SENS2); // VDD VL53L3
hardware_set_pin(PIN_PWR_SENS); // Pull-up i2c
bus_i2c_Init();
// Enter main loop.
for (;;)
{
status = VL53LX_RdByte(Dev, 0x010F, &byteData);
nrf_delay_ms(1000);
}
}
The i2c init returns 0 so it is fine, I have both SDA and SCL to VDD. Then I try to have the same data I have with the STM32F401RE with 53L3A2 board example (i.e 0xEA). VL53LX_RdByte() calls VL53LX_RdByte() which calls VL53LX_ReadMulti(). I edited this function as follow :
/**
* @brief Reads the requested number of bytes from the device
*
* @param[in] pdev : pointer to device structure (device handle)
* @param[in] index : uint16_t register index value
* @param[out] pdata : pointer to the uint8_t (byte) buffer to store read data
* @param[in] count : number of bytes to read
*
* @return VL53LX_ERROR_NONE Success
* @return "Other error code" See ::VL53LX_Error
*/
VL53LX_Error VL53LX_ReadMulti(
VL53LX_Dev_t *pdev,
uint16_t index,
uint8_t *pdata,
uint32_t count)
{
VL53LX_Error status = VL53LX_ERROR_NONE;
uint8_t *pBuffer = pdata;
uint8_t DevAddr = pdev->I2cDevAddr;
status = (int8_t)bus_i2c_ReadData(DevAddr, pdata, pdev->i2c_slave_address + index, count);
return status;
}
bus_i2c_ReadData() is the function I have to use :
/**
* @brief Reads Data from I2C communication bus.
* @param[in] DevAddr: Device address.
* @param[out] pBuffer: pointer to the buffer that receives the data read from the Device.
* @param[in] ReadAddr: Device's internal address to start reading from.
* @param[in] DataSize: number of bytes to read.
* @retval ret_code_t: NRF_SUCCESS (0) if operation is correctly performed, else return value different from NRF_SUCCESS (0).
*/
ret_code_t bus_i2c_ReadData(uint8_t DevAddr, uint8_t * pBuffer, uint8_t ReadAddr, size_t DataSize)
{
ret_code_t ret;
uint8_t addr8 = (uint8_t)ReadAddr;
do
{
ret = nrf_drv_twi_tx(&I2Cinstance_drv, DevAddr, &addr8, 1, true);
if (NRF_SUCCESS != ret)
break;
ret = nrf_drv_twi_rx(&&I2Cinstance_drv, DevAddr, pBuffer, DataSize);
debug_uart_printf("ret = 0d%d 0x%x \n\r", ret, ret);
} while(0);
debug_uart_printf("ret = 0d%d 0x%x \n\r", ret, ret);
return ret;
}
nrf_twi_drv_tx() returns a NACK ( error code 0x8201). I saw in the documentation that it may occurs after the nRF sends the address and the slave doesn't respond, so I assume I have implementation issue concerning slave address. What exactly is the index parameter in VL53LX_ReadMulti()? How can I pass this parameter to bus_i2c_ReadData()? index is uint16_t while ReadAddr is uint8_t. Thank you very much for your help!
2020-11-20 09:05 AM
UPDATE : the answer was dead simple : I forgot to shift the I2C adress like this :
status = (int8_t)bus_i2c_ReadData(DevAddr>>1, pdata, pdev->i2c_slave_address + index, count);
here is my final code for those who have the issue aswell :
VL53LX_Error VL53LX_ReadMulti(VL53LX_Dev_t *pdev, uint16_t index, uint8_t *pdata, uint32_t count)
{
VL53LX_Error status = VL53LX_ERROR_NONE;
uint8_t DevAddr = pdev->I2CDevAddr;
int32_t status_int;
uint8_t I2C_buff[2] = {0};
if (count>=VL53LX_COMMS_BUFFER_SIZE)
{
status = VL53LX_ERROR_INVALID_PARAMS;
}
if (count > (VL53LX_MAX_STRING_LENGTH_PLT))
{
return NRF_ERROR_INVALID_LENGTH;
}
status_int = bus_i2c_ReadData(DevAddr>>1, pdata, index, count);
if (status_int != 0)
status = VL53LX_ERROR_CONTROL_INTERFACE;
return status;
}
2020-11-20 09:47 AM
well done.
I2C addressing has always been an issue. Does one specify the write address 0x52 or the 7-bit address (0x29)?
I always thought specifiing it as 0x52/0x53 would be best as it's clearly the Write/Read.
But I'm guessing even with that some software wants the 7-bit address - as yours apparently does.
This bus has been around forever and putting the Write/Read bit in the LSB has caused issues from the beginning.