Skip to content

Commit 7b06138

Browse files
committed
Experimental multiple i2c bus support
1 parent eaea567 commit 7b06138

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, uint16_t reg) = NULL;
119-
120-
// calls read_i2c_block_data(address, reg, length)
121-
static int (*i2c_read_func)(uint8_t address, uint16_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, uint16_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, uint16_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, uint16_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, uint16_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, uint16_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, uint16_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, uint16_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, uint16_t reg);
92+
93+
// calls read_i2c_block_data(address, reg, length)
94+
int (*i2c_read_func)(uint8_t address, uint16_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, uint16_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
@@ -110,7 +110,14 @@ def __init__(self, i2c_bus=1, i2c_address=0x29, tca9548a_num=255, tca9548a_addr=
110110
def open(self, reset=False):
111111
self._i2c.open(bus=self._i2c_bus)
112112
self._configure_i2c_library_functions()
113-
self._dev = _TOF_LIBRARY.initialise(self.i2c_address, self._tca9548a_num, self._tca9548a_addr, reset)
113+
self._dev = _TOF_LIBRARY.initialise(
114+
self.i2c_address,
115+
self._tca9548a_num,
116+
self._tca9548a_addr,
117+
reset,
118+
self._i2c_multi_func,
119+
self._i2c_read_func,
120+
self._i2c_write_func)
114121

115122
def close(self):
116123
self._i2c.close()
@@ -159,7 +166,7 @@ def _i2c_multi(address, reg):
159166
self._i2c_multi_func = _I2C_MULTI_FUNC(_i2c_multi)
160167
self._i2c_read_func = _I2C_READ_FUNC(_i2c_read)
161168
self._i2c_write_func = _I2C_WRITE_FUNC(_i2c_write)
162-
_TOF_LIBRARY.VL53L1_set_i2c(self._i2c_multi_func, self._i2c_read_func, self._i2c_write_func)
169+
# _TOF_LIBRARY.VL53L1_set_i2c(self._i2c_multi_func, self._i2c_read_func, self._i2c_write_func)
163170

164171
# The ROI is a square or rectangle defined by two corners: top left and bottom right.
165172
# 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;
@@ -78,6 +78,10 @@ VL53L1_DEV *initialise(uint8_t i2c_address, uint8_t TCA9548A_Device, uint8_t TCA
7878
dev->TCA9548A_Device = TCA9548A_Device;
7979
dev->TCA9548A_Address = TCA9548A_Address;
8080

81+
dev->i2c_multi_func = multi_func;
82+
dev->i2c_read_func = read_func;
83+
dev->i2c_write_func = write_func;
84+
8185
if(perform_reset){
8286
Status = VL53L1_software_reset(dev);
8387
dev->I2cDevAddr = 0x29; // Resetting will reset i2c address back to default
@@ -120,7 +124,7 @@ VL53L1_DEV *initialise(uint8_t i2c_address, uint8_t TCA9548A_Device, uint8_t TCA
120124
VL53L1_PerformRefSpadManagement(dev);
121125
VL53L1_SetXTalkCompensationEnable(dev, 0); // Disable crosstalk compensation (bare sensor)
122126

123-
return dev;
127+
return (VL53L1_DEV *)dev;
124128
}
125129

126130
VL53L1_Error setDeviceAddress(VL53L1_Dev_t *dev, int i2c_address)

0 commit comments

Comments
 (0)