Handling multiple slaves on single spi master channel

Hi, 

The spi_config structure zephyr provides is as below:

	struct spi_config
	{
		uint32_t frequency;
		uint16_t operation;
		uint16_t slave;

		const struct spi_cs_control *cs;
	};


Wherein cs is initialized to the chip select pin.

In my project I have a requirement to handle 2 slaves which are exactly the same.

Herein, how can I manage 2 instances of, same slaves

And do I need to get multiple device bindings for the same spi channel..?

Thanks,

Ubaid

  • Hi Ubaid

    This should indeed be possible. In order to have one SPI(M) instance handle multiple slaves, you should make multiple structs with different CS pin inits that can be called from the SPI write/read functions so you easily can switch between what devices you'd like to handle.

    Best regards,

    Simon

  • Hello  ,

    Thanks for the info, 

    you should make multiple structs with different CS pin inits that can be called from the SPI write/read functions so you easily can switch between what devices you'd like to handle.

    I am more interested in the dts/overlay file's way of using multiple cs:

    &spi1 {
    	compatible = "nordic,nrf-spim";
    	status = "okay";
    	sck-pin = <41>;
    	mosi-pin = <40>;
    	miso-pin = <4>;
    	cs-gpios = <&gpio0 22 0xff>,<&gpio0 23 0xff>;
    };
     

    As in this code snippet, "cs-gpios" node is assigned to multiple cs pins,
    I want to know how this is handled in the code to switch between slaves.

    Kindly suggest.

    Thanks,

  • Hi

    For SPI devices, "@0 or @1" refers to the position of the chip select pin in the cs-gpios configuration. As you can see in the config below from the Thingy91.dts file, the SPI devices are defined to use one CS pin each.

    &spi3 {
    	compatible = "nordic,nrf-spim";
    	status = "okay";
    	sck-pin = <3>;
    	mosi-pin = <4>;
    	miso-pin = <5>;
    	cs-gpios = <&gpio0 8 GPIO_ACTIVE_LOW>, <&gpio0 7 GPIO_ACTIVE_LOW>;
    
    	adxl362@0 {
    		compatible = "adi,adxl362";
    		label = "ADXL362";
    		spi-max-frequency = <8000000>;
    		reg = <0>;
    		int1-gpios = <&gpio0 9 0>;
    	};
    
    	adxl372@1 {
    		compatible = "adi,adxl372";
    		label = "ADXL372";
    		spi-max-frequency = <8000000>;
    		reg = <1>;
    		int1-gpios = <&gpio0 6 0>;
    	};
    };

    Best regards,

    Simon

  • The standard, most common, and obvious way to handle multiple SPI slaves is for each to have its own slave select line. That's precisely what they are for. You wire the MOSI, SCK, and MISO lines altogether, then each slave gets its own slave select line.

  • Hello ,

    Thank you for the info,
    Noted on the dts syntax for multiple slaves.

    Kindly suggest how this can be handled in code, if i want to call "SPI_Write()" on:

    1. slave one <&gpio0 8 GPIO_ACTIVE_LOW>

    2. slave two <&gpio0 7 GPIO_ACTIVE_LOW>

    What would the code syntax look like..?

    How would I configure spi_config..?

    and how would I call  "SPI_Write()" on each slave.

    Thanks,

Related