« Moddableをインストールする(v2) | トップページ | Teensy3.6ボードでTinyGOを動かす »

2020年10月 7日 (水)

ESP32ボードでTinyGOを動かす(v2)

2020/10/8:
もう一つのnexpixelのデモを追加した。

2020/10/7:
初版

TinyGO Install ESP32 v2

TinyGO Install ESP32 v2

概要

ESP32ボードでTinyGOを動かす(v2)。
tinygo_0.15.0でesp32が正式にサポートされたので、それをインストールして使ってみる。
なお、ホストPCとしてはubuntu20.04を想定している。

ESP32ボードとしては、M5Stack,M5Stick-C,M5Atomなどがある。

準備

以下のツールを予めインストールする:
(0)udev設定
以下を実行する:

curl -fsSL https://raw.githubusercontent.com/platformio/platformio-core/master/scripts/99-platformio-udev.rules | sudo tee /etc/udev/rules.d/99-platformio-udev.rules sudo udevadm control --reload-rules sudo usermod -a -G dialout $USER sudo usermod -a -G plugdev $USER

(1)TinyGOのインストール
以下の手順でインストールする:

mkdir tinygo_ws cd tinygo_ws wget https://github.com/tinygo-org/tinygo/releases/download/v0.15.0/tinygo_0.15.0_amd64.deb sudo dpkg -i tinygo_0.15.0_amd64.deb export PATH=$PATH:/usr/local/tinygo/bin # esp32には不要だが、ターゲットがarduinoのときに # 必要となるツールをついでにインストールする sudo apt-get install gcc-avr sudo apt-get install avr-libc sudo apt-get install avrdude

(2)GOのインストール
TinyGOのモジュールを使用するのにGOが必要なので 予めインストールする。
(ただし、TinyGOとの整合性により最新版ではない)

wget https://dl.google.com/go/go1.13.7.linux-amd64.tar.gz sudo tar -C /usr/local -xzf go1.13.7.linux-amd64.tar.gz export PATH=$PATH:/usr/local/go/bin export GOPATH=$HOME/tinygo_ws

なお、GOPATHのパス設定値は、任意だが、それをベースに その配下にTinyGO/GOの関係ファイル/ディレクトリが置かれる。

(3)esptoolのインストール
ESP32の書き込みツールをインストールする:

# python2.xのpipをインストールする mkdir tools cd tools sudo add-apt-repository universe sudo apt update sudo apt install python2 curl https://bootstrap.pypa.io/get-pip.py --output get-pip.py sudo python2 get-pip.py # esptoolをインストールする git clone https://github.com/espressif/esptool.git cd esptool pip install --user -e .

以上のexportは、.bashrcに登録する。

(4)export登録(まとめ)
以下を.bashrcに追加する:

# tinygo/go export PATH=$PATH:/usr/local/go/bin export PATH=$PATH:/usr/local/tinygo/bin export GOPATH=$HOME/tinygo_ws export TIGOLIBS=$GOPATH/src/tinygo.org/x/drivers/

examples/serialを動かす

M5Atomをホストに接続して、以下を実行する:

tinygo flash -size full -target=esp32-wroom-32 -port=/dev/ttyUSB0 examples/serial #ビルド時の出力 code rodata data bss | flash ram | package 115 0 0 0 | 115 0 | (bootstrap) 120 12 0 0 | 132 0 | examples/serial 200 76 0 8 | 276 8 | runtime 21 0 0 0 | 21 0 | runtime/volatile 456 88 0 8 | 544 8 | (sum) 673 - 0 4108 | 673 4108 | (all) esptool.py v3.0-dev Serial port /dev/ttyUSB0 Connecting.... Chip is ESP32-PICO-D4 (revision 1) Features: WiFi, BT, Dual Core, 240MHz, Embedded Flash, VRef calibration in efuse, Coding Scheme None Crystal is 40MHz MAC: 50:02:91:91:19:20 Uploading stub... Running stub... Stub running... Configuring flash size... Auto-detected Flash size: 4MB Flash params set to 0x032f Compressed 880 bytes to 678... Wrote 880 bytes (678 compressed) at 0x00001000 in 0.1 seconds (effective 103.8 kbit/s)... Hash of data verified. Leaving... Hard resetting via RTS pin...

