Skip to content

Commit 2307bd5

Browse files
committed
Experimental multiple i2c bus support
1 parent a0418c5 commit 2307bd5

5 files changed

Lines changed: 37 additions & 26 deletions

File tree

api/platform/vl53l1_platform.c

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -114,32 +114,21 @@
114114
// return Status;
115115
// }
116116

117-
// calls the i2c write to multiplexer function
118-
static int (*i2c_multi_func)(uint8_t address, uint8_t reg) = NULL;
119-
120-
// calls read_i2c_block_data(address, reg, length)
121-
static int (*i2c_read_func)(uint8_t address, uint8_t reg,
122-
uint8_t *list, uint8_t length) = NULL;
123-
124-
// calls write_i2c_block_data(address, reg, list)
125-
static int (*i2c_write_func)(uint8_t address, uint8_t reg,
126-
uint8_t *list, uint8_t length) = NULL;
127-
128117
static pthread_mutex_t i2c_mutex = PTHREAD_MUTEX_INITIALIZER;
129118

130-
void VL53L1_set_i2c(void *multi_func, void *read_func, void *write_func)
119+
void VL53L1_set_i2c(VL53L1_DEV Dev, void *multi_func, void *read_func, void *write_func)
131120
{
132-
i2c_multi_func = multi_func;
133-
i2c_read_func = read_func;
134-
i2c_write_func = write_func;
121+
Dev->i2c_multi_func = multi_func;
122+
Dev->i2c_read_func = read_func;
123+
Dev->i2c_write_func = write_func;
135124
}
136125

