SPI Communication Problems Concerning the nRF5340

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


#include <nrfx.h>
#include <nrfx_spim.h>
#include <nrfx_gpiote.h>
#include <nrfx_dppi.h>


/* Define your SPI frequency and timing parameters */
#define SPI_FREQUENCY 26000000    // 26 MHz
#define COMMAND_RATE 10000         // Desired command rate: 10 kHz

/* Define your SPI command and data variables */
#define COMMAND_SIZE 32            // Command size: 32 bits
#define CONVERSION_COMMAND 0x12345678  // Example conversion command
#define SETUP_COMMANDS {0xe0ff0000, 0x80200000, 0x80210000, 0x8026ffff, 0x6a000000, 0x800000c7, 0x8001051a, 0x80020000,0x80030080, 0x80040016, 0x80050017, 0x800600a8, 0x8007000a, 0x8008ffff, 0xa00a0000, 0xa00cffff, 0x802200e2, 0x802300aa, 0x80240080, 0x80254f00, 0xa02a0000, 0xa02c0000,0xa02e0000,0xa0300000,0x8020aaaa, 0x802100ff, 0xe0ff0000}


void main(void)
{
    const struct device *spi_dev;
    //const struct device *cs_dev;
    int rc;

    spi_dev = DEVICE_DT_GET(DT_NODELABEL(spi4));
    //cs_dev = device_get_binding(DT_GPIO_LABEL(DT_ALIAS(cs_gpios), gpios));

    
    // Configure SPI device settings
    struct spi_config spi_cfg = {
        .frequency = SPI_FREQUENCY,
        .operation = SPI_OP_MODE_MASTER | SPI_TRANSFER_MSB | SPI_WORD_SET(32),
        .slave = 0,
        //.cs = DT_GPIO_PIN(DT_ALIAS(cs_gpios), gpios),
    };

    // Set the active level of the chip select line
    //gpio_pin_configure(cs_dev, CS_PIN, GPIO_OUTPUT_ACTIVE | CS_ACTIVE_LEVEL);
	
    if (!device_is_ready(spi_dev)) {
        printk("%s: device not ready.\n", spi_dev->name);
        return;
    }
	

    printk("\n%s SPI command generation\n", spi_dev->name);
    printk("=============================\n");
    rc = spi_transceive(spi_dev, &spi_cfg, NULL, NULL);
	
    // Calculate the delay needed between commands to achieve the desired rate
    k_timeout_t command_delay = K_MSEC(1000 / COMMAND_RATE);

    while (1) {
        // Generate SPI command and send it
        uint8_t tx_buf[4] = {
            (CONVERSION_COMMAND >> 24) & 0xFF,
            (CONVERSION_COMMAND >> 16) & 0xFF,
            (CONVERSION_COMMAND >> 8) & 0xFF,
            CONVERSION_COMMAND & 0xFF
        };
        uint8_t rx_buf[4] = { 0 };

        spi_cfg.frequency = SPI_FREQUENCY;
        spi_cfg.operation = SPI_OP_MODE_MASTER | SPI_TRANSFER_MSB | SPI_WORD_SET(32);
        spi_cfg.slave = 0;

        struct spi_buf tx_bufs[] = {
            { .buf = tx_buf, .len = sizeof(tx_buf) }
        };
        struct spi_buf rx_bufs[] = {
            { .buf = rx_buf, .len = sizeof(rx_buf) }
        };
        struct spi_buf_set txrx_bufs = {
            .buffers = { tx_bufs, rx_bufs },
            .count = ARRAY_SIZE(tx_bufs) + ARRAY_SIZE(rx_bufs)
        };

        //rc = spi_transceive(spi_dev, &spi_cfg, &txrx_bufs, &txrx_bufs);
        //rc = spi_transceive(spi_dev, &spi_cfg, tx_bufs, rx_bufs);
        rc = spi_write(spi_dev, &spi_cfg, tx_bufs);
        if (rc != 0) {
            printk("SPI command transmission failed! Error: %d\n", rc);
        }

        // Wait for the desired delay before sending the next command
        k_sleep(command_delay);
    }
}

I am able to build and flash to my board but the issue comes during debugging. I get a code of -22 after calling spi_transceive and I notice that my spi_dev is optimized out. I am a beginner when it comes to Nordic boards and this software is very new to me as well. I don't know how much I need to do in terms of SPI setup using zephyr as from what I can tell everything else is fine except for these things. Let me know if there are any resources that could help as well.

Parents Reply Children
Related