TinyGO

2021年3月25日 (木)

WSL2でTinyGOを動かす

2021/3/25

WSL2 TinyGO Install

WSL2 TinyGO Install

概要

WSL2でTinyGOを動かす。
WSL2のUbuntu20.04をホストにする前提で
XIAOボードを中心としてインストールからビルド書き込みまで説明している。
target名を変更することで他のボードのビルドなども行うことができる。

Go/TinyGoのインストール

Ubuntu20.04/WSL2で以下を実行する:

mkdir ~/tigo_ws cd ~/Downloads # goのインストール wget https://golang.org/dl/go1.16.2.linux-amd64.tar.gz sudo rm -rf /usr/local/go sudo tar -C /usr/local -xzf go1.16.2.linux-amd64.tar.gz export PATH=$PATH:/usr/local/go/bin # tinygoのインストール wget https://github.com/tinygo-org/tinygo/releases/download/v0.17.0/tinygo_0.17.0_amd64.deb sudo dpkg -i tinygo_0.17.0_amd64.deb export PATH=$PATH:/usr/local/tinygo/bin # その他のexport export GOPATH=$HOME/tigo_ws export TIGOLIBS=$GOPATH/pkg/mod/tinygo.org/x/drivers@v0.15.1 # 書き込み用のディレクトリを作る mkdir /mnt/c/temp

以下を.bashrcに追加する:

# tinygo/go export PATH=$PATH:/usr/local/go/bin export PATH=$PATH:/usr/local/tinygo/bin export GOPATH=$HOME/tigo_ws export TIGOLIBS=$GOPATH/pkg/mod/tinygo.org/x/drivers@v0.15.1

書き込みスクリプト

WSL2はUSBデバイス(USBストレージも含む)をサポートしていないので、
WSL2でhexまたはuf2のファイルまで作成した後、
PowerShell.exe経由で USBストレージに書き込むことで、ボード書き込みを行う。

このスクリプトは、「~/tigo_ws」に置いて、実際のプロジェクト・ディレクトリにコピーして使用することとする。
以下、対応ボードごとのbashスクリプトになる。

fUF2.sh

#!/bin/bash rm /mnt/c/temp/* cp *.uf2 /mnt/c/temp/ PowerShell.exe copy c:/temp/*.uf2 e: exit 0

・実行属性を与えること。
・「e:」は環境依存なので、自分の環境で、uf2ファイルを書き込むドライブ・レターを設定すること。

fHEX.sh

#!/bin/bash rm /mnt/c/temp/* cp *.hex /mnt/c/temp/ PowerShell.exe copy c:/temp/*.hex e: exit 0

・実行属性を与えること。
・「e:」は環境依存なので、自分の環境で、uf2ファイルを書き込むドライブ・レターを設定すること。

インストール時付属サンプルのビルド例

cd ~/tigo_ws # microbit系 tinygo build -size full -o=mb.hex -target=microbit examples/microbit-blink tinygo build -size full -o=mb2.hex -target=microbit-v2 examples/microbit-blink # SAMD系 tinygo build -size full -o=cpx.uf2 -target=circuitplay-express examples/blinky1 tinygo build -size full -o=fm4.uf2 -target=feather-m4 examples/blinky1 tinygo build -size full -o=xiao.uf2 -target=xiao examples/blinky1 tinygo build -size full -o=wiot.uf2 -target=wioterminal examples/blinky1 # mbed系 tinygo build -size full -o f103.hex -target nucleo-f103rb examples/blinky1

独自プロジェクト(blinky)の作成

独自プロジェクト(blinky)を作成するには
ディレクトリblinkyを作成して、

(1)その中に以下のファイルを作成する:
blinky.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 * 1000) led.High() time.Sleep(time.Millisecond * 1000) } }

(2)go.modの作成
以下を実行してgo.modを作成する:

go mod init blinky

「blinky」は任意のようだが プロジェクト名(ディレクトリ名)に合わせる。

(3)ビルド
以下を実行してビルドする:

tinygo build -size full -o=xiao.uf2 -target=xiao .

これで、xiao.uf2が作成される。

出力例:

$ tinygo build -size full -o=xiao.uf2 -target=xiao . code rodata data bss | flash ram | package 2574 13 0 130 | 2587 130 | (bootstrap) 0 36 0 0 | 36 0 | handleHardFault$string 164 24 4 4 | 192 8 | internal/task 2844 18 13 2054 | 2875 2067 | machine 0 16 0 0 | 16 0 | machine$alloc 40 0 0 0 | 40 0 | main 2352 79 0 48 | 2431 48 | runtime 28 0 0 0 | 28 0 | runtime/volatile 92 0 0 0 | 92 0 | time 8094 186 17 2236 | 8297 2253 | (sum) 8508 - 20 4312 | 8528 4332 | (all) $ ls -l *.uf2 -rw-r--r-- 1 xxxx xxxx 17408 3月 25 08:10 xiao.uf2

(4)書き込み
以下を実行して書き込む:

# 書き込みスクリプトをカレント・ディレクトリにコピーする cp ~/tigo_ws/fUF2.sh . # ボードとホストPCをUSBで接続する。 # RSTパッドを2度GNDショートして、bootloader-modeに入る。 # Arduinoのディレクトリが現れる。 #書き込み ./fUF2.sh

ボードの赤いLED(D13)が1秒周期で点滅すれば動作としてはOKとなる。

serialを動かす

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

cd tigo_ws mkdir serial cd serial

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

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

(3)go.modの作成
以下を実行してgo.modを作成する:

go mod init serial

「serial」は任意のようだが プロジェクト名(ディレクトリ名)に合わせる。

(4)ビルド/書き込み 以下を実行する:

# ビルド tinygo build -size full -o=xiao.uf2 -target=xiao . # 書き込みスクリプトをカレント・ディレクトリにコピーする cp ~/tigo_ws/fUF2.sh . # ボードとホストPCをUSBで接続する。 # RSTパッドを2度GNDショートして、bootloader-modeに入る。 # Arduinoのディレクトリが現れる。 #書き込み ./fUF2.sh

以上で、プログラムが起動する。

(4)シリアル通信の起動
windows側の通信ソフト(teratermなど)でシリアル接続すると
以下のような出力が得られる。(通信速度:115200 bps)

hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! ...

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

実行例: # 以下で、モジュールをインストールする go get tinygo.org/x/drivers # インストールしたモジュールのパスを設定する #(この場合は設定済みなので特にやることはない) # インポートしたいモジュールを確認する ls $TIGOLIBS #出力 CHANGELOG.md bmp280 hcsr04 microbitmatrix st7789 CONTRIBUTING.md bmp388 hd44780 microphone tester LICENSE buzzer hd44780i2c mma8653 thermistor Makefile dht hub75 mpu6050 tmp102 README.md displayer.go i2c.go net touch adt7410 drivers.go ili9341 pcd8544 veml6070 adxl345 ds1307 l293x semihosting version.go amg88xx ds3231 l9110x shifter vl53l1x apa102 easystepper lis2mdl shiftregister waveshare-epd at24cx espat lis3dh sht3x wifinina bh1750 examples lsm303agr spi.go ws2812 blinkm flash lsm6ds3 ssd1306 bme280 go.mod mag3110 ssd1331 bmi160 go.sum mcp23017 ssd1351 bmp180 gps mcp3008 st7735 # ここでは例として「ws2812」を使用してみる mkdir ws2812 cd ws2812 # exampleのmain.goをコピーする cp $TIGOLIBS/examples/ws2812/main.go . chmod +rw main.go # main.goを編集する nano 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" ) // 以下は接続するneopixelの数に合わせる var leds [64]color.RGBA //var leds [10]color.RGBA func main() { led := machine.LED led.Configure(machine.PinConfig{Mode: machine.PinOutput}) 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} leds[i] = color.RGBA{R: 0x0f, G: 0x00, B: 0x00} } else { //leds[i] = color.RGBA{R: 0x00, G: 0xff, B: 0x00} leds[i] = color.RGBA{R: 0x00, G: 0x0f, B: 0x00} } } ws.WriteColors(leds[:]) led.Set(rg) time.Sleep(100 * time.Millisecond) } }

