0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ESP32-C3 SuperMini 用 オリジナル CircuitPython をビルドする

Last updated at Posted at 2025-05-12

はじめに

esp32-c3_super-mini.png

10個で 2,144円(送料込み)と安価な ESP32-C3 SuperMini を購入した。
いくつかのタイプがあるようだが、今回購入したものは、裏面に HW-466AB のプリントがあるもの。

ボード情報
% esptool.py --chip auto --port /dev/cu.usbmodem1201 flash_id
esptool.py v4.8.1
Serial port /dev/cu.usbmodem1201
Connecting...
Detecting chip type... ESP32-C3
Chip is ESP32-C3 (QFN32) (revision v0.4)
Features: WiFi, BLE, Embedded Flash 4MB (XMC)
Crystal is 40MHz
MAC: aa:bb:cc:xx:yy:zz
Uploading stub...
Running stub...
Stub running...
Manufacturer: 20
Device: 4016
Detected flash size: 4MB
Hard resetting via RTS pin...


Maker Go ESP32-C3 SuperMini向けの CircuitPython が提供されており、ピンアサインが同じため そのままこのボードで使用できます。

今回は、購入した ESP32-C3 SuperMini に 4つのボタンと 1つのフルカラーLEDを 追加した オリジナルボード 向けに CircuitPython をビルドします。

0. ビルド 環境準備

CircuitPythonをクローンして、esp-idf環境をセットアップ

cd /path/to/you
git clone https://github.com/adafruit/circuitpython.git
cd circuitpython
git checkout main # or git checkout 9.2.7
cd ports/espressif
make fetch-port-submodules

esp-idf/install.sh
source esp-idf/export.sh
cd ../..
pip install --upgrade -r requirements-dev.txt
pip install --upgrade -r requirements-doc.txt

1. ピンアサイン

オリジナルボード の ピンアサイン は次のとおり。

✅オリジナル仕様

Pin Analog Peripheral PWM Function
5V
GND
3V3
4 A4 SCK ✅RGBLED
3 A3 ✅SW2
2 A2 ✅SW4
1 A1 ✅SW1
0 A0 ✅SW3
5 A5 MISO
6 MOSI
7 SS
8 SDA LED
9 SCL BOOT0 /
BUTTON
10
20 RXD
21 TXD

2. オリジナル ボード を定義

2.1 前提

ターミナルを起動しcircuitpython.gitをクローンしたディレクトリに移動しておく。

% cd /path/to/you/circuitpython

2.2 流用ボードをコピー

Maker Go ESP32-C3 SuperMiniを流用します。
オリジナルボードの名前をnak435_esp32c3_superminiとしました。

circuitpython % cd ports/espressif/boards

boards % cp -r makergo_esp32c3_supermini nak435_esp32c3_supermini 

ちなみに、Maker Go ESP32-C3 SuperMiniボードをサポートしたのは、CircuitPython 9.1.0-beta.0からのようです。#9131

2.3 ファイルの修正

コピーしたディレクトリにある、次の2つのファイルに変更を加えます。

  • mpconfigboard.h(ボード名の変更)
  • pins.c(ピン定義の追加)
  • mpconfigboard.mk(インクルード ライブラリの追加)
nak435_esp32c3_supermini/mpconfigboard.h
- #define MICROPY_HW_BOARD_NAME       "Maker Go ESP32C3 Supermini"
+ #define MICROPY_HW_BOARD_NAME       "nak435 ESP32-C3 Supermini"
  #define MICROPY_HW_MCU_NAME         "ESP32-C3"
nak435_esp32c3_supermini/pins.c
    // LED
    { MP_ROM_QSTR(MP_QSTR_LED), MP_ROM_PTR(&pin_GPIO8) },
