Electronic – How to recieve data over SPI as a master device

spistm32

Working on Serial data protocols using STM32 (bare metal coding, not using any HAL libraries).

So far I've configured the board to act as master and send data to an IC and also configured two different stm boards (one as master and other as slave) to exchange data. So all that included using the MOSI line. Now I wish to use the MISO line so that I can have a duplex connection between the two boards.

For the master side of things, the CS (chip select) is configured as output pin so I can pull it high or low after as many as bytes as I like, and thus I have enable 'Software Slave Management' in the registers.

On the slave board side, CS is configured as Alternate Function and disabled 'Software Slave Management' so the CS pin gets pulled low whenever master wants to send data.

Keeping this in mind, how do I control the CS pins on the two boards when using the MISO line?

Do I just swap the 'Software Slave Management' enable and the Alternate Function on both boards?

Best Answer

There is no difference.

The CS is always from master to slave, as master is the one that generates the clock, and slave always listens for the clock (and it basically is gated by the CS).

Basically when the master transmits a byte to slave, it also receives a byte from the slave.

If you want the master to receive data from slave, the slave must set the byte into data register for transfer, and the master must transfer dummy bytes to clock data back from slave.

While it is possible for the devices to arbitrate who is the master and exchange roles, multimaster SPI implementations may not be the easiest to start with. It just is easier to have one board as master, and one board as slave.