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

Clarification on intended driver to communicating with generic SPI device on nRF52 DK running Zephyr

I apologize if this has been asked in a different way but I have come across several older threads that seem outdated and was hoping for the new "best practices" for the current release of the nRF Connect SDK (v1.2.0) at the time of this writing.

I am trying to communicate with a "generic" SPI device. Unfortunately this is not a device already included in the Zephyr base.

Reading through the Zephyr documentation it seems that I must:

1. Write a device tree overlay compatible with "nordic,nrf-spim" that specifies the MISO, MOSI, and SCK pins

2. Add a child node compatible with "spi-device" that has a reg property equal to the pin I am using as CS pin (although there is some confusion here because there is also a property in the spi-controller device tree binding that specifies a "cs-gpios" and it seems that there is redundancy between this property and the "reg" property of the child node for the slave device)

3. Include a library that is aware of these device tree configurations

Currently my devicetree overlay file looks like this:

&spi0 {
	compatible = "nordic,nrf-spim";
	status = "okay";
	sck-pin = <25>;
	mosi-pin = <23>;
	miso-pin = <24>;
    rhd2132@22 {
        compatible = "spi-device";
        reg = <22>
        spi-max-frequency = <24000000>
        label = "RHD2132"
    };
};

Digging through the documentation it appears that there used to be a nordic driver for SPI devices in the nRF5 SDKs, but I guess that is no longer true in the nRF Connect SDK. It also looks like that driver did not rely on the devicetree system so all the pin configurations happen as #DEFINE's in the application code.

Instead it now appears that there is a driver located at <SDK_DIR>/zephyr/drivers/spi/spi_nrfx_spim.c.

Is this the appropriate driver to use. If so could you point me in the direction of the documentation for this driver and/or and example of it being used including the devicetree overlay file necessary? I assume I need to use this one if I want to take advantage of the DMA features of the nrf devices.

Again, I apologize for the confusion I think I am getting tripped up in the change from nRF5 to nRF Connect SDK and want to use the "most" supported method of communicating with SPI devices on nRF chips.

Parents
  • Hi, 

     

    Is this the appropriate driver to use. If so could you point me in the direction of the documentation for this driver and/or and example of it being used including the devicetree overlay file necessary? I assume I need to use this one if I want to take advantage of the DMA features of the nrf devices.

    When you set compatible = "nordic,nrf-spim";, you're telling dts to target the NRF_SPIM peripheral, which is DMA only.

    If you were to switch it out with "nordic,nrf-spi", you will use the older NRF_SPI module, which is not DMA capable. NRF_SPI is only supported on nRF51 and nRF52-series devices.

     

    Drivers-wise, you essentially have two options (zephyr choice is divided into two options):

    1. Use the generic Zephyr driver API

        a. Hard way: Create a full-blown zephyr based driver - this is normally not done unless you plan to do a pull-request to the zephyr-rtos project.

        b. Easier way: Setup the SPI instance in your application, then add the communication protocol on-top.

    2. Use the nordic specific nrfx driver directly

    You can use the Zephyr API to create your own driver. This includes creating Kconfig menu, device tree, and developing the sensor src and porting to use the spi.h API. This would then be a portable driver, which can theoretically run on any of the supported SPI "backends" (ie: x86, arm, xtensa, etc), but its not a straight forward process.

    You also have the option to use the nrfx_spim driver directly in your application, by adding CONFIG_NRFX_SPIM=y, then adding for instance CONFIG_NRFX_SPIM1=y.

     

    I made a quick example, based on zephyr/samples/basic/blinky/, where I added nrfx_spim, so that you can have a look at the steps for manually adding a nrfx driver to a sample.

    The sequence is very similar on other nrfx drivers (pdm, i2s, rtc, etc).

    nrfx_spim.zip

     

    I tested this on both nrf52_pca10040 board, and nrf9160_pca10090 board. You short MISO and MOSI pin to receive what you transmit.

    I also added a .overlay file for nrf52_pca10040, in case you want to look into adding the zephyr SPI driver.

    Again, I apologize for the confusion I think I am getting tripped up in the change from nRF5 to nRF Connect SDK and want to use the "most" supported method of communicating with SPI devices on nRF chips.

     This is nothing to apologize for. It is a huge change, and it is very understandable that you ask questions. Have you seen this blog post series? https://devzone.nordicsemi.com/nordic/nrf-connect-sdk-guides/b/getting-started/posts/nrf-connect-sdk-tutorial

    That should help with understanding how it all is tied together.

     

    Kind regards,

    Håkon

Reply
  • Hi, 

     

    Is this the appropriate driver to use. If so could you point me in the direction of the documentation for this driver and/or and example of it being used including the devicetree overlay file necessary? I assume I need to use this one if I want to take advantage of the DMA features of the nrf devices.

    When you set compatible = "nordic,nrf-spim";, you're telling dts to target the NRF_SPIM peripheral, which is DMA only.

    If you were to switch it out with "nordic,nrf-spi", you will use the older NRF_SPI module, which is not DMA capable. NRF_SPI is only supported on nRF51 and nRF52-series devices.

     

    Drivers-wise, you essentially have two options (zephyr choice is divided into two options):

    1. Use the generic Zephyr driver API

        a. Hard way: Create a full-blown zephyr based driver - this is normally not done unless you plan to do a pull-request to the zephyr-rtos project.

        b. Easier way: Setup the SPI instance in your application, then add the communication protocol on-top.

    2. Use the nordic specific nrfx driver directly

    You can use the Zephyr API to create your own driver. This includes creating Kconfig menu, device tree, and developing the sensor src and porting to use the spi.h API. This would then be a portable driver, which can theoretically run on any of the supported SPI "backends" (ie: x86, arm, xtensa, etc), but its not a straight forward process.

    You also have the option to use the nrfx_spim driver directly in your application, by adding CONFIG_NRFX_SPIM=y, then adding for instance CONFIG_NRFX_SPIM1=y.

     

    I made a quick example, based on zephyr/samples/basic/blinky/, where I added nrfx_spim, so that you can have a look at the steps for manually adding a nrfx driver to a sample.

    The sequence is very similar on other nrfx drivers (pdm, i2s, rtc, etc).

    nrfx_spim.zip

     

    I tested this on both nrf52_pca10040 board, and nrf9160_pca10090 board. You short MISO and MOSI pin to receive what you transmit.

    I also added a .overlay file for nrf52_pca10040, in case you want to look into adding the zephyr SPI driver.

    Again, I apologize for the confusion I think I am getting tripped up in the change from nRF5 to nRF Connect SDK and want to use the "most" supported method of communicating with SPI devices on nRF chips.

     This is nothing to apologize for. It is a huge change, and it is very understandable that you ask questions. Have you seen this blog post series? https://devzone.nordicsemi.com/nordic/nrf-connect-sdk-guides/b/getting-started/posts/nrf-connect-sdk-tutorial

    That should help with understanding how it all is tied together.

     

    Kind regards,

    Håkon

Children
No Data
Related