SPI slave on NCS v2.0.0

Hi

I am trying to migrate my project to newest NCS v2.0.0  but is struggling figuring out how to configure SPI2 slave operation mode.

The project is currently running v1.9.1 with calls direct to nrfx_spis.c driver using the following i prj.conf

CONFIG_SPI=n  # Zephyr mode disabled
CONFIG_NRFX_SPIS=y
CONFIG_NRFX_SPIS2=y

However for NCS v2.0.0 it seems that a move towards closer integration with DTS and Zephyr driver model has been done.

Do you have any example code on, how to configure this correctly, or can you help point me in the right direction.

Br Anders

Parents
  • Hi,

    Yes, this has changed a bit in NCS 2.0.0. Here is an example that works on the nRF52840 DK:

    main.c

    #include <zephyr.h>
    #include <drivers/gpio.h>
    #include <drivers/spi.h>
    #include <device.h>
    
    const struct device * spi_dev;
    static struct spi_config spi_cfg = {
    	.operation = SPI_WORD_SET(8)
    	         | SPI_OP_MODE_SLAVE
                 | SPI_TRANSFER_MSB
                 //| SPI_MODE_CPOL
                 //| SPI_MODE_CPHA
                 ,
    	.frequency = 4000000,
    	.slave = 1,
    };
    
    static struct spi_buf rx;
    const struct spi_buf_set rx_bufs = {
    	.buffers = &rx,
    	.count = 1
    };
    
    static struct spi_buf tx;
    const struct spi_buf_set tx_bufs = {
    	.buffers = &tx,
    	.count = 1
    };
    
    static void spi_init(void)
    {
    	spi_dev = device_get_binding(DT_LABEL(DT_NODELABEL(spi2)));
    }
    void spi_test_send(void)
    {
    	int err;
    	static uint8_t tx_buffer[2] = {0x2A, 0xF0};
    	tx.buf = tx_buffer;
    	tx.len = sizeof(tx_buffer);
    
    	static uint8_t rx_buffer[2];
    	rx.buf = rx_buffer;
    	rx.len = sizeof(rx_buffer);
    
    	err = spi_transceive(spi_dev, &spi_cfg, &tx_bufs, &rx_bufs);
    	if (err < 0) {
    		printk("SPI error: %d\n", err);
    	} else {
    		printk("RX recv:");
    		for (int i = 0; i < err; i++){
    			printk(" %x", rx_buffer[i]);
    		}
    		printk("\n");
    	}
    }
    void main(void)
    {
        spi_init();
    
    	while (1) {
    		k_msleep(1000);
            spi_test_send();
    	}
    }
    

    prj.conf

    CONFIG_GPIO=y
    CONFIG_SPI=y
    CONFIG_SPI_NRFX=y
    CONFIG_SPI_SLAVE=y

    nrf52840dk_nrf52840.overlay

    &spi2 {
        compatible = "nordic,nrf-spis";
        status = "okay";
        pinctrl-0 = <&spi2_default_new>;
        pinctrl-1 = <&spi2_sleep_new>;
        def-char = <0xAA>;
    };
    
    &pinctrl {
        spi2_default_new: spi2_defalt_new {
            group1 {
                psels = <NRF_PSEL(SPIM_SCK, 0, 3)>,
                        <NRF_PSEL(SPIM_MISO, 0, 4)>,
                        <NRF_PSEL(SPIM_MOSI, 0, 28)>,
                        <NRF_PSEL(SPIS_CSN, 0, 29)>;
            };
        };
        spi2_sleep_new: spi2_sleep_new {
            group1 {
                psels = <NRF_PSEL(SPIM_SCK, 0, 3)>,
                        <NRF_PSEL(SPIM_MISO, 0, 4)>,
                        <NRF_PSEL(SPIM_MOSI, 0, 28)>,
                        <NRF_PSEL(SPIS_CSN, 0, 29)>;
            };
        };
    };

    This code is inspired by the zephyr/sample/bluetooth/hci_spi project. So you can have a look at that project for more details.

    Complete project: 4532.spis.zip

  • Update:   I got it working using NCS v2.0.0 using the  nrfx_spi api directly.

    Thank you for your support

Reply Children
No Data
Related