Circuit-Playground-Express

2020年5月29日 (金)

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月28日 (木)

プログラミング言語RustをCircuit-Playground-Expressで動かす

2020/5/30:
書き込みツール(bossac)の説明を追加した。

2020/5/27:
初版

Rust Circuit-Playground-Express

Rust Circuit-Playground-Express

概要

プログラミング言語Rustを以下のCircuit-Playground-Expressで動かす。 (ホストPCとしてはubuntuを想定している)

Circuit-Playground-Express

関連ツールのインストール

以下で必要なツールをインスールする:
(1)rustツールのインストール

curl https://sh.rustup.rs -sSf | sh

(2)Cコンパイラのバージョンアップ
binを作成するのに、これに含まれるツールを使用するのでインストールする

以下でインストールしたコンパイラが古くてビルドエラーが出たのでバージョンアップする。 sudo apt-get install gcc-arm-none-eabi (1)以下のurlから最新版をダウンロードする: https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/gnu-rm/downloads 解凍したものを以下のフォルダに置く: $HOME/Downloads/gcc-arm-none-eabi-8-2019-q3-update (2)パス設定 #古いコンパイラを削除する sudo apt-get remove gcc-arm-none-eabi #パスを設定する export PATH=$PATH:$HOME/Downloads/gcc-arm-none-eabi-8-2019-q3-update/bin

exportは、.bashrcに登録する

(3)picocom(通信ソフト)のインストール

sudo apt-get install picocom

(4)rust関連ツールのインストール

# for samd11, samd21: rustup target add thumbv6m-none-eabi # for samd51, same54: rustup target add thumbv7em-none-eabihf # uf2conv etc install rustup component add llvm-tools-preview cargo install uf2conv cargo-binutils

blinkyを実行する

# download git clone https://github.com/atsamd-rs/atsamd.git cd atsamd/boards/circuit_playground_express # build cargo build --example blinky_basic # conv elf to bin arm-none-eabi-objcopy -O binary target/thumbv6m-none-eabi/debug/examples/blinky_basic firmware.bin # conv bin to uf2 uf2conv firmware.bin --base 0x2000 --output firmware.uf2 # flash # bootloader-modeにする(resetを2度押す) cp firmware.uf2 /media/USER_NAME/CPLAYBOOT/

「USER_NAME」は、自分の環境に合わせて変更すること。 実行結果として、オンボードのLEDが点滅する。

bossacによる書き込み

uf2ファイルに変換せずにbinのまま書き込みができるbossacというツールがあるので それの使い方について説明する。
(1)bossacツールのインストール

sudo apt-get install build-essential git clone https://github.com/shumatech/BOSSA/tree/arduino cd BOSSA make bin/bossac -j4 sudo cp bin/bossac /bin/

(2)bossacによる書き込み

... # conv elf to bin arm-none-eabi-objcopy -O binary target/thumbv6m-none-eabi/debug/examples/blinky_basic firmware.bin #バイナリを作るところまでは同じ # flash # bootloader-modeにする(resetを2度押す) bossac -p /dev/ttyACM0 -i -e -w -v -R -o 0x2000 firmware.bin

出力例:

$ bossac -p /dev/ttyACM0 -i -e -w -v -R -o 0x2000 firmware.bin Device : ATSAMD21x18 Version : v1.1 [Arduino:XYZ] May 17 2020 17:56:16 Address : 0x0 Pages : 4096 Page Size : 64 bytes Total Size : 256KB Planes : 1 Lock Regions : 16 Locked : none Security : false BOD : true BOR : true Erase flash Done in 0.756 seconds Write 13392 bytes to flash (210 pages) [==============================] 100% (210/210 pages) Done in 0.085 seconds Verify 13392 bytes of flash [==============================] 100% (210/210 pages) Verify successful Done in 0.084 seconds

参照情報

atsamd & atsame support for Rust
Running bossac on the command line

STM32 Nucleo Board STM32F103RB

The Embedded Rust Book
The Embedded Rust Book - Semihosting

UbuntuにRustをインストールする

Adding a new board

以上

続きを読む "プログラミング言語RustをCircuit-Playground-Expressで動かす"

| | コメント (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)

2020年5月25日 (月)

開発ツールPlatformIOをCircuit-Playground-Expressで使う(arduino版)

2020/5/25

PlatformIO cli Circuit-Playground-Express

