Using ESP8266/ESP8285 to Blink an LED

  • Author: Pengfei Wu
  • Date:2016.8.3

Overview

This tutorial briefly introduces ESP8266 and ESP8285, and how to develop a simple LED blinking applications based on the official SDK. Following contents will focus on how to download the generated firmware to ESP8266/ESP8285.

Introduction

ESP8266

Espressif’s ESP8266EX delivers highly integrated Wi-Fi SoC solution to meet the continuous demands of users for efficient power usage, compact design and reliable performance in the Internet of Things industry.

Official Introduction:

http://espressif.com/en/products/hardware/esp8266ex/overview

ESP8285

ESP8285 is the ESP8266 chip embedded with 1 MByte flash memory. It is specially designed for wearable devices.

Official Introduction:

http://www.espressif.com/en/media_overview/news/espressif-announces-esp8285-wi-fi-chip-wearable-devices

Download Datasheet:

http://www.espressif.com/en/support/download/documents?keys=ESP8285%20Datasheet

Differences between ESP8285 and ESP8266

Since ESP8285 is equivalent to an ESP8266 embedded with 1MB Flash memory, therefore, ESP8285 uses the same set of SDK as ESP8266’s, namely ESP8266 SDK. But compare to ESP8286, ESP8285 has the following differences:

  • ESP8285 integrates 1MB Flash in DOUT mode. While ESP8266 requires external Flash.
  • ESP8285 has two additional GPIO pins(GPIO9 and GPIO10).
  • To compile and download ESP8285 firmware, you need to set SPI MODE to DOUT mode, otherwise it can not work even after successfully download.

Except the above differences, ESP8285 and ESP8266 have the same usage. Therefore, in the following description, unless otherwise stated, ESP8266 can be replaced by ESP8285.

Introduction of ESP8266 SDK

ESP8266 SDK (Software Development Kits) is an IOT application development platform developed by Espressif for developers that includes basic platform and upper application development examples, such as Smart Light and Smart Plug. Depending on whether it is based on an operating system (OS), SDK can be categorized into two versions: Non-OS SDK and RTOS SDK.

Official document ESP8266 SDK Getting Started Guide

http://espressif.com/sites/default/files/documentation/2a-esp8266-sdk_getting_started_guide_en.pdf

Official Document

The official website provides rich hardware and software development reference documents which users can download from here:

http://bbs.espressif.com/viewtopic.php?f=67&t=225

Develop a Blinking Application

The blinking application is based on ESP8266 SDK (here use Non-OS SDK), the actual effect after downloading the generated firmware to hardware is: GPIO13 connected LED is on for 1s (repeats itself).

Hardware Preparation

Here we use PSF-A85 as hardware. We connect GPIO13 pin with an LED, we are going to develop an application to lit it. We assume here power supply for PSF-A85 and LED and is connected correctly. Meanwhile PSF-A85 serial port (TX, RX) has also been connected to your PC, the serial port is used to download firmware to ESP8285.

PSF-A85 Related Links:

https://www.itead.cc/psf-a85.html
http://wiki.iteadstudio.com/PSF-A85

LED Related Links:

https://www.itead.cc/prototyping/electronic-brick/light-and-sound/electronic-brick-lighting-emitting-diode.html
http://wiki.iteadstudio.com/Lighting_Emitting_Diode_(LED)

Development Environment Preparation

According to ESP8266 SDK Getting Started Guide Chapter III, users can set up their development environment. Please note that here we use the Non-OS SDK.

Notes:

  • The recommended development model is the Ubuntu/Linux virtual machine + Windows, the virtual machine is Windows version download tool that is used to compile and generate firmware.
  • ESP DOWNLOAD TOOL download firmware to the target board (here is PSF-A85).
  • We assume that users have some experience in embedded Linux development, or at least familiar with the Linux operating system.

Blinking Application Coding

The code is very simple:

  • initialize GPIO 13;
  • start timer;
  • set GPIO13 output high level or low level in the timer callback function;

