<?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_gpio_pin_read and optimized code might return wrong value</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/25161/nrf_gpio_pin_read-and-optimized-code-might-return-wrong-value</link><description>Hi,
I have found an interesting behavior from this code: 
 nrf_gpio_cfg_input(7,NRF_GPIO_PIN_NOPULL);
if (nrf_gpio_pin_read(7)==0)
{
 NRF_LOG_INFO(&amp;quot;ID1(P0.%d) is LOW.&amp;quot;,7);
}else{
 NRF_LOG_INFO(&amp;quot;ID1(P0.%d) is HIGH.&amp;quot;,7);
}
 
 the nrf_gpio_pin_read</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 30 Jul 2019 09:28:55 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/25161/nrf_gpio_pin_read-and-optimized-code-might-return-wrong-value" /><item><title>RE: nrf_gpio_pin_read and optimized code might return wrong value</title><link>https://devzone.nordicsemi.com/thread/201180?ContentTypeID=1</link><pubDate>Tue, 30 Jul 2019 09:28:55 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c6544409-24a5-43c2-ae16-d132d98bffe5</guid><dc:creator>Bruno Randolf</dc:creator><description>&lt;p&gt;Just ran into this as well with SDK15.3.0. The issue is neither fixed nor documented in the SDK docs... I would argue it should be fixed in the SDK as this is a pretty basic functionality...&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nrf_gpio_pin_read and optimized code might return wrong value</title><link>https://devzone.nordicsemi.com/thread/133952?ContentTypeID=1</link><pubDate>Wed, 30 May 2018 07:36:43 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a62f65c2-6aff-4c2f-bb8d-292f7503b8fc</guid><dc:creator>MartinBL</dc:creator><description>&lt;p&gt;Happy to help. My apologies about the post.&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nrf_gpio_pin_read and optimized code might return wrong value</title><link>https://devzone.nordicsemi.com/thread/133949?ContentTypeID=1</link><pubDate>Wed, 30 May 2018 07:33:05 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4631603e-51f0-4421-b51a-d94358d540ee</guid><dc:creator>martin-ve</dc:creator><description>&lt;p&gt;It was mine, I was already wondering why I couldn&amp;#39;t find my post or your reply xD.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Anyway, thanks for your answer.&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nrf_gpio_pin_read and optimized code might return wrong value</title><link>https://devzone.nordicsemi.com/thread/133943?ContentTypeID=1</link><pubDate>Wed, 30 May 2018 07:12:47 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:994d5939-5ce6-42e0-a35c-a13e7b7fdce0</guid><dc:creator>MartinBL</dc:creator><description>&lt;p&gt;I accidentally deleted a post here, and I&amp;#39;m unable to undo it or find the name of the poster. I copied the text before I deleted the post though, and here it is:&lt;/p&gt;
