Raspberry Pi に Teensy をつなげて platformio でプログラム

今回は、RaspberryPiにplatformioをインストールして、TeensyにUSB接続してあれこれします。

# platformio

CUIで動作する、ArduinoIDEのようなもの。ターゲットボードはAVR/ARM/ESP系、その他多数。GUIもあるらしい。


詳細はこちら
http://monoist.atmarkit.co.jp/mn/articles/1610/31/news005.html

母艦で動作するCUI環境は、python2.7 ベースで動作するらしいが Python3 では動かないようです。残念！！

# Teensy

https://www.pjrc.com/teensy/

以下はTeensy3.2のスペックです。
- ARM Cortex-M4　72 MHz
- フラッシュ256KBytes、RAM64KBytes、EEPROM2KBytes
- 小さい！
- USBインタフェースが使えます。
- 5Vトレラントで使いやすい


SwitchScienceでも販売しています。

https://www.switch-science.com/catalog/2447/


# インストール

raspberry piにログオンする

```

pi@raspberrypi:~ $ python --version
Python 2.7.9
pi@raspberrypi:~ $ pip install -U platformio
-bash: pip: コマンドが見つかりません

```
そういや、いつも Python3 環境で動かしていたため pip も2.7ではインストールしてなかったんだった。

```

pi@raspberrypi:~ $ sudo apt-get install python-pip
```
としてインストール。
pipがインストールできたら pipからplatformioをインストール

```

$ pip install -U platformio
Downloading/unpacking platformio
  Downloading platformio-3.4.0-py27-none-any.whl (132kB): 132kB downloaded
Downloading/unpacking arrow<1 (from platformio)
  Downloading arrow-0.10.0.tar.gz (86kB): 86kB downloaded
  Running setup.py (path:/tmp/pip-build-BXquak/arrow/setup.py) egg_info for package arrow
    
Downloading/unpacking lockfile>=0.9.1,<0.13 (from platformio)
  Downloading lockfile-0.12.2-py2.py3-none-any.whl
Downloading/unpacking click>=5,<6 (from platformio)
  Downloading click-5.1-py2.py3-none-any.whl (65kB): 65kB downloaded
Downloading/unpacking bottle<0.13 (from platformio)
  Downloading bottle-0.12.13.tar.gz (70kB): 70kB downloaded
.
.
.
Cleaning up...
Exception:
Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/pip/basecommand.py", line 122, in main
    status = self.run(options, args)
  File "/usr/lib/python2.7/dist-packages/pip/commands/install.py", line 295, in run
    requirement_set.install(install_options, global_options, root=options.root_path)
  File "/usr/lib/python2.7/dist-packages/pip/req.py", line 1436, in install
    requirement.install(install_options, global_options, *args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/pip/req.py", line 672, in install
    self.move_wheel_files(self.source_dir, root=root)
  File "/usr/lib/python2.7/dist-packages/pip/req.py", line 902, in move_wheel_files
    pycompile=self.pycompile,
  File "/usr/lib/python2.7/dist-packages/pip/wheel.py", line 214, in move_wheel_files
    clobber(source, lib_dir, True)
  File "/usr/lib/python2.7/dist-packages/pip/wheel.py", line 204, in clobber
    os.makedirs(destdir)
  File "/usr/lib/python2.7/os.py", line 157, in makedirs
    mkdir(name, mode)
OSError: [Errno 13] Permission denied: '/usr/local/lib/python2.7/dist-packages/platformio-3.4.0.dist-info'

Storing debug log for failure in /home/pi/.pip/pip.log
```

そうだった。sudoつけなきゃ。

```
pi@raspberrypi:~ $ sudo pip install -U platformio
Downloading/unpacking platformio
  Downloading platformio-3.4.0-py27-none-any.whl (132kB): 132kB downloaded
Downloading/unpacking arrow<1 (from platformio)
  Downloading arrow-0.10.0.tar.gz (86kB): 86kB downloaded
  Running setup.py (path:/tmp/pip-build-90F4aH/arrow/setup.py) egg_info for package arrow
.
.
.
  Found existing installation: six 1.8.0
    Not uninstalling six at /usr/lib/python2.7/dist-packages, owned by OS
Successfully installed platformio arrow lockfile click bottle pyserial colorama requests semantic-version python-dateutil certifi chardet urllib3 idna six
Cleaning up...
pi@raspberrypi:~ $
```
うまくいった。はて、個別のボードの環境はどうやって設定するんだろう。

