How to restore the settings of SPI interface? #15214
-
I have a SPI device connected to my RPI Pico (rfm transciever). The user may connect another SPI device which may use different SPI settings (baudrate...). When communicating with the specific module, I may have to setup different baudrate. I came with an idea to have SPI context manager which would revert SPI settings back as they were before using the SPI interface... Something like:
I'm hw newbie so please be kind to me :) I saw that several drivers in Arduino are doing this like: https://github.com/sparkfun/RFM69HCW_Breakout/blob/master/Libraries/Arduino/RFM69/RFM69.cpp#L440 The micropython library for SDCard (https://github.com/micropython/micropython-lib/blob/master/micropython/drivers/storage/sdcard/sdcard.py#L56) seems to just reconfigure the interface and leave it that way. What is the recommended practice for this? I tried to implement the context manager, but realized that:
The firstbit is not supported as per #11404 Can this be related?
Another approach may be to setup the required SPI settings right before using it and leave it in this state, but this looks incomplete to me. How do you guys deal with this? Thank you |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
There are a couple of attempts at an RFM69 library already out there:
|
Beta Was this translation helpful? Give feedback.
-
@mishal There are a number of ways of approaching this. One approach I use is that a device driver constructor takes a 2-tuple arg. Elements are as follows:
Whenever the device driver needs to use the SPI it does something like spi = arb[0]
spi.init(**arb[1]) # Init works fine with a subset of args
cs(0) # Assert the CS\ line (possibly after a brief delay)
# Do the transfers
cs(1) I don't interrogate the SPI bus as every device "knows" what settings it needs. |
Beta Was this translation helpful? Give feedback.
@mishal There are a number of ways of approaching this. One approach I use is that a device driver constructor takes a 2-tuple arg. Elements are as follows:
dict
containinginit
args e.g. {"baudrate": 1_000_000, "polarity": 1}Whenever the device driver needs to use the SPI it does something like
I don't interrogate the SPI bus as every device "knows" what settings it needs.