Saturday 28 April 2018

A Very Simple ESP8266 Blinky Source Code Using the Non-OS SDK

There are examples come with the official Espressif SDK, for the latest version 2.2.0, the examples are those:

As I said before, Espressif has the worst documentation comparing to other chip suppliers. These examples don't have any detailed information, the only thing you can find on their web site is an article about the Iot_Demo, which tells you how to use the program, not explaining how the code works.

I created a very simple blinky program with the Non-OS SDK, it has only one souce file in c, and it does nothing more than blink a LED on the ESP Launcher board. Not using any WiFi or communication APIs, anyway, the SDK forces you to include an initialization code in the user code to tell the chip where is the default parameters for the wireless circuits inside, and it seems the chip goes into Access Point mode though you don't have any code to let it do so.

There is a blue LED connected to MTDI/GPIO12 pin on the ESP Launcher board, what out code does, is to setup a software system timer, then toggle the status of the GPIO12 pin in the callback function. Plus a "Hello World" printed to the serial port. Here is the complete source code:

#include "ets_sys.h"
#include "osapi.h"
#include "gpio.h"
#include "user_interface.h"

uint32 priv_param_start_sec;
/******************************************************************************
 * FunctionName : user_rf_cal_sector_set
 * Description  : SDK just reversed 4 sectors, used for rf init data and paramters.
 *                We add this function to force users to set rf cal sector, since
 *                we don't know which sector is free in user's application.
 *                sector map for last several sectors : ABCCC
 *                A : rf cal
 *                B : rf init data
 *                C : sdk parameters
 * Parameters   : none
 * Returns      : rf cal sector
*******************************************************************************/
uint32 ICACHE_FLASH_ATTR
user_rf_cal_sector_set(void)
{
    enum flash_size_map size_map = system_get_flash_size_map();
    uint32 rf_cal_sec = 0;

    switch (size_map) {
        case FLASH_SIZE_4M_MAP_256_256:
            rf_cal_sec = 128 - 5;
            priv_param_start_sec = 0x3C;
            break;

        case FLASH_SIZE_8M_MAP_512_512:
            rf_cal_sec = 256 - 5;
            priv_param_start_sec = 0x7C;
            break;

        case FLASH_SIZE_16M_MAP_512_512:
            rf_cal_sec = 512 - 5;
            priv_param_start_sec = 0x7C;
            break;
        case FLASH_SIZE_16M_MAP_1024_1024:
            rf_cal_sec = 512 - 5;
            priv_param_start_sec = 0xFC;
            break;

        case FLASH_SIZE_32M_MAP_512_512:
            rf_cal_sec = 1024 - 5;
            priv_param_start_sec = 0x7C;
            break;
        case FLASH_SIZE_32M_MAP_1024_1024:
            rf_cal_sec = 1024 - 5;
            priv_param_start_sec = 0xFC;
            break;

        case FLASH_SIZE_64M_MAP_1024_1024:
            rf_cal_sec = 2048 - 5;
            priv_param_start_sec = 0xFC;
            break;
        case FLASH_SIZE_128M_MAP_1024_1024:
            rf_cal_sec = 4096 - 5;
            priv_param_start_sec = 0xFC;
            break;
        default:
            rf_cal_sec = 0;
            priv_param_start_sec = 0;
            break;
    }

    return rf_cal_sec;
}

/*******  toggle LED  **************/
void ICACHE_FLASH_ATTR toggle(void)
{
if (GPIO_REG_READ(GPIO_OUT_ADDRESS) & BIT12) // test if GPIO12 is 1
{
gpio_output_set(0, BIT12, 0, 0); // output 0
}
else
{
gpio_output_set(BIT12, 0, 0, 0); // output 1
}
}

