<?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>Understanding basic button sample code</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/105328/understanding-basic-button-sample-code</link><description>Hello, 
 I&amp;#39;m studying the code for the button sample program in the nrf in zephyr. 
 
 
 I would like to ask for assistance in understanding some parts of the code. 
 
 Is there a diffence between the above code and #include &amp;lt;zephyr/device.h&amp;gt;? which should</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 08 Nov 2023 16:18:55 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/105328/understanding-basic-button-sample-code" /><item><title>RE: Understanding basic button sample code</title><link>https://devzone.nordicsemi.com/thread/454749?ContentTypeID=1</link><pubDate>Wed, 08 Nov 2023 16:18:55 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:319c1c00-46f4-48fb-9aad-eb48ddc6ff7c</guid><dc:creator>al_ns</dc:creator><description>&lt;p&gt;Much appreciated Einar! The explanations have help me better understood the button sample code &lt;span class="emoticon" data-url="https://devzone.nordicsemi.com/cfs-file/__key/system/emoji/1f604.svg" title="Smile"&gt;&amp;#x1f604;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Understanding basic button sample code</title><link>https://devzone.nordicsemi.com/thread/454736?ContentTypeID=1</link><pubDate>Wed, 08 Nov 2023 15:14:45 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0504f586-fd29-469e-a795-3d5a9ea6e24c</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;The difference is that&amp;nbsp;GPIO_DT_SPEC_GET_OR has a default/fallback value which is used if the node and property is not found in the device tree. This makes more sense in generic samples like this which is supposed to work on many different board, and here the LED is optional. In most projects it is not something you would use.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Understanding basic button sample code</title><link>https://devzone.nordicsemi.com/thread/454716?ContentTypeID=1</link><pubDate>Wed, 08 Nov 2023 14:21:42 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:18406744-0e58-4321-b2e4-cb194c118473</guid><dc:creator>al_ns</dc:creator><description>&lt;p&gt;This is understood.&lt;/p&gt;
&lt;p&gt;Next question I have is for:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;static const struct gpio_dt_spec button = GPIO_DT_SPEC_GET_OR(SW0_NODE, gpios,
							      {0});&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Is there a difference between GPIO_DT_SPEC_GET and GPIO_DT_SPEC_GET_OR?&lt;/p&gt;
&lt;p&gt;when do you use which?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Understanding basic button sample code</title><link>https://devzone.nordicsemi.com/thread/454114?ContentTypeID=1</link><pubDate>Mon, 06 Nov 2023 07:38:04 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:19ae88c0-2109-4162-9966-8e6ea6e92772</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;It is true (meaning not 0) if &lt;code&gt;led0&lt;/code&gt; exists in the device tree. The reason is is done like this is that a LED is optional in this sample, as you can see from this comment where &lt;code&gt;led&lt;/code&gt; is declared:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;/*
 * The led0 devicetree alias is optional. If present, we&amp;#39;ll use it
 * to turn on the LED whenever the button is pressed.
 */
static struct gpio_dt_spec led = GPIO_DT_SPEC_GET_OR(DT_ALIAS(led0), gpios,
						     {0});&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Understanding basic button sample code</title><link>https://devzone.nordicsemi.com/thread/454075?ContentTypeID=1</link><pubDate>Sat, 04 Nov 2023 18:09:10 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:934fb8c0-3ec2-42f9-9b9f-24cc8b53e356</guid><dc:creator>al_ns</dc:creator><description>&lt;p&gt;When is led.port true/1? in the if (led.port)?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Understanding basic button sample code</title><link>https://devzone.nordicsemi.com/thread/453988?ContentTypeID=1</link><pubDate>Fri, 03 Nov 2023 13:44:37 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:dcc68e88-852c-4e20-9da5-f276309d5f0e</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
[quote user=""]Is there a diffence between the above code and #include &amp;lt;zephyr/device.h&amp;gt;? which should be the better choice?[/quote]
&lt;p&gt;This is just form different versions of nRF Connect SDK. With recent versions, you must use&amp;nbsp;&lt;code&gt;#include &amp;lt;zephyr/device.h&amp;gt;&lt;/code&gt;.&lt;/p&gt;
[quote user=""]May I know if this code is relevant? I assume this is used for unit testing?[/quote]
&lt;p&gt;You can build Zephyr project for many different boards, and as this sample needs a button it is sensible to make sure you get an error if you try to build for a board that does not have a button. You can remove this if you want to.&lt;/p&gt;
[quote user=""]This is the first time I have encountered &amp;quot;PRIu32&amp;quot;, is this a time data?[/quote]
&lt;p&gt;It is a way to explicitly print a 32 bit unsigned integer.&lt;/p&gt;
[quote user=""]What is &amp;quot;led.port-&amp;gt;name&amp;quot;? why does it point to name?[/quote]
&lt;p&gt;this comes from the device tree. If you are building for anRF52840 DK, the &amp;quot;name&amp;quot; will be gpio@50000000 because&amp;nbsp;0x50000000 is the base address for GPIOTE port 0. You can find it in&amp;nbsp;zephyr/dts/arm/nordic/nrf52840.dtsi:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;		gpio0: gpio@50000000 {
			compatible = &amp;quot;nordic,nrf-gpio&amp;quot;;
			gpio-controller;
			reg = &amp;lt;0x50000000 0x200
			       0x50000500 0x300&amp;gt;;
			#gpio-cells = &amp;lt;2&amp;gt;;
			status = &amp;quot;disabled&amp;quot;;
			port = &amp;lt;0&amp;gt;;
		};&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>