Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix SPI when used with PSRAM #3339

Closed
emard opened this issue Aug 27, 2020 · 18 comments · Fixed by #3393
Closed

Fix SPI when used with PSRAM #3339

emard opened this issue Aug 27, 2020 · 18 comments · Fixed by #3393
Assignees
Labels
bug espressif applies to multiple Espressif chips

Comments

@emard
Copy link

emard commented Aug 27, 2020

I have ported a small project for ECP5 JTAG programmer
from micropython to circuitpython:
https://github.com/emard/esp32ecp5/tree/master/circuitpython

Last known good circuitpython that runs it correctly was this:

adafruit-circuitpython-espressif_saola_1_wrover-en_US-20200827-fe73cfb.bin

In later versions 2MB PSRAM support appears which is absolutely great,
but now SPI is either not working or has issues if initialized and deinitialized often.

This project needs to switch between bitbanging of JTAG pins and
driving the same pins by hardware SPI for speed. JTAG traffic
doesn't always consist of multiple of 8 clocks so SPI can't be constantly
used.
During the switch from bitbanging to SPI and back,
glitches appear (pins change state while
we didn't want them to).

Some glitches are tolerable and some must be workarounded so
the project is sensitive to what is done with the pins.

@tannewt
Copy link
Member

tannewt commented Aug 27, 2020

What happens with SPI? It's possible the external RAM prevents DMA.

(I have a JTAG library started here: https://github.com/adafruit/Adafruit_CircuitPython_JTAG based on tinyfpga's programmer for the Mach XO2)

@emard
Copy link
Author

emard commented Aug 27, 2020

I'd say SPI doesn't work at all now :) but I don't know the details,
for this I must analyze and investigate more.
Apart from glitches, what I see currently is that SPI doesn't send correct
data I pass it (probably it sends all 00 or FF) and reads all 0. Only the
bitbanging part is still working.

This project esp32ecp5 is in use whole year for micropython ESP32-WROOM for
ECP5 and now I got it also to work on ESP32-S2, almost at the same speed as
on micropython ESP32-WROOM (dual core).

@tannewt tannewt added bug espressif applies to multiple Espressif chips needs retest labels Aug 28, 2020
@tannewt tannewt added this to the 6.x.x - Bug Fixes milestone Aug 28, 2020
@tannewt tannewt changed the title ESP32-S2: Is SPI still working? Confirm SPI works. Aug 28, 2020
@tannewt
Copy link
Member

tannewt commented Aug 28, 2020

Ok, we'll need to retest SPI. Please let me know if you have a simple test case that fails.

@emard
Copy link
Author

emard commented Aug 28, 2020

SPI SD card won't mount.
I tried SD card with:
adafruit-circuitpython-espressif_saola_1_wrover-en_US-20200827-fe73cfb.bin
adafruit-circuitpython-espressif_saola_1_wrover-en_US-6.0.0-alpha.3.bin
the code that is trying to mount the card:
https://github.com/emard/esp32ecp5/blob/master/circuitpython/sdmount.py#L1
output:
import sdmount
Traceback (most recent call last):
File "", line 1, in
File "sdmount.py", line 30, in
File "adafruit_sdcard.py", line 116, in init
File "adafruit_sdcard.py", line 177, in _init_card
File "adafruit_sdcard.py", line 145, in _init_card
OSError: no SD card

@emard
Copy link
Author

emard commented Aug 29, 2020

Another even simpler test: SPI LOOPBACK, connect MISO-MOSI IO35-IO37

adafruit-circuitpython-espressif_saola_1_wrover-en_US-20200827-fe73cfb.bin passed this test

adafruit-circuitpython-espressif_saola_1_wrover-en_US-20200829-d858d04.bin
fails this test

https://github.com/emard/esp32ecp5/blob/master/circuitpython/spiloopback.py#L1

fails as functions spi.readinto(rx) or spi.write_readinto(tx,rx)
don't change content of rx bytearray buffer.

@tannewt
Copy link
Member

tannewt commented Aug 31, 2020

@emard Can you confirm it works for you with psram disabled? I think we're trying to dma from memory that can't be used for DMA.

@emard
Copy link
Author

emard commented Aug 31, 2020

where is download for non-PSRAM circuitpython?

I have only WROVER-S2 with PSRAM and
circuitpython binary automatically enables it.

@tannewt
Copy link
Member

tannewt commented Aug 31, 2020

@emard try loading the WROOM version. It should work just fine and not use the PSRAM.

@emard
Copy link
Author

emard commented Aug 31, 2020

spi loopback works with

adafruit-circuitpython-espressif_saola_1_wroom-en_US-20200831-877a4f4.bin

the same example that works for non-PSRAM version,
doesn't work with PSRAM version

but I can't test real thing ecp5.py because
out of memory at wroom circuitpython

@tannewt
Copy link
Member

tannewt commented Sep 1, 2020

Ok, thanks for testing this! I've gotta decide how to fix it. We want bytearrays in PSRAM by default but need them in RAM when sending them to SPI.

@emard
Copy link
Author

emard commented Sep 1, 2020 via email

@tannewt
Copy link
Member

tannewt commented Sep 2, 2020

Do you reuse the buffer? Would it work if we moved the bytearray into RAM when first used for DMA?

@emard
Copy link
Author

emard commented Sep 2, 2020 via email

@tannewt
Copy link
Member

tannewt commented Sep 2, 2020

Reusing it is definitely the way to go.

I don't think there is a way to currently get this to work. It requires some changes under the hood to move memory as needed. Hopefully it'll just work in the future with a copy cost on first DMA.

@emard
Copy link
Author

emard commented Sep 3, 2020 via email

@emard
Copy link
Author

emard commented Sep 7, 2020

I tried current binary but it still doesn't work,
seems SPI-RAM-DMA fixes are not yet there,

I have tried this:

adafruit-circuitpython-espressif_saola_1_wrover-en_US-20200906-a9779b9.bin

@tannewt tannewt self-assigned this Sep 8, 2020
@tannewt
Copy link
Member

tannewt commented Sep 8, 2020

My plan is to work on it tomorrow. I'll update here when a fix is created.

@tannewt tannewt changed the title Confirm SPI works. Fix SPI when used with PSRAM Sep 10, 2020
@tannewt
Copy link
Member

tannewt commented Sep 10, 2020

Ok, I've confirmed its very fast at transmitting zeroes. :-) Working on a fix now.

tannewt added a commit that referenced this issue Sep 17, 2020
This fixes SPI with PSRAM allocated buffers. DMA with SPI2 was
attempted but produced junk output. This manual copy is less than
2x slower than DMA when not interrupted.

Fixes #3339
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug espressif applies to multiple Espressif chips
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants