Using the Espressif ESP8266 WiFi Chip



Wifi and MPU Summary

There are SPI driven wifi interfaces such as the nRF2401, but they require a separate MPU and suitable driver software and a separate MPU.

The Espressif ESP8266 provides an extendable MPU connected to a wifi interface, and is a simpler and very popular means of obtaining embedded wifi connectivity. It may be used standalone or as an adjunct to another controller.
There is a vast amount of material on the net about it, some helpful, and some less so.
The original poprietary default application firmware provided by Espressif was a wifi interface controlled by simple commands ("AT" commands) sent via the serial interface. This was later extended by Espressif and others to provide a fully capable IOT (Internet of Things) programming environment.
The CPU is based on the extendable Xtensa 32 bit architecture.
A later version is the ESP32, which has more RAM, and a partial ethernet interface. It is not directly compatible and has a different SDK. Unfortunately the ethernet interface does not include the "PHY". If ethernet connection is required the ENC28J60 remains the easiest path.
A good resumee is Summary Description
Here follows a brief summary of what I have found on the net and tried on the MPU.
The MPU has onboard rom with a fixed bootloader. The user/application program and data is stored in a separate serial flash eeprom. These two chips may be covered with a labelled metal RFI shield as in the ESP-12E and ESP-12F modules. The program data is loaded as needed from the eeprom into a cache memory in the MPU as described in the memory map .
The chip includes a fully functional WiFi interface with a good operating range, which is the ESP8266's raison d'être.

Programming and Using the ESP8266

Espressif have provided libraries for using the hardware functions of the chip, complete with useful documentation as well as a complete SDK. I tried to build the Espressif IOT SDK, but it proved difficult to sort out the dependencies.
The Espressif libraries have been incorporated into the alternative Open-Sdk , which can also compile and link C programs for the MPU. This built under Debian Linux with no problems.
A final user firmware consists of 2 parts, a boot loader at location 0x0000 , and a user program at 0x10000. Both these sections need to be flashed.
The size of the flash memory varies, a common size is 4 megabyte. It can usually be discovered using esptool.py. Information for the wifi interface is stored at "msize-0x4000" ( e.g. 3fc000 if msize=4Mb=0x400000). If this is corrupted or erased it must be reprogrammed from the file esp_init_data_default.bin (in the SDK) to address msize-0x4000.

Serial Flash for Data Storage

The serial flash memory is very useful for data storage. The available memory depends on the memory map used. The mapping depends in turn on the flash size and whether OTA is needed. If OTA is not needed , then the space between the top of the pogram and approximately msize-0x8000 is available for user data

Programming the flash is done through three system calls:

spi_flash_read , spi_flash_write and spi_flash_erase_sector,

as described in 99a-sdk-espressif_iot_flash_rw_operation_en_v1.0_0.pdf

The data buffers passed to these system calls must sit on 4 byte address boundaries, and the length parameters are rounded up to the next highest multiple of 4. If any trailing bytes (+0 to +3) are not intended to be overwritten they must be suitably masked (set to 0xff).
Esptool.py does not take this into account, and erases a sector when any write to data within it is requested.
All the flash chips I have tried all program "1" to "0". This means that a sector (SPI_FLASH_SEC_SIZE=4096 bytes) only need be erased if any "0" bits within it need to revert to "1".
Various sorts of flash are used , some are described in Flash Chips
pushing upgrades The bootloader provides the possibility of upgrading the program via the net. In this case two alternative copies of the firmware are stored in flash
All initial external programming of the eeprom occurs via a serial port on the MPU. Finding or setting the baud rate of user firmware can be troublesome, and often involves searching through th code, and/or experiment. A common rate is 74880 baud.
The chip can be programmed/flashed and examined using esptool.py which is included in the sdk. The boot rom enters uart boot mode when reset is activated with appropriate GPIO settings. Espressif provide two programming environments, RTOS and NONOS . Both of these are incorporated in the Open-Sdk. The NONOS variant is a simpler timer driven environment, and was adequate for my purposes.

ESP8266 Hardware Platforms


Lots of low priced hardware is available, some cards such as the "Nodemcu 12E/F" or "ESP12E: include a usb to serial converter and a 3.3 volt regulator; this serial converter also uses RTS to set the appropriate GPIO pin for booting . These cards work mostly quite well, but some show flash eerom faults. I was caught by such a fault on my first attempt - the eeprom fatigued after a few erase cycles.

Lower priced cards such as the ESP12F bare module have no USB interface or regulator, but have pads for I/O and shield for eerom and MPU. To program these cards a separate serial interface and a 3.3 Volt power supply are needed. These can be programmed using a separate USB-serial adapter (cp210x etc) and are probably the best option for multiple standalone IOT applications.

Even simpler boards such as the ESP-01 only have no shield and few pins. These suitable for applications where external RFI shielding is provided, and where the main function is to provide wifi interfacing for another controller via the serial port

"Hello World" Firmware Using "C" Source Code

The file {sdk-path}/esp-open-sdk/examples/blinky/blinky.c is a good starting point. It may be necessary to change the GPIO pin number to suit the card you are using. The next step is to try calling a few of the API's If this is successful it is worth building and flashing esphttpd. Esphttpd is a classy little webserver written by Jeroen Domburg. It is neatly written and gives a good straightforward example of how the ESP8266 is programmed - I definitely owe him a beer! The default baud rate for debugging this program is 115200.

E.G. , assuming already flashed:

esptool.py flash_id;screen /dev/ttyUSB0 115200

will reset the program, and display transaction debug messages

Subroutines can be specified to run directly from RAM, or to be cached from the much slower SRAM. There is not much RAM available, so only absolute essentials should stay in RAM.

Interpreters

There is a very large amount of software for the ESP8266. Many of the applications use interpretive languages. The two main languages are Arduino and Lua. I have tried Lua with the Nodemcu Lua interpreter built with Open-SDK. It worked, but was a bit overweight and slow for my purposes.
What is not immediately obvious is that the terms "Nodemcu" and "Arduino" both have double meanings.
Arduino can mean a specific hardware platform, or an interpretive language called Arduino. The Arduino language can be used on platforms other than Arduino such as the ESP8266.
Similarly the term Nodemcu applies either to a hardware platform (ESP12E/F pcbs often have the label "Nodemcu" and are sold as such) , or an interpreter for the "LUA" language.