[nRF52832] Not getting the expected results using zephyr SPI

Hi,

I am using the NRF52832 DK version 3.0.0 with a UWB module: the DWM1000 from Qorvo.

I was struggling with the SPI communication to get the Chip ID by sending a simple 0x00 byte through the MOSI line. I have tried with nrfx_spi alone, but 

no success for now. I share my code, which is written in zephyr, it should work but I always receive a 0xFFFFF. I tested the qorvo module with arduino and

it seems to work fine, as I am getting the expected results; as a result, the problem seems to be my code or the board. (I am using the NrfConnect for VScode)

I also followed the instruction in the User Guide where it is said that I should enable the I/O expander to avoid any problem with the buttons and leds in the board

which uses the SPI pins also. 

source: infocenter.nordicsemi.com/.../nRF52_DK_User_Guide_v1.2.pdf 

&pinctrl {
	spi_master_default: spi_master_default {
		group1 {
			psels = <NRF_PSEL(SPIM_SCK, 0, 25)>,
					<NRF_PSEL(SPIM_MOSI, 0, 23)>,
					<NRF_PSEL(SPIM_MISO, 0, 24)>;
		};
	};

	spi_master_sleep: spi_master_sleep {
		group1 {
			psels = <NRF_PSEL(SPIM_SCK, 0, 25)>,
					<NRF_PSEL(SPIM_MOSI, 0, 23)>,
					<NRF_PSEL(SPIM_MISO, 0, 24)>;
			low-power-enable;
		};
	};
};

spi2_master: &spi2 {
	compatible = "nordic,nrf-spi";
	status = "okay";
	pinctrl-0 = <&spi_master_default>;
	pinctrl-1 = <&spi_master_sleep>;
	pinctrl-names = "default", "sleep";
	cs-gpios = <&gpio0 22 GPIO_ACTIVE_HIGH>;
	reg_spi2_master: spi-dev-a@0 {
		reg = <0>;
	};
};

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

/* 1000 msec = 1 sec */
#define SLEEP_TIME_MS   		1000
#define ZEPHYR_MHZ_TO_HZ(freq)  ((freq) * 1000 * 1000)
#define QORVO_CHIPID_REG		0x00
#define MY_SPI_MASTER 			DT_NODELABEL(spi2)
// SPI master functionality
const struct device *spi_dev = DEVICE_DT_GET(MY_SPI_MASTER);

// Chip select functionality
#define MY_GPIO0                DT_NODELABEL(gpio0)
#define GPIO_0_CS               22
static const struct device *gpio0_dev = DEVICE_DT_GET(MY_GPIO0);

struct spi_config spi_cfg = {
	.operation = SPI_TRANSFER_MSB | SPI_WORD_SET(8),// | SPI_MODE_CPOL | SPI_MODE_CPHA,
	.frequency = 2000000U,
	.slave = 0,
	//.cs = NULL,
};

void print_buffer(uint32_t readlength, uint8_t *readBuffer){
    int j = 0;
    printf("Payload is: ");
    for(int loop = readlength; loop > 0; loop--){
        j = (j << 8) | readBuffer[loop];
    } 
    printf("0x%.8X \t\n", j);
}

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

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

    struct spi_buf tx_buf[] = {
		{.buf = tx_buffer, .len = sizeof(tx_buffer),}
	};
	struct spi_buf_set tx = {
		.buffers = tx_buf,
		.count = 1
	};

	struct spi_buf rx_buf[] = {
		{.buf = values, .len = size,}
	};
	struct spi_buf_set rx = {
		.buffers = rx_buf,
		.count = 1
	};

	gpio_pin_set(gpio0_dev, GPIO_0_CS, 0);
    // Start transaction
	err = spi_write(spi_dev, &spi_cfg, &tx);
	if(err<0){
		printk("SPI transaction error: %i\n", err);
		return err;
	}
	err = spi_read(spi_dev, &spi_cfg, &rx);
	if(err<0){
		printk("SPI receive error: %i\n", err);
		return err;
	}
	gpio_pin_set(gpio0_dev, GPIO_0_CS, 1);
    return 0;
}

static void readChipID(void){
	uint8_t rx_buff_chipid[5];
	readfromspi(QORVO_CHIPID_REG,rx_buff_chipid,5);
	//printk("ChipID: 0x%02X\n");
	print_buffer(5, rx_buff_chipid);
}

int main(void)
{
	gpio_pin_configure(gpio0_dev, GPIO_0_CS, GPIO_OUTPUT);
	gpio_pin_set(gpio0_dev, GPIO_0_CS, 1);
	if(!device_is_ready(spi_dev)) {
		printk("SPI master device not ready!\n");
	}
	readChipID();
	k_msleep(SLEEP_TIME_MS);

	while (1) {
		k_msleep(SLEEP_TIME_MS);
	}
	return 0;
}

Please, I need some suggestions about the code or configuration. Probably I am misunderstanding something. 

Parents Reply Children
  • Hi, Simonr

    Thank you so much for your rappid reply. Fortunately, I figured out. My first suspicion was related to the code, but then it turns out to be a hardware problem sort to say. I tried connecting the SPI pins to a different port (using &spi1 default pins instead of the ones assigned to &spi2, which are the ones connecting to the board directly). Below I show my set up, it is a total mess but the effort worth it. Now, I am able to communicate succesfully using SPI. For anyone reaching this thread, you can check the example code for communicating with this Qorvo module using zephyr in my github: https://github.com/EmbeddedRick/NRF52832_deca1000_SPI.git 

    Best, regards, 

    Rick

Related