NodeMcu除了可以使用自家的nodemcu-firmware - 基于eLua的固件,当然还可以使用基于esp-open-rtos系统的固件。个人还是喜欢使用C语言的esp-open-rtos系统。
- 关于NodeMcu
- 编译环境搭建
参考:https://github.com/pfalcon/esp-open-sdk/
我这里使用的系统为Mac OS
由于编译esp-open-rtos需要安装esp-open-sdk, 所以我们需要先安装编译esp-open-sdk:
1. 安装编译工具
$ brew tap homebrew/dupes $ brew install binutils coreutils automake wget gawk libtool help2man gperf gnu-sed --with-default-names grep $ export PATH="/usr/local/opt/gnu-sed/bin:$PATH"
NOTE:
sed需要使用/usr/local/opt/gnu-sed/bin/sed中的sed, 这与官方的文档稍有出路。
2. 下载并编译esp-open-sdk
$ git clone https://github.com/pfalcon/esp-open-sdk.git $ cd esp-open-sdk && git checkout b1929e3 $ git submodule update --init
NOTE:
这里我使用的是commit id为b1929e3:
commit b1929e33f41c4bd9715e5f91ab0cf68de5939396 Author: Max Filippov <jcmvbkbc@gmail.com> Date: Sun Feb 5 21:02:17 2017 -0800 examples/blinky: add -lc to LDLIBS for memchr This fixes issue #213 Signed-off-by: Max Filippov <jcmvbkbc@gmail.com>
如果想使用最新的代码,可以使用如下命令,将esp-open-sdk及其子模块clone下来:
$ git clone --recursive https://github.com/pfalcon/esp-open-sdk.git
3. 编译esp-open-sdk
编译时会从网上下载文件:
esp-open-sdk/crosstool-NG/.build/tarballs/binutils-2.25.1.tar.bz2 esp-open-sdk/crosstool-NG/.build/tarballs/cloog-0.18.4.tar.gz esp-open-sdk/crosstool-NG/.build/tarballs/expat-2.1.0.tar.gz esp-open-sdk/crosstool-NG/.build/tarballs/gcc-4.8.5.tar.bz2 esp-open-sdk/crosstool-NG/.build/tarballs/gdb-7.10.tar.xz esp-open-sdk/crosstool-NG/.build/tarballs/gmp-6.0.0a.tar.xz esp-open-sdk/crosstool-NG/.build/tarballs/isl-0.14.tar.xz esp-open-sdk/crosstool-NG/.build/tarballs/mpc-1.0.3.tar.gz esp-open-sdk/crosstool-NG/.build/tarballs/mpfr-3.1.3.tar.xz esp-open-sdk/crosstool-NG/.build/tarballs/ncurses-6.0.tar.gz esp-open-sdk/crosstool-NG/.build/tarballs/newlib-2.0.0.tar.gz esp-open-sdk/crosstool-NG/.build/tarballs/xtensa_lx106.tar
由https://github.com/SuperHouse/esp-open-rtos/blob/master/README.md可知,编译使用如下命令:
$ make toolchain esptool libhal STANDALONE=n
4. 设置环境变量
编译之后,会在esp-open-sdk/xtensa-lx106-elf/bin所需的编译工具及固件更新工具esptool.py,将这个路径加入PATH:
$ export PATH=`pwd`/xtensa-lx106-elf/bin:$PATH
- 代码下载
参考:https://github.com/SuperHouse/esp-open-rtos/blob/master/README.md
$ git clone https://github.com/Superhouse/esp-open-rtos.git $ cd esp-open-rtos && git checkout f51a210 $ git submodule update --init
NOTE:
我这里使用的commit id为f51a210:
commit f51a2109b1c1a3b4a712b93c5a50d0aa2553fa6f Author: Zaltora <lilian.marazano@gmail.com> Date: Tue Feb 21 20:44:03 2017 +0100 CRC library (#339) * Crc initial example * Update example and fix submodule
- 编译
在esp-open-rtos/example中包含有sample code, 可以编译出来看看:
Makefile ds1302_test http_get_mbedtls pwm_test tests access_point ds1307 http_server sdio_raw tls_server ads1115_test ds18b20_broadcaster i2c_lcd_test serial_echo tls_server_bearssl aws_iot ds18b20_onewire i2s_audio simple tsl2561 blink ds3231_test ina3221_test simple_cplusplus ultrasonic blink_timers experiments json_jsmn_simple sntp wifi_scan bmp180_i2c fatfs max7219_7seg spi_test ws2812_i2s bmp280 fatfs_rtc mcp4725_test spiffs ws2812_rainbow button hd44780_lcd mqtt_client ssd1306_example cpp_01_tasks hmc5883l ms561101ba03 ssd1306_fps crc_example http_get ota_basic sysparam_editor dht_sensor http_get_bearssl pca9685_pwm terminal
对于需要WiFi的例子,如access_point,需要添加include/private_ssid_config.h文件,包含如下内容:
#define WIFI_SSID "mywifissid" #define WIFI_PASS "my secret password"
编译命令如下:
$ make -C examples/access_point/
- 固件更新
可通过如下命令:
$ make -C examples/access_point/ ESPPORT=/dev/cu.SLAB_USBtoUART flash esptool.py -p /dev/cu.SLAB_USBtoUART --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/access_point.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 (90.1 kbit/s)... Writing 4096 @ 0x1000... 4096 (100 %) Wrote 4096 bytes at 0x1000 in 0.4 seconds (90.0 kbit/s)... Writing 249856 @ 0x2000... 249856 (100 %) Wrote 249856 bytes at 0x2000 in 21.7 seconds (92.0 kbit/s)... Leaving...
可以看到这里使用esptool.py(https://github.com/espressif/esptool)脚本对固件进行更新:
- -fs 16m:flash size 为16MiB(?)
- -fm qio:flash mode为qio (https://nodemcu.readthedocs.io/en/master/en/flash/)
- -ff 40m: flash frequency为40Mhz
- 相关的参考文档
- https://github.com/pfalcon/esp-open-sdk/
- https://github.com/SuperHouse/esp-open-rtos
- https://github.com/nodemcu/nodemcu-firmware
- https://github.com/espressif/esptool