<?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>NRF9160 I2C/TWI recovery and microsecond resolution timer</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/74731/nrf9160-i2c-twi-recovery-and-microsecond-resolution-timer</link><description>Hi, 
 Something locks-up my I2C sometimes and I need to make I2C recovery. It looks like Nordic has not implemented the zephyr i2c_recover_bus function, so I guess I need to make it manually? 
 I&amp;#39;ve already drafted a somehow working code: 
 
 But I am</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 11 May 2021 14:09:05 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/74731/nrf9160-i2c-twi-recovery-and-microsecond-resolution-timer" /><item><title>RE: NRF9160 I2C/TWI recovery and microsecond resolution timer</title><link>https://devzone.nordicsemi.com/thread/309286?ContentTypeID=1</link><pubDate>Tue, 11 May 2021 14:09:05 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6ed308df-47fe-4d4a-842d-f1b9524fe5a5</guid><dc:creator>Simon</dc:creator><description>[quote user="fastfox"]But as I will make this recovery just before a reboot, it might just save my butt here. But obviously, this is not the right way to do it as the bus goes haywire after the operation.[/quote]
&lt;p&gt;I think just rebooting might be the best option, if possible. As mentioned&amp;nbsp;i&lt;span&gt;n the thread&amp;nbsp;&lt;/span&gt;&lt;a href="https://devzone.nordicsemi.com/f/nordic-q-a/21508/twi-stuck-bus-recovery"&gt;TWI Stuck Bus Recovery&lt;/a&gt;&lt;span&gt;&amp;nbsp;it was recommended to&amp;nbsp;&lt;/span&gt;&lt;span&gt;reinitialize the I2C driver (not run the recovery/clear function). When you reboot the chip, the I2C init function will run again and get reinitialized.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Best regards,&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Simon&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: NRF9160 I2C/TWI recovery and microsecond resolution timer</title><link>https://devzone.nordicsemi.com/thread/308890?ContentTypeID=1</link><pubDate>Mon, 10 May 2021 08:36:26 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f6f6838c-5c33-4c97-b8db-661de97b8774</guid><dc:creator>fastfox</dc:creator><description>&lt;p&gt;Ok, so it works and doesn&amp;#39;t.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Works part:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;static inline void print_error(int err) {
  if (err != 0) {
    LOG_DBG(&amp;quot;Fail: %d&amp;quot;, err);
  }
}

