How to get the SPI working on the Nrf52840

Hello there!

I´m new in the nordic universe. I have a nrf52840 dk and i want to learn how it works. I succsesfully started with using the gpios and the i2c and now i want to use SPI.

But i wont get it running. I want to create a new spi device and want to read out some registers. I tried several exemples form the internet and read much in some forums but it didn´t help. I´m using SDK 2.2.0 and VS Code.

I created a new device in the device tree and added the SPI in the configuration file.

CONFIG_GPIO=y
CONFIG_SPI=y

my prj.conf

&spi1 {

	eeprom:test@0 {
		status = "okay";
		compatible = "bosch,bmi160"; 
		//compatible = "spi-device"; //spi-device did not work
        spi-max-frequency = <1000000>;
		reg = <0>;
	};

	cs-gpios = <&gpio1 12 (GPIO_ACTIVE_LOW | GPIO_PULL_UP)>;
};

and my overlay...
i needed to join one different compatible because it does not compile... Thats why i select the bmi160. How can i use the "spi-device"?

And here the main()
#include <zephyr/kernel.h>
#include <zephyr/sys/printk.h>
#include <zephyr/drivers/spi.h>
#include <zephyr/drivers/gpio.h>

#define SPI_NODE DT_NODELABEL(eeprom)
#define SPI_OP_MODE (SPI_OP_MODE_MASTER | SPI_WORD_SET(8) | SPI_LINES_SINGLE | SPI_TRANSFER_MSB )

static struct spi_dt_spec appSPI = SPI_DT_SPEC_GET(SPI_NODE, SPI_OP_MODE, 1);

void readRegister(uint8_t reg, uint8_t values[], uint8_t size)
{
	int err;

	uint8_t tx_buffer[1];
	tx_buffer[0] = reg;

	struct spi_buf tx_spi_bufs[] =
		{
			{.buf = tx_buffer,
			 .len = sizeof(tx_buffer)},
		};

	struct spi_buf_set spi_tx_buffer_set =
		{
			.buffers = tx_spi_bufs,
			.count = 1};

	struct spi_buf rx_spi_bufs[] =
		{
			{.buf = values,
			 .len = size}};

	struct spi_buf_set spi_rx_buffer_set =
		{
			.buffers = rx_spi_bufs,
			.count = 1};

	err = spi_transceive_dt(&appSPI, &spi_tx_buffer_set, &spi_rx_buffer_set);

	if (err < 0)
	{
		printk("Read Registers failed: %d\n", reg);
	}
}

void main(void)
{
	if (!device_is_ready(appSPI.bus))
	{
		printk("SPI bus %s not ready!\r\n", appSPI.bus->name);
		return;
	}

	uint8_t reg_value[1];
	readRegister(0x00, &reg_value, sizeof(reg_value));
	printk("Value: 0x%02X", reg_value[0]);
}

I want to read the value from reg 0x00... it must be a 0x24 but i only got 0x00. I checkt it with an logic analyser and I saw that there is no SPI Clock and I don´t know how to fix it.
I spent amost 2 week to get the spi running but nothing works Disappointed. Does anyone have an idea how to get it working?


Parents
  • I think you should check the configuration of the SPI master when you initialize it. According to the spi.h file, there are several parameters that affect the SPI behavior and performance, such as:

    • Frequency - The SPI frequency in kHz (between 125 kHz and 8000 kHz)

    • Mode - The SPI mode (0-3) that determines the clock polarity and phase

    • Bit order - The bit order (MSB first or LSB first) that determines how bytes are shifted on the bus

    • Slave select pin - The GPIO pin number that is used to select the SPI slave device

    • Master output slave input pin - The GPIO pin number that is used for data output from the master and input to the slave

    • Master input slave output pin - The GPIO pin number that is used for data input to the master and output from the slave

    • Serial clock pin - The GPIO pin number that is used for the serial clock signal

    Depending on the configuration you choose, the SPI master will communicate differently with the SPI slave device. For example, if you choose a different frequency or mode, you may need to adjust the timing parameters of the slave device. If you choose a different bit order, you may need to reverse the byte order of the data. If you choose a different pin assignment, you may need to change the wiring or the board layout.

    stumble guys

Reply
  • I think you should check the configuration of the SPI master when you initialize it. According to the spi.h file, there are several parameters that affect the SPI behavior and performance, such as:

    • Frequency - The SPI frequency in kHz (between 125 kHz and 8000 kHz)

    • Mode - The SPI mode (0-3) that determines the clock polarity and phase

    • Bit order - The bit order (MSB first or LSB first) that determines how bytes are shifted on the bus

    • Slave select pin - The GPIO pin number that is used to select the SPI slave device

    • Master output slave input pin - The GPIO pin number that is used for data output from the master and input to the slave

    • Master input slave output pin - The GPIO pin number that is used for data input to the master and output from the slave

    • Serial clock pin - The GPIO pin number that is used for the serial clock signal

    Depending on the configuration you choose, the SPI master will communicate differently with the SPI slave device. For example, if you choose a different frequency or mode, you may need to adjust the timing parameters of the slave device. If you choose a different bit order, you may need to reverse the byte order of the data. If you choose a different pin assignment, you may need to change the wiring or the board layout.

    stumble guys

Children
No Data
Related