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

nrF52840_dk SPI configuration

Hello, I'm trying to set up SPI communication with a sensor from my nrf5284_pca10056 running zephyr.

This is my prj.conf file:

CONFIG_SPI=y
CONFIG_SPI_1=y
CONFIG_SPI_1_NRF_SPI=y
CONFIG_SPI_1=y
CONFIG_SPI_NRFX=y

This is my .overlay file:

&spi1 {
	compatible = "nordic,nrf-spi";
	status = "okay";
	sck-pin = <16>;
	mosi-pin = <18>;
	miso-pin = <20>;
	cs-gpios = <&gpio0 17 0>;

};

And this is the code I'm trying to run:

#include <zephyr.h>
#include <misc/printk.h>
#include <spi.h>
#include <errno.h>

struct device * spi_dev;

struct spi_cs_control spi_cs;

struct spi_config spi_cfg = {
	.operation = SPI_WORD_SET(8) | SPI_TRANSFER_MSB |
		     SPI_MODE_CPOL | SPI_MODE_CPHA | SPI_OP_MODE_MASTER,
	.frequency = 1000000,
	.slave = 0,
};

static void spi_init(void)
{

	spi_cs.gpio_dev = device_get_binding(DT_NORDIC_NRF_SPI_SPI_1_CS_GPIOS_CONTROLLER);
	spi_cs.gpio_pin = DT_NORDIC_NRF_SPI_SPI_1_CS_GPIOS_PIN;
	spi_cs.delay = 0;

	spi_cfg.cs = &spi_cs;
	spi_dev = device_get_binding("SPI_1");

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

void spi_test_send(void)
{
	int err;
	static u8_t tx_buffer[1];
	static u8_t rx_buffer[1];
	tx_buffer[0] = 0x0F;
	
	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
	};
	err = spi_transceive(spi_dev, &spi_cfg, &tx, &rx);
	if (err) {
		printk("SPI error: %d\n", err);
	} else {
		/* Connect MISO to MOSI for loopback */
		printk("TX sent: %x\n", tx_buffer[0]);
		printk("RX recv: %x\n", rx_buffer[0]);
		//tx_buffer[0]++;
	}	
}

void main(void)
{
	spi_init();
	while (1) {
		spi_test_send();
		k_sleep(1000);
	}
}

The code compiles and runs fine. The problem I'm facing is that when i hook up my logic analyzer it shows no activity except for the SCK/clock line. The clock line starts every time spi_transcieve() is run but the other lines(MISO, MOSI and CS) shows no activity.

Here is a cut from nrf52840_pca10056.dts_compiled found in the build folder:

		spi1: spi@40004000 {
			#address-cells = < 0x01 >;
			#size-cells = < 0x00 >;
			reg = < 0x40004000 0x1000 >;
			interrupts = < 0x04 0x01 >;
			status = "okay";
			label = "SPI_1";
			compatible = "nordic,nrf-spi";
			sck-pin = < 0x10 >;
			mosi-pin = < 0x12 >;
			miso-pin = < 0x14 >;
			cs-gpios = < 0x02 0x11 0x00 >;
		};

I have connected the logic analyzer in the following setup:

P0.16 - SCK
P0.17 - CS
P0.18 - MOSI
P0.20 - MISO

I suspect there is an issue with the way I configured the spi_cs_control struct, but I'm not sure what is wrong.

If anyone has ideas on how to fix this issue I would greatly appreciate it.

Many thanks // Love Berg

Related