+   // RGB
+   { MP_ROM_QSTR(MP_QSTR_RGB), MP_ROM_PTR(&pin_GPIO4) },
    
    // SPI
    { MP_ROM_QSTR(MP_QSTR_SCK), MP_ROM_PTR(&pin_GPIO4) },

 (中略)
    
    // Button
    { MP_ROM_QSTR(MP_QSTR_BUTTON), MP_ROM_PTR(&pin_GPIO9) },
    { MP_ROM_QSTR(MP_QSTR_BOOT0), MP_ROM_PTR(&pin_GPIO9) },
   
+   { MP_ROM_QSTR(MP_QSTR_SW1), MP_ROM_PTR(&pin_GPIO1) },
+   { MP_ROM_QSTR(MP_QSTR_SW2), MP_ROM_PTR(&pin_GPIO3) },
+   { MP_ROM_QSTR(MP_QSTR_SW3), MP_ROM_PTR(&pin_GPIO0) },
+   { MP_ROM_QSTR(MP_QSTR_SW4), MP_ROM_PTR(&pin_GPIO2) },

MP_QSTR_XXXXXX部分がピン名称として定義されます。この文字列定義はビルドプロセスにおいて自動生成されるため、XXX部分には任意の識別子を指定することができます。

nak435_esp32c3_supermini/mpconfigboard.mk
  CIRCUITPY_ESP_USB_SERIAL_JTAG = 1

+ # Include these Python libraries in firmware.
+ FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_NeoPixel
+ FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_Ticks

2.4 ビルド

流用して作成したオリジナルボードの CircuitPython をビルドします。

boards % cd ..
espressif % make -j8 BOARD=nak435_esp32c3_supermini 
#数分待つ
espressif % find . -name "firmware.bin" 
./build-nak435_esp32c3_supermini/firmware.bin

初回のビルドにおいては、1000本近いファイルが生成/コンパイルされるため、数分要します。

3. ESP32-C3 SuperMini に焼く

さっそく ビルドできた ファームウェアfirmware.binを焼きます。

espressif % esptool.py --chip esp32c3 --port /dev/cu.usbmodem1201 write_flash 0 ./build-nak435_esp32c3_supermini/firmware.bin 
esptool.py v4.8.1
Serial port /dev/cu.usbmodem1201
Connecting...
Detecting chip type... ESP32-C3
Chip is ESP32-C3 (QFN32) (revision v0.4)
Features: WiFi, BLE, Embedded Flash 4MB (XMC)
Crystal is 40MHz
MAC: 94:a9:90:52:91:1c
Uploading stub...
Running stub...
Stub running...
Configuring flash size...
Flash will be erased from 0x00000000 to 0x0016efff...
Compressed 1501264 bytes to 942459...
Wrote 1501264 bytes (942459 compressed) at 0x00000000 in 11.4 seconds (effective 1057.8 kbit/s)...
Hash of data verified.

Leaving...
Hard resetting via RTS pin...


REPL から バージョン他、諸々を確認します。

espressif % screen /dev/cu.usbmodem1201 115200

>>> (Ctrl+D)
soft reboot

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
code.py output:
Hello World!

Code done running.

Press any key to enter the REPL. Use CTRL-D to reload.
(enter)

Adafruit CircuitPython 10.0.0-alpha.4-2-g9819e7a1bf on 2025-05-08; nak435 ESP32-C3 Supermini with ESP32-C3
>>> import board
>>> dir(board)
['__class__', '__name__', 'BOOT0', 'BUTTON', 'I2C', 'IO0', 'IO1', 'IO10', 'IO2', 'IO20', 'IO21', 'IO3', 'IO4', 'IO5', 'IO6', 'IO7', 'IO8', 'IO9', 'LED', 'MISO', 'MOSI', 'RGB', 'RX', 'SCK', 'SCL', 'SDA', 'SPI', 'SW1', 'SW2', 'SW3', 'SW4', 'TX', 'UART', '__dict__', 'board_id']
>>> board.RGB
board.IO4
>>> board.SW1
board.IO10
>>> board.SW2
board.IO6
>>> board.SW3
board.IO7
>>> board.SW4
board.IO3

>>> import os
>>> os.listdir()
['sd', 'settings.toml', 'code.py', 'lib', 'boot_out.txt']

