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!
Parents
  • 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

    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

  • Hi ovrebekk,

                 thanks for your reply. I'll check out the update later on. Many thanks for your help, I'm up to speed on SPI now!

    Duncan

  • Hi Duncan

    Glad I could help. A big thanks to you for pointing out the mistake in the example Slight smile

    Best regards
    Torbjørn

  • Hi ovrebekk

    Apologies for chiming in on this years later, but I have a question regarding your devicetree overlay for the NRF5340 DK, specifically the code below:

    my_spi_master: &spi4 {
    	compatible = "nordic,nrf-spim";
    	status = "okay";
    	pinctrl-0 = <&spi_master_default>;
    	pinctrl-1 = <&spi_master_sleep>;
    	pinctrl-names = "default", "sleep";
    	cs-gpios = <&gpio0 7 GPIO_ACTIVE_LOW>;
    	reg_my_spi_master:  spi-dev-a@0 {
    		reg = <0>;
    	};
    };

    What does the following code relate to? I cannot find any reference of reg_my_spi_master or spi-dev-a?

    Regards

     
  • Hi,

     

    This thread is quite old, so if you run into any issues, I highly recommend that you open a new thread.

    s23 said:
    but I have a question regarding your devicetree overlay for the NRF5340 DK, specifically the code below:

    reg-my-spi-master is just a name given to a node under the "my_spi_master" device, which in this case is &spi4.

    This is referenced in main.c.

    If you look at parts of the nordic dev academy, especially this section on the i2c, you can see a similar approach when implementing a application specific sensor: https://academy.nordicsemi.com/courses/nrf-connect-sdk-fundamentals/lessons/lesson-6-serial-com-i2c/topic/i2c-driver/

     

    Kind regards,

    Håkon

Reply Children
No Data
Related