1
\$\begingroup\$

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?

\$\endgroup\$
3
\$\begingroup\$

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.

\$\endgroup\$
4
  • \$\begingroup\$ Good verbal description. This website has some nice diagrams: corelis.com/education/tutorials/spi-tutorial A scope is very helpful. \$\endgroup\$
    – Mattman944
    Apr 5 '20 at 17:55
  • \$\begingroup\$ Thank you for the solution. I was really confused about the MISO line but that cleared it. So I did as you said and connected MISO line to scope and decoded it. It correctly contains my string. However storing it is another story. Since sending that dummy byte takes a clock cycle, my buffer is storing only the alternate characters. Both my boards only have a single data register for tx and rx on SPI. Any suggestions? \$\endgroup\$
    – Luffy
    Apr 5 '20 at 21:42
  • \$\begingroup\$ I don't get what goes wrong. Step 1 is both ends write data to data register. Real or dummy data. And slave must have a chance to write first because master writing to data register starts transfer. Step 2 is clocks happen to exchange the bytes. Step 3 both ends read data register. Repeat and go back to step 1 to transfer multiple bytes. Writing to data register can't happen during the transmission, maybe you need some delay between bytes. It may take longer than expected for the slave to react to SPI receive interrupt and load new data for transmission. \$\endgroup\$
    – Justme
    Apr 5 '20 at 22:03
  • \$\begingroup\$ @Justme hey sorry. It works just as you said, I made a small mistake in my code with respect to my master device since it uses the newer implementation of SPI controller (FIFO on data register). Thank you \$\endgroup\$
    – Luffy
    Apr 6 '20 at 8:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.