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 Reply
  • Thank you for the documentation.

    Yes I was looking for Zephyr SPI driver as well as nrf drivers for nRF52 also. I tried the spi programs on both the DKs. Separately they are working properly. But when I try to read from nrf52 on nrf91, it shows error. So now I am switching to I2C. I2C communication is good between both the DKs. I can receive data from nrf52.

    But still curious why spi did not work together.

Children
  • Jagruti said:
    Thank you for the documentation.

    No problem at all, I am happy to help!

    Jagruti said:
    Separately they are working properly.

    What do you mean when you say this; do you mean that the performance was as expected when you flashed one device with the SPI Slave example, and another one with the SPI Master example?

    Jagruti said:
    I2C communication is good between both the DKs.

    I am glad to hear that you have achieve the communication you were after.
    If required, I would not think setting up SPI should prove to be a large endeavor.

    Best regards,
    Karl

  • Yes the performance was as expected when I flashed SPI master example on nrf52 and as well as on nrf91. On nrf91, I used zephyr spi drivers. But when I connected nrf91 spi salve to nrf52 spi master. The data was not received on nrf91. There was error on nrf91 side. I think I did not configure nrf91 as spi slave properly. That's why the error is coming and there is no communication between nrf52 and nrf91. I have not yet understood how to the configuration for spi slave. I must be missing something.

  • 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

Related