1
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 で TinyGo

Last updated at Posted at 2025-10-10

環境

Ubuntu 22.04 LTS

インストール

以下のサイトの通りやっていきます。
https://tinygo.org/getting-started/install/linux/

$ wget https://github.com/tinygo-org/tinygo/releases/download/v0.39.0/tinygo_0.39.0_amd64.deb
$ sudo dpkg -i tinygo_0.39.0_amd64.deb
$ which tinygo
/usr/local/bin/tinygo
$ tinygo version
tinygo version 0.39.0 linux/amd64 (using go version go1.24.3 and LLVM version 19.1.2)

ビルドテスト

以下のようなソースを作ります。

package main

import "fmt"

func main() {
	fmt.Println("Hello world!")
}

ビルドします。

$ go build -o hello ./hello.go
$ tinygo build -o hellotinygo ./hello.go

最初は go, 2つ目はtinygo のビルドらしいです。

思ったよりサイズの違いはありませんが・・・

$ ls -alh
合計 3.4M
drwxrwxr-x   2 nanbuwks nanbuwks 4.0K 10月 10 19:30 .
drwxr-xr-x 320 nanbuwks nanbuwks 240K 10月 10 19:29 ..
-rwxrwxr-x   1 nanbuwks nanbuwks 2.2M 10月 10 19:29 hello
-rw-rw-r--   1 nanbuwks nanbuwks   73 10月 10 19:29 hello.go
-rwxrwxr-x   1 nanbuwks nanbuwks 1.1M 10月 10 19:30 hellotinygo

strip コマンドをかけると

$ strip hello
$ strip hellotinygo 

違いが明瞭になります。

$ ls -alh
合計 1.8M
drwxrwxr-x   2 nanbuwks nanbuwks 4.0K 10月 10 19:31 .
drwxr-xr-x 320 nanbuwks nanbuwks 240K 10月 10 19:29 ..
-rwxrwxr-x   1 nanbuwks nanbuwks 1.4M 10月 10 19:31 hello
-rw-rw-r--   1 nanbuwks nanbuwks   73 10月 10 19:29 hello.go
-rwxrwxr-x   1 nanbuwks nanbuwks 164K 10月 10 19:31 hellotinygo

この内容であれば実行結果に違いはありません。

$ ./hello
Hello world!
$ ./hellotinygo 
Hello world!

通常の go のローカルビルドと同じように見えます。

ESP32 セットアップ

c などでは、クロスコンパイラなどをインストールしてクロスビルドします。どうやら TinyGo でも同様のようですが、セットアップはだいぶ勝手が違うようです。

「Standard Setup of Toolchain for Linux — ESP-IDF Programming Guide v3.0.8-30-gf3704f027 documentation」
https://docs.espressif.com/projects/esp-idf/en/release-v3.0/get-started/linux-setup.html#toolchain-setup

の記述通りセットアップを進めていきます。

$ mkdir ~/esp
$ cd ~/esp
$ wget https://dl.espressif.com/dl/xtensa-esp32-elf-linux64-1.22.0-80-g6c4433a-5.2.0.tar.gz
$ tar xzvf xtensa-esp32-elf-linux64-1.22.0-80-g6c4433a-5.2.0.tar.gz 
$ echo export PATH='$PATH:$HOME/esp/xtensa-esp32-elf/bin' >> ~/.bashrc

最後の PATH 設定は .bashrc に行いましたが、環境によって .bash_profile などに合わせる必要があります。

ログオフしてログオンします。

esptool.py のインストールは既に行われていました。

サンプルを取得するために、レポジトリをクローンします。

$ git clone https://github.com/tinygo-org/tinygo.git

作業場所に適当なサンプルをコピーします。

$ cp tinygo/src/examples/blinky1/blinky1.go .
$ cp tinygo/src/examples/serial/serial.go .
$ cat blinky1.go 
package main

// This is the most minimal blinky example and should run almost everywhere.

import (
	"machine"
	"time"
)

func main() {
	led := machine.LED
	led.Configure(machine.PinConfig{Mode: machine.PinOutput})
	for {
		led.Low()
		time.Sleep(time.Millisecond * 500)

		led.High()
		time.Sleep(time.Millisecond * 500)
	}
}
$ cat serial.go 
package main

import "time"

func main() {
	for {
		println("hello world!")
		time.Sleep(time.Second)
	}
}

machine.LED が target board により定義されているようです。

target board の一覧はこちらにあります。
https://tinygo.org/docs/reference/microcontrollers/

今回はESP32-Core board が良さそうです。

ESP32-Core borard の定義を見ると LED が GPIO2 になっていました。
image.png

書き込みます。

$ tinygo flash --target esp32-coreboard-v2 --size short blinky1.go

一見 flasn オプションで書いているだけのようですが、どうやらクロスビルドが自動で行われているようです。

   
   code    data     bss |   flash     ram
   3537       0    4184 |    3537    4184
esptool.py v4.5
Serial port /dev/ttyUSB0
Connecting.....
Chip is ESP32-D0WD-V3 (revision v3.0)
Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None
Crystal is 40MHz
MAC: b0:a7:32:dc:20:08
Uploading stub...
Running stub...
Stub running...
Configuring flash size...
Flash will be erased from 0x00001000 to 0x00001fff...
Warning: Image file at 0x1000 is protected with a hash checksum, so not changing the flash mode setting. Use the --flash_mode=keep option instead of --flash_mode=dout in order to remove this warning, or use the --dont-append-digest option for the elf2image command in order to generate an image file without a hash checksum
Compressed 3616 bytes to 2817...
Wrote 3616 bytes (2817 compressed) at 0x00001000 in 0.3 seconds (effective 107.1 kbit/s)...
Hash of data verified.

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

LED点滅ができました。

╭─ Error ──────────────────────────────────────────────────────────────────────╮
│ Invalid value for '--port' / '-p': Path '/dev/ttyUSB0' is not readable.      │
╰──────────────────────────────────────────────────────────────────────────────╯
                                                                                
failed to flash /tmp/tinygo141724891/main.bin: exit status 2

このようになったときは serial アクセス権をつける必要があります。

$ sudo adduser $USER dialout

HEXファイルやBINファイルなどはどこにも見当たりませんが、表に出さずに処理をしているようです。

次にシリアルサンプルを書き込んでみます。

$ tinygo flash --target esp32-coreboard-v2 --size short  serial.go

書き込んだ後、以下のコマンドで確認できます。

$ tinygo monitor
Connected to /dev/ttyUSB0. Press Ctrl-C to exit.
hello world!
hello world!
hello world!
hello world!
hello world!
hello world!
hello world!
hello world!
hello world!
.
.
.
1
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
1
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?