Monday, October 13, 2014

Getting started with development on ESP8266

Enterprising tinkerers on the esp8266 message boards have added the Xtensa Call0 ABI support to gcc, allowing a fairly clean way to compile the code and run it on the device.

I'm going try to try to put the steps together here, mainly for my own benefit since I'll probably forgot otherwise. :-)

To get the toolchain up and running, follow the instructions given here:
$ git clone -b lx106 git://github.com/jcmvbkbc/crosstool-NG.git
$ cd crosstool-NG
$ ./bootstrap && ./configure --prefix=`pwd` && make && make install
$ ./ct-ng xtensa-lx106-elf
$ ./ct-ng build
This will compile the toolchain targeted at xtensa lx106 into the builds/ directory and will take a while.  You may be required to install some missing dependencies.  Meanwhile, we need to download the SDK and few other odds and ends.

From this post, download the esptool-0.0.2.zip file which contains the "esptool" package which will parse the elf generated by gcc compiler above, extract the relevant sections, and repack it into a binary format ready to be flashed.  While you're there, also download the Makefiles which we'll modify to work with the toolchain above.

From this post, download the esp8266 sdk package.  Extract the SDK package, extract the esptool-0.0.2.zip inside the SDK (as the Makefile looks for SDK_DIR/esptool/esptool executable) and build the esptool executable
$ unzip esp8266_sdk_v0.9.1.zip
$ cp esptool-0.0.2.zip esp8266_sdk_v0.9.1
$ cd esp8266_sdk_v0.9.1
$ unzip esptool-0.0.2.zip
$ cd esptool
$ vim Makefile  # edit target from WINDOWS to LINUX
$ make
To build the "AT" firmware, I had to copy the new updated makefile from Makefile_AT_0.9.1 into the SDK_DIR/examples/at directory and make couple of changes to the Makefile by setting the SDK_BASE, setting EXTRA_INCDIR to add include/ and /usr/include/ fields, and pointing to the toolchain built above for CC/AR/LD calls.
$ cp Makefile_AT_0.9.1.zip esp8266_sdk_v0.9.1/examples/at
$ cd esp8266_sdk_v0.9.1/examples/at
$ unzip Makefile_AT_0.9.1.zip
$ vim Makefile # Update SDK_BASE, EXTRA_INCDIR, CC, AR, LD
$ make
It would've been lovely if it all had just worked...  but some more tweaks and missing dependencies are required.  First, we need to put the libc.a and libhal.a from extra_libs.tgz from this post and put it in the SDK_DIR/lib directory.  I also had to comment out int32_t in SDK_DIR/include/c_types.h.  After that, though, simple "$ make" and we get the two firmware files, 0x00000.bin and 0x40000.bin!

To download the firmware to the ESP8266, you need to boot the board with GPIO0 tied to ground to have it come up in programming mode.  Once in programming mode, I was able to use the esp8266.py download script to program the new code
python esptool.py --port COM3 write_flash 0x00000 0x00000.bin
python esptool.py --port COM3 write_flash 0x40000 0x40000.bin
I got some error messages after erasing and programming had completed, but everything seems to work OK even with the error message.

ESP8266 Communicating
The module uses AT command set, and I was able to test the basic functionality by doing the following:
COM3> AT+RST
COM3> AT+CWMODE=1
COM3> AT+CWLAP
COM3> AT+CWJAP="SSID","PASSWORD"
COM3> AT+CIFSR # Get IP address
COM3> AT+CIPMUX=1
COM3> AT+CIPSERVER=1,5000
# Server mode enabled, test with nc IP_ADDRESS 5000
COM3> AT+CIPSERVER=0

# Client mode, test with nc -l 6000
COM3> AT+CIPMUX=0
COM3> AT+CIPSTART="TCP","TARGET_IP",6000
COM3> AT+CIPSEND=<length>
COM3> <DATA>
COM3> AT+CIPCLOSE
What I'm interested in doing doing, though, is mode where the device wakes up by itself, automatically connects to a known Wifi station, and either automatically starts sending data or starts up a server so that data can be pulled out.  This seems closer to the SDK's "IoT_demo" code base.  I'm able to build and flash the resulting image, but I'm not quite sure what it is that the demo does yet.  I suppose if I can't figure it out, I can just write something from scratch instead. :-)

No comments:

Post a Comment