SPI transceiving error and cs pin configuration

Dear ,

I am using nrf52833dk configured as nrf52820 using NCS1.7.0 SDK. I am experiencing some difficulties in regard to the SPI.

I have configured SPI as followed, but still received spi error 140 and 251. Where can I check the meaning of the error codes 140 and 251? Do I need to do other initialisation apart from below? This error message remains unchanged after I connect MOSI and MISO pin directly, i,e, loopback setup.

static const struct spi_cs_control spi_cs = {
	.gpio_dev = DEVICE_DT_GET(DT_NODELABEL(gpio0)),
	.gpio_pin = 14,
	.gpio_dt_flags = GPIO_ACTIVE_LOW,
	.delay = 0,
};
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,
	.cs = &spi_cs,
};

I also checked similar topics across other posts and found out that the cs pin was not initialised explicitly in the spi loopback example. However, it works fine without problems. Are there any reasons we don't need to initialise in this case?

#include <zephyr.h>
#include <sys/printk.h>
#include <drivers/spi.h>

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,
};

struct device * spi_dev;

static void spi_init(void)
{
	const char* const spiName = "SPI_0";
	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];

	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)
{
	printk("SPIM Example\n");
	spi_init();

	while (1) {
		spi_test_send();
		k_sleep(K_MSEC(1000));
	}
}

Thanks and kind regards, Kevin

Related