esp-open-rtosを使ってみる

環境構築


FreeRTOSはマイコンで動くRTOSで、こちらに公式でサポートするマ イコンの一覧が有ります。
公式サポートにESP8266はありませんが、こちらに ESP8266用のFreeRTOS(esp-open-rtos)が公開されています。
esp-open-rtosを使うと、どのようなことができるのか紹介していきます。
なお、今回使用したプラットフォームはAMD E2-9000eで動くUbuntu 18.04.1です。



まずはtoolchainのインストールです。
ESP8266(Xtensa LX106)のtoolchainとしてesp_open_sdkを使います。
名前が紛らわしいですが、esp_open_sdkはtoolchain、esp_open_rtosはRTOSです。

esp-open-sdkの入手方法はこちらで 紹介しています。
今回はセルフビルドしたものを使いました。

pythonとpyserialがインストールされていないときは、以下の手順であらかじめインストールしておきます。
$ sudo apt install python
$ sudo apt install python-pip
$ pip install pyserial

使用したpythonのバージョンは以下のバージョンです。
$ python --version
Python 2.7.15rc1

ファームの書き込みで使用するesptool.pyがインストールされていないときは、こちらの手順でインストールしておきま す。



次にesp-open-rtosをこちらからダウンロード し、適当なディレクトリに展開します。
私は$HOME/esp-open-rtosに展開しました。

$HOME/esp-open-rtos/examplesディレクトには多数のサンプルソースが含まれています。
ファームのビルドの前に、$HOME/esp-open-rtos/include/private_ssid_config.hにAPの情報を 設定します。
以前のバージョンでは、$HOME/esp-open-rtos/include/ssid_config.hに設定していましたが、
最新のバージョンでは設定ファイルが変わっています。

$ cat private_ssid_config.h
#define WIFI_SSID "APのSSID"
#define WIFI_PASS "APのパスワード"



動作確認のために、$HOME/esp-open-rtos/examples/http_getをビルドします。
私はNodeMCUを使いましたが、USB-TTLアダプターをNodeMCUのシリアルポートに接続し、
WindowsPCのTeraTermで実行結果をモニターリングしています。
ちょっと複雑ですが、このような感じです。

USBケーブルはコンパイルマシンのLinuxに繋がっています。
USB-TTL変換ケーブルはTX/RX/GNDだけ使って、USB-HUB経由でWindowsPCに繋がっています。
WindowsPCではLinuxのsshコンソールと、シリアルポート(115200bps)の2つのTeraTermを起動しています。


http-getのサンプルコードをビルドしてESPに書き込みます。
$ cd $HOME/esp-open-rtos
$ sudo make flash -C examples/http_get ESPPORT=/dev/ttyUSB0
make: Entering directory '/home/nop/esp-open-rtos/examples/http_get'
CC /home/nop/esp-open-rtos/open_esplibs/libwpa/os_xtensa.c
CC /home/nop/esp-open-rtos/open_esplibs/libwpa/wpa_main.c
AR build/open_esplibs_libwpa.a
LD build/http_get.out
FW firmware/http_get.bin
esptool.py v1.2
esptool.py -p /dev/ttyUSB0 --baud 115200 write_flash -fs 16m -fm qio -ff 40m \
        0x0 ../../bootloader/firmware_prebuilt/rboot.bin 0x1000 ../../bootloader/firmware_prebuilt/blank_config.bin 0x2000 ./firmware/http_get.bin
esptool.py v1.2
Connecting...
Running Cesanta flasher stub...
Flash params set to 0x0030
Writing 4096 @ 0x0... 4096 (100 %)
Wrote 4096 bytes at 0x0 in 0.4 seconds (89.4 kbit/s)...
Writing 4096 @ 0x1000... 4096 (100 %)
Wrote 4096 bytes at 0x1000 in 0.4 seconds (89.4 kbit/s)...
Writing 262144 @ 0x2000... 262144 (100 %)
Wrote 262144 bytes at 0x2000 in 22.7 seconds (92.3 kbit/s)...
Leaving...
make: Leaving directory '/home/nop/esp-open-rtos/examples/http_get'

ビルド中はシリアルポート側のTearTermはこのように表示されます。
おそらくファーム書き込み時の応答コードだと思います。


ファームのビルドが終わると、シリアルポート側のTeraTermに実行結果が表示されます。


なお、ファームビルド時の以下のコマンドは、どちらも同じ動きをしますが、clean時のコマンドが違ってきます。

@
sudo make flash -C examples/http_get

この時は
sudo make clean

A
cd examples/http_get
make
sudo make flash

この時は
make clean



makeのオプションは以下で確認する事ができます。
PlatformIOの様にシリアルモニターを起動するオプションはありません。
ESPの標準出力をモニターする場合は、別途picocomなどのターミナルツールを起動する必要が有ります。
$ make help
esp-open-rtos make

Other targets:

all
Default target. Will build firmware including any changed source files.

clean
Delete all build output.

rebuild
Build everything fresh from scratch.

flash
Build then upload firmware to MCU. Set ESPPORT & ESPBAUD to override port/baud rate.

test
'flash', then start a GNU Screen session on the same serial port to see serial output.

size
Build, then print a summary of built firmware size.

TIPS:
* You can use -jN for parallel builds. Much faster! Use 'make rebuild' instead of 'make clean all' for parallel builds.
* You can create a local.mk file to create local overrides of variables like ESPPORT & ESPBAUD.

SAMPLE COMMAND LINE:
make -j2 test ESPPORT=/dev/ttyUSB0

picocomでESPの標準出力をモニターする場合は、以下のコマンドで書き込みが終わり次第、picocomが起動します。
$ sudo make flash && sudo picocom -b 115200 /dev/ttyUSB0
esptool.py -p /dev/ttyUSB0 --baud 115200 write_flash -fs 16m -fm qio -ff 40m \
        0x0 ../../bootloader/firmware_prebuilt/rboot.bin 0x1000 ../../bootloader/firmware_prebuilt/blank_config.bin 0x2000 ./firmware/http_get.bin
esptool.py v1.2
Connecting...
Running Cesanta flasher stub...
Flash params set to 0x0030
Writing 4096 @ 0x0... 4096 (100 %)
Wrote 4096 bytes at 0x0 in 0.4 seconds (89.4 kbit/s)...
Writing 4096 @ 0x1000... 4096 (100 %)
Wrote 4096 bytes at 0x1000 in 0.4 seconds (89.4 kbit/s)...
Writing 262144 @ 0x2000... 262144 (100 %)
Wrote 262144 bytes at 0x2000 in 22.7 seconds (92.3 kbit/s)...
Leaving...
picocom v2.2

port is        : /dev/ttyUSB0
flowcontrol    : none
baudrate is    : 115200
parity is      : none
databits are   : 8
stopbits are   : 1
escape is      : C-a
local echo is  : no
noinit is      : no
noreset is     : no
nolock is      : no
send_cmd is    : sz -vv
receive_cmd is : rz -vv -E
imap is        :
omap is        :
emap is        : crcrlf,delbs,

Type [C-a] [C-h] to see available commands

Terminal ready
Booting rom 0.
WA絅9 蘆ould not initialize sysparams (1)!

咥箔鑪曁

        斃Ym    5俘      懣?燮楊?} ̄薜x3fff3818, prio:12, stack:200

ESP-Open-SDK ver: 0.0.1 compiled @ Feb 10 2019 11:46:32
phy ver: 273, pp ver: 8.3

SDK version:0.9.9
mode : sta(84:0d:8e:8c:f4:00)
add if0
HTTP get task starting...
Running DNS lookup for httpbin.org...
DNS lookup failed err=202 res=0
scandone
Running DNS lookup for httpbin.org...
DNS lookup failed err=202 res=0
add 0
aid 7
cnt

connected with aterm-e625c0-g, channel 11
dhcp client start...
Running DNS lookup for httpbin.org...
DNS lookup failed err=202 res=0
Running DNS lookup for httpbin.org...
DNS lookup failed err=202 res=0
ip:192.168.10.186,mask:255.255.255.0,gw:192.168.10.1
Running DNS lookup for httpbin.org...
DNS lookup succeeded. IP=52.201.75.180
... allocated socket
... connected
... socket send success
HTTP/1.1 200 OK
Connection: close
Server: gunicorn/19.9.0
Date: Sun, 10 Feb 2019 02:54:28 GMT
Content-Type: application/json
Content-Length: 207
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Via: 1.1 vegur

{
  "args": {},
  "headers": {
    "Connection": "close",
    "Host": "httpbin.org",
    "User-Agent": "esp-open-rtos/0.1 esp8266"
  },
  "origin": "180.200.27.227",
  "url": "http://httpbin.org/get"
}
... done reading from socket. Last read return=0 errno=128
successes = 1 failures = 4

続く....