<?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>Initializing SPI and TWI drivers on Zephyr</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/113155/initializing-spi-and-twi-drivers-on-zephyr</link><description>Hi 
 Followup on this ticket: Enabling SPI and TWI drivers on Zephyr 
 First the fundamentals, I am using: 
 
 nRF52840-DK 
 nRF Connect for VS Code: v2024.7.13 
 
 
 nRF Connect Ver. 2.5.1 
 
 
 
 I am trying to create a demonstration project, where</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 23 Jul 2024 06:22:37 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/113155/initializing-spi-and-twi-drivers-on-zephyr" /><item><title>RE: Initializing SPI and TWI drivers on Zephyr</title><link>https://devzone.nordicsemi.com/thread/495124?ContentTypeID=1</link><pubDate>Tue, 23 Jul 2024 06:22:37 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:1b76f37a-8d5f-4c5d-9365-2c7b66265e93</guid><dc:creator>kleonhardt</dc:creator><description>&lt;p&gt;Please note that the peripheral driver examples are using the&amp;nbsp;&lt;span&gt;Zephyr independent drivers, but the examples for a target chip e.g. bmi280 are made with the Zephyr device tree drivers. Don&amp;#39;t mix them, you will only be confused.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;And by the way, even though the TWI initialization partly is done by Zephyr initialization by a call to&amp;nbsp;nrfx_twim_init, the pin configuration is done later. I imagine that all pin configuration in the devicetree are done centrally&amp;nbsp;during the Zephyr initialization.&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Initializing SPI and TWI drivers on Zephyr</title><link>https://devzone.nordicsemi.com/thread/494995?ContentTypeID=1</link><pubDate>Mon, 22 Jul 2024 10:36:40 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:cf85c474-a14c-479e-b88a-cca8a1cc740d</guid><dc:creator>trampas</dc:creator><description>&lt;p&gt;That is also my understanding.&amp;nbsp; The idea with the Zephyr devicetree drivers is that you can port your code to another processor easy.&amp;nbsp; Which I think is kind of crazy in the embedded space, but that is what the Zephyr creators dream of.&amp;nbsp;&amp;nbsp;&lt;br /&gt;&lt;br /&gt;The thing that might help is looking at the zephyr driver source code, this help me once I understood that device trees just create a header file.&amp;nbsp; The driver source code should help you understand the zephyr drivers better.&amp;nbsp;&amp;nbsp;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Initializing SPI and TWI drivers on Zephyr</title><link>https://devzone.nordicsemi.com/thread/494973?ContentTypeID=1</link><pubDate>Mon, 22 Jul 2024 07:30:04 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:cb9461c5-cc4d-42f0-aa07-0470ab7193ba</guid><dc:creator>kleonhardt</dc:creator><description>&lt;p&gt;Thanks, that was helpful in many ways. However, it did not really answer my question.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I made some debugging, and it seems that the Zephyr INIT_LEVEL_POST_KERNEL initialization&amp;nbsp;does not include a call to nrfx_spi_init in the same way as i2c calls nrfx_twim_init. And in my example I &amp;quot;manually&amp;quot; called the&amp;nbsp;&lt;span&gt;nrfx_spi_init/nrfx_twim_init functions for initialization. Worked for SPI but not for I2C obviously. I cannot see why it should be different. But it is of cause wrong both adding the driver to the device tree, an do the initialization in the application code.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Can this be explained? Are my observations wrong?&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Further more I found the initializer fro the twim_0_config struct that is used for initializing the twi peripheral, but the scl/sda pins are not defined here.&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Are the pin mappings done later in the Zephyr devicetree initialization? I have not been able to find it.&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Initializing SPI and TWI drivers on Zephyr</title><link>https://devzone.nordicsemi.com/thread/494967?ContentTypeID=1</link><pubDate>Mon, 22 Jul 2024 06:47:02 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2e17118a-b0d4-4d3e-8eb8-89e9a02cdc56</guid><dc:creator>kleonhardt</dc:creator><description>&lt;p&gt;I just realized that&amp;nbsp;there are two categories of peripheral drivers.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Zephyr devicetree drivers. (Included by e.g. #include&lt;span&gt; &lt;/span&gt;&lt;span&gt;&amp;lt;zephyr/drivers/i2c.h&amp;gt;)&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;Zephyr independent drivers. (Included by e.g #include &amp;lt;nrfx_twim.h&amp;gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;All the peripheral examples seems to be using &amp;quot;&lt;span&gt;Zephyr independent drivers.&amp;quot;, so there is no help here if you want to use the Zephyr Devicetree.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Is this correct?&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Initializing SPI and TWI drivers on Zephyr</title><link>https://devzone.nordicsemi.com/thread/494668?ContentTypeID=1</link><pubDate>Thu, 18 Jul 2024 21:52:37 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:67c1865f-c581-4711-8f37-03c05da2731d</guid><dc:creator>trampas</dc:creator><description>&lt;p&gt;First off in Zephyr with device trees is to realize the device trees get &amp;quot;preprocessed&amp;quot; or &amp;quot;compiled&amp;quot; down to a header file in your build/zephyr/include/generated directory called &amp;quot;devicetree_generated.h&amp;quot;.&amp;nbsp; That is the device tree mess all gets processed to this one header file, nothing more or less.&amp;nbsp;&lt;br /&gt;The source code for drivers in Zephyr then look at the &amp;quot;devicetree_generated.h&amp;quot; header file to see if they need to create a driver for you or not.&amp;nbsp; For example when you include the I2C in the device tree the nordic I2C driver source file then knows to build driver&amp;nbsp; and before main()&amp;nbsp; the driver is configured and ready to be used.&amp;nbsp;&lt;br /&gt;&lt;pre class="ui-code" data-mode="text"&gt;&amp;amp;i2c1 {
	compatible = &amp;quot;nordic,nrf-twim&amp;quot;;
	status = &amp;quot;okay&amp;quot;;
	pinctrl-0 = &amp;lt;&amp;amp;i2c1_default&amp;gt;;
	pinctrl-1 = &amp;lt;&amp;amp;i2c1_sleep&amp;gt;;
	pinctrl-names = &amp;quot;default&amp;quot;, &amp;quot;sleep&amp;quot;;
};&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Above the above device tree says to use the &amp;quot;nordic-nrf-twim&amp;quot; driver and use the pins and such defined for I2C1.&amp;nbsp; Then what I do is in my device tree I add the following:&amp;nbsp;&lt;br /&gt;&lt;pre class="ui-code" data-mode="text"&gt;&amp;amp;i2c1 {
	tlv320aic3206: tlv320aic3206@18 {
		compatible=&amp;quot;i2c-device&amp;quot;;
		label=&amp;quot;TLV320AIC3206&amp;quot;;
		reg=&amp;lt;0x18&amp;gt;;
	};
	sx1509b: sx1509b@3E {
		compatible=&amp;quot;i2c-device&amp;quot;;
		label=&amp;quot;SX1509B&amp;quot;;
		reg=&amp;lt;0x3E&amp;gt;;
	};
	ltc2959: ltc2959@63 {
		compatible=&amp;quot;i2c-device&amp;quot;;
		label=&amp;quot;LTC2959&amp;quot;;
		reg=&amp;lt;0x63&amp;gt;;
	};
};&lt;/pre&gt;&lt;br /&gt;Here this says that I have three chips on the I2C and what the I2C address is for each chip.&amp;nbsp; Each chip will be a generic i2c-device driver, that is not any custom driver per chip, rather I will have to use I2C read/writes to talk to the chips in my code.&amp;nbsp;&lt;br /&gt;&lt;br /&gt;Zephyr still creates a driver for each chip, &amp;quot;i2c-device&amp;quot;.&amp;nbsp; All device tree drivers are referenced by getting a pointer to the &amp;#39;device&amp;#39;, but since these are &amp;quot;i2c-device&amp;quot; we can use the &amp;quot;i2c_dt_spec&amp;quot; special purpose device type pointers.&amp;nbsp;&lt;br /&gt;&lt;pre class="ui-code" data-mode="text"&gt;static const struct i2c_dt_spec dev_i2c = I2C_DT_SPEC_GET(DT_NODELABEL(tlv320aic3206));
static const struct i2c_dt_spec dev_i2c_sx1509b = I2C_DT_SPEC_GET(DT_NODELABEL(sx1509b));
static const struct i2c_dt_spec dev_i2c_ltc2959 = I2C_DT_SPEC_GET(DT_NODELABEL(ltc2959));&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Again the these macros are part of the device tree confusion, and basically a way to go from your device tree label to get a pointer to the actual device driver.&amp;nbsp; Once you have these pointers to the device driver you can use them with I2C driver API.&lt;a href="https://docs.zephyrproject.org/latest/hardware/peripherals/i2c.html"&gt;docs.zephyrproject.org/.../i2c.html&lt;/a&gt;&lt;br /&gt;&lt;pre class="ui-code" data-mode="text"&gt;i2c_write_read_dt()
i2c_write_dt()
i2c_read_dt()
&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Getting started the whole device tree system is a big confusing mess, and I found little documentation that helped me.&amp;nbsp; Once I figured out that the device tree compiles to the one header file, it helped a lot.&amp;nbsp; That is the device tree header file is the glue for everything.&lt;br /&gt;&lt;br /&gt;Also note that the Zephyr nomenclature is that the &amp;quot;compatible&amp;quot; part of the device tree selects which driver that node in device trees uses.&amp;nbsp; However Nordic&amp;#39;s UARTE driver does not use the compatible flag as designed.&amp;nbsp; Keep this in mind if you want to write custom &amp;#39;out of tree&amp;#39; UARTE drivers as that it is very hard to do so without a lot of work.&amp;nbsp; &amp;nbsp;The Zephyr guys indicate this is a bug in Nordic&amp;#39;s driver...&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>