&lt;p&gt;POST:&lt;/p&gt;
&lt;p&gt;We&amp;#39;ve experienced the same issue. When a read of the IN register is executed right after the instruction &lt;br /&gt;where the PIN_CFG is written, it returns a wrong value (always 0?). When we add 4 consecutive (dummy) &lt;br /&gt;reads of PIN_CFG, as suggested in the workaround for errata 173, it works.&lt;/p&gt;
&lt;p&gt;Shouldn&amp;#39;t this issue be mentioned in the errata? And also include it in the SDK?&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;The following code works for me:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;__STATIC_INLINE void nrf_gpio_cfg(
uint32_t pin_number,
nrf_gpio_pin_dir_t 
dir,
nrf_gpio_pin_input_t input,
nrf_gpio_pin_pull_t pull,
nrf_gpio_pin_drive_t 
drive,
nrf_gpio_pin_sense_t sense)
{
NRF_GPIO-&amp;gt;PIN_CNF[pin_number] = ((uint32_t)dir &amp;lt;&amp;lt; 
GPIO_PIN_CNF_DIR_Pos)
| ((uint32_t)input &amp;lt;&amp;lt; GPIO_PIN_CNF_INPUT_Pos)

| ((uint32_t)pull &amp;lt;&amp;lt; GPIO_PIN_CNF_PULL_Pos)
| 
((uint32_t)drive &amp;lt;&amp;lt; GPIO_PIN_CNF_DRIVE_Pos)
| ((uint32_t)sense &amp;lt;&amp;lt; 
GPIO_PIN_CNF_SENSE_Pos);

#if defined(NRF52) || defined(NRF52_SERIES)
/*
* A read of the IN register 
immediately after a write to PIN_CFG[pin] might result in a wrong value.
* We suspect a 
synchronization issue with the CPU and GPIO clock, similar to PAN173.
*
* Any read on the system 
bus will require the write buffer to be emptied first.
* Synchronize with the 16MHz GPIO clock. This 
takes at least 3 cycles to take effect.
*/
(void)NRF_GPIO-&amp;gt;PIN_CNF[pin_number];
(void)NRF_GPIO- 
&amp;gt;PIN_CNF[pin_number];
(void)NRF_GPIO-&amp;gt;PIN_CNF[pin_number];
(void)NRF_GPIO-&amp;gt;PIN_CNF[pin_number];
#endif
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;ANSWER:&lt;br /&gt;You are right. There is a propagation delay on the GPIO-&amp;gt;IN register of a couple of cycles. The number of cycles appears to depend on the IC variant and use case, but should never be more than 3. The system architects are currently discussing how to improve the description of errata 173.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nrf_gpio_pin_read and optimized code might return wrong value</title><link>https://devzone.nordicsemi.com/thread/99123?ContentTypeID=1</link><pubDate>Tue, 19 Sep 2017 10:17:47 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:217d2a93-e27a-47ee-ba9b-ac181ed280c9</guid><dc:creator>MartinBL</dc:creator><description>&lt;p&gt;You can just leave it open and we will see what happens.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nrf_gpio_pin_read and optimized code might return wrong value</title><link>https://devzone.nordicsemi.com/thread/99121?ContentTypeID=1</link><pubDate>Tue, 19 Sep 2017 09:10:48 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ae74ad5d-5af0-4071-8d77-f4f3a4696873</guid><dc:creator>Tamisoft</dc:creator><description>&lt;p&gt;Will do! Do you want me to close this ticket, or the SDK team will write their response here?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nrf_gpio_pin_read and optimized code might return wrong value</title><link>https://devzone.nordicsemi.com/thread/99122?ContentTypeID=1</link><pubDate>Tue, 19 Sep 2017 08:39:10 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:56eec67d-ecef-4996-aa12-093bd0e11e51</guid><dc:creator>MartinBL</dc:creator><description>&lt;p&gt;I tend to agree regarding incorporation in the SDK. However, the argument against it is that fixing this corner case will affect other implementations and that we should rather improve our documentation to help people decide on an implementation themselves. I have notified the SDK team so that they can look into it.&lt;/p&gt;
&lt;p&gt;Please keep me posted.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nrf_gpio_pin_read and optimized code might return wrong value</title><link>https://devzone.nordicsemi.com/thread/99124?ContentTypeID=1</link><pubDate>Tue, 19 Sep 2017 06:48:23 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5cf154d8-003c-424c-a1f0-885c494158d6</guid><dc:creator>Tamisoft</dc:creator><description>&lt;p&gt;Thanks for the possible solutions, but would it be better to incorporate these into the SDK where it can cause undeterministic output? I mean everywhere else this just manifests as a slight latency (or jitter in it), but in the scenario where IO changes direction it leads to a wrong value read under certain conditions (like optimized enough code generated by the compiler that the two instructions follow closely). The idea about NRF_GPIO-&amp;gt;OUT is the same as nops, one is not enough :). ISB and DMB could be a solution, I will test it later, but still I think it should be in the SDK, so from the user&amp;#39;s point of view it is consistent.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nrf_gpio_pin_read and optimized code might return wrong value</title><link>https://devzone.nordicsemi.com/thread/99125?ContentTypeID=1</link><pubDate>Tue, 19 Sep 2017 06:19:09 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:7c2fb617-0d99-4ba1-be3b-c19d4593d011</guid><dc:creator>MartinBL</dc:creator><description>&lt;p&gt;Can you try to add a &amp;quot;dummy instruction&amp;quot; after nrf_gpio_cfg_input()? Something like this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;nrf_gpio_cfg_input(7,NRF_GPIO_PIN_NOPULL);
(void)NRF_GPIO-&amp;gt;OUT;
if (nrf_gpio_pin_read(7)==0)
    [....]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You can also use  __ISB() eller __DMB(). The theory being that it might be because the nRF52&amp;#39;s peripherals run at 16 MHz while the CPU runs at 64 MHz. It might be worth a shot to use one of these instructions to ensure that the CPU buffers are empty. &lt;a href="http://infocenter.nordicsemi.com/index.jsp?topic=%2Fcom.nordic.infocenter.nrf52%2Fdita%2Fnrf52%2Fmigration%2Fperformance.html&amp;amp;cp=2_4_3"&gt;Here&lt;/a&gt; is a somewhat sparse documentation of the issue.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nrf_gpio_pin_read and optimized code might return wrong value</title><link>https://devzone.nordicsemi.com/thread/99120?ContentTypeID=1</link><pubDate>Thu, 14 Sep 2017 13:12:16 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:847c7735-6890-47dd-9438-64318cd5a7e5</guid><dc:creator>Tamisoft</dc:creator><description>&lt;p&gt;Yes, it is a 1.8V system. I mentioned above that we have suspected the capacitance so we have tried to tie the gpio directly to 1.8V as well. If you want we could try to replicate this on one of the nrf52 dks as well, but that might take some time.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nrf_gpio_pin_read and optimized code might return wrong value</title><link>https://devzone.nordicsemi.com/thread/99126?ContentTypeID=1</link><pubDate>Thu, 14 Sep 2017 11:08:18 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:938b6ec3-620d-4d69-805b-532336acd344</guid><dc:creator>MartinBL</dc:creator><description>&lt;p&gt;I suppose your 10k resistor and the nRF52&amp;#39;s pad capacitance can slow down the voltage rise time on the input just enough so that it doesn&amp;#39;t reach a logic &amp;#39;1&amp;#39; fast enough for nrf_gpio_pin_read().&lt;/p&gt;
&lt;p&gt;But before I report this to the SDK team, is your nRF52&amp;#39;s supply voltage also 1.8V? To register as a logic 1 the input voltage must be higher than 0.7*Vdd (&lt;a href="http://infocenter.nordicsemi.com/topic/com.nordic.infocenter.nrf52832.ps.v1.1/gpio.html?cp=2_1_0_19_3#unique_717482065"&gt;electrical GPIO specs&lt;/a&gt;).&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>