コンパイルと書き込み実行が自動的に行わるので、その後、 以下のように通信ソフトを起動する:

$ picocom /dev/ttyUSB0 -b115200 <省略> Type [C-a] [C-h] to see available commands Terminal ready 2:57 rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT) configsip: 188777542, SPIWP:0xee clk_drv:0x00,q_drv:0x00,d_dr hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! <省略>

任意のプログラムを動かす

(1)以下のようにプロジェクト用ディレクトリを作成する:

cd tigo_ws mkdir blink cd blink

(2)プロジェクト(blink)ディレクトリに以下のファイルを作成する:
main.go

package main // This is the most minimal blinky example and should run almost everywhere. import ( "machine" "time" ) func main() { led := machine.Pin(25) // G25 for M5Atom led.Configure(machine.PinConfig{Mode: machine.PinOutput}) for { led.Low() time.Sleep(time.Millisecond * 1000) led.High() time.Sleep(time.Millisecond * 1000) } }

抵抗付きLEDをG25に接続する。

(3)ビルド実行
M5Atomをホストに接続して、以下を実行する:

tinygo flash -size full -target=esp32-wroom-32 -port=/dev/ttyUSB0 .

コンパイルと書き込み実行が自動的に行われ、接続したLEDが点滅する。

TinyGOのモジュールを利用する

実行例: # 以下で、モジュールをインストールする go get tinygo.org/x/drivers # インストールしたモジュールのパスを設定する export TIGOLIBS=$GOPATH/src/tinygo.org/x/drivers/ # インポートしたいモジュールを確認する ls $TIGOLIBS ... adt7410 ds1307 l9110x shifter vl53l1x adxl345 ds3231 lis3dh shiftregister waveshare-epd amg88xx easystepper lsm6ds3 sht3x wifinina apa102 espat mag3110 ssd1306 ws2812 at24cx examples mcp3008 ssd1331 bh1750 flash microbitmatrix st7735 ... # ここでは例として「ws2812」を使用する mkdir ws2812 cd ws2812 # exampleのmain.goをコピーする cp $TIGOLIBS/examples/ws2812/main.go .

以下のようにmain.goを変更する:
main.go

// Connects to an WS2812 RGB LED strip with 10 LEDS. // // See either the others.go or digispark.go files in this directory // for the neopixels pin assignments. package main import ( "image/color" "machine" "time" "tinygo.org/x/drivers/ws2812" ) var leds [15]color.RGBA func main() { led := machine.LED led.Configure(machine.PinConfig{Mode: machine.PinOutput}) // neo := machine.Pin(26) // G26 for Grove connector of M5Atom neo := machine.Pin(25) // G25 for M5Atom // neo := machine.D4 // for Feater-M4/XIAO // neo := machine.NEOPIXELS neo.Configure(machine.PinConfig{Mode: machine.PinOutput}) ws := ws2812.New(neo) rg := false for { rg = !rg for i := range leds { rg = !rg if rg { // Alpha channel is not supported by WS2812 so we leave it out leds[i] = color.RGBA{R: 0xff, G: 0x00, B: 0x00} } else { leds[i] = color.RGBA{R: 0x00, G: 0xff, B: 0x00} } } ws.WriteColors(leds[:]) led.Set(rg) time.Sleep(100 * time.Millisecond) } }

以下でビルド実行する:

tinygo flash -size full -target=esp32-wroom-32 -port=/dev/ttyUSB0 .

M5AtomのG25をneopixelsのDIN,5VをVCC,GNDをGNDに接続する。
(または、Groveコネクタに接続する)
書き込み実行すると、接続したneopixelsが光る。

他の外部モジュールを利用する

