Does lib_i2c support I2C on 2 different 4-bit ports Topic is solved

If you have a simple question and just want an answer.
User avatar
ccrome
Active Member
Posts: 62
Joined: Wed Sep 23, 2015 1:15 am

Does lib_i2c support I2C on 2 different 4-bit ports

Post by ccrome »

Hello,
I have a board (the DSP4YOU AVB-DG board), which puts I2C_SCL on PORT_4A[0] and SDA on PORT_4B[0]. This board's software uses the older module_i2c, rather than lib_i2c, and the board works fine when using their software.

I'm repurposing the board, and having trouble getting lib_i2c to work with this setup.

The I2C address that gets sent seems to have little to do with the I2C address that is passed it :-)

Code: Select all

#include <xs1.h>
#include <platform.h>
#include <i2s.h>
#include <gpio.h>
#include <i2c.h>
#include <stdio.h>


port p_sda = XS1_PORT_4A;
port p_scl = XS1_PORT_4B;

void run_tile_1(client i2c_master_if i2c)
{
    uint8_t data[2];
    i2c_res_t res;
    data[0]=0xff;
    data[1] = 0xff;
    res = i2c.read(1, data, 1, 1);
    res = i2c.read(2, data, 1, 1);
    res = i2c.read(4, data, 1, 1);
    res = i2c.read(8, data, 1, 1);
    res = i2c.read(16, data, 1, 1);
    res = i2c.read(32, data, 1, 1);
    res = i2c.read(64, data, 1, 1);
    res = i2c.read(128, data, 1, 1);
    while(1);
}
int main(void)
{
    i2c_master_if i2c[1];
    par {
        on tile[1] : i2c_master(i2c, 1, p_scl, p_sda, 100);
        on tile[1] : run_tile_1(i2c[0]);
    }
    return 0;
}
And the addresses that appear on the bus for reads of address 1, 2, 4, 8, 16, 32, 64, 128 are, respectively:

addr in read() (decimal), addr on bus (binary)
1, 0000000
2, 0000000
4, 0100000,
8, 0000000,
16, 0000000,
32, 0000000,
64, 1000000,
128, 0000000,

Other than the SDA and SCL, there is nothing connected to ports 4A and 4B.

Any idea what could be going on? It seems like the 4-bit ports are throwing the library off.


View Solution
User avatar
ccrome
Active Member
Posts: 62
Joined: Wed Sep 23, 2015 1:15 am

Post by ccrome »

In answer to my own question: It appears to me that there is a bug in the lib_i2c if the SDA port is multi-bit. This patch makes it work correctly:

Code: Select all

diff --git a/lib_i2c/src/i2c_master.xc b/lib_i2c/src/i2c_master.xc
index 7e17e2e..a84d39a 100644
--- a/lib_i2c/src/i2c_master.xc
+++ b/lib_i2c/src/i2c_master.xc
@@ -90,7 +90,9 @@ static int tx8(port p_scl, port p_sda, unsigned data,
                unsigned &fall_time) {
   unsigned CtlAdrsData = ((unsigned) bitrev(data)) >> 24;
   for (int i = 8; i != 0; i--) {
-    p_sda <: >> CtlAdrsData;
+    int b = CtlAdrsData & 0x1;
+    p_sda <: b;
+    CtlAdrsData >>= 1;
     high_pulse(p_scl, bit_time, fall_time);
   }
   return high_pulse_sample(p_scl, p_sda, bit_time, fall_time);
Where is the right place to submit this patch for consideration?
srinie
XCore Addict
Posts: 158
Joined: Thu Mar 20, 2014 8:04 am

Post by srinie »

Thanks for the update.
Please submit the patch here:
https://github.com/xmos/lib_i2c
User avatar
Ross
XCore Expert
Posts: 968
Joined: Thu Dec 10, 2009 9:20 pm
Location: Bristol, UK

Post by Ross »

Thanks, I have submitted the patch