2 SPI devices at different speeds

I am successfully driving two devices off the M7’s SPI interface, the LCD shield and a third party device controlled from Pin 9. However, the other device operates at a maximum speed of 2MHz. Configuring the bus for 2MHz reduces the main-loop framerate to 5 fps as the LCD takes much longer to update. I’m not sure I have any options here. The PyBoard has two buses, but the M7 (and the upcoming H7) only has one. Can anyone see a way to get what I want out of this setup. The LCD needs to use SPI at its desired faster speed (whatever speed lcd.init() sets the speed to, I’m not sure) and the other device needs to use SPI at 2MHz. Am I out of the luck here or is there a solution to this problem?

Thank you.

Hi, just reinit the SPI bus when switching between the two devices. It doesn’t take much time to reinit it. Only a few clock cycles.

Um, I’m still working on upgrading find_blobs() but I should be done by the end of the week on it. Then I just have to get in some fixes for drawing ellipses and your rotated text request and I will be able to redo the LCD code then such that it doesn’t leave any traces behind that it uses the SPI bus. I.e. the redone code will capture the previous state, then set the bus to what it needs, send an image, and then restore the state.

I actually thought of that before writing my post, but the resulting framerate dropped from 5 to 2! It got considerably worse. In fact, even if I just call lcd.init() right before I call lcd.display() the framerate drops to 2, even if I don’t bother setting the bus to the slower speed in between frames. Weird.

Are you sure you would expect that to work? I could try again.

BTW, the rotated text wasn’t an explicit coding or effort request, although I appreciate your diligence. I was asking what was supported at the time, but not trying to make extra work for you.


Oooookay. I see. Instead of calling lcd.init() every time, I directly reset the bus speed. That actually seems to work:

    pyb.SPI(2).init(pyb.SPI.MASTER, baudrate=15000000, polarity=0, phase=0)
    pyb.SPI(2).init(pyb.SPI.MASTER, baudrate=2000000, polarity=0, phase=0)

That leaves one question remaining. What is the correct bus speed to set it to for the LCD. I just “narrowed in” on 15MHz by finding the speed that reproduced the original framerate, but that probably isn’t exactly correct. What speed would the LCD like me to set the bus to?

About 20 MHz. Don’t worry however, the M7 can’t actually do any freq. SO, it sets it to a freq below what you want to hit normally however.