This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Reading JEDEC ID with SPI on nrf9160

Hello,

I have an external flash w25n01gv, I am using this simple spi loopback example to test the spi functionality of nrf9160. I want to read the jedec id of my device and my code is like:

/*
 * 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_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 u8_t tx_buffer[2] = {
	0X9F,
	0x00
  };
  static u8_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 = 13;
  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 < 2; 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(".");
  }
}

The dataset of my externaş flash says:

so I am expecting to get Ef,AA,2f however what I am receiving is 00 00 ef. I also connected the output pins to logic analyzer but the clk signal and miso also doesn't seems right. What am I doing wrong in the code can you provide me some feedback about it. In the original code before I just do simple modifications I was observing the loopback properly. I was receiving what I was sending so the code was working properly. 

To adjust the chip select I am only using this code:

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 = 13;
spi_cfg.cs = & cs_control;

does that code changes cs from 1 to 0 automatically? I am doing something wrong in that part?

My overlay file is :

&spi3 {
    compatible = "nordic,nrf-spim";
    status = "okay";
    sck-pin = <11>;
    mosi-pin = <10>;
    miso-pin = <12>;
};

My proj.config is:

# CONFIG_BOARD_ENABLE_CPUNET=n
CONFIG_SPI=y
CONFIG_MAIN_STACK_SIZE=4096
# CONFIG_SPI_0=y
# UART, SPIM, and I2C are shared peripherals on nrf9160
# therefore we choose SPI3
CONFIG_SPI_3=y
CONFIG_SPI_NRFX=y
CONFIG_DEPRECATED_ZEPHYR_INT_TYPES=y

#addition for combining blinky
CONFIG_GPIO=y

Thank you for the help.

Related