What are ESP32 SPI Ports for Micropython SPI(1)?

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
choies
Posts: 8
Joined: Sat Jan 30, 2016 11:06 am

What are ESP32 SPI Ports for Micropython SPI(1)?

Post by choies » Sat May 20, 2017 4:23 pm

I make bin files for ESP32 module( ESP-WROOM-32)
Now I would like to test hardware SPI function using Micropython.

Please let me know the Hardware SPI port in ESP32 module( ESP-WROOM-32) for Micropython code?

For example.
1) SPI(1)
SPI_MISO = HSPIQ(GPIO12)
SPI_MOSI = HSPID(GPIO13)
SPI_CLK = HSPICLK(GPIO14)
SPI_CS = HSPICS0(GPIO15)

Thanks in advanced.

Damien
Site Admin
Posts: 647
Joined: Mon Dec 09, 2013 5:02 pm

Re: What are ESP32 SPI Ports for Micropython SPI(1)?

Post by Damien » Tue May 23, 2017 12:14 pm

The default SPI ports are defined by the ESP32 module itself (and actually the ESP IDF is doing all the low-level work here to configure the pins). If you don't provide sck/mosi/miso pins for a peripheral then the "most recent" pins are used, which is usually going to be the default pins for that peripheral.

According to the ESP IDF, the default pins for SPI(1) are: sck=Pin(6), mosi=Pin(8), miso=Pin(7). For SPI(2) it's: sck=Pin(14), mosi=Pin(13), miso=Pin(12). In MicroPython the CS pin must be handled by user code, it's not controlled by the hardware peripheral.

Capstan
Posts: 117
Joined: Sun Jan 29, 2017 4:03 pm
Location: Texas, USA

Re: What are ESP32 SPI Ports for Micropython SPI(1)?

Post by Capstan » Mon Dec 04, 2017 10:02 pm

Damien wrote:
Tue May 23, 2017 12:14 pm
According to the ESP IDF, the default pins for SPI(1) are: sck=Pin(6), mosi=Pin(8), miso=Pin(7).
Is this correct? I am looking at the WROOM-32 datasheet and also at the programming guide;
http://espressif.com/sites/default/file ... eet_en.pdf
https://esp-idf.readthedocs.io/en/v2.0/ ... aster.html

"The ESP32 has four SPI peripheral devices, called SPI0, SPI1, HSPI and VSPI. SPI0 is entirely dedicated to the flash cache the ESP32 uses to map the SPI flash device it is connected to into memory. SPI1 is connected to the same hardware lines as SPI0 and is used to write to the flash chip. HSPI and VSPI are free to use."

and...
"Host: The SPI peripheral inside the ESP32 initiating the SPI transmissions. One of SPI, HSPI or VSPI. (For now, only HSPI or VSPI are actually supported in the driver; it will support all 3 peripherals somewhere in the future.)"

This seems to imply that we should use only HSPI and VSPI for driving SPI peripherals? The datasheet looks like it is showing the VSPI pins as IO19(VSPIQ or MISO), IO23(VSPID or MOSI), and IO18(VSPICLK). The HSPI pins show as IO12(HSPIQ or MISO), IO13(HSPID or MOSI), and IO14(HSPICLK).

The micropython documentation for SPI is here;
http://docs.micropython.org/en/v1.9.3/p ... b.SPI.html

The SPI constructor mentions SPI(1) and SPI(2). The SPI(2) pins appear to correspond with VSPI. I would have thought SPI(1) pins correspond with HSPI but they don't.

Confusing, can someone clear this up?

loboris
Posts: 344
Joined: Fri Oct 02, 2015 6:19 pm

Re: What are ESP32 SPI Ports for Micropython SPI(1)?

Post by loboris » Mon Dec 04, 2017 11:45 pm

ESP32 SPI peripheral can be used with any GPIO via GPIO matrix (input only GPIOS can be used only for miso function).

Native SPI pins are (clk, mosi, miso, cs):
  • SPI1: 6, 8, 7, 11
  • HSPI: 14,13,12,15
  • VSPI: 18,23,19, 5
If using native pins maximum SPI clock can be set to 80 MHZ.
If using the pins routed via gpio matrix, maximum spi clock is 40 MHz in half-duplex mode and 26 MHz in full duplex mode.

Only HSPI & VSPI are supported by esp-idf driver.
Additionally, if psRAM is used with ESP32, and is set to run at 80MHz, VSPI is not available to the user, as it is used by psRAM driver.

