nuttx app添加自己的小應用程序

nuttx想要添加自己寫的一些小應用非常方便,具體的步驟如下:
1,在apps/examples/目錄下添加自己的目錄。
2,添加Kconfig, Makefile, Make.defs, 以及應用的源文件。
3,通過build.sh menuconfig去選擇自己添加的app即可。
4,將編譯好的版本下載到板子中,在nuttx shell試圖下執行?回車即可看到自己添加的小應用。在shell下輸入自己添加的小應用的名字回車既可運行程序。

首先我們如果想添加一些自己的小程序需要在apps/examples/目錄下添加自己的app目錄,以apps/examples/hello爲例:
我們需要添加hello_main.c, Kconfig, Make.defs, Makefile這四個文件。

Kconfig的內容如下。

#
# For a description of the syntax of this configuration file,
# see the file kconfig-language.txt in the NuttX tools repository.
#

config EXAMPLES_HELLO		改名字是用來在defconfig中配置使用的。添加該配置之後,該應用會被打開。
	tristate "\"Hello, World!\" example"	這個是在menuconfig中看到的選項提示
	default n				默認是不打開
	---help---
		Enable the \"Hello, World!\" example

if EXAMPLES_HELLO	如果打開該選項就進行下面的配置。

config EXAMPLES_HELLO_PROGNAME		app的名字
	string "Program name"
	default "hello"
	---help---
		This is the name of the program that will be used when the NSH ELF
		program is installed.

config EXAMPLES_HELLO_PRIORITY		設置棧的優先級
	int "Hello task priority"
	default 100

config EXAMPLES_HELLO_STACKSIZE		設置棧的大小
	int "Hello stack size"
	default DEFAULT_TASK_STACKSIZE

endif

Make.defs的內容如下。

############################################################################
# apps/examples/hello/Make.defs
# Adds selected applications to apps/ build
#
#   Copyright (C) 2015 Gregory Nutt. All rights reserved.
#   Author: Gregory Nutt <gnutt@nuttx.org>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in
#    the documentation and/or other materials provided with the
#    distribution.
# 3. Neither the name NuttX nor the names of its contributors may be
#    used to endorse or promote products derived from this software
#    without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
############################################################################

ifneq ($(CONFIG_EXAMPLES_HELLO),)	需要注意該名字需要和Kconfig的一致性
CONFIGURED_APPS += $(APPDIR)/examples/hello			這個是hello這個app所在的目錄。
endif

Makefile的內容:

############################################################################
# apps/examples/hello/Makefile
#
#   Copyright (C) 2008, 2010-2013 Gregory Nutt. All rights reserved.
#   Author: Gregory Nutt <gnutt@nuttx.org>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in
#    the documentation and/or other materials provided with the
#    distribution.
# 3. Neither the name NuttX nor the names of its contributors may be
#    used to endorse or promote products derived from this software
#    without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
############################################################################

include $(TOPDIR)/Make.defs

# Hello, World! built-in application info

PROGNAME  = $(CONFIG_EXAMPLES_HELLO_PROGNAME)	PROGNAME設置爲在Kconfig中配置的名字
PRIORITY  = $(CONFIG_EXAMPLES_HELLO_PRIORITY)	優先級設置爲Kconfig中的配置
STACKSIZE = $(CONFIG_EXAMPLES_HELLO_STACKSIZE)	棧大小設置爲Kconfig給hello配置的大小
MODULE    = $(CONFIG_EXAMPLES_HELLO)			模塊也是Kconfig中的配置名稱

# Hello, World! Example

MAINSRC = hello_main.c			main src文件是hello.c文件

include $(APPDIR)/Application.mk

hello_main.c的內容:

/****************************************************************************
 * Included Files
 ****************************************************************************/

#include <nuttx/config.h>		需要注意的一點是通常我們會在我們添加的app源文件的頂部添加該頭文件的引用。
#include <stdio.h>

/****************************************************************************
 * Public Functions
 ****************************************************************************/

/****************************************************************************
 * hello_main
 ****************************************************************************/

int main(int argc, FAR char *argv[])
{
  printf("Hello, World!!\n");
  return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章