LoginSignup
8
2

More than 3 years have passed since last update.

MacOS CatalinaでESP-IDFの開発環境構築およびC++でのビルド手順

Last updated at Posted at 2019-11-12

この記事では、ESP-IDFの環境構築手順とC++でのビルド方法について説明します。

2021/2/3 追記:
現在は手順が簡略化されているようですので、これからトライする人は以下URLを参照してください。
https://github.com/tasanakorn/homebrew-esp32

用意したもの

  • ESP32ボード
  • Macbook Pro (Catalina 10.15.1)
  • マイクロUSBケーブル

python3にする

この手順は不要かもしれませんが、私のPCはデフォルトがpython2だったので3にします。
(pyenvを使えばいつでもpython2に戻せます)

$ python --version
Python 2.7.16
$ brew install pyenv
$ pyenv -v
pyenv 1.2.15
$ echo 'export PATH=$PATH:$HOME/.pyenv/shims' >> ~/.bash_profile
$ echo 'eval "$(pyenv init -)"' >> ~/.bash_profile
$ source ~/.bash_profile
$ pyenv install 3.8.0
$ pyenv global 3.8.0
$ python --version
Python 3.8.0
$ pip install --upgrade pip

pyserialが必要なのでいれます。

$ pip install pyserial

esp-idfをインストール

$ mkdir ~/esp
$ cd ~/esp
$ git clone https://github.com/espressif/esp-idf # a45e9985344575a80acb1dc9c12e7bec4d8af401
$ cd esp-idf
$ git submodule init
$ git submodule update
$ cd ../
$ curl -O https://dl.espressif.com/dl/xtensa-esp32-elf-osx-1.22.0-73-ge28a011-5.2.0.tar.gz
$ tar -xzf xtensa-esp32-elf-osx-1.22.0-80-g6c4433a-5.2.0.tar.gz
$ echo 'export PATH=$HOME/esp/xtensa-esp32-elf/bin:$PATH' >> ~/.bash_profile
$ echo 'export IDF_PATH=~/esp/esp-idf' >> ~/.bash_profile
$ source ~/.bash_profile
$ python -m pip install --user -r $IDF_PATH/requirements.txt

ビルド&書き込み

まずはサンプルプロジェクトのhello_world(C言語)を練習台にビルドします。
あらかじめESP32をUSBケーブルでMacと繋いでください。


$ cd ~/esp
$ cp -r esp-idf/examples/get-started/hello_world .
$ cd hello_world
$ make menuconfig
メニュー画面が表示されるのでDefault Serial Portを/dev/tty.SLAB_USBtoUARTのように設定する。
$ make
$ make flash

動作確認は以下コマンド

$ screen /dev/tty.SLAB_USBtoUART 115200
control + a + k > y で終了

hello worldの表示と再起動を繰り返すプログラムのようです。

cmakeでビルド

上記では予め用意されているMakefileを使いましたが、C++版でビルドしたいときなどMakefileを作り直す必要があるため、同じC言語のプログラムをcmakeでもビルドできることを確認しておきます。
なお、この方法ではシリアルポートは自動検索されるためmake menuconfigでポート指定する必要はありません。

$ cmake --version
cmake version 3.15.4
$ cd ~/esp/hello_world
$ mkdir bin
$ cd bin
$ cmake ..
$ make
$ make flash

make flashで下記のようなエラーがでる場合、pythonのバージョンが影響しているかもしれません。私のPCではpython2.7からpython3.8に変更したら解決しました。

serial.serialutil.SerialException: [Errno 16] could not open port /dev/cu.usbserial-0001: [Errno 16] Resource busy: '/dev/cu.usbserial-0001'

screenコマンドで上記と同じ動作をすることを確認してください。

C++コンパイラでビルドする

ようやくC++でビルドをします。

C++ファイル作成

hello_world.cの拡張子をcppに変更し、内容を以下に書き換えます。

#include <iostream>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"

extern "C" void app_main(void)
{
    for (;;)
    {
        vTaskDelay(pdMS_TO_TICKS(1000));
        std::cout << "hello world" << std::endl;
    }
}

cmakeファイル編集

~/esp/hello_world/CMakeLists.txtに以下を追記します。

set(CMAKE_CXX_STANDARD 11)
set(CONFIG_COMPILER_CXX_EXCEPTIONS ON)
idf_build_set_property(CXX_COMPILE_OPTIONS "-std=gnu++11")

hello_world/main/CMakeLists.txtのhello_world_main.cの拡張子をcppに変更します

idf_component_register(SRCS "hello_world_main.cpp"
                    INCLUDE_DIRS "")

ビルド

以上でC++版のビルドができるようになったはずです。

$ mkdir bin
$ cd bin
$ cmake ..
$ make
$ make flash

screenコマンドで動作確認してください。
hello worldが1秒おきに表示されます。

参考記事

勝手ながら以下の記事を参照させていただきました。

8
2
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
2