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

  • Hi,

    Glad to hear you've managed to get past the BLE issues.

    I see you've commented out the error check to see if the device is ready (line 49). Do you check at any point if your device is set up? Could you add a check to see if you've enabled and initialized the device properly and that it is working? There are some lessons in the fundamentals course that should cover how to do this as well

    Kind regards,
    Andreas

  • Hi, 

    if (spi_dev == NULL)
    {
    LOG_ERR("Error: SPI device is not ready, err: %d", err);
    return 0;
    }

    This will check for whether the device is ready or not right?

  • Hi,

    DEVICE_DT_GET returns a pointer to the device object created for the instance, i.e a pointer to spi1 based on the DT_NODELABEL property of the device. See https://github.com/nrfconnect/sdk-zephyr/blob/bd1cf27b22f1003d58ecf4af81961722e1eb9949/include/zephyr/device.h#L254 or look the API up by going to definition within VS Code.

    The following

    	spi_dev = DEVICE_DT_GET(DT_NODELABEL(spi1));
    	if (spi_dev == NULL)
    	{
    		LOG_ERR("Error: SPI device is not ready, err: %d", err);
    		return 0;
    	}

    returns a pointer. And the ONLY time you enter the If-check is if said pointer is NULL. This means that you never check if the device is ready.

    See https://academy.nordicsemi.com/courses/nrf-connect-sdk-intermediate/lessons/lesson-5-serial-peripheral-interface-spi/ for more information about setting up and checking a SPI peripheral.

    Kind regards,
    Andreas

  • Hi,

    Now able to read AD5940 chip id.

    struct spi_cs_control spi_cs = {
    	.gpio = SPI_CS_GPIOS_DT_SPEC_GET(DT_NODELABEL(reg_my_spi_master)),
    	.delay = 0,
    };
    
    static void spi_init(void)
    {
    	/*get device address*/
    	spi_dev = DEVICE_DT_GET(DT_NODELABEL(spi1_master));
    
    	if (!device_is_ready(spi_dev))
    	{
    		printk("SPI master device not ready!\n");
    	}
    	if (!device_is_ready(spi_cs->gpio.port))
    	{
    		printk("SPI master chip select device not ready!");
    	}
    	printk("SPI : %s	CS_GPIO: %s		CS_PIN: %d\n", spi_dev->name, spi_cs->gpio.port->name, spi_cs->gpio.pin);
    	printk("SPI device ready\n");
    }

    Thank you

      
  • Glad to hear that! :) 

    Kind regards,
    Andreas

Reply Children
No Data
Related