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

How to Use SPI Interface in a Zephyr sample example?

Hi,

I am trying to understand how to enable SPI in blinky application provided by Nordic. My intention is to transfer data between the two Nordic development kit running blinky application over SPI. I have enabled SPI with the following details in the prj.conf file.

CONFIG_GPIO=y
CONFIG_SPI=y

CONFIG_SPI_NRFX=y
CONFIG_SPI_SLAVE=y
CONFIG_SPI_ASYNC=y
CONFIG_SPI_1=y

I see that DEVICE_INIT() in main.c and  DEVICE_AND_API_INIT(1) in spi_nrfx_spi.c do all the initialization based on the board dts file. And, I could use spi_read(), spi_write(), spi_transceive() as appropriate to send/receive data thru SPI interface.

Is my understanding correct? 

Please add if I am missing anything.

Thanks,

Ram

Parents
  • Hi Hakon,

    Thanks for your reply!

    I have decided to use nrfx SPI driver at this time instead of Zephyr SPI port, and I have created two simple projects (using minimal) to run on two separate nRF52840dk: one for the master and the other for the slave. I start the slave and then start the master. The interrupt handler is called on the slave but the rx_amount is showing 0. I also noticed that interrupt handler on master is called but nrfx_spim_xfer() returns an error code. The prj.conf files for the two projects and the main.c are attached here. 

    Slave:

    CONFIG_SPI=n
    CONFIG_NRFX_SPIS=y
    CONFIG_NRFX_SPIS1=y

    CONFIG_MAIN_STACK_SIZE=4096

    ---------------------------------------------------

    Master:

    CONFIG_SPI=n
    CONFIG_NRFX_SPIM=y
    CONFIG_NRFX_SPIM1=y
    CONFIG_MAIN_STACK_SIZE=4096

    *** Booting Zephyr OS build v2.4.0-ncs1-3-gfe00929d308d ***
    SPIM example Error ransfer completed.code = 195887104 (this is due to coding issue but this is NRFX_SUCCESS)

    *** Booting Zephyr OS build v2.4.0-ncs1-3-gfe00929d308d ***
    SPIS example Transfer completed. Received: 0                      It means slave is seeing the interrupt without any data.

    Could you please see the .config and main files and guide me what is missing for slave not reading the data? 

    Thanks,

    Ram

  • Hi Hakon,

    Thanks for your prompt reply!

    I have updated the pin ordering in the configuration on both sides. I have also changed the number of bytes to send.

    Is the setup for the event handler correct? I am ready to share my screen with you if you have a few minutes?

    Let me know. 

    Thanks

    Ram

Reply
  • Hi Hakon,

    Thanks for your prompt reply!

    I have updated the pin ordering in the configuration on both sides. I have also changed the number of bytes to send.

    Is the setup for the event handler correct? I am ready to share my screen with you if you have a few minutes?

    Let me know. 

    Thanks

    Ram

Children
  • Hi Ram,

     

    I slightly modified the samples, so that it sets up RX and TX transaction on the master side, and increments the first byte in the payload (cycles through ascii '0' to '9').

    I do not have two nrf52840dk's at my end at this point, so I'm using a nRF9160-DK as the master, but it should be possible to use nRF52/nRF53/nRF91 with no or little modifications. I tested both examples on a nRF52840 (ie: reversed the SPI roles)

    nRF52840 is the SPIS in my end with the below prints.

    Master should print like this when properly connected to the slave, and sends once per second (note: it prints in hex) 

     

    Slave should print this:

    Here you should see that the first byte increments.

     

    The actual bus communication should look like this (here sampled with a logic analyzer from Saleae):

     

    Here are my projects: 

    nrfx_spi_master.zip

    nrfx_spi_slave.zip

     

    If you, after flashing and connecting SPIS -> SPIM, still do not receive or send data as expected, could you please use a logic analyzer to see if there's anything happening on the SPI pins?

      

    Kind regards,

    Håkon