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

    I would suggest checking out the SPI test and SPIS test example projects my colleague Sigurd has uploaded to his GitHub here. You should get an idea on how to set up SPI slaves and masters correctly from those.

    Best regards,

    Simon

  • Hello ,

    checking out the SPI test and SPIS test

    I referred to Sigurd's upload and it does have slave code.

    But what I want to know is, in SPI config structure:

    /**
     * @brief SPI controller configuration structure
     *
     * @param frequency is the bus frequency in Hertz
     * @param operation is a bit field with the following parts:
     *
     *     operational mode    [ 0 ]       - master or slave.
     *     mode                [ 1 : 3 ]   - Polarity, phase and loop mode.
     *     transfer            [ 4 ]       - LSB or MSB first.
     *     word_size           [ 5 : 10 ]  - Size of a data frame in bits.
     *     lines               [ 11 : 12 ] - MISO lines: Single/Dual/Quad/Octal.
     *     cs_hold             [ 13 ]      - Hold on the CS line if possible.
     *     lock_on             [ 14 ]      - Keep resource locked for the caller.
     *     cs_active_high      [ 15 ]      - Active high CS logic.
     * @param slave is the slave number from 0 to host controller slave limit.
     * @param cs is a valid pointer on a struct spi_cs_control is CS line is
     *    emulated through a gpio line, or NULL otherwise.
     *
     * @note Only cs_hold and lock_on can be changed between consecutive
     * transceive call. Rest of the attributes are not meant to be tweaked.
     *
     * @warning Most drivers use pointer comparison to determine whether a
     * passed configuration is different from one used in a previous
     * transaction.  Changes to fields in the structure may not be
     * detected.
     */
    struct spi_config {
    	uint32_t		frequency;
    	uint16_t		operation;
    	uint16_t		slave;
    
    	const struct spi_cs_control *cs;
    };

    What does "@param slave is the slave number from 0 to host controller slave limit." mean..?
    If i set slave as 2,

    From code how do I operate master to work on 2 slaves..?

    In dts as in below:

    &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>;
    	}

    To send data to <&gpio0 8 GPIO_ACTIVE_LOW>, how would my device_binding & spi_cfg look like before I call spi_transceive..?

    To send data to <&gpio0 7 GPIO_ACTIVE_LOW>, how would my device_binding & spi_cfg look like before I call spi_transceive..?

    Thanks,

    Ubaid

  • Hi

    The host controller limit is set in master side, and the "slave number" will get a number between 0 and what the master side has set to the limit, so the master knows what slave device it is communicating with. You need to set the slave limit to 2 or more on the master side and configure a second SPI setup with another instance, etc. The SPIM API reference should be helpful here.

    Device binding and enabling SPI peripherals should be explained in this nRFConnect SDK tutorial and should give you an idea of how to set up peripherals on the master side.

    Best regards,

    Simon

Related