2
1
記事投稿キャンペーン 「2024年!初アウトプットをしよう」

rp2040用のFreeRTOSをカスタマイズする

Last updated at Posted at 2024-01-27

1. はじめに

FreeRTOSのコンフィグレーションを変更するためには、ファイルFreeRTOSConfig.hの内容を変更する必要があります。PlatformIOにおけるRaspberry Pi Pico用のArduino環境(earlephilhower版)では、このファイルはC:\Users\%USERNAME%\.platformio\packages\framework-arduinopico\libraries\FreeRTOS\src以下にあり、これを変更してしまうと他のプロジェクトにも影響してしまいます。
このため、各プロジェクトごとにFreeRTOSConfig.hを変更可能にする方法を検討しました。

2. 方針

変更したいファイルはFreeRTOSConfig.hのみですが、includeの順番などを考慮し、以下の方針で環境を作りました。

  1. プロジェクト内のlib/以下にFreeRTOSのファイル一式を格納する。
  2. 変更が必要なFreeRTOSConfig.h以外はファイルをコピーしないで、元の場所にあるファイルをincludeするだけのファイルを用意する。
  3. ディレクトリ構造は、元の形を踏襲する。

ソース一式は以下のリポジトリに格納しました。

3. 動作確認

@@@### 3.1 動作確認用プログラム
関数vTaskListを使用してタスク一覧を表示させるプログラムです。

main.cpp
#include <Arduino.h>
#include <FreeRTOS.h>
#include <task.h>
#ifdef USE_TINYUSB
#include <Adafruit_TinyUSB.h>
#endif
void
setup()
{
  Serial.begin(115200);
  while (!Serial)
  {
    ;
  }
  Serial.printf("build [%s %s]\n", __DATE__, __TIME__);
}

void
loop()
{
  static char buf[1024];
  vTaskList(buf);
  Serial.print(buf);
  delay(5000);
}
platformio.ini
[platformio]
default_envs = rpipico

[env:rpipico]
platform = https://github.com/maxgerhardt/platform-raspberrypi.git
board = rpipico
framework = arduino
board_build.core = earlephilhower
board_build.filesystem_size = 0m
build_flags = 
  -D USE_TINYUSB

3.2 タイマタスクの優先度が既定値の場合

標準の設定では、タイマタスクの優先度は2となっています。

設定

FreeRTOSConfig.h(抜粋)
/* Software timer definitions. */
#define configUSE_TIMERS				1
#define configTIMER_TASK_PRIORITY		( 2 )
#define configTIMER_QUEUE_LENGTH		5
#define configTIMER_TASK_STACK_DEPTH	( 1024 )

実行結果

-----------------------------------------
CORE0           X       4       958     1
IDLE1           X       0       231     6
IDLE0           R       0       229     5
IdleCore0       S       7       98      2
Tmr Svc         B       2*      988     4
IdleCore1       S       7       98      3

3.3 タイマタスクの優先度を変更

configTIMER_TASK_PRIORITYの値を2から4に変更します。実行させてみると、タイマタスク(Tmr Svc)の優先度が変わっていることがわかります。

設定

FreeRTOSConfig.h(抜粋)
/* Software timer definitions. */
#define configUSE_TIMERS				1
#define configTIMER_TASK_PRIORITY		( 4 )  ←ここを変更
#define configTIMER_QUEUE_LENGTH		5
#define configTIMER_TASK_STACK_DEPTH	( 1024 )

実行結果

-----------------------------------------
CORE0           X       4       894     1
IDLE0           X       0       229     5
IDLE1           R       0       231     6
Tmr Svc         B       4*      988     4
IdleCore1       S       7       98      3
IdleCore0       S       7       98      2

*のところが変更した優先度です

4. さいごに

すべてのソースコードをコピーしないで、FreeRTOSをカスタマイズする方法を紹介しました。pico-sdkのいくつかのサンプルで、FreeRTOSConfig.hを変更しているものがあり、それらをArduino環境(earlephilhower版)で動作させる場合に応用できます。

2
1
0

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
2
1