<?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>Zephyr Timer + I2C</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/87471/zephyr-timer-i2c</link><description>I&amp;#39;m building a simple application using I2C to read sensor data (BNO055). I confirmed I2C and my sensor are all configured appropriately and working smoothly, but when I added in zephyr timers to my application to read the sensors, the application repeatedly</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Mon, 02 May 2022 12:51:44 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/87471/zephyr-timer-i2c" /><item><title>RE: Zephyr Timer + I2C</title><link>https://devzone.nordicsemi.com/thread/365782?ContentTypeID=1</link><pubDate>Mon, 02 May 2022 12:51:44 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:12918fd2-ad14-4850-8368-35c8bdb96aed</guid><dc:creator>Simon</dc:creator><description>&lt;p&gt;Doing a lot of work inside an ISR is not good practice, and it should be offloaded to somewhere else.&lt;/p&gt;
&lt;p&gt;Check out these solutions as well as the one suggested by &lt;a href="https://devzone.nordicsemi.com/members/qwertynoon"&gt;qwertynoon&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Timer + semaphore:&amp;nbsp;&lt;a href="https://devzone.nordicsemi.com/f/nordic-q-a/74617/config_assert-cause-code-to-crash/307651#307651"&gt;https://devzone.nordicsemi.com/f/nordic-q-a/74617/config_assert-cause-code-to-crash/307651#307651&lt;/a&gt;.&amp;nbsp;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;You could also put the snippet below in an own thread instead of having it in main&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;while(true){
        k_sem_take(&amp;amp;adc_sem, K_FOREVER);
        adc_sample();
    }
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Timer + work queue:&amp;nbsp;&lt;a href="https://github.com/too1/ncs-peripheral-uart-adc/blob/d575cdb9a2e0d7e5f0984a6a228bcb0d3a6f1a7c/src/main.c#L563-L565"&gt;https://github.com/too1/ncs-peripheral-uart-adc/blob/d575cdb9a2e0d7e5f0984a6a228bcb0d3a6f1a7c/src/main.c#L563-L565&lt;/a&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I&amp;#39;m not sure what the accuracy will be when using the approches above, and I guess it depends on your application and how much other work you&amp;#39;re doing. I suggest you test it yourself and see if it meets your requirements. Maybe increasing the priority of the thread doing the ADC reading will help.&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><item><title>RE: Zephyr Timer + I2C</title><link>https://devzone.nordicsemi.com/thread/365769?ContentTypeID=1</link><pubDate>Mon, 02 May 2022 12:33:13 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:bbaceaf9-71dc-40ac-a662-70d320d72e77</guid><dc:creator>josh_s</dc:creator><description>&lt;p&gt;Thanks, but doesn&amp;rsquo;t that submit solution not force the sensor to read every 100ms as intended? &amp;nbsp;In other words, once submitted via k_work_submit, the i2c instructions may await execution for a variable amount of time,&amp;nbsp;and therefore I&amp;nbsp;may not get 100ms intervals on the sensor reads.&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Zephyr Timer + I2C</title><link>https://devzone.nordicsemi.com/thread/365676?ContentTypeID=1</link><pubDate>Mon, 02 May 2022 06:00:14 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c110dfba-38f8-411b-b4a5-f23fe3e9f0e7</guid><dc:creator>qwertynoon</dc:creator><description>&lt;p&gt;Hi.&lt;/p&gt;
&lt;p&gt;I2C can take a lot of time, thats why you shoud use K_WORK_DEFINE for your timer&lt;/p&gt;
&lt;p&gt;Try this&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;static void repeating_timer_callback(struct k_timer *timer_id){

        i2c_read_write(...);

        ...

}

K_WORK_DEFINE(repeating_timer_work, repeating_timer_callback);

void repeating_timer_handler(struct k_timer *dummy)
{
        k_work_submit(&amp;amp;repeating_timer_work);
}

K_TIMER_DEFINE(my_timer, repeating_timer_handler, NULL);
&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>