SPI Zephyr assembly stack trace error from simple example (nrf5340DK)

I have this basic script to initialise and write spi whilst toggling an led:

#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

/* The devicetree node identifier for the "led0" alias. */
#define LED0_NODE DT_ALIAS(led0)

#define MY_SPI_MASTER DT_NODELABEL(my_spi_master)

#define MY_SPI_SLAVE  DT_NODELABEL(my_spi_slave)

// SPI master functionality
const struct device *spi_dev;
static struct k_poll_signal spi_done_sig = K_POLL_SIGNAL_INITIALIZER(spi_done_sig);

struct spi_cs_control spim_cs = {
	.gpio = SPI_CS_GPIOS_DT_SPEC_GET(DT_NODELABEL(reg_my_spi_master)),
	.delay = 0,
};

static void spi_init(void)
{
	spi_dev = DEVICE_DT_GET(MY_SPI_MASTER);
	if(!device_is_ready(spi_dev)) {
		printk("SPI master device not ready!\n");
	}
	if(!device_is_ready(spim_cs.gpio.port)){
		printk("SPI master chip select device not ready!\n");
	}
}

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 = &spim_cs,
};

static int spi_write_test_msg(void)
{
	static uint8_t counter = 0;
	static uint8_t tx_buffer[2];
	static uint8_t rx_buffer[2];

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

	// Update the TX buffer with a rolling counter
	tx_buffer[0] = counter++;
	printk("SPI TX: 0x%.2x, 0x%.2x\n", tx_buffer[0], tx_buffer[1]);

	// Start transaction
	int error = spi_transceive(spi_dev, &spi_cfg, &tx, &rx);
	if(error != 0){
		printk("SPI transceive error: %i\n", error);
		return error;
	}


	printk("SPI RX: 0x%.2x, 0x%.2x\n", rx_buffer[0], rx_buffer[1]);
	return 0;
}

/*
 * A build error on this line means your board is unsupported.
 * See the sample documentation for information on how to fix this.
 */
static const struct gpio_dt_spec led = GPIO_DT_SPEC_GET(LED0_NODE, gpios);

void main(void)
{
	int ret;
	if (!device_is_ready(led.port)) {
		return;
	}
	ret = gpio_pin_configure_dt(&led, GPIO_OUTPUT_ACTIVE);
	if (ret < 0) {
		return;
	}

	spi_init();
	printk("SPI master/slave example started\n");

	while (1) {
		spi_write_test_msg();
		ret = gpio_pin_toggle_dt(&led);
		if (ret < 0) {
			return;
		}
		k_msleep(SLEEP_TIME_MS);
	}
}

It's a modification of this example:https://github.com/too1/ncs-spi-master-slave-example/tree/master

here's my nrf5340dk_nrf5340_cpuapp.overlay file:

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

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

my_spi_master: &spi4 {
	compatible = "nordic,nrf-spim";
	status = "okay";
	pinctrl-0 = <&spi_master_default>;
	pinctrl-1 = <&spi_master_sleep>;
	pinctrl-names = "default", "sleep";
	cs-gpios = <&gpio0 26 GPIO_ACTIVE_LOW>;
	reg_my_spi_master:  spi-dev-a@0 {
		reg = <0>;
	};
};

Whenever i try and call spi_write or spi_transcieve i get stack trace errors like this:

*** Booting Zephyr OS build zephyr-v3.4.0-2408-g3c1394c17aa4 ***
SPI master/slave example started
SPI TX: 0x00, 0x00
[00:00:00.421,722] <err> os: ***** Reserved Exception ( -16) *****
[00:00:00.421,722] <err> os: r0/a1:  0x20001b10  r1/a2:  0x00003819  r2/a3:  0x000098a3
[00:00:00.421,752] <err> os: r3/a4:  0x000037d1 r12/ip:  0x000037d1 r14/lr:  0x000037d1
[00:00:00.421,752] <err> os:  xpsr:  0x00003600
[00:00:00.421,752] <err> os: Faulting instruction address (r15/pc): 0x000037d1
[00:00:00.421,783] <err> os: >>> ZEPHYR FATAL ERROR 0: CPU exception on CPU 0
[00:00:00.421,813] <err> os: Current thread: 0x20000728 (unknown)
[00:00:00.484,222] <err> os: Halting system

When i put the Faulting instruction address (0x000037d1) through the addr2line tool it points me to this line "C:/Users/Liam/zephyrproject/zephyr/arch/arm/core/aarch32/cortex_m/fault_s.S:80" which is a low level assembly file complaining about data being set in mrs r0, MSP, i'm wondering if there's some nooby mistake i'm making, i'm using zephyr 3.4.99 as the nrf connect sdk and zephyr 0.16.1 for the toolchain.

Any help or comments would be greatly appreciated.

Update, just tried on an nrf52832DK and got the same stack trace error.

Related