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

SPI communication between nRF9160DK and nRF52840DK

Hello,

I want to transfer data from nRF52840DK to nRF9160DK using SPI. I want to use nRF52840DK as master which will transfer the data and nRF9160DK as slave which will receive the data.

I have never worked with nRF52840DK. For SPI master, I tried SPI example on nRF52840DK. When I connect MOSI to MISO, the data is transferred. Now I want to transfer this data to nRF9160DK.

On nRF9160DK, I tried SPI master example to check if it works. Now I want to use nRF91 as SPI slave. So I added spi slave drivers and changed overlay file also. But I am not understanding how to configure spi_config struct for slave.

static const struct spi_config spi_cfg = {
//	.operation = SPI_OP_MODE_SLAVE | SPI_MODE_CPOL | SPI_MODE_CPHA,
        .operation = SPI_OP_MODE_SLAVE,
	.frequency = 4000000,
	//.slave = 1,

};

I am not understanding how to set these parameters to configure spi in slave and receive the data.

I connected both the dks to check if nrf91 can receive the data, but spi_read shows error

Parents
  • Hello,

    Have you attempted to flash one of the devices with the SPI Slave example, and the other with the SPI Master example - and testing them according to their documentation?
    If you have not, please do it and let me know if you experience any unexpected results.

    Are you working out of an example provided in the SDK? If so, which one?
    Could you also tell me where the code snippet you provided is from, I am unable to recognize it.

    I am not understanding how to set these parameters to configure spi in slave and receive the data.

    Have you seen the SPIS API Reference or Data Structure description in the SDK documentation?
    They detail the different options for each data structure and driver functions.
    You might also be interested in seeing this description of SPI Slave driver usage.

    Was this what you were looking for?

    but spi_read shows error

    Please detail what error it shows, and when it is encountered.

    Best regards,
    Karl

     

  • Actually I want to configure nRF9160DK as spi slave. So can you please provide the document for that. For nRF91 I am using zephyr spis driver.

  • I see.
    If you would share the relevant code you have written to configure your nRF9160 as SPI Slave, I could take a look.

    Do you configure it as SPI_OP_MODE_SLAVE in the SPI config?
    The different configuration parameters are detailed in the Zephyr SPI API Reference I linked earlier.

    Best regards,
    Karl

  • I read the SPI API reference. But still few things were unclear.

    My spi config struct looks like this:

    static const struct spi_config spi_cfg = {
    //	.operation = SPI_OP_MODE_SLAVE | SPI_MODE_CPOL | SPI_MODE_CPHA,
            .operation = SPI_OP_MODE_SLAVE,
    	.frequency = 4000000,
    	//.slave = 1,
    
    };

    I did not understand how to set other parameters. As it is a slave, the cs pin will be active_low. But how to set the spi mode that I did not understand and even in .slave parameter what value goes, that is also not clear to me. While configuring master, it was set to 0.

  • Jagruti said:
    But still few things were unclear.

    Is there certain parameters you are uncertain about? Please let me know, then I will explain their usage. You may also want to see the different defines in the spi.h file. It is located in NCS\zephyr\include\drivers\spi.h
    There you can read the description of each parameter, if you would like.

    Jagruti said:
    I did not understand how to set other parameters.

    Are you referring to the bitwise or ' | ' operation taking place in the example code?
    The main thing to note here is that the .operation element of the spi_config struct is a 16 bit bit-field. So, you configure this by doing the bitwise or operation between the different functionality you would like. Are you familiar with bitfields and bitwise operations?
    As an example, if you were to configure slave mode and a word_size of 8, it would look like this:

    static const struct spi_config spi_cfg = 
    {
        .operation = SPI_OP_MODE_SLAVE | SPI_WORD_SIZE_SET(8),
    };

    Jagruti said:
    As it is a slave, the cs pin will be active_low.

    This option is not for configuring it as a slave. The option exists because some devices would rather like the CS signal to be active high, instead of active low. This is not necessary to set for your application.
    The default value is active_low, so you do not need to do anything with it when using SPI between nRF9160 and nRF52840, since both of these devices use active_low. 

    If you are not certain how to use the SPI, you could take a look at the hci_spi example from the ncs sdk, located in ncs\zephyr\samples\bluetooth\hci_spi
    This example demonstrates pushing data received from BLE to another device using SPI.
    The example documentation can be seen here.

    Please let me know if things are still unclear.

    Best regards,
    Karl

  • Hi,

    Thank you so much for the detailed answer. Many things are now clear to me. I will try your solution.

  • No problem at all, I am happy to help.

    Jagruti said:
    Many things are now clear to me.

    I am glad to hear that!

    Let me know if you should encounter any more issues, or have more questions! :)

    Best regards,
    Karl

Reply Children
No Data
Related