<?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>how to initialize spi_cfg and switch the slave when multiple slaves on a same spi bus?</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/82289/how-to-initialize-spi_cfg-and-switch-the-slave-when-multiple-slaves-on-a-same-spi-bus</link><description>Hi Nordic, 
 In case of multiple slaves on the same spi bus, what is the best way to initialize &amp;#39; spi_config&amp;#39; for spi_transceive ( ) when Write device drivers using devicetree APIs by Option 1: create devices using instance numbers ? 
 Does SPI_CONFIG_DT_INST</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 03 Dec 2021 09:02:02 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/82289/how-to-initialize-spi_cfg-and-switch-the-slave-when-multiple-slaves-on-a-same-spi-bus" /><item><title>RE: how to initialize spi_cfg and switch the slave when multiple slaves on a same spi bus?</title><link>https://devzone.nordicsemi.com/thread/341806?ContentTypeID=1</link><pubDate>Fri, 03 Dec 2021 09:02:02 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6c9da5f8-53d2-4fc8-a420-f8c0d88bb02a</guid><dc:creator>scottwan</dc:creator><description>&lt;p&gt;Hi Simon,&lt;/p&gt;
&lt;p&gt;I use&amp;nbsp;&lt;span&gt;DEVICE_DT_GET&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;node_id&lt;/span&gt;&lt;span&gt;)&amp;nbsp;&lt;/span&gt;can get the specific spi device and get the correct specific data which is the device chip id 0xd1 and 0xd2 as in picture below by call &lt;span&gt;spi_transceive&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;) in sensor_channel_get()&amp;nbsp;function.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;There still have a problem, the sensor driver initiation function get the same chip id&amp;nbsp;but sensor_channel_get function can get different chip id:&lt;/p&gt;
&lt;p&gt;&lt;img alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/320x240/__key/communityserver-discussions-components-files/4/same-chip-id-in-init-func.png" /&gt;&lt;/p&gt;
&lt;p&gt;main():&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;pre class="ui-code" data-mode="text"&gt;#include &amp;lt;zephyr.h&amp;gt;
#include &amp;lt;sys/printk.h&amp;gt;
#include &amp;lt;drivers/sensor.h&amp;gt;

/* 1000 msec = 1 sec */
#define SLEEP_TIME_MS   1000
#define ads129xr0 DT_NODELABEL(ads129xr0)
#define ads129xr1 DT_NODELABEL(ads129xr1)

