Repository navigation
Add hardware SPI support on ESP32 - #1047
Conversation
|
Hi Nick! Thanks for the contribution. I've got a couple of questions for you before we merge it in. |
|
Hi Sam, glad to give back to this project. I've used it for years. To answer your questions:
|
|
Personally with the ESP8266, I do not have a logic analyzer but the SPI change was the only way to be able to drive my LEDs, it wans't only laggy, the behavior of the bit bangging was random (interupted animation wrong colors, flickering etc, there is reports of such issues on YouTube, SPI implementation resolved them all in my case) Maybe the bit banging tend to have clocking speed issue and "dephasing" on some hardwares when there is too much LEDs or a too high refresh frequency (just presuming) |
|
What compiler are you using? I was very excited about trying this out, but I can't get it to compile. The first error I see is Looking at this, I can see that mSPI is an instance variable. Changing the code to I'm using the Arduino IDE from the Windows Store, 1.8.13 (Windows Store 1.8.42.0). I'm not sure what C compiler is used; it does not tell me. |
|
Hi @endotronic, that's strange. I have no issues compiling with defaults on Arduino IDE 1.8.13 for MacOS, 1.8.13 for Ubuntu, or PlatformIO 4.3.4. Both Arduino and PIO are using the standard Xtensa GCC compiler for ESP32 setup. I don't have a windows machine to test with unfortunately. If you enable verbose logging during compilation the Arduino IDE will show you the path to the compiler. Mine looks like this: |
|
Thanks for pointing that out. I forgot to mention that I'm using an ESP8266 and was hopeful that these changes would work with that processor as well, given the comment by BenoitAnastay. With verbose output, I see this path for gcc (for ESP8266): It makes sense it wouldn't be the same as the one for ESP32. I will explore this more in a bit. |
|
Ah, yeah this won't compile for the ESP8266 without some changes, unfortunately. In the FastLED SPI template it's looking for the board to be defined as ESP32. The ESP32 has 2 SPI buses, and this is passing a pointer in the ESP32 specific driver to initialize the SPI setup on one or the other. |
|
Ah gotcha. Thanks. I will probably get an ESP32 then. :) |
|
@endotronic The PR for the ESP8266 is here #936 |
|
I really appreciate you adding this support @ntwallace Part of the problem is that The Arduino IDE compiles and links all the source files at the same time, but PlatformIO compiles source objects individually, so each file is compiled in separate commands, then in the link step combines everything. This allows for fast incremental builds and is a much more common practice for C/C++ development. It might be why other compilers are having issues as per comments above. So first build has many errors like: If I add I think EDITI moved |
|
It's extremely easy to use PlatformIO. I'm developing using SparkFun ESP32 "Thing" and the only thing needed to test your library fork is to use VSCode with PlatformIO extension installed and this platformio.ini file: check out platformIO available boards if you're using another one beside the SparkFun "Thing" |
|
Two questions: One, does this work well with multiple SPI pins, or does it gracefully handle if the ESP32 runs out of SPI pin mappings? Two, does this have a chance of getting merged before the holidays, so I can drive my many, many strings of LEDs? :) |
|
Just wanted to say THANK YOU @ntwallace , you've just saved my life with this change ! |
|
@ntwallace I'm looking at this PR, and I'd like to merge it as soon as possible. Two questions: first, do we need to add instructions on how to set up the SPI library? Can we guard the code that won't compile with #ifdefs? Second, is there any issue with the limited number of SPI busses? |
|
@ntwallace question on the SPI bus, if I am reading the ESP32 datasheet right, it looks like you can use any GPIO pin for the SPI unless you need parallel SPI and then it has some pin restrictions. I'm still getting things started for my project but any thoughts on if the general purpose SPI is sufficient for driving the APA102s, if so how could someone override the predefined pins? |
|
@ntwallace We finally merged your SPI work! Thank you so much for this contribution. |
|
@ntwallace could you add documentation for how this should be used? I added |
|
@stickperson add |
|
Maybe this will be convenient for others; an example of using the above for non-default SPI pins, with FastLed |
|
@Benik3 Should note that it must be added in a particular file (see @ChisholmKyle's comment earlier); adding it to your own code does bupkis. And even then I'm getting the linker error he mentioned as well. Unfortunately something must have changed between this PR and 3.5.0 because now even his additional changes aren't working for me. @gitTinker WS2811s don't use SPI, though. |
|
I was attempting this "Hardware SPI driver for ESP32,". |
|
@gitTinker, I think the pins you are using for SPI do not interface with the higher-speed
It's kinda tedious to figure out what pins are supported by IO_MUX, what their names are, and how accessable they are on each particular esp32 board. This header file is helpful #pragma once
#define SPI_FUNC_NUM 1
#define SPI_IOMUX_PIN_NUM_MISO 7
#define SPI_IOMUX_PIN_NUM_MOSI 8
#define SPI_IOMUX_PIN_NUM_CLK 6
#define SPI_IOMUX_PIN_NUM_CS 11
#define SPI_IOMUX_PIN_NUM_WP 10
#define SPI_IOMUX_PIN_NUM_HD 9
//For D2WD and PICO-D4 chip
#define SPI_D2WD_PIN_NUM_MISO 17
#define SPI_D2WD_PIN_NUM_MOSI 8
#define SPI_D2WD_PIN_NUM_CLK 6
#define SPI_D2WD_PIN_NUM_CS 16
#define SPI_D2WD_PIN_NUM_WP 7
#define SPI_D2WD_PIN_NUM_HD 11
#define SPI2_FUNC_NUM HSPI_FUNC_NUM
#define SPI2_IOMUX_PIN_NUM_MISO HSPI_IOMUX_PIN_NUM_MISO
#define SPI2_IOMUX_PIN_NUM_MOSI HSPI_IOMUX_PIN_NUM_MOSI
#define SPI2_IOMUX_PIN_NUM_CLK HSPI_IOMUX_PIN_NUM_CLK
#define SPI2_IOMUX_PIN_NUM_CS HSPI_IOMUX_PIN_NUM_CS
#define SPI2_IOMUX_PIN_NUM_WP HSPI_IOMUX_PIN_NUM_WP
#define SPI2_IOMUX_PIN_NUM_HD HSPI_IOMUX_PIN_NUM_HD
#define SPI3_FUNC_NUM VSPI_FUNC_NUM
#define SPI3_IOMUX_PIN_NUM_MISO VSPI_IOMUX_PIN_NUM_MISO
#define SPI3_IOMUX_PIN_NUM_MOSI VSPI_IOMUX_PIN_NUM_MOSI
#define SPI3_IOMUX_PIN_NUM_CLK VSPI_IOMUX_PIN_NUM_CLK
#define SPI3_IOMUX_PIN_NUM_CS VSPI_IOMUX_PIN_NUM_CS
#define SPI3_IOMUX_PIN_NUM_WP VSPI_IOMUX_PIN_NUM_WP
#define SPI3_IOMUX_PIN_NUM_HD VSPI_IOMUX_PIN_NUM_HD
//Following Macros are deprecated. Please use the Macros above
#define HSPI_FUNC_NUM 1
#define HSPI_IOMUX_PIN_NUM_MISO 12
#define HSPI_IOMUX_PIN_NUM_MOSI 13
#define HSPI_IOMUX_PIN_NUM_CLK 14
#define HSPI_IOMUX_PIN_NUM_CS 15
#define HSPI_IOMUX_PIN_NUM_WP 2
#define HSPI_IOMUX_PIN_NUM_HD 4
#define VSPI_FUNC_NUM 1
#define VSPI_IOMUX_PIN_NUM_MISO 19
#define VSPI_IOMUX_PIN_NUM_MOSI 23
#define VSPI_IOMUX_PIN_NUM_CLK 18
#define VSPI_IOMUX_PIN_NUM_CS 5
#define VSPI_IOMUX_PIN_NUM_WP 22
#define VSPI_IOMUX_PIN_NUM_HD 21So looking at the espressif docs for ESP-PICO-KIT-1, ESP-PICO-KIT-V4 pin description list, it indicates that
the venerable @Kolban asked what these unusual Qs and Ds indicate on an esp32 forum (see bottom) and the response was: So then, if you wanted to try IO_MUX enabled SPI definitions, you might try: #define HSPI_MISO 12
#define HSPI_MOSI 13 #this is the only IO pin used in this example (master out, slave in)
#define HSPI_SCLK 14
#define HSPI_SS 15I could have this backwards, I've just been doing a lot of research into how to run dual One thing I'm not sure about is what happens if [Answered] SPI: Meaning of some of the pins for spi_bus_config_t
|
|
Has anyone used this ESP32 Hardware SPI support successfully for effective transfer rates higher than about 5MHz? It looks to me like it's using the DMA channel for only single byte transfers - very inefficient. I'm using a simple test program that just continually calls FastLED.show() to rewrite a string of 128 APA102's (so each "show" should write about 4k bits to the string). On an ESP32S3 and FastLED 3.6.0, I'm using pins 14 & 13, the correct hw pins for the HSPI. I get the #pragma message that ESP32 hardware support was added, and indeed, I see the stable 20MHz default clock. But there's a microsecond pause between each 8 SPI clocks. The Espressif docs talk about how important it is to set up the DMA channel correctly for the SPI driver, as each transfer setup takes a uSec. Looks to me like this code sends each individual byte for its own DMA transfer rather than sending the whole 4k at once? Am I missing something? Seems a shame to not be able to use the 40-80MHz rates this chip is capable of for the newer faster LED strips. I see this driver calling ledSPI.transfer(byte) continually. Is all FastLED transfer byte-wise without digging down into the fundamentals? |

Hardware SPI driver for ESP32, derived from code written by @BenoitAnastay: #936 and the Teensy 4 hardware SPI implementation. Tested on a Lolin/WeMos D32 Pro and two custom WROVER boards.
To use, add
#define FASTLED_ALL_PINS_HARDWARE_SPIbefore including FastLED.h.The driver uses the VSPI bus by default (pins 18, 19, 23, & 5), but can be set to use the HSPI bus (pins 14, 12, 13, & 15) by adding
#define FASTLED_ESP32_SPI_BUS HSPIbefore FastLED.h