ESP8266 SD card interfacing using FreeRTOS SDK – part 1

posted in: Internet of Things (IoT) | 3

Over the last one week, I did spend a significant amount of time trying to understand how the SPI interface actually works on the ESP8266EX. Though there is not much information available, I found out 2 different modes of transmission when transmission speed is concerned. I will put both of those in the ESP8266 LCD Application Guide (I am currently making that for Espressif Systems).

Apart from that, one thing is for sure – the HSPI interface on the ESP8266 can do pretty much all that any other ARM processor with an SPI interface can. However, the library and examples provided by Espressif Systems is pretty troublesome because they lack good descriptive comments and a manual for the code.

So what I am going to do is make my own library for very efficient SPI interface, all the way up from scratch because that is the only way to do it now. It would probably be better than struggling with existing code. The number of undocumented definitions and redefinitions is just overwhelming in the RTOS SDK and confuses things a lot. So that’s it – a whole new library for the ESP8266.

I can be sure of achieving a transfer speed of about 2-3 MB/s from a good SD card using the ESP8266 and mounting a FAT16 or FAT32 file system.

As for now, I am struggling with the file spi_interface.c provided by Espressif Systems in an example.

What on earth does the following do?

SET_PERI_REG_BITS was nowhere to be seen in the SPI related files, but after hunting it down further, I see some related definitions in gpio_register.h.

Your guess is as good as mine! And by the way, I like WRITE_PERI_REG much more than I like the above complicated macro used in SPI example. So I would just stick to using 2-argument functions for the sake of readability. Implementing FAT16/32 on a completely different platform is no joke and readability can make the difference between crash and run.

I will take a closer look at the SPI registers and when I figure out what to do to send data, I will make more blog posts. The main mission is to get a FAT32 formatted SD card up and running with the ESP8266 and FreeRTOS SDK!

This is mainly a part of my IoTBlocks project, by the way, which you may want to follow. I will definitely post more about it in future (not-too-future!) blogs. 🙂

Read part-2 (HSPI initialization)


3 Responses

  1. new299

    Oh man. I had a bunch of fun getting the SPI interface on the esp8266 working with SD cards too. The code in the Arduino support pack thankfully now works pretty well, but getting it to work from the esp-open-sdk was quite a bit of work. I wrote up my notes here:

    I’m also now selling a small board from my hobby store that integrates an esp8266 and SD card:

    • pratik

      That is pretty cool!
      I could manage to get file I/O speed from FAT32 cards upto 15 mbps. Enough to stream videos with the ESP8266 server now! 😀
      Could not update this article series because of lack of time…

Leave a Reply

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