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

2 SPIM switching continuously

Hi,

There are 2 SPI device on our custom pcb. Pin connections:

Pins of 1st:

#define bmx_miso1 22
#define bmx_mosi 19
#define bmx_sck 20

Pins of 2nd:

#define bmx_miso2 18
#define bmx_mosi 19
#define bmx_sck 20

Clock,Mosi lines are common pins. Miso and CS lines are different pins. 

In the initialization, we only select one SPI slave.  

void initialize_spi(void)
{

pin_set(bmx_cs1);
pin_set(bmx_cs2);
NRF_SPIM0->PSEL.SCK = bmx_sck;
NRF_SPIM0->PSEL.MOSI = bmx_mosi;
NRF_SPIM0->PSEL.MISO = bmx_miso1;

}

But we need read both device in turn in a while loop.

Should i reinitialize SPIM peripheral every time when reading other device? What is the propor way of this?

Sould i do that?

first 

NRF_SPIM0->PSEL.MISO = bmx_miso1;

then 

NRF_SPIM0->PSEL.MISO = bmx_miso2;

again first again second... Contiuously changing SPI MISO pin, is it the way? 

Best Regards

  • Have you read this thread? It suggests several ways of interfacing with two slaves over SPI.

    Best regards,

    Simon

  • Thanks for reply,

    But this not the answer what i am expect. The examples in your link related to SDK. ı am using standalone code. 

  • I'm not sure why you used the pin arrangement you did. You seem to be using the same SCK and MOSI pins for both slaves, but different MISO pins. Can you explain why you did this? Is there some limitation concerning pin availability on your PCB?

    In general. you can have multiple SPI slave devices connected to the same MOSI/MISO/SCK pins on a SPI master. There's no need to separate them. The one thing you must separate is the chip select: each slave must have its own, and this controls whether or not the slave device is actually paying attention to the SPI bus. If device1 and device1 are on the same bus, and only device1's chip select pin is asserted, then only device1 will talk to the master (device2 will ignore the states of the MOSI/MISO/SCK).

    This is how most typical SPI devices work anyway; they generally will have a chip select pin. I don't know if maybe you're using a custom device that wasn't designed with such a mechanism.

    Is it possible for you to change the wiring so they both share all 3 pins, and you use a chip select as I described? If you can't, then yes, you are required to change the PSEL.MISO register each time you want to talk to a different device.

    A more standard approach would be one of the following:

    - The nRF52 chips have multiple I/O blocks that can be configured as SPI masters. This means you can have two completely independent SPIM controllers active at once, with device1 connected to one of them and device2 connected to the others. This also allows you to access both devices at the same time, totally independent of each other, and you can just keep their chip select pins asserted all the time (as long as they're the only devices on each bus).

    - Use one SPIM controller and connect both SPI slaved to the same MOSI/MISO/SCK pins, then use a GPIO pin to assert the chip selects for each slave device in turn. When the CS for device1 is asserted, you can only talk to device1. When the CS for device2 is asserted, you can only talk to device2.

    -Bill

Related