<?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>best practice - delay between setting pin as input with pull up/pull down and reading the pin</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/36881/best-practice---delay-between-setting-pin-as-input-with-pull-up-pull-down-and-reading-the-pin</link><description>We are seeing a potential issue with a product. on start up a pin is read and the read value determines if we go in to a test mode. Pretty standard. 
 Our failure is during normal operation when there is nothing connected to this pin and the firmware</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Sun, 29 Jul 2018 15:30:32 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/36881/best-practice---delay-between-setting-pin-as-input-with-pull-up-pull-down-and-reading-the-pin" /><item><title>RE: best practice - delay between setting pin as input with pull up/pull down and reading the pin</title><link>https://devzone.nordicsemi.com/thread/141791?ContentTypeID=1</link><pubDate>Sun, 29 Jul 2018 15:30:32 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c3947ac4-f8c4-4b61-b3be-744c341ed0a2</guid><dc:creator>AmbystomaLabs</dc:creator><description>&lt;p&gt;All good info.&lt;/p&gt;
&lt;p&gt;Can you provide a link to the register spec that states it sets and latches state on the same clock?&lt;/p&gt;
&lt;p&gt;I couldn&amp;#39;t find anything in the Nordic docs and was loathe to start combing through generic ARM documentation. The information could prove useful for later questions.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: best practice - delay between setting pin as input with pull up/pull down and reading the pin</title><link>https://devzone.nordicsemi.com/thread/141790?ContentTypeID=1</link><pubDate>Sun, 29 Jul 2018 15:25:27 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0f9ec6fd-0f31-4fb6-8fd3-9991a2f2e843</guid><dc:creator>RK</dc:creator><description>&lt;p&gt;I didn&amp;#39;t reply &amp;nbsp;because I had nothing more to add. I agree with the last post from the OP that you need to consider&amp;nbsp;the rise time on the line, that&amp;#39;s the only thing you need to consider however.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;There is no need for an extra &amp;#39;wait&amp;#39; condition with the nRF51. The pullup is activated on the edge of the clock cycle on which you write, if you do a read on the next cycle it&amp;#39;s sampled just around the next edge, there is no extra clock cycle latch in there you need to compensate for. You have most of one full&amp;nbsp;16MHz cycle for the signal to rise.Unless you have a slow rise/lots of capacitance you have enough&amp;nbsp;setup time.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;On the nRF52 you only need to ensure that the write to the status register has been committed and the pullup enabled before you read the value. As register writes are cached that just requires reading back the status register after you write it because the read will stall until the status write commits. Reading the value is another APB cycle later and so you&amp;#39;re back where you were with the nRF51, nothing extra required.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;So with nRF51, write status then read value on the next cycle is fine and with nRF52 write status, read status then read value is enough.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;All that is contingent on having reasonable rise times on the signal itself, you need to calculate it, but at 16MHz it would not usually be an issue.&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: best practice - delay between setting pin as input with pull up/pull down and reading the pin</title><link>https://devzone.nordicsemi.com/thread/141789?ContentTypeID=1</link><pubDate>Sun, 29 Jul 2018 14:25:25 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:3e6788d4-58a2-43d9-913d-fcf587f3b424</guid><dc:creator>AmbystomaLabs</dc:creator><description>&lt;p&gt;I noticed RK didn&amp;#39;t respond to you.&lt;/p&gt;
&lt;p&gt;Again, the peripherals are all driven by pclk. So if you write the config register on one pclk on the next pclk the new value will be set (ie, latched). So you should not attempt a read until the third pclk.&lt;/p&gt;
&lt;p&gt;If the register writes are being cached as RK stated, then you will have to wait longer since the register write will not be synchronous with the opcode.&lt;/p&gt;
&lt;p&gt;On nRF51 if you just throw in half a dozen NOP&amp;#39;s between the config and read, you should have compensated for any latency. If you don&amp;#39;t like embedding assembly code then just add a for loop to hold up the processor for a few clocks.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: best practice - delay between setting pin as input with pull up/pull down and reading the pin</title><link>https://devzone.nordicsemi.com/thread/141599?ContentTypeID=1</link><pubDate>Fri, 27 Jul 2018 06:26:54 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:fff99635-596a-4afd-826d-220221b25e92</guid><dc:creator>cp52</dc:creator><description>&lt;p&gt;You have pointed out that on the nrf51 we are synchronous and we can guarantee that the pull up gets connected to the pin before we read. However people should still make sure they consider what is on that line and consider the relevant time that the line will take to be &amp;quot;pulled up&amp;quot;.&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: best practice - delay between setting pin as input with pull up/pull down and reading the pin</title><link>https://devzone.nordicsemi.com/thread/141598?ContentTypeID=1</link><pubDate>Fri, 27 Jul 2018 06:21:46 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:56335ed8-e489-49f0-9c1f-64be7bfc64ee</guid><dc:creator>cp52</dc:creator><description>&lt;p&gt;I am using the nrf51 however we use the nrf52 as well so this is great a a forum reply.&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: best practice - delay between setting pin as input with pull up/pull down and reading the pin</title><link>https://devzone.nordicsemi.com/thread/141580?ContentTypeID=1</link><pubDate>Thu, 26 Jul 2018 21:35:14 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:099be1e1-b0d7-4eea-ae29-fff4cace9313</guid><dc:creator>RK</dc:creator><description>&lt;p&gt;You don&amp;#39;t say whether you&amp;#39;re using nRF51 or nRF52. On nRF51 the answer is no, you don&amp;#39;t need a delay, the APB and&amp;nbsp;CPU run at 16MHz and everything is synchronous. On nRF52 however the APB still runs at 16MHz but the CPU runs at 64MHz and has write caching so it can take up to 4 cycles for data to actually be written to the register. Reads on the APB bus block so they&amp;#39;re fine. So on nRF52 you do need to ensure the status write to the register has occured before you read the value.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The recommended way of doing that is to read back the configuration you just set, ie read back the gpio config register which nrf_gpio_cfg_input() just set. That forces the bus to stall until the write has completed at which point the register input value can be used.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Checks the implementation of that macro/function/whatever it is to see if Nordic already added that readback into the call. For safety&amp;#39;s sake they should have done (on the&amp;nbsp; nRF52) but I don&amp;#39;t know if they did for performance reasons.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;// update&lt;/p&gt;
&lt;p&gt;I just did look at the macros in SDK15.0 and they don&amp;#39;t readback the register, so if you&amp;#39;re on&amp;nbsp; nRF52, you should do so. Either you do it using the actual register (which rather negates the point of having the hardware abstraction layer) or find a function which implicitly reads it like `&lt;span class="s1"&gt;nrf_gpio_pin_sense_get()`. Just look at the source, pick a method you like.&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span class="s1"&gt;TLDR;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span class="s1"&gt;nRF51, no need, nRF52, need to read back the configuration (not the value) register after you write it to ensure&amp;nbsp; it&amp;#39;s been written.&amp;nbsp;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: best practice - delay between setting pin as input with pull up/pull down and reading the pin</title><link>https://devzone.nordicsemi.com/thread/141565?ContentTypeID=1</link><pubDate>Thu, 26 Jul 2018 16:11:04 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:443cbdcc-b97e-4b29-a468-c342908f223f</guid><dc:creator>AmbystomaLabs</dc:creator><description>&lt;p style="color:#000000;font-family:Arial, Helvetica, sans-serif;font-size:12px;font-style:normal;font-weight:400;letter-spacing:normal;margin-top:0px;text-align:start;text-indent:0px;text-transform:none;white-space:normal;"&gt;Offhand, I believe everything on the gpio is delayed by one pclk. So, the delay depends on how you have pclk configured.&lt;/p&gt;
&lt;p style="color:#000000;font-family:Arial, Helvetica, sans-serif;font-size:12px;font-style:normal;font-weight:400;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;"&gt;Yes, it is normal practice to delay a few clocks after setting a register as registers rarely set and latch state on the same clock edge.&amp;nbsp; Normally I would just throw in a few NOP&amp;#39;s after writing the config register depending on the spec for the device.&lt;/p&gt;
&lt;p style="color:#000000;font-family:Arial, Helvetica, sans-serif;font-size:12px;font-style:normal;font-weight:400;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;"&gt;The pullup is spec&amp;#39;d as 13kohm so there is no need to worry about the capacitance unless you added one on the pin.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>