安裝 USB 轉 TTL 芯片驅動
我的 ESP32 開發板用的 USB 轉 TTL 芯片是 CH340,因此需要安裝 CH340 的驅動。
CH340 芯片的官方網站是:
Windows 驅動在以下頁面下載:
下載完后默認安裝即可。如果提示失敗,那就關掉程序重新打開。實在不行就重啟操作系統再試。
安裝后查看 Windows 能否識別。
-
把 ESP32 開發板連接到 Windows。
-
打開 Windows 的設備管理器,在【端口(COM 和 LPT)】底下可以看到 CH340,后面的 COM 帶了個數字。
這個數字不固定,比如我的是 COM7,實際以設備管理器顯示的為准。 -
打開 Arduino 或者其他串口工具,選擇剛剛看到的 COM 口進行連接,選擇 115200 波特率。就可以看到輸出了[1]。
此時的輸出可能是:rst:0x10 (RTCWDT_RTC_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT) flash read err, 1000 ets_main.c 371
現在可以忽略這個錯誤。
刷入 MicroPython 固件
第一步:下載 MicroPython 固件
進入 MicroPython 的官方網站下載頁面:
找到 ESP32,例如 “Generic ESP32 module”。點進去。
找到 Firmware with ESP-IDF
。這個表示它包含了 ESP 官方的 ESP-IDF[2],不用怕下載錯。選擇 bin 文件的方式:
- 如果買的開發板是基於 ESP32-WROVER 模組,則選擇 GENERIC-SPIRAM,否則選 GENERIC。
- v 版本選最新,但不選包含 unstable 的 bin 文件,因為不穩定。
第二步:電腦安裝 Python (不是 ESP32)。電腦如已安裝 Python 則跳過。
Python 的官方網站:
隨便下載個版本,最新的也行。按默認選項的安裝。
可以先將軟件源設置為清華大學的鏡像(也可以不配置):
pip install pip -U
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
第三步:安裝 ESP 工具
通過 Python 的 pip 安裝 ESP 工具。
pip install esptool
安裝過程會出現以下內容:
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting esptool
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/dd/3d/d1d4c004927e6e6807c441ce70330ed969c725d2906053fbd2ff994b4439/esptool-3.0.tar.gz (149 kB)
|████████████████████████████████| 149 kB 1.1 MB/s
Collecting bitstring>=3.1.6
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/c3/fc/ffac2c199d2efe1ec5111f55efeb78f5f2972456df6939fea849f103f9f5/bitstring-3.1.7.tar.gz (195 kB)
|████████████████████████████████| 195 kB 6.4 MB/s
Collecting cryptography>=2.1.4
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/64/03/b2a66da95d0a0acac2b5348526f9b92302136563444b33c7049cbdfecf69/cryptography-3.4.6-cp36-abi3-win_amd64.whl (1.6 MB)
|████████████████████████████████| 1.6 MB 6.4 MB/s
Collecting cffi>=1.12
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/aa/0c/20c3ccdb32fdf86e38901d548f0e11b47d7e037b95373efc1c2379129358/cffi-1.14.5-cp39-cp39-win_amd64.whl (179 kB)
|████████████████████████████████| 179 kB 6.4 MB/s
Collecting ecdsa>=0.16.0
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/98/16/70be2716e24eaf5d81074bb3c05429d60292c2a96613a78ac3d69526a |████████████████████████████████| 104 kB 6.4 MB/s
Collecting pyserial>=3.0
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/07/bc/587a445451b253b285629263eb51c2d8e9bcea4fc97826266d186f96f558/pyserial-3.5-py2.py3-none-any.whl (90 kB)
|████████████████████████████████| 90 kB 2.6 MB/s
Collecting reedsolo<=1.5.4,>=1.5.3
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/c8/cb/bb2ddbd00c9b4215dd57a2abf7042b0ae222b44522c5eb664a8fd9d786da/reedsolo-1.5.4.tar.gz (271 kB)
|████████████████████████████████| 271 kB 6.4 MB/s
Collecting six>=1.9.0
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/ee/ff/48bde5c0f013094d729fe4b0316ba2a24774b3ff1c52d924a8a4cb04078a/six-1.15.0-py2.py3-none-any.whl (10 kB)
Collecting pycparser
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/ae/e7/d9c3a176ca4b02024debf82342dab36efadfc5776f9c8db077e8f6e71821/pycparser-2.20-py2.py3-none-any.whl (112 kB)
|████████████████████████████████| 112 kB 6.8 MB/s
Using legacy 'setup.py install' for esptool, since package 'wheel' is not installed.
Using legacy 'setup.py install' for bitstring, since package 'wheel' is not installed.
Using legacy 'setup.py install' for reedsolo, since package 'wheel' is not installed.
Installing collected packages: pycparser, six, cffi, reedsolo, pyserial, ecdsa, cryptography, bitstring, esptool
Running setup.py install for reedsolo ... done
Running setup.py install for bitstring ... done
Running setup.py install for esptool ... done
Successfully installed bitstring-3.1.7 cffi-1.14.5 cryptography-3.4.6 ecdsa-0.16.1 esptool-3.0 pycparser-2.20 pyserial-3.5 reedsolo-1.5.4 six-1.15.0
看到 Successfully installed
表示安裝成功。
使用這個工具查看 ESP32 的串口:
esptool.py chip_id
比如我的輸出是:
esptool.py v3.0
Found 4 serial ports
Serial port COM7
Detecting chip type... ESP32
Chip is ESP32-D0WDQ6 (revision 1)
Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None
Crystal is 40MHz
MAC: xx:xx:xx:xx:xx:xx
Uploading stub...
Running stub...
Stub running...
Warning: ESP32 has no Chip ID. Reading MAC instead.
MAC: xx:xx:xx:xx:xx:xx
Hard resetting via RTS pin...
Serial port COM7
表示 ESP32 在 COM7。
第四步:清除 ESP32 開發板的 flash 芯片已有的內容
這一步是為了確保 MicroPython 刷入 flash 時的成功率。
esptool.py --chip esp32 --port COM7 erase_flash
以上的 COM7 替換為設備管理器里顯示的 ESP32 串口。下同,不贅述。
第五步:刷入 MicroPython
esptool.py --chip esp32 --port COM7 --baud 460800 write_flash -z 0x1000 D:\esp32-idf4-20210202-v1.14.bin
上面的 D:\esp32-idf4-20210202-v1.14.bin
換成剛才下載的 bin 文件的實際位置。
得到以下輸出:
esptool.py v3.0
Serial port COM7
Connecting......
Detecting chip type... ESP32
Chip is ESP32-D0WDQ6 (revision 1)
Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None
Crystal is 40MHz
MAC: xx:xx:xx:xx:xx:xx
Uploading stub...
Running stub...
Stub running...
Configuring flash size...
Compressed 1484624 bytes to 951640...
Wrote 1484624 bytes (951640 compressed) at 0x00001000 in 84.3 seconds (effective 140.9 kbit/s)...
Hash of data verified.
Leaving...
Hard resetting via RTS pin...
中間需要等一陣子。
第六步:驗證 MicroPython
現在,用串口工具連接到 ESP32。
如果沒有什么反應,那么按一下 Enter 鍵看看。會出現:
>>>
打印個 hello world:
>>> print("hello world")
得到輸出:
hello world