At some point hopefully the standard Wire library will support Wire1. Once I am semi happy with my version may issue Pull Request. I have not been doing any testing yet on TLC, mainly working with T3.6.
Looks like the basic Client stuff is working. I have a simiple sketch that acts like there are 8 registers that the Master can read or write from the slave. I have both the client and master in same sketch, and I tried with Wire as master talking to Wire1, then Wire2, then Wire3. I then had Wire3 as master talking to Wire and then to Wire2... So I think most combinations should work.
If anyone is interested the sketch looks like:
Code:
// Quick and dirty I2c Master/Slave combination test.
#define SLAVE_ADDR 8
#include <Wire.h>
#define SLAVE_I2C Wire
#define MASTER_I2C Wire3
#define REG_COUNT 8
extern void requestEvent();
void setup()
{
while (!Serial && (millis() < 3000)) ;
Serial.begin(115200);
delay(250);
// Start up both Master and save SPIs
Serial.println("Before Master begin");
MASTER_I2C.begin(); // join i2c bus (address optional for master)
Serial.println("Before Slave Begin");
SLAVE_I2C.onReceive(receiveEvent); // register a slave even handler
SLAVE_I2C.onRequest(requestEvent);
SLAVE_I2C.begin(SLAVE_ADDR);
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
}
uint8_t vals_to_write = REG_COUNT;
void loop()
{
digitalWrite(13, HIGH);
for (uint8_t i = 0; i < REG_COUNT; i++) {
uint8_t reg_val = ReadReg(i);
Serial.printf("Reg %d = %d\n", i, reg_val);
}
// lets try updating all of them...
Serial.println("Update values");
for (uint8_t i = 0; i < REG_COUNT; i++) {
WriteReg(i, vals_to_write++);
}
digitalWrite(13, LOW);
Serial.println("\nHit any key to repeat");
while (Serial.read() == -1) ;
while (Serial.read() != -1) ; // get rid of other data.
}
//==========================================================================================
// Some master request or set register functions
uint8_t ReadReg(uint8_t reg) {
MASTER_I2C.beginTransmission(SLAVE_ADDR); // transmit to device #8
MASTER_I2C.write(0); // Do a read
MASTER_I2C.write(reg);
MASTER_I2C.write(1); // one byte
MASTER_I2C.endTransmission(); // stop transmitting
// Then request data from other side
if (MASTER_I2C.requestFrom(SLAVE_ADDR, 1)) { // Try to get the data back from the other side
return MASTER_I2C.read(); // read in the result and return it.
}
}
uint8_t WriteReg(uint8_t reg, uint8_t val) {
MASTER_I2C.beginTransmission(SLAVE_ADDR); // transmit to device #8
MASTER_I2C.write(1); // Do a read
MASTER_I2C.write(reg);
MASTER_I2C.write(val); // one byte
MASTER_I2C.endTransmission(); // stop transmitting
}
//===========================================================================================
// Some slave registers and state values
uint8_t registers[REG_COUNT] = {0, 1, 2, 3, 4, 5, 6, 7}; // some dummy registers to play with.
uint8_t read_write = 0;
uint8_t start_reg = 0;
uint8_t cnt_reg = 1;
void receiveEvent(int count_bytes)
{
read_write = SLAVE_I2C.read(); // first byte is command
if (count_bytes > 1) start_reg = SLAVE_I2C.read();
if (read_write) {
while (SLAVE_I2C.available()) {
registers[start_reg++] = SLAVE_I2C.read();
}
} else {
// read mode
if (SLAVE_I2C.available()) {
cnt_reg = SLAVE_I2C.read();
}
}
}
void requestEvent()
{
// Host is asking for data.
for (uint8_t i = 0; i < cnt_reg; i++) {
SLAVE_I2C.write(registers[start_reg++]);
}
}