はじめに
人間(僕だけ?)と言うのは実に怠け者です。ボタンを押しながら電源入れる(とかリセットする)とか複数のボタンを順番に押すとブートモードになってUART(とかUSB)からMPUに書き込めるってのは御約束の儀式(様式美)ですがRTSやCTSを使ってノータッチ(無詠唱)で書き込める経験をしちゃうとやめられません。世の中にはCH340とかFT232を使ったUARTがいっぱいあってオートブートモードに対応した板も多く売られています。という事はそれだけのニーズがあるからこそでもあります。諸兄の弛まぬ御努力により半世紀以上の渡り性能はインフレ、価格はデフレが続いた業界でした。しかしながら世相の影響などで近年は物価高騰や入手困難が続くようになっております。だからこそ一般人として業界へ御布施の必要性は承知しておりますが庶民としては板付きより少しでも安い生石を買ってきて何とかしたいと思うのもまた自然の摂理かと存じます。MPU(CPU)だけではなく同じことがUSB-UARTブリッジを含めた電子系パーツ全般に言えます。生石同士だと大差ないのですが板付きだとCH340x(秋月なら500円~、aitendoなら売り切れ)の方がCH552P(秋月なら110円+USBソケット代)より高い状況を鑑みるとUSBaspにも使えるCH552Pを踏み台にするのが合理的と判断しました。その上 CH552P(ソフト) で組めば例のトランジスタとかFETをクロスさせる回路とか要らないしね。
用意するもの
結果的には"CH55 ESP"でググると https://github.com/diodep/ch55x_esp を見つけたので著者に感謝を捧げつつ download して make します。
$ make
sdcc -c -V -mmcs51 --model-small --xram-size 0x0300 --xram-loc 0x0100 --code-size 0x2800 -I/c/Users/2ru/source/repos/ch55x_esp/ch554_sdcc/examples/../include -DFREQ_SYS=24000000 --opt-code-speed main.c
+ ""C:\msys64\mingw64\bin\sdcpp.exe" -nostdinc -Wall -std=c11 -I"C:/Users/2ru/source/repos/ch55x_esp/ch554_sdcc/examples/../include" -D"FREQ_SYS=24000000" --obj-ext=.rel -D__SDCC_CHAR_UNSIGNED -D__SDCC_MODEL_SMALL -D__SDCC_OPTIMIZE_SPEED -D__SDCC_FLOAT_REENT -D__SDCCCALL=0 -D__SDCC=4_5_0 -D__SDCC_VERSION_MAJOR=4 -D__SDCC_VERSION_MINOR=5 -D__SDCC_VERSION_PATCH=0 -DSDCC=450 -D__SDCC_REVISION=15242 -D__SDCC_mcs51 -D__STDC_NO_COMPLEX__=1 -D__STDC_NO_THREADS__=1 -D__STDC_NO_ATOMICS__=1 -D__STDC_NO_VLA__=1 -D__STDC_ISO_10646__=201409L -D__SIZEOF_FLOAT__=4 -D__SIZEOF_DOUBLE__=4 -D__SDCC_BITINT_MAXWIDTH=64 -isystem "C:\msys64\mingw64\bin\..\share\sdcc\include\mcs51" -isystem "C:\msys64\mingw64\bin\..\share\sdcc\include" -xc "main.c" "
c:\users\2ru\source\repos\ch55x_esp\ch554_sdcc\include\debug.h:16: warning 283: function declarator with no prototype
c:\users\2ru\source\repos\ch55x_esp\ch554_sdcc\include\debug.h:22: warning 283: function declarator with no prototype
c:\users\2ru\source\repos\ch55x_esp\ch554_sdcc\include\debug.h:23: warning 283: function declarator with no prototype
c:\users\2ru\source\repos\ch55x_esp\ch554_sdcc\include\debug.h:24: warning 283: function declarator with no prototype
c:\users\2ru\source\repos\ch55x_esp\ch554_sdcc\include\debug.h:27: warning 283: function declarator with no prototype
c:\users\2ru\source\repos\ch55x_esp\ch554_sdcc\include\debug.h:28: warning 283: function declarator with no prototype
c:\users\2ru\source\repos\ch55x_esp\ch554_sdcc\include\debug.h:29: warning 283: function declarator with no prototype
main.c:103: warning 283: function declarator with no prototype
main.c:117: warning 283: function declarator with no prototype
main.c:144: warning 283: function declarator with no prototype
main.c:161: warning 283: function declarator with no prototype
main.c:320: warning 158: overflow in implicit constant conversion
main.c:728: warning 283: function declarator with no prototype
main.c:824: syntax error: token -> '1' ; column 46
main.c:15: fatal error: when writing output to : Broken pipe
15 |
|
compilation terminated.
subprocess error 1
make: *** [../ch554_sdcc/examples/Makefile.include:42: main.rel] エラー 1
アレ~っ、SYNTAXエラーが出てますね。
エラーの出てる824行を
void Uart0_ISR(void) __interrupt (INT_NO_UART0) __using(1) __naked
に書き換えて再度makeします。
$ make
sdcc -c -V -mmcs51 --model-small --xram-size 0x0300 --xram-loc 0x0100 --code-size 0x2800 -I/c/Users/2ru/source/repos/ch55x_esp/ch554_sdcc/examples/../include -DFREQ_SYS=24000000 --opt-code-speed main.c
+ ""C:\msys64\mingw64\bin\sdcpp.exe" -nostdinc -Wall -std=c11 -I"C:/Users/2ru/source/repos/ch55x_esp/ch554_sdcc/examples/../include" -D"FREQ_SYS=24000000" --obj-ext=.rel -D__SDCC_CHAR_UNSIGNED -D__SDCC_MODEL_SMALL -D__SDCC_OPTIMIZE_SPEED -D__SDCC_FLOAT_REENT -D__SDCCCALL=0 -D__SDCC=4_5_0 -D__SDCC_VERSION_MAJOR=4 -D__SDCC_VERSION_MINOR=5 -D__SDCC_VERSION_PATCH=0 -DSDCC=450 -D__SDCC_REVISION=15242 -D__SDCC_mcs51 -D__STDC_NO_COMPLEX__=1 -D__STDC_NO_THREADS__=1 -D__STDC_NO_ATOMICS__=1 -D__STDC_NO_VLA__=1 -D__STDC_ISO_10646__=201409L -D__SIZEOF_FLOAT__=4 -D__SIZEOF_DOUBLE__=4 -D__SDCC_BITINT_MAXWIDTH=64 -isystem "C:\msys64\mingw64\bin\..\share\sdcc\include\mcs51" -isystem "C:\msys64\mingw64\bin\..\share\sdcc\include" -xc "main.c" "
c:\users\2ru\source\repos\ch55x_esp\ch554_sdcc\include\debug.h:16: warning 283: function declarator with no prototype
c:\users\2ru\source\repos\ch55x_esp\ch554_sdcc\include\debug.h:22: warning 283: function declarator with no prototype
c:\users\2ru\source\repos\ch55x_esp\ch554_sdcc\include\debug.h:23: warning 283: function declarator with no prototype
c:\users\2ru\source\repos\ch55x_esp\ch554_sdcc\include\debug.h:24: warning 283: function declarator with no prototype
c:\users\2ru\source\repos\ch55x_esp\ch554_sdcc\include\debug.h:27: warning 283: function declarator with no prototype
c:\users\2ru\source\repos\ch55x_esp\ch554_sdcc\include\debug.h:28: warning 283: function declarator with no prototype
c:\users\2ru\source\repos\ch55x_esp\ch554_sdcc\include\debug.h:29: warning 283: function declarator with no prototype
main.c:103: warning 283: function declarator with no prototype
main.c:117: warning 283: function declarator with no prototype
main.c:144: warning 283: function declarator with no prototype
main.c:161: warning 283: function declarator with no prototype
main.c:320: warning 158: overflow in implicit constant conversion
main.c:728: warning 283: function declarator with no prototype
main.c:1082: warning 283: function declarator with no prototype
main.c:1094: warning 283: function declarator with no prototype
main.c:1095: warning 278: return type of function omitted, assuming int
+ ""C:\msys64\mingw64\bin\sdas8051.exe" -plosgffw "main.rel" "main".asm"
sdcc -c -V -mmcs51 --model-small --xram-size 0x0300 --xram-loc 0x0100 --code-size 0x2800 -I/c/Users/2ru/source/repos/ch55x_esp/ch554_sdcc/examples/../include -DFREQ_SYS=24000000 --opt-code-speed ../ch554_sdcc/include/debug.c
+ ""C:\msys64\mingw64\bin\sdcpp.exe" -nostdinc -Wall -std=c11 -I"C:/Users/2ru/source/repos/ch55x_esp/ch554_sdcc/examples/../include" -D"FREQ_SYS=24000000" --obj-ext=.rel -D__SDCC_CHAR_UNSIGNED -D__SDCC_MODEL_SMALL -D__SDCC_OPTIMIZE_SPEED -D__SDCC_FLOAT_REENT -D__SDCCCALL=0 -D__SDCC=4_5_0 -D__SDCC_VERSION_MAJOR=4 -D__SDCC_VERSION_MINOR=5 -D__SDCC_VERSION_PATCH=0 -DSDCC=450 -D__SDCC_REVISION=15242 -D__SDCC_mcs51 -D__STDC_NO_COMPLEX__=1 -D__STDC_NO_THREADS__=1 -D__STDC_NO_ATOMICS__=1 -D__STDC_NO_VLA__=1 -D__STDC_ISO_10646__=201409L -D__SIZEOF_FLOAT__=4 -D__SIZEOF_DOUBLE__=4 -D__SDCC_BITINT_MAXWIDTH=64 -isystem "C:\msys64\mingw64\bin\..\share\sdcc\include\mcs51" -isystem "C:\msys64\mingw64\bin\..\share\sdcc\include" -xc "../ch554_sdcc/include/debug.c" "
../ch554_sdcc/include/debug.h:16: warning 283: function declarator with no prototype
../ch554_sdcc/include/debug.h:22: warning 283: function declarator with no prototype
../ch554_sdcc/include/debug.h:23: warning 283: function declarator with no prototype
../ch554_sdcc/include/debug.h:24: warning 283: function declarator with no prototype
../ch554_sdcc/include/debug.h:27: warning 283: function declarator with no prototype
../ch554_sdcc/include/debug.h:28: warning 283: function declarator with no prototype
../ch554_sdcc/include/debug.h:29: warning 283: function declarator with no prototype
../ch554_sdcc/include/debug.c:27: warning 283: function declarator with no prototype
../ch554_sdcc/include/debug.c:147: warning 283: function declarator with no prototype
../ch554_sdcc/include/debug.c:161: warning 283: function declarator with no prototype
../ch554_sdcc/include/debug.c:193: warning 283: function declarator with no prototype
../ch554_sdcc/include/debug.c:239: warning 283: function declarator with no prototype
../ch554_sdcc/include/debug.c:253: warning 283: function declarator with no prototype
../ch554_sdcc/include/debug.c:265: warning 283: function declarator with no prototype
../ch554_sdcc/include/debug.c:280: warning 283: function declarator with no prototype
+ ""C:\msys64\mingw64\bin\sdas8051.exe" -plosgffw "debug.rel" "debug".asm"
sdcc main.rel debug.rel -V -mmcs51 --model-small --xram-size 0x0300 --xram-loc 0x0100 --code-size 0x2800 -I/c/Users/2ru/source/repos/ch55x_esp/ch554_sdcc/examples/../include -DFREQ_SYS=24000000 --opt-code-speed -o usb_ft232bm.ihx
+ ""C:\msys64\mingw64\bin\sdld.exe" -nf "usb_ft232bm.lk""
objcopy -I ihex -O binary usb_ft232bm.ihx usb_ft232bm.bin
packihx usb_ft232bm.ihx > usb_ft232bm.hex
packihx: read 148 lines, wrote 271: OK.
出来あがった usb_ft232bm.hex(気持ちは分かるけどねぇ~) をWCHISPStudioでCH552Pに焼きます

