SPI communication with Lis3dh sensor (nrfConnect SDK)

Hello,

I'm encountering an issue while configuring the SPI bus to communicate with a LIS3DH sensor using nrf52832. I'm working with Visual Studio Code using nrfConnect SDK (Zephyr).

Here's the Device Tree Source (DTS):

// Copyright (c) 2024 Nordic Semiconductor ASA
// SPDX-License-Identifier: Apache-2.0

/dts-v1/;
#include <nordic/nrf52832_qfaa.dtsi>

/ {
    model = "xxx";
    compatible = "xxx";

    aliases {
        led0 = &led0;
        led1 = &led1;
        led2 = &led2;
    };

    chosen {
        zephyr,sram = &sram0;
        zephyr,flash = &flash0;
        zephyr,code-partition = &slot0_partition;
    };

    leds{
        compatible = "gpio-leds";
        led0: led_0{
            gpios = <&gpio0 10 GPIO_ACTIVE_LOW>;
            label = "Blue LED 0";
        };
        led1: led_1{
            gpios = <&gpio0 9 GPIO_ACTIVE_LOW>;
            label = "Green LED 1";
        };
        led2: led_2{
            gpios = <&gpio0 8 GPIO_ACTIVE_LOW>;
            label = "Red LED 2";
        };
    };
};

&flash0 {
    partitions {
        compatible = "fixed-partitions";
        #address-cells = <1>;
        #size-cells = <1>;

        boot_partition: partition@0 {
            label = "mcuboot";
            reg = <0x0 0xc000>;
        };
        slot0_partition: partition@c000 {
            label = "image-0";
            reg = <0xc000 0x32000>;
        };
        slot1_partition: partition@3e000 {
            label = "image-1";
            reg = <0x3e000 0x32000>;
        };
        scratch_partition: partition@70000 {
            label = "image-scratch";
            reg = <0x70000 0xa000>;
        };
        storage_partition: partition@7a000 {
            label = "storage";
            reg = <0x7a000 0x6000>;
        };
    };
};


&gpio0 {
    status = "okay";
};

&gpiote {
    status = "okay";
};



lis3dh_spi_master: &spi0 {
    compatible = "nordic,nrf-spi";
    status = "okay";
    pinctrl-0 = <&spi0_default>;
    pinctrl-1 = <&spi0_sleep>;
    pinctrl-names = "default","sleep";
    cs-gpios = <&gpio0 18 GPIO_ACTIVE_LOW>;
   
    reg_lis3dh: lis3dh@0{
        compatible = "st,lis2dh";
        reg = <0>;
        irq-gpios = <&gpio0 16 GPIO_ACTIVE_HIGH>, <&gpio0 17 GPIO_ACTIVE_HIGH>;
        spi-max-frequency = <8000000>;
    };
};

&pinctrl {
    spi0_default: spi0_default {
        group1 {
            psels = <NRF_PSEL(SPIM_SCK, 0, 15)>,
                    <NRF_PSEL(SPIM_MOSI, 0, 20)>,
                    <NRF_PSEL(SPIM_MISO, 0, 19)>;

        };
    };
    spi0_sleep: spi0_sleep {
        group1 {
            psels = <NRF_PSEL(SPIM_SCK, 0, 15)>,
                    <NRF_PSEL(SPIM_MOSI, 0, 20)>,
                    <NRF_PSEL(SPIM_MISO, 0, 19)>;
            low-power-enable;
        };  

    };
};



The spi_file is : 

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

#define LIS3DH_OUT_X_L                  0x28
#define LIS3DH_OUT_X_H                  0x29


#define LIS3DH_SPI_MASTER DT_NODELABEL(lis3dh_spi_master)

// SPI master functionality
const struct device *spi_dev;

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


static const struct spi_config spi_cfg = {
    .operation = SPI_OP_MODE_MASTER | SPI_WORD_SET(8) | SPI_TRANSFER_MSB,
    .frequency = 8000000,
    .slave = 0,
    .cs = &spim_cs,
};

void spi_init(void)
{
    spi_dev = DEVICE_DT_GET(LIS3DH_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");
    }
}

uint8_t test_spi(void)
{
    int err;
    static uint8_t tx_buf[1];
    static uint8_t rx_buf[1];

    const struct spi_buf_set tx = {
        .buffers = &tx_buf,
        .count = 1
    };
    const struct spi_buf_set rx = {
        .buffers = &rx_buf,
        .count = 1
    };

    tx_buf[0] = LIS3DH_OUT_X_L;
    err = spi_transceive(spi_dev, &spi_cfg, &tx, &rx);
    if (err < 0) {
        printk("spi_transceive_dt() failed, err: %d", err);
        return err;
    }
    printk("\nTx data : %d, Tx data : %d",tx_buf[0], rx_buf[0]);
}

And the main calls spi_init() and test_spi().

I'm encountering this error during spi_transceive() function :

Bluetooth initialized
Advertising successfully started
BEACON START
[00:00:01.005,676] <err> os: ***** Reserved Exception ( -16) *****
[00:00:01.005,706] <err> os: r0/a1: 0x20006f00 r1/a2: 0x00016a0d r2/a3: 0x0002e2bf
[00:00:01.005,706] <err> os: r3/a4: 0x000169f9 r12/ip: 0x000169f9 r14/lr: 0x000169f9
[00:00:01.005,737] <err> os: xpsr: 0x00000000
[00:00:01.005,737] <err> os: s[ 0]: 0x00000000 s[ 1]: 0x00000000 s[ 2]: 0x00000000 s[ 3]: 0x00016c09
[00:00:01.005,767] <err> os: s[ 4]: 0x000169f9 s[ 5]: 0x00000000 s[ 6]: 0x00016b69 s[ 7]: 0x000169f9
[00:00:01.005,767] <err> os: s[ 8]: 0x00016f15 s[ 9]: 0x00031ed3 s[10]: 0x00016f15 s[11]: 0x00016f15
[00:00:01.005,798] <err> os: s[12]: 0x00016f15 s[13]: 0x00016f15 s[14]: 0x00016f15 s[15]: 0x00016f15
[00:00:01.005,798] <err> os: fpscr: 0x00031e89
[00:00:01.005,798] <err> os: Faulting instruction address (r15/pc): 0x000169f9
[00:00:01.005,859] <err> os: >>> ZEPHYR FATAL ERROR 0: CPU exception on CPU 0
[00:00:01.005,889] <err> os: Current thread: 0x20003030 (unknown)
[00:00:01.391,052] <err> fatal_error: Resetting system

Thank for you help

Related