(3) 新しいプロジェクトの作成

今回は前回使用した ”hello_world” を自分で新しいプロジェクトとして作成して行きます。

”idf.py create-project”

ESP-IDFで新しいプロジェクトを作成するコマンドは、”idf.py create-project” です。モニターを上げて先ずは、”idf.py create-project –help” でヘルプを表示。


@G500:~/esp$ idf.py create-project --help
Usage: idf.py create-project [OPTIONS] NAME

  Create a new project with the name NAME specified as argument. For example: `idf.py
  create-project new_proj` will create a new project in subdirectory called
  `new_proj` of the current working directory. For specifying the new project's path,
  use either the option --path for specifying the destination directory, or the
  global option -C if the project should be created as a subdirectory of the
  specified directory. If the target path does not exist it will be created. If the
  target folder is not empty then the operation will fail with return code 3. If the
  target path is not a folder, the script will fail with return code 4. After the
  execution idf.py terminates so this operation should be used alone.

Options:
  -C, --project-dir PATH  Project directory.
  -p, --path TEXT         Set the path for the new project. The project will be
                          created directly in the given folder if it does not contain
                          anything
  --help                  Show this message and exit.
@G500:~/esp$ 

  • idf.py create-project new_proj” を実行すると ”new_proj” というフォルダーがサブディレクトリーに出来る。
  • 作成するパスを指定したければ、オプションの ”–path” 又は ”−C” を使用
  • −C −p オプションの説明。

”idf.py create-project hello_world” と実行すれば、”hello_world”プロジェクトが作成される様です。サンプルで ”hello_world”は、”~/esp/” の下に有ったので、

  1. モニターを上げて ”~/esp/” に移動。
  2. 前回使用した ” hello_world” ホルダーが有ればそれを削除。
  3. ESP-IDFのパスを通す。
  4. ”idf.py create-project hello_world” を実行

これで ”~/esp/” の下に ”hello_world” プロジェクトが出来ます。


@G500:~/esp$ idf.py create-project hello_world
Executing action: create-project
The project was created in /home/kita_note/esp/hello_world
@G500:~/esp$ 

”hello_world”の構造は以下の様になっていました。


~/esp/
    --hello_world/
       --CMakeList.txt
       --main/
             --hello_world.c
             --CMakeList.txt

ディレクトリの中身を見ると

  • CMakeList.txt:
    • 
      # The following lines of boilerplate have to be in your project's
      # CMakeLists in this exact order for cmake to work correctly
      cmake_minimum_required(VERSION 3.5)
      
      include($ENV{IDF_PATH}/tools/cmake/project.cmake)
      project(hello_world)
      
    • サンプルで使用したファイルと同じ内容。
    • コンパイラーのバージョン/インクルードファイルのパス/プロジェクト名の指定
  • main フォルダには、CMakeList.txtとhello_world.c。
    • CMakeList.txt:
      • 
        idf_component_register(SRCS "hello_world.c"
                            INCLUDE_DIRS ".")
        
      • SRCSでソースのファイル名を、INCLUDE_DIRSでインクルードパスを指定。
    • hello_world.c:
      • 
        #include <stdio.h>
        
        void app_main(void)
        {
        
        }
        
      • 関数の宣言のみ

となっていました。ここで”hello_world.c”にprintf関数を追加して保存しました。


#include <stdio.h>

void app_main(void)
{
    printf("Hello world!\n");
}

ソースが準備出来ました。サンプルでは次にCPUの指定を”idf.py set-target”を行いました。”~/esp/hello_world”フォルダに移動して”idf.py set-target esp32”を実行します。実行後ディレクトリーは


~/esp/
    --hello_world/
       --CMakeList.txt
       --sdkconfig
       --main/
             --hello_world_main.c
             --CMakeList.txt
       --build/
  • ”sdkconfig” ファイルと ”build” フォルダーが作成されました。
  • 両者ともサンプルの時と同じ内容。

次にサンプルではコンフィグの設定、”idf.py menuconfig”を行いました。今回はサンプル同様スキップしました。

ソースのコンパイル、書き込み、結果表示するため、”idf.py flash monitor” を実行しました。問題無く実行されモニターに、”Hello world!”が表示されました。


I (267) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (273) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (279) heap_init: At 4008B160 len 00014EA0 (83 KiB): IRAM
I (287) spi_flash: detected chip: generic
I (290) spi_flash: flash io: dio
W (294) spi_flash: Detected size(4096k) larger than the size in the binary image header(2048k). Using the size in the binary image header.
I (308) cpu_start: Starting scheduler on PRO CPU.
I (0) cpu_start: Starting scheduler on APP CPU.
Hello world!

新規プロジェクトを作成するには

今回は下記の事が分かりました。

  • 使うコマンドは: idf.py create-project
  • コマンドを実行すると以下が作成される
    • プロジェクト(実際にはフォルダー)
    • プロジェクト用CMakeList.txt
    • プロジェクトフォルダーの下にmainフォルダー
    • mainフォルダーには、Cのソースファイルとそれ用のCMakeList.txt
  • その後は以下を行う。
    • CPUの指定:”idf.py set-target esp32”
    • コンフィグの設定:”idf.py menuconfig”
    • コンパイル:”idf.py build”
    • 書き込み:”idf.py flash”
    • 結果表示:”idf.py monitor”

次回は

次回はもう少し、プロジェクトのファイル構造に付いて調べてみます。