>>> f=open('boot_out.txt','r')
>>> print(f.read())
Adafruit CircuitPython 10.0.0-alpha.4-2-g9819e7a1bf on 2025-05-08; nak435 ESP32-C3 Supermini with ESP32-C3
Board ID:nak435_esp32c3_supermini
UID:499A092519C1
MAC:06:00:00:00:BE:F4

>>> f.close()

>>> import gc
>>> print(gc.mem_free())
120784

>>> (Ctrl+A Ctrl+K) 
Really kill this window [y/n] y

[screen is terminating]

期待通り ピンの定義が追加されています。

最新ソースをクローンしたので、バージョンはプレリリース版の10.0.0-alpha.4より新しい 10.0.0-alpha.4-2でした。

最終リリース版の9.2.7を作るなら、クローン時にgit checkout 9.2.7すればよいでしょう。
 

空きメモリは 120KB弱。あまり大きなコードは作れないかも。使わないライブラリをバンドルから削除して、空きを増やすしかないか。

4. オリジナルボード

back.png front.png

いい感じに実装できました。

IMG_4541.png IMG_4543.png

5. 本当は、

オリジナル CircuitPython をビルドしたのは、_bleioをバンドルライブラリに追加するのが目的でした。 標準では ESP32-C3向けの CircuitPython は、BLE が使用できない設定になっています。
フラッシュが4MBと少ないことが理由だと思いますが、sdkconfigCIRCUITPY_BLEIO_NATIVE=1を追加し、パーティションテーブルを変更してビルドすることで、_bleioをバンドルできました。しかし、次のエラーとなり CircuitPythonが安定しません。

CircuitPython core code crashed hard. Whoops!
Hard fault: memory access or instruction error.
Thonnyにて実行
Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
code.py output:
ESP-ROM:esp32c3-api1-20210207
Build:Feb  7 2021
rst:0xc (RTC_SW_CPU_RST),boot:0xd (SPI_FAST_FLASH_BOOT)
Saved PC:0x40382be0
SPIWP:0xee
mode:DIO, clock div:1
load:0x3fcd5820,len:0x78
load:0x403cc710,len:0x848
load:0x403ce710,len:0x20b8
entry 0x403cc710

Auto-reload is off.
Running in safe mode! Not running saved code.

You are in safe mode because:
CircuitPython core code crashed hard. Whoops!
Hard fault: memory access or instruction error.
Please file an issue with your program at github.com/adafruit/circuitpython/issues.
Press reset to exit safe mode.

Press any key to enter the REPL. Use CTRL-D to reload.

Adafruit CircuitPython 10.0.0-alpha.4-2-g9819e7a1bf on 2025-05-08; nak435 ESP32-C3 Supermini with ESP32-C3
>>> 
>>> 
PROBLEM IN THONNY'S BACK-END: Internal error (thonny.plugins.micropython.mp_back.ManagementError: Command output was not empty).
See Thonny's backend.log for more info.
You may need to press "Stop/Restart" or hard-reset your CircuitPython device and try again.


Process ended with exit code 0.

原因究明には時間がかかりそうです。

circuitpython/ports/espressif/boards/nak435_esp32c3_supermini/sdkconfig
#
# Espressif IoT Development Framework Configuration
#
#
# Component config
#
#
#
# Partition Table
#
CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="esp-idf-config/partitions-4MB-no-uf2-2.csv"
CONFIG_PARTITION_TABLE_FILENAME="esp-idf-config/partitions-4MB-no-uf2-2.csv"
# end of Partition Table
#
# LWIP
#
# end of LWIP

# end of Component config

# end of Espressif IoT Development Framework Configuration
circuitpython/ports/espressif/boards/nak435_esp32c3_supermini/mpconfigboard.mk
CIRCUITPY_CREATOR_ID =  0x19981000
CIRCUITPY_CREATION_ID = 0x00BB0001

IDF_TARGET = esp32c3

