Error while trying to config SPI in VS code .

HI i am working with the nrf 52833 dk board using  nrf connect and trying to implement SPI ,

#define SPI3_NODE DT_NODELABEL(SPI_3)
static const struct spi_dt_spec* dev_spi = SPI_DT_SPEC_GET(SPI3_NODE,SPI_OP_MODE_MASTER,1);

I am getting this error - identifier "__device_dts_ord_DT_N_NODELABEL_SPI_3_BUS_ORD" is undefined. 

I am new to ncs and zephyr , so what is causing this error?

Parents Reply Children
  • #include <zephyr.h>
    #include <sys/printk.h>
    #include <device.h>
    #include <drivers/spi.h>
    #include <hal/nrf_gpio.h>
    
    #define CS_PIN 16
    
    static const struct spi_config spi_cfg = {
    	.operation = SPI_WORD_SET(8) | SPI_TRANSFER_MSB |
    		     SPI_MODE_CPOL | SPI_MODE_CPHA,
    	.frequency = 4000000,
    	.slave = 0,
    };
    
    const struct device * spi_dev;
    
    static void spi_init(void)
    {
    	#define SPI_3 DT_NODELABEL(spi3)
    	const struct device *spi_dev = device_get_binding(DT_LABEL(SPI_3));
    }
    
    void spi_test_send(void)
    {
    	int err;
    	static uint8_t tx_buffer[1];
    	static uint8_t rx_buffer[1];
    
    	const struct spi_buf tx_buf = {
    		.buf = tx_buffer,
    		.len = sizeof(tx_buffer)
    	};
    	const struct spi_buf_set tx = {
    		.buffers = &tx_buf,
    		.count = 1
    	};
    
    	struct spi_buf rx_buf = {
    		.buf = rx_buffer,
    		.len = sizeof(rx_buffer),
    	};
    	const struct spi_buf_set rx = {
    		.buffers = &rx_buf,
    		.count = 1
    	};
    	nrf_gpio_cfg_output(CS_PIN);
    		// GPIO_SetBits(SPI_CS_MS_DEMO_PIN);
    		nrf_gpio_pin_set(CS_PIN);
    		// GPIO_ResetBits(SPI_CS_MS_DEMO_PIN);
    		nrf_gpio_pin_clear(CS_PIN);
    		tx_buffer[0]=0x9F;
    		// SPI_Send_Receive_Data(&a, &a1);
    		err = spi_transceive(spi_dev, &spi_cfg, &tx, &rx);
    	if (err) {
    		printk("SPI error: %d\n", err);
    	}else{
    		printk("TX sent: %x\n", tx_buffer[0]);
    		printk("RX recv: %x\n", rx_buffer[0]);
    	}
    
    		nrf_gpio_pin_set(CS_PIN);
    	
    }
    
    void main(void)
    {
    	printk("SPIM Example\n");
    	spi_init();
    
    	while (1) {
    		spi_test_send();
    		k_sleep(K_SECONDS(1));
    	}
    }

    this is the code i am working with , what changes do you suggest i make 

    I cannot establish a connection only with the spi device .

  • Hi,

    Have you checked that the SPI device is found? For example:

    if (spi_dev == NULL) {
    	printk("Could not get %s device\n", spiName);
    	return;
    }

    Best regards,

    Marte

  • yes i have managed to get that , 

    err = spi_transceive(spi_dev, &spi_cfg, &tx, &rx);
    	if (err) {
    		printk("SPI error: %d\n", err);
    		}

    im getting error value to be -22

  • Hi,

    I do not believe you get the SPI device successfully. You are creating a new spi_dev inside of spi_init(). Try the following instead:

    const struct device * spi_dev;
    
    static void spi_init(void)
    {
    	#define SPI_3 DT_NODELABEL(spi3)
    	spi_dev = device_get_binding(DT_LABEL(SPI_3));
    	if (spi_dev == NULL) {
    		printk("Could not get device\n");
    		return;
    	}
    }

    Then remove everything on lines 47 to 53, i.e. this part:

    nrf_gpio_cfg_output(CS_PIN);
    // GPIO_SetBits(SPI_CS_MS_DEMO_PIN);
    nrf_gpio_pin_set(CS_PIN);
    // GPIO_ResetBits(SPI_CS_MS_DEMO_PIN);
    nrf_gpio_pin_clear(CS_PIN);
    tx_buffer[0]=0x9F;
    // SPI_Send_Receive_Data(&a, &a1);

    And also remove nrf_gpio_pin_set(CS_PIN); on line 62.

    Best regards,

    Marte

  • HI yes i did all the changes and  have got it working , this is the code i am working with and winbond flash and my first step is to get the JEDEC ID, hence all the extra lines 

    /*
     * Copyright (c) 2012-2014 Wind River Systems, Inc.
     *
     * SPDX-License-Identifier: Apache-2.0
     */
    
    #include <zephyr.h>
    
    #include <sys/printk.h>
    
    #include <drivers/spi.h>
    
    //addition for combining blinky
    
    static struct spi_config spi_cfg = {
      .operation = SPI_OP_MODE_MASTER|SPI_WORD_SET(8) | SPI_TRANSFER_MSB |
      SPI_MODE_CPOL | SPI_MODE_CPHA,
      .frequency = 4000000,
      .slave = 0,
    };
    
    struct device * spi_dev;
    
    static void spi_init(void) {
        const char *
          const spiName = "SPI_3";
        spi_dev = device_get_binding(spiName);
    
        if (spi_dev == NULL) {
          printk("Could not get %s device\n", spiName);
          return;
        }
    }
    
    void spi_test_send(void) {
      int err;
      //static u8_t tx_buffer[1];
      //static u8_t rx_buffer[1];
      static uint8_t tx_buffer[2] = {
    	0X9F,
    	0x00,
    	0x00
      };
      static uint8_t rx_buffer[3];
    
      const struct spi_buf tx_buf = {
    	.buf = tx_buffer,
    	.len = sizeof(tx_buffer)
      };
      const struct spi_buf_set tx = {
    	.buffers = & tx_buf,
    	.count = 1
      };
    
      struct spi_buf rx_buf = {
    	.buf = rx_buffer,
    	.len = sizeof(rx_buffer),
      };
      const struct spi_buf_set rx = {
    	.buffers = & rx_buf,
    	.count = 1
      };
    
      struct device * gpio_dev;
      struct spi_cs_control cs_control;
    
      gpio_dev = device_get_binding("GPIO_0");
      if (!gpio_dev) {
    	printk("could not find GPIO device\n");
    	return;
      }
      cs_control.delay = 0;
      cs_control.gpio_dev = gpio_dev;
      cs_control.gpio_pin = 16;
      spi_cfg.cs = & cs_control;
    
      err = spi_transceive(spi_dev, & spi_cfg, & tx, & rx);
    
      if (err) {
    	printk("SPI error: %d\n", err);
      } else {
    	/* Connect MISO to MOSI for loopback */
    
    	for (int i = 0; i < 3; i++) {
    	  printk("TX transmit: %x\n", tx_buffer[i]);
    	}
    
    	for (int i = 0; i < 3; i++) {
    	  printk("RX recv: %x\n", rx_buffer[i]);
    	}
      }
    }
    
    void main(void) {
      printk("SPIM Example\n");
      spi_init();
      printk("After init\n");
      while (1) {
    	spi_test_send();
    	k_sleep(K_MSEC(1000));
    	printk(".");
      }
    }

    but I still am not able to get the JEDEC ID  , 

    thank you for all your help , I'm having a little trouble understanding it . 

Related