This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Struggling to use SPI with Zephyr

Hi,

    really new to the Zephyr stuff and Nordic micros. I've been through the dev academy training examples and get how to use GPIO and I2C by getting a pointer to a device structure from the dts file.

But I just cannot work out how to do the same for an spi device!

I have used the same method as the GPIO and LED devices but get errors when I try to get a pointer to the spi device struct....

main.c

#include <zephyr/zephyr.h>
#include <drivers/gpio.h>
#include <zephyr/drivers/spi.h>

 struct device *spiDev;  /* SPI device pointer*/
void main(void)
{
    spiDev = DEVICE_DT_GET(DT_NODELABEL(spi0));
}

in the nrf52840dk_nrf52840 dts file spi0 is defined as...

  spi0: spi@40003000 {
                        compatible = "nordic,nrf-spi";
                        #address-cells = <1>;
                        #size-cells = <0>;
                        reg = <0x40003000 0x1000>;
                        interrupts = <3 NRF_DEFAULT_IRQ_PRIORITY>;
                        max-frequency = <DT_FREQ_M(8)>;
                        status = "disabled";
                        pinctrl-0 = <&spi0_default>;
                        pinctrl-1 = <&spi0_sleep>;
                        pinctrl-names = "default", "sleep";
                };
I keep getting this error..
C:\ncs\myApps\MySPI_Example1\src\main.c:15: undefined reference to `__device_dts_ord_105'
All I want to do it control a display device via spi, which I have done on other micros fairly easily in the past, so what am i doing wrong here?
I've looked at lots of the examples, including the spi bitbang one but am really confused!!
Many thanks!
  • Hi Duncan

    You could have a look at an SPI master/slave example I made here

    I believe the issue in your case is the nodelabel. spi0 is the name of the node from the board definition, you should not use this as the label. 

    Rather I would try something like this:

    // Overlay
    my_spi: spi0 {
      // Add properties here 
    };
    
    // Source code
    struct device *spiDev;  /* SPI device pointer*/
    void main(void)
    {
        spiDev = DEVICE_DT_GET(DT_NODELABEL(my_spi));
    }

    Best regards
    Torbjørn

  • That's great! Thanks for the example, I have managed to get something working after realising that I needed an overlay file! Your example is really good and I understand it much better now. Thank you so much.

  • Hi Duncan

    So glad to hear the example was helpful. 

    The best of luck with your project Slight smile

    Best regards
    Torbjørn

  • Hi, got your example to build Ok and I have connected the MISO and MOSI pins between master and slave. My build config is for the nrf52840dk and the overlay file is correctly loaded

    Getting the following from terminal output.

    *** Booting Zephyr OS build v3.1.99-ncs1 ***
    SPI master device not ready!
    SPI master chip select device not ready!
    SPI slave device not ready!
    SPI master/slave example started
    SPI SLAVE TX: 0x00, 0x00
    SPI TX: 0x00, 0x00

    and this from the overlay file..

    def-char = <0x00>;

     "Property not mentioned in \"nordic,nrf-spi\"",

    reg_my_spi_master: spi-dev-a@0

     "Node spi-dev-a should have \"compatible\" property",
     "Node should only occur on the undefined bus.",

    Any ideas why it's not working?

    Cheers

    Duncan

  • Hi Duncan

    Thanks for the update, it seems I was using the device_is_ready(..) function incorrectly. Most zephyr functions return an int, but this one returns a bool, so I had to change the example a little. 

    If you check out my latest update I have corrected this. 

    The overlay warnings I can see here also, but they don't seem to affect the example. I will try to find some time later to look into this. 

    Best regards
    Torbjørn

Related