This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

NCS zephyr SPI on nRF52832

Hello, I've been trying to launch zephyr SPI example, but I'm having issues with SPI sending anything. The code itself does not throw any errors, and it seems like my board does execute spi_transceive function, but nothing on osciloscope. I'm not worried about CS at the moment, I just want to see any activity from SPI on Osciloscope. The code is fairly simple:

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

#define SLEEP_TIME_MS   	100

#define RECEIVE_BUFF_SIZE 	10
#define RECEIVE_TIMEOUT 	100

/* The devicetree node identifier for the "led0" alias. */
#define LED0_NODE DT_ALIAS(led0)
#define LED0	DT_GPIO_LABEL(LED0_NODE, gpios)
#define PIN	DT_GPIO_PIN(LED0_NODE, gpios)
#define FLAGS	DT_GPIO_FLAGS(LED0_NODE, gpios)

struct device * spi_dev;

static void spi_init(void)
{
	const char* const spiName = "SPI_0";
	spi_dev = device_get_binding(spiName);
}

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

void spi_test_send(const struct device *dev)
{
	int err;
	static uint8_t tx_buffer[100] = "hello";
	static uint8_t rx_buffer[100];
	gpio_pin_toggle(dev,PIN);
	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
	};

	spi_transceive(spi_dev, &spi_cfg, &tx, &rx);
	//tx_buffer[0]++;	
}

void main(void)
{
	const struct device *dev;
	dev = device_get_binding(LED0);
	gpio_pin_configure(dev, PIN, GPIO_OUTPUT_ACTIVE | FLAGS);
	spi_init();

	while (1) {
		spi_test_send(dev);
		k_msleep(SLEEP_TIME_MS);
	}
}

config file:

CONFIG_GPIO=y
CONFIG_SPI=y

dts file:

&spi0 {
	compatible = "nordic,nrf-spis";
	/* Cannot be used together with i2c0. */
	status = "okay";
	sck-pin = <27>;
	mosi-pin = <26>;
	miso-pin = <28>;
	csn-pin = <31>;
	def-char = <0x00>;
};

Could someone glance over the code and tell me what am I doing wrong? Maybe it's a simple overlook from my side.

Parents
  • Hi Marius,

    It would be helpful if you would include what version of NCS is being used. The use of CONFIG_SPI, suggests you may not be using the latest version.

    The implementation is that of an SPI Slave. Therefore, an SPI Clock must be present before data will be clocked out of the slave. 

    Unless you are creating your own dts file for customer hardware, using an overlay is recommended over editing a dts file. 

    The SPI is the peripheral used in the blog Adding a Peripheral to an NCS Zephyr project. It's a bit dated, but it should help resolve any potential binding issues.

    Cheers,

    Jeff

Reply
  • Hi Marius,

    It would be helpful if you would include what version of NCS is being used. The use of CONFIG_SPI, suggests you may not be using the latest version.

    The implementation is that of an SPI Slave. Therefore, an SPI Clock must be present before data will be clocked out of the slave. 

    Unless you are creating your own dts file for customer hardware, using an overlay is recommended over editing a dts file. 

    The SPI is the peripheral used in the blog Adding a Peripheral to an NCS Zephyr project. It's a bit dated, but it should help resolve any potential binding issues.

    Cheers,

    Jeff

Children
  • I am using nRF Connect version 1.9.1. I honestly thought that what I was using is the latest thing. I guess now the question is what is the latest thing? Because I took this line "CONFIG_SPI" from sample code provided in the NCS sample which is called hci_spi.

  • Hi Marius,

    Sorry I had confused CONFIG_SPI with a different SPI macro that has been modified earlier. CONFIG_SPI is the correct macro for NCS v1.9.1.

    I had started to draft a response to the post referencing the hci_spi project. Since that reference has been removed,  I assume that you now understand the hci_spi is used for communicating HCI API between the BLE Host and BLE controller over an SPI transport.

    An SPIS device can not transmit data without the outside world providing an SPI Clock. This is typically provided by a SPIM device.

    You could use the hci_spi example to piece an SPI example together. However, the Adding a Peripheral to an NCS Zephyr project blog is a complete SPIS and SPIM example. For NCS 1.9.X, use the NCS 1.7 updates and move the nf52dk_nrf52840.overlay to the project's root directory.

    Cheers

Related