SPI slave problem

Hi, trying to run the following script, renders the openmv H7 hardware unusable.

print('First line')
from pyb import SPI
spi = SPI(2, SPI.SLAVE, polarity=0)
ch = bytearray('ab')
res = spi.send_recv(ch, timeout=1)
print(res, ch)

When I try to run the script above, the hardware “appears” to hang.
I don’t even see the first line printed.
The only way to get back to interacting with the board is to power cycle it.

If I remove the spi.send_recv call then the hardware does not freeze and the script runs to completion.

I have nothing connected to the SPI pins.

Any ideas?

Yeah, what happens is that in slave mode the board needs to receive a clock from the SPI master. Without that clock the MCU will wait on the SPI receive call to return. The timeout should causes the system to throw an exception however if no clock was being received.

Um, I don’t recommend using the OpenMV Cam as a SPI or I2C slave. I tried this long ago and determined it to be a bad path to go down and a waste of your time. Make it a master device.

The hanging part implies there’s another bug however. Did you run this in the IDE? Does the USB disconnect after running the script?

Yes, I run this from the IDE.
The IDE appears to hangs for 10-20 seconds and then eventually it comes back and shows the device as disconnected (I imagine something times out).

Yeah, that’s the IDE timing out. Okay, there’s probably a bug in the SLAVE mode. It should just throw an exception on board timeout. Instead it’s getting stuck in an infinite loop.

Sorry about this… Whenever we update MP (like to version 1.11 with our latest firmware) various things break.

Hi, there’s an SPI example with the Arduino as the master device and the OpenMV Cam as the slave in Examples->00-Arduino/arduino_spi_slave.py that has been tested and know to work with the latest firmware.

Hi, I know that it’s 2 years later, but I hope you will answer. Do you still don’t recommend using OpenMV as SPI slave? And if so, could you tell me why?
I want to use my H7 cam as a slave for STM32F407.
Thanks.
~Maria

We fixed the SPI slave issues. It works fine for this. See the RPC library on the camera for how to use the SPI slave mode.

In particular, please use the RPC library for chip to chip communication. We developed it to make what you are trying to do easy.

1 Like

That’s great news! Thanks! :slight_smile: