<?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>Devicetree and nrfx SPI slave driver configuration</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/96717/devicetree-and-nrfx-spi-slave-driver-configuration</link><description>First - I haven&amp;#39;t been able to find an example of how to configure and use &amp;quot;nordic,nrf-spis&amp;quot; in NCS. If one exists that would probably help. 
 I configure SPI like this in devicetree (moved to using pinctrl to avoid warnings on pindefinitions - and it</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Thu, 23 Feb 2023 10:32:34 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/96717/devicetree-and-nrfx-spi-slave-driver-configuration" /><item><title>RE: Devicetree and nrfx SPI slave driver configuration</title><link>https://devzone.nordicsemi.com/thread/411619?ContentTypeID=1</link><pubDate>Thu, 23 Feb 2023 10:32:34 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:22861290-1a5e-4c37-aea1-f9a32abedbe4</guid><dc:creator>Tage Korsdal Nielsen</dc:creator><description>&lt;p&gt;With SPIS_xxx the SPIS events started to come through (the CSN was correctly set), but then I didn&amp;#39;t get any RX data.&lt;/p&gt;
&lt;p&gt;For now I am OK using the hack above until zephyr or nrf contains a working (loopback maybe?) SPIM/SPIS example.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Devicetree and nrfx SPI slave driver configuration</title><link>https://devzone.nordicsemi.com/thread/411180?ContentTypeID=1</link><pubDate>Tue, 21 Feb 2023 16:33:03 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:cbf9b908-7a93-4647-bdcb-178fb8976c63</guid><dc:creator>haakonsh</dc:creator><description>&lt;p&gt;Did you read my last reply?&amp;nbsp;&lt;br /&gt;&lt;br /&gt;The nRF52832&amp;#39;s SPIM peripheral does not have an PSEL.CSN register, therefore&amp;nbsp;pinctrl_nrf.c skips configuration of NRF_PSEL(SPIM_CSN, 0, 12).&lt;br /&gt;You need to use&amp;nbsp;&lt;span&gt;SPIS instead of SPIM in the pinctrl devicetree node.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;From&amp;nbsp;zephyr\drivers\pinctrl\pinctrl_nrf.c:&lt;br /&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;int pinctrl_configure_pins(const pinctrl_soc_pin_t *pins, uint8_t pin_cnt,
			   uintptr_t reg)
{
	for (uint8_t i = 0U; i &amp;lt; pin_cnt; i++) {
		nrf_gpio_pin_drive_t drive = NRF_GET_DRIVE(pins[i]);
		uint32_t pin = NRF_GET_PIN(pins[i]);
		uint32_t write = NO_WRITE;
		nrf_gpio_pin_dir_t dir;
		nrf_gpio_pin_input_t input;

		if (pin == NRF_PIN_DISCONNECTED) {
			pin = 0xFFFFFFFFU;
		}

		switch (NRF_GET_FUN(pins[i])) {

#if defined(NRF_PSEL_SPIM)
		case NRF_FUN_SPIM_SCK:
			NRF_PSEL_SPIM(reg, SCK) = pin;
			write = 0U;
			dir = NRF_GPIO_PIN_DIR_OUTPUT;
			input = NRF_GPIO_PIN_INPUT_CONNECT;
			break;
		case NRF_FUN_SPIM_MOSI:
			NRF_PSEL_SPIM(reg, MOSI) = pin;
			write = 0U;
			dir = NRF_GPIO_PIN_DIR_OUTPUT;
			input = NRF_GPIO_PIN_INPUT_DISCONNECT;
			break;
		case NRF_FUN_SPIM_MISO:
			NRF_PSEL_SPIM(reg, MISO) = pin;
			dir = NRF_GPIO_PIN_DIR_INPUT;
			input = NRF_GPIO_PIN_INPUT_CONNECT;
			break;
#endif /* defined(NRF_PSEL_SPIM) */

#if defined(NRF_PSEL_SPIS)
		case NRF_FUN_SPIS_SCK:
			NRF_PSEL_SPIS(reg, SCK) = pin;
			dir = NRF_GPIO_PIN_DIR_INPUT;
			input = NRF_GPIO_PIN_INPUT_CONNECT;
			break;
		case NRF_FUN_SPIS_MOSI:
			NRF_PSEL_SPIS(reg, MOSI) = pin;
			dir = NRF_GPIO_PIN_DIR_INPUT;
			input = NRF_GPIO_PIN_INPUT_CONNECT;
			break;
		case NRF_FUN_SPIS_MISO:
			NRF_PSEL_SPIS(reg, MISO) = pin;
			dir = NRF_GPIO_PIN_DIR_INPUT;
			input = NRF_GPIO_PIN_INPUT_DISCONNECT;
			break;
		case NRF_FUN_SPIS_CSN:
			NRF_PSEL_SPIS(reg, CSN) = pin;
			dir = NRF_GPIO_PIN_DIR_INPUT;
			input = NRF_GPIO_PIN_INPUT_CONNECT;
			break;
#endif /* defined(NRF_PSEL_SPIS) */&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Devicetree and nrfx SPI slave driver configuration</title><link>https://devzone.nordicsemi.com/thread/411149?ContentTypeID=1</link><pubDate>Tue, 21 Feb 2023 14:37:16 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:08dd7d9c-ebcf-4eb7-ba0f-75df5c9bdd03</guid><dc:creator>Tage Korsdal Nielsen</dc:creator><description>&lt;p&gt;Hi Haakon,&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Thanks for this.&lt;/p&gt;
&lt;p&gt;I got it working by adding the following hack in my slave SPI init code:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;    // Hack to get CS pin configured for slave
	NRF_SPIS_Type *spis = ( NRF_SPIS_Type *)DT_REG_ADDR( DT_CHOSEN( bluejay_ipc_slave ));
	spis-&amp;gt;PSEL.CSN = DT_SPI_DEV_CS_GPIOS_PIN( DT_NODELABEL( a ));&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Thank you for providing alternatives.&lt;/p&gt;
&lt;p&gt;Do you know of an upcoming&amp;nbsp;nordic,nrf-spis update that will make it work with pin control driver?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Devicetree and nrfx SPI slave driver configuration</title><link>https://devzone.nordicsemi.com/thread/411145?ContentTypeID=1</link><pubDate>Tue, 21 Feb 2023 14:27:16 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a9f7a152-c763-4320-9bf3-1078ab099975</guid><dc:creator>haakonsh</dc:creator><description>&lt;p&gt;-edit, typo:&amp;nbsp;NRF_PSEL(SPIM_XXXX&amp;nbsp; ---&amp;gt;&amp;nbsp;NRF_PSEL(SPIS_XXXX&lt;br /&gt;&lt;br /&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;/delete-node/ &amp;amp;boot_partition;
/delete-node/ &amp;amp;slot1_partition;
/delete-node/ &amp;amp;i2c0;

/ {
	chosen {
		bluejay,ipc-slave = &amp;amp;spi0;
	};
};
&amp;amp;pinctrl {
	spi0_default: spi0_default {
		group1 {
			psels = &amp;lt;NRF_PSEL(SPIS_SCK, 0, 18)&amp;gt;,
				&amp;lt;NRF_PSEL(SPIS_MISO, 0, 14)&amp;gt;,
				&amp;lt;NRF_PSEL(SPIS_MOSI, 0, 16)&amp;gt;,
				&amp;lt;NRF_PSEL(SPIS_CSN, 0, 12);
		};
	};	

	spi0_sleep: spi0_sleep {
		group1 {
			psels = &amp;lt;NRF_PSEL(SPIM_SCK, 0, 18)&amp;gt;,
				&amp;lt;NRF_PSEL(SPIM_MISO, 0, 14)&amp;gt;,
				&amp;lt;NRF_PSEL(SPIM_MOSI, 0, 16)&amp;gt;,
				&amp;lt;NRF_PSEL(SPIM_CSN, 0, 12);
			low-power-enable;

		};
	};	
};

&amp;amp;spi0 {
		compatible = &amp;quot;nordic,nrf-spis&amp;quot;;
		status = &amp;quot;okay&amp;quot;;
		a: spi-device@0 {
			reg = &amp;lt;0&amp;gt;;
		};
		pinctrl-0 = &amp;lt;&amp;amp;spi0_default&amp;gt;;
		pinctrl-1 = &amp;lt;&amp;amp;spi0_sleep&amp;gt;;
		pinctrl-names = &amp;quot;default&amp;quot;, &amp;quot;sleep&amp;quot;;
		def-char = &amp;lt;0xFF&amp;gt;;
		overrun-character = &amp;lt;0xaa&amp;gt;;
};
/delete-node/ &amp;amp;led3;

/ {
	leds {
                led3: led_3 {
			gpios = &amp;lt;&amp;amp;gpio0 22 GPIO_ACTIVE_LOW&amp;gt;;
			label = &amp;quot;Green LED 3&amp;quot;;
                };
		gpio_evt: gpio_evt_0 {
                        gpios = &amp;lt;&amp;amp;gpio0 20 (GPIO_PUSH_PULL | GPIO_ACTIVE_LOW)&amp;gt;;
			label = &amp;quot;GPIO Event Pin&amp;quot;;
		};
	};
};&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Devicetree and nrfx SPI slave driver configuration</title><link>https://devzone.nordicsemi.com/thread/411114?ContentTypeID=1</link><pubDate>Tue, 21 Feb 2023 13:20:10 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a35164ca-83c6-4954-81b7-0d377286e35b</guid><dc:creator>haakonsh</dc:creator><description>&lt;p&gt;Try this:&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;/delete-node/ &amp;amp;boot_partition;
/delete-node/ &amp;amp;slot1_partition;
/delete-node/ &amp;amp;i2c0;

/ {
	chosen {
		bluejay,ipc-slave = &amp;amp;spi0;
	};
};
&amp;amp;pinctrl {
	spi0_default: spi0_default {
		group1 {
			psels = &amp;lt;NRF_PSEL(SPIM_SCK, 0, 18)&amp;gt;,
				&amp;lt;NRF_PSEL(SPIM_MISO, 0, 14)&amp;gt;,
				&amp;lt;NRF_PSEL(SPIM_MOSI, 0, 16)&amp;gt;,
				&amp;lt;NRF_PSEL(SPIM_CSN, 0, 12);
		};
	};	

	spi0_sleep: spi0_sleep {
		group1 {
			psels = &amp;lt;NRF_PSEL(SPIM_SCK, 0, 18)&amp;gt;,
				&amp;lt;NRF_PSEL(SPIM_MISO, 0, 14)&amp;gt;,
				&amp;lt;NRF_PSEL(SPIM_MOSI, 0, 16)&amp;gt;,
				&amp;lt;NRF_PSEL(SPIM_CSN, 0, 12);
			low-power-enable;

		};
	};	
};

&amp;amp;spi0 {
		compatible = &amp;quot;nordic,nrf-spis&amp;quot;;
		status = &amp;quot;okay&amp;quot;;
		a: spi-device@0 {
			reg = &amp;lt;0&amp;gt;;
		};
		pinctrl-0 = &amp;lt;&amp;amp;spi0_default&amp;gt;;
		pinctrl-1 = &amp;lt;&amp;amp;spi0_sleep&amp;gt;;
		pinctrl-names = &amp;quot;default&amp;quot;, &amp;quot;sleep&amp;quot;;
		def-char = &amp;lt;0xFF&amp;gt;;
		overrun-character = &amp;lt;0xaa&amp;gt;;
};
/delete-node/ &amp;amp;led3;

/ {
	leds {
                led3: led_3 {
			gpios = &amp;lt;&amp;amp;gpio0 22 GPIO_ACTIVE_LOW&amp;gt;;
			label = &amp;quot;Green LED 3&amp;quot;;
                };
		gpio_evt: gpio_evt_0 {
                        gpios = &amp;lt;&amp;amp;gpio0 20 (GPIO_PUSH_PULL | GPIO_ACTIVE_LOW)&amp;gt;;
			label = &amp;quot;GPIO Event Pin&amp;quot;;
		};
	};
};&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;From&amp;nbsp;zephyr\dts\bindings\spi\nordic,nrf-spis.yaml:&amp;nbsp;&lt;br /&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;# Copyright (c) 2019 Nordic Semiconductor ASA
# SPDX-License-Identifier: Apache-2.0

description: Nordic nRF family SPIS (SPI slave with EasyDMA)

compatible: &amp;quot;nordic,nrf-spis&amp;quot;

include: nordic,nrf-spi-common.yaml

properties:
    csn-pin:
      type: int
      deprecated: true
      required: false
      description: |
        IMPORTANT: This option will only be used if the new pin control driver
        is not enabled.

        The CSN pin to use. The pin numbering scheme is the same as
        the sck-pin property&amp;#39;s.
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;From&amp;nbsp;zephyr\include\zephyr\dt-bindings\pinctrl\nrf-pinctrl.h:&lt;br /&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;#define NRF_FUN_SPIS_SCK 7U
/** SPI slave MOSI */
#define NRF_FUN_SPIS_MOSI 8U
/** SPI slave MISO */
#define NRF_FUN_SPIS_MISO 9U
/** SPI slave CSN */
#define NRF_FUN_SPIS_CSN 10U&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I believe the new pinctrl feature of zephyr is intended to allow for multiple drivers to share access to the same pins, i.e. runtime re-configuration of a GPIO multiplexer. Previously, only one devicetree node could have access to a GPIO, defined at compile-time.&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Devicetree and nrfx SPI slave driver configuration</title><link>https://devzone.nordicsemi.com/thread/411052?ContentTypeID=1</link><pubDate>Tue, 21 Feb 2023 09:32:33 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ddab7dbe-15b5-450d-ad4c-d8d77424737d</guid><dc:creator>Tage Korsdal Nielsen</dc:creator><description>&lt;p&gt;During initialization of the nrfx driver PSEL.CSN is not set - all other pins are set.&lt;/p&gt;
&lt;p&gt;If I set the PSEL.CSN like this in&amp;nbsp;spi_nrfx_init&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;dev_config-&amp;gt;spis.p_reg-&amp;gt;PSEL.CSN = 12;&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;it works, so obviously my configuration is not right.&lt;/p&gt;
&lt;p&gt;Overlay for slave SPI:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;/delete-node/ &amp;amp;boot_partition;
/delete-node/ &amp;amp;slot1_partition;
/delete-node/ &amp;amp;i2c0;

/ {
	chosen {
		bluejay,ipc-slave = &amp;amp;spi0;
	};
};
&amp;amp;pinctrl {
	spi0_default: spi0_default {
		group1 {
			psels = &amp;lt;NRF_PSEL(SPIM_SCK, 0, 18)&amp;gt;,
				&amp;lt;NRF_PSEL(SPIM_MISO, 0, 14)&amp;gt;,
				&amp;lt;NRF_PSEL(SPIM_MOSI, 0, 16)&amp;gt;;
		};
	};	

	spi0_sleep: spi0_sleep {
		group1 {
			psels = &amp;lt;NRF_PSEL(SPIM_SCK, 0, 18)&amp;gt;,
				&amp;lt;NRF_PSEL(SPIM_MISO, 0, 14)&amp;gt;,
				&amp;lt;NRF_PSEL(SPIM_MOSI, 0, 16)&amp;gt;;
			low-power-enable;

		};
	};	
};

&amp;amp;spi0 {
		compatible = &amp;quot;nordic,nrf-spis&amp;quot;;
		status = &amp;quot;okay&amp;quot;;
		cs-gpios = &amp;lt;&amp;amp;gpio0 12 GPIO_ACTIVE_LOW&amp;gt;;
		a: spi-device@0 {
			reg = &amp;lt;0&amp;gt;;
		};
		pinctrl-0 = &amp;lt;&amp;amp;spi0_default&amp;gt;;
		pinctrl-1 = &amp;lt;&amp;amp;spi0_sleep&amp;gt;;
		pinctrl-names = &amp;quot;default&amp;quot;, &amp;quot;sleep&amp;quot;;
		def-char = &amp;lt;0xFF&amp;gt;;
		overrun-character = &amp;lt;0xaa&amp;gt;;
};
/delete-node/ &amp;amp;led3;

/ {
	leds {
                led3: led_3 {
			gpios = &amp;lt;&amp;amp;gpio0 22 GPIO_ACTIVE_LOW&amp;gt;;
			label = &amp;quot;Green LED 3&amp;quot;;
                };
		gpio_evt: gpio_evt_0 {
                        gpios = &amp;lt;&amp;amp;gpio0 20 (GPIO_PUSH_PULL | GPIO_ACTIVE_LOW)&amp;gt;;
			label = &amp;quot;GPIO Event Pin&amp;quot;;
		};
	};
};

&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Devicetree and nrfx SPI slave driver configuration</title><link>https://devzone.nordicsemi.com/thread/410949?ContentTypeID=1</link><pubDate>Mon, 20 Feb 2023 15:22:32 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:52f6a32e-790e-4d97-b876-e9efa585aeef</guid><dc:creator>Tage Korsdal Nielsen</dc:creator><description>&lt;p&gt;First I tried this principle with a SPI master. That worked - needed to specify the CS pin in the spi_config like this:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;    spi_device_cfg.cs = SPI_CS_CONTROL_PTR_DT(DT_NODELABEL(spiipc), 2);&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Which makes sense for a master SPI. I got the driver reference like this:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;static const struct device *spi_device = DEVICE_DT_GET(DT_CHOSEN(bluejay_ipc_master));&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;However, I cannot get the slave working.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I have tried different ways of configuring:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;With DT_CHOSEN like the working SPI master.&lt;/li&gt;
&lt;li&gt;DT_NODELABEL( a ) like you suggest.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;1) compiles but doesn&amp;#39;t work. 2) doesn&amp;#39;t compile.&lt;/p&gt;
&lt;p&gt;When I break at configure_pins in module &amp;quot;nrfx_spis.c&amp;quot;, both skip_gpio_cfg and skip_psel_cfg of p_config is true, leading me to&amp;nbsp;conclude that the CS pin is never configured by the driver.&lt;/p&gt;
&lt;p&gt;What to do?&lt;/p&gt;
&lt;p&gt;rgds Tage&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Devicetree and nrfx SPI slave driver configuration</title><link>https://devzone.nordicsemi.com/thread/410578?ContentTypeID=1</link><pubDate>Fri, 17 Feb 2023 09:58:15 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:75db7663-efa0-4d76-845d-b8e46ae41b33</guid><dc:creator>haakonsh</dc:creator><description>&lt;p&gt;If you don&amp;#39;t have a very specific reason to&amp;nbsp;use the nrfx drivers directly the zephyr device api is really useful when interfacing with devices on the various serial buses, especially when there already exists a driver for that device.&amp;nbsp;&lt;br /&gt;&lt;br /&gt;The NCS sdk has come a long way since 1.6.0, and I hope that you&amp;#39;ll have the same experience developing on this platform as I have :)&lt;br /&gt;&lt;br /&gt;Note that the code in by previous instructions might not compile out of the box, but it should be fairly close. I find that studying other device drivers in zephyr helpful in understanding how to use or write new ones.&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: Devicetree and nrfx SPI slave driver configuration</title><link>https://devzone.nordicsemi.com/thread/410355?ContentTypeID=1</link><pubDate>Thu, 16 Feb 2023 13:04:07 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e4db7eb0-36f3-47a4-947f-19ee88235b6c</guid><dc:creator>Tage Korsdal Nielsen</dc:creator><description>&lt;p&gt;Thank you Haakon - this makes a &lt;strong&gt;lot&lt;/strong&gt; of sense.&lt;/p&gt;
&lt;p&gt;I am porting existing ncs 1.6.0 based code which makes direct use of nrfx_spis calls. The device agnostic approach you show here sure is a lot more easy on the eye!&lt;/p&gt;
&lt;p&gt;I will refactor our SPIS code accordingly and swing by here again and close the ticket when I have verified functionality.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Devicetree and nrfx SPI slave driver configuration</title><link>https://devzone.nordicsemi.com/thread/410345?ContentTypeID=1</link><pubDate>Thu, 16 Feb 2023 12:46:33 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:750c0ebc-0419-46cc-85ff-81eef006804d</guid><dc:creator>haakonsh</dc:creator><description>&lt;p&gt;The short answer is &amp;#39;you&amp;#39;re not supposed to&amp;#39;.&amp;nbsp;&lt;br /&gt;&lt;br /&gt;Zephyr&amp;#39;s SPI device driver API is the intended access point for your &amp;#39;a: spi-dev-a@0&amp;#39; devicetree node.&lt;/p&gt;
&lt;p&gt;The build system knows&amp;nbsp;that the&amp;nbsp;&lt;span&gt;&amp;#39;a: spi-dev-a@0&amp;#39; node is a child node of the SPI controller node &amp;#39;spi0&amp;#39; because its declaration follows the rules of how a child node is decleared (ie. inside its parent node).&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;The build system knows&amp;nbsp;that&amp;nbsp;the &amp;#39;spi0&amp;#39; is an SPI controller node because its &amp;#39;compatible&amp;#39; property is&amp;nbsp;&amp;quot;nordic,nrf-spis&amp;quot;.&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;The build system knows&amp;nbsp;that&amp;nbsp;the&amp;nbsp;&amp;#39;compatible&amp;#39; property of the &amp;#39;spi0&amp;#39; node refers to a specific devicetree &amp;#39;binding&amp;#39;, in this case zephyr\dts\bindings\spi\nordic,nrf-spis.yaml:&amp;nbsp;&lt;br /&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;description: Nordic nRF family SPIS (SPI slave with EasyDMA)

compatible: &amp;quot;nordic,nrf-spis&amp;quot;

include: nordic,nrf-spi-common.yaml

properties:
    def-char:
      type: int
      required: true
      description: |
          Default character. Character clocked out when the slave was not
          provided with buffers and is ignoring the transaction.&lt;/pre&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;nordic,nrf-spis.yaml includes another binding,&amp;nbsp;nordic,nrf-spi-common.yaml. Who, in turn, includes the&amp;nbsp;&lt;/span&gt;spi-controller.yaml binding.&amp;nbsp;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;The build system now knows that the &amp;#39;spi0&amp;#39; node is in fact an SPI controller node, and it needs a driver that has implemented the SPI controller API.&amp;nbsp;&lt;/span&gt;&lt;span&gt;In this case it is the&amp;nbsp;&lt;/span&gt;spi_nrfx_spis.c driver.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;If CONFIG_NRFX_SPIS and CONFIG_NRFX_SPIS0 =y the&amp;nbsp;&lt;span&gt;spi_nrfx_spis.c will be&amp;nbsp;built and linked to the zephyr&amp;#39;s SPI controller API functions.&amp;nbsp;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;The zephyr\drivers\spi\spi_nrfx_spis.c line 267-342 will initialize the driver in modules\hal\nordic\nrfx\drivers\src\nrfx_spis.c after the kernel has booted,&amp;nbsp;with the configuration set in your DTS and DTS overlay files.&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;/*
 * Current factors requiring use of DT_NODELABEL:
 *
 * - HAL design (requirement of drv_inst_idx in nrfx_spis_t)
 * - Name-based HAL IRQ handlers, e.g. nrfx_spis_0_irq_handler
 */

#define SPIS(idx) DT_NODELABEL(spi##idx)
#define SPIS_PROP(idx, prop) DT_PROP(SPIS(idx), prop)

#define SPI_NRFX_SPIS_PIN_CFG(idx)					\
	COND_CODE_1(CONFIG_PINCTRL,					\
		(.skip_gpio_cfg = true,					\
		 .skip_psel_cfg = true,),				\
		(.sck_pin    = SPIS_PROP(idx, sck_pin),			\
		 .mosi_pin   = DT_PROP_OR(SPIS(idx), mosi_pin,		\
					  NRFX_SPIS_PIN_NOT_USED),	\
		 .miso_pin   = DT_PROP_OR(SPIS(idx), miso_pin,		\
					  NRFX_SPIS_PIN_NOT_USED),	\
		 .csn_pin    = SPIS_PROP(idx, csn_pin),			\
		 .csn_pullup = NRF_GPIO_PIN_NOPULL,			\
		 .miso_drive = NRF_GPIO_PIN_S0S1,))

#define SPI_NRFX_SPIS_DEFINE(idx)					       \
	NRF_DT_CHECK_PIN_ASSIGNMENTS(SPIS(idx), 0,			       \
				     sck_pin, mosi_pin, miso_pin, csn_pin);    \
	static void irq_connect##idx(void)				       \
	{								       \
		IRQ_CONNECT(DT_IRQN(SPIS(idx)), DT_IRQ(SPIS(idx), priority),   \
			    nrfx_isr, nrfx_spis_##idx##_irq_handler, 0);       \
	}								       \
	static struct spi_nrfx_data spi_##idx##_data = {		       \
		SPI_CONTEXT_INIT_LOCK(spi_##idx##_data, ctx),		       \
		SPI_CONTEXT_INIT_SYNC(spi_##idx##_data, ctx),		       \
	};								       \
	IF_ENABLED(CONFIG_PINCTRL, (PINCTRL_DT_DEFINE(SPIS(idx))));	       \
	static const struct spi_nrfx_config spi_##idx##z_config = {	       \
		.spis = {						       \
			.p_reg = (NRF_SPIS_Type *)DT_REG_ADDR(SPIS(idx)),      \
			.drv_inst_idx = NRFX_SPIS##idx##_INST_IDX,	       \
		},							       \
		.config = {						       \
			SPI_NRFX_SPIS_PIN_CFG(idx)			       \
			.mode      = NRF_SPIS_MODE_0,			       \
			.bit_order = NRF_SPIS_BIT_ORDER_MSB_FIRST,	       \
			.orc       = SPIS_PROP(idx, overrun_character),	       \
			.def       = SPIS_PROP(idx, def_char),		       \
		},							       \
		.irq_connect = irq_connect##idx,			       \
		IF_ENABLED(CONFIG_PINCTRL,				       \
			(.pcfg = PINCTRL_DT_DEV_CONFIG_GET(SPIS(idx)),))       \
	};								       \
	DEVICE_DT_DEFINE(SPIS(idx),					       \
			    spi_nrfx_init,				       \
			    NULL,					       \
			    &amp;amp;spi_##idx##_data,				       \
			    &amp;amp;spi_##idx##z_config,			       \
			    POST_KERNEL,				       \
			    CONFIG_SPI_INIT_PRIORITY,			       \
			    &amp;amp;spi_nrfx_driver_api)

#ifdef CONFIG_SPI_0_NRF_SPIS
SPI_NRFX_SPIS_DEFINE(0);
#endif

#ifdef CONFIG_SPI_1_NRF_SPIS
SPI_NRFX_SPIS_DEFINE(1);
#endif

#ifdef CONFIG_SPI_2_NRF_SPIS
SPI_NRFX_SPIS_DEFINE(2);
#endif

#ifdef CONFIG_SPI_3_NRF_SPIS
SPI_NRFX_SPIS_DEFINE(3);
#endif&lt;/pre&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;To use zephyr&amp;#39;s SPI device driver API you need to let the build system know that the devicetree node you accessing is in fact an SPI device. This is done by adding the property &amp;#39;compatible = &amp;quot;spi-device&amp;quot;;&amp;#39; to your&amp;nbsp;&amp;#39;a: spi-dev-a@0&amp;#39; node.&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Now you can access your SPI device:&lt;br /&gt;&lt;pre class="ui-code" data-mode="text"&gt;#include &amp;lt;zephyr/drivers/spi.h&amp;gt;

spi_device_callback(const struct device *dev, int result, void *data)
{
    //Get result and data
}

void main(void)
{
    const struct device *const spi_device = DEVICE_DT_GET(DT_NODELABEL(a));
    
    if (!device_is_ready(spi_device)) {
    		printk(&amp;quot;%s: device not ready.\n&amp;quot;, spi_device-&amp;gt;name);
    		return;
    	}
    
    const struct spi_driver_api *spi_device_api = (const struct spi_driver_api *)spi_device-&amp;gt;api;
    
    const struct spi_config spi_device_cfg;
    spi_device_cfg.operation = SPI_OP_MODE_SLAVE;
    
    uint8_t spi_device_tx_buf[128];
    struct spi_buf tx_buf;
    tx_buf.buf = &amp;amp;spi_device_tx_buf;
    tx_buf.len = sizeof(spi_device_tx_buf);
    const struct spi_buf_set tx_bufs;
    tx_bufs.buffers = &amp;amp;tx_buf;
    tx_bufs.count = 1;
    
    uint8_t spi_device_rx_buf[128];
    struct spi_buf rx_buf;
    rx_buf.buf = &amp;amp;spi_device_rx_buf;
    rx_buf.len = sizeof(spi_device_rx_buf);
    const struct spi_buf_set rx_bufs;
    rx_bufs.buffers = &amp;amp;rx_buf;
    rx_bufs.count = 1;
    
    int rc = 0;
    rc = spi_device_api.transceive_async(spi_device, &amp;amp;spi_device_cfg, &amp;amp;tx_bufs, &amp;amp;rx_bufs, spi_device_callback, NULL);
}&lt;/pre&gt;&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&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>