This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

SPI example for Zephyr (nRF COnnect SDK v1.6.0-rc2)

Hello,

I need an example for using the spi in zephyr and how to change the pins if necessary.

Where can I find it?

Thank you

  • Hello,

    Yes, there are a bunch of examples from Zephyr showing the usage of the SPI interface API.

    You could for instance have a look at the SPI drivers of the BM280 sample.

    For changing pins you can use a devicetree overlay file.

    Regards,

    Markus

  • HI Markus,

    from bme280 i get the function to get the device  and i have modified for use the spi2 

    of board.dts (NRF52-DK), but i have error no device found.

    How am I wrong?

    Thanks


    const struct device *spi2_dev;


    static const struct device *get_spi2_device(void)
    {
    const struct device *dev;
    dev= DEVICE_DT_GET_ANY(arduino_spi);

    if (dev == NULL) {
    /* No such node, or the node does not have status "okay". */
    printk("\nError: no device found.\n");
    return NULL;
    }

    if (!device_is_ready(dev)) {
    printk("\nError: Device \"%s\" is not ready; "
    "check the driver initialization logs for errors.\n",
    dev->name);
    return NULL;
    }

    printk("Found device \"%s\", getting sensor data\n", dev->name);
    return dev;
    }

  • Hello,

    DEVICE_DT_GET_ANY has the devicetree compatible as input arugment. You are using a subnode label.

    So in case of nrf52dk_nrf52832 this would be:

    compatible = "nordic,nrf-spi";

    But using this function is probably not a good idea since you have several SPI peripherals with the same compatibale avialable. 

    spi0: spi@40003000 {
    			#address-cells = < 0x1 >;
    			#size-cells = < 0x0 >;
    			reg = < 0x40003000 0x1000 >;
    			interrupts = < 0x3 0x1 >;
    			status = "disabled";
    			label = "SPI_0";
    			compatible = "nordic,nrf-spi";
    			sck-pin = < 0x1b >;
    			mosi-pin = < 0x1a >;
    			miso-pin = < 0x1c >;
    		};
    		spi1: spi@40004000 {
    			#address-cells = < 0x1 >;
    			#size-cells = < 0x0 >;
    			reg = < 0x40004000 0x1000 >;
    			interrupts = < 0x4 0x1 >;
    			status = "okay";
    			label = "SPI_1";
    			compatible = "nordic,nrf-spi";
    			sck-pin = < 0x1f >;
    			mosi-pin = < 0x1e >;
    			miso-pin = < 0x1d >;
    		};
    		spi2: arduino_spi: spi@40023000 {
    			#address-cells = < 0x1 >;
    			#size-cells = < 0x0 >;
    			reg = < 0x40023000 0x1000 >;
    			interrupts = < 0x23 0x1 >;
    			status = "okay";
    			label = "SPI_2";
    			compatible = "nordic,nrf-spi";
    			sck-pin = < 0x19 >;
    			mosi-pin = < 0x17 >;
    			miso-pin = < 0x18 >;
    			cs-gpios = < &arduino_header 0x10 0x1 >;
    		};

    And as the API description states: "If there are multiple, this returns an arbitrary one."  I recommend you using this function instead.

    Regards,

    Markus

  • Hi Markus,

    Hi Markus,

    thanks for your help, but still i can't understand how to open the SPI2, using the functions you suggest. An example is available that uses these functions.

    Thanks in advance.

    Fausto

  • Hi Markus,

    Hi Markus,

    thanks for your help, but still i can't understand how to open the SPI2, using the functions you suggest. An example is available that uses these functions.

    Thanks in advance.

    Fausto

Related