続き:

# go.modを初期化する go mod init ws2812 # 以下のコマンドで必要なモジュールを自動設定する go mod tidy # ビルド tinygo build -size full -o=xiao.uf2 -target=xiao . # 書き込みスクリプトをカレント・ディレクトリにコピーする cp ~/tigo_ws/fUF2.sh . # ボードとホストPCをUSBで接続する。 # RSTパッドを2度GNDショートして、bootloader-modeに入る。 # Arduinoのディレクトリが現れる。 #書き込み ./fUF2.sh

D4をneopixelsのDIN,3V3をVCC,GNDをGNDに接続する。
書き込んで実行すると、接続したneopixelsが色を変えて光る。

tmp102(i2c)を動かす

mkdir tmp102 cd tmp102 cp $TIGOLIBS/examples/tmp102/main.go . chmod a+rw main.go nano main.go # 以下の内容にする: (修正点は無いはずだが以下の内容であることを確認する)
package main import ( "fmt" "machine" "time" "tinygo.org/x/drivers/tmp102" ) func main() { machine.I2C0.Configure(machine.I2CConfig{ Frequency: machine.TWI_FREQ_400KHZ, }) thermo := tmp102.New(machine.I2C0) thermo.Configure(tmp102.Config{}) for { temp, _ := thermo.ReadTemperature() print(fmt.Sprintf("%.2f°C\r\n", float32(temp)/1000.0)) time.Sleep(time.Millisecond * 1000) } }

続き:

# go.modを設定する go mod init tmp102 go mod tidy # 以下、ビルド&書き込み tinygo build -size full -o=xiao.uf2 -target=xiao . # 書き込みスクリプトをカレント・ディレクトリにコピーする cp ~/tigo_ws/fUF2.sh . # ボードとホストPCをUSBで接続する。 # RSTパッドを2度GNDショートして、bootloader-modeに入る。 # Arduinoのディレクトリが現れる。 #書き込み ./fUF2.sh

XIAOのピン定義

port pin
PA02 A0/D0
PA04 A1/D1
PA10 A2/D2
PA11 A3/D3
PA08 A4/D4
PA09 A5/D5
PB08 A6/D6
PB09 A7/D7
PA07 A8/D8
PA05 A9/D9
PA06 A10/D10

ボードのターゲット名

「-target=nxiao」を以下にあるターゲット名に変更すると
他のボード用にビルドできる。

主なもの(ビルド時にhexまたはuf2を生成するもの)

(1)Adafruit Circuit Playground Bluefruit circuitplay-bluefruit (2)Adafruit Circuit Playground Express circuitplay-express (3)Adafruit Feather M0 feather-m0 (4)Adafruit Feather M4 feather-m4 (5)Adafruit ItsyBitsy M0 itsybitsy-m0 (6)Adafruit ItsyBitsy M4 itsybitsy-m4 (7)BBC micro:bit microbit (8)BBC micro:bit v2 microbit-v2 (9)seeed XIAO xiao (10)seeed wio-terminal wioterminal (11)ST Micro "Nucleo F103RB" nucleo-f103rb

・mbed系(nucleo,microbitなど)のボードは、hexファイルで書き込むので、
書き込みの際は、fHEX.shを使用すること。
・上で上げているadafruitならびにseeedのボードは、uf2ファイルで書き込むので、
書き込みの際は、fUF2.shを使用すること。

参考情報

(古い情報も含まれているので注意のこと)

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

WSL2関連:
WSL2でRDPサーバーを動かす
WSL2でSSHサーバーを動かす
WSL2でのplatformioやPico-SDKの利用方法

以上

続きを読む "WSL2でTinyGOを動かす"

| | コメント (0)

2020年10月20日 (火)

RaspberryPiにTinyGOをインストールする

2020/10/20+:
初版

TinyGO Install on RaspberryPi

TinyGO Install on RaspberryPi

概要

RaspberryPiにTinyGOをインストールする。

ターゲットは以下とする:
Seeeduino XIAO

準備

