Hardware SPI ~0.85ms delay before sending data

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
blark
Posts: 6
Joined: Fri Jan 10, 2020 3:08 pm

Hardware SPI ~0.85ms delay before sending data

Post by blark » Mon Jan 13, 2020 7:30 pm

I have a question about delays that I'm experiencing with hardware SPI. I have the following code which sends 16 bytes of data:

Code: Select all

vspi = SPI(2, 1000000, polarity=0, phase=0, bits=16, firstbit=0, sck=Pin(18), mosi=Pin(23), miso=Pin(19))
test = bytearray(16)
ss.off()
vspi.write_readinto((0xff).to_bytes(16, 'little'), test); 
ss.on()
Each time I send this sample frame it takes what seems an excessively long time to actually start the SPI transaction after bringing the SS line low. Logic analyzer screen cap:

Image

By comparison it only takes 78 uS to bring the SS line back up afterwards. Any ideas how I can speed this up? I need to send 512KB with SPI and a near 1ms delay for each transmission adds up.

Thanks!

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

Re: Hardware SPI ~0.85ms delay before sending data

Post by pythoncoder » Tue Jan 14, 2020 8:04 am

The ESP32 is not renowned for low latency but you might gain a little by cacheing the send bytes:

Code: Select all

wb = (0xff).to_bytes(16, 'little')  # Can be done once only
vspi = SPI(2, 1000000, polarity=0, phase=0, bits=16, firstbit=0, sck=Pin(18), mosi=Pin(23), miso=Pin(19))
test = bytearray(16)
ss.off()
vspi.write_readinto(wb, test); 
ss.on()
Peter Hinch

blark
Posts: 6
Joined: Fri Jan 10, 2020 3:08 pm

Re: Hardware SPI ~0.85ms delay before sending data

Post by blark » Wed Jan 15, 2020 4:21 pm

Not much improvement at all from caching bytes. I found another thread on this that states there is a lot of data checks and SPI setup going on in the background. It seems like something that could possibly be optimized.

I guess I will use large transfers (of about 1KB at a time) so I don't experience much overhead from SPI setup when transferring the data.

Post Reply