STM32でNUTTXを使ってみる

新しいアプリケーションの追加


前回はNuttxに付属しているサンプルアプリ (HelloWorld)を追加しまし た。
そこで、今回は新しいアプリの追加方法を紹介します。

新しいアプリを追加するもっとも簡単な方法は、既存のアプリをコピーして作ります。
コピー元には前回紹介したHelloWorldアプリケーションです。
$ cd $HOME/nuttxspace/apps/examples
$ cp -r hello myapp
$ ls -l myapp
合計 24
-rw-rw-r-- 1 nop nop  620  1月  5 17:31 Kconfig
-rw-rw-r-- 1 nop nop 1056  1月  5 17:31 Make.defs
-rw-rw-r-- 1 nop nop 2481  1月  5 17:31 Make.dep
-rw-rw-r-- 1 nop nop 1303  1月  5 17:31 Makefile
-rw-rw-r-- 1 nop nop 1650  1月  5 17:31 hello_main.c
-rw-rw-r-- 1 nop nop 1256  1月  5 17:31 hello_main.c.home.nop.nuttxspace.apps.examples.hello.o

$ cd myapp
$ mv hello_main.c myapp_main.c
$ rm Make.dep # あれば削除する
$ rm hello_main.c.home.nop.nuttxspace.apps.examples.hello.o #あれば削除する

このようになります

$ ls -l
合計 16
-rw-rw-r-- 1 nop nop  612  9月 10 21:30 Kconfig
-rw-rw-r-- 1 nop nop 1887  9月 10 21:24 Make.defs
-rw-rw-r-- 1 nop nop 2262  9月 10 21:27 Makefile
-rw-rw-r-- 1 nop nop 2546  9月 10 21:21 myapp_main.c

このままではHelloWorldアプリのままなので、ソースファイル(myapp_main.c)を変更します。

【変更前】
/****************************************************************************
 * hello_main
 ****************************************************************************/

int main(int argc, FAR char *argv[])
{
  printf("Hello, World!!\n");
  return 0;
}

【変更後】
/****************************************************************************
 * myapp_main
 ****************************************************************************/

int main(int argc, FAR char *argv[])
{
  printf("This is myapp!!\n");
  return 0;
}

Make.defsも同様に変更します。
CONFIGURED_APPSに定義するのは、アプリケーションファイルが存在するパスです。
今回は$HOME/nuttxspace/apps/examples/myappにアプリを作ったので、この様になります。
ifneq ($(CONFIG_EXAMPLES_MYAPP),)
CONFIGURED_APPS += $(APPDIR)/examples/myapp
endif

Makefileも同様に変更します。
include $(APPDIR)/Make.defs

# myapp built-in application info

PROGNAME  = $(CONFIG_EXAMPLES_MYAPP_PROGNAME)
PRIORITY  = $(CONFIG_EXAMPLES_MYAPP_PRIORITY)
STACKSIZE = $(CONFIG_EXAMPLES_MYAPP_STACKSIZE)
MODULE    = $(CONFIG_EXAMPLES_MYAPP)

# myapp Example

MAINSRC = myapp_main.c

include $(APPDIR)/Application.mk

Kconfigも同様に変更します。
EXAMPLES_MYAPP、EXAMPLES_MYAPP_PROGNAME、EXAMPLES_MYAPP_PRIORITY、 EXAMPLES_MYAPP_STACKSIZEは、
CONFIG_EXAMPLES_MYAPPの様な変数名として.configに書き込まれ、ビルド時に参照される変 数です。
MakefileのCONFIG_XXXXと一致している必要が有ります。
変更例を紹介します。
#
# For a description of the syntax of this configuration file,
# see the file kconfig-language.txt in the NuttX tools repository.
#

config EXAMPLES_MYAPP
    tristate "MyApp example"
    default n
    ---help---
        Enable the MyApp example

if EXAMPLES_MYAPP

config EXAMPLES_MYAPP_PROGNAME
    string "Program name"
    default "myapp"
    ---help---
        This is the name of the program that will be used when the NSH ELF
        program is installed.

config EXAMPLES_MYAPP_PRIORITY
    int "MyApp task priority"
    default 100

config EXAMPLES_MYAPP_STACKSIZE
    int "MyApp stack size"
    default DEFAULT_TASK_STACKSIZE

endif

以下のコマンドで変更漏れがないかどうか確認することができます。
$ grep hello *
$ grep Hello *
$ grep HELLO *

最後に以下のファイルにmyappを追加します。
これでMenuに表示されるようになります。
$ vi $HOME/nuttxspace/apps/examples/Kconfig
source "/home/nop/nuttxspace/apps/examples/hello/Kconfig"
source "/home/nop/nuttxspace/apps/examples/myapp/Kconfig" ---> 追加

今回もusbnshのひな形をベースとして使います。
OTG-USBのないボードの時はnshのひな形を使ってください。
$ cd $HOME/nuttxspace/nuttx
$ make distclean

# STM32F4 Discovery
$ ./tools/configure.sh -l stm32f4discovery/usbnsh

# Generic STM32F4
$ ./tools/configure.sh -l stm32f4discovery/nsh

distcleanすると、今までの設定ファイルが消えてしまいます。
今までの設定ファイルをそのまま有効にして、機能を追加する場合は、上記のオペレーションは不要です。

メニューに新しい項目を追加するときは、make cleanする必要が有ります。
これをしないとメニューに更新が反映されません。
$ make clean
$ make menuconfig







ここまで変更したらひたすらExitで抜けます。
最後に以下の画面で[.config]を上書きします。



新しいアプリを追加すると、前回と同様に.configに以下の変数が追加されます。
メニューでtask priorityとstacksizeを変更すると、以下の内容も変わります。
この.configの内容に従ってカーネル+アプリが1つの実行可能なファイルとしてビルドされます。
$ grep EXAMPLES_MYAPP $HOME/nuttxspace/nuttx/.config
CONFIG_EXAMPLES_MYAPP=y
CONFIG_EXAMPLES_MYAPP_PROGNAME="myapp"
CONFIG_EXAMPLES_MYAPP_PRIORITY=100
CONFIG_EXAMPLES_MYAPP_STACKSIZE=2048

新しい[.config]を使ってファームウェアをビルドします。
$ make

$ st-flash --connect-under-reset write nuttx.bin 0x8000000

新しいファームウェアをマイコンに書き込みます。
nshに接続するとmyappアプリが追加されています。




ファームの書き込み方法と、nshへの接続方法はこちらに紹介しています。
STM32F4 Discovery
STM32F3 Discovery
STM32F103RB Nucleo
STM32F103 BluePill/BlackPill
STM32F103 VEボード
STM32F407 VGボード

続く...