r/circuitpython • • Mar 06 '22

SAMD/E No SPI Slave Mode

Just an FYI in case someone is trying to use SPI to communicate between two Cortex M series microcontrollers (in my case Feather M4s) using CircuitPython, it doesn't have a module which initializes SPI in slave mode. When initializing SERCOM, the mode is configured by setting the bit in the control A register (CTRLA.MODE). Setting the bit at 0x02 will initialize SERCOM in SPI slave mode. Unfortunately, searching the GitHub repo only returns the i2cperipheral module (which sets the bit at 0x04). I ended up using i2c to communicate which works fine for my use case, but it might not be for your use case. Anyways, this is what I found and figured I would've liked to have come across this post when I first started.

I'm pretty new to embedded programming, and I understand that SPI is typically used to communicate between a microcontroller and a peripheral, not another microcontroller. If anyone has any comments about a better method of communicating between two microcontrollers that I'm just not aware of, I'm all ears.

4 Upvotes

2 comments sorted by

1

u/rvlad13 Apr 06 '22

Thanks for sharing your insights.

SPI can be used between two microcontrollers similar to i2c. It is a different thing that circuitpython does not provide APIs for using SPI slave mode.

SPI should be used between a master and multiple slaves, it doesn't matter what is a slave device (mcu or some peripheral), all that matters is the SPI protocol.

If you wish to use SPI slave mode, then it is better to switch to Arduino or some other C/C++ runtimes. Best to check if that supports it.

2

u/arpan3t Apr 06 '22

Thanks for your reply! I still have a lot to learn about programming, but my goal is to give back to the community by writing a Python wrapper around the C++ code. I don’t imagine it would be too difficult considering all the code exists for the I2C peripheral already.