ESP8266 Hardware SPI Driver Code UPDATE!

Code library has been updated with much more functionality.

Command Support:

You can now include a command component in the SPI transaction. Commands can be up to 16 bits long.

Address Support:

You can now include an address component in the SPI transaction. Addresses can be up to 32 bits long.

Dummy Bit Support:

The number of dummy bits can now be set if required.


Most importantly, it now implements reading data up to 32bits in length!

Example code snippets and function descriptors are in the file and on the GitHub page.


Posted in Electronics, ESP8266 and tagged , , , , .


  1. Thanks for all the code and information about the ESP SPI interface. Your timing for release was perfect because I am just starting a project the needs SPI ­čÖé
    I am planning on streaming mp3 web radio to a VS1053 audio decoder via HSPI. This device uses two CS lines, a regular CS for commands and xCS for data. Both lines are active low. There is also a mode where toggling one pin switches between modes (command = active low, data = active high).
    Neither of these modes works with the standard CS implementation on the ESP which uses one fixed pin in active low mode. I wonder if you have a suggestion for driving SPI with either multiple CS pins or active high CS pins?
    I am hoping that by configuring GPIO15 as a simple GPIO pin I can disable HSPI chip select without losing a pin and then pull them up / down as needed. Does this sound reasonable to you?
    Thanks again for the library code, it will be very helpful!

    • Hi MisoLabs. You can change the gpio_init function so that the HSPI CS pin remains as GPIO. You can then use that pin in combination with another GPIO for your 2 chip select lines. You’ll just have to toggle them high and low as needed before calling the SPI commands. I was planning on implementing using normal GPIO as a “software chip select” line to get around the issues with the hardware CS polarity, but haven’t got around to it yet.

      (Also, comments are set to hidden until I approve them. This was to stop spam but the other plugin I’m using seems to be filtering it pretty well so I’ll change this)

  2. Hi David,

    Would this library allow esp8266 to be a slave spi device? The project I am working on already has an MCU. I just want to access the AT commands over SPI for higher data rates.


    • Jim, it doesn’t support using the ESP as a slave SPI device at the moment. Espressif has some SPI Slave code in their SDK. There’s a bit of documentation somewhere that kind of explains it, but I haven’t really looked into it.

      • Hi metalphreak,
        I also try to use ESP8266 as spi slave device, but my program doesn’ t work properly. How about information about spi slave in documentation? There is no information that ESP8266 can’t works as slave yet. I tried to use functions from official SDK and your functions spi_rx8(), spi_rx16, spi_rx32 (after spi_init) to receive data, but data in SPI_W0 – SPI_W15 registers is always the same. Data transmited by master is properly, I checked it with other device.

  3. Is it possible with this spi driver to write the on bord flash memory (esp8266 12e)directly bypassing the sdk function “spi_flash_write”. This would be useful for writing any address of the flash and not as actually limited by the sdk to the memory mapped flash section!

  4. Hi,
    I am Electronics Engineer and just started working on esp8266. I saw your code on github for esp8266 spi driver. The problem is, I am not getting the line SET_PERI_REG_MASK(), WRITE_PERI_REG(), CLEAR_PERI_REG_MASK().
    Can you please help me with it David.
    And it would be so good of you if you provide any link or any book or material where I can learn programming esp8266 right from scratch.
    Waiting for your reply.

  5. Just wondered if it would be possible to use your SPI code to rework the ESP8266 into a cheap & cheerful SPI flasher? – I’ve seen and have a teensy3.1 but would love to use an ESP8266 as an alternative ( using SPI and Serial or Wifi? ). No idea if that’s possible or just the dreams of a madman?

    • You’d just need to rewrite that to use the hardware SPI driver commands for sending/receiving data, rather than toggling pins manually (ie CS). If you want to write it natively for the espressif toolchain you’ll need to re-write it in standard C not object oriented C++.

    • a tax on soda won’t deter consumption, which must be music to liberal ears.I predict a soda tax will be enacted in 2011 and the tax, per can of soda, will hit 25 cents a can by 2016. That’s because the National Healthcare program we be in debt and everyone will be paying for it one way or another.They did it to tobacco, they’ll do it to soda because “it’s bad for you”.

Leave a Reply

Your email address will not be published. Required fields are marked *