# 以下で、モジュールをインストールする go get github.com/aykevl/ledsgo # 参考(以下のディレクトリにダウンロードしたものが置かれる) ls $GOPATH/src github.com tinygo.org ls $GOPATH/src/github.com aykevl # プログラムを置くディレクトリを作る mkdir neopixels0 cd neopixels0 # デモ・プログラムをダウンロードする wget https://gist.githubusercontent.com/aykevl/47d0a24408cf585f6ba181c4dc663bca/raw/c4db52a1fedb215b4743a31842aeba31a4f2fe77/ws2812.go

以下のようにws2812.goを修正する:
ws2812.go

package main // This is an example of controlling WS2812 LEDs from an ESP32. // The following PRs are still needed to get this to work: // https://github.com/tinygo-org/tinygo/pull/1353 // https://github.com/tinygo-org/tinygo/pull/1354 // https://github.com/tinygo-org/drivers/pull/198 import ( "machine" "time" "github.com/aykevl/ledsgo" "tinygo.org/x/drivers/ws2812" ) const brightness = 0x44 const pin = machine.Pin(27) // G27 for Matrix(5x5) of M5Atom //const pin = machine.Pin(26) // G26 for Grove Connector of M5Atom //const pin = machine.Pin(25) // G25 for M5Atom var ws ws2812.Device func main() { strip := make(ledsgo.Strip, 25) // for Matrix(5x5) M5Atom //strip := make(ledsgo.Strip, 15) // for Grove Neopixel //strip := make(ledsgo.Strip, 50) pin.Configure(machine.PinConfig{Mode: machine.PinOutput}) ws = ws2812.New(pin) rainbow(strip) } func rainbow(strip ledsgo.Strip) { for { now := time.Now().UnixNano() for i := range strip { strip[i] = ledsgo.Color{uint16(now>>15) - uint16(i)<<12, 0xff, brightness}.Spectrum() } ws.WriteColors(strip) time.Sleep(time.Second / 100) } } func noise(strip ledsgo.Strip) { for { now := time.Now().UnixNano() for i := range strip { const spread = 100 val := int32(ledsgo.Noise2(int32(now>>22), int32(i*spread))) * 3 / 2 strip[i] = ledsgo.Color{uint16(val), 0xff, brightness}.Spectrum() } ws.WriteColors(strip) time.Sleep(time.Second / 100) } }

以下の選択肢があるので、それに応じてソースをコメントアウトする:
(1)M5Atom_Matrixの内蔵のLED(5x5)を使用する。
(2)M5AtomのGroveコネクタにnexpixelsを接続する。
(3)M5AtomのG25をneopixelsのDIN,5VをVCC,GNDをGNDに接続する。

以下でビルド実行する:

tinygo flash -size full -target=esp32-wroom-32 -port=/dev/ttyUSB0 .

書き込み実行すると、neopixelsが虹色に変化して光る。

参考情報

ESP32 and ESP8266 support in TinyGo

https://github.com/tinygo-org/tinygo
https://tinygo.org/
https://tinygo.org/getting-started/linux/

コンピュータボードでTinyGOを動かす
docker/TinyGO Helper Script
TinyGOでLightSensorを動かす

TinyGoで始める組み込みプログラミング
TinyGo on Arduino Uno: An Introduction

Circuit Playground Express
Adafruit Circuit Playground Express - Overview
Infrared Receive and Transmit with Circuit Playground Express

Adafruit Circuit Playground Express - PINOUT
Adafruit Circuit Playground Express Pin Assign

NUCLEO-F103RB mbed pinout
NUCLEO-F103RB Pin Assign
STM32F4DISCO Pin Assign
MICROBIT Pin Assign
ARDUINO-NANO Pin Assign
ARDUINO Pin Assign

TinyGo Drivers

USB Flashing Format (UF2)

XIAO Schematic(zip)
How to use Seeeduino XIAO to log in to your Raspberry PI

以上

|

« Moddableをインストールする(v2) | トップページ | Teensy3.6ボードでTinyGOを動かす »

TinyGO」カテゴリの記事

M5Atom」カテゴリの記事

ESP32」カテゴリの記事

コメント

この記事へのコメントは終了しました。