PlatformIO cli Circuit-Playground-Express

概要

開発ツールPlatformIOを以下のCircuit-Playground-Expressで使う(arduino版)。
VisualCodeのプラグインとしてPlatformIOを使用することができるが、ここでは、cliとしての使い方について記する。
(ホストPCとしてはubuntuを想定している)

Circuit Playground Express

PlatformIOのインストール

python3 -m venv pio_env source pio_env/bin/activate pip3 install platformio インストール後も、本ツールを使用する場合 同じディレクトリで以下を実行する: source pio_env/bin/activate # 「source」は、「.」でも良い

準備

以下を実行して、udevのrulesを登録する:

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

テスト用プロジェクトを作成/実行する

#ターゲットボードのtarget名を検索する # (ここでは circuitplayground を検索する) $ pio boards | grep circuitplayground #出力例: adafruit_circuitplayground_m0 SAMD21G18A 48MHz 256KB 32KB Adafruit Circuit Playground Express #target名として「adafruit_circuitplayground_m0」が判明した # プロジェクトdemoのディレクトリを作成する mkdir demo cd demo # 以下を実行して必要なファイルを作成する pio init --board adafruit_circuitplayground_m0 nano platformio.ini # platformio.iniを以下のように編集する:
; PlatformIO Project Configuration File ; ; Build options: build flags, source filter ; Upload options: custom upload port, speed and extra flags ; Library options: dependencies, extra library storages ; Advanced options: extra scripting ; ; Please visit documentation for the other options and examples ; https://docs.platformio.org/page/projectconf.html [env:adafruit_circuitplayground_m0] platform = atmelsam board = adafruit_circuitplayground_m0 framework = arduino upload_protocol = sam-ba

続き:

#必要なライブラリーを用意する: pio lib install "Adafruit Circuit Playground"

続き:

# テスト用のdemo.inoを作成する nano src/demo.ino 以下の内容に編集する:
// Demo program for testing library and board - flip the switch to turn on/off buzzer #include <Adafruit_CircuitPlayground.h> // we light one pixel at a time, this is our counter uint8_t pixeln = 0; void setup() { Serial.begin(115200); Serial.println("Circuit Playground test!"); CircuitPlayground.begin(); } void loop() { // turn off speaker when not in use CircuitPlayground.speaker.enable(false); // test Red #13 LED CircuitPlayground.redLED(HIGH); delay(100); CircuitPlayground.redLED(LOW); /************* TEST CAPTOUCH */ Serial.print("Capsense #3: "); Serial.println(CircuitPlayground.readCap(3)); Serial.print("Capsense #2: "); Serial.println(CircuitPlayground.readCap(2)); Serial.print("Capsense #0: "); Serial.println(CircuitPlayground.readCap(0)); Serial.print("Capsense #1: "); Serial.println(CircuitPlayground.readCap(1)); Serial.print("Capsense #12: "); Serial.println(CircuitPlayground.readCap(12)); Serial.print("Capsense #6: "); Serial.println(CircuitPlayground.readCap(6)); Serial.print("Capsense #9: "); Serial.println(CircuitPlayground.readCap(9)); Serial.print("Capsense #10: "); Serial.println(CircuitPlayground.readCap(10)); delay(10); /************* TEST SLIDE SWITCH */ if (CircuitPlayground.slideSwitch()) { Serial.println("Slide to the left"); } else { Serial.println("Slide to the right"); CircuitPlayground.speaker.enable(true); CircuitPlayground.playTone(500 + pixeln * 500, 100); } delay(10); /************* TEST 10 NEOPIXELS */ CircuitPlayground.setPixelColor(pixeln++, CircuitPlayground.colorWheel(25 * pixeln)); if (pixeln == 11) { pixeln = 0; CircuitPlayground.clearPixels(); } delay(10); /************* TEST BOTH BUTTONS */ if (CircuitPlayground.leftButton()) { Serial.println("Left button pressed!"); } if (CircuitPlayground.rightButton()) { Serial.println("Right button pressed!"); } delay(10); /************* TEST LIGHT SENSOR */ Serial.print("Light sensor: "); Serial.println(CircuitPlayground.lightSensor()); delay(10); /************* TEST SOUND SENSOR */ Serial.print("Sound sensor: "); Serial.println(CircuitPlayground.mic.soundPressureLevel(10)); delay(10); /************* TEST ACCEL */ // Display the results (acceleration is measured in m/s*s) Serial.print("X: "); Serial.print(CircuitPlayground.motionX()); Serial.print(" \tY: "); Serial.print(CircuitPlayground.motionY()); Serial.print(" \tZ: "); Serial.print(CircuitPlayground.motionZ()); Serial.println(" m/s^2"); delay(10); /************* TEST THERMISTOR */ Serial.print("Temperature "); Serial.print(CircuitPlayground.temperature()); Serial.println(" *C"); Serial.print("==========================="); delay(1000); }

