cancel
Showing results for 
Search instead for 
Did you mean: 

stm32f7 as spi slave

EP.1
Associate

Hi. I am trying to work with AD7763. I have the same problem as in this article, but there are no answers in it. https://community.st.com/s/question/0D50X00009Xkfaw/spi-oneway-communication-slavemaster?t=1639997683527

So AD7763 is master and stm32f7 is slave. AD7763 after reset starting to generate continuous endless clock. To start the measurement I must as slave write start combination to control register, and I have 2 problems with it:

1) When I configure my stm32 as slave and send one byte with any send function (blocking, IT or DMA) it starts sending continuously, I think because of endless clock from AD7763. If I use __HAL_SPI_DISABLE(&hspi3) in HAL_SPI_TxCpltCallback, it manages to transmit 1 byte 8-10 times before stops. How can I transmit once with continuous clock?

2) AD7763 waits falling edge of write_data flag and start to read control data from stm32 on second falling clock adge. How can I do that? If I use GPIO reset function before SPI write function, time between falling edge of GPIO and spi data fronts always different and more than 1 clock of AD7763.

3 REPLIES 3
TDK
Guru

If you want to transmit 0x00 after the first byte, put that in the buffer to send out. So you could for example transmit 0xXX, then 31 0x00's, during which time you disable the peripheral. That will achieve your goal, but it seems like an odd thing to do. Probably more useful would be to transmit a circular buffer.

If you need finer control than HAL provides, you'll need to write your own driver. With high clock rates, it can be difficult to keep up.

If you feel a post has answered your question, please click "Accept as Solution".
KBuch
Associate II

" When I configure my stm32 as slave and send one"

This is not how SPI works. The slave cannot send on its own. It is always the master who initiates a data transfer (which better should be described as a data exchange. At the same time 1 byte is clocked into the slave, 1 byte is clocked from the slave to the master.

So in order to give the slave a change to transfer 1 byte, the master must transfer a byte (which is often just a dummy byte). To do that, the clock must be operating. Thus

" I think because of endless clock from AD7763"

yes. This is how SPI works. Now the question arrives: What should the slave transmit back while the master is clocking in the next data byte? The slave HAS TO SEND SOMETHING! The line MISO cannot hold no value. it must have some value. Thus the slave just clocks out the last data byte it had available. There is nothing the slave can do against it: If the master clocks, the slave has to put somthing on the MISO line.

To your question:

The SPI has a 4 Byte FIFO. You can use that

I haven't looked up the details of the protocol the AD7763 uses, but from the data sheet it seems

"Writing to these registers involves writing the register address first, followed by a 16-bit data-word"

Unfortunately the data sheet doesn not say if the register address is 1 byte or 2 bytes, but it seems like it is 1 byte. Which means for a complete "command" you need to transfer 3 bytes. Add an additional extre 0x00 byte and you have 4 bytes for the FIFO. The SPI will transmit all 4 of them in sequence, meaning that the last one will be the 0x00, which shouldn't have any meaning to the AD7763.

When the master continues clocking (and yours does), the slave will repeat the last byte it had, which is the 0x00 you put as the last byte into the FIFO:

(You do not have to take care of the FIFO, the hardware does this for you. You just put in the 4 bytes with eg HAL_SPI_Transmit which takes a pointer to the bytes and a length. So instead of feeding the SPI with just 1 byte you feed it with 4 bytes, which have carefully chosen such that the last byte equals 0x00)

SPI is tricky. But once you figured out that it is better to view it as "master and slave exchange bytes at the same time" instead of "sending and receiving" it is much better to understand what is going on. It is like 2 persons sitting opposite on a table. Each one has a piece of paper in front of him and if the master signals "exchange", then each person grabs his paper with the right hand and moves it to the other person which grabs it with his left hand while moving his paper in return at the same time. The slave has no way to stop that. When the master signals "exchange" than the paper has to be moved, no matter if the slave has already finished writing down whatver he wants or what he requests. The master initiates the transfer and there is nothing the slave can do about it.

The opposite is true also; If the master does not initiate an exchange, then the slave has no way of bringing information to the master. (And that is why the AD is clocking the whole time). This is why the term "transmit" in combination with "slave" is misleading. The slave cannot transmit on his own if the master does not transfer a byte to the slave.

EP.1
Associate

Thanks a lot, it seems that no problem to do so. The second question is with this diagram0693W00000HpHC4QAN.jpgFSI is output for stm, and it's not NSS at all. How to connect second falling clock edge with gpio?