SPI for nrf5340dk in ncsv2.8

nrfConnectSDK V2.8

Toolchain

Board: nrf5340dk

Hi, while compiling ncs-spi-master-slave-example , I am getting below errors

In function 'spi_init':
C:/Users/hello/spi/src/main.c:34:8: error: macro "GPIO_DT_SPEC_GET_BY_IDX_OR" requires 4 arguments, but only 3 given
34 | struct gpio_dt_spec spim_cs_gpio = MY_SPI_MASTER_CS_DT_SPEC;


In file included from C:/Users/hello/spi/src/main.c:11:
C:/ncs/v2.8.0/zephyr/include/zephyr/drivers/spi.h:213:9: error: 'GPIO_DT_SPEC_GET_BY_IDX_OR' undeclared (first use in this function)
213 | GPIO_DT_SPEC_GET_BY_IDX_OR(DT_BUS(spi_dev), cs_gpios, \

C:/Users/hello/spi/src/main.c:44:8: error: macro "GPIO_DT_SPEC_GET_BY_IDX_OR" requires 4 arguments, but only 3 given
44 | .cs = {.gpio = MY_SPI_MASTER_CS_DT_SPEC, .delay = 0},

C:/ncs/v2.8.0/zephyr/include/zephyr/drivers/spi.h:213:9: error: 'GPIO_DT_SPEC_GET_BY_IDX_OR' undeclared here (not in a function)
213 | GPIO_DT_SPEC_GET_BY_IDX_OR(DT_BUS(spi_dev), cs_gpios, \

Is it related to sdk version? Can anyone help to resolve ?

Parents
  • Hi,

    I recommend you go through the SPI lesson on our academy pages https://academy.nordicsemi.com/courses/nrf-connect-sdk-intermediate/lessons/lesson-5-serial-peripheral-interface-spi/ as well as the sysbuild exercise https://academy.nordicsemi.com/courses/nrf-connect-sdk-intermediate/lessons/lesson-8-sysbuild/topic/sysbuild-explained/.

    The unofficial sample that you're using will unfortunately not be maintained and upgraded to support any newer release and the latest version this sample was supported for is NCS older than v2.7.0 as far as I can see.

    The only resolution for you is to go through the two supplied links, understand the topics and apply the required changes to the sample you've based the application on.

    Kind regards,
    Andreas

  • Hi, We want a design where we want use 2 spi buses for 4 slaves.

    &pinctrl {
    	spi_master1_default: spi_master1_default {
    		group1 {
    			psels = <NRF_PSEL(SPIM_SCK, 1, 15)>,
    					<NRF_PSEL(SPIM_MOSI, 1, 13)>,
    					<NRF_PSEL(SPIM_MISO, 1, 14)>;
    		};
    	};
    
    	spi_master1_sleep: spi_master1_sleep {
    		group1 {
    			psels = <NRF_PSEL(SPIM_SCK, 1, 15)>,
    					<NRF_PSEL(SPIM_MOSI, 1, 13)>,
    					<NRF_PSEL(SPIM_MISO, 1, 14)>;
    			low-power-enable;
    		};
    	};
    	
    	spi_master0_default: spi_master0_default {
    		group1 {
    			psels = <NRF_PSEL(SPIM_SCK, 0, 8)>,
    					<NRF_PSEL(SPIM_MOSI, 0, 9)>,
    					<NRF_PSEL(SPIM_MISO, 0, 10)>;
    		};
    	};
    
    	spi_master0_sleep: spi_master0_sleep {
    		group1 {
    			psels = <NRF_PSEL(SPIM_SCK, 0, 8)>,
    					<NRF_PSEL(SPIM_MOSI, 0, 9)>,
    					<NRF_PSEL(SPIM_MISO, 0, 10)>;
    			low-power-enable;
    		};
    	};
    	
    };
    
    &gpio0 {
    	status = "okay";
    };
    
    &gpio1 {
    	status = "okay";
    };
    
    &spi4 {
    	compatible = "nordic,nrf-spim";
    	status = "okay";
    	pinctrl-0 = <&spi_master1_default>;
    	pinctrl-1 = <&spi_master1_sleep>;
    	pinctrl-names = "default", "sleep";
    	/*cs-gpios = <&gpio1 12 GPIO_ACTIVE_LOW>, 	
    			   <&gpio1 10 GPIO_ACTIVE_LOW>;	
    			   
    	reg_spi_master1: spi-dev-a@0 {
    		reg = <0>;
    		compatible = "spi-device";
    	};
    	
    	reg_spi_master2: spi-dev-a@1 {
    		reg = <1>;
    		compatible = "spi-device";
    	};*/
    };
    
    
    &spi0{
    	compatible = "nordic,nrf-spim";
    	status = "okay";
    	pinctrl-0 = <&spi0_default>;
    	pinctrl-1 = <&spi0_sleep>;
    	pinctrl-names = "default", "sleep";
    	
    	/*cs-gpios = <&gpio0 11 GPIO_ACTIVE_LOW>, 	
    			   <&gpio0 19 GPIO_ACTIVE_LOW>;		
    			   
    	reg_spi0_master1: spi-dev-a@0 {
    		reg = <0>;
    	};
    	
    	reg_spi0_master2: spi-dev-b@1 {
    		reg = <1>;
    	};*/
    
    };
    
    When configuring like above in overlay
    In main 
    
    #define SPI1_MASTER                         DT_NODELABEL(spi4)
    #define SPI0_MASTER                         DT_NODELABEL(spi0)
    #define SPI0_MASTER_CS_GPIO                 DT_NODELABEL(gpio0)
    #define SPI1_MASTER_CS_GPIO                 DT_NODELABEL(gpio1)
    
    
    const struct device *spi_dev;
    const struct device *gpio_dev;
    
    
    struct spi_config spi_cfg={
    .operation = SPI_WORD_SET(8) | SPI_TRANSFER_MSB | SPI_MODE_CPOL | SPI_MODE_CPHA,
    .frequency = 4000000,
    .slave = 0,
    };
    
    
    void Init(uint8_t slave)
    {
            spi_dev = DEVICE_DT_GET(SPI0_MASTER);
            gpio_dev = DEVICE_DT_GET(SPI0_MASTER_CS_GPIO);
    
            spi_cfg.cs.gpio.port=gpio_dev;
            spi_cfg.cs.gpio.pin = 11;
            spi_cfg.cs.gpio.dt_flags = GPIO_ACTIVE_HIGH;
    
    }
    
    Giving error only for SPI0_MASTER. when all are SPI1_MASTER, it is building
    
    error: '__device_dts_ord_105' undeclared (first use in this function); did you mean '__device_dts_ord_15'?
       92 | #define DEVICE_NAME_GET(dev_id) _CONCAT(__device_, dev_id)
          |                                         ^~~~~~~~~
    C:/ncs/v2.8.0/zephyr/include/zephyr/toolchain/common.h:137:26: note: in definition of macro '_DO_CONCAT'
      137 | #define _DO_CONCAT(x, y) x ## y
          |                          ^
    C:/ncs/v2.8.0/zephyr/include/zephyr/device.h:92:33: note: in expansion of macro '_CONCAT'
       92 | #define DEVICE_NAME_GET(dev_id) _CONCAT(__device_, dev_id)
          |                                 ^~~~~~~
    C:/ncs/v2.8.0/zephyr/include/zephyr/device.h:229:37: note: in expansion of macro 'DEVICE_NAME_GET'
      229 | #define DEVICE_DT_NAME_GET(node_id) DEVICE_NAME_GET(Z_DEVICE_DT_DEV_ID(node_id))
          |                                     ^~~~~~~~~~~~~~~
    C:/ncs/v2.8.0/zephyr/include/zephyr/device.h:246:34: note: in expansion of macro 'DEVICE_DT_NAME_GET'
      246 | #define DEVICE_DT_GET(node_id) (&DEVICE_DT_NAME_GET(node_id))
          |                                  ^~~~~~~~~~~~~~~~~~
    C:/Users/nrf_Projects/ad_spi/src/main.c:40:19: note: in expansion of macro 'DEVICE_DT_GET'
       40 |         spi_dev = DEVICE_DT_GET(SPI0_MASTER);
          |                   ^~~~~~~~~~~~~
    C:/ncs/v2.8.0/zephyr/include/zephyr/device.h:92:41: note: each undeclared identifier is reported only once for each function it appears in
       92 | #define DEVICE_NAME_GET(dev_id) _CONCAT(__device_, dev_id)
          |                                         ^~~~~~~~~
    C:/ncs/v2.8.0/zephyr/include/zephyr/toolchain/common.h:137:26: note: in definition of macro '_DO_CONCAT'
      137 | #define _DO_CONCAT(x, y) x ## y
          |                          ^
    C:/ncs/v2.8.0/zephyr/include/zephyr/device.h:92:33: note: in expansion of macro '_CONCAT'
       92 | #define DEVICE_NAME_GET(dev_id) _CONCAT(__device_, dev_id)
          |                                 ^~~~~~~
    C:/ncs/v2.8.0/zephyr/include/zephyr/device.h:229:37: note: in expansion of macro 'DEVICE_NAME_GET'
      229 | #define DEVICE_DT_NAME_GET(node_id) DEVICE_NAME_GET(Z_DEVICE_DT_DEV_ID(node_id))
          |                                     ^~~~~~~~~~~~~~~
    C:/ncs/v2.8.0/zephyr/include/zephyr/device.h:246:34: note: in expansion of macro 'DEVICE_DT_NAME_GET'
      246 | #define DEVICE_DT_GET(node_id) (&DEVICE_DT_NAME_GET(node_id))
          |                                  ^~~~~~~~~~~~~~~~~~
    C:/Users/nrf_Projects/ad_spi/src/main.c:40:19: note: in expansion of macro 'DEVICE_DT_GET'
       40 |         spi_dev = DEVICE_DT_GET(SPI0_MASTER);
    
    
    /*
    In overlay file
    spi1_master:&spi4 {
    	compatible = "nordic,nrf-spim";
    	status = "okay";
    	pinctrl-0 = <&spi_master1_default>;
    	pinctrl-1 = <&spi_master1_sleep>;
    	pinctrl-names = "default", "sleep";
    	cs-gpios = <&gpio1 12 GPIO_ACTIVE_LOW>, 	
    			   <&gpio1 10 GPIO_ACTIVE_LOW>;	
    			   
    	reg_spi_master1: spi-dev-a@0 {
    		reg = <0>;
    		compatible = "spi-device";
    	};
    	
    	reg_spi_master2: spi-dev-a@1 {
    		reg = <1>;
    		compatible = "spi-device";
    	};
    };
    In main 
    #define SPI1_MASTER                         DT_NODELABEL(spi1_master)
    spi_dev = DEVICE_DT_GET(SPI1_MASTER);
    
    
    Giving below error
    error: '__device_dts_ord_DT_N_NODELABEL_spi1_master_ORD' undeclared (first use in this function)
       92 | #define DEVICE_NAME_GET(dev_id) _CONCAT(__device_, dev_id)
          |                                         ^~~~~~~~~
    C:/ncs/v2.8.0/zephyr/include/zephyr/toolchain/common.h:137:26: note: in definition of macro '_DO_CONCAT'
      137 | #define _DO_CONCAT(x, y) x ## y
          |                          ^
    C:/ncs/v2.8.0/zephyr/include/zephyr/device.h:92:33: note: in expansion of macro '_CONCAT'
       92 | #define DEVICE_NAME_GET(dev_id) _CONCAT(__device_, dev_id)
          |                                 ^~~~~~~
    C:/ncs/v2.8.0/zephyr/include/zephyr/device.h:229:37: note: in expansion of macro 'DEVICE_NAME_GET'
      229 | #define DEVICE_DT_NAME_GET(node_id) DEVICE_NAME_GET(Z_DEVICE_DT_DEV_ID(node_id))
          |                                     ^~~~~~~~~~~~~~~
    C:/ncs/v2.8.0/zephyr/include/zephyr/device.h:246:34: note: in expansion of macro 'DEVICE_DT_NAME_GET'
      246 | #define DEVICE_DT_GET(node_id) (&DEVICE_DT_NAME_GET(node_id))
          |                                  ^~~~~~~~~~~~~~~~~~
    C:/Users/nrf_Projects/spi/src/main.c:40:19: note: in expansion of macro 'DEVICE_DT_GET'
       40 |         spi_dev = DEVICE_DT_GET(SPI1_MASTER);
          |                   ^~~~~~~~~~~~~
    C:/ncs/v2.8.0/zephyr/include/zephyr/device.h:92:41: note: each undeclared identifier is reported only once for each function it appears in
       92 | #define DEVICE_NAME_GET(dev_id) _CONCAT(__device_, dev_id)
          |                                         ^~~~~~~~~
    C:/ncs/v2.8.0/zephyr/include/zephyr/toolchain/common.h:137:26: note: in definition of macro '_DO_CONCAT'
      137 | #define _DO_CONCAT(x, y) x ## y
          |                          ^
    C:/ncs/v2.8.0/zephyr/include/zephyr/device.h:92:33: note: in expansion of macro '_CONCAT'
       92 | #define DEVICE_NAME_GET(dev_id) _CONCAT(__device_, dev_id)
          |                                 ^~~~~~~
    C:/ncs/v2.8.0/zephyr/include/zephyr/device.h:229:37: note: in expansion of macro 'DEVICE_NAME_GET'
      229 | #define DEVICE_DT_NAME_GET(node_id) DEVICE_NAME_GET(Z_DEVICE_DT_DEV_ID(node_id))
          |                                     ^~~~~~~~~~~~~~~
    C:/ncs/v2.8.0/zephyr/include/zephyr/device.h:246:34: note: in expansion of macro 'DEVICE_DT_NAME_GET'
      246 | #define DEVICE_DT_GET(node_id) (&DEVICE_DT_NAME_GET(node_id))
          |                                  ^~~~~~~~~~~~~~~~~~
    C:/Users/nrf_Projects/spi/src/main.c:40:19: note: in expansion of macro 'DEVICE_DT_GET'
       40 |         spi_dev = DEVICE_DT_GET(SPI1_MASTER);
          |                   ^~~~~~~~~~~~~
    */
    
    

    Can you once check above attached file??

    Is there any zephyr spi sample example for nrf5340 for multiple slaves?

    Thankyou

  • I must admit I'm baffled. I can't see anything but there being something faulty with the installation or that something has been modified in the SDK

    1. Could you try with a different work station
    2. Could you uninstall NCS and all tools completely and reinstall them per https://docs.nordicsemi.com/bundle/ncs-latest/page/nrf/installation/install_ncs.html

    I've tried building with multiple different work stations myself, as well as different OS and none give the faults you're seeing. Since you were able to build the adc sample out of tree, I am expecting you to be able to build the zip I sent you with the exact same steps as you successfully built the adc sample with.

    Kind regards,
    Andreas

  • Hi Andreas,

    1. Tried with a different pc where ncs2.6.1 and toolchain 2.7 were installed, it is building.

    2.Reinstalled ncs  and all tools again.

       ncs 2.9

      tool chain 2.9. Able to build the sample.

    But in both the cases it is giving error related to overlay file

    And also, do we have to add different overlay for application and network core?

    When building the same sample for configuration nrf5340dk/nrf5340/cpunet, it is giving errors

    Thank you

  • KTH said:

    1. Tried with a different pc where ncs2.6.1 and toolchain 2.7 were installed, it is building.

    2.Reinstalled ncs  and all tools again.

       ncs 2.9

      tool chain 2.9. Able to build the sample.

    Great! Glad to hear that

    KTH said:
    But in both the cases it is giving error related to overlay file

    To be frank, the Problem-tab is quite misleading in some cases and might through warnings and red-underline some issues that is not there at all. But if I would guess if the error is a real one, I would guess that it is because some naming has changed between v2.5.1 (where the sample was built) and v2.6.1. On second thought, it might actually be due to hardware model changes https://docs.nordicsemi.com/bundle/ncs-latest/page/nrf/releases_and_maturity/migration/migration_hwmv2.html 

    KTH said:
    And also, do we have to add different overlay for application and network core?

    For the 5340 you only need to have a board file for the cpuapp and the non-secure version of cpuapp, i.e cpuapp/ns unless you have custom netcore firmware with custom board changes. As it is by default: The build system will add the companion components it needs to add for the netcore if you add the relevant configurations to prj.conf or sysbuild.conf and sysbuild/<your_companion_image>.conf and .overlay. See https://academy.nordicsemi.com/courses/nrf-connect-sdk-fundamentals/lessons/lesson-3-elements-of-an-nrf-connect-sdk-application/ for a great explanation of the components in an NCS application and https://academy.nordicsemi.com/courses/nrf-connect-sdk-intermediate/lessons/lesson-8-sysbuild/topic/sysbuild-explained/ for the sysbuild expandation

    KTH said:
    When building the same sample for configuration nrf5340dk/nrf5340/cpunet, it is giving errors

    This is roughly the same errors that you got previously, where the build fault log states that it can't find the items in main.c that is specified in <your_build_target>.overlay, since there are no nrf5340dk/nrf5340/cpunet overlay files that states that the various SPIM/S instances should be on the board. 

    Let me know if this answers your questions! 

    Kind regards,
    Andreas

  • Hi, we do have our custom board where we want to read sensor data using spi and send it through bt. 

    Able to test bt.

    prj.conf:

    CONFIG_BT=y
    CONFIG_BT_PERIPHERAL=y
    CONFIG_BT_DEVICE_NAME="nRF5340 DKK BLE"
    CONFIG_BT_GATT_CLIENT=y

    But when integrating bt in the sample where spi is working, it is building for cpuapp but not for cpunet. It is giving errors like i mentioned before.

    1. Can you explain how to get "custom netcore firmware with custom board changes"?

    2. For our requirement do we need to add overlay for cpunet also?

    Thank you

  • Hi,

    KTH said:
    1. Can you explain how to get "custom netcore firmware with custom board changes"?

    I can't quite see why this is relevant, since you can always set the SPI up on the appcore (which is recommended) and simply send the data using BLE, for instance NUS over BLE.

    If I  understand you're issue correct, you're now struggling with adding BLE to the SPI applicaiton. In NCS v2.9.0 we're using "sysbuild" and "companion images" instead og child/parent-images which was previously used.

    What you need to do to get BLE is the same as is done in the BLE samples for nrf5340, where you build the hci_ipc companion component for the netcore through sysbuild. https://academy.nordicsemi.com/courses/bluetooth-low-energy-fundamentals/ should explain it, but in case it does not you can see the sysbuild images here:https://docs.nordicsemi.com/bundle/ncs-latest/page/nrf/app_dev/config_and_build/sysbuild/sysbuild_images.html 

    Enabling SB_CONFIG_NETCORE_HCI_IPC in sysbuild.conf adds the netcore image to the application.

    I strongly recommend you go through https://academy.nordicsemi.com/courses/nrf-connect-sdk-intermediate/lessons/lesson-8-sysbuild/topic/sysbuild-explained/ 

    KTH said:
    2. For our requirement do we need to add overlay for cpunet also?

    I believe the answer to this is within my answer to 1 as well.

    Apologies for not supplying you with a simple sample, but I would recommend you to go through any BLE sample in the SDK that is built for the nrf5340 and copy the settings and setup to your application.

    This will be a multi-image build, and you don't have to build for the netcore itself. By enabling the correct components (such as SB_CONFIG_NETCORE_HCI_IPC), the build system will drag in the required images to generate the hex that will flash both the appcore and netcore.

    Kind regards,
    Andreas

Reply Children
  • Hi Andreas,

    Thank you for your guidance in resolving bt issue, flashed hci_ipc sample to network core and our application to application core. Able to transfer data.

    Now we have our custom board where AD5940 is connected to nrf5340 through spi. Not able to read chip id of ad5940. Tried with the example above.

    Taken reference from Exercise 1 – Interfacing with a sensor over SPI and sample from github mentioned in the exercise, spi_example. Modified spi pins in overlay.

    Using ad5940library.

    /*
     * Copyright (c) 2024 Nordic Semiconductor ASA
     *
     * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
     */
    
    #include <zephyr/kernel.h>
    #include <zephyr/logging/log.h>
    
    /* STEP 1.2 - Include the header files for SPI, GPIO and devicetree */
    #include <zephyr/device.h>
    #include <zephyr/devicetree.h>
    #include <zephyr/drivers/gpio.h>
    #include <zephyr/drivers/spi.h>
    
    #include "ad5940.h"
    
    LOG_MODULE_REGISTER(Lesson5_Exercise1, LOG_LEVEL_INF);
    #define DELAY_VALUES 1000
    
    const struct gpio_dt_spec ledspec = GPIO_DT_SPEC_GET(DT_NODELABEL(led0), gpios);
    
    /* STEP 3 - Retrieve the API-device structure */
    // #define SPIOP	SPI_WORD_SET(8) | SPI_TRANSFER_MSB
    // struct spi_dt_spec spispec = SPI_DT_SPEC_GET(DT_NODELABEL(bme280), SPIOP, 0);
    
    const struct device *spi_dev;
    const struct device *gpio_dev= DEVICE_DT_GET(DT_NODELABEL(gpio0));
    
    struct spi_config spi_cfg = {
    	.operation = SPI_WORD_SET(8) | SPI_TRANSFER_MSB | SPI_MODE_CPOL | SPI_MODE_CPHA,
    	.frequency = 1000000,
    	.slave = 0,
    };
    
    int main(void)
    {
    	int err;
    
    	err = gpio_is_ready_dt(&ledspec);
    	if (!err)
    	{
    		LOG_ERR("Error: GPIO device is not ready, err: %d", err);
    		return 0;
    	}
    
    	spi_dev = DEVICE_DT_GET(DT_NODELABEL(spi1));
    	/* STEP 10.1 - Check if SPI and GPIO devices are ready */
    	// err = spi_is_ready_dt(&spispec);
    	// err = spi_is_ready(spi_dev);
    	// if (!err) {
    	if (spi_dev == NULL)
    	{
    		LOG_ERR("Error: SPI device is not ready, err: %d", err);
    		return 0;
    	}
    	// printk("SPI : %s",spispec.bus->name);
    	printk("SPI : %s", spi_dev->name);
    
    	gpio_pin_configure_dt(&ledspec, GPIO_OUTPUT_ACTIVE);
    	gpio_pin_configure(gpio_dev,11,GPIO_OUTPUT_INACTIVE);
    
    	// LOG_INF("Continuously read sensor samples, compensate, and display");
    
    	while (1)
    	{
    		/* STEP 10.4 - Continuously read sensor samples and toggle led */
    		AD5940_Initialize();
    		gpio_pin_toggle_dt(&ledspec);
    		k_msleep(DELAY_VALUES);
    	}
    
    	return 0;
    }
    
    void AD5940_ReadWriteNBytes(unsigned char *pSendBuffer, unsigned char *pRecvBuff, unsigned long length)
    {
    	int err;
    	struct spi_buf tx_spi_buf = {.buf = (void *)&pSendBuffer, .len = length};
    	struct spi_buf_set tx_spi_buf_set = {.buffers = &tx_spi_buf, .count = 1};
    	struct spi_buf rx_spi_bufs = {.buf = pRecvBuff, .len = length};
    	struct spi_buf_set rx_spi_buf_set = {.buffers = &rx_spi_bufs, .count = 1};
    
    	/* STEP 4.2 - Call the transceive function */
    	// err = spi_transceive_dt(&spispec, &tx_spi_buf_set, &rx_spi_buf_set);
    	err = spi_transceive(spi_dev, &spi_cfg, &tx_spi_buf_set, &rx_spi_buf_set);
    	if (err < 0)
    	{
    		LOG_ERR("spi_transceive_dt() failed, err: %d", err);
    	}
    }
    
    void AD5940_CsClr(void)
    {
    	// gpio_pin_set_dt(spispec.config.cs.gpio.pin,1);
    	gpio_pin_set(gpio_dev,11,1);
    }
    
    void AD5940_CsSet(void)
    {
    	// gpio_pin_set_dt(spispec.config.cs.gpio.pin,0);
    	gpio_pin_set(gpio_dev,11,0);
    }
    
    void AD5940_RstSet(void)
    {
    }
    
    void AD5940_RstClr(void)
    {
    }
    
    void AD5940_Delay10us(uint32_t time)
    {
    	if (time > 0)
    	{
    		k_usleep(time * 10); // Delay in microseconds
    	}
    }

    When we are tyring to read chipid, getting output as below

    *** Booting nRF Connect SDK v2.9.0-7787b2649840 ***
    *** Using Zephyr OS v3.7.99-1f8f3dc29142 ***
    SPI : spi@9000
    00000000

Related