以下のツールを予めインストールする:
(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のインストール
以下の手順でインストールする:

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

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

cd ~/Downloads wget https://golang.org/dl/go1.13.7.linux-armv6l.tar.gz -O go.tar.gz sudo tar -C /usr/local -xzf go.tar.gz mkdir $HOME/go export GOPATH=$HOME/go export PATH=/usr/local/go/bin:$PATH:$GOPATH/bin

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

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

(3)ESP32関係ツールのインストール
(ターゲットとしてESP32を使用しないときはインストール不要)

# esp-idfツールのインストール mkdir -p ~/esp cd ~/esp git clone --recursive https://github.com/espressif/esp-idf.git cd ~/esp/esp-idf ./install.sh . $HOME/esp/esp-idf/export.sh alias get_idf='. $HOME/esp/esp-idf/export.sh' # esptool(書き込みツール)のインストール pip install esptool

rpiZeroの場合、以下のようなエラーが出るので インストールできない。
(rpi4はインストールできる)

$ ./install.sh Installing ESP-IDF tools ERROR: Platform Linux-armv6l appears to be unsupported

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

# tinygo/go export GOPATH=$HOME/go export PATH=/usr/local/go/bin:$PATH:$GOPATH/bin export PATH=$PATH:/usr/local/tinygo/bin export TIGOLIBS=$GOPATH/src/tinygo.org/x/drivers/ # for esp-idf alias get_idf='. $HOME/esp/esp-idf/export.sh'

バージョンの確認
以下のバージョンがインストールされる:

$ tinygo version tinygo version 0.15.0 linux/arm (using go version go1.13.7 and LLVM version 10.0.1) $ go version go version go1.13.7 linux/arm

ここまでで、RaspberryPiにtinygo/goがインストールできたので これ以降の実際の使い方は以下を参照のこと:

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

参考情報

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

Seeeduino XIAO用Grove シールド バッテリー管理チップ 搭載

ESP32 and ESP8266 support in TinyGo

コンピュータボードで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

以上

続きを読む "RaspberryPiにTinyGOをインストールする"

| | コメント (0)

2020年10月 9日 (金)

XIAOボードでTinyGOを動かす(Windows10版)

2020/10/9++:
初版

TinyGO Install XIAO on Windows10

TinyGO Install XIAO on Windows10

概要

以下のXIAOボードでTinyGOを動かす(Windows10版)。
tinygo_0.15.0でxiaoが正式にサポートされたので、それをインストールして使ってみる。
なお、ホストPCとしてはwindows10を想定している。

Seeeduino XIAO

tinygoのインストール

以下の手順でインストールする:
PowerShell:

# scoopをインストール Set-ExecutionPolicy RemoteSigned -scope CurrentUser iwr -useb get.scoop.sh | iex # goをインストールする scoop install go # tinygoをインストールする scoop install tinygo scoop update tinygo # その他のツールをインストールする scoop install git # windows用VScodeをインストールする scoop bucket add extras scoop install vscode

scoopの簡単な説明:

# xxxxをインストール scoop install # Scoop自身とローカル内にあるアプリの更新情報を更新 scoop update # 最新バージョンでないアプリがあるかをチェック scoop status # xxxxを更新 scoop update xxxx # すべてのアプリを更新 scoop update * # xxxxをアンインストール scoop uninstall xxxx

bootloader mode

XIAOにfirmwareを書き込めるモードを「bootloader mode」といい、このモードでは、USBストレージとしてArduinoが現れる。
arduinoのプログラムかCircuitPythonのプログラムが書き込まれている場合、RSTパッドを2度GNDとショートすると このモードに入りUSBストレージとしてarduinoが現れる。

examples/blinky1を動かす

xiaoをホストに接続して、bootloader-modeにして、以下を実行する:

tinygo flash -size full -target xiao examples/blinky1

コンパイルと書き込み実行が自動的に行われ、ボードのLEDが点滅する。

注意: シリアルポートが複数ある場合、xiaoが接続しているポートを指定する必要があるので 以下のようにportを指定する。

tinygo flash -port com4 -size full -target xiao examples/blinky1

上の例は、com4がxiaoのポートの場合である。
また、接続し直すと、comの番号が変わるので注意が必要である。
接続しているポートを確認は、TeraTermなどの通信ソフトを起動して シリアルポートの設定を見て知ることができる。

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

(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.LED led.Configure(machine.PinConfig{Mode: machine.PinOutput}) for { led.Low() time.Sleep(time.Millisecond * 1000) led.High() time.Sleep(time.Millisecond * 1000) } }

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

tinygo flash -size full -target xiao .

コンパイルと書き込み実行が自動的に行われ、ボードのLEDが点滅する。
(example/blinky1と区別しにくいと思うので、点滅周期を変更して実行してみる)

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

実行例: # 以下で、モジュールをインストールする go get tinygo.org/x/drivers # 参考(ダウンロードしたモジュールは「~/go/src」の配下に置かれる # インポートしたいモジュールを確認する ls ~/go/src/tinygo.org/x/drivers ... 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 ~/go/src/tinygo.org/x/driver/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 [10]color.RGBA func main() { led := machine.LED led.Configure(machine.PinConfig{Mode: machine.PinOutput}) 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) } }

続き:

# ボードをbootloader-modeに入れる tinygo flash -size full -target xiao .

D4をneopixelsのDIN,3V3をVCC,GNDをGNDに接続する。
書き込んで実行すると、接続したneopixelsが色を変えて光る。

tmp102(i2c)を動かす

以下を実行する:

mkdir tmp102 cd tmp102 cp ~/go/src/tinygo.org/x/driver/examples/tmp102/main.go . # bootloader-modeに入る tinygo flash -size full -target xiao .

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

# 以下で、モジュールをインストールする go get github.com/aykevl/ledsgo # 参考(以下のディレクトリにダウンロードしたものが置かれる) ls ~/go/src github.com tinygo.org ls ~/go/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.D2 // 2 for Grove Connector of XIAO //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) } }

XIAOのGrove_Shield経由でGroveコネクタにnexpixelsを接続する。
参照:Seeeduino XIAO用Grove シールド バッテリー管理チップ 搭載

以下でビルド実行する:

tinygo flash -size full -target=xiao .

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

XIAOのピン定義

port pin
PA02 A0/D0
PA04 A1/D1
PA10 A2/D2
PA11 A3/D3
PA08 A4/D4
PA09 A5/D5
PB08 A6/D6
PB09 A7/D7
PA07 A8/D8
PA05 A9/D9
PA06 A10/D10

参考情報

WindowsコマンドラインツールScoopのすすめ(基礎編)
Windowsでパッケージ管理したいなら、先ずScoopより始めよ

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

Seeeduino XIAO用Grove シールド バッテリー管理チップ 搭載

ESP32 and ESP8266 support in TinyGo

コンピュータボードで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

以上

続きを読む "XIAOボードでTinyGOを動かす(Windows10版)"

| | コメント (0)

2020年10月 8日 (木)

Teensy3.6ボードでTinyGOを動かす

2020/10/8:
初版

TinyGO Install Teensy3.6

TinyGO Install Teensy3.6

概要

以下のTeensy3.6ボードでTinyGOを動かす。
tinygo_0.15.0でteensy3.6がサポートされたので、それをインストールして使ってみる。
まだ、実装途上のようで、GPIO,UARTのみのようである。USBシリアル(USB CDC serial port)はサポートされない。
なお、ホストPCとしてはubuntu20.04を想定している。

Teensy 3.6

準備

以下のツールを予めインストールする:
(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 # xiaoには不要だが、ターゲットが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)teensy書き込みツールのインスール
以下の手順でインストールする:

mkdir teensy_loader_cli cd teensy_loader_cli sudo apt-get install libusb-dev wget https://raw.githubusercontent.com/PaulStoffregen/teensy_loader_cli/master/Makefile wget https://raw.githubusercontent.com/PaulStoffregen/teensy_loader_cli/master/teensy_loader_cli.c make sudo cp teensy_loader_cli /usr/bin/

以上の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/blinky1を動かす

teensy3.6ボードをPCに接続して、以下を実行する:

tinygo flash -size full -target teensy36 examples/blinky1 <省略> (hint: press the reset button) #以上のメッセージが表示されたら、ボードのRESETボタンを押す <省略> Programming........ Booting

コンパイルと書き込み実行が自動的に行われ、ボードのLEDが点滅する。

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

(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.LED led.Configure(machine.PinConfig{Mode: machine.PinOutput}) for { led.Low() time.Sleep(time.Millisecond * 100) led.High() time.Sleep(time.Millisecond * 100) } }

(3)ビルド実行
teensy3.6ボードをPCに接続して以下を実行する:

tinygo flash -size full -target teensy36 . <省略> (hint: press the reset button) #以上のメッセージが表示されたら、ボードのRESETボタンを押す <省略> Programming........ Booting

コンパイルと書き込み実行が自動的に行われ、ボードのLEDが点滅する。
(example/blinky1と区別しにくいと思うので、点滅周期を変更して実行してみる)

参考情報

Teenst3.6 Pinout:
https://cdn.sparkfun.com/datasheets/Dev/Arduino/Boards/teensy36_front.pdf
https://cdn.sparkfun.com/datasheets/Dev/Arduino/Boards/teensy36_back.pdf

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

Seeeduino XIAO用Grove シールド バッテリー管理チップ 搭載

ESP32 and ESP8266 support in TinyGo

コンピュータボードで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

以上

続きを読む "Teensy3.6ボードでTinyGOを動かす"

| | コメント (0)

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

以上

続きを読む "ESP32ボードでTinyGOを動かす(v2)"

| | コメント (0)

2020年10月 5日 (月)

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

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

2020/10/5:
初版

TinyGO Install XIAO v2

TinyGO Install XIAO v2

概要

以下のXIAOボードでTinyGOを動かす(v2)。
tinygo_0.15.0でxiaoが正式にサポートされたので、それをインストールして使ってみる。
前の記事では、その時点では、XIAOはサポート対象外だったので、同じチップを採用しているFeather-M0としてビルドして試用していた。
なお、ホストPCとしてはubuntu20.04を想定している。

Seeeduino XIAO

準備

以下のツールを予めインストールする:
(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 # xiaoには不要だが、ターゲットが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)BOSSAのインスール
ここでは使用しないが、xiaoの書き込みツールをインストールする:

sudo apt-get update sudo apt-get install bossa sudo apt-get install bossa-cli

bossaでGUIツール、bossacでCLIツールが起動する。
参照:https://www.shumatech.com/web/products/bossa

以上の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/

bootloader mode

XIAOにfirmwareを書き込めるモードを「bootloader mode」といい、このモードでは、USBストレージとしてArduinoが現れる。
arduinoのプログラムかCircuitPythonのプログラムが書き込まれている場合、RSTパッドを2度GNDとショートすると このモードに入りUSBストレージとしてarduinoが現れる。

examples/blinky1を動かす

xiaoをホストに接続して、bootloader-modeにして、以下を実行する:

tinygo flash -size full -target xiao examples/blinky1

コンパイルと書き込み実行が自動的に行われ、ボードのLEDが点滅する。

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

(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.LED led.Configure(machine.PinConfig{Mode: machine.PinOutput}) for { led.Low() time.Sleep(time.Millisecond * 1000) led.High() time.Sleep(time.Millisecond * 1000) } }

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

tinygo flash -size full -target xiao .

コンパイルと書き込み実行が自動的に行われ、ボードのLEDが点滅する。
(example/blinky1と区別しにくいと思うので、点滅周期を変更して実行してみる)

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 [10]color.RGBA func main() { led := machine.LED led.Configure(machine.PinConfig{Mode: machine.PinOutput}) 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) } }

続き:

# ボードをbootloader-modeに入れる tinygo flash -size full -target xiao .

D4をneopixelsのDIN,3V3をVCC,GNDをGNDに接続する。
書き込んで実行すると、接続したneopixelsが色を変えて光る。

tmp102(i2c)を動かす

以下を実行する:

mkdir tmp102 cd tmp102 cp $TIGOLIBS/examples/tmp102/main.go . # bootloader-modeに入る tinygo flash -size full -target xiao .

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

# 以下で、モジュールをインストールする 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.D2 // 2 for Grove Connector of XIAO //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) } }

XIAOのGrove_Shield経由でGroveコネクタにnexpixelsを接続する。
参照:Seeeduino XIAO用Grove シールド バッテリー管理チップ 搭載

以下でビルド実行する:

tinygo flash -size full -target=xiao .

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

XIAOのピン定義

port pin
PA02 A0/D0
PA04 A1/D1
PA10 A2/D2
PA11 A3/D3
PA08 A4/D4
PA09 A5/D5
PB08 A6/D6
PB09 A7/D7
PA07 A8/D8
PA05 A9/D9
PA06 A10/D10

参考情報

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

Seeeduino XIAO用Grove シールド バッテリー管理チップ 搭載

ESP32 and ESP8266 support in TinyGo

旧版:XIAOボードでTinyGOを動かす

コンピュータボードで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

以上

続きを読む "XIAOボードでTinyGOを動かす(v2)"

| | コメント (0)

2020年6月11日 (木)

XIAOボードでTinyGOを動かす

2020/6/11

TinyGO Install XIAO

TinyGO Install XIAO

概要

以下のXIAOボードでTinyGOを動かす。XIAOはサポート対象のボードになっていないので、同じチップを採用しているFeather-M0としてビルドする。 (ホストPCとしてはubuntuを想定している)

Seeeduino XIAO

準備

以下のツールを予めインストールする:
(1)st-flash

sudo apt install cmake sudo apt install libusb-1.0 git clone https://github.com/texane/stlink.git cd stlink make cd build/Release sudo make install sudo ldconfig

XIAOでは使用しない。

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

(1)dockerのインストール sudo apt-get docker sudo apt-get docker.io sudo groupadd docker sudo usermod -aG docker $USER sudo systemctl enable docker sudo systemctl start docker #ここで再起動する (2)TinyGOのインストール docker pull tinygo/tinygo:latest

(3)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の関係ファイル/ディレクトリが置かれる。

alias用スクリプトの設定:
nano ~/setenv4tg
以下の内容に編集する:

# for XIAO alias tgxiao="docker run -it --rm -v $PWD:/go/src/github.com/myusr/myapp -w /go/src/github.com/myusr/myapp -e GOPATH=/go tinygo/tinygo:latest tinygo build -size full -o xiao.uf2 -target feather-m0 ." alias fxiao="cp xiao.uf2 /media/USER_NAME/Arduino/"

「USER_NAME」の部分は自分の環境に合わせること。

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

bootloader mode

XIAOにfirmwareを書き込めるモードを「bootloader mode」といい、このモードでは、USBストレージとしてArduinoが現れる。 arduinoのプログラムかCircuitPythonのプログラムが書き込まれている場合、RSTパッドを2度GNDとショートすると このモードに入りUSBストレージとしてarduinoが現れる。

blinkyを動かす

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

cd tigo_ws mkdir blinky cd blinky #aliasを設定する source ~/setenv4tg #上のコマンドはプロジェクトのディレクトリで実行する必要がある。 #「source」は「.」に置き換えることができる。

(2)プロジェクト(blinky)ディレクトリに 以下のファイルを作成する:
blinky.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 * 1000) led.High() time.Sleep(time.Millisecond * 1000) } }

