ESP8266 Windows Compiler Setups
Visual Studio IDE
This document will allow you to develop native C/C++ firmware applications for the ESP8266 using Windows. You can setup your development environment using Microsoft Visual Studio IDE. Using the accompanying serial terminal and flash writer application, you will be able to edit compile, link, flash and run without ever needing to leave the IDE. You can download Microsoft Visual Studio Comunity for free. If you want to setup environment manually (more control) please refer to Toolset installation section. Otherwise you can use ESP8266 Wizard.
You can also use a third-party plugin to setup building and debugging automatically.
The following lists all the software components I have gathered in order to create and upload firmware for the ESP8266. Most of these components have been supplied by other hobbyists and are subject to change or move. Even the official SDKs supplied by Espressif seem to evolve rather quickly.
This particular version of the toolset is designed to work under Windows 7 or 8, 32 or 64bit.
You can download a pre-built ESP8266 toolchain that contains Python and the ESP8266 SDK. Alternatively you can gather the necessary components manually:
Create a directory to put all your ESP stuff in. I put mine in E:\ESP8266, but obviously yours will be different.
Get Python 2.7X
. Install it where you want. I put it in E:\Python27
. In the package manager, choose mingw-developer-toolkit
. This will give you make.exe
and some important utilities, like mkdir.exe
that the makefiles will need.
Get the sdk
. Install it under your chosen directory. Mine is in E:\ESP8266\esp_iot_rtos_sdk-master
Download the missing libraries for the SDK: FreeRTOS libraries for SDK
. Add them to the E:\ESP8266\espiotrtos_sdk-master\lib
Get the pre-built compiler
. Stick it in your chosen directory. Mine is in E:\ESP8266\xtensa-lx106-elf
Get the missing xtensa includes for gcc here
. It is the file xtensa.tar.bz2
. Extract them into the compiler subdirectory: E:\ESP8266\xtensa-lx106-elf\xtensa-lx106-elf\include
Get the source code for the xPL example project
. Install it into a directory under your chosen project directory. Mine are in E:\ESP8266\Apps\xPL-ESP8266
Now you need to correctly setup your path. You can of course add things to your global user path, but I like to setup a separate command prompt for things like this. I created a file called Vars.cmd, that sets up the path for working with the ESP toolset:
Then I setup a desktop shortcut for a command prompt:
%comspec% /k E:\ESP8266\VARS.cmd. When I click this I get a command prompt with the proper path already set.
For the same reason, I also created a batch file that sets up the path before calling make:
make %1 %2 %3 %4 %5
So in my project setup, in Visual Studio, I use ESPMAKE instead of make.
Modify the makefile in the xPLESP8266 project so that it reflects your locations for the tools and sdk. You should be able to compile and flash by simply typing 'make' or 'make flash' in the xPLESP8266 directory.
Get the enhanced serial terminal and firmware tool
. You need the modified Python tool to flash the chip, esptool-py.py
. This version is enhanced to reset the chip and bring it into firmware update mode automatically by using DTR. It also connects to Termie, the serial terminal application to share the serial port connection.
Compile 'Termie' the serial terminal. I modified this program to use a named pipe to communicate with the esptool-py flasher application. Thus it knows when to disconnect from the serial port when you want to flash the chip.
You can use Visual Studio as your development IDE, by adding your project as a C++ Makefile project. I have included sample project files. You need to modify the project properties to setup your make command and add the include directories in the configuration so that intellisense can show you function definitions in the editor. I recommend that you get the free Visual Studio 2013 community edition
About the example project, xPL-ESP8266
This project is designed using xPL, a protocol for Home Automation devices and systems. I used the FreeRTOS based SDK, as I believe it will be more flexible for most users. The program uses UDP to send and receive xPL messages. I used an ESP-01 variant, since it has two available GPIOs, GPIO0 and GPIO2. Although these GPIOs are used during the boot process, with some care, they can be used by applications. The application currently does two things:
Responds to xPL X10.BASIC ON and OFF commands, on a user-defined address, to turn on or off GPIO0. I have connected this to a LED for easy confirmation.
Generates an xPL X10.BASIC trigger command based on the state of GPIO2. Grounding GPIO2 generates an ON trigger, releasing it generates an OFF trigger.
Things to watch out for
You cannot use most of the standard 'C' libraries, as they are not compatible with the runtime. The sdk does provide many common functions, like 'printf', 'memcpy', and similar, but I had to scrounge the Internets to get source code to some missing functions, like 'sscanf', 'strlcpy'.
Since every task created with FreeRTOS has it's own stack, the size of these needs to be managed carefully. Although the ESP8266 has plenty of RAM when compared to most microcontrollers, it is easy to run out of stack, or free memory. For this reason, I have used
malloc() for many larger structures that would be otherwise declared on the stack in other environments.
By default, all code goes into iram, which is only 32k, so it is easy to overflow that segment by linking in a bunch of stuff from the libraries or from your own code.
Use ICACHEFLASHATTR on your functions to make sure they go into irom instead. Irom resides off-chip on a 4M flash ROM, thus leaving about 240-440kB for applications. Although the final compiled program appears huge (about 180k in irom, and 28k in iram), most of this is library code that runs the Wifi and IP stacks. The actual user application is something like 8k.
The hardware design shown above also assumes that the switch will remain open at bootup, as GPIO2 needs to be high for the chip to boot normally.