前言
之前在nuttx下编写应用的时候,陷入了之前写单片机代码的误区,没有很好的讲逻辑层和驱动层的代码分割开来,
导致写出来的程序,可读性和可移植性太差,这次参考ardupilot的思路重新整理下实现的过程;
目标
实现一个电机接口库,具体目标如下:
1.这个库应该是建立在nuttx上;
2.利用os原有的PWM驱动以及IO驱动实现电机的控制接口;
3.抽象出电机模型(例如差速模型、姿态模型、自行车模型、单电机模型...);
4.文件目录层级:
|—../app/libraries
|—motor
|—差速模型(*.c、.h、.....)
|—自行车模型(*.c、.h、.....)
|—.......
|—单电机模型(*.c、.h、.....)
|—InertialSensor
....
|—编译文件(makefile、Kconfig、make.dep、make.defs)
文件的大概目录就是这样,下面结合实际的例子来详细说明。
libraries
libraries这个文件夹是新建的,就从这里开始吧~
然后编译器在编译的时候才找得到新建的源文件,从而编译;
首先在app下面按照上面的文件目录层级建立好所需要的源文件,接下怎么呢?
刚开始我蒙圈了,经验告诉我去看看readme,里面有如下描述:(我就只挑重点的来了)
Building NuttX with Board-Specific Pieces Outside the Source Tree
-----------------------------------------------------------------
3) If you like the random collection of stuff in the apps/ directory
but just want to expand the existing components with your own,
external sub-directory then there is an easy way to that too:
You just create a sympolic link in the apps/ directory that
redirects to your application sub-directory.
In order to be incorporated into the build, the directory that
you link under the apps/ directory should contain (1) a Makefile
that supports the clean and distclean targets (see other Makefiles
for examples), and (2) a tiny Make.defs file that simply adds the
custon build directories to the variable CONFIGURED_APPS like:
CONFIGURED_APPS += my_directory1 my_directory2
The apps/Makefile will always automatically check for the
existence of subdirectories containing a Makefile and a Make.defs
file. The Makefile will be used only to support cleaning operations.
The Make.defs file provides the set of directories to be built; these
directories must also contain a Makefile. That Makefile must be able
to build the sources and add the objects to the apps/libapps.a archive.
(see other Makefiles for examples). It should support the all,
install, context, and depend targets.
总结一下,新增加的代码树需要做以下工作:
1.apps/ directory路径下需要包含makefile
(支持clean、distclean(本质是将目录结构还原为其原始的、未配置的状态),可参考其他的makefile);
2.apps/ directory路径下需要包含make.defs(将新建的路径添加到编译构建的环境变量);
3.apps/ directory路径下需要包含kconfig(这里不是必须的,方便我们图形化设置一些变量);
makefile
当按照上述步骤进行后,我们在返回的顶层目录,apps/makfile会自动检查其子目录下的makefile和make.defs,
刚刚说了make.defs提供了新建文件夹的路径,这样编译器才能找到新建路径下的源文件并进行编译
(当然该路径下也是需要包含makefile的),我们来看看源文件是怎么写的(提前透露下 ,我是参考modbus的写的)
############################################################################
# apps/modbus/Makefile
#
# Copyright (C) 2012, 2016 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)/.config
-include $(TOPDIR)/Make.defs
include $(APPDIR)/Make.defs
#这个宏是方便在kconfig对某些模块进行预编译设置
#ifeq ($(CONFIG_MOTORS),y)
#这里是引用具体文件下的定义(对象是具体的源文件*.c了,设置编译规则...)
include motor/Make.defs
#endif #CONFIG_MOTORS
#下面这些是固定框架(目标文件、源文件...)
AOBJS = $(ASRCS:.S=$(OBJEXT))
COBJS = $(CSRCS:.c=$(OBJEXT))
SRCS = $(ASRCS) $(CSRCS)
OBJS = $(AOBJS) $(COBJS)
ifeq ($(CONFIG_WINDOWS_NATIVE),y)
BIN = ..\libapps$(LIBEXT)
else
ifeq ($(WINTOOL),y)
BIN = ..\\libapps$(LIBEXT)
else
BIN = ../libapps$(LIBEXT)
endif
endif
# Build targets
all: .built
.PHONY: context .depend depend clean distclean
ifeq ($(CONFIG_MOTORS),y)
$(AOBJS): %$(OBJEXT): %.S
$(call ASSEMBLE, $<, $@)
$(COBJS): %$(OBJEXT): %.c
$(call COMPILE, $<, $@)
endif
.built: $(OBJS)
ifeq ($(CONFIG_MOTORS),y)#这里需要改成我设置预编译变量
$(call ARCHIVE, $(BIN), $(OBJS))
$(Q) touch .built
endif
install:
context:
.depend: Makefile $(SRCS)
ifeq ($(CONFIG_MOTORS),y)#这里需要改成我设置预编译变量
$(Q) $(MKDEP) $(DEPPATH) "$(CC)" -- $(CFLAGS) -- $(SRCS) >Make.dep
$(Q) touch $@
endif
depend: .depend
clean:
$(call DELFILE, .built)
$(call CLEAN)
distclean: clean
$(call DELFILE, Make.dep)
$(call DELFILE, .depend)
-include Make.dep
.PHONY: preconfig
preconfig:
make.defs
主要是添加了libraries的路径到环境变量中,Make.defs 文件是 Makefile 片断;
############################################################################
# apps/modbus/Make.defs
# Adds selected applications to apps/ build
#
# Copyright (C) 2012 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.
#
############################################################################
#ifeq ($(CONFIG_MOTORS),y)
CONFIGURED_APPS += libraries
#endif
完成上面必须的两步,基本上也就完成了新建路径到编译器中去了;接着往下走,在具体包含的原文件下面还需要增加一个make.defs:
############################################################################
# apps/modbus/tcp/Make.defs
#
# Copyright (C) 2012 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.
#
############################################################################
#ifeq ($(CONFIG_SINGLE_DC_MOTORS),y)
CSRCS += singel_dc_motor.c #添加源文件
DEPPATH += --dep-path motor #
VPATH += :motor #指定搜索路径,使用空格或者冒号(:)将多个目录分开。
CFLAGS += ${shell $(INCDIR) $(INCDIROPT) "$(CC)" $(APPDIR)/libraries/motor}
#endif #CONFIG_SINGLE_DC_MOTORS
完成上面几个步骤,直接就可以将新建的路径添加到编译系统中了~