(3)ビルド
以下を実行してビルドする:

tgxiao

これで、xiao.uf2が作成される。

出力例:

$ tgxiao code rodata data bss | flash ram | package 3400 13 0 0 | 3413 0 | (bootstrap) 40 0 0 0 | 40 0 | github 0 36 0 0 | 36 0 | handleHardFault$string 108 24 0 4 | 132 4 | internal/task 2992 43 5 2054 | 3040 2059 | machine 0 16 0 130 | 16 130 | machine$alloc 2302 79 0 45 | 2381 45 | runtime 92 0 0 0 | 92 0 | time 8934 211 5 2233 | 9150 2238 | (sum) 9476 - 8 4312 | 9484 4320 | (all) $ ls -l xiao.* -rwxr-xr-x 1 root root 19456 6月 11 09:54 xiao.uf2

(4)書き込み
以下を実行して書き込む:

# ボードとホストPCをUSBで接続する。 # RSTパッドを2度GNDショートして、bootloader-modeに入る。 # Arduinoのディレクトリが現れる。 fxiao

ボードの赤いLED(D13)が1秒周期で点滅すれば動作としてはOKとなる。

serialを動かす

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

cd tigo_ws mkdir serial cd serial #aliasを設定する source ~/setenv4tg #上のコマンドはプロジェクトのディレクトリで実行する必要がある

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

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

(3)ビルド/書き込み 以下を実行する:

tgxiao # ボードとホストPCをUSBで接続する。 # RSTパッドを2度GNDショートして、bootloader-modeに入る。 # Arduinoのディレクトリが現れる。 fxiao

以上で、プログラムが起動する。

(4)シリアル通信の起動
以下のように別の端末でシリアル通信を起動すると シリアルに結果が出力される:

$ picocom /dev/ttyACM0 -b115200 picocom v1.7 <省略> Terminal ready hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! ...

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 # モジュールをディレクトリの形でコピーする cp -r $TIGOLIBS/ws2812 . # exampleのmain.goをコピーする cp $TIGOLIBS/examples/ws2812/main.go . # main.goを編集する leafpad 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" "./ws2812" //"tinygo.org/x/drivers/ws2812" ) var leds [64]color.RGBA //var leds [10]color.RGBA func main() { led := machine.LED led.Configure(machine.PinConfig{Mode: machine.PinOutput}) 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} leds[i] = color.RGBA{R: 0x0f, G: 0x00, B: 0x00} } else { //leds[i] = color.RGBA{R: 0x00, G: 0xff, B: 0x00} leds[i] = color.RGBA{R: 0x00, G: 0x0f, B: 0x00} } } ws.WriteColors(leds[:]) led.Set(rg) time.Sleep(100 * time.Millisecond) } }

続き:

# 以下、ビルド&書き込み . ~/setenv4tg tgxiao # ボードをbootloader-modeに入れる fxiao

D4をneopixelsのDIN,3V3をVCC,GNDをGNDに接続する。 書き込んで実行すると、接続したneopixelsが色を変えて光る。

tmp102(i2c)を動かす