続き:

# build pio run # ボードをホストPCに接続する # build&upload(flash) pio run -t upload # buildしないで書き込む場合は以下を実行する: pio run -t nobuild -t upload -v # -v は、詳細を表示するオプション # 以上で、基本的な操作としては完了となる

書き込み後、ボード上のセンサー、スイッチ、LEDを使ったデモが起動する。
センサーの値などはシリアル出力されるので「picocom /dev/ttyACM0 -b115200」を実行して表示する。

参考情報

Circuit Playground Express

All in one library to control Adafruit's Circuit Playground

Adafruit Circuit Playground Express - PINOUT
Adafruit Circuit Playground Express - Overview

PlatformIO Core (CLI)

以上

続きを読む "開発ツールPlatformIOをCircuit-Playground-Expressで使う(arduino版)"

| | コメント (0)

2020年5月23日 (土)

Circuit-Playground-ExpressにCircuitPythonをインストールする

2020/5/22+

Circuit-Playground-Express CircuitPython Install

Circuit-Playground-Express CircuitPython Install

概要

Circuit-Playground-ExpressにCircuitPythonをインストールする方法について記載する。 以下、Circuit-Playground-ExpressをCPXとする。 (ホストPCとしてはubuntuを想定している)

事前準備

(1)picocomのインストール

sudo apt-get install picocom

bootloader mode

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

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

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

bootloader update

以下の手順でbootloaderをアップデートする:

# download wget https://github.com/adafruit/uf2-samdx1/releases/download/v3.10.0/update-bootloader-circuitplay_m0-v3.10.0.uf2 # CPXをホストPCにUSB接続して、リセットを2度押して、bootloader-modeに入る。 # このモードに入ると、CPLAYBOOTのディレクトリが現れる。 # ファイルをドラッグ&ドロップするか、以下のコマンドを実行してファイルをコピーする cp update-bootloader-circuitplay_m0-v3.10.0.uf2 /media/USER_NAME/CPLAYBOOT/ # ファームウェアの書き込みが実行されて、完了後、CPLAYBOOTが現れるまで待つ

「USER_NAME」は実際の環境に合わせること。

CircuitPythonの書き込み

以下の手順でCircuitPythonを書き込む:

wget https://downloads.circuitpython.org/bin/circuitplayground_express/en_US/adafruit-circuitpython-circuitplayground_express-en_US-5.3.0.uf2 # ファイルをドラッグ&ドロップするか、以下のコマンドを実行してファイルをコピーする cp adafruit-circuitpython-circuitplayground_express-en_US-5.3.0.uf2 /media/USER_NAME/CPLAYBOOT/ # ファームウェアの書き込みが実行されて、完了後、CIRCUITPYが現れるまで待つ

「USER_NAME」は実際の環境に合わせること。

動作確認(REPL)

以上、CircuitPythonのインストールが終わったので、picocomを使いCPXとシリアルで通信する。(REPL)

以下、通信(REPL)例:

