Issue to configure SPI on nRF5340

Hello,

I am experiencing issues with an nRF5340 board and its SPI.

I am trying to add an Pmod OLED (128x32) screen to my board, but firstly I am trying to set up the SPI communication.

I would like to make my code the more board agnostic possible so I am avoiding nRF libraries and focusing on Zephyr's ones.

I am using nRF SDK 2.0.0 with VS Code extensions.

I have activated the SPI in the prj.conf with : CONFIG_SPI=y

I have configure spi1 in the nrf5340dk_nrf5340_cpuapp.overlay like that:

&spi1 {
    compatible = "nordic,nrf-spi-common";
	status = "okay";
    
    sck-pin = < 0 >;
    miso-pin = < 1 >;
    mosi-pin = < 2 >;
    cs-gpios = < &gpio0 3 GPIO_ACTIVE_LOW >;
};

And my main.c looks like this:

/*
 * Copyright (c) 2012-2014 Wind River Systems, Inc.
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <device.h>
#include <drivers/spi.h>
#include <zephyr.h>

#define SPI		DT_NODELABEL(spi1)



void main(void)
{

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

	const char * pcLabelSpi = DT_LABEL(SPI);
	
	struct spi_dt_spec spi = {
		.bus = device_get_binding(pcLabelSpi),
		.config = spi_cfg,
	};
	if (spi.bus == NULL) {
		printk("Could not get spi device: %s\n", pcLabelSpi);
		return;
	}

	int err = 0;

	uint8_t iDataTx = 0x12;
	uint8_t * pDataTx = &iDataTx;

	
	const struct spi_buf_set tx = {
		.buffers = &(const struct spi_buf){
			.buf = pDataTx,
			.len = sizeof pDataTx,
		},
		.count = 1,
	};

	
	while (1)
	{
		if(spi_is_ready(&spi))
		{	
			printk("SPI transmitting...\n");

			err = spi_write_dt(&spi, &tx);
			if(err)
				printk("SPI transmit failed, error: %d\n", err);
		} 
		else 
			printk("SPI not ready: %s\n", pcLabelSpi);

		k_sleep(K_SECONDS(1));
	}
}

My troubles come from the device_get_biding() function which return me a NULL so my code go into the if and stop.

I think the function return me a NULL because of the configuration but I can't find what is the problem.

Also, I tried with spi2 (which is already configured but not as I want) and the get_binding function works. So it may confirms that the issue come from my configuration.

Thanks in advance for your help.

Regards,

Sulian

Related