Macの仮想環境管理 venv下でESP32 IDFをインストール

# 概要

[公式ドキュメント](https://esp-idf.readthedocs.io/en/v1.0/macos-setup.html)通りだとPythonのバージョンが違ってエラーが出たので仮想管理環境下でEPS 32 IDFをインストールする。

# 環境

* macOS Sierra 10.12.6
* ESP IDF 3.0-rc1
* ESPr Developer 32

# 手順

1. 仮想環境管理設定 

    ```
$ mkdir -p ~/esp
$ cd ~/esp
$ python3 -m venv -python=/usr/local/bin/python2.7 myvenv
$ source myvenv/bin/activate
(myvenv) ~$ pip install pyserial
Collecting pyserial
  Downloading pyserial-3.4-py2.py3-none-any.whl (193kB)
    100% |████████████████████████████████| 194kB 520kB/s 
Installing collected packages: pyserial
Successfully installed pyserial-3.4
You are using pip version 8.1.1, however version 9.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
```

2. Toolchainインストール
　[Toolchain](https://esp-idf.readthedocs.io/en/latest/get-started/macos-setup.html)は頻繁に更新されますので最新版を参照。

    ```
(myvenv) ~$ tar -xzf ~/Downloads/xtensa-esp32-elf-osx-1.22.0-73-ge28a011-5.2.0.tar.gz
(myvenv) ~$ export PATH=$PATH:$HOME/esp/xtensa-esp32-elf/bin
(myvenv) ~$ echo 'export PATH=$PATH:$HOME/esp/xtensa-esp32-elf/bin' >> ~/.bash_profile
(myvenv) ~$ git clone --recursive https://github.com/espressif/esp-idf.git
(myvenv) ~$ export IDF_PATH=~/esp/esp-idf
(myvenv) ~$ echo 'export IDF_PATH=~/esp/esp-idf' >> ~/.bash_profile
```

3. ESPr Developer 32をMacにUSBで接続
4. サンプルプログラムをコピー

    ```
(myvenv) ~$ cd ~/esp
(myvenv) ~$ cp -r $IDF_PATH/examples/get-started/hello_world .
```

5. Portを調べる
   /dev/cu.usbserial-で始まるのがESPr Developer 32が接続されているポート。

    ```
(myvenv) ~$ ls /dev/cu.*
/dev/cu.Bluetooth-Incoming-Port	/dev/cu.usbserial-DN01EAI5
```


6. ポートを設定

    ```
(myvenv) ~$ cd ~/esp/hello_world
(myvenv) ~$ make menuconfig
```

  1. Serial flasher config > Default serial portと辿り、5.で調べたポートを設定する。 
  2.  終わったらSave。


# 動作確認

1. コンパイル、書き込み

    ```
(myvenv) ~$ make flash
WARNING: Toolchain version is not supported: 1.22.0-73-ge28a011
Expected to see version: 1.22.0-75-gbaf03c2
Please check ESP-IDF setup instructions and update the toolchain, or proceed at your own risk.
WARNING: Toolchain version is not supported: 1.22.0-73-ge28a011
Expected to see version: 1.22.0-75-gbaf03c2
Please check ESP-IDF setup instructions and update the toolchain, or proceed at your own risk.
CC build/app_trace/app_trace.o
CC build/app_trace/app_trace_util.o
 .
 .
 .
Hash of data verified.
Compressed 3072 bytes to 82...
Wrote 3072 bytes (82 compressed) at 0x00008000 in 0.0 seconds (effective 1536.5 kbit/s)...
Hash of data verified.
Leaving...
Hard resetting...
```

2. 動作を確認

    ```
(myvenv) ~$ make monitor
WARNING: Toolchain version is not supported: 1.22.0-73-ge28a011
Expected to see version: 1.22.0-75-gbaf03c2
Please check ESP-IDF setup instructions and update the toolchain, or proceed at your own risk.
MONITOR
--- idf_monitor on /dev/cu.usbserial-DN01EAI5 115200 ---
--- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
ERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
ets Jun  8 2016 0ets Jun  8 2016 00:22:57
rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
ets Jun  8 2016 00:22:57
rst:0x10 (RTCWDT_RTC_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:5728
ho 0 tail 12 room 4
load:0x40078000,len:0
load:0x40078000,len:13804
entry 0x40079030
I (74) boot: Detected ESP32
I (34) boot: ESP-IDF v3.0-dev-1327-g02304ad8 2nd stage bootloader
I (34) boot: compile time 11:54:17
I (34) boot: Enabling RNG early entropy source...
I (40) boot: SPI Speed      : 40MHz
I (44) boot: SPI Mode       : DIO
I (48) boot: SPI Flash Size : 4MB
I (52) boot: Partition Table:
I (56) boot: ## Label            Usage          Type ST Offset   Length
I (63) boot:  0 nvs              WiFi data        01 02 00009000 00006000
I (71) boot:  1 phy_init         RF data          01 01 0000f000 00001000
I (78) boot:  2 factory          factory app      00 00 00010000 00100000
I (85) boot: End of partition table
I (90) esp_image: segment 0: paddr=0x00010020 vaddr=0x3f400020 size=0x04bdc ( 19420) map
I (105) esp_image: segment 1: paddr=0x00014c04 vaddr=0x3ffb0000 size=0x02138 (  8504) load
I (111) esp_image: segment 2: paddr=0x00016d44 vaddr=0x40080000 size=0x00400 (  1024) load
0x40080000: _iram_start at /Users/htokuno/esp/esp-idf/components/freertos/./xtensa_vectors.S:1685
I (117) esp_image: segment 3: paddr=0x0001714c vaddr=0x40080400 size=0x081fc ( 33276) load
I (139) esp_image: segment 4: paddr=0x0001f350 vaddr=0x400c0000 size=0x00000 (     0) load
I (139) esp_image: segment 5: paddr=0x0001f358 vaddr=0x00000000 size=0x00cb8 (  3256) 
I (147) esp_image: segment 6: paddr=0x00020018 vaddr=0x400d0018 size=0x0fce0 ( 64736) map
0x400d0018: _stext at ??:?
I (182) boot: Loaded app from partition at offset 0x10000
I (182) boot: Disabling RNG early entropy source...
I (182) cpu_start: Pro cpu up.
I (186) cpu_start: Starting app cpu, entry point is 0x40080df4
0x40080df4: call_start_cpu1 at /Users/htokuno/esp/esp-idf/components/esp32/./cpu_start.c:222
I (0) cpu_start: App cpu up.
I (197) heap_init: Initializing. RAM available for dynamic allocation:
I (203) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (209) heap_init: At 3FFB2940 len 0002D6C0 (181 KiB): DRAM
I (216) heap_init: At 3FFE0440 len 00003BC0 (14 KiB): D/IRAM
I (222) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (228) heap_init: At 400885FC len 00017A04 (94 KiB): IRAM
I (235) cpu_start: Pro cpu start user code
I (252) cpu_start: Starting scheduler on PRO CPU.
I (0) cpu_start: Starting scheduler on APP CPU.
Hello world!
This is ESP32 chip with 2 CPU cores, WiFi/BT/BLE, silicon revision 0, 4MB external flash
Restarting in 10 seconds...
Restarting in 9 seconds...
Restarting in 8 seconds...
Restarting in 7 seconds...
Restarting in 6 seconds...
Restarting in 5 seconds...
Restarting in 4 seconds...
Restarting in 3 seconds...
Restarting in 2 seconds...
Restarting in 1 seconds...
Restarting in 0 seconds...
Restarting now.
```

3. monitorを止めるにはCtrl+]を押す。