FYI, I modified the python xpt2046 driver to perform a “read” (get X Y coords of a click) using a single SPI transaction and using the “16 cycle overlapped” mode. For this I had to switch to “full duplex” SPI transactions. Well, that didn’t go so well…
After more than a day of banging my head against the wall I discovered that the problem is a bug in esp-idf. Specifically, the ili9341 display uses half duplex transactions and that causes all full duplex transactions on the same bus to break: they run correctly looking at the pins but the data sent by the slave doesn’t show up in the rx buffer (under some circumstances the first 4 bytes do show up). Note that the esp-idf docs have some warning that half duplex and rx has bugs…
After discovering all this in test cases I switched the display driver to run full-duplex transactions (it’s just a matter of commenting out the "flags": esp.SPI_DEVICE.HALFDUPLEX line) and now everything works. Argh!
It might be worth making this change in the display driver so others don’t hit similar bugs… I don’t see any downside.
In a nutshell, Full-duplex transactions are not compatible with the dummy bit workaround and limit SPI speed to 26MHz.
On the other hand, half-duplex with the dummy bit workaround allow speeds up to 80MHz. I usually use the display driver at 40MHz.
It’s possible, however, to add another parameter to ili9341 constructor to configure it as full-duplex when needed, if it helps to solve some problems and when the SPI speed is less important.
If you want to help, we always welcome PRs!
The problem I was trying to solve was that it wasn’t working at all. I couldn’t tell whether it was a software or hardware issue so I read the datasheet, looked at other drivers, and rewrote. In the end the real problem turned out to be that two pins on the touch controller chip were shorted and the flex cable to the touch panel was also improperly soldered. I suppose the module I got is a reject from some manufacturing run… Sigh.
In the end, the new scheme has a bunch of benefits:
it runs a single transaction to read x, y, and pressure, which is faster
it provides extended settling time as described in the original burr brown app note, and I see experimentally that it helps
it’s less code and simpler code
Thanks for pointing this out. I’ve read the dummy bit workaround 4 times now and I still don’t understand what that really does. I understand the problem, just not the workaround…
But how is this relevant for the display given that nothing is being read? Is it that esp-idf doesn’t allow you to configure the clock to 40mhz?
Dummy bit workaround : Dummy clocks, during which the Host does not read data, can be inserted before the read phase begins. The Device still sees the dummy clocks and sends out data, but the Host does not read until the read phase comes. This compensates for the lack of the MISO setup time required by the Host and allows the Host to do reading at a higher frequency.
So it seems that ESP32 has some timing issue, and esp-idf automatically solves this by either adding the dummy bit in half duplex mode, or by limiting the SPI frequency.
Since the ili9341 only writes, as you mentioned, another option is to try enabling the SPI_DEVICE_NO_DUMMY option in full duplex mode:
In full-duplex mode, however, the hardware cannot use dummy bits, so there is no way to prevent data being read from getting corrupted. Set this flag to confirm that you’re going to work with output only, or read without dummy bits at your own risk.
I wanted to make a quick test comparing your driver to mine.
I’ve set ili9341 driver to full duplex and tried your version of xpt2046 - it doesn’t work for me.
Initialization of both ili9341 and xpt2046 is completed, but something hangs afterwards, no display and no Micropython REPL.
I haven’t really tried to debug it, maybe if I find some time I’ll try later.
Is this a known bug in esp-idf? Could you point me to the bug report?
Or is this something you discovered when working with ESP32 SPI?
I found out that running my version of xpt2046 (half duplex) with ili9341 full duplex works for me, so for some reason I’m not experiencing this bug. (esp-idf v4.0.1)