```

pi@raspberrypi:~ $ platformio boards | grep teensy
Platform: teensy
teensy20              ATMEGA32U4     16Mhz     31kB    2.5kB  Teensy 2.0
teensy30              MK20DX128      48Mhz     128kB   16kB   Teensy 3.0
teensy31              MK20DX256      72Mhz     256kB   64kB   Teensy 3.1 / 3.2
teensy35              MK64FX512      120Mhz    512kB   192kB  Teensy 3.5
teensy36              MK66FX1M0      180Mhz    1024kB  256kB  Teensy 3.6
teensylc              MKL26Z64       48Mhz     62kB    8kB    Teensy LC
teensy20pp            AT90USB1286    16Mhz     127kB   8kB    Teensy++ 2.0

```
うん、何か設定は入っているようだ。

某プロジェクトをダウンロードしてコンパイルしてみる。

```

pi@raspberrypi:~ $ unzip hogehoge-master.zip 
```
としてから

```

pi@raspberrypi:~ $ cd hogehoge-master/
pi@raspberrypi:~/hogehoge-master $ ls
README.md  lib  platformio.ini  src
pi@raspberrypi:~/hogehoge-master $ platformio run

```

とするとteensyのための環境など自動でダウンロードして進めてくれる。

```

[Fri Jul 28 01:01:52 2017] Processing teensy31 (platform: teensy; build_flags: "-std=gnu11"; board: teensy31; framework: arduino)
-------------------------------------------------------------------------------------------------------------------------------------------------------
PlatformManager: Installing teensy
Downloading  [####################################]  100%
Unpacking  [####################################]  100%
teensy @ 2.2.0 has been successfully installed!
The platform 'teensy' has been successfully installed!
The rest of packages will be installed automatically depending on your build environment.
.
.
.
Indexing .pioenvs/teensy31/lib/libEncorders.a
Linking .pioenvs/teensy31/firmware.elf
Calculating size .pioenvs/teensy31/firmware.elf
Building .pioenvs/teensy31/firmware.hex
text	   data	    bss	    dec	    hex	filename
24980	      0	   2588	  27568	   6bb0	.pioenvs/teensy31/firmware.elf
============================================================ [SUCCESS] Took 276.43 seconds ============================================================

```
できた。でもiniファイルによってアップロードする設定にはなっていない。
改めてアップロードを明示的に指定してやり直してみる。

```
$ platformio run --target upload
[Fri Jul 28 01:19:01 2017] Processing teensy31 (platform: teensy; build_flags: "-std=gnu11"; board: teensy31; framework: arduino)
-------------------------------------------------------------------------------------------------------------------------------------------------------
PackageManager: Installing tool-teensy @ ~1.21.0
Downloading  [####################################]  100%
Unpacking  [####################################]  100%
Verbose mode can be enabled via `-v, --verbose` option
Converting sketch.ino
Collected 94 compatible libraries
Looking for dependencies...
Library Dependency Graph
|-- <Servo> v1.1.2
|-- <SparkFun VL6180 Sensor> v1.1.0
|   |-- <MultiI2C>
|   |   |-- <i2c_t3>
|-- <Encorders>
Compiling .pioenvs/teensy31/src/sketch.ino.o
In file included from lib/VL6180X/src/SparkFun_VL6180X.h:41:0,
from src/Sensors.h:1,
from /home/pi/chargeroboV4_test-master/src/sketch.ino:4:
lib/MultiI2C/multi_i2c.h:17:17: warning: passing NULL to non-pointer argument 1 of 'i2c_t3::i2c_t3(uint8_t)' [-Wconversion-null]
i2c_t3 hard = NULL;
^
Linking .pioenvs/teensy31/firmware.elf
Checking program size
text	   data	    bss	    dec	    hex	filename
24980	      0	   2588	  27568	   6bb0	.pioenvs/teensy31/firmware.elf
Building .pioenvs/teensy31/firmware.hex
Uploading .pioenvs/teensy31/firmware.hex
Teensy Loader, Command Line, Version 2.1
Read ".pioenvs/teensy31/firmware.hex": 24980 bytes, 9.5% usage
Unable to claim interface, check USB permissions
Error opening USB device: could not claim interface 0: Operation not permitted
Waiting for Teensy device...
(hint: press the reset button)
Unable to claim interface, check USB permissions
Unable to claim interface, check USB permissions
Unable to claim interface, check USB permissions
Unable to claim interface, check USB permissions
Unable to claim interface, check USB permissions
Unable to claim interface, check USB permissions
```

