<?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>i2c driver from scratch using direct register access</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/28367/i2c-driver-from-scratch-using-direct-register-access</link><description>//-----------------------------------------------------------------------------------------------
 void i2c_init(unsigned char addr)
 { 
 
 NRF_TWI0-&amp;gt;PSELSCL=27;
 NRF_TWI0-&amp;gt;PSELSDA=26;
 
 NRF_TWI0-&amp;gt;ADDRESS=addr;
 
 //TWSR = 0x00;
 //NRF_TWI0</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 15 Dec 2017 15:21:44 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/28367/i2c-driver-from-scratch-using-direct-register-access" /><item><title>RE: i2c driver from scratch using direct register access</title><link>https://devzone.nordicsemi.com/thread/111918?ContentTypeID=1</link><pubDate>Fri, 15 Dec 2017 15:21:44 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c3d61a73-3651-480d-8937-2561072bf7c7</guid><dc:creator>vikrant8051</dc:creator><description>&lt;p&gt;[issue is solved !!]&lt;/p&gt;
&lt;p&gt;I found my mistake. Here I&amp;#39;m presenting my ( tested on nRF52840-PDK ) corrected version .....&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;//-----------------------------------------------------------------------------------------------------------

void i2c_init(unsigned char addr)
{ 

    NRF_TWI0-&amp;gt;PSELSCL=27;
    NRF_TWI0-&amp;gt;PSELSDA=26;

    NRF_TWI0-&amp;gt;ADDRESS=addr;

    //TWSR = 0x00;
    //NRF_TWI0-&amp;gt;FREQUENCY = 0x01980000;   //100 KHz 
    NRF_TWI0-&amp;gt;FREQUENCY = 0x06680000;     //400 Khz

    NRF_TWI0-&amp;gt;ENABLE = 0x00000005;
}


void i2c_startTX() 
{ 
    NRF_TWI0-&amp;gt;TASKS_STARTTX=1; 
}


void i2c_startRX() 
{ 
    NRF_TWI0-&amp;gt;TASKS_STARTRX=1;
}

void i2c_write(unsigned char data)
{ 
    NRF_TWI0-&amp;gt;TXD=data;
    while(NRF_TWI0-&amp;gt;EVENTS_TXDSENT==0){}
    NRF_TWI0-&amp;gt;EVENTS_TXDSENT=0;
}

unsigned char i2c_read(unsigned char nack)
{

    unsigned char data;
    
    if(nack==1)
    {
        NRF_TWI0-&amp;gt;TASKS_STOP=1;
    }

    while(NRF_TWI0-&amp;gt;EVENTS_RXDREADY==0){}  
    NRF_TWI0-&amp;gt;EVENTS_RXDREADY=0;

    data = NRF_TWI0-&amp;gt;RXD;

    return data;

}


void i2c_stopTX(void)
{
    NRF_TWI0-&amp;gt;TASKS_STOP=1;
    while(NRF_TWI0-&amp;gt;EVENTS_STOPPED==0){}
    NRF_TWI0-&amp;gt;EVENTS_STOPPED=0;
}

void i2c_stopRX(void)
{
    while(NRF_TWI0-&amp;gt;EVENTS_STOPPED==0){}
    NRF_TWI0-&amp;gt;EVENTS_STOPPED=0;
}

//--------------------------------------------------------------------------------------------------------


void eeprom_init()
{
    unsigned char cntr;

    i2c_init(0x50);  // EEPROM i2c address

    i2c_startTX();
    i2c_write(0x00);

    for(cntr=0;cntr&amp;lt;=15;cntr++)
    {
        i2c_write(&amp;#39;A&amp;#39; + cntr);
    }

    i2c_stopTX();

    nrf_delay_ms(10);  // delay for EEPROM write 

    serial_write_string(&amp;quot;EEPROM initialisation completed !!\n\r&amp;quot;);
}

void eeprom_read(unsigned char addr)
{
    unsigned char boo[17]={&amp;#39;\0&amp;#39;};
    unsigned char cntr;
    int data;

    i2c_startTX();
    i2c_write(addr);
    //i2c_stop();

    i2c_startRX();
    for(cntr=0;cntr&amp;lt;=15;cntr++)
    {
        if(cntr != 15)
        {
            boo[cntr]=i2c_read(0);
        }
        else
        {
            boo[cntr]=i2c_read(1);
        }
    }
    i2c_stopRX();

    serial_write_string(boo);    
    serial_write_string(&amp;quot;\n\r&amp;quot;);

}

//--------------------------------------------------------------------------------------------------------
&lt;/code&gt;&lt;/pre&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: i2c driver from scratch using direct register access</title><link>https://devzone.nordicsemi.com/thread/111916?ContentTypeID=1</link><pubDate>Fri, 15 Dec 2017 15:04:25 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:7be2bec1-c3b4-4725-b86a-77809ab1ff12</guid><dc:creator>vikrant8051</dc:creator><description>&lt;p&gt;Hey Jorgen Thanks for you clarification, I found my mistake.&lt;/p&gt;
&lt;p&gt;The TWI master read sequence is stopped by triggering the STOP task. This task must be triggered before
the last byte is extracted from RXD to ensure that the TWI master sends a NACK back to the slave before
generating the stop condition.&lt;/p&gt;
&lt;p&gt;I&amp;#39;ve not implemented this properly&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: i2c driver from scratch using direct register access</title><link>https://devzone.nordicsemi.com/thread/111917?ContentTypeID=1</link><pubDate>Fri, 15 Dec 2017 13:23:07 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c49fd25b-58bf-4b5b-b696-d366b07b5863</guid><dc:creator>J&amp;#248;rgen Holmefjord</dc:creator><description>&lt;p&gt;Please have a look at the &lt;a href="http://infocenter.nordicsemi.com/topic/com.nordic.infocenter.nrf52840.ps/twi.html?cp=2_0_0_47_3#concept_unj_wlw_sr"&gt;Master write sequence&lt;/a&gt; and &lt;a href="http://infocenter.nordicsemi.com/topic/com.nordic.infocenter.nrf52840.ps/twi.html?cp=2_0_0_47_4#concept_zqr_mtv_sr"&gt;Master read sequence&lt;/a&gt; documentation in the nRF52840 Objective Product Specifications.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: i2c driver from scratch using direct register access</title><link>https://devzone.nordicsemi.com/thread/111915?ContentTypeID=1</link><pubDate>Fri, 15 Dec 2017 13:01:10 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f5c66826-6b3f-4e8c-abaf-3192e95e871a</guid><dc:creator>vikrant8051</dc:creator><description>&lt;p&gt;serial_write_string() has no issue.&lt;/p&gt;
&lt;p&gt;I want to only know, how nrf52840 send ACk or NACK in background ?&lt;/p&gt;
&lt;p&gt;Actually what to send out of ACK or NACK, should be in control of developer !!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: i2c driver from scratch using direct register access</title><link>https://devzone.nordicsemi.com/thread/111914?ContentTypeID=1</link><pubDate>Fri, 15 Dec 2017 12:46:27 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ace738ac-33c6-40b7-b86b-17f946c7ff2e</guid><dc:creator>J&amp;#248;rgen Holmefjord</dc:creator><description>&lt;p&gt;What is in your &lt;code&gt;serial_write_string()&lt;/code&gt; function? Have you debugged the application to see how boo array is updated?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: i2c driver from scratch using direct register access</title><link>https://devzone.nordicsemi.com/thread/111913?ContentTypeID=1</link><pubDate>Fri, 15 Dec 2017 10:56:45 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:86c0944a-9228-41a7-82be-e814f944be90</guid><dc:creator>endnode</dc:creator><description>&lt;p&gt;These &lt;code&gt;while {}&lt;/code&gt; busy loops are soooooo ugly! Why you don&amp;#39;t do proper wait for event/interrupt and interrupt handler to catch I2C events?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>