ESP32のフラッシュへの書き込みエラー

久しぶりに、「PDMインタフェース MEMS 超音波マイク SPH0641LU4H-1 を ESP32のESP-IDF で試す」のプログラムを修正しようとした。まず、Visual Studio Code (Ver.1.45.0) の Platform IO の Espessif 32 プラットフォーム 1.12.1  (esp-idf 4.0.0)の環境でビルドできるようにして、ボードLolin D32 のフラッシュに書き込もうとしたところエラーになった。リセット回路の問題のようであり、リセットとGNDの間にキャパシタを追加して解消したが、備忘録として残しておく。

書き込みエラー

PlatformIO: Upload によって、プログラムをESP32のフラッシュにアップロードして書き込んだところ、Connecting…….._____….._____….._____….._____….._____….._____….._____

と接続ができず、最終的に次のエラーとなった。

A fatal error occurred: Failed to connect to ESP32: Timed out waiting for packet header *** [upload] Error 2

メッセージ全体は以下の通り。

Processing esp32dev (platform: https://github.com/platformio/platform-espressif32.git; board: lolin_d32; framework: espidf)
------------------------------------------------------------------------------------------------------------------------------------------------

Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/espressif32/lolin_d32.html
PLATFORM: Espressif 32 1.12.1 #32d50ab > WEMOS LOLIN D32
HARDWARE: ESP32 240MHz, 320KB RAM, 4MB Flash
DEBUG: Current (esp-prog) External (esp-prog, iot-bus-jtag, jlink, minimodule, olimex-arm-usb-ocd, olimex-arm-usb-ocd-h, olimex-arm-usb-tiny-h, 
olimex-jtag-tiny, tumpa)
PACKAGES:
 - framework-espidf 3.40000.200303 (4.0.0)
 - tool-cmake 3.16.4
 - tool-esptoolpy 1.20600.0 (2.6.0)
 - tool-idf 1.0.1
 - tool-mconf 1.4060000.20190628 (406.0.0)
 - tool-mkspiffs 2.230.0 (2.30)
 - tool-ninja 1.9.0
 - toolchain-esp32ulp 1.22851.190618 (2.28.51)
 - toolchain-xtensa32 2.80200.200226 (8.2.0)
Warning! Starting with ESP-IDF v4.0, new project structure is required: 
https://docs.platformio.org/en/latest/frameworks/espidf.html#project-structure
Reading CMake configuration...
LDF: Library Dependency Finder -> http://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 2 compatible libraries
Scanning dependencies...
No dependencies
Building in release mode
Retrieving maximum program size .pio\build\esp32dev\firmware.elf
Checking size .pio\build\esp32dev\firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [          ]   3.8% (used 12596 bytes from 327680 bytes)
Flash: [=====     ]  52.5% (used 550257 bytes from 1048576 bytes)
Configuring upload protocol...
AVAILABLE: esp-prog, espota, esptool, iot-bus-jtag, jlink, minimodule, olimex-arm-usb-ocd, olimex-arm-usb-ocd-h, olimex-arm-usb-tiny-h, olimex-jtag-tiny, tumpa
CURRENT: upload_protocol = esptool
Looking for upload port...
Auto-detected: COM5
Uploading .pio\build\esp32dev\firmware.bin
esptool.py v2.6
Serial port COM5
Connecting........_____....._____....._____....._____....._____....._____....._____

A fatal error occurred: Failed to connect to ESP32: Timed out waiting for packet header
*** [upload] Error 2
========================================================= [FAILED] Took 40.63 seconds =========================================================
The terminal process terminated with exit code: 1

原因調査

Lolin D32 ボードで使用されている CH340 USBシリアル変換のドライバが古いかと思って更新してみたが、エラーになるのは変わらなかった。

次に、ESP32 Boot Mode Selection の問題かどうかを確認した。

https://github.com/espressif/esptool/wiki/ESP32-Boot-Mode-Selection によれば、GPIO0がLowで、GPIO2がハイインピーダンス状態かLowであれば、リセットすることで bootloader mode となり、フラッシュへの書き込みがされる。

ボードに GPIO0をLowにするBoot のボタンがあればそれを押しておき、書き込みが始まったら、Bootボタンを放すということが試せる。しかし、使っているボードが、Lolin D32 で、Bootのボタンがない。

しかたがないので、ジャンパー線でGPIO0をGNDに落としておき、書き込みが始まるか試してみたところ、書き込みが始まったので、リセット時に Boot Mode にうまくなっていないことが原因と思われた。

https://docs.wemos.cc/en/latest/d32/d32.html にある回路図  (Schematic V1.0.0[PDF])を見たところでは、RESETとENが接続され、100nFのコンデンサがRESETとGNDとの間に入っている。RESET(EN)がシリアルのDTRとRTSで制御されるようになっており、また、GPIO0もシリアルのDTRとRTSで異なった論理で制御されるようになっている。

GPIO2は結線されておらずハイインピーダンス状態で、GPIO0がLowならば、Bootモードになる状態になっている。

以前、ESP8266のリセット回路 でみた問題と考えられた。つまり、Boot ModeにするためにDTRとRTSが制御されるが、プログラムでGPIO0をLowにするよりも前にRESETがHighになってしまい、Boot Modeにならない。

100nFのコンデンサがRESETとGNDとの間に入っており、RESETの立ち上がりを遅くしているが、容量が不足のようだ。

問題解消法と確認

ボードに入っている100nFだけでは容量不足と考えられることから、100nF(0.1μF)のコンデンサをさらに、RESETとGNDの間に入れてみたが、うまくいかなかった。

まだ不足と思われることから、1μFのコンデンサをRESETとGNDの間に入れてみたところBoot Modeになって、以下のように書き込みが正常に行われるようになった。

Processing esp32dev (platform: https://github.com/platformio/platform-espressif32.git; board: lolin_d32; framework: espidf)
------------------------------------------------------------------------------------------------------------------------------------------------

Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/espressif32/lolin_d32.html
PLATFORM: Espressif 32 1.12.1 #32d50ab > WEMOS LOLIN D32
HARDWARE: ESP32 240MHz, 320KB RAM, 4MB Flash
DEBUG: Current (esp-prog) External (esp-prog, iot-bus-jtag, jlink, minimodule, olimex-arm-usb-ocd, olimex-arm-usb-ocd-h, olimex-arm-usb-tiny-h, 
olimex-jtag-tiny, tumpa)
PACKAGES:
 - framework-espidf 3.40000.200303 (4.0.0)
 - tool-cmake 3.16.4
 - tool-esptoolpy 1.20600.0 (2.6.0)
 - tool-idf 1.0.1
 - tool-mconf 1.4060000.20190628 (406.0.0)
 - tool-mkspiffs 2.230.0 (2.30)
 - tool-ninja 1.9.0
 - toolchain-esp32ulp 1.22851.190618 (2.28.51)
 - toolchain-xtensa32 2.80200.200226 (8.2.0)
Warning! Starting with ESP-IDF v4.0, new project structure is required: 
https://docs.platformio.org/en/latest/frameworks/espidf.html#project-structure
Reading CMake configuration...
LDF: Library Dependency Finder -> http://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 2 compatible libraries
Scanning dependencies...
No dependencies
Building in release mode
Retrieving maximum program size .pio\build\esp32dev\firmware.elf
Checking size .pio\build\esp32dev\firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [          ]   3.8% (used 12596 bytes from 327680 bytes)
Flash: [=====     ]  52.5% (used 550257 bytes from 1048576 bytes)
Configuring upload protocol...
AVAILABLE: esp-prog, espota, esptool, iot-bus-jtag, jlink, minimodule, olimex-arm-usb-ocd, olimex-arm-usb-ocd-h, olimex-arm-usb-tiny-h, olimex-jtag-tiny, tumpa
CURRENT: upload_protocol = esptool
Looking for upload port...
Auto-detected: COM5
Uploading .pio\build\esp32dev\firmware.bin
esptool.py v2.6
Serial port COM5
Connecting....
Chip is ESP32D0WDQ6 (revision 1)
Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None
MAC: 80:7d:3a:81:6d:f0
Uploading stub...
Running stub...
Stub running...
Configuring flash size...
Auto-detected Flash size: 4MB
Compressed 25504 bytes to 14985...

Writing at 0x00001000... (100 %)
Wrote 25504 bytes (14985 compressed) at 0x00001000 in 1.3 seconds (effective 152.4 kbit/s)...
Hash of data verified.
Compressed 3072 bytes to 119...

Writing at 0x00008000... (100 %)
Wrote 3072 bytes (119 compressed) at 0x00008000 in 0.0 seconds (effective 983.1 kbit/s)...
Hash of data verified.
Compressed 550368 bytes to 447341...

Writing at 0x00010000... (3 %)
Writing at 0x00014000... (7 %)
Writing at 0x00018000... (10 %)
Writing at 0x0001c000... (14 %)
Writing at 0x00020000... (17 %)
Writing at 0x00024000... (21 %)
Writing at 0x00028000... (25 %)
Writing at 0x0002c000... (28 %)
Writing at 0x00030000... (32 %)
Writing at 0x00034000... (35 %)
Writing at 0x00038000... (39 %)
Writing at 0x0003c000... (42 %)
Writing at 0x00040000... (46 %)
Writing at 0x00044000... (50 %)
Writing at 0x00048000... (53 %)
Writing at 0x0004c000... (57 %)
Writing at 0x00050000... (60 %)
Writing at 0x00054000... (64 %)
Writing at 0x00058000... (67 %)
Writing at 0x0005c000... (71 %)
Writing at 0x00060000... (75 %)
Writing at 0x00064000... (78 %)
Writing at 0x00068000... (82 %)
Writing at 0x0006c000... (85 %)
Writing at 0x00070000... (89 %)
Writing at 0x00074000... (92 %)
Writing at 0x00078000... (96 %)
Writing at 0x0007c000... (100 %)
Wrote 550368 bytes (447341 compressed) at 0x00010000 in 39.7 seconds (effective 110.8 kbit/s)...
Hash of data verified.

Leaving...
Hard resetting via RTS pin...
========================================================= [SUCCESS] Took 64.07 seconds =========================================================