137126
static int i2c_write(VL53L1_DEV Dev, uint8_t cmd,
138127
uint8_t *data, uint8_t len)
139128
{
140129
int result = VL53L1_ERROR_NONE;
141130

142-
if (i2c_write_func != NULL)
131+
if (Dev->i2c_write_func != NULL)
143132
{
144133
if (Dev->TCA9548A_Device < 8)
145134
{
@@ -149,7 +138,7 @@ static int i2c_write(VL53L1_DEV Dev, uint8_t cmd,
149138
pthread_mutex_lock(&i2c_mutex);
150139

151140
// Write to the multiplexer
152-
if (i2c_multi_func(Dev->TCA9548A_Address, (1 << Dev->TCA9548A_Device)) < 0)
141+
if (Dev->i2c_multi_func(Dev->TCA9548A_Address, (1 << Dev->TCA9548A_Device)) < 0)
153142
{
154143
printf("i2c bus on multiplexer not set.\n");
155144
result = VL53L1_ERROR_CONTROL_INTERFACE;
@@ -158,7 +147,7 @@ static int i2c_write(VL53L1_DEV Dev, uint8_t cmd,
158147

159148
if (result == VL53L1_ERROR_NONE)
160149
{
161-
if (i2c_write_func(Dev->I2cDevAddr, cmd, data, len) < 0)
150+
if (Dev->i2c_write_func(Dev->I2cDevAddr, cmd, data, len) < 0)
162151
{
163152
result = VL53L1_ERROR_CONTROL_INTERFACE;
164153
}
@@ -183,7 +172,7 @@ static int i2c_read(VL53L1_DEV Dev, uint8_t cmd,
183172
{
184173
int result = VL53L1_ERROR_NONE;
185174

186-
if (i2c_read_func != NULL)
175+
if (Dev->i2c_read_func != NULL)
187176
{
188177
if (Dev->TCA9548A_Device < 8)
189178
{
@@ -193,7 +182,7 @@ static int i2c_read(VL53L1_DEV Dev, uint8_t cmd,
193182
pthread_mutex_lock(&i2c_mutex);
194183

195184
// Write to the multiplexer
196-
if (i2c_multi_func(Dev->TCA9548A_Address, (1 << Dev->TCA9548A_Device)) < 0)
185+
if (Dev->i2c_multi_func(Dev->TCA9548A_Address, (1 << Dev->TCA9548A_Device)) < 0)
197186
{
198187
printf("i2c bus on multiplexer not set.\n");
199188
result = VL53L1_ERROR_CONTROL_INTERFACE;
@@ -202,7 +191,7 @@ static int i2c_read(VL53L1_DEV Dev, uint8_t cmd,
202191

203192
if (result == VL53L1_ERROR_NONE)
204193
{
205-
if (i2c_read_func(Dev->I2cDevAddr, cmd, data, len) < 0)
194+
if (Dev->i2c_read_func(Dev->I2cDevAddr, cmd, data, len) < 0)
206195
{
207196
result = VL53L1_ERROR_CONTROL_INTERFACE;
208197
}

api/platform/vl53l1_platform.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ extern "C"
7676
#endif
7777

7878

79-
void VL53L1_set_i2c(void *multi_func, void *read_func, void *write_func);
79+
void VL53L1_set_i2c(VL53L1_DEV pdev, void *multi_func, void *read_func, void *write_func);
8080

8181
VL53L1_Error VL53L1_CommsInitialise(
8282
VL53L1_DEV pdev,

api/platform/vl53l1_platform_user_data.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,17 @@ typedef struct {
8787
uint8_t TCA9548A_Device; /*!< Device number on TCA9548A I2C Multiplexer or 255 if TCA9548A not being used */
8888
uint8_t TCA9548A_Address; /*!< Address of TCA9548A I2C Multiplexer or 255 if TCA9548A not being used */
8989

90+
// calls the i2c write to multiplexer function
91+
int (*i2c_multi_func)(uint8_t address, uint8_t reg);
92+
93+
// calls read_i2c_block_data(address, reg, length)
94+
int (*i2c_read_func)(uint8_t address, uint8_t reg,
95+
uint8_t *list, uint8_t length);
96+
97+
// calls write_i2c_block_data(address, reg, list)
98+
int (*i2c_write_func)(uint8_t address, uint8_t reg,
99+
uint8_t *list, uint8_t length);
100+
90101
} VL53L1_Dev_t;
91102

92103
typedef VL53L1_Dev_t *VL53L1_DEV;

python/VL53L1X.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,14 @@ def __init__(self, i2c_bus=1, i2c_address=0x29, tca9548a_num=255, tca9548a_addr=
107107
def open(self, reset=False):
108108
self._i2c.open(bus=self._i2c_bus)
109109
self._configure_i2c_library_functions()
110-
self._dev = _TOF_LIBRARY.initialise(self.i2c_address, self._tca9548a_num, self._tca9548a_addr, reset)
110+
self._dev = _TOF_LIBRARY.initialise(
111+
self.i2c_address,
112+
self._tca9548a_num,
113+
self._tca9548a_addr,
114+
reset,
115+
self._i2c_multi_func,
116+
self._i2c_read_func,
117+
self._i2c_write_func)
111118

112119
def close(self):
113120
self._i2c.close()
@@ -156,7 +163,7 @@ def _i2c_multi(address, reg):
156163
self._i2c_multi_func = _I2C_MULTI_FUNC(_i2c_multi)
157164
self._i2c_read_func = _I2C_READ_FUNC(_i2c_read)
158165
self._i2c_write_func = _I2C_WRITE_FUNC(_i2c_write)
159-
_TOF_LIBRARY.VL53L1_set_i2c(self._i2c_multi_func, self._i2c_read_func, self._i2c_write_func)
166+
# _TOF_LIBRARY.VL53L1_set_i2c(self._i2c_multi_func, self._i2c_read_func, self._i2c_write_func)
160167

161168
# The ROI is a square or rectangle defined by two corners: top left and bottom right.
162169
# Default ROI is 16x16 (indices 0-15). The minimum ROI size is 4x4.

python_lib/vl53l1x_python.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ static VL53L1_RangingMeasurementData_t *pRangingMeasurementData = &RangingMeasur
4545
* being used. If not being used, set to 0.
4646
* @retval The Dev Object to pass to other library functions.
4747
*****************************************************************************/
48-
VL53L1_DEV *initialise(uint8_t i2c_address, uint8_t TCA9548A_Device, uint8_t TCA9548A_Address, uint8_t perform_reset)
48+
VL53L1_DEV *initialise(uint8_t i2c_address, uint8_t TCA9548A_Device, uint8_t TCA9548A_Address, uint8_t perform_reset, void *multi_func, void *read_func, void *write_func)
4949
{
5050
VL53L1_Error Status = VL53L1_ERROR_NONE;
5151
uint32_t refSpadCount;
@@ -74,6 +74,10 @@ VL53L1_DEV *initialise(uint8_t i2c_address, uint8_t TCA9548A_Device, uint8_t TCA
7474
dev->TCA9548A_Device = TCA9548A_Device;
7575
dev->TCA9548A_Address = TCA9548A_Address;
7676

77+
dev->i2c_multi_func = multi_func;
78+
dev->i2c_read_func = read_func;
79+
dev->i2c_write_func = write_func;
80+
7781
if(perform_reset){
7882
Status = VL53L1_software_reset(dev);
7983
dev->I2cDevAddr = 0x29; // Resetting will reset i2c address back to default
@@ -97,7 +101,7 @@ VL53L1_DEV *initialise(uint8_t i2c_address, uint8_t TCA9548A_Device, uint8_t TCA
97101
VL53L1_PerformRefSpadManagement(dev);
98102
VL53L1_SetXTalkCompensationEnable(dev, 0); // Disable crosstalk compensation (bare sensor)
99103

100-
return dev;
104+
return (VL53L1_DEV *)dev;
101105
}
102106

103107
VL53L1_Error setDeviceAddress(VL53L1_Dev_t *dev, int i2c_address)

0 commit comments

Comments
 (0)