On the SPI wizard, I write a program that should interact with the slave: send and receive data. There are no problems with sending, but with reception there are unresolved, dimly lit questions in the literature.

The SPI master transmits a byte: a clock information signal. Slave sees the clock signal and starts receiving this byte. But if he has data to transmit, can he transmit it to the same clock signal? Will their controller take over? And if the slave does not transmit data at this moment, will the controller accept 0xff?

Now the situation is different. The slave has the data that it prepared for transmission in response to the master's request, and waits for a clock signal to transmit it. How does the clock signal appear on the line? Should master pass 0xff? After all, the master does not have a register with which it would be possible to issue a clock signal without data transmission, and reading the buffer register only returns the last received byte, and does not generate a clock signal.

But suppose the slave began transmitting data under the master clock signal. Will he accept 0xff at this moment?

Used controller 1982E92U (Milandr).

  • What you describe is configured by phase parameters. The protocol itself is synchronous. I recommend reading the specification for the SPI protocol (s) (in general there are several of them, you probably use the Motorola variant). - 0andriy
  • @ 0andriy, you added the label stm32 . Let the editing remain: this contributes to the detection of a question by specialists. But in general, the question does not apply to stm32. Controller Milandr. - maestro
  • @maesrto that way, I added the closest what it looks like. As far as I understand it, it is even in some sense an analogue (specifically BE9x) of some kind of controller from stm32. - 0andriy

1 answer 1

SPI - duplex thing. The master and the slave are determined only by those who form a clock signal and who adapts to it.

For the same clock signal, the device receives one byte and transmits the second. Moreover, the exchange over SPI in the simplest case is recorded in this way:

unsigned char SpiTransfer (unsigned char data) { регистр данных = data; while (идёт обмен) {} return регистр данных; } unsigned char SpiRead () { return SpiTransfer (0xFF); // передавать можно что угодно } void SpiWrite (unsigned char data) { SpiTransfer (data); } 

Accordingly, yes, if we need to read, we write an arbitrary value; if it is necessary to write, something is considered in parallel (zeros, ones or some kind of garbage - it depends on the implementation of the second device).

  • Write duplex , and an example for a half-duplex . Nestykovochka - 0andriy
  • @ 0andriy, why is there half duplex? In my opinion, the code here is just duplex, and the description below is half duplex. - maestro
  • spi_transfer () - duplex. This all ends. - 0andriy
  • one
    spi_transfer () - duplex. And thinking in most cases is half duplex. The simplest protocol: "send a command, get an answer" - half duplex in its purest form, and on SPI it will look like "send a command, throw out accepted garbage; send a byte, read the answer" - Alexey Esaulenko