void recover_i2c() {
  const struct device* gpio_dev = NULL;
  gpio_dev = device_get_binding(&amp;quot;GPIO_0&amp;quot;);
  if (gpio_dev == NULL) {
    k_panic();
  }
  // Capture I2C pins to GPIOs
  print_error(gpio_pin_configure(gpio_dev, I2C_SCL, GPIO_OUTPUT_HIGH));
  print_error(gpio_pin_configure(gpio_dev, I2C_SDA, GPIO_INPUT));

  for (int i = 0; i &amp;lt; 9; i++) {
    // If SDA is high, it means that it is now/already released and there is no need to
    // try to send recovery clocks anymore
    if (gpio_pin_get(gpio_dev, I2C_SDA) &amp;gt; 0) {
      break;
    }
    // Make clock pulses, 5us(10us) should give us 100kHz, but GPIO set also takes time
    // so in practice this is ~6us or ~80kHz. The one time I could did this for device that was stuck,
    // frequency was around 60-70kHz.
    print_error(gpio_pin_set(gpio_dev, I2C_SCL, 0));
    k_busy_wait(5);
    print_error(gpio_pin_set(gpio_dev, I2C_SCL, 1));
    k_busy_wait(5);
  }
  // Stop condition
  print_error(gpio_pin_configure(gpio_dev, I2C_SDA, GPIO_OUTPUT_HIGH));
  print_error(gpio_pin_set(gpio_dev, I2C_SDA, 0));
  k_busy_wait(5);
  print_error(gpio_pin_set(gpio_dev, I2C_SDA, 1));
  k_busy_wait(5);

  print_error(gpio_pin_configure(gpio_dev, I2C_SCL, GPIO_DISCONNECTED));
  print_error(gpio_pin_configure(gpio_dev, I2C_SDA, GPIO_DISCONNECTED));
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;This code seems to generate the correct clock and data pulses for the recovery.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Does not work part:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;After running that code I2C is broken. Voltage levels are somewhere in between and some devices on the bus stop working :(&lt;/p&gt;
&lt;p&gt;&lt;img alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/320x240/__key/communityserver-discussions-components-files/4/pastedimage1620635560743v1.png" /&gt;&lt;/p&gt;
&lt;p&gt;But as I will make this recovery just before a reboot, it might just save my butt here. But obviously, this is not the right way to do it as the bus goes haywire after the operation.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: NRF9160 I2C/TWI recovery and microsecond resolution timer</title><link>https://devzone.nordicsemi.com/thread/308772?ContentTypeID=1</link><pubDate>Fri, 07 May 2021 14:26:08 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b645e4c3-c273-42f9-99ea-52f416f373e0</guid><dc:creator>Simon</dc:creator><description>[quote user="fastfox"]I will tell how it went.[/quote]
&lt;p&gt;&amp;nbsp;Sounds good. If you don&amp;#39;t get it to work I will look into this myself and try to get it working.&lt;/p&gt;
&lt;p&gt;I have answered a similar question earlier, that may be useful:&amp;nbsp;&lt;a href="https://devzone.nordicsemi.com/f/nordic-q-a/71454/i2c-twi-driver-bus-recovery"&gt;https://devzone.nordicsemi.com/f/nordic-q-a/71454/i2c-twi-driver-bus-recovery&lt;/a&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: NRF9160 I2C/TWI recovery and microsecond resolution timer</title><link>https://devzone.nordicsemi.com/thread/308742?ContentTypeID=1</link><pubDate>Fri, 07 May 2021 12:34:12 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9703f399-4d78-447b-a7b4-887357436033</guid><dc:creator>fastfox</dc:creator><description>&lt;p&gt;Thanks Simon,&lt;/p&gt;
&lt;p&gt;I&amp;#39;ll give this a try next week. I suppose the minimum hassle will be to implement that by my self and use k_busy_wait() for sleeping. I already tried that you can just hijack I2C pins as GPIOs like that.&lt;/p&gt;
&lt;p&gt;I will tell how it went.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: NRF9160 I2C/TWI recovery and microsecond resolution timer</title><link>https://devzone.nordicsemi.com/thread/308597?ContentTypeID=1</link><pubDate>Thu, 06 May 2021 15:25:50 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4375d60d-8948-4a8f-9245-45a41c75c379</guid><dc:creator>Simon</dc:creator><description>[quote user=""]- If the preferred way needs a microsecond resolution timing, what is the correct way to do that?[/quote]
&lt;p&gt;&amp;nbsp;This note may be useful::&amp;nbsp;&lt;a href="https://github.com/nrfconnect/sdk-zephyr/blob/v2.4.99-ncs2/include/kernel.h#L457-L462"&gt;https://github.com/nrfconnect/sdk-zephyr/blob/v2.4.99-ncs2/include/kernel.h#L457-L462&lt;/a&gt;&amp;nbsp;&lt;/p&gt;
[quote user=""]- There is no built in I2C recovery in NRD91 SDK, right?[/quote]
&lt;p&gt;&amp;nbsp;No, it does&amp;nbsp;seem like the i2c_recover_bus() is supported by Nordic at the moment. However, you may use the NRFX API, where you can use the function&amp;nbsp;nrfx_twi_twim_bus_recover()&lt;/p&gt;
&lt;p&gt;&lt;a href="https://github.com/nrfconnect/sdk-hal_nordic/blob/v1.5.1/nrfx/drivers/include/nrfx_twim.h#L366-L369"&gt;https://github.com/nrfconnect/sdk-hal_nordic/blob/v1.5.1/nrfx/drivers/include/nrfx_twim.h#L366-L369&lt;/a&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;a href="https://github.com/nrfconnect/sdk-hal_nordic/blob/v1.5.1/nrfx/drivers/include/nrfx_twim.h#L366-L369"&gt;&lt;/a&gt;&lt;a href="https://github.com/nrfconnect/sdk-hal_nordic/blob/v1.5.1/nrfx/drivers/src/nrfx_twi_twim.c#L45"&gt;ttps://github.com/nrfconnect/sdk-hal_nordic/blob/v1.5.1/nrfx/drivers/src/nrfx_twi_twim.c#L45&lt;/a&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The function above may also be helpful if you want to implement it yourself&lt;/p&gt;
[quote user=""]- What is the preferred way to do it? Capture the I2C pin to GPIO like that? Or maybe convert&amp;nbsp;I2C clock&amp;nbsp;to PWM on the fly? Can you do that?[/quote]
&lt;p&gt;&amp;nbsp;To be honest, I don&amp;#39;t have complete control of this at the moment. I will do some more investigation and get back to.&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Simon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>