void main(void)
{
	struct sensor_value value;
	// const struct device *dev = DEVICE_DT_GET_ANY(ti_ads129xr);
	const struct device *ads129xr0 = DEVICE_DT_GET(ads129xr0);
	const struct device *ads129xr1 = DEVICE_DT_GET(ads129xr1);

	if (!device_is_ready(ads129xr0)) {
		printk(&amp;quot;Device %s is not ready\n&amp;quot;, ads129xr0-&amp;gt;name);
		return;
	}
	if (!device_is_ready(ads129xr1)) {
		printk(&amp;quot;Device %s is not ready\n&amp;quot;, ads129xr1-&amp;gt;name);
		return;
	}

	sensor_channel_get(ads129xr0, SENSOR_CHAN_ALL, &amp;amp;value);
	sensor_channel_get(ads129xr1, SENSOR_CHAN_ALL, &amp;amp;value);

	// printk(&amp;quot;ads129xr volt %d.%d\r\n&amp;quot;, value.val1, value.val2);

	printk(&amp;quot;Hello World! %s\n&amp;quot;, CONFIG_BOARD);
	
	while (1) {
		// printk(&amp;quot;Hello World! %s\n&amp;quot;, CONFIG_BOARD);
		k_msleep(SLEEP_TIME_MS);
	}
}
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;I think the problem raised because at the beginning, both device CS are in active status during the first device call spi_tranceive(), so uncertain result received, the result maybe from the first device but also probably from the other one depends who&amp;#39;s signal arrive MCU first.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: how to initialize spi_cfg and switch the slave when multiple slaves on a same spi bus?</title><link>https://devzone.nordicsemi.com/thread/341598?ContentTypeID=1</link><pubDate>Wed, 01 Dec 2021 22:10:38 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:45a97e68-2355-442a-a585-51daed58fcc3</guid><dc:creator>Simon</dc:creator><description>[quote user=""]&lt;span&gt;Does&amp;nbsp;&lt;/span&gt;&lt;code&gt;&lt;span&gt;&lt;span&gt;&lt;a title="SPI_CONFIG_DT_INST(inst, operation_, delay_)" href="https://docs.zephyrproject.org/latest/reference/peripherals/spi.html?highlight=spi_config_dt_inst#c.SPI_CONFIG_DT_INST" rel="noopener noreferrer" target="_blank"&gt;SPI_CONFIG_DT_INST&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;a title="SPI_CONFIG_DT_INST(inst, operation_, delay_)" href="https://docs.zephyrproject.org/latest/reference/peripherals/spi.html?highlight=spi_config_dt_inst#c.SPI_CONFIG_DT_INST" rel="noopener noreferrer" target="_blank"&gt;&lt;span&gt;(&lt;/span&gt;&lt;em&gt;&lt;span&gt;inst&lt;/span&gt;&lt;/em&gt;&lt;span&gt;,&amp;nbsp;&lt;/span&gt;&lt;em&gt;&lt;span&gt;operation_&lt;/span&gt;&lt;/em&gt;&lt;span&gt;,&amp;nbsp;&lt;/span&gt;&lt;em&gt;&lt;span&gt;delay_&lt;/span&gt;&lt;/em&gt;&lt;/a&gt;&lt;span&gt;&lt;a title="SPI_CONFIG_DT_INST(inst, operation_, delay_)" href="https://docs.zephyrproject.org/latest/reference/peripherals/spi.html?highlight=spi_config_dt_inst#c.SPI_CONFIG_DT_INST" rel="noopener noreferrer" target="_blank"&gt;)&lt;/a&gt;&amp;nbsp; can be used for multiple slaves on the same spi bus? and how to select or active the specific slave when call&amp;nbsp;&lt;span&gt;spi_transceive&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)?&lt;/span&gt;&lt;/span&gt;[/quote]
&lt;p&gt;I looked into the documentation, and it does not seem like using the&amp;nbsp;&lt;span&gt;Devicetree instance number&amp;nbsp;is the way to go if you want to get a specific node (slave). Check out the comments given in&amp;nbsp;&lt;a href="https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.7.1/zephyr/guides/dts/howtos.html#option-1-create-devices-using-instance-numbers"&gt;Option 1: create devices using instance numbers&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.7.1/zephyr/reference/devicetree/api.html#c.DT_INST"&gt;API Reference--&amp;gt;Devicetree--&amp;gt;Devicetree API&lt;/a&gt;:&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;em&gt;&amp;quot;However, they only work when devicetree nodes for your driver’s&amp;nbsp;&lt;code&gt;&lt;span&gt;compatible&lt;/span&gt;&lt;/code&gt;&amp;nbsp;are all equivalent, and you &lt;span style="text-decoration:underline;"&gt;&lt;strong&gt;do not need to be able to distinguish between them&lt;/strong&gt;&lt;/span&gt;.&amp;quot;&lt;/em&gt;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&amp;quot;instance numbers&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;strong&gt;in no way reflect&lt;/strong&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;any numbering scheme that might exist in SoC documentation, node labels or unit addresses, or properties of the /aliases node (use&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;a href="https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.7.1/zephyr/reference/devicetree/api.html#group__devicetree-generic-id_1gab7d23294a6bf7fd44a98b48ec47d8a79"&gt;&lt;span&gt;DT_NODELABEL()&lt;/span&gt;&lt;/a&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;or&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;a href="https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.7.1/zephyr/reference/devicetree/api.html#group__devicetree-generic-id_1gaa49e19bbc39dc0d6f16b78a5d02482c9"&gt;&lt;span&gt;DT_ALIAS()&lt;/span&gt;&lt;/a&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;for those)&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;there&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;strong&gt;is no general guarantee&lt;/strong&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;that the same node will have the same instance number between builds, even if you are building the same application again in the same build directory&amp;quot;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;/p&gt;
&lt;p&gt;I would recommend you to go for&amp;nbsp;&lt;a href="https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.7.1/zephyr/guides/dts/howtos.html#option-2-create-devices-using-node-labels"&gt;Option 2: create devices using node labels&lt;/a&gt;&amp;nbsp;instead, then you will be able to get a specific node (slave).&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Simon&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>