nrf52dk not able to build the SPI project.

Hello,

We are using nrf52dk, with zephyr OS also we have done the setup in VS code with extension.

Toolchain version: v2.9.0

SDK version: 3.7.99-ncs2, these all came in extension of nrf connect in vs code.

Expectation: We need to communicate the sensor over SPI1 with MAX30001 device.

Problem: I am not able to build the project. getting error of SPI node.

Build error logs:

//
FATAL ERROR: command exited with status 1: 'C:\ncs\toolchains\b620d30767\opt\bin\cmake.EXE' --build C:/nrf52/my_sample/sample_SPI/build

Can anyone please help me on this. As I am new on this. Please guide us how to solve this issue.

  • I have attached the logs

    FAILED: CMakeFiles/app.dir/src/main.c.obj 
    C:/nrf52/my_sample/sample_SPI/build/sample_SPI/zephyr/include/generated/zephyr/devicetree_generated.h:12836:42: error: 'DT_N_S_soc_S_spi_40004000_S_spi_dev_a_0_P_frame_format' undeclared here (not in a function); did you mean 'DT_N_S_soc_S_spi_40004000_S_spi_dev_a_0_P_reg'?
    12836 | #define DT_N_NODELABEL_reg_my_spi_master DT_N_S_soc_S_spi_40004000_S_spi_dev_a_0
          |                                          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    C:/ncs/v2.9.0/zephyr/include/zephyr/devicetree.h:4879:29: note: in definition of macro 'DT_CAT3'
     4879 | #define DT_CAT3(a1, a2, a3) a1 ## a2 ## a3
          |                             ^~
    C:/ncs/v2.9.0/zephyr/include/zephyr/drivers/spi.h:352:25: note: in expansion of macro 'DT_PROP'
      352 |                         DT_PROP(node_id, frame_format) |                        \
          |                         ^~~~~~~
    C:/ncs/v2.9.0/zephyr/include/zephyr/drivers/spi.h:404:27: note: in expansion of macro 'SPI_CONFIG_DT'
      404 |                 .config = SPI_CONFIG_DT(node_id, operation_, delay_) \
          |                           ^~~~~~~~~~~~~
    C:/nrf52/my_sample/sample_SPI/src/main.c:10:45: note: in expansion of macro 'SPI_DT_SPEC_GET'
       10 | static const struct spi_dt_spec spi1_spec = SPI_DT_SPEC_GET(
          |                                             ^~~~~~~~~~~~~~~
    C:/ncs/v2.9.0/zephyr/include/zephyr/devicetree.h:4877:24: note: in expansion of macro 'DT_N_NODELABEL_reg_my_spi_master'
     4877 | #define DT_CAT(a1, a2) a1 ## a2
          |                        ^~
    C:/ncs/v2.9.0/zephyr/include/zephyr/devicetree.h:200:29: note: in expansion of macro 'DT_CAT'
      200 | #define DT_NODELABEL(label) DT_CAT(DT_N_NODELABEL_, label)
          |                             ^~~~~~
    C:/nrf52/my_sample/sample_SPI/src/main.c:11:5: note: in expansion of macro 'DT_NODELABEL'
       11 |     DT_NODELABEL(reg_my_spi_master),  // Device tree node label
          |     ^~~~~~~~~~~~
    [47/148] Building C object zephyr/arch/arch/arm/core/cortex_m/CMakeFiles/arch__arm__core__cortex_m.dir/scb.c.obj
    ninja: build stopped: subcommand failed.
    FAILED: _sysbuild/sysbuild/images/sample_SPI-prefix/src/sample_SPI-stamp/sample_SPI-build C:/nrf52/my_sample/sample_SPI/build/_sysbuild/sysbuild/images/sample_SPI-prefix/src/sample_SPI-stamp/sample_SPI-build 

  • #include <stdio.h>
    #include <zephyr/kernel.h>
    #include <zephyr/drivers/gpio.h>
    #include <zephyr/drivers/spi.h>
    
    static const struct spi_dt_spec spi1_spec = SPI_DT_SPEC_GET(
        DT_NODELABEL(reg_my_spi_master),  // Device tree node label
        SPI_WORD_SET(8),                  // 8-bit word size
        0                                 // CS release delay
    );
    
    static void readRegister()
    {
    	int err;
    	uint8_t chip_id[3];
    	uint8_t spiTxCommand[1];
    	spiTxCommand[0] = ((0x0F << 1) | 0x01);
    
    	const struct spi_buf tx_buf = {
    		.buf = &spiTxCommand,
    		.len = sizeof(spiTxCommand)
    	};
    
        const struct spi_buf_set tx = {
    		.buffers = &tx_buf,
    		.count = 1
    	};
    
        struct spi_buf rx_buf[2] = {
    		{
    			.buf = NULL,
    			.len = 1
    		},
    		{
    			.buf = chip_id,
    			.len = 3
    		}
    	}; // 24 bit register + 1 dummy byte
        
    	const struct spi_buf_set rx = {
    		.buffers = rx_buf,
    		.count = 2
    	};
    	
    	do
    	{
    		err = spi_transceive_dt(&spi1_spec, &tx, &rx);
    		if (err  < 0) { break; }
    
    	} while (false);
    
    	printk("MAX30001 ID: 0x%02X 0x%02X 0x%02X\n", (uint8_t)chip_id[0], (uint8_t)chip_id[1], (uint8_t)chip_id[2]);
    }
    
    
    int main(void)
    {
    	if (!spi_is_ready_dt(&spi1_spec)) {
    		printf("SPI1 DEV NOT READY\n");
    		return 0;
    	}
    
    	readRegister();
    
    	while (1) {
    		readRegister();
    		k_msleep(1000);
    	}
    	return 0;
    }

  • &spi1 {
    	compatible = "nordic,nrf-spi";
    	status = "okay";
    	pinctrl-0 = <&spi1_default>;
    	pinctrl-1 = <&spi1_sleep>;
    	pinctrl-names = "default", "sleep";
    	cs-gpios = <&gpio0 28 GPIO_ACTIVE_LOW>;
    	reg_my_spi_master: spi-dev-a@0 {
    		reg = <0>;
    		spi-max-frequency = <4000000>;  // Set frequency to 4 MHz
    	};
    };


    Overlay file

  • Hi,

    There are three alternatives as I see it:

    1. I see that zephyr has drivers for some different max chips. See sensor samples. While this is not the exact sensor you got, sometimes the interface is similar enough so that you can just use the same driver. Worth a try.

    2.  Learn how to use SPI in our DevAcademy lesson  on SPI.

    3. If you want to integrate the sensor more properly, but wich requires some more work: see DevAcademy Intermediate: Lesson 7 – Device driver model.

    Regards,
    Sigurd Hellesvik

Related