Hi Nordic,
when I run a spi sample, get a error as below:
00> [00:00:00.254,241] <err> os: ***** MPU FAULT ***** 00> [00:00:00.254,241] <err> os: Instruction Access Violation 00> [00:00:00.254,272] <err> os: r0/a1: 0x00000000 r1/a2: 0x00006c88 r2/a3: 0x20000fa8 00> [00:00:00.254,272] <err> os: r3/a4: 0x20000f98 r12/ip: 0x00000000 r14/lr: 0x00000429 00> [00:00:00.254,272] <err> os: xpsr: 0x21000000 00> [00:00:00.254,272] <err> os: Faulting instruction address (r15/pc): 0xf6f7fcb4 00> [00:00:00.254,272] <err> os: >>> ZEPHYR FATAL ERROR 0: CPU exception on CPU 0 00> [00:00:00.254,302] <err> os: Current thread: 0x20000178 (unknown) 00> [00:00:01.231,719] <err> fatal_error: Resetting system
the source code:
/*
* Copyright (c) 2016 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr.h>
#include <device.h>
#include <devicetree.h>
#include <drivers/gpio.h>
#include <drivers/spi.h>
#include <logging/log.h>
LOG_MODULE_REGISTER(main);
/* 1000 msec = 1 sec */
#define SLEEP_TIME_MS 1000
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,
};
const struct device * device_spi;
static void spi_config(void)
{
device_spi = device_get_binding("SPI_2");
if (device_spi == 0) {
printk("SPI2 device not found!\n");
}
}
void spi_test_send(void)
{
int err;
static uint8_t tx_buffer[1];
static uint8_t rx_buffer[1];
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
};
err = spi_transceive(device_spi, &spi_cfg, &tx, &rx);
if (err) {
printk("SPI error: %d\n", err);
} else {
/* Connect MISO to MOSI for loopback */
printk("SPI sent/received: %x/%x\n", tx_buffer[0], rx_buffer[0]);
tx_buffer[0]++;
}
}
void main(void)
{
LOG_INF("Hello World! %s", CONFIG_BOARD);
spi_config();
printk("Blinky with SPI started\n");
while (1) {
spi_test_send();
LOG_INF("Hello World! %s", CONFIG_BOARD);
k_msleep(SLEEP_TIME_MS);
}
}
prj.conf:
# SPI CONFIG_DEBUG_OPTIMIZATIONS=y CONFIG_COMPILER_OPT="" CONFIG_SPI=y CONFIG_PRINTK=y CONFIG_LOG=y CONFIG_USE_SEGGER_RTT=y CONFIG_RTT_CONSOLE=y CONFIG_UART_CONSOLE=n CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=4096