<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Multi-instance TWI devices: style question &amp;amp; enhancement suggestion</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/20163/multi-instance-twi-devices-style-question-enhancement-suggestion</link><description>I have run into a challenge with the app_twi API, and I&amp;#39;d like to run it by you for your advice. To be clear, I am not blocked. But I am not pleased with the source code maintainability aesthetics of my current solution and I&amp;#39;d either like your advice</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Mon, 06 Mar 2017 13:27:58 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/20163/multi-instance-twi-devices-style-question-enhancement-suggestion" /><item><title>RE: Multi-instance TWI devices: style question &amp; enhancement suggestion</title><link>https://devzone.nordicsemi.com/thread/78537?ContentTypeID=1</link><pubDate>Mon, 06 Mar 2017 13:27:58 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6ff94216-68b1-477f-bdb6-c6a3175ac887</guid><dc:creator>bjorn-spockeli</dc:creator><description>&lt;p&gt;Hi Ray, I agree that it appears that the app_twi library is designed to communicate with a single TWI device, i.e statically-compiled for a single I2C address as you put it.  I will convey your feedback to the app_twi developer. In order to achieve the functionality you want, then I think that you will have to go down one abstraction layer and use the twi driver directly, i.e. &lt;code&gt;nrf_drv_twi.c/h&lt;/code&gt;, instead of the TWI Transaction Manager library(app_twi) which in turn uses the driver.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Multi-instance TWI devices: style question &amp; enhancement suggestion</title><link>https://devzone.nordicsemi.com/thread/78538?ContentTypeID=1</link><pubDate>Fri, 03 Mar 2017 20:07:39 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a3c8c12e-8d61-4afb-8f7a-9d3ab108788c</guid><dc:creator>Nguyen Hoan Hoang</dc:creator><description>&lt;p&gt;Perhaps you could have a look at the &lt;a href="http://embeddedsoftdev.blogspot.ca/p/ehal-nrf51.html"&gt;EHAL library from this blog&lt;/a&gt;.  It&amp;#39;s reusable object oriented multi-architecture. UART, SPI &amp;amp; I2C are available. An example for reading any EEPROM connected to I2C&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;static const I2CCFG s_I2C_CfgData = {
    0,  // i2c device number
    {
        { SDA_PORT, SDA_PIN, SDA_PINOP },
        { SCL_PORT, SCL_PIN, SCL_PINOP },
    },
    100000,     // Rate in Hz
    I2CMODE_MASTER,
    0,              // Device address (slave mode only)
    5,              // Maximum number of retries
    APP_IRQ_PRIORITY_LOW,      // Interrupt priority
   
};

I2C g_I2C_Instance;

static const I2CCFG s_I2C_CfgData2 = {
    1,  // i2c device number
    {
        { SDA_PORT, SDA_PIN, SDA_PINOP },
        { SCL_PORT, SCL_PIN, SCL_PINOP },
    },
    100000,     // Rate in Hz
    I2CMODE_MASTER,
    0,              // Device address (slave mode only)
    5,              // Maximum number of retries
    APP_IRQ_PRIORITY_LOW,      // Interrupt priority
   
};

I2C g_I2C_Instance2;

static const SEEP_CFG s_Eeprom_CfgData = {
    (0xA2 &amp;gt;&amp;gt; 1),    // Dev addr (7bits address)
    2,                   // Addressing length in bytes
    32,                 // Page size in bytes
    8192,              // 8KB eeprom
    5,                   // Write delays in sec
    {-1, -1, },
};
 
SEEP g_Eeprom_Instance;

int main()
{
    g_I2C_Instance.Init(s_I2C_CfgData);
    g_I2C_Instance2.Init(s_I2C_CfgData2);

    // use i2c0 instance 1
    g_Eeprom_Instance.Init(s_Eeprom_CfgData, &amp;amp;g_I2C_Instance);

    // use i2c1 instance 2
    g_Eeprom_Instance.Init(s_Eeprom_CfgData, &amp;amp;g_I2C_Instance2);

    uint8_t d[32];
    
    // Read 32 bytes eeprom at address 0x40
    // returns number of bytes read
    int count = g_Eeprom_Instance.Read(0x40, d, 32);

    // multiple devices using same I2C interface
    g_Dev2_Instance.Init(s_Dev2_CfgData, &amp;amp;g_I2C_Instance);
    count = g_Dev2_Instance.Read(0x40, d, 32);

    // your device can support SPI want to use SPI instead
    //
    g_SPI_Instance.Init(s_SPI_CfgData);

    // same eeprom now using SPI instead
    g_Eeprom_Instance.Init(s_Eeprom_CfgData, &amp;amp;g_SPI_Instance);

    count = g_Eeprom_Instance.Read(0x40, d, 32);
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As simple as that.  Same code on nRF5x, LPCxx series,..&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>