The complete code is as follows:

#include <ets_sys.h>
#include <osapi.h>
#include <gpio.h>
#include <os_type.h>
#include <user_interface.h>

static os_timer_t blink_timer;

void user_rf_pre_init(void)
{
}

void ICACHE_FLASH_ATTR blinkTimerCallback(void *arg)
{
    static bool is_low = true;
    if (is_low)
    {
        GPIO_OUTPUT_SET(13, 1);
        is_low = false;
    }
    else
    {
        GPIO_OUTPUT_SET(13, 0);
        is_low = true;
    }
}

void ICACHE_FLASH_ATTR user_init()
{   
    gpio_init();
    PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTCK_U, FUNC_GPIO13);
    GPIO_OUTPUT_SET(13, 0);
    os_timer_disarm(&blink_timer);
    os_timer_setfn(&blink_timer, (os_timer_func_t *)blinkTimerCallback, NULL);
    os_timer_arm(&blink_timer, 1000, 1);
}

Save the above code as user_main.c.

Compile

According to *ESP8266 SDK Getting Started Guide *Chapter V, run ./gen_misc.sh, complete the compilation according to the hints, users will get user1.*.bin file in SDK root directory bin/upgrade.

Notes:

  • Select support OTA during compilation, and select APP=1;
  • This example uses ESP8285 based module, and therefore, the flash size is 1MB. Also, SPI MODE must be DOUT;
  • If you are using a module based on ESP8266, you should select corresponding compile options according to actual size of the flash and SPI MODE.

Download

After successful compiling, users can download the generated user1.*.bin and the SDK released boot*.bin, blank.bin andesp_init_data_default.binto the ESP8285 internal flash through serial port.

Confirm Download Address

According to ESP8266 SDK Getting Started Guide chapter IV, corresponding download addresses for each binary file are:

  • boot*.bin:0x00000;
  • esp_init_data_default.bin:0xfc000;
  • blank.bin:0xfe000;
  • user1.*.bin:0x01000;

Download Firmware

Step 1: Get ESP8266 enter into download mode. When ESP8266 starts, select pin level according to the following table:

Mode GPIO15 GPIO0 GPIO2
SDIO/SPI 1 X X
UART Download 0 0 1
Flash Boot 0 1 1

To get ESP8266 enter into firmware download mode, please follow the below mentioned basic steps:

  • Set up a peripheral circuit, set GPIO15 to low level, GPIO0 to low level and GPIO2 to high level, that is UART Download mode.
  • Reset ESP8266 or re-power the module. ESP8266 will then enter into firmware download mode.

Only in download mode can users download the compiled firmware to ESP8266 external flash or ESP8285 internal flash by ESP DOWNLOAD TOOL

Step 2: According to ESP8266 SDK Getting Started Guide chapter VI, in Windows, use ESP DOWNLOAD TOOL to download last step’s 4 binary files to PSA-A85 module ESP8285 internal flash.

Notes:

  • Before download, users should confirm the selection of SPI MODE is DOUT, otherwise it won’t work even after successful download.
  • ESP8285 internal Flash is 1MByte = 8Mbit.
  • PSF-A85 crystal is26M, SPI SPEED should choose 40MHz.
  • Select download buadrate to be 115200(default).

Run the Application

After the downloading process is complete, reset ESP8285 or re-power PSF-A85 module to run the blinking application. Note that GPIO13 pin connected LED should be: on for 1s, off for 1s, then repeat itself.

Summary

This tutorial has mentioned ESP8266 SDK Getting Started Guide for several times, which is the official SDK developer’s guide provided by Espressif. Every beginner should should seriously study the document. In fact, the purpose of this tutorial is to allow beginners to get familiar with the ESP8266 development process. And the tutorial focuses on firmware downloading process, while it also emphasizes the conditions to allow ESP8266 enter to download mode, which is not specified in the official documentation.

End

Leave a Reply

%d bloggers like this: