need help on twi

65 views
Skip to first unread message

Lumi

unread,
Feb 10, 2016, 6:33:25 AM2/10/16
to ioio-users
Dear all,

I am trying to talk with an external device via ioio-otg twi ports. I would like to write and read data to/from the registers of my external device.

Here are samples from my code:

byte[] request=new byte[]{0x01}; //data that I want to write
byte[] response = new byte[13];

In setup()( function:

i2c=ioio_.openTwiMaster(2, TwiMaster.Rate.RATE_100KHz, false);

In loop() function:

if(i2c.writeRead(0x63, false, request, request.length, null, 0)) { //Here, I only want to write 
if (i2c.writeRead(0x63, false, null, 0, response, response.length))// Here, I only want to read
{
for (int i = 0; i < 13; i++) {
pw.print(String.format("%02X", response[i]));
pw.print(" ");
}
}

Both writeRead() functions return true.
However, the data 0x01 in request array is interpreted as an internal address of a register in the device. I could not get 0x01 interpreted as data and written into external device register.

This may not be very clear, but maybe there are some people who can already spot the problem.

Thank you in advance,
Regards.

Lumi

unread,
Feb 10, 2016, 8:08:51 AM2/10/16
to ioio-users
Hello

I found a figure (attached) explaining how the communication should take place, I believe this helps to clarify the issue.

Thanks.
Screen Shot 2016-02-10 at 14.06.00.png

Ytai Ben-Tsvi

unread,
Feb 10, 2016, 10:23:30 AM2/10/16
to ioio-...@googlegroups.com

Why are you separating this into two transactions? It is very uncommon.

--
You received this message because you are subscribed to the Google Groups "ioio-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ioio-users+...@googlegroups.com.
To post to this group, send email to ioio-...@googlegroups.com.
Visit this group at https://groups.google.com/group/ioio-users.
For more options, visit https://groups.google.com/d/optout.

Lumi

unread,
Feb 11, 2016, 4:25:54 AM2/11/16
to ioio-users
Hi Ytai,

You are right, I can do it in one writeRead() call. As I did below:

byte[] request=new byte[]{0x01}; //data that I want to write
byte[] response = new byte[1];

In setup()( function:

i2c=ioio_.openTwiMaster(2, TwiMaster.Rate.RATE_100KHz, false);

In loop() function:

if(i2c.writeRead(0x63, false, request, request.length, response, response.length)) //This call returns true 
{
pw.print(String.format("%02X", response[0]));
pw.print(" ");
}

However, my problem still remains. The data (0x01) in request interpreted as starting address of read. Therefore, I read the default data in the register which has the address 0x01. However, I would like to write 0x01 as (data) into the first register of device and read it back.

What might am I doing wrong ? Is there a way to log/dump writeRead() call ?

Thank you in advance.
Regards,

Lumi

unread,
Feb 11, 2016, 6:31:16 AM2/11/16
to ioio-users
I would like to give some more information:

I modified IOIOSimpleApp and kept the configuration the same in AndroidManifest.xml and I am running app on the android phone in OpenAccessory mode connected to ioio-otg with usb cable.

Do I need to change anything to AndroidManifest.xml (such as adding permission for usb) ?

Ytai Ben-Tsvi

unread,
Feb 11, 2016, 10:29:12 AM2/11/16
to ioio-...@googlegroups.com

If you want to write a byte, you typically perform a write read operation that has 2 bytes (register, value) on the write buffer and an empty read buffer. Then you can read back using another transaction, like the one you have.

Lumi

unread,
Feb 11, 2016, 10:57:14 AM2/11/16
to ioio-users
Thanks Ytai, that worked !!

I paste the working code below, in case someone need such an example in the future:

byte[] write_reg_data = {(byte)0x03, (byte)0x20};// write 0x20 into register 0x03
byte[] empty_read ={}; //empty buffer passed to first writeRead below (to enable write only)
byte[] read_reg ={(byte)0x01};//start address for reading data, used in second writeRead call
byte[] read_buffer = {(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00}; // buffer to keep read elements

if(i2c.writeRead(0x63, false, write_reg_data, write_reg_data.length, empty_read,0))
if(i2c.writeRead(0x63, false, read_reg, read_reg.length, read_buffer, read_buffer.length))
{
for (int i = 0; i < 3; i++) {
pw.print(String.format("%02X", read_buffer[i])); //print out first three elements of read_buffer
}
}

Joseph Gonzalez

unread,
Mar 7, 2018, 2:45:25 PM3/7/18
to ioio-users
Hi there. I am trying to do something similar with the IOIO board and I followed your example. My trouble is the saveData (your read_buffer) variable is not saving what is being read. The first screenshot is of my write function. The second is of my read function where the savaData is not saving. The last is of the output in the Log.

The variable measurementArray is the register I am trying to read from and distanceArray is the saveData. These are the inputs to the read function. The Boolean parameter is not being used at the moment so that could be ignored for now.
 
private byte[] measurementArray = {(byte) 0x0f};
byte[] distanceArray = {(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00};

private void read(byte address, boolean monitorBusyFlag, byte[] register, byte[] saveData) throws ConnectionLostException, InterruptedException

read(LIDAR_ADDRESS, measurementArray, distanceArray)

Would you be able to see what I am possibly missing?
Screenshot (7).png
Screenshot (8).png
Screenshot (9).png
Reply all
Reply to author
Forward
0 new messages