$ picocom /dev/ttyACM0 -b115200 Adafruit CircuitPython 5.3.0 on 2020-04-29; Adafruit CircuitPlayground Express with samd21g18 >>> import os >>> os.uname() (sysname='samd21', nodename='samd21', release='5.3.0', version='5.3.0 on 2020-04-29', machine='Adafruit CircuitPlayground Express with samd21g18') >>> >>> import gc >>> gc.collect() >>> gc.mem_free() 20816 >>> >>> list(5 * x + y for x in range(10) for y in [4, 2, 1]) [4, 2, 1, 9, 7, 6, 14, 12, 11, 19, 17, 16, 24, 22, 21, 29, 27, 26, 34, 32, 31, 39, 37, 36, 44, 42, 41, 49, 47, 46] >>> >>> >>> help('modules') __main__ adafruit_hid/gamepad builtins random _os adafruit_hid/keyboard busio re _pixelbuf adafruit_hid/keyboard_layout_us collections rotaryio _time adafruit_hid/keycode digitalio rtc adafruit_bus_device/__init__ adafruit_hid/mouse errno storage adafruit_bus_device/i2c_device adafruit_lis3dh gamepad struct adafruit_bus_device/spi_device adafruit_thermistor gc supervisor adafruit_circuitplayground/__init__ analogio math sys adafruit_circuitplayground/bluefruit array microcontroller time adafruit_circuitplayground/circuit_playground_base audiobusio micropython touchio adafruit_circuitplayground/express audiocore neopixel usb_hid adafruit_hid/__init__ audioio neopixel_write usb_midi adafruit_hid/consumer_control bitbangio os adafruit_hid/consumer_control_code board pulseio Plus any modul >>>

mpy library install

以下の手順でmpyライブラリをインストールする:

wget https://github.com/adafruit/Adafruit_CircuitPython_Bundle/releases/download/20200522/adafruit-circuitpython-bundle-5.x-mpy-20200522.zip unzip adafruit-circuitpython-bundle-5.x-mpy-20200522.zip cd adafruit-circuitpython-bundle-5.x-mpy-20200522 # ドラッグ&ドロップか、以下を実行して、libの内容をCIRCUITPY/libにコピーする cp -r lib /media/USER_NAME/CIRCUITCPY/lib # 実際に使用するライブラリだけが必要になるが、容量に余裕があるので、全部コピーする # 将来、容量が不足した不要なライブラリを削除すること

「USER_NAME」は実際の環境に合わせること。

sample#1(neopixelデモ)

以下にあるサンプルを動かしてみる:
CircuitPython NeoPixel

以下の手順でサンプルをダウンロードとする:

# dewnload example wget https://raw.githubusercontent.com/adafruit/Adafruit_Learning_System_Guides/master/Introducing_CircuitPlaygroundExpress/CircuitPlaygroundExpress_NeoPixel.py cp CircuitPlaygroundExpress_NeoPixel.py CIRCUITPY/ # resetで自動起動させる場合は以下を実行する: cp CircuitPlaygroundExpress_NeoPixel.py CIRCUITPY/code.py

「CIRCUITPY」は「/media/USER_NAME/CIRCUITPY/」に読み換えること。
実行すると、ボード上のneopixelが色を変えて光る。

sample#2(ドラムマシン)

以下にあるプログラムを実行してみる:
Playground Drum Machine

以下の手順でサンプルをダウンロードとする:

# プログラムで使用するwavファイルをダウンロードする mkdir wav cd wav wget https://cdn-learn.adafruit.com/assets/assets/000/047/298/original/drumSamples.zip unzip drumSamples.zip cp *.wav CIRCUITPY/ # dewnload example wget https://raw.githubusercontent.com/adafruit/Adafruit_Learning_System_Guides/master/Introducing_CircuitPlaygroundExpress/CircuitPlaygroundExpress_808_Drum_Machine.py cp CircuitPlaygroundExpress_808_Drum_Machine.py CIRCUITPY/ # resetで自動起動させる場合は以下を実行する: cp CircuitPlaygroundExpress_808_Drum_Machine.py CIRCUITPY/code.py

「CIRCUITPY」は「/media/USER_NAME/CIRCUITPY/」に読み換えること。
実行すると、ボード上のワニ口用パッドを触るとドラム音が出る。(サイトの説明では、ワニ口クリップとコインでタッチパッドを作っているが、そのまま触っても音が鳴る)

Performance Test

performanceCircuitPython.py

# Peformace Test for CircuitPython from time import monotonic_ns def performanceTest(): endTime = monotonic_ns() + 10000000000 # 10 sec count = 0 while monotonic_ns() < endTime: count += 1 print("Count: ", count) performanceTest()

実行すると以下のような結果が得られる:
Count: 121800

参照URL

Circuit Playground Express

Adafruit Circuit Playground Express - PINOUT
Adafruit Circuit Playground Express - Overview

Build CircuitPython
Adding Frozen Modules

Adafruit CircuitPython API Reference(v5.x)

CircuitPython Essentials
Example Code

以上

続きを読む "Circuit-Playground-ExpressにCircuitPythonをインストールする"

| | コメント (0)