配線
| AE-QFN16-DIP-CH552P | AE-ESP32-WROOM-32E-MINI | |
|---|---|---|
| TXD0 = P3.1(9pin) | ~~~> | RX |
| RXD0 = P3.0(10pin) | <--- | TX |
| GND = GND(0pin) | ---- | GND |
| TXD1 = P1.7(6pin) | RTS~~> | IO0(19pin) |
| CAP1 = P1.0(8pin) | DTR~~> | EN(2pin) |
皆さんがトランジスタ/FETをクロスで組んでる回路はmain.cの337~380行にかけてのコードで実装されているので不要です。ただし面倒なんでCH552Pは5VのままESP32は3.3Vで行きます。レベルコンバータの買い置きが無かったのとフォトカプラ並べるのが面倒だったんで僕は10kΩと10KΩx2の分圧回路を3組とESP32からCH552Pの戻りは直結にしちゃいました。
書き込みテスト
CH552Pはボーレートのクロック分周の都合で高速域は誤差が大きくなります。安全運転のためArduino IDEを立ち上げたらツール→Upload Speed→115200bpsにボーレート設定して偽JJYプログラムを書き込んでみます。
C:\Users\2ru\AppData\Local\Arduino15\packages\esp32\tools\esptool_py\5.1.0/esptool.exe" --chip esp32 --port "COM14" --baud 115200 --before default-reset --after hard-reset write-flash -z --flash-mode keep --flash-freq keep --flash-size keep 0x1000 "C:\Users\2ru\AppData\Local\arduino\sketches\1265600A096E427FBE620E55B6ED0C53/esp32_NTP2JJY.ino.bootloader.bin" 0x8000 "C:\Users\2ru\AppData\Local\arduino\sketches\1265600A096E427FBE620E55B6ED0C53/esp32_NTP2JJY.ino.partitions.bin" 0xe000 "C:\Users\2ru\AppData\Local\Arduino15\packages\esp32\hardware\esp32\3.3.5/tools/partitions/boot_app0.bin" 0x10000 "C:\Users\2ru\AppData\Local\arduino\sketches\1265600A096E427FBE620E55B6ED0C53/esp32_NTP2JJY.ino.bin"
esptool v5.1.0
Serial port COM14:
Connecting....
Connected to ESP32 on COM14:
Chip type: ESP32-D0WD-V3 (revision v3.1)
Features: Wi-Fi, BT, Dual Core + LP Core, 240MHz, Vref calibration in eFuse, Coding Scheme None
Crystal frequency: 40MHz
MAC: xx:xx:xx:xx:xx:xx
Uploading stub flasher...
Running stub flasher...
Stub flasher running.
Configuring flash size...
Flash will be erased from 0x00001000 to 0x00007fff...
Flash will be erased from 0x00008000 to 0x00008fff...
Flash will be erased from 0x0000e000 to 0x0000ffff...
Flash will be erased from 0x00010000 to 0x000effff...
Compressed 25184 bytes to 16080...
Writing at 0x00001000 [ ] 0.0% 0/16080 bytes...
Writing at 0x00007260 [==============================] 100.0% 16080/16080 bytes...
Wrote 25184 bytes (16080 compressed) at 0x00001000 in 1.8 seconds (111.8 kbit/s).
Hash of data verified.
Compressed 3072 bytes to 146...
Writing at 0x00008000 [ ] 0.0% 0/146 bytes...
Writing at 0x00008c00 [==============================] 100.0% 146/146 bytes...
Wrote 3072 bytes (146 compressed) at 0x00008000 in 0.1 seconds (346.4 kbit/s).
Hash of data verified.
Compressed 8192 bytes to 47...
Writing at 0x0000e000 [ ] 0.0% 0/47 bytes...
Writing at 0x00010000 [==============================] 100.0% 47/47 bytes...
Wrote 8192 bytes (47 compressed) at 0x0000e000 in 0.1 seconds (710.7 kbit/s).
Hash of data verified.
Compressed 915632 bytes to 589133...
Writing at 0x00010000 [ ] 0.0% 0/589133 bytes...
Writing at 0x0001c45c [ ] 2.8% 16384/589133 bytes...
Writing at 0x00026232 [> ] 5.6% 32768/589133 bytes...
Writing at 0x0003390e [=> ] 8.3% 49152/589133 bytes...
Writing at 0x0003c59d [==> ] 11.1% 65536/589133 bytes...
Writing at 0x00042289 [===> ] 13.9% 81920/589133 bytes...
Writing at 0x000477e8 [====> ] 16.7% 98304/589133 bytes...
Writing at 0x0004ca2b [====> ] 19.5% 114688/589133 bytes...
Writing at 0x00051ff0 [=====> ] 22.2% 131072/589133 bytes...
Writing at 0x00057596 [======> ] 25.0% 147456/589133 bytes...
Writing at 0x0005c764 [=======> ] 27.8% 163840/589133 bytes...
Writing at 0x00061de1 [========> ] 30.6% 180224/589133 bytes...
Writing at 0x00067479 [=========> ] 33.4% 196608/589133 bytes...
Writing at 0x0006cc60 [=========> ] 36.2% 212992/589133 bytes...
Writing at 0x00072512 [==========> ] 38.9% 229376/589133 bytes...
Writing at 0x000777a3 [===========> ] 41.7% 245760/589133 bytes...
Writing at 0x0007ca80 [============> ] 44.5% 262144/589133 bytes...
Writing at 0x0008236f [=============> ] 47.3% 278528/589133 bytes...
Writing at 0x000879c3 [==============> ] 50.1% 294912/589133 bytes...
Writing at 0x0008d385 [==============> ] 52.8% 311296/589133 bytes...
Writing at 0x00092feb [===============> ] 55.6% 327680/589133 bytes...
Writing at 0x0009887a [================> ] 58.4% 344064/589133 bytes...
Writing at 0x0009de22 [=================> ] 61.2% 360448/589133 bytes...
Writing at 0x000a373d [==================> ] 64.0% 376832/589133 bytes...
Writing at 0x000a9204 [===================> ] 66.7% 393216/589133 bytes...
Writing at 0x000aec5f [===================> ] 69.5% 409600/589133 bytes...
Writing at 0x000b43f6 [====================> ] 72.3% 425984/589133 bytes...
Writing at 0x000b9d86 [=====================> ] 75.1% 442368/589133 bytes...
Writing at 0x000bfcec [======================> ] 77.9% 458752/589133 bytes...
Writing at 0x000c5897 [=======================> ] 80.7% 475136/589133 bytes...
Writing at 0x000cb110 [========================> ] 83.4% 491520/589133 bytes...
Writing at 0x000d3fb5 [========================> ] 86.2% 507904/589133 bytes...
Writing at 0x000d981b [=========================> ] 89.0% 524288/589133 bytes...
Writing at 0x000deb36 [==========================> ] 91.8% 540672/589133 bytes...
Writing at 0x000e4387 [===========================> ] 94.6% 557056/589133 bytes...
Writing at 0x000e9ee9 [============================> ] 97.3% 573440/589133 bytes...
Writing at 0x000ef8b0 [==============================] 100.0% 589133/589133 bytes...
Wrote 915632 bytes (589133 compressed) at 0x00010000 in 53.8 seconds (136.2 kbit/s).
Hash of data verified.
Hard resetting via RTS pin...
バッチリですね。
まとめ
一番時間を要したのはファームウェア(先人の叡智)を見つけ出す事で1週間以上かかってます。配線ミスして(5V3.3V問題があるんでビビり(確認し)ながらやったんで)数時間を要しましたが良い経験です。ピンの初期化が悪いんじゃないかとかアレコレ悩み勉強(大概、先人が正しいの法則)させていただきました。転送レートが8分の1なのがネックですが10秒待つか1分待つかの違いなんで(昭和のプログラマーである)僕的には許容内です。これが10分待つか1時間待つかだとダメでしょうけどね。いずれにしても1時間ググって見つからないなら秋月で買うのが合理的なのは間違いないです。最低賃金が1000円を超える時代に於いて(更には買いに行く運賃なり通販なら送料を加えるても)皆様におかれましては費用対効果を鑑みてと買ってきた方が明らかにリーズナブルだと思います。