/******************************************************************************
 * FunctionName : user_init
 * Description  : entry of user application, init user function here
 * Parameters   : none
 * Returns      : none
*******************************************************************************/
void ICACHE_FLASH_ATTR
user_init(void)
{
LOCAL os_timer_t blink_timer;
os_printf("Hello World from MCU Labs ESP8266\n");
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDI_U, FUNC_GPIO12); //set the pin as GPIO
gpio_output_set(0, 0, BIT12, 0); //enable output on GPIO12
os_timer_disarm(&blink_timer);
os_timer_setfn(&blink_timer, (os_timer_func_t*)toggle, NULL); //setup callback
os_timer_arm(&blink_timer, 300, 1); //start timer at an interval of 300ms
}

Here is the video for compiling and running of the code, and the serial port output.
For how to download the code to ESP8266 in Ubuntu, see this article : https://www.mculabs.net/2018/04/downloading-code-to-esp8266-in-ubuntu.html 
For how to set Ubuntu to ESP8266's 74880 baudrate, see this: https://www.mculabs.net/2018/04/getting-odd-74880-baudrate-for-esp8266.html

Friday 20 April 2018

Getting the odd 74880 baudrate for ESP8266 in Ubuntu

ESP8266's official demos use a very odd baudrate 74880 for the UART (except for the AT demo). It is rarely supported by the operating systems. For Ubuntu, the serial port only supports those 'standard' baudrates, 38400, 57600, 115200 etc.

In Linux, it's very easy to see what's going on at the serial port, you don't even need a software to do that, use the terminal command 'cat':

$cat /dev/ttyUSB3

where ttyUSB3 is the device number in Ubuntu of the on-board USB-Serial converter of ESP Launcher.

But if you are not at 74880, you'll only see junks on the serial port when you run the IoT Demo.

 After a lot of searching on the internet, I finally find this python program, it can set the baud rate to any value you like, thanks for the excellent work of the creator of it.

#!/usr/bin/python
# set nonstandard baudrate. http://unix.stackexchange.com/a/327366/119298
import sys,array,fcntl

# from /usr/lib/python2.7/site-packages/serial/serialposix.py
# /usr/include/asm-generic/termbits.h for struct termios2
#  [2]c_cflag [9]c_ispeed [10]c_ospeed
def set_special_baudrate(fd, baudrate):
    TCGETS2 = 0x802C542A
    TCSETS2 = 0x402C542B
    BOTHER = 0o010000
    CBAUD = 0o010017
    buf = array.array('i', [0] * 64) # is 44 really
    fcntl.ioctl(fd, TCGETS2, buf)
    buf[2] &= ~CBAUD
    buf[2] |= BOTHER
    buf[9] = buf[10] = baudrate
    assert(fcntl.ioctl(fd, TCSETS2, buf)==0)
    fcntl.ioctl(fd, TCGETS2, buf)
    if buf[9]!=baudrate or buf[10]!=baudrate:
       print("failed. speed is %d %d" % (buf[9],buf[10]))
       sys.exit(1)

set_special_baudrate(0, int(sys.argv[1]))

You need to have Python installed on your computer to run it. The usage is pretty simple, if the program is named set_baud_rate.py, change to the folder that contains the file, and run:

$./set_baud_rate.py <>/dev/ttyUSB3 74880 

Now you can see the ESP8266's bootup messages.

Friday 13 April 2018

Downloading code to ESP8266 in Ubuntu

In last article I talked about setting up the SDK in Ubuntu, if everything goes well, you may can compile the code and got the binary code generated.

To download in Ubuntu is every simple, you just need to install the downloading tool and run it. There is a folder in the esp-open-sdk, which is the open source part of the whole SDK, named 'esptool'. inside the folder there's a Python program 'esptool.py'. If you have Python installed on your computer, you probably can use this tool. But for me sometimes it gives me confusing error message. I have two laptops both with Ubuntu 16.04, it runs well on one of them but on the other it says 'ImportError: No module named serial'. I don't know Python so I'm not able to figure out the problem. I hope you don't encounter this.

There is another way to do it. Since Ubuntu 12, the 'esptool' has been in the official repository and it's still there in Ubuntu 16. All you need to do is install it using the 'apt install' command.