So, in most cases (if you don't need the speed >26 MHz), you can use any pin for miso and any input/output pin for mosi, clk & cs.

Capstan
Posts: 117
Joined: Sun Jan 29, 2017 4:03 pm
Location: Texas, USA

Re: What are ESP32 SPI Ports for Micropython SPI(1)?

Post by Capstan » Tue Dec 05, 2017 12:10 am

loboris wrote:
Mon Dec 04, 2017 11:45 pm
ESP32 SPI peripheral can be used with any GPIO via GPIO matrix (input only GPIOS can be used only for miso function).

So, in most cases (if you don't need the speed >26 MHz), you can use any pin for miso and any input/output pin for mosi, clk & cs.
Good to know, thanks. But can you access selected pins with the API? According to the docs it looks like you can choose one of two 'bus' options with the SPI constructor;

The physical pins of the SPI busses are:

SPI(1) is on the X position: (NSS, SCK, MISO, MOSI) = (X5, X6, X7, X8) = (PA4, PA5, PA6, PA7)
SPI(2) is on the Y position: (NSS, SCK, MISO, MOSI) = (Y5, Y6, Y7, Y8) = (PB12, PB13, PB14, PB15)

How would you specify a different set of pins?

Also curious to know if any of the lines require pull-up resistors similar to I2C?

SpotlightKid
Posts: 463
Joined: Wed Apr 08, 2015 5:19 am

Re: What are ESP32 SPI Ports for Micropython SPI(1)?

Post by SpotlightKid » Tue Dec 05, 2017 1:14 am

The docs you quite refer to the Pyboard, not the ESP8266 or EPS32.

The SPI lines managed by the driver (i.e. SCK, MISO and MOSI) do not require pull-ups, they always have a defined state.

Capstan
Posts: 117
Joined: Sun Jan 29, 2017 4:03 pm
Location: Texas, USA

Re: What are ESP32 SPI Ports for Micropython SPI(1)?

Post by Capstan » Tue Dec 05, 2017 1:29 am

Ah, didn't catch that. Is the ESP32 API like the ESP8266 API? If so you can put pin designations in the constructor.

https://docs.micropython.org/en/latest/ ... achine.SPI

OutoftheBOTS_
Posts: 847
Joined: Mon Nov 20, 2017 10:18 am

Re: What are ESP32 SPI Ports for Micropython SPI(1)?

Post by OutoftheBOTS_ » Mon Dec 18, 2017 9:02 am

I have just changed over from ESP8266 to the ESP32. I have a Wrover board with 4mb SPI flash and 4MB SPI Ram.

I am playing with a ili9341 SPI screen. I can get it to work using any GPIO pins using software SPI and am suprised by the speed of the software SPI on the ESP32 compared to the very slow software SPI on the ESP8266

I have hooked the ili9341 to the native pins as listed above in this thread but if I try to use bus1 or bus2 it gets an error saying they r already in use (i assume the SPI RAM is using these 2 buses)

Will I be getting the full speed of 80Mhz if using these pins even though I don't specify a hardware bus ??

wallaceowen
Posts: 2
Joined: Wed Jan 03, 2018 4:00 am

Re: What are ESP32 SPI Ports for Micropython SPI(1)?

Post by wallaceowen » Wed Jan 03, 2018 4:05 am

Unlike AVR chips, specifying the pin doesn't mean you are using a 'software' spi. The ESP32 can map SPI to the pins you specify, as long as the pins you specify are capable of the job you want them to do.

Quoting Loboris: "ESP32 SPI peripheral can be used with any GPIO via GPIO matrix (input only GPIOS can be used only for miso function)."

mosi
Posts: 28
Joined: Tue Oct 07, 2014 12:07 am

Re: What are ESP32 SPI Ports for Micropython SPI(1)?

Post by mosi » Sun Mar 11, 2018 10:47 am

Any idea on ESP32 WROVER what is wrong with the SPI Init?
This is loboris Micropython build with ESP32 and March 2017 date.
Tried to remove the SD card interface, as it is on SPI, no luck.

Code: Select all

from machine import Pin, SPI
sck=Pin(18)
miso=Pin(19)
mosi=Pin(23)
dc=Pin(32)
cs=Pin(33)
spi = SPI(1, baudrate=32000000, sck=sck, miso=miso, mosi=mosi)
>>> spi = machine.SPI(2, baudrate=32000000, sck=sck, mosi=mosi, miso=miso)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: SPI host must be HSPI(1), VSPI(2) used by SDCard driver

>>> spi = machine.SPI(SPI.VSPI, baudrate=32000000, sck=sck, mosi=mosi, miso=miso)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: SPI host must be HSPI(1), VSPI(2) used by SDCard driver

>>> spi = machine.SPI(SPI.HSPI, baudrate=32000000, sck=sck, mosi=mosi, miso=miso)
E (703027) gpio: gpio_set_direction(247): GPIO number error
E (703028) gpio: gpio_set_level(189): GPIO output gpio_num error
E (703030) spi_master: spi_bus_add_device(221): no free cs pins for host
E (703037) [SPI_UTILS]: Adding spi device failed with rc=0x105
E (703044) spi_master: spi_bus_free(178): not all CSses freed
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OSError: no free slots


E (31511) gpio: gpio_set_direction(247): GPIO number error
E (31512) gpio: gpio_set_level(189): GPIO output gpio_num error
D (31515) intr_alloc: Connected src 30 to int 9 (cpu 1)
>>> spi = SPI(2, baudrate=32000000, sck=sck, miso=miso, mosi=mosi)
E (49901) gpio: gpio_set_direction(247): GPIO number error
E (49901) gpio: gpio_set_level(189): GPIO output gpio_num error
E (49903) spi_master: spi_bus_initialize(94): dma channel already in use
E (49910) [SPI_UTILS]: spi bus initialization failed with rc=0x103
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OSError: invalid state


Now trying to put other pins:
>>> sck=Pin(25)
>>> miso=Pin(26)
>>> mosi=Pin(27)
>>> spi = SPI(1, baudrate=32000000, sck=sck, miso=miso, mosi=mosi)
E (240684) gpio: gpio_set_direction(247): GPIO number error
E (240684) gpio: gpio_set_level(189): GPIO output gpio_num error

Maybe SPIram / flash is using the DMA and not letting second SPI initialize?
Internal FS (SPIFFS): Mounted on partition 'internalfs' [size: 1048576; Flash address: 0x1F0000]
----------------
Filesystem size: 956416 B
Used: 512 B
Free: 955904 B
----------------

FreeRTOS running on BOTH CORES, MicroPython task running on both cores.
Running from Factory partition starting at 0x10000, [MicroPython].

Reset reason: Power on reset
uPY stack: 19456 bytes
uPY heap: 3073664/6096/3067568 bytes (in SPIRAM using heap_caps_malloc)

MicroPython ESP32_LoBo_v3.1.21 - 2017-03-03 on ESP32 board with ESP32
Type "help()" for more information.

Post Reply