<?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>NRF Zephyr Timing SoC implementation - possible bug</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/77916/nrf-zephyr-timing-soc-implementation---possible-bug</link><description>Hello, 
 I&amp;#39;m playing with the timing subsystem in order to measure some execution time with higher granularity than the RTC tick on nRF52840 (using PCA10056 DK). 
 I&amp;#39;m specifically using the Nordic SoC implementation ( NOT the architecture default for</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 22 Sep 2021 13:49:19 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/77916/nrf-zephyr-timing-soc-implementation---possible-bug" /><item><title>RE: NRF Zephyr Timing SoC implementation - possible bug</title><link>https://devzone.nordicsemi.com/thread/330697?ContentTypeID=1</link><pubDate>Wed, 22 Sep 2021 13:49:19 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:7e457de8-7a8e-4b15-9bde-690bcb258d79</guid><dc:creator>Simon</dc:creator><description>&lt;p&gt;I talked to&amp;nbsp;a developer in Nordic with more knowledge about&amp;nbsp; &lt;em&gt;zephyr/soc/arm/nordic_nrf/timing.c&lt;/em&gt;, and he told me you should just go ahead an create a PR.&lt;/p&gt;
&lt;p&gt;The guy assigned to &lt;a href="https://github.com/zephyrproject-rtos/zephyr/issues/37390"&gt;Zephyr issue 37390&lt;/a&gt;&amp;nbsp;has not had time to look into it yet unfortunately.&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: NRF Zephyr Timing SoC implementation - possible bug</title><link>https://devzone.nordicsemi.com/thread/330663?ContentTypeID=1</link><pubDate>Wed, 22 Sep 2021 12:36:48 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e995f23c-93a9-429d-b569-c22c8d55d623</guid><dc:creator>davege</dc:creator><description>&lt;p&gt;Hi Simon,&lt;/p&gt;
&lt;p&gt;of course:&amp;nbsp;&lt;a href="https://github.com/zephyrproject-rtos/zephyr/issues/37390"&gt;https://github.com/zephyrproject-rtos/zephyr/issues/37390&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;D&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: NRF Zephyr Timing SoC implementation - possible bug</title><link>https://devzone.nordicsemi.com/thread/330643?ContentTypeID=1</link><pubDate>Wed, 22 Sep 2021 11:54:44 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a9ecdcb5-8fff-4397-8a1f-262594aa8044</guid><dc:creator>Simon</dc:creator><description>&lt;p&gt;Could you share a link to the Zephyr issue you created?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: NRF Zephyr Timing SoC implementation - possible bug</title><link>https://devzone.nordicsemi.com/thread/330258?ContentTypeID=1</link><pubDate>Mon, 20 Sep 2021 11:27:31 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:33f8790d-ee6c-42e5-81ee-c6c39c74a6b5</guid><dc:creator>Simon</dc:creator><description>&lt;p&gt;I just got back from holiday, and I will look into this the next days.&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: NRF Zephyr Timing SoC implementation - possible bug</title><link>https://devzone.nordicsemi.com/thread/328501?ContentTypeID=1</link><pubDate>Wed, 08 Sep 2021 07:20:44 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:20a7e16b-7de6-4b67-88b7-c80e8cd378f2</guid><dc:creator>davege</dc:creator><description>&lt;p&gt;Hello Simon,&lt;/p&gt;
&lt;p&gt;hope you had good holidays.&lt;/p&gt;
&lt;p&gt;Unfortunately, no one on Zephyr side picked up the issue I created, would you be able to have a look since it&amp;#39;s Nordic specific code?&lt;/p&gt;
&lt;p&gt;I&amp;#39;d be curious to know your comments.&lt;br /&gt;Thanks!&lt;/p&gt;
&lt;p&gt;Best,&lt;/p&gt;
&lt;p&gt;D&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: NRF Zephyr Timing SoC implementation - possible bug</title><link>https://devzone.nordicsemi.com/thread/322614?ContentTypeID=1</link><pubDate>Fri, 30 Jul 2021 09:13:12 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b6a05538-6c6c-4650-bbaf-5e315602c4a9</guid><dc:creator>Simon</dc:creator><description>&lt;p&gt;Hello Davege,&lt;/p&gt;
&lt;p&gt;I&amp;#39;m going on vacation for a long time now, so I won&amp;#39;t be able to look into this unfortunately.&lt;/p&gt;
&lt;p&gt;Would you be able to open a Zephyr issue? It will be seen by the people working on this driver (including many Nordic engineers), and you can get feedback whether&amp;nbsp;your proposed solution is a valid one before opening a PR.&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: NRF Zephyr Timing SoC implementation - possible bug</title><link>https://devzone.nordicsemi.com/thread/322369?ContentTypeID=1</link><pubDate>Thu, 29 Jul 2021 07:34:57 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b26f764c-0673-4689-a75e-4f1b514be767</guid><dc:creator>davege</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
[quote userid="72692" url="~/f/nordic-q-a/77916/nrf-zephyr-timing-soc-implementation---possible-bug/322362#322362"]&lt;blockquote class="quote"&gt;&lt;div class="quote-content"&gt;since &lt;em&gt;start&lt;/em&gt; and &lt;em&gt;end&lt;/em&gt; are 32-bit values (just casted), &lt;/div&gt;&lt;/blockquote&gt;&lt;div class="quote-footer"&gt;&lt;/div&gt;
&lt;p&gt;&amp;nbsp;I do not understand this.&lt;/p&gt;
&lt;p&gt;The start and end values are pointers of type&amp;nbsp;timing_t, which is a typedef of&amp;nbsp;uint64_t:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://github.com/nrfconnect/sdk-zephyr/blob/0944459b5b622048a08ad1f8cf8a044c135fd0d3/include/timing/types.h#L10"&gt;https://github.com/nrfconnect/sdk-zephyr/blob/0944459b5b622048a08ad1f8cf8a044c135fd0d3/include/timing/types.h#L10&lt;/a&gt;&amp;nbsp;&lt;/p&gt;[/quote]
&lt;p&gt;With this I mean that start and end are detected using the&amp;nbsp;&lt;span&gt;soc_timing_counter_get function:&lt;br /&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;timing_t soc_timing_counter_get(void)
{
	NRF_TIMER2-&amp;gt;TASKS_CAPTURE[0] = 1;
	return NRF_TIMER2-&amp;gt;CC[0] * ((SystemCoreClock) / CYCLES_PER_SEC);
}&lt;/pre&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;By nature, these are just 32-bit values (as the TIMER has resolution 32bits) multiplied by 4 and then returned into a uint64_t container.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;If you try to detect average execution delta over time, you of course expect to end up in the situation I described, where:&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;start = large number, but coming first in time&lt;/li&gt;
&lt;li&gt;end&amp;nbsp; = small number, but coming last in time&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;What I think is happening is&amp;nbsp;that overflow is not on UINT64_MAX boundary, but on a smaller value, as the max value you can have using soc_timing_counter_get is:&lt;/p&gt;
&lt;p&gt;UINT32_MAX * 4 (which is &amp;lt;&amp;lt; UINT64_MAX)&lt;/p&gt;
&lt;p&gt;So I believe normal modulo arithmetic cannot apply here, and special handling of the overflow would be needed rather than just subtracting:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;*end - *start&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Hope this clarifies it, let me know if this is not clear still.&lt;/p&gt;
&lt;p&gt;I&amp;#39;d like to get an opinion here before opening a PR and waste someone time...&lt;/p&gt;
&lt;p&gt;Thanks!&lt;/p&gt;
&lt;p&gt;D&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: NRF Zephyr Timing SoC implementation - possible bug</title><link>https://devzone.nordicsemi.com/thread/322362?ContentTypeID=1</link><pubDate>Thu, 29 Jul 2021 07:18:09 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:be79b43e-6f0d-44e0-89a2-f6128ecaa8a0</guid><dc:creator>Simon</dc:creator><description>[quote user=""] since &lt;em&gt;start&lt;/em&gt; and &lt;em&gt;end&lt;/em&gt; are 32-bit values (just casted), [/quote]
&lt;p&gt;&amp;nbsp;I do not understand this.&lt;/p&gt;
&lt;p&gt;The start and end values are pointers of type&amp;nbsp;timing_t, which is a typedef of&amp;nbsp;uint64_t:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://github.com/nrfconnect/sdk-zephyr/blob/0944459b5b622048a08ad1f8cf8a044c135fd0d3/include/timing/types.h#L10"&gt;https://github.com/nrfconnect/sdk-zephyr/blob/0944459b5b622048a08ad1f8cf8a044c135fd0d3/include/timing/types.h#L10&lt;/a&gt;&amp;nbsp;&lt;/p&gt;
[quote user=""]I discovered this by using a routines measuring an average of execution&amp;nbsp;over time, as the problem is occurring only in the specific overflow cases.[/quote]
&lt;p&gt;Since you do see unwanted behaviour, could you make a &lt;a href="https://github.com/zephyrproject-rtos/zephyr/issues/new/choose"&gt;Zephyr (upstream) issue&lt;/a&gt; and explain your issue there?&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Then it will be looked at by Zephyr developers&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>