$sudo apt install esptool

Yes, it's that simple. After this you'll have the ESP8266 downloading tool installed on your computer and that's a binary code you don't even need Python environment.


There are more functions than downloading in this esptool, you can find them out using 'man', Yes! yet another benefit to install esptool from the Ubuntu repository. To see the user manual, type :

$man esptool

Briefly, the command format is:
esptool -cp 'serial port' -ca 'address1' -cf 'file1' -ca 'address2' -cf 'file2' ....  (without the quote character)

For connecting the ESP8266, please see this article : Connect ESP8266 in Ubuntu

If you have multiple USB tty devices connected, it would be difficult to know which is which by just seeing the /dev/ttyUSB* list. A shortcut to sort it out is to use the 'dmesg' command. It will output all the system messages since the computer was been turned on, but a grep will filter unwanted out. Unplug your ESP8266 and connect again, then type:

$dmesg | grep tty

The latest message about tty device will be your ESP8266 and you'll know which ttyUSB number it's at.

A real download command would look like this:

esptool -cp /dev/ttyUSB0 -ca 0 -cf eagle.flash.bin -ca 0x10000 -cf eagle.irom0text.bin -ca 0x3fc000 -cf esp_init_data_default_v08.bin -ca 0x3fe000 -cf blank.bin

Tuesday 3 April 2018

Building the ESP8266 SDK in Ubuntu

The ESP8266 is built upon Linux entirely, the compiler and linker are both based on GCC, and even the downloading tool is also a Linux application. But for some unknown reasons, Espressif chooses to provide its SDK in a Windows favor. In their official guides, you need to install a virtual machine in Windows and then download an entire Lubuntu image which contains the SDK.

For someone who uses Ubuntu more than Windows like me, this simply doesn't make sense. And Espressif won't tell you how to build the environment in linux at all. If you let me pick a company with bad documentation, Espressif must be in the top 3.

I finally figured it out after a lot of searching and trying, and I would share it here.

At first, it should be made clear, which Espressif has failed to do so, the SDK is consist of 2 parts, the esp-open-sdk and the ESP8266 SDK. The esp-open-sdk is complete open source, containing the compiler and other utilities. This part can be downloaded from Github as source file, and then compiled locally. Thanks for pfalcon for doing this great job. Just choose where you want to install it and run:

$git clone --recursive https://github.com/pfalcon/esp-open-sdk

 You should get a dir named esp-open-sdk, and then enter the dir and run:

$make

If there are any missing dependencies the make program will give an error message and you can just follow the hints it gives to fix that. There are detailed instructions about the install on Github. The compiling will take quite a while (tens of minutes) if your computer is not fast enough.

The next step, is to install the ESP8266 SDK itself. This part is related to the ESP8266 chip and not completely open source. It contains some proprietary libraries. This part should be downloaded from Espressif's official web site. It would be something like ESP8266_NONOS_SDK-2.2.0 or so. You can put the downloaded directory anywhere you like, but not in the esp-open-sdk.

An important step, don't forget to add the /esp-open-sdk/xtensa-lx106-elf/bin/ to your PATH:

$export PATH=$PATH:(your full /esp-open-sdk/xtensa-lx106-elf/bin/ path)

Now we are ready to compile the examples. There is another pitfall hiding there which Espressif didn't tell you and will make you crazy. The examples in the SDK can NOT be compiled unless you move them to the upper folder!

All the examples are in the /ESP8266_NONOS_SDK-2.2.0/examples/ , for example, the IoT_Demo example is in /ESP8266_NONOS_SDK-2.2.0/examples/IoT_Demo/ . If you would like to compile this example, you have to MOVE it one level up, parallel to the 'examples' folder, so the project compiled must be at a folder like /ESP8266_NONOS_SDK-2.2.0/IoT_Demo/ .

At the correct location, inside the IoT_Demo, just run:

$./gen_misc.sh

I hope this could help you.