mkdir tmp102 cd tmp102 cp -r $TIGOLIBS/tmp102 . cp $TIGOLIBS/examples/tmp102/main.go . leafpad main.go # 以下の内容にする: (修正点はimportの部分のみ)
package main import ( "fmt" "machine" "time" "./tmp102" //"tinygo.org/x/drivers/tmp102" ) func main() { machine.I2C0.Configure(machine.I2CConfig{ Frequency: machine.TWI_FREQ_400KHZ, }) thermo := tmp102.New(machine.I2C0) thermo.Configure(tmp102.Config{}) for { temp, _ := thermo.ReadTemperature() print(fmt.Sprintf("%.2f°C\r\n", float32(temp)/1000.0)) time.Sleep(time.Millisecond * 1000) } }

続き:

# 以下、ビルド&書き込み . ~/setenv4tg tgxiao # bootloader-modeに入る fxiao

ピン定義が実際のXIOAと異なるせいで動作しなかった。

総括

制限付きだがXIAOでTinyGOが動作することが確認できた。 本格的に使用するには、ピン定義を実際のXIAOのものに合わせる必要がある。

Feather-M0のピン定義

流用しているFeather-M0のピン定義を参考に以下にあげる:

const ( D0 = PA11 // UART0 RX D1 = PA10 // UART0 TX D2 = NoPin // does not seem to exist D3 = PA09 D4 = PA08 D5 = PA15 // PWM available D6 = PA20 // PWM available D7 = NoPin // does not seem to exist D8 = PA06 D9 = PA07 // PWM available D10 = PA18 // can be used for PWM or UART1 TX D11 = PA16 // can be used for PWM or UART1 RX D12 = PA19 // PWM available D13 = PA17 // PWM available ) const ( A0 = PA02 // ADC/AIN[0] A1 = PB08 // ADC/AIN[2] A2 = PB09 // ADC/AIN[3] A3 = PA04 // ADC/AIN[4] A4 = PA05 // ADC/AIN[5] A5 = PB02 // ADC/AIN[10] ) const ( LED = D13 ) const ( USBCDC_DM_PIN = PA24 USBCDC_DP_PIN = PA25 ) const ( UART_TX_PIN = D10 UART_RX_PIN = D11 ) const ( SDA_PIN = PA22 // SDA: SERCOM3/PAD[0] SCL_PIN = PA23 // SCL: SERCOM3/PAD[1] ) const ( SPI0_SCK_PIN = PB11 // SCK: SERCOM4/PAD[3] SPI0_MOSI_PIN = PB10 // MOSI: SERCOM4/PAD[2] SPI0_MISO_PIN = PA12 // MISO: SERCOM4/PAD[0] )

XIAOのピン定義

port pin
PA02 A0/D0
PA04 A1/D1
PA10 A2/D2
PA11 A3/D3
PA08 A4/D4
PA09 A5/D5
PB08 A6/D6
PB09 A7/D7
PA07 A8/D8
PA05 A9/D9
PA06 A10/D10

参考情報

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

以上

続きを読む "XIAOボードでTinyGOを動かす"

| | コメント (0)

2020年5月29日 (金)

Feather-M4-ExpressボードでTinyGOを動かす

2020/5/29

TinyGO Install Feather-M4-Express

TinyGO Install Feather-M4-Express

概要

以下のFeather-M4-ExpressボードでTinyGOを動かす。 以降、Feather-M4-ExpressをFeatherM4とする。 (ホストPCとしてはubuntuを想定している)

Feather-M4-Express

準備

以下のツールを予めインストールする:
(1)st-flash

sudo apt install cmake sudo apt install libusb-1.0 git clone https://github.com/texane/stlink.git cd stlink make cd build/Release sudo make install sudo ldconfig

FeatherM4では使用しない。

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

(1)dockerのインストール sudo apt-get docker sudo apt-get docker.io sudo groupadd docker sudo usermod -aG docker $USER sudo systemctl enable docker sudo systemctl start docker #ここで再起動する (2)TinyGOのインストール docker pull tinygo/tinygo:latest

(3)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の関係ファイル/ディレクトリが置かれる。

(4)alias用スクリプトの設定
nano ~/setenv4tg
以下の内容に編集する:

# for Feather-M4-Express alias tgfm4="docker run -it --rm -v $PWD:/go/src/github.com/myusr/myapp -w /go/src/github.com/myusr/myapp -e GOPATH=/go tinygo/tinygo:latest tinygo build -size full -o fm4.uf2 -target feather-m4 ." alias ffm4="cp fm4.uf2 /media/USER_NAME/FEATHERBOOT/"

「USER_NAME」の部分は自分の環境に合わせること。

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

bootloader mode

FeatherM4にfirmwareを書き込めるモードを「bootloader mode」といい、このモードでは、USBストレージとしてFEATHERBOOTが現れる。 FeatherM4に事前に、どんなプログラムが書かれていたかで、やり方が異なる。 arduinoのプログラムかCircuitPythonのプログラムが書き込まれている場合、resetをdouble-clickのように2度押すと このモードに入りUSBストレージとしてFEATHERBOOTが現れる。その他の場合、resetを1度押すと、このモードに入る。

blinkyを動かす

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

cd tigo_ws mkdir blinky cd blinky #aliasを設定する source ~/setenv4tg #上のコマンドはプロジェクトのディレクトリで実行する必要がある。 #「source」は「.」に置き換えることができる。

(2)プロジェクト(blinky)ディレクトリに 以下のファイルを作成する:
blinky.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 * 1000) led.High() time.Sleep(time.Millisecond * 1000) } }

(3)ビルド
以下を実行してビルドする:

tgfm4

これで、fm4.uf2が作成される。

出力例:

$ tgfm4 code rodata data bss | flash ram | package 1304 13 0 0 | 1317 0 | (bootstrap) 36 0 0 0 | 36 0 | github 0 36 0 0 | 36 0 | handleHardFault$string 102 24 0 4 | 126 4 | internal/task 3114 27 5 2054 | 3146 2059 | machine 0 16 0 130 | 16 130 | machine$alloc 2276 79 0 45 | 2355 45 | runtime 104 0 0 0 | 104 0 | time 6936 195 5 2233 | 7136 2238 | (sum) 7860 - 8 6360 | 7868 6368 | (all) $ ls -l *.uf2 -rwxr-xr-x 1 root root 15872 5月 29 11:19 fm4.uf2

(4)書き込み
以下を実行して書き込む:

# ボードとホストPCをUSBで接続する。 # resetを2度押して、bootloader-modeに入る。 # FEATHERBOOTのディレクトリが現れる。 ffm4

ボードの赤いLED(D13)が1秒周期で点滅すれば動作としてはOKとなる。

serialを動かす

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

cd tigo_ws mkdir serial cd serial #aliasを設定する source ~/setenv4tg #上のコマンドはプロジェクトのディレクトリで実行する必要がある

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

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

(3)ビルド/書き込み 以下を実行する:

tgfm4 # ボードとホストPCをUSBで接続する。 # resetを2度押して、bootloader-modeに入る。 # FEATHERBOOTのディレクトリが現れる。 ffm4

以上で、プログラムが起動する。

(4)シリアル通信の起動
以下のように別の端末でシリアル通信を起動すると シリアルに結果が出力される:

$ picocom /dev/ttyACM0 -b115200 picocom v1.7 <省略> Terminal ready hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! ...

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 # モジュールをディレクトリの形でコピーする cp -r $TIGOLIBS/ws2812 . # exampleのmain.goをコピーする cp $TIGOLIBS/examples/ws2812/main.go . # main.goを編集する leafpad main.go # 以下のように修正する: (外部のneopixelを使用するように修正している)
// 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" "./ws2812" //"tinygo.org/x/drivers/ws2812" ) var leds [64]color.RGBA //var leds [10]color.RGBA func main() { led := machine.LED led.Configure(machine.PinConfig{Mode: machine.PinOutput}) neo := machine.D4 // for Feater-M4 // 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} leds[i] = color.RGBA{R: 0x0f, G: 0x00, B: 0x00} } else { //leds[i] = color.RGBA{R: 0x00, G: 0xff, B: 0x00} leds[i] = color.RGBA{R: 0x00, G: 0x0f, B: 0x00} } } ws.WriteColors(leds[:]) led.Set(rg) time.Sleep(100 * time.Millisecond) } }

続き:

# 以下、ビルド&書き込み . ~/setenv4tg tgcpx # ボードをbootloader-modeに入れる fcpx

neopixelのDINをD4と接続して実行すると、neopixelが光る。

外部のtmp102(i2c)を動かす