アップロードできない感じ。一旦Ctrl+Cで終了してから sudo でやり直してみる

```

~/hogehoge-master $ sudo platformio run --target upload
*******************************************************************************************************************************************************
If you like PlatformIO, please:
- follow us on Twitter to stay up-to-date on the latest project news > https://twitter.com/PlatformIO_Org
- star it on GitHub > https://github.com/platformio/platformio
- try PlatformIO IDE for IoT development > http://platformio.org/platformio-ide
- support us with PlatformIO Plus > https://pioplus.com
*******************************************************************************************************************************************************

[Fri Jul 28 01:28:54 2017] Processing teensy31 (platform: teensy; build_flags: "-std=gnu11"; board: teensy31; framework: arduino)
-------------------------------------------------------------------------------------------------------------------------------------------------------
PlatformManager: Installing teensy
Downloading  [####################################]  100%
Unpacking  [####################################]  100%
teensy @ 2.2.0 has been successfully installed!
The platform 'teensy' has been successfully installed!
The rest of packages will be installed automatically depending on your build environment.
PackageManager: Installing framework-arduinoteensy @ ~1.136.0
Downloading  [####################################]  100%          
Unpacking  [####################################]  100%          
PackageManager: Installing tool-teensy @ ~1.21.0
Downloading  [####################################]  100%
Unpacking  [####################################]  100%
PackageManager: Installing toolchain-gccarmnoneeabi @ >=1.40803.0,<1.40805.0
Downloading  [####################################]  100%          
Unpacking  [####################################]  100%          
CorePackageManager: Installing tool-scons @ ~3.20501.2
Downloading  [####################################]  100%          
Unpacking  [####################################]  100%
tool-scons @ 3.20501.2 has been successfully installed!
Verbose mode can be enabled via `-v, --verbose` option
Converting sketch.ino
Collected 94 compatible libraries
Looking for dependencies...
Library Dependency Graph
|-- <Encorders>
|-- <SparkFun VL6180 Sensor> v1.1.0
|   |-- <MultiI2C>
|   |   |-- <i2c_t3>
|-- <Servo> v1.1.2
Compiling .pioenvs/teensy31/src/sketch.ino.o
In file included from lib/VL6180X/src/SparkFun_VL6180X.h:41:0,
from src/Sensors.h:1,
from /home/pi/chargeroboV4_test-master/src/sketch.ino:4:
lib/MultiI2C/multi_i2c.h:17:17: warning: passing NULL to non-pointer argument 1 of 'i2c_t3::i2c_t3(uint8_t)' [-Wconversion-null]
i2c_t3 hard = NULL;
^
Linking .pioenvs/teensy31/firmware.elf
Checking program size
text	   data	    bss	    dec	    hex	filename
24980	      0	   2584	  27564	   6bac	.pioenvs/teensy31/firmware.elf
Building .pioenvs/teensy31/firmware.hex
Uploading .pioenvs/teensy31/firmware.hex
Teensy Loader, Command Line, Version 2.1
Read ".pioenvs/teensy31/firmware.hex": 24980 bytes, 9.5% usage
Found HalfKay Bootloader
Programming.........................
Booting
============================================================ [SUCCESS] Took 209.45 seconds ============================================================
```

できた。