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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章