CIRCUITPY_ESP_FLASH_MODE=dio
CIRCUITPY_ESP_FLASH_FREQ=80m
CIRCUITPY_ESP_FLASH_SIZE=4MB


CIRCUITPY_BLEIO_NATIVE = 1


CIRCUITPY_LEGACY_4MB_FLASH_LAYOUT = 1

CIRCUITPY_ESP_USB_SERIAL_JTAG = 1
circuitpython/ports/espressif/esp-idf-config/partitions-4MB-no-uf2-2.csv
# Name,             Type,   SubType,    Offset,     Size
# bootloader,       app,    boot,       0x1000/0x0, 28/32K
# partition_table,  data,   table,      0x8000,     4K
nvs,                data,   nvs,        0x9000,     20K
otadata,            data,   ota,        0xe000,     8K
ota_0,              app,    ota_0,      0x10000,    1664K
ota_1,              app,    ota_1,      0x1B0000,   1024K
user_fs,            data,   fat,        0x2B0000,   1344K


ESP32-S34MBフラッシュ版ではこの方法でBLEが使えるようになったので、ESP32-C3は何か別な制約があるのかも??・・・

その後もいろいろ試していたところ、code.pyを削除すると、ターミナルからscreenコマンドで接続すると、エラーが起きずにREPLが起動しました。Thonnyでは相変わらずエラーで動かないが・・・

% screen /dev/cu.usbmodem1201 115200

Adafruit CircuitPython 10.0.0-alpha.4-2-g9819e7a1bf on 2025-05-08; nak435 ESP32-C3 Supermini with ESP32-C3
>>>
>>>
>>> import microcontroller
>>> microcontroller.reset()
ESP-ROM:esp32c3-api1-20210207
Build:Feb  7 2021
rst:0xc (RTC_SW_CPU_RST),boot:0xd (SPI_FAST_FLASH_BOOT)
Saved PC:0x40382be0
SPIWP:0xee
mode:DIO, clock div:1
load:0x3fcd5820,len:0x78
load:0x403cc710,len:0x848
load:0x403ce710,len:0x20b8
entry 0x403cc710

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.

Code done running.

Press any key to enter the REPL. Use CTRL-D to reload.

Adafruit CircuitPython 10.0.0-alpha.4-2-g9819e7a1bf on 2025-05-08; nak435 ESP32-C3 Supermini with ESP32-C3
>>> help('modules')
__future__        codeop            max3421e          synthio
__main__          collections       mdns              sys
_asyncio          digitalio         microcontroller   terminalio
_bleio            displayio         micropython       tilepalettemapper
_pixelmap         dualbank          msgpack           time
adafruit_bus_device                 epaperdisplay     neopixel_write    touchio
adafruit_bus_device.i2c_device      errno             nvm               traceback
adafruit_bus_device.spi_device      espidf            onewireio         ulab
adafruit_pixelbuf espnow            os                ulab.numpy
analogbufio       fontio            ps2io             ulab.numpy.fft
analogio          fourwire          pulseio           ulab.numpy.linalg
array             framebufferio     pwmio             ulab.scipy
atexit            gc                rainbowio         ulab.scipy.linalg
audiobusio        getpass           random            ulab.scipy.optimize
audiocore         gifio             re                ulab.scipy.signal
audiomixer        hashlib           rgbmatrix         ulab.scipy.special
audiomp3          i2cdisplaybus     rtc               ulab.utils
binascii          io                sdcardio          usb
bitbangio         ipaddress         select            usb.core
bitmaptools       jpegio            sharpdisplay      usb.util
board             json              socketpool        vectorio
builtins          keypad            ssl               warnings
busdisplay        locale            storage           watchdog
busio             lvfontio          struct            wifi
canio             math              supervisor        zlib
Plus any modules on the filesystem

>>> import _bleio
>>> 

WebRepl で接続することもできましたが、やはりエラーになります。

webrepl.png

継続調査が必要・・・


Micropython や Arduino であれば、普通に BLE が使えるので、とりあえず 困っていませんが。。。




以上

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?