Skip to content

Commit cef46be

Browse files
committed
initial impl of changing i2c address on device. Compiles.
1 parent 0f35732 commit cef46be

File tree

2 files changed

+74
-4
lines changed

2 files changed

+74
-4
lines changed

src/qwiic_tmf882x.cpp

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,48 @@ bool QwDevTMF882X::isConnected()
168168
return (_i2cBus && _i2cAddress ? _i2cBus->ping(_i2cAddress) : false);
169169
}
170170

171+
//////////////////////////////////////////////////////////////////////////////
172+
// setI2CAddress()
173+
//
174+
// Change the address of the connected device.
175+
//
176+
177+
bool QwDevTMF882X::setI2CAddress(uint8_t address)
178+
{
179+
// Initialized? Is the address legal?
180+
if (!_isInitialized || address < 0x08 || address > 0x77)
181+
false;
182+
183+
// is the address the same as already set?
184+
if (address == _i2cAddress)
185+
return true;
186+
187+
// Okay, go time -- get the config
188+
struct tmf882x_mode_app_config tofConfig;
189+
190+
// Get the config struct from the underlying SDK
191+
if (tmf882x_ioctl(&_TOF, IOCAPP_GET_CFG, NULL, &tofConfig))
192+
return false;
193+
194+
// change the address in the config
195+
tofConfig.i2c_slave_addr = address;
196+
197+
// set it.
198+
if (tmf882x_ioctl(&_TOF, IOCAPP_SET_CFG, &tofConfig, NULL))
199+
return false;
171200

201+
// Now tell the device to switch to the address in the config page.
202+
uint8_t cmdCode = TMF8X2X_COM_CMD_STAT__cmd_stat__CMD_I2C_SLAVE_ADDRESS;
203+
if ( _i2cBus->writeRegisterRegion(_i2cAddress, TMF8X2X_COM_CMD_STAT, &cmdCode, sizeof(uint8_t)) )
204+
return false;
205+
206+
// Potential TODO - check status register... .. need to test.
207+
208+
// If we are here, the address should of been changed
209+
_i2cAddress = address;
210+
return true;
211+
212+
}
172213
//////////////////////////////////////////////////////////////////////////////
173214
//
174215
bool QwDevTMF882X::getApplicationVersion(char *pVersion, uint8_t vlen)
@@ -546,10 +587,10 @@ bool QwDevTMF882X::setSPADConfig(struct tmf882x_mode_app_spad_config &spadConfig
546587
//
547588
// TODO - In the *future*, generalize to match SDK
548589

549-
void QwDevTMF882X::setCommBus(QwI2C &theBus, uint8_t id_bus)
590+
void QwDevTMF882X::setCommBus(QwI2C &theBus, uint8_t idBus)
550591
{
551-
_i2cBus = &theBus;
552-
_i2cAddress = id_bus;
592+
_i2cBus = &theBus;
593+
_i2cAddress = idBus;
553594
}
554595

555596
//////////////////////////////////////////////////////////////////////////////

src/qwiic_tmf882x.h

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,35 @@ class QwDevTMF882X
126126

127127
bool isConnected(); // Checks if sensor ack's the I2C request
128128

129+
///////////////////////////////////////////////////////////////////////
130+
// setI2CAddress()
131+
//
132+
// Set/Change the address of the connected device.
133+
//
134+
// Called after the device has been initialized.
135+
//
136+
// Parameter Description
137+
// --------- -----------------------------
138+
// address The new address for the device
139+
// retval true on success, false on failure
140+
141+
bool setI2CAddress(uint8_t address);
142+
143+
///////////////////////////////////////////////////////////////////////
144+
// getI2CAddress()
145+
//
146+
// Returns the i2c address for the connected device
147+
//
148+
// Called after the device has been initialized.
149+
//
150+
// Parameter Description
151+
// --------- -----------------------------
152+
// retval The device i2c address. 0 if the device isn't conntected
153+
154+
uint8_t getI2CAddress(void)
155+
{
156+
return _i2cAddress;
157+
}
129158
///////////////////////////////////////////////////////////////////////
130159
// getApplicationVersion()
131160
//
@@ -458,7 +487,7 @@ class QwDevTMF882X
458487
{
459488
return _TOF;
460489
}
461-
490+
462491
//////////////////////////////////////////////////////////////////////////////////
463492
// setDebug()
464493
//

0 commit comments

Comments
 (0)