Lacking support for multiple spi devices on the same bus

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
DanielRossinsky
Posts: 9
Joined: Fri Jun 12, 2020 4:04 am

Lacking support for multiple spi devices on the same bus

Post by DanielRossinsky » Sat Aug 15, 2020 2:58 pm

Important: The platform Im working on is the Doit esp32 devkit v1.

Iv'e come from a c++/Arduino background where if I want to use multiple spi devices on the same bus i just need to manage their slave select pin and use SPI.beginTransaction to set various speeds. However, in micropython there is no such thing as SPI.beginTransaction and I didn't find any custom drivers that ported the Arduino core SPI to micropython. Therefore, Im stuck with using init and deinit to imitate SPI.beginTransaction and SPI.endTransaction and the problem is it doesn't work. the sensors stop working completely.

As for the reason I need such functionality:
I want to power an ili9341 TFT LCD and use both its touch and graphics capabilities and a MAX7219 seven segment display. In arduino I could just assign them the same spi bus(MISO, MOSI, SCK) for example vspi (23,19,18) and manually enable them through their slave select pin. However I dont see it possible with micropython.

Iv'e attached two examples for the max7219 7 segment I've ported from c++ 1 working without imitating SPI.beginTransaction and the other tries to imitate SPI.beginTransaction and doesn't work at all, the seven segment doesn't even light up.

Am I missing something obvious ? or does such a feature not exist in micropython ?
Attachments
imitating.rar
(1.55 KiB) Downloaded 156 times
normal.rar
(1.54 KiB) Downloaded 181 times

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Lacking support for multiple spi devices on the same bus

Post by Roberthh » Sun Aug 16, 2020 11:26 am

I did not look into your files yet, having problems to unpack them. But there is a common trap in using spi.init() and other init calls. When you use that, you have to specify ALL non-default parameters again. Parameters which had been specified by the constructor will be overwritten with the defaults. That is caused by the implementation, which uses the same code both for the instantiation of the spi object and the init call. Same for UART, I2C, ....

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Lacking support for multiple spi devices on the same bus

Post by pythoncoder » Sun Aug 16, 2020 3:38 pm

The normal example is correct. The application instantiates the interface and passes the instance to the constructor of each object that uses it. This should "just work" so long as each device can share the same bus properties, with each device having a separate CS line.

If they can't share bus properties, management is required - this can be easy or quite hard depending on the application and how much concurrency is involved.
Peter Hinch
Index to my micropython libraries.

Post Reply