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 Håkon,

    I have finally had some more time to work on this. I decided I wanted to try and get the "Zephyr" option working so I was referring to the nrf9160 example you sent. At the moment I am just trying to get the "device_get_binding()" function to run properly and identify the SPI controller I am hoping to use. During complilation I get an error from the nrfx_spi library that says

    "'DT_NORDIC_NRF_SPI_SPI_1_IRQ_0_PRIORITY' undeclared (first use in this function); did you mean 'DT_NORDIC_NRF_SPIM_SPI_1_IRQ_0_PRIORITY'?"

     

    The surrounding code in the driver is this:

    	static int spi_##idx##_init(struct device *dev)			       \
    	{								       \
    		IRQ_CONNECT(NRFX_IRQ_NUMBER_GET(NRF_SPI##idx),		       \
    			    DT_NORDIC_NRF_SPI_SPI_##idx##_IRQ_0_PRIORITY,      \
    			    nrfx_isr, nrfx_spi_##idx##_irq_handler, 0);	       \
    		return init_spi(dev);					       \
    	}	
    	
    	...
    	
    		static const struct spi_nrfx_config spi_##idx##z_config = {	       \
    		.spi = NRFX_SPI_INSTANCE(idx),				       \
    		.config = {						       \
    			.sck_pin   = DT_NORDIC_NRF_SPI_SPI_##idx##_SCK_PIN,    \
    			.mosi_pin  = DT_NORDIC_NRF_SPI_SPI_##idx##_MOSI_PIN,   \
    			.miso_pin  = DT_NORDIC_NRF_SPI_SPI_##idx##_MISO_PIN,   \
    			.ss_pin    = NRFX_SPI_PIN_NOT_USED,		       \
    			.orc       = CONFIG_SPI_##idx##_NRF_ORC,	       \
    			.frequency = NRF_SPI_FREQ_4M,			       \
    			.mode      = NRF_SPI_MODE_0,			       \
    			.bit_order = NRF_SPI_BIT_ORDER_MSB_FIRST,	       \
    			.miso_pull = SPI_NRFX_MISO_PULL(idx),		       \
    		}							       \
    	};	

    I also get some errors around where the pin assigments are made (also in code snippet above). For the record it looks like changing the SPI port I am trying to configure does properly populate the "idx" variable.

    Also here is my most up-to-date overlay file and proj.conf file

    # GPIO
    CONFIG_GPIO=y
    
    # SPI - Copied from Nordic nrf9160 samples
    CONFIG_SPI=y
    CONFIG_SPI_NRFX=y
    CONFIG_SPI_1=y
    CONFIG_SPI_1_NRF_SPIM=y

    &spi1 {
    	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";
        };
    };

    Any ideas? In order to get things working in the short term I may just switch to the nrfx library that you suggested.

    Thanks again

  • Hi,

     

    nRF9160 has the NRF_SPIM peripheral only, not the legacy NRF_SPI peripheral.

    Have you manually created a driver for the rhd2132 in the zephyr tree (dts + .c / .h)? If not, that overlay section related to "rhd2132@22" will not work as you intend it to, and you should remove it from the overlay file.

     

    Kind regards,

    Håkon

Reply Children
No Data
Related