Teensy 4.0 I2C Read Specific Register Address Multiple Bytes

Cricket

Member
How do I read a specific register address?

Does this read starting from register address 0x00
Wire.requestFrom(6B, 3);
do I have to wait once while(Wire.available())
and then collect the data sequentially:
dataBuffer[0] = Wire.read();
dataBuffer[1] = Wire.read();
dataBuffer[2] = Wire.read();

while(Wire.available())
dataBuffer[0] = Wire.read();
while(Wire.available())
dataBuffer[1] = Wire.read();
while(Wire.available())
dataBuffer[2] = Wire.read();


to read from a specific register address (for example register 0x52) do I have to combine the read/write?

Wire.beginTransmission(0x6B);
Wire.write(0x52)
while(Wire.available())
dataBuffer[0] = Wire.read();
Wire.endTransmission();

or insert the request from also

Wire.beginTransmission(0x6B);
Wire.write(0x52)
Wire.requestFrom(6B, 1);
while(Wire.available())
dataBuffer[0] = Wire.read();
Wire.endTransmission();

and to put it all together, how would I read 3 bytes but starting at register address 0x52?

Thank you
 
How exactly depends on the specific I2C chip. Many of them are similar, there is no standard. In fact, I2C standard doesn't require any sort of register-based access model. But most chips do have that sort of access paradigm.

Usually beginTransmission() & endTransmission() are used to transmit the register number you wish to read. Then requestFrom() is used to actually read it. Many chips use just 1 byte to select the register, but some use 2 or more bytes. Some chips put various configuration bits into those bytes as well as the register address.

After you've read the register data, you may or may not need to transmit the register address again. Some chips keep the same address. Others automatically increment the address after each access. Some can do it either way, with a configuration bit to select which access style.

All these details depend on the specific chip. Since you didn't mention a part number or link to a datasheet, this generic message is the only advice I can give.

For many chips people have written libraries which do this Wire library stuff specific to that particular chip.
 
Yes, more or less, except those "while" loops. Probably more like this:

Wire.requestFrom(0x6B, 2);
if (Wire.available() == 2) {
dataBuffer[0] = Wire.read();
dataBuffer[1] = Wire.read();
}
Wire.endTransmission();
 
Im still confused, I believe my above example is incorrect.

The code below would seem to write 5242 to device/address 35 starting at register zero

Wire.beginTransmission(35);
Wire.write(5242)
Wire.endTransmission();

So how would I start writing at a different register, like 01?

Wire.beginTransmission(35, 01);
Wire.write(5242)
Wire.endTransmission();

similarly for the read??
I have successfully read 7 bytes from address 35, starting at register 0 using
Wire.requestFrom(35, 7)
without the Wire.beginTransmission(35) preceding it
Also the address being present in both commands seems redundant
my guess is that to read 7 bytes starting at register 1 : Wire.requestFrom(35, 7, 1) ??
 
is not starting at register zero an uncommon practice?
I think it would create problems, especially in the write function.
For example if you want to write to the second register but dont want to change the first register (it is read only, or if it is constantly changing and unknown and you dont want to tamper with it / mess it up)
 
I found that the teensy write function simply adds the write bit to the address and subsequently whatever you put in next
so for my IC the format is the MASTER: address+writeBit regsiter data data data.... which is what the teensy provides

my ICs read is ~the same MASTER: address+ReadBit regsiter SLAVE data data data.... BUT the teensy read function seems to only do MASTER: address+ReadBit SLAVE data data data....
is there something I am missing or is this functionality not possible for the teensy read function to send the register after?

Thank you
 
Back
Top