Trying to use external SPI device + lvgl

In the last couple days I have been trying to use a Max31865 ( with a m5stack with micropython and lvgl.
Everything works in separate but when I try to use them together the problems start.
I am getting SPI erros and haven’t found a way to make it work.
I tried different max31856 libs, and different ways of initializing the SPI in my main code.

Following the tips that I have found in several posts about SPI and SDcard compatibility my last error was:

E (2880) spi_master: check_trans_valid(801): txdata transfer > host maximum

and my current test code is:

import lvgl as lv
from ili9XXX import ili9341

import utime
from machine import I2C, Pin, DAC, PWM, SPI
from max31856 import Max31856

tc_type = “K”
spi = SPI(1, baudrate=1000000, polarity=1, miso = Pin(19, Pin.IN), mosi = Pin(23, Pin.OUT), sck = Pin(18, Pin.OUT))
cs = Pin(5, Pin.OUT)
max31856 = Max31856(spi, cs, tc_type)

disp = ili9341(spihost=1,miso=-1,mosi=-1,clk=-1)


tc = max31856.temperature(read_chip=True)
cj = max31856.cold_junction()
f, fs = max31856.faults()
tcs = '{:7.2f}'.format(tc)
cjs = '{:7.2f}'.format(cj)
print('Temperatures:', tcs, cjs)  

What is the best way to integrate an SPI device with lvgl? I would be much grateful if someone could point me in the right way!

Hi @Tiago_Almeida!

If I understand correctly, you are trying to use your ILI9341 and MAX31856 on the same SPI bus.
I noticed that you let micropython SPI driver initialize the bus first, and then use the initialized bus with ILI9341. Actually I never tried this because my SPI devices work with ESP-IDF SPI API directly and not with Micropython’s SPI driver.

One simple solution could be to put them each on its own SPI bus.
If you need to use them on the same SPI bus, it may still be possible if both devices behave correctly and if the drivers support this.
I’m using ILI9341 and XPT2046 on the same SPI bus with LVGL and it works fine.

You can try tweaking some parameters, for example use ILI9341 in full-duplex mode instead of half duplex etc.
You can examine ILI9341 driver and Micropython SPI driver, specifically the SPI initialization code, and see if they configure SPI differently.

The error you are getting (txdata transfer > host maximum) hints that the SPI bus initialized by Micropython for MAX31856 does not set max_transfer_sz high enough.
Looking at Micropython SPI driver code, it does not set max_transfer_sz at all, so it gets the default value of 4094 which might not be high enough. You can try setting max_transfer_sz in machine_hw_spi.c to a high enough value.
From the other side, you can also try to set the factor argument of ILI9341 to higher values (64 for example) so that ILI9341 driver would allocate a smaller TX buffer, however this could affect the display performance.

If the problem is not the initialization but the actual SPI communication, you can try to use a scope or some logic analyzer equipment to see what’s happening on the SPI lines.

1 Like

@amirgon … This might sound stupid but I don’t even know how to thank you… I am finally finishing a looong college project and using this chip and library was a last minute change that I had to make, wasn’t planning on using SPI, hence haven’t tested it in the beginning .

I started by the easy stuff and changing factor to 64 made it work (as far as I noticed), screen refreshes and temperature updates! I have tried before using a factor of 8 and 16 but for some reason didn’t tried higher…
Don’t know if it will affect the rest of the project, but at least looks fine by now and its progress!

For future reference:

I tried to make them use different SPI buses but I wasn’t successful, they would initialize but screen would then freeze with no error.

Currently I have 2 setups, one working in full duplex mode and one in half duplex, further tests are needed but the one in full duplex looks like it’s working fine, the one in half duplex bugs and doesn’t refresh screen and the readings are all wrong. So I guess we need to work in full duplex.

If anyone needs in the future this is the library I am using

Once again thank you so much @amirgon, not just for this time but for all the others you helped me get out of a hole!

You are welcome! :smile:
It’s great that it is working for you!

If I find some time I would send a PR to micropython to improve their ESP32 SPI driver.
Their SPI driver should be able to set max_transfer_sz, and it should handle cases where another driver already initialized the bus. That would allow it to work smoothly with LVGL drivers such as ILI9341 and XPT2046.