Best practices upgrading from i2c_t3

Status
Not open for further replies.

kdharbert

Well-known member
I want to use the this product with a Teensy4.x:
https://www.tindie.com/products/onehorse/usfsmax-module-and-carrier/

The library is built for Teensy3.x and I need to upgrade it. It seems pretty straight forward to change the calls to use Wire.xxxxx(), but it seems there is still some conflict with enumerations established in the i2c_t3.h. If I don't include, i2c_t3.h and place the needed enumerations in my own code, I get definition conflicts that don't make sense. Its saying I have a duplicate definition between my own file and some cached sketch file. I don't know how to work around it, and likely there is a better process.

Specifically, I think some of the way the transactions are set up can be accomplished with one call to Wire. Please confirm.

My basic steps are to:
Omit including i2c_t3.h,
Change calls over to use Wire
Add necessary enumerations to I2CDev.h.
Change function calls to not to need the i2c_t3 object passed in.

I'm not sure if there is a simpler method. Here's unmodified code from I2Cdev.cpp:

#include "Arduino.h"
#include "I2Cdev.h"

I2Cdev::I2Cdev(i2c_t3* i2c_bus)
{
_I2C_Bus = i2c_bus;
}


/**
* @fn: readByte(uint8_t address, uint8_t subAddress)
*
* @brief: Read one byte from an I2C device
*
* @params: I2C slave device address, Register subAddress
* @returns: unsigned short read
*/
uint8_t I2Cdev::readByte(uint8_t address, uint8_t subAddress)
{
uint8_t data; // `data` will store the register data

_I2C_Bus->beginTransmission(address); // Initialize the Tx buffer
_I2C_Bus->write(subAddress); // Put slave register address in Tx buffer
_I2C_Bus->endTransmission(I2C_NOSTOP); // Send the Tx buffer, but send a restart to keep connection alive
_I2C_Bus->requestFrom(address, (size_t) 1); // Read one byte from slave register address
data = _I2C_Bus->read(); // Fill Rx buffer with result
return data; // Return data read from slave register
}

/**
* @fn: readBytes(uint8_t address, uint8_t subAddress, uint8_t count, uint8_t * dest)
*
* @brief: Read multiple bytes from an I2C device
*
* @params: I2C slave device address, Register subAddress, number of btes to be read, aray to store the read data
* @returns: void
*/
void I2Cdev::readBytes(uint8_t address, uint8_t subAddress, uint8_t count, uint8_t * dest)
{
uint8_t i = 0;

_I2C_Bus->beginTransmission(address); // Initialize the Tx buffer
_I2C_Bus->write(subAddress); // Put slave register address in Tx buffer
_I2C_Bus->endTransmission(I2C_NOSTOP); // Send the Tx buffer, but send a restart to keep connection alive
_I2C_Bus->requestFrom(address, (size_t) count); // Read bytes from slave register address
while (_I2C_Bus->available())
{
dest[i++] = _I2C_Bus->read();
} // Put read results in the Rx buffer
}

/**
* @fn: writeByte(uint8_t devAddr, uint8_t regAddr, uint8_t data)
*
* @brief: Write one byte to an I2C device
*
* @params: I2C slave device address, Register subAddress, data to be written
* @returns: void
*/
void I2Cdev::writeByte(uint8_t devAddr, uint8_t regAddr, uint8_t data)
{
writeBytes(devAddr, regAddr, 1, &data);
}

/**
* @fn: writeBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t *data)
*
* @brief: Write multiple bytes to an to an I2C device
*
* @params: I2C slave device address, Register subAddress, data to be written
* @params: number of bytes to be written, data array to be written
* @returns: void
*/
void I2Cdev::writeBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t *data)
{
uint8_t status = 0;

_I2C_Bus->beginTransmission(devAddr);
_I2C_Bus->write((uint8_t) regAddr);
for (uint8_t i=0; i < length; i++)
{
_I2C_Bus->write((uint8_t)data);
}
status = _I2C_Bus->endTransmission();
}

/**
* @fn:I2Cscan()
* @brief: Scan the I2C bus for active I2C slave devices
*
* @params: void
* @returns: void
*/
void I2Cdev::I2Cscan()
{
// Scan for i2c devices
byte error, address;
int nDevices;

Serial.println("Scanning...");

nDevices = 0;
for(address = 1; address < 127; address++ )
{
// The i2c_scanner uses the return value of the Wire.endTransmisstion to see if a device did acknowledge to the address.
_I2C_Bus->beginTransmission(address);
error = _I2C_Bus->endTransmission();

if (error == 0)
{
Serial.print("I2C device found at address 0x");
if (address<16)
Serial.print("0");
Serial.print(address,HEX);
Serial.println(" !");
nDevices++;
}
else if (error==4)
{
Serial.print("Unknow error at address 0x");
if (address<16)
Serial.print("0");
Serial.println(address,HEX);
}
}
if (nDevices == 0)
Serial.println("No I2C devices found\n");
else
Serial.println("I2C scan complete\n");
}
 
Status
Not open for further replies.
Back
Top