mkdir tmp102 cd tmp102 cp -r $TIGOLIBS/tmp102 . cp $TIGOLIBS/examples/tmp102/main.go . leafpad main.go # 以下の内容にする: (修正点はimportの部分のみ)
package main import ( "fmt" "machine" "time" "./tmp102" //"tinygo.org/x/drivers/tmp102" ) func main() { machine.I2C0.Configure(machine.I2CConfig{ Frequency: machine.TWI_FREQ_400KHZ, }) thermo := tmp102.New(machine.I2C0) thermo.Configure(tmp102.Config{}) for { temp, _ := thermo.ReadTemperature() print(fmt.Sprintf("%.2f°C\r\n", float32(temp)/1000.0)) time.Sleep(time.Millisecond * 1000) } }

続き:

# 以下、ビルド&書き込み . ~/setenv4tg tgfm4 # bootloader-modeに入る ffm4

なにかしらのバグがあるのか実行後に「/dev/ttyACM0」が出現せず、プログラムが正常に動作しなかった。

FeatherM4のピン定義

const ( D0 = PB17 // UART0 RX/PWM available D1 = PB16 // UART0 TX/PWM available D4 = PA14 // PWM available D5 = PA16 // PWM available D6 = PA18 // PWM available D8 = PB03 // built-in neopixel D9 = PA19 // PWM available D10 = PA20 // can be used for PWM or UART1 TX D11 = PA21 // can be used for PWM or UART1 RX D12 = PA22 // PWM available D13 = PA23 // PWM available D21 = PA13 // PWM available D22 = PA12 // PWM available D23 = PB22 // PWM available D24 = PB23 // PWM available D25 = PA17 // PWM available ) const ( A0 = PA02 // ADC/AIN[0] A1 = PA05 // ADC/AIN[2] A2 = PB08 // ADC/AIN[3] A3 = PB09 // ADC/AIN[4] A4 = PA04 // ADC/AIN[5] A5 = PA06 // ADC/AIN[10] ) const ( LED = D13 ) const ( UART_TX_PIN = D1 UART_RX_PIN = D0 ) const ( UART2_TX_PIN = A4 UART2_RX_PIN = A5 ) const ( SDA_PIN = D22 // SDA: SERCOM2/PAD[0] SCL_PIN = D21 // SCL: SERCOM2/PAD[1] ) const ( SPI0_SCK_PIN = D25 // SCK: SERCOM1/PAD[1] SPI0_MOSI_PIN = D24 // MOSI: SERCOM1/PAD[3] SPI0_MISO_PIN = D23 // MISO: SERCOM1/PAD[2] )

参考情報

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

Adafruit Feather M4 Express - Overview
Adafruit Feather M4 Express - PINOUT
Adafruit Feater M4 Express Pin Assign

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)

以上

続きを読む "Feather-M4-ExpressボードでTinyGOを動かす"

| | コメント (0)

Circuit-Playground-ExpressボードでTinyGOを動かす(v2)

2020/5/26

TinyGO Install Circuit-Playground-Express

TinyGO Install Circuit-Playground-Express

概要

以下のCircuit-Playground-ExpressボードでTinyGOを動かす。 以降、Circuit-Playground-ExpressをCPXとする。 (ホストPCとしてはubuntuを想定している)

Circuit Playground Express

準備

以下のツールを予めインストールする:
(1)st-flash

sudo apt install cmake sudo apt install libusb-1.0 git clone https://github.com/texane/stlink.git cd stlink make cd build/Release sudo make install sudo ldconfig

CPXでは使用しない。

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

(1)dockerのインストール sudo apt-get docker sudo apt-get docker.io sudo groupadd docker sudo usermod -aG docker $USER sudo systemctl enable docker sudo systemctl start docker #ここで再起動する (2)TinyGOのインストール docker pull tinygo/tinygo:latest

(3)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の関係ファイル/ディレクトリが置かれる。

alias用スクリプトの設定:
nano ~/setenv4tg
以下の内容に編集する:

# for CPX(Circuit-Playground-Express) alias tgcpx="docker run -it --rm -v $PWD:/go/src/github.com/myusr/myapp -w /go/src/github.com/myusr/myapp -e GOPATH=/go tinygo/tinygo:latest tinygo build -size full -o cpx.uf2 -target circuitplay-express ." alias fcpx="cp cpx.uf2 /media/USER_NAME/CPLAYBOOT/" # for nucleo-f103rb alias tgf103="docker run -it --rm -v $PWD:/go/src/github.com/myusr/myapp -w /go/src/github.com/myusr/myapp -e GOPATH=/go tinygo/tinygo:latest tinygo build -size full -o firmware.bin -target nucleo-f103rb ." alias stf="st-flash write firmware.bin 0x8000000"

「USER_NAME」の部分は自分の環境に合わせること。 なお、この設定には別ボード用も含む。

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

bootloader mode

CPXにfirmwareを書き込めるモードを「bootloader mode」といい、このモードでは、USBストレージとしてCPLAYBOOTが現れる。 CPXに事前に、どんなプログラムが書かれていたかで、やり方が異なる。 arduinoのプログラムかCircuitPythonのプログラムが書き込まれている場合、resetをdouble-clickのように2度押すと このモードに入りUSBストレージとしてCPLAYBOOTが現れる。その他の場合、resetを1度押すと、このモードに入る。

ただし、本家サイトの説明ではdouble-clickのようにと説明があるが 実際には、2度目のリセットは少し長めに押してから離さないとbootloader-modeには入らない。 このモードに入るとボード上のnexpixelが緑になる。
失敗すると赤色になる。その場合、再度トライすること.

工場出荷の状態では、arduinoのデモプログラムが入っているようなので 実際にはresetの2度押しで、このモードに入る。

blinkyを動かす

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

cd tigo_ws mkdir blinky cd blinky #aliasを設定する source ~/setenv4tg #上のコマンドはプロジェクトのディレクトリで実行する必要がある。 #「source」は「.」に置き換えることができる。

(2)プロジェクト(blinky)ディレクトリに 以下のファイルを作成する:
blinky.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 * 1000) led.High() time.Sleep(time.Millisecond * 1000) } }

(3)ビルド
以下を実行してビルドする:

tgcpx

これで、cpx.uf2が作成される。

出力例:

$ tgcpx code rodata data bss | flash ram | package 3442 13 0 0 | 3455 0 | (bootstrap) 60 0 0 0 | 60 0 | github 0 36 0 0 | 36 0 | handleHardFault$string 108 24 0 4 | 132 4 | internal/task 2992 43 5 2054 | 3040 2059 | machine 0 16 0 130 | 16 130 | machine$alloc 2302 79 0 45 | 2381 45 | runtime 96 0 0 0 | 96 0 | time 9000 211 5 2233 | 9216 2238 | (sum) 9540 - 8 4312 | 9548 4320 | (all) $ ls -l cpx.* -rwxr-xr-x 1 root root 19456 5月 25 10:35 cpx.uf2

(4)書き込み
以下を実行して書き込む:

# ボードとホストPCをUSBで接続する。 # resetを2度押して、bootloader-modeに入る。 # CPLAYBOOTのディレクトリが現れる。 fcpx

ボードの赤いLED(D13)が1秒周期で点滅すれば動作としてはOKとなる。

serialを動かす

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

cd tigo_ws mkdir serial cd serial #aliasを設定する source ~/setenv4tg #上のコマンドはプロジェクトのディレクトリで実行する必要がある

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

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

(3)ビルド/書き込み 以下を実行する:

tgcpx # ボードとホストPCをUSBで接続する。 # resetを2度押して、bootloader-modeに入る。 # CPLAYBOOTのディレクトリが現れる。 fcpx

以上で、プログラムが起動する。

(4)シリアル通信の起動
以下のように別の端末でシリアル通信を起動すると シリアルに結果が出力される:

$ picocom /dev/ttyACM0 -b115200 picocom v1.7 <省略> Terminal ready hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! ...

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 # モジュールをディレクトリの形でコピーする cp -r $TIGOLIBS/ws2812 . # exampleのmain.goをコピーする cp $TIGOLIBS/examples/ws2812/main.go . # main.goを編集する leafpad 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" "./ws2812" //"tinygo.org/x/drivers/ws2812" ) var leds [10]color.RGBA func main() { led := machine.LED led.Configure(machine.PinConfig{Mode: machine.PinOutput}) 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} leds[i] = color.RGBA{R: 0x0f, G: 0x00, B: 0x00} } else { //leds[i] = color.RGBA{R: 0x00, G: 0xff, B: 0x00} leds[i] = color.RGBA{R: 0x00, G: 0x0f, B: 0x00} } } ws.WriteColors(leds[:]) led.Set(rg) time.Sleep(100 * time.Millisecond) } }

続き:

# 以下、ビルド&書き込み . ~/setenv4tg tgcpx # ボードをbootloader-modeに入れる fcpx

書き込みが終わると、ボードのnexpixelが色を変えて光る。

温度/光センサを動かす

mkdir thermistor cd thermistor cp -r $TIGOLIBS/thermistor . cp $TIGOLIBS/examples/thermistor/main.go . # main.goを編集する leafpad main.go # 以下のように修正する: (光センサの部分も追加している)
// This example uses the settings for the thermistor that is built in to the // Adafruit Circuit Playground Express. package main import ( "machine" "time" "./thermistor" //"tinygo.org/x/drivers/thermistor" ) func main() { machine.InitADC() lsense := machine.ADC{machine.LIGHTSENSOR} // Light Sensor (set ADC pin) lsense.Configure() // start the pin's ADC function sensor := thermistor.New(machine.TEMPSENSOR) // Tempe Sensor (set ADC pin) sensor.Configure() for { temp, _ := sensor.ReadTemperature() println("Temperature:", temp/1000, "°C") println("Light Sensor:", lsense.Get()) time.Sleep(1 * time.Second) } }

続き:

# 以下、ビルド&書き込み . ~/setenv4tg tgcpx # bootloader-modeに入る fcpx

実行後、「picocom /dev/ttyACM0 -b115200」を実行して、シリアル出力を表示すると、温度と光センサの値が出力される。

加速度センサ(LIS3DH(i2c))を動かす

mkdir lis3dh cd lis3dh cp -r $TIGOLIBS/lis3dh . cp $TIGOLIBS/examples/lis3dh/main.go . leafpad main.go # 以下の内容にする: (修正点はimportの部分のみ)
// Connects to a LIS3DH I2C accelerometer on the Adafruit Circuit Playground Express. package main import ( "machine" "time" "./lis3dh" //"tinygo.org/x/drivers/lis3dh" ) var i2c = machine.I2C1 func main() { i2c.Configure(machine.I2CConfig{SCL: machine.SCL1_PIN, SDA: machine.SDA1_PIN}) accel := lis3dh.New(i2c) accel.Address = lis3dh.Address1 // address on the Circuit Playground Express accel.Configure() accel.SetRange(lis3dh.RANGE_2_G) println(accel.Connected()) for { x, y, z, _ := accel.ReadAcceleration() println("X:", x, "Y:", y, "Z:", z) rx, ry, rz := accel.ReadRawAcceleration() println("X (raw):", rx, "Y (raw):", ry, "Z (raw):", rz) time.Sleep(time.Millisecond * 100) } }

続き:

# 以下、ビルド&書き込み . ~/setenv4tg tgcpx # bootloader-modeに入る fcpx

実行後、「picocom /dev/ttyACM0 -b115200」を実行して、シリアル出力を表示すると、加速度センサの値が出力される。

出力例:

$ picocom /dev/ttyACM0 -b115200 ... Terminal ready X: -110378 Y: -16605 Z: 958241 X (raw): -1984 Y (raw): -144 Z (raw): 16064 X: -136752 Y: -22466 Z: 999267 X (raw): -2240 Y (raw): -368 Z (raw): 15920 X: -136752 Y: -13675 Z: 993406 X (raw): -2224 Y (raw): -320 Z (raw): 16272

音センサ(マイク)を動かす

mkdir lis3dh cd lis3dh cp -r $TIGOLIBS/microphone . cp $TIGOLIBS/examples/microphone/main.go . leafpad main.go # 以下の内容にする: (修正点はimportの部分のみ)
// Example using the i2s hardware interface on the Adafruit Circuit Playground Express // to read data from the onboard MEMS microphone. // // Uses ideas from the https://github.com/adafruit/Adafruit_CircuitPlayground repo. // package main import ( "machine" "./microphone" //"tinygo.org/x/drivers/microphone" ) const ( defaultSampleRate = 22000 quantizeSteps = 64 msForSPLSample = 50 defaultSampleCountForSPL = (defaultSampleRate / 1000) * msForSPLSample ) func main() { machine.I2S0.Configure(machine.I2SConfig{ Mode: machine.I2SModePDM, AudioFrequency: defaultSampleRate * quantizeSteps / 16, ClockSource: machine.I2SClockSourceExternal, Stereo: true, }) mic := microphone.New(machine.I2S0) mic.SampleCountForSPL = defaultSampleCountForSPL mic.Configure() for { spl, maxval := mic.GetSoundPressure() println("C", spl, "max", maxval) } }

続き:

# 以下、ビルド&書き込み . ~/setenv4tg tgcpx # bootloader-modeに入る fcpx

実行後、「picocom /dev/ttyACM0 -b115200」を実行して、シリアル出力を表示すると、音センサ(マイク)の値が出力される。

出力例:

$ picocom /dev/ttyACM0 -b115200 ... C 85536 max 43 C 100324 max 236 C 85735 max 44 C 85735 max 44 C 85735 max 44 C 85536 max 43 C 104530 max 383 C 88990 max 64 C 85536 max 43 C 89125 max 65 C 85536 max 43 C 106178 max 463 C 85536 max 43 C 85735 max 44 C 85735 max 44 C 98973 max 202 C 85735 max 44 ...

外部のtmp102(i2c)を動かす

mkdir tmp102 cd tmp102 cp -r $TIGOLIBS/tmp102 . cp $TIGOLIBS/examples/tmp102/main.go . leafpad main.go # 以下の内容にする: (修正点はimportの部分のみ)
package main import ( "fmt" "machine" "time" "./tmp102" //"tinygo.org/x/drivers/tmp102" ) func main() { machine.I2C0.Configure(machine.I2CConfig{ Frequency: machine.TWI_FREQ_400KHZ, }) thermo := tmp102.New(machine.I2C0) thermo.Configure(tmp102.Config{}) for { temp, _ := thermo.ReadTemperature() print(fmt.Sprintf("%.2f°C\r\n", float32(temp)/1000.0)) time.Sleep(time.Millisecond * 1000) } }

続き:

# 以下、ビルド&書き込み . ~/setenv4tg tgcpx # bootloader-modeに入る fcpx

CPXにtmp102(i2c)を接続して、プログラム実行後、「picocom /dev/ttyACM0 -b115200」を実行して、シリアル出力を表示すると、tmp102(温度)の値が出力される。

CPXのピン定義

const ( LED = D13 NEOPIXELS = D8 BUTTONA = D4 BUTTONB = D5 SLIDER = D7 // built-in slide switch BUTTON = BUTTONA BUTTON1 = BUTTONB LIGHTSENSOR = A8 TEMPSENSOR = A9 PROXIMITY = A10 ) Capacitive Touch: A1,A2,A3,A4,A5,A6,A7 Infrared Receive and Transmit: IR TX: D29 IR RX: D39 (The direct analog value is available from pin A10) const ( SDA_PIN = PB02 // I2C0 external SCL_PIN = PB03 // I2C0 external SDA1_PIN = PA00 // I2C1 internal SCL1_PIN = PA01 // I2C1 internal )

参考情報

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)

以上

続きを読む "Circuit-Playground-ExpressボードでTinyGOを動かす(v2)"

| | コメント (0)

2020年5月26日 (火)

Nucleo-F103RBボードでTinyGOを動かす(v2)

2020/5/26:
分かりやすくするために一部修正した。

2020/5/12:
初版

TinyGO Install Nucleo-F103RB

TinyGO Install Nucleo-F103RB

概要

Nucleo-F103RBボードでTinyGOを動かす。 ここでは、Docker版のTinyGOを使用する。 (ホストPCとしてはubuntuを想定している)

準備

以下のツールを予めインストールする:
(1)st-flash

sudo apt install cmake sudo apt install libusb-1.0 git clone https://github.com/texane/stlink.git cd stlink make cd build/Release sudo make install sudo ldconfig

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

(1)dockerのインストール sudo apt-get docker sudo apt-get docker.io sudo groupadd docker sudo usermod -aG docker $USER sudo systemctl enable docker sudo systemctl start docker #ここで再起動する (2)TinyGOのインストール docker pull tinygo/tinygo:latest

(4)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の関係ファイル/ディレクトリが置かれる。

(5)alias用スクリプトの設定
nano ~/setenv4tg
以下の内容に編集する:

# for nucleo-f103rb alias tgf103="docker run -it --rm -v $PWD:/go/src/github.com/myusr/myapp -w /go/src/github.com/myusr/myapp -e GOPATH=/go tinygo/tinygo:latest tinygo build -size full -o firmware.bin -target nucleo-f103rb ." alias stf="st-flash write firmware.bin 0x8000000"

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

blinkyを動かす

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

cd tigo_ws mkdir blinky cd blinky #aliasを設定する source ~/setenv4tg #上のコマンドはプロジェクトのディレクトリで実行する必要がある。 #「source」は「.」に置き換えることができる。

(2)プロジェクト(blinky)ディレクトリに 以下のファイルを作成する: blinky.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 * 1000) led.High() time.Sleep(time.Millisecond * 1000) } }

(3)ビルド
以下を実行してビルドする:

tgf103 出力例: ```bash docker run -it --rm -v $PWD:/go/src/github.com/myusr/myapp -w /go/src/github.com/myusr/myapp -e GOPATH=/go tinygo/tinygo:latest tinygo build -size full -o firmware.bin -target nucleo-f103rb . code rodata data bss | flash ram | package 796 21 0 130 | 817 130 | (bootstrap) 38 0 0 0 | 38 0 | github 0 36 0 0 | 36 0 | handleHardFault$string 102 24 0 4 | 126 4 | internal/task 202 21 0 0 | 223 0 | machine 1690 86 0 45 | 1776 45 | runtime 38 0 0 0 | 38 0 | runtime/volatile 104 0 0 0 | 104 0 | time 2970 188 0 179 | 3158 179 | (sum) 3596 - 0 2244 | 3596 2244 | (all)

(4)書き込み
以下を実行して書き込む:

#ボードとホストPCをUSBで接続する: stf

ボードのグリーンLEDが1秒周期で点滅すれば動作としてはOKとなる。

出力例:

$ stf st-flash 1.6.0 2020-05-12T11:59:36 INFO common.c: Loading device parameters.... 2020-05-12T11:59:36 INFO common.c: Device connected is: F1 Medium-density device, id 0x20036410 2020-05-12T11:59:36 INFO common.c: SRAM size: 0x5000 bytes (20 KiB), Flash: 0x20000 bytes (128 KiB) in pages of 1024 bytes 2020-05-12T11:59:36 INFO common.c: Attempting to write 3596 (0xe0c) bytes to stm32 address: 134217728 (0x8000000) Flash page at addr: 0x08000c00 erased 2020-05-12T11:59:37 INFO common.c: Finished erasing 4 pages of 1024 (0x400) bytes 2020-05-12T11:59:37 INFO common.c: Starting Flash write for VL/F0/F3/F1_XL core id 2020-05-12T11:59:37 INFO flash_loader.c: Successfully loaded flash loader in sram 4/4 pages written 2020-05-12T11:59:37 INFO common.c: Starting verification of write complete 2020-05-12T11:59:37 INFO common.c: Flash written and verified! jolly good!

serialを動かす

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

cd tigo_ws mkdir serial cd serial

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

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

(3)ビルド/書き込み
以下を実行する:

. ~/setenv4tg tgf103 stf

(4)シリアル通信の起動
以下のように別の端末でシリアル通信を起動すると シリアルに結果が出力される:

$ picocom /dev/ttyACM0 -b115200 picocom v1.7 <省略> Terminal ready hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! ...

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 ... # ここでは例として「tmp102」をインポートする mkdir tmp102 cd tmp102 # モジュールをディレクトリの形でコピーする cp -r $TIGOLIBS/tmp102 . # exampleのmain.goをコピーする cp $TIGOLIBS/examples/tmp102/main.go . # main.goを編集する leafpad main.go # 以下のように修正する: # "tinygo.org/x/drivers/tmp102" → "./tmp102" # dockerを実行環境にする場合、インポートモジュールをカレント・ディレクトリに置く必要がある # 以下、ビルド&書き込み . ~/setenv4tg tgf103 stf

以上で、例としてtmp102のプログラムがビルド&実行できる。
注意:I2Cの接続が、Arduinoのピン配列と異なり 「NUCLEO-F103RB Pin Assign」のピン配置になるので 以下になる:

SCL_PIN = PB6(D10) SDA_PIN = PB7(CN7上) # CN6のマーキングの2つあるGNDの下の方の位置

正常動作であればシリアル出力で気温が出力される。

出力例:

$ picocom /dev/ttyACM0 -b115200 ... Terminal ready 27.12°C 27.12°C ... ... 27.12°C 27.19°C 27.25°C 27.38°C 27.56°C 28.06°C 30.12°C # ←動作確認のためにセンサーを指で触って温度を上げた ..

NUCLEO-F103RBのピン定義

const ( LED = LED_BUILTIN LED_BUILTIN = LED_GREEN LED_GREEN = PA5 ) const ( UART_TX_PIN = PA2 UART_RX_PIN = PA3 UART_ALT_TX_PIN = PD5 UART_ALT_RX_PIN = PD6 ) const ( SCL_PIN = PB6 SDA_PIN = PB7 ) const ( SPI0_SCK_PIN = PA5 SPI0_MISO_PIN = PA6 SPI0_MOSI_PIN = PA7 )

ボードのターゲット名

「-target nucleo-f103rb」を以下にあるターゲット名に変更すると 他のボード用にビルドできる。

主なもののみ(=個人的に興味があるもの)

(1)Adafruit Circuit Playground Bluefruit circuitplay-bluefruit (2)Adafruit Circuit Playground Express circuitplay-express (3)Adafruit Feather M0 feather-m0 (4)Adafruit Feather M4 feather-m4 (5)Adafruit ItsyBitsy M0 itsybitsy-m0 (6)Adafruit ItsyBitsy M4 itsybitsy-m4 (7)Arduino Nano arduino-nano (8)Arduino Nano33 IoT arduino-nano33 (9)Arduino Uno arduino (10)BBC micro:bit microbit (11)SiFIve HiFive1 hifive1b (12)ST Micro "Nucleo F103RB" nucleo-f103rb (13)ST Micro STM32F103XX "Bluepill" bluepill (14)ST Micro STM32F407 "Discovery" stm32f4disco

参考情報

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

NUCLEO-F103RB mbed pinout
NUCLEO-F103RB Pin Assign

STM32F4DISCO Pin Assign
MICROBIT Pin Assign
ARDUINO-NANO Pin Assign
ARDUINO Pin Assign

TinyGo Drivers

以上

続きを読む "Nucleo-F103RBボードでTinyGOを動かす(v2)"

| | コメント (0)