diff options
author | px4dev <px4@purgatory.org> | 2012-08-04 15:12:36 -0700 |
---|---|---|
committer | px4dev <px4@purgatory.org> | 2012-08-04 15:12:36 -0700 |
commit | 8a365179eafdf3aea98e60ab9f5882b200d4c759 (patch) | |
tree | 4f38d6d4cd80bd0b6e22e2bb534c3f117ce44e56 /apps/system | |
download | px4-firmware-8a365179eafdf3aea98e60ab9f5882b200d4c759.tar.gz px4-firmware-8a365179eafdf3aea98e60ab9f5882b200d4c759.tar.bz2 px4-firmware-8a365179eafdf3aea98e60ab9f5882b200d4c759.zip |
Fresh import of the PX4 firmware sources.
Diffstat (limited to 'apps/system')
-rw-r--r-- | apps/system/Kconfig | 20 | ||||
-rw-r--r-- | apps/system/Make.defs | 51 | ||||
-rw-r--r-- | apps/system/Makefile | 70 | ||||
-rw-r--r-- | apps/system/free/Kconfig | 14 | ||||
-rw-r--r-- | apps/system/free/Makefile | 114 | ||||
-rw-r--r-- | apps/system/free/README.txt | 6 | ||||
-rw-r--r-- | apps/system/free/free.c | 112 | ||||
-rw-r--r-- | apps/system/i2c/Kconfig | 61 | ||||
-rw-r--r-- | apps/system/i2c/Makefile | 102 | ||||
-rwxr-xr-x | apps/system/i2c/README.txt | 397 | ||||
-rw-r--r-- | apps/system/i2c/i2c_bus.c | 99 | ||||
-rw-r--r-- | apps/system/i2c/i2c_common.c | 216 | ||||
-rw-r--r-- | apps/system/i2c/i2c_dev.c | 235 | ||||
-rw-r--r-- | apps/system/i2c/i2c_get.c | 257 | ||||
-rw-r--r-- | apps/system/i2c/i2c_main.c | 454 | ||||
-rw-r--r-- | apps/system/i2c/i2c_set.c | 275 | ||||
-rw-r--r-- | apps/system/i2c/i2c_verf.c | 249 | ||||
-rw-r--r-- | apps/system/i2c/i2ctool.h | 210 | ||||
-rw-r--r-- | apps/system/install/Kconfig | 14 | ||||
-rwxr-xr-x | apps/system/install/Makefile | 114 | ||||
-rwxr-xr-x | apps/system/install/README.txt | 26 | ||||
-rwxr-xr-x | apps/system/install/install.c | 412 | ||||
-rw-r--r-- | apps/system/readline/Kconfig | 39 | ||||
-rw-r--r-- | apps/system/readline/Makefile | 103 | ||||
-rw-r--r-- | apps/system/readline/readline.c | 345 |
25 files changed, 3995 insertions, 0 deletions
diff --git a/apps/system/Kconfig b/apps/system/Kconfig new file mode 100644 index 000000000..44bf5a2e6 --- /dev/null +++ b/apps/system/Kconfig @@ -0,0 +1,20 @@ +# +# For a description of the syntax of this configuration file, +# see misc/tools/kconfig-language.txt. +# + +menu "Custom free memory command" +source "$APPSDIR/system/free/Kconfig" +endmenu + +menu "I2C tool" +source "$APPSDIR/system/i2c/Kconfig" +endmenu + +menu "FLASH Program Installation" +source "$APPSDIR/system/install/Kconfig" +endmenu + +menu "readline() support" +source "$APPSDIR/system/readline/Kconfig" +endmenu diff --git a/apps/system/Make.defs b/apps/system/Make.defs new file mode 100644 index 000000000..1ddabd337 --- /dev/null +++ b/apps/system/Make.defs @@ -0,0 +1,51 @@ +############################################################################ +# apps/system/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. +# +############################################################################ + +if ($(CONFIG_VSN_POWEROFF),y) +CONFIGURED_APPS += vsn/poweroff +endif + +if ($(CONFIG_VSN_RAMTRON),y) +CONFIGURED_APPS += vsn/ramtron +endif + +if ($(CONFIG_VSN_SDCARD),y) +CONFIGURED_APPS += vsn/sdcard +endif + +if ($(CONFIG_VSN_SYSINFO),y) +CONFIGURED_APPS += vsn/sysinfo +endif diff --git a/apps/system/Makefile b/apps/system/Makefile new file mode 100644 index 000000000..a0eb5dfde --- /dev/null +++ b/apps/system/Makefile @@ -0,0 +1,70 @@ +############################################################################ +# apps/system/Makefile +# +# Copyright (C) 2011-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. +# +############################################################################ + +-include $(TOPDIR)/.config # Current configuration + +# Sub-directories containing system task + +SUBDIRS = free i2c install readline + +# Create the list of installed runtime modules (INSTALLED_DIRS) + +define ADD_DIRECTORY +INSTALLED_DIRS += ${shell if [ -r $1/Makefile ]; then echo "$1"; fi} +endef + +$(foreach DIR, $(SUBDIRS), $(eval $(call ADD_DIRECTORY,$(DIR)))) + +all: nothing +.PHONY: nothing context depend clean distclean + +nothing: + +context: + +depend: + @for dir in $(INSTALLED_DIRS) ; do \ + $(MAKE) -C $$dir depend TOPDIR="$(TOPDIR)" APPDIR="$(APPDIR)"; \ + done + +clean: + @for dir in $(INSTALLED_DIRS) ; do \ + $(MAKE) -C $$dir clean TOPDIR="$(TOPDIR)" APPDIR="$(APPDIR)"; \ + done + +distclean: clean + @for dir in $(INSTALLED_DIRS) ; do \ + $(MAKE) -C $$dir distclean TOPDIR="$(TOPDIR)" APPDIR="$(APPDIR)"; \ + done diff --git a/apps/system/free/Kconfig b/apps/system/free/Kconfig new file mode 100644 index 000000000..239559867 --- /dev/null +++ b/apps/system/free/Kconfig @@ -0,0 +1,14 @@ +# +# For a description of the syntax of this configuration file, +# see misc/tools/kconfig-language.txt. +# + +config SYSTEM_FREE + bool "NSH free command replacement" + default n + ---help--- + Enable support for the NSH free replacement command. + +if SYSTEM_FREE +endif + diff --git a/apps/system/free/Makefile b/apps/system/free/Makefile new file mode 100644 index 000000000..974c98913 --- /dev/null +++ b/apps/system/free/Makefile @@ -0,0 +1,114 @@ +############################################################################ +# apps/system/free/Makefile +# +# Copyright (C) 2011 Uros Platise. All rights reserved. +# Author: Uros Platise <uros.platise@isotel.eu> +# Gregory Nutt <spudmonkey@racsa.co.cr> +# +# 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. +# +############################################################################ + +# TODO, this makefile should run make under the app dirs, instead of +# sourcing the Make.defs! + +-include $(TOPDIR)/.config +-include $(TOPDIR)/Make.defs +include $(APPDIR)/Make.defs + +ifeq ($(WINTOOL),y) +INCDIROPT = -w +endif + +# Hello Application +# TODO: appname can be automatically extracted from the directory name + +APPNAME = free +PRIORITY = SCHED_PRIORITY_DEFAULT +STACKSIZE = 768 + +ASRCS = +CSRCS = free.c + +AOBJS = $(ASRCS:.S=$(OBJEXT)) +COBJS = $(CSRCS:.c=$(OBJEXT)) + +SRCS = $(ASRCS) $(CSRCS) +OBJS = $(AOBJS) $(COBJS) + +ifeq ($(WINTOOL),y) + BIN = "${shell cygpath -w $(APPDIR)/libapps$(LIBEXT)}" +else + BIN = "$(APPDIR)/libapps$(LIBEXT)" +endif + +ROOTDEPPATH = --dep-path . + +# Common build + +VPATH = + +all: .built +.PHONY: context depend clean distclean + +$(AOBJS): %$(OBJEXT): %.S + $(call ASSEMBLE, $<, $@) + +$(COBJS): %$(OBJEXT): %.c + $(call COMPILE, $<, $@) + +.built: $(OBJS) + @( for obj in $(OBJS) ; do \ + $(call ARCHIVE, $(BIN), $${obj}); \ + done ; ) + @touch .built + +# Register application + +.context: + $(call REGISTER,$(APPNAME),$(PRIORITY),$(STACKSIZE),$(APPNAME)_main) + @touch $@ + +context: .context + +# Create dependencies + +.depend: Makefile $(SRCS) + @$(MKDEP) $(ROOTDEPPATH) $(CC) -- $(CFLAGS) -- $(SRCS) >Make.dep + @touch $@ + +depend: .depend + +clean: + @rm -f *.o *~ .*.swp .built + $(call CLEAN) + +distclean: clean + @rm -f .context Make.dep .depend + +-include Make.dep diff --git a/apps/system/free/README.txt b/apps/system/free/README.txt new file mode 100644 index 000000000..dd92a94ae --- /dev/null +++ b/apps/system/free/README.txt @@ -0,0 +1,6 @@ + +This application provides UNIX style memory free information. + + Source: NuttX + Author: Gregory Nutt <spudmonkey@racsa.co.cr> + Date: 17. March 2011 diff --git a/apps/system/free/free.c b/apps/system/free/free.c new file mode 100644 index 000000000..2f61a1dae --- /dev/null +++ b/apps/system/free/free.c @@ -0,0 +1,112 @@ +/**************************************************************************** + * apps/system/free/free.c + * + * Copyright (C) 2011 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * 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 <nuttx/config.h> +#include <nuttx/progmem.h> + +#include <stdio.h> +#include <stdlib.h> + + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/* \todo Max block size only works on uniform prog mem */ + +void free_getprogmeminfo(struct mallinfo * mem) +{ + uint16_t page = 0, stpage = 0xFFFF; + uint16_t pagesize = 0; + int status; + + mem->arena = 0; + mem->fordblks = 0; + mem->uordblks = 0; + mem->mxordblk = 0; + + for (status=0, page=0; status >= 0; page++) { + + status = up_progmem_ispageerased(page); + pagesize = up_progmem_pagesize(page); + + mem->arena += pagesize; + + /* Is this beginning of new free space section */ + if (status == 0) { + if (stpage == 0xFFFF) stpage = page; + mem->fordblks += pagesize; + } + else if (status != 0) { + mem->uordblks += pagesize; + + if (stpage != 0xFFFF && up_progmem_isuniform()) { + stpage = page - stpage; + if (stpage > mem->mxordblk) + mem->mxordblk = stpage; + stpage = 0xFFFF; + } + } + } + + mem->mxordblk *= pagesize; +} + + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +int free_main(int argc, char **argv) +{ + struct mallinfo data; + struct mallinfo prog; + +#ifdef CONFIG_CAN_PASS_STRUCTS + data = mallinfo(); +#else + (void)mallinfo(&data); +#endif + + free_getprogmeminfo(&prog); + + printf(" total used free largest\n"); + printf("Data: %11d%11d%11d%11d\n", + data.arena, data.uordblks, data.fordblks, data.mxordblk); + printf("Prog: %11d%11d%11d%11d\n", + prog.arena, prog.uordblks, prog.fordblks, prog.mxordblk); + + return OK; +} diff --git a/apps/system/i2c/Kconfig b/apps/system/i2c/Kconfig new file mode 100644 index 000000000..745378b37 --- /dev/null +++ b/apps/system/i2c/Kconfig @@ -0,0 +1,61 @@ +# +# For a description of the syntax of this configuration file, +# see misc/tools/kconfig-language.txt. +# + + +config SYSTEM_I2CTOOL + bool "I2C tool" + default n + depends on I2C + ---help--- + Enable support for the I2C tool. + +config I2CTOOL_BUILTIN + bool "NSH built-in command" + default y + depends on SYSTEM_I2CTOOL && NSH_BUILTIN_APPS + ---help--- + Build the tools as an NSH built-in command + +config I2CTOOL_MINBUS + int "Minimum bus number" + default 0 + depends on SYSTEM_I2CTOOL + ---help--- + Smallest bus index supported by the hardware (default 0). + +config I2CTOOL_MAXBUS + int "Maximum bus number" + depends on SYSTEM_I2CTOOL + default 3 + ---help--- + Largest bus index supported by the hardware (default 3) + +config I2CTOOL_MINADDR + hex "Minimum I2C address" + depends on SYSTEM_I2CTOOL + default 0x03 + ---help--- + Minium 7-bit device address (default: 0x03) + +config I2CTOOL_MAXADDR + hex "Maximum I2C address" + depends on SYSTEM_I2CTOOL + default 0x77 + ---help--- + Largest 7-bit device address (default: 0x77) + +config I2CTOOL_MAXREGADDR + hex "Maximum I2C register address" + default 0xff + depends on SYSTEM_I2CTOOL + ---help--- + Largest I2C register address (default: 0xff) + +config I2CTOOL_DEFFREQ + int "Default I2C frequency" + default 4000000 + depends on SYSTEM_I2CTOOL + ---help--- + Default I2C frequency (default: 4000000) diff --git a/apps/system/i2c/Makefile b/apps/system/i2c/Makefile new file mode 100644 index 000000000..845c149f6 --- /dev/null +++ b/apps/system/i2c/Makefile @@ -0,0 +1,102 @@ +############################################################################ +# apps/system/i2c +# +# Copyright (C) 2011-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. +# +############################################################################ + +-include $(TOPDIR)/.config +-include $(TOPDIR)/Make.defs +include $(APPDIR)/Make.defs + +# I2C tool + +ASRCS = +CSRCS = i2c_bus.c i2c_common.c i2c_dev.c i2c_get.c i2c_main.c i2c_set.c i2c_verf.c + +AOBJS = $(ASRCS:.S=$(OBJEXT)) +COBJS = $(CSRCS:.c=$(OBJEXT)) + +SRCS = $(ASRCS) $(CSRCS) +OBJS = $(AOBJS) $(COBJS) + +ifeq ($(WINTOOL),y) + BIN = "${shell cygpath -w $(APPDIR)/libapps$(LIBEXT)}" +else + BIN = "$(APPDIR)/libapps$(LIBEXT)" +endif + +ROOTDEPPATH = --dep-path . +VPATH = + +APPNAME = i2c +PRIORITY = SCHED_PRIORITY_DEFAULT +STACKSIZE = 2048 + +# Build targets + +all: .built +.PHONY: .context context .depend depend clean distclean + +$(AOBJS): %$(OBJEXT): %.S + $(call ASSEMBLE, $<, $@) + +$(COBJS): %$(OBJEXT): %.c + $(call COMPILE, $<, $@) + +.built: $(OBJS) + @( for obj in $(OBJS) ; do \ + $(call ARCHIVE, $(BIN), $${obj}); \ + done ; ) + @touch .built + +.context: + $(call REGISTER,$(APPNAME),$(PRIORITY),$(STACKSIZE),$(APPNAME)_main) + @touch $@ + +context: .context + +.depend: Makefile $(SRCS) + @$(MKDEP) $(ROOTDEPPATH) \ + $(CC) -- $(CFLAGS) -- $(SRCS) >Make.dep + @touch $@ + +depend: .depend + +clean: + @rm -f *.o *~ .*.swp .built + $(call CLEAN) + +distclean: clean + @rm -f Make.dep .depend + +-include Make.dep + diff --git a/apps/system/i2c/README.txt b/apps/system/i2c/README.txt new file mode 100755 index 000000000..97801bcaa --- /dev/null +++ b/apps/system/i2c/README.txt @@ -0,0 +1,397 @@ +README File for the I2C Tool
+============================
+
+The I2C tool provides a way to debug I2C related problems. This README file
+will provide usage information for the I2C tools.
+
+CONTENTS
+========
+
+ o System Requirements
+ - I2C Driver
+ - Configuration Options
+ o Help
+ o Common Line Form
+ o Common Command Options
+ - "Sticky" Options
+ - Environment variables
+ - Common Option Summary
+ o Command summary
+ - bus
+ - dev
+ - get
+ - set
+ - verf
+ o I2C Build Configuration
+ - NuttX Configuration Requirements
+ - I2C Tool Configuration Options
+
+System Requirements
+===================
+
+I2C Driver
+----------
+In order to use the I2C driver, you system -- in particular, your I2C driver --
+must meet certain requirements:
+
+1. It support calling up_i2cinitialize() numerous times, resetting the I2C
+ hardware on each (initial) time. up_i2cuninitialize() will be called after
+ each call to up_i2cinitialize() to free any resources and disable the I2C.
+2. up_i2cinitialize must accept any interface number without crashing. It
+ must simply return NULL if the device is not supported.
+3. The I2C driver must support the transfer method (CONFIG_I2C_TRANSFER=y).
+
+The I2C tool is designed to be implemented as a NuttShell (NSH) add-on. Read
+the apps/nshlib/README.txt file for information about add-ons.
+
+Configuration Options
+---------------------
+CONFIG_I2CTOOL_BUILTIN - Build the tools as an NSH built-in command
+CONFIG_I2CTOOL_MINBUS - Smallest bus index supported by the hardware (default 0).
+CONFIG_I2CTOOL_MAXBUS - Largest bus index supported by the hardware (default 3)
+CONFIG_I2CTOOL_MINADDR - Minium device address (default: 0x03)
+CONFIG_I2CTOOL_MAXADDR - Largest device address (default: 0x77)
+CONFIG_I2CTOOL_MAXREGADDR - Largest register address (default: 0xff)
+CONFIG_I2CTOOL_DEFFREQ - Default frequency (default: 4000000)
+
+HELP
+====
+
+First of all, the I2C tools supports a pretty extensive help output. That
+help output can be view by entering either:
+
+ nsh> i2c help
+
+or
+
+ nsh> i2c ?
+
+Here is an example of the help output. I shows the general form of the
+command line, the various I2C commands supported with their unique command
+line options, and a more detailed summary of the command I2C command
+options.
+
+ nsh> i2c help
+ Usage: i2c <cmd> [arguments]
+ Where <cmd> is one of:
+
+ Show help : ?
+ List buses : bus
+ List devices : dev [OPTIONS] <first> <last>
+ Read register : get [OPTIONS] [<repititions>]
+ Show help : help
+ Write register: set [OPTIONS] <value> [<repititions>]
+ Verify access : verf [OPTIONS] <value> [<repititions>]
+
+ Where common "sticky" OPTIONS include:
+ [-a addr] is the I2C device address (hex). Default: 03 Current: 03
+ [-b bus] is the I2C bus number (decimal). Default: 1 Current: 1
+ [-r regaddr] is the I2C device register address (hex). Default: 00 Current: 00
+ [-w width] is the data width (8 or 16 decimal). Default: 8 Current: 8
+ [-s|n], send/don't send start between command and data. Default: -n Current: -n
+ [-i|j], Auto increment|don't increment regaddr on repititions. Default: NO Current: NO
+ [-f freq] I2C frequency. Default: 100000 Current: 100000
+
+ NOTES:
+ o An environment variable like $PATH may be used for any argument.
+ o Arguments are "sticky". For example, once the I2C address is
+ specified, that address will be re-used until it is changed.
+
+ WARNING:
+ o The I2C dev command may have bad side effects on your I2C devices.
+ Use only at your own risk.
+
+COMMAND LINE FORM
+=================
+
+The I2C is started from NSH by invoking the 'i2c' command from the NSH
+command line. The general form of the 'i2c' command is:
+
+ i2c <cmd> [arguments]
+
+Where <cmd> is a "sub-command" and identifies one I2C operations supported
+by the tool. [arguments] represents the list of arguments needed to perform
+the I2C operation. Those arguments vary from command to command as
+described below. However, there is also a core set of common OPTIONS
+supported by all commands. So perhaps a better representation of the
+general I2C command would be:
+
+ i2c <cmd> [OPTIONS] [arguments]
+
+Where [OPTIONS] represents the common options and and arguments represent
+the operation-specific arguments.
+
+COMMON COMMAND OPTIONS
+======================
+
+"Sticky" Options
+----------------
+In order to interact with I2C devices, there are a number of I2C parameters
+that must be set correctly. One way to do this would be to provide to set
+the value of each separate command for each I2C parameter. The I2C tool
+takes a different approach, instead: The I2C configuration can be specified
+as a (potentially long) sequence of command line arguments.
+
+These arguments, however, are "sticky." They are sticky in the sense that
+once you set the I2C parameter, that value will remain until it is reset
+with a new value (or until you reset the board).
+
+Environment Variables
+---------------------
+NOTE also that if environment variables are not disabled (by
+CONFIG_DISABLE_ENVIRON=y), then these options may also be environment
+variables. Environment variables must be preceded with the special
+character $. For example, PWD is the variable that holds the current
+working directory and so $PWD could be used as a command line argument. The
+use of environment variables on the I2C tools command is really only useful
+if you wish to write NSH scripts to execute a longer, more complex series of
+I2C commands.
+
+Common Option Summary
+---------------------
+
+[-a addr] is the I2C device address (hex). Default: 03 Current: 03
+
+ The [-a addr] sets the I2C device address. The valid range is 0x03
+ through 0x77 (this valid range is controlled by the configuration settings
+ CONFIG_I2CTOOL_MINADDR and CONFIG_I2CTOOL_MAXADDR). If you are working
+ with the same device, the address needs to be set only once.
+
+ All I2C address are 7-bit, hexadecimal values.
+
+ NOTE 1: Notice in the "help" output above it shows both default value of
+ the I2C address (03 hex) and the current address value (also 03 hex).
+
+ NOTE 2: Sometimes I2C addresses are represented as 8-bit values (with
+ bit zero indicating a read or write operation). The I2C tool uses a
+ 7-bit representation of the address with bit 7 unused and no read/write
+ indication in bit 0. Essentially, the 7-bit address is like the 8-bit
+ address shifted right by 1.
+
+ NOTE 3: Most I2C bus controllers will also support 10-bit addressing.
+ That capability has not been integrated into the I2C tool as of this
+ writing.
+
+[-b bus] is the I2C bus number (decimal). Default: 1 Current: 1
+
+ Most devices support multiple I2C devices and also have unique bus
+ numbering. This option identifies which bus you are working with now.
+ The valid range of bus numbers is controlled by the configuration settings
+ CONFIG_I2CTOOL_MINBUS and CONFIG_I2CTOOL_MAXBUS.
+
+ The bus numbers are small, decimal numbers.
+
+[-r regaddr] is the I2C device register address (hex). Default: 00 Current: 00
+
+ The I2C set and get commands will access registers on the I2C device. This
+ option selects the device register address (sometimes called the sub-address).
+ This is an 8-bit hexadecimal value. The maximum value is determined by
+ the configuration setting CONFIG_I2CTOOL_MAXREGADDR.
+
+[-w width] is the data width (8 or 16 decimal). Default: 8 Current: 8
+
+ Device register data may be 8-bit or 16-bit. This options selects one of
+ those two data widths.
+
+[-s|n], send/don't send start between command and data. Default: -n Current: -n
+
+ This determines whether or not there should be a new I2C START between
+ sending of the register address and sending/receiving of the register data.
+
+[-i|j], Auto increment|don't increment regaddr on repititions. Default: NO Current: NO
+
+ On commands that take a optional number of repetitions, the option can be
+ used to temporarily increment the regaddr value by one on each repitition.
+
+[-f freq] I2C frequency. Default: 400000 Current: 400000
+
+ The [-f freq] sets the frequency of the I2C device.
+
+COMMAND SUMMARY
+===============
+
+We have already seen the I2C help (or ?) commands above. This section will
+discusse the remaining commands.
+
+List buses: bus [OPTIONS]
+--------------------------
+
+This command will simply list all of the configured I2C buses and indicate
+which are supported by the driver and which are not:
+
+ BUS EXISTS?
+ Bus 1: YES
+ Bus 2: NO
+
+The valid range of bus numbers is controlled by the configuration settings
+CONFIG_I2CTOOL_MINBUS and CONFIG_I2CTOOL_MAXBUS.
+
+List devices: dev [OPTIONS] <first> <last>
+------------------------------------------
+
+The 'dev' command will attempt to identify all of the I2C devices on the
+selected bus. The <first> and <last> arguments are 7-bit, hexadecimal
+I2C addresses. This command will examine a range of addresses beginning
+with <first> and continuing through <last>. It will request the value
+of register zero from each device.
+
+If the device at an address responds, then this command will display the
+address of the device. If the device does not respond, this command will
+display "--". The resulting display is like:
+
+nsh> i2c dev 03 77
+ 0 1 2 3 4 5 6 7 8 9 a b c d e f
+00: -- -- -- -- -- -- -- -- -- -- -- -- --
+10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
+20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
+30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
+40: -- -- -- -- -- -- -- -- -- 49 -- -- -- -- -- --
+50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
+70: -- -- -- -- -- -- -- --
+
+WARNINGS:
+ o The I2C dev command may have bad side effects on certain I2C devices.
+ For example, if could cause data loss in an EEPROM device.
+ o The I2C dev command also depends upon the underlying behavior of the
+ I2C driver. How does the driver respond to addressing failures?
+
+Read register: get [OPTIONS]
+----------------------------
+
+ This command will read the value of the I2C register using the selected
+ I2C parameters in the common options. No other arguments are required.
+
+ This command with write the 8-bit address value then read an 8- or 16-bit
+ data value from the device. Optionally, it may re-start the transfer
+ before obtaining the data.
+
+ An optional <repititions> argument can be supplied to repeat the
+ read operation an arbitrary number of times (up to 2 billion). If
+ auto-increment is select (-i), then the register address will be
+ temporarily incremented on each repitions. The increment is temporary
+ in the since that it will not alter the "sticky" value of the
+ register address.
+
+ On success, the output will look like the following (the data value
+ read will be shown as a 4-character hexadecimal number if the 16-bit
+ data width option is selected).
+
+ READ Bus: 1 Addr: 49 Subaddr: 04 Value: 96
+
+ All values (except the bus numbers) are hexadecimal.
+
+Write register: set [OPTIONS] <value>
+-------------------------------------
+
+ This command will write a value to an I2C register using the selected
+ I2C parameters in the common options. The value to write must be provided
+ as the final, hexadecimal value. This value may be an 8-bit value (in the
+ range 00-ff) or a 16-bit value (in the range 0000-ffff), depending upon
+ the selected data width.
+
+ This command will write the 8-bit address value then write the 8- or 16-bit
+ data value to the device. Optionally, it may re-start the transfer
+ before writing the data.
+
+ An optional <repititions> argument can be supplied to repeat the
+ write operation an arbitrary number of times (up to 2 billion). If
+ auto-increment is select (-i), then the register address will be
+ temporarily incremented on each repitions. The increment is temporary
+ in the since that it will not alter the "sticky" value of the
+ register address.
+
+ On success, the output will look like the following (the data value
+ written will be shown as a 4-character hexadecimal number if the 16-bit
+ data width option is selected).
+
+ WROTE Bus: 1 Addr: 49 Subaddr: 04 Value: 96
+
+ All values (except the bus numbers) are hexadecimal.
+
+Verify access : verf [OPTIONS] <value> [<repititions>]
+------------------------------------------------------
+
+ This command combines writing and reading from an I2C device register.
+ It will write a value to an will write a value to an I2C register using
+ the selected I2C parameters in the common options just as described for
+ tie 'set' command. Then this command will read the value back just
+ as described with the 'get' command. Finally, this command will compare
+ the value read and against the value written and emit an error message
+ if they do not match.
+
+ If no value is provided, then this command will use the register address
+ itself as the data, providing for a address-in-address test.
+
+ An optional <repititions> argument can be supplied to repeat the
+ verify operation an arbitrary number of times (up to 2 billion). If
+ auto-increment is select (-i), then the register address will be
+ temporarily incremented on each repitions. The increment is temporary
+ in the since that it will not alter the "sticky" value of the
+ register address.
+
+ On success, the output will look like the following (the data value
+ written will be shown as a 4-character hexadecimal number if the 16-bit
+ data width option is selected).
+
+ VERIFY Bus: 1 Addr: 49 Subaddr: 04 Wrote: 96 Read: 92 FAILURE
+
+ All values (except the bus numbers) are hexadecimal.
+
+I2C BUILD CONFIGURATION
+=======================
+
+NuttX Configuration Requirements
+--------------------------------
+The I2C tools requires the following in your NuttX configuration:
+
+1. Device-specific I2C support must be enabled. The I2C tool will call the
+ platform-specific function up_i2cinitialize() to get instances of the
+ I2C interface and the platform-specific function up_i2cuninitialize()
+ to discard instances of the I2C interface.
+
+ NOTE 1: The I2C interface is defined in include/nuttx/i2c.h.
+
+ NOTE 2: This I2C tool uses direct I2C device interfaces. As such, it
+ relies on internal OS interfaces that are not normally available to a
+ user-space program. As a result, the I2C tool cannot be used if a
+ NuttX is built as a protected, supervisor kernel (CONFIG_NUTTX_KERNEL).
+
+2. I2C driver configuration
+
+ The CONFIG_I2C_TRANSFER option must also be set in your NuttX
+ configuration. This configuration is the defconfig file in your
+ configuration directory that is copied to the NuttX top-level
+ directory as .config when NuttX is configured.
+
+ CONFIG_I2C_TRANSFER=y
+
+ NOTE: CONFIG_I2C_TRANSFER adds extra methods to the I2C interface.
+ Not all I2C interfaces support these extra methods. If your platform's
+ I2C driver does not support these extra methods, then you cannot use
+ the I2C tool unless you extend the support in your platform I2C
+ driver.
+
+3. Application configuration.
+
+ The path to the I2C tool directory must also be set in your NuttX
+ application configuration. This application configuration is the
+ appconfig file in your configuration directory that is copied to the
+ NuttX application directory as .config when NuttX is configured.
+
+ CONFIGURE_APPS += system/i2c
+
+I2C Tool Configuration Options
+------------------------------
+
+The default behavior of the I2C tool can be modified by the setting the
+options in the NuttX configuration. This configuration is the defconfig
+file in your configuration directory that is copied to the NuttX top-level
+directory as .config when NuttX is configured.
+
+ CONFIG_I2CTOOL_BUILTIN: Build the tools as an NSH built-in command
+ CONFIG_I2CTOOL_MINBUS: Smallest bus index supported by the hardware (default 0).
+ CONFIG_I2CTOOL_MAXBUS: Largest bus index supported by the hardware (default 3)
+ CONFIG_I2CTOOL_MINADDR: Minium device address (default: 0x03)
+ CONFIG_I2CTOOL_MAXADDR: Largest device address (default: 0x77)
+ CONFIG_I2CTOOL_MAXREGADDR: Largest register address (default: 0xff)
+ CONFIG_I2CTOOL_DEFFREQ: Default frequency (default: 4000000)
diff --git a/apps/system/i2c/i2c_bus.c b/apps/system/i2c/i2c_bus.c new file mode 100644 index 000000000..07e6d2da3 --- /dev/null +++ b/apps/system/i2c/i2c_bus.c @@ -0,0 +1,99 @@ +/**************************************************************************** + * apps/system/i2c/i2c_bus.c + * + * Copyright (C) 2011 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * 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. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <nuttx/i2c.h> + +#include "i2ctool.h" + +/**************************************************************************** + * Definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: i2ccmd_bus + ****************************************************************************/ + +int i2ccmd_bus(FAR struct i2ctool_s *i2ctool, int argc, char **argv) +{ + FAR struct i2c_dev_s *dev; + int i; + + i2ctool_printf(i2ctool, " BUS EXISTS?\n"); + for (i = CONFIG_I2CTOOL_MINBUS; i <= CONFIG_I2CTOOL_MAXBUS; i++) + { + dev = up_i2cinitialize(i); + if (dev) + { + i2ctool_printf(i2ctool, "Bus %d: YES\n", i); + (void)up_i2cuninitialize(dev); + } + else + { + i2ctool_printf(i2ctool, "Bus %d: NO\n", i); + } + } + + return OK; +} diff --git a/apps/system/i2c/i2c_common.c b/apps/system/i2c/i2c_common.c new file mode 100644 index 000000000..e7b27693a --- /dev/null +++ b/apps/system/i2c/i2c_common.c @@ -0,0 +1,216 @@ +/**************************************************************************** + * apps/system/i2c/i2c_common.c + * + * Copyright (C) 2011 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * 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. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <stdlib.h> + +#include "i2ctool.h" + +/**************************************************************************** + * Definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: common_args + ****************************************************************************/ + +int common_args(FAR struct i2ctool_s *i2ctool, FAR char **arg) +{ + FAR char *ptr = *arg; + long value; + int ret; + + if (ptr[0] != '-') + { + goto invalid_argument; + } + + switch (ptr[1]) + { + case 'a': + ret = arg_hex(arg, &value); + if (value < CONFIG_I2CTOOL_MINADDR || value > CONFIG_I2CTOOL_MAXADDR) + { + goto out_of_range; + } + + i2ctool->addr = (uint8_t) value; + return ret; + + case 'b': + ret = arg_decimal(arg, &value); + if (value < CONFIG_I2CTOOL_MINBUS || value > CONFIG_I2CTOOL_MAXBUS) + { + goto out_of_range; + } + + i2ctool->bus = (uint8_t) value; + return ret; + + case 'f': + ret = arg_decimal(arg, &value); + if (value == 0) + { + goto out_of_range; + } + + i2ctool->freq = value; + return ret; + + case 'i': + i2ctool->autoincr = true; + return 1; + + case 'j': + i2ctool->autoincr = false; + return 1; + + case 'n': + i2ctool->start = false; + return 1; + + case 'r': + ret = arg_hex(arg, &value); + if (value < 0 || value > CONFIG_I2CTOOL_MAXREGADDR) + { + goto out_of_range; + } + + i2ctool->regaddr = (uint8_t) value; + return ret; + + case 's': + i2ctool->start = true; + return 1; + + case 'w': + ret = arg_decimal(arg, &value); + if (value != 8 && value != 16) + { + goto out_of_range; + } + + i2ctool->width = (uint8_t) value; + return ret; + + default: + goto invalid_argument; + } + +invalid_argument: + i2ctool_printf(i2ctool, g_i2carginvalid, ptr); + return ERROR; + +out_of_range: + i2ctool_printf(i2ctool, g_i2cargrange, ptr); + return ERROR; +} + +/**************************************************************************** + * Name: arg_string + ****************************************************************************/ + +int arg_string(FAR char **arg, FAR char **value) +{ + FAR char *ptr = *arg; + + if (ptr[2] == '\0') + { + *value = arg[1]; + return 2; + } + else + { + *value = &ptr[2]; + return 1; + } +} + +/**************************************************************************** + * Name: arg_decimal + ****************************************************************************/ + +int arg_decimal(FAR char **arg, FAR long *value) +{ + FAR char *string; + int ret; + + ret = arg_string(arg, &string); + *value = strtol(string, NULL, 10); + return ret; +} + +/**************************************************************************** + * Name: arg_hex + ****************************************************************************/ + +int arg_hex(FAR char **arg, FAR long *value) +{ + FAR char *string; + int ret; + + ret = arg_string(arg, &string); + *value = strtol(string, NULL, 16); + return ret; +} diff --git a/apps/system/i2c/i2c_dev.c b/apps/system/i2c/i2c_dev.c new file mode 100644 index 000000000..f2b5a0e21 --- /dev/null +++ b/apps/system/i2c/i2c_dev.c @@ -0,0 +1,235 @@ +/**************************************************************************** + * apps/system/i2c/i2c_dev.c + * + * Copyright (C) 2011 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * 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. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <stdlib.h> + +#include <nuttx/i2c.h> + +#include "i2ctool.h" + +/**************************************************************************** + * Definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: i2ccmd_dev + ****************************************************************************/ + +int i2ccmd_dev(FAR struct i2ctool_s *i2ctool, int argc, char **argv) +{ + FAR struct i2c_dev_s *dev; + struct i2c_msg_s msg[2]; + FAR char *ptr; + union + { + uint16_t data16; + uint8_t data8; + } u; + + uint8_t regaddr; + long first; + long last; + int addr; + int nargs; + int argndx; + int ret; + int i; + int j; + + /* Parse any command line arguments */ + + for (argndx = 1; argndx < argc; ) + { + /* Break out of the look when the last option has been parsed */ + + ptr = argv[argndx]; + if (*ptr != '-') + { + break; + } + + /* Otherwise, check for common options */ + + nargs = common_args(i2ctool, &argv[argndx]); + if (nargs < 0) + { + return ERROR; + } + argndx += nargs; + } + + /* There should be exactly two more things on the command line: The first and + * last addresses to be probed. + */ + + if (argndx+1 < argc) + { + first = strtol(argv[argndx], NULL, 16); + last = strtol(argv[argndx+1], NULL, 16); + if (first < 0 || first > 0x7f || last < 0 || last > 0x7f || first > last) + { + i2ctool_printf(i2ctool, g_i2cargrange, argv[0]); + return ERROR; + } + + argndx += 2; + } + else + { + i2ctool_printf(i2ctool, g_i2cargrequired, argv[0]); + return ERROR; + } + + if (argndx != argc) + { + i2ctool_printf(i2ctool, g_i2ctoomanyargs, argv[0]); + return ERROR; + } + + /* Get a handle to the I2C bus */ + + dev = up_i2cinitialize(i2ctool->bus); + if (!dev) + { + i2ctool_printf(i2ctool, "Failed to get bus %d\n", i2ctool->bus); + return ERROR; + } + + /* Set the frequency and address (NOTE: Only 7-bit address supported now) */ + + I2C_SETFREQUENCY(dev, i2ctool->freq); + + /* Probe each address */ + + i2ctool_printf(i2ctool, " 0 1 2 3 4 5 6 7 8 9 a b c d e f\n"); + for (i = 0; i < 128; i += 16) + { + i2ctool_printf(i2ctool, "%02x: ", i); + for (j = 0; j < 16; j++) + { + /* Skip addresses that are out of the selected range */ + + addr = i+j; + if (addr < first || addr > last) + { + i2ctool_printf(i2ctool, " "); + continue; + } + + /* Set the I2C address */ + + I2C_SETADDRESS(dev, addr, 7); + + /* Set up data structures */ + + regaddr = 0; + + msg[0].addr = addr; + msg[0].flags = 0; + msg[0].buffer = ®addr; + msg[0].length = 1; + + msg[1].addr = addr; + msg[1].flags = I2C_M_READ; + if (i2ctool->width == 8) + { + msg[1].buffer = &u.data8; + msg[1].length = 1; + } + else + { + msg[1].buffer = (uint8_t*)&u.data16; + msg[1].length = 2; + } + + if (i2ctool->start) + { + ret = I2C_TRANSFER(dev, &msg[0], 1); + if (ret == OK) + { + ret = I2C_TRANSFER(dev, &msg[1], 1); + } + } + else + { + ret = I2C_TRANSFER(dev, msg, 2); + } + + if (ret == OK) + { + i2ctool_printf(i2ctool, "%02x ", addr); + } + else + { + i2ctool_printf(i2ctool, "-- "); + } + } + i2ctool_printf(i2ctool, "\n"); + i2ctool_flush(i2ctool); + } + + (void)up_i2cuninitialize(dev); + return OK; +} diff --git a/apps/system/i2c/i2c_get.c b/apps/system/i2c/i2c_get.c new file mode 100644 index 000000000..773f2c963 --- /dev/null +++ b/apps/system/i2c/i2c_get.c @@ -0,0 +1,257 @@ +/**************************************************************************** + * apps/system/i2c/i2c_get.c + * + * Copyright (C) 2011 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * 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. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <stdlib.h> + +#include <nuttx/i2c.h> + +#include "i2ctool.h" + +/**************************************************************************** + * Definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: i2ccmd_get + ****************************************************************************/ + +int i2ccmd_get(FAR struct i2ctool_s *i2ctool, int argc, FAR char **argv) +{ + FAR struct i2c_dev_s *dev; + FAR char *ptr; + uint16_t result; + uint8_t regaddr; + long repititions; + int nargs; + int argndx; + int ret; + int i; + + /* Parse any command line arguments */ + + for (argndx = 1; argndx < argc; ) + { + /* Break out of the look when the last option has been parsed */ + + ptr = argv[argndx]; + if (*ptr != '-') + { + break; + } + + /* Otherwise, check for common options */ + + nargs = common_args(i2ctool, &argv[argndx]); + if (nargs < 0) + { + return ERROR; + } + argndx += nargs; + } + + /* There may be one more thing on the command line: The repitition + * count. + */ + + repititions = 1; + if (argndx < argc) + { + repititions = strtol(argv[argndx], NULL, 16); + if (repititions < 1) + { + i2ctool_printf(i2ctool, g_i2cargrange, argv[0]); + return ERROR; + } + + argndx++; + } + + if (argndx != argc) + { + i2ctool_printf(i2ctool, g_i2ctoomanyargs, argv[0]); + return ERROR; + } + + /* Get a handle to the I2C bus */ + + dev = up_i2cinitialize(i2ctool->bus); + if (!dev) + { + i2ctool_printf(i2ctool, "Failed to get bus %d\n", i2ctool->bus); + return ERROR; + } + + /* Set the frequency and the address (NOTE: Only 7-bit address supported now) */ + + I2C_SETFREQUENCY(dev, i2ctool->freq); + I2C_SETADDRESS(dev, i2ctool->addr, 7); + + /* Loop for the requested number of repititions */ + + regaddr = i2ctool->regaddr; + ret = OK; + + for (i = 0; i < repititions; i++) + { + /* Read from the I2C bus */ + + ret = i2ctool_get(i2ctool, dev, regaddr, &result); + + /* Display the result */ + + if (ret == OK) + { + i2ctool_printf(i2ctool, "READ Bus: %d Addr: %02x Subaddr: %02x Value: ", + i2ctool->bus, i2ctool->addr, i2ctool->regaddr); + if (i2ctool->width == 8) + { + i2ctool_printf(i2ctool, "%02x\n", result); + } + else + { + i2ctool_printf(i2ctool, "%04x\n", result); + } + } + else + { + i2ctool_printf(i2ctool, g_i2cxfrerror, argv[0], -ret); + break; + } + + /* Auto-increment the address if so configured */ + + if (i2ctool->autoincr) + { + regaddr++; + } + } + + (void)up_i2cuninitialize(dev); + return ret; +} + +/**************************************************************************** + * Name: i2ctool_get + ****************************************************************************/ + +int i2ctool_get(FAR struct i2ctool_s *i2ctool, FAR struct i2c_dev_s *dev, + uint8_t regaddr, uint16_t *result) +{ + struct i2c_msg_s msg[2]; + union + { + uint16_t data16; + uint8_t data8; + } u; + int ret; + + /* Set up data structures */ + + msg[0].addr = i2ctool->addr; + msg[0].flags = 0; + msg[0].buffer = ®addr; + msg[0].length = 1; + + msg[1].addr = i2ctool->addr; + msg[1].flags = I2C_M_READ; + if (i2ctool->width == 8) + { + msg[1].buffer = &u.data8; + msg[1].length = 1; + } + else + { + msg[1].buffer = (uint8_t*)&u.data16; + msg[1].length = 2; + } + + if (i2ctool->start) + { + ret = I2C_TRANSFER(dev, &msg[0], 1); + if (ret== OK) + { + ret = I2C_TRANSFER(dev, &msg[1], 1); + } + } + else + { + ret = I2C_TRANSFER(dev, msg, 2); + } + + /* Return the result of the read operation */ + + if (ret == OK) + { + if (i2ctool->width == 8) + { + *result = (uint16_t)u.data8; + } + else + { + *result = u.data16; + } + } + return ret; +} diff --git a/apps/system/i2c/i2c_main.c b/apps/system/i2c/i2c_main.c new file mode 100644 index 000000000..2a0e5d626 --- /dev/null +++ b/apps/system/i2c/i2c_main.c @@ -0,0 +1,454 @@ +/**************************************************************************** + * apps/system/i2c/i2c_main.c + * + * Copyright (C) 2011 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * 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 Gregory Nutt 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. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <fcntl.h> +#include <string.h> +#include <stdarg.h> +#include <assert.h> +#include <errno.h> +#include <debug.h> + +#include "i2ctool.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +static int i2ccmd_help(FAR struct i2ctool_s *i2ctool, int argc, char **argv); +static int i2ccmd_unrecognized(FAR struct i2ctool_s *i2ctool, int argc, char **argv); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +struct i2ctool_s g_i2ctool; + +static const struct cmdmap_s g_i2ccmds[] = +{ + { "?", i2ccmd_help, "Show help ", NULL }, + { "bus", i2ccmd_bus, "List busses ", NULL }, + { "dev", i2ccmd_dev, "List devices ", "[OPTIONS] <first> <last>" }, + { "get", i2ccmd_get, "Read register ", "[OPTIONS] [<repititions>]" }, + { "help", i2ccmd_help, "Show help ", NULL }, + { "set", i2ccmd_set, "Write register", "[OPTIONS] <value> [<repititions>]" }, + { "verf", i2ccmd_verf, "Verify access ", "[OPTIONS] [<value>] [<repititions>]" }, + { NULL, NULL, NULL, NULL } +}; + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +/* Common, message formats */ + +const char g_i2cargrequired[] = "i2ctool: %s: missing required argument(s)\n"; +const char g_i2carginvalid[] = "i2ctool: %s: argument invalid\n"; +const char g_i2cargrange[] = "i2ctool: %s: value out of range\n"; +const char g_i2ccmdnotfound[] = "i2ctool: %s: command not found\n"; +const char g_i2ctoomanyargs[] = "i2ctool: %s: too many arguments\n"; +const char g_i2ccmdfailed[] = "i2ctool: %s: %s failed: %d\n"; +const char g_i2cxfrerror[] = "i2ctool: %s: Transfer failed: %d\n"; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: i2ccmd_help + ****************************************************************************/ + +static int i2ccmd_help(FAR struct i2ctool_s *i2ctool, int argc, char **argv) +{ + const struct cmdmap_s *ptr; + + i2ctool_printf(i2ctool, "Usage: i2c <cmd> [arguments]\n"); + i2ctool_printf(i2ctool, "Where <cmd> is one of:\n\n"); + for (ptr = g_i2ccmds; ptr->cmd; ptr++) + { + if (ptr->usage) + { + i2ctool_printf(i2ctool, " %s: %s %s\n", ptr->desc, ptr->cmd, ptr->usage); + } + else + { + i2ctool_printf(i2ctool, " %s: %s\n", ptr->desc, ptr->cmd); + } + } + + i2ctool_printf(i2ctool, "\nWhere common \"sticky\" OPTIONS include:\n"); + i2ctool_printf(i2ctool, " [-a addr] is the I2C device address (hex). " + "Default: %02x Current: %02x\n", + CONFIG_I2CTOOL_MINADDR, i2ctool->addr); + i2ctool_printf(i2ctool, " [-b bus] is the I2C bus number (decimal). " + "Default: %d Current: %d\n", + CONFIG_I2CTOOL_MINBUS, i2ctool->bus); + i2ctool_printf(i2ctool, " [-r regaddr] is the I2C device register address (hex). " + "Default: 00 Current: %02x\n", + i2ctool->regaddr); + i2ctool_printf(i2ctool, " [-w width] is the data width (8 or 16 decimal). " + "Default: 8 Current: %d\n", + i2ctool->width); + i2ctool_printf(i2ctool, " [-s|n], send/don't send start between command and data. " + "Default: -n Current: %s\n", + i2ctool->start ? "-s" : "-n"); + i2ctool_printf(i2ctool, " [-i|j], Auto increment|don't increment regaddr on repititions. " + "Default: NO Current: %s\n", + i2ctool->autoincr ? "YES" : "NO"); + i2ctool_printf(i2ctool, " [-f freq] I2C frequency. " + "Default: %d Current: %d\n", + CONFIG_I2CTOOL_DEFFREQ, i2ctool->freq); + i2ctool_printf(i2ctool, "\nNOTES:\n"); +#ifndef CONFIG_DISABLE_ENVIRON + i2ctool_printf(i2ctool, "o An environment variable like $PATH may be used for any argument.\n"); +#endif + i2ctool_printf(i2ctool, "o Arguments are \"sticky\". For example, once the I2C address is\n"); + i2ctool_printf(i2ctool, " specified, that address will be re-used until it is changed.\n"); + i2ctool_printf(i2ctool, "\nWARNING:\n"); + i2ctool_printf(i2ctool, "o The I2C dev command may have bad side effects on your I2C devices.\n"); + i2ctool_printf(i2ctool, " Use only at your own risk.\n"); + return OK; +} + +/**************************************************************************** + * Name: i2ccmd_unrecognized + ****************************************************************************/ + +static int i2ccmd_unrecognized(FAR struct i2ctool_s *i2ctool, int argc, char **argv) +{ + i2ctool_printf(i2ctool, g_i2ccmdnotfound, argv[0]); + return ERROR; +} + +/**************************************************************************** + * Name: i2c_execute + ****************************************************************************/ + +static int i2c_execute(FAR struct i2ctool_s *i2ctool, int argc, char *argv[]) +{ + const struct cmdmap_s *cmdmap; + const char *cmd; + cmd_t handler; + int ret; + + /* The form of argv is: + * + * argv[0]: The command name. This is argv[0] when the arguments + * are, finally, received by the command vtblr + * argv[1]: The beginning of argument (up to MAX_ARGUMENTS) + * argv[argc]: NULL terminating pointer + */ + + /* See if the command is one that we understand */ + + cmd = argv[0]; + handler = i2ccmd_unrecognized; + + for (cmdmap = g_i2ccmds; cmdmap->cmd; cmdmap++) + { + if (strcmp(cmdmap->cmd, cmd) == 0) + { + handler = cmdmap->handler; + break; + } + } + + ret = handler(i2ctool, argc, argv); + return ret; +} + +/**************************************************************************** + * Name: i2c_argument + ****************************************************************************/ + +FAR char *i2c_argument(FAR struct i2ctool_s *i2ctool, int argc, char *argv[], int *pindex) +{ + FAR char *arg; + int index = *pindex; + + /* If we are at the end of the arguments with nothing, then return NULL */ + + if (index >= argc) + { + return NULL; + } + + /* Get the return parameter */ + + arg = argv[index]; + *pindex = index + 1; + +#ifndef CONFIG_DISABLE_ENVIRON + /* Check for references to environment variables */ + + if (arg[0] == '$') + { + /* Return the value of the environment variable with this name */ + + FAR char *value = getenv(arg+1); + if (value) + { + return value; + } + else + { + return (FAR char*)""; + } + } +#endif + + /* Return the next argument. */ + + return arg; +} + +/**************************************************************************** + * Name: i2c_parse + ****************************************************************************/ + +int i2c_parse(FAR struct i2ctool_s *i2ctool, int argc, char *argv[]) +{ + FAR char *newargs[MAX_ARGUMENTS+2]; + FAR char *cmd; + int nargs; + int index; + + /* Parse out the command, skipping the first argument (the program name)*/ + + index = 1; + cmd = i2c_argument(i2ctool, argc, argv, &index); + + /* Check if any command was provided */ + + if (!cmd) + { + /* An empty line is not an error and an unprocessed command cannot + * generate an error, but neither should they change the last + * command status. + */ + + return i2ccmd_help(i2ctool, 0, NULL); + } + + /* Parse all of the arguments following the command name. */ + + newargs[0] = cmd; + for (nargs = 1; nargs <= MAX_ARGUMENTS; nargs++) + { + newargs[nargs] = i2c_argument(i2ctool, argc, argv, &index); + if (!newargs[nargs]) + { + break; + } + } + newargs[nargs] = NULL; + + /* Then execute the command */ + + return i2c_execute(i2ctool, nargs, newargs); +} + +/**************************************************************************** + * Name: i2c_setup + ****************************************************************************/ + +static inline int i2c_setup(FAR struct i2ctool_s *i2ctool) +{ + /* Initialize the output stream */ + +#ifdef CONFIG_I2CTOOL_OUTDEV + i2ctool->ss_outfd = open(CONFIG_I2CTOOL_OUTDEV, O_WRONLY); + if (i2ctool->ss_outfd < 0) + { + fprintf(stderr, g_i2ccmdfailed, "open", errno); + return ERROR; + } + + /* Create a standard C stream on the console device */ + + i2ctool->ss_outstream = fdopen(i2ctool->ss_outfd, "w"); + if (!i2ctool->ss_outstream) + { + fprintf(stderr, g_i2ccmdfailed, "fdopen", errno); + return ERROR; + } +#endif + + return OK; +} + +/**************************************************************************** + * Name: i2c_teardown + * + * Description: + * Close the output stream if it is not the standard output stream. + * + ****************************************************************************/ + +static void i2c_teardown(FAR struct i2ctool_s *i2ctool) +{ + fflush(OUTSTREAM(&g_i2ctool)); + +#ifdef CONFIG_I2CTOOL_OUTDEV + fclose(i2ctool->ss_outstream); +#endif +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: i2c_main + ****************************************************************************/ + +#ifdef CONFIG_I2CTOOL_BUILTIN +# define MAIN_NAME i2c_main +# define MAIN_NAME_STRING "i2c_main" +#else +# define MAIN_NAME user_start +# define MAIN_NAME_STRING "user_start" +#endif + +int MAIN_NAME(int argc, char *argv[]) +{ + /* Verify settings */ + + if (g_i2ctool.bus < CONFIG_I2CTOOL_MINBUS || g_i2ctool.bus > CONFIG_I2CTOOL_MAXBUS) + { + g_i2ctool.bus = CONFIG_I2CTOOL_MINBUS; + } + + if (g_i2ctool.addr < CONFIG_I2CTOOL_MINADDR || g_i2ctool.addr > CONFIG_I2CTOOL_MAXADDR) + { + g_i2ctool.addr = CONFIG_I2CTOOL_MINADDR; + } + + if (g_i2ctool.regaddr < CONFIG_I2CTOOL_MAXREGADDR) + { + g_i2ctool.regaddr = 0; + } + + if (g_i2ctool.width != 8 && g_i2ctool.width != 16) + { + g_i2ctool.width = 8; + } + + if (g_i2ctool.freq == 0) + { + g_i2ctool.freq = CONFIG_I2CTOOL_DEFFREQ; + } + + /* Parse process the command line */ + + i2c_setup(&g_i2ctool); + (void)i2c_parse(&g_i2ctool, argc, argv); + + i2ctool_flush(&g_i2ctool); + i2c_teardown(&g_i2ctool); + return OK; +} + +/**************************************************************************** + * Name: i2ctool_printf + * + * Description: + * Print a string to the currently selected stream. + * + ****************************************************************************/ + +int i2ctool_printf(FAR struct i2ctool_s *i2ctool, const char *fmt, ...) +{ + va_list ap; + int ret; + + va_start(ap, fmt); + ret = vfprintf(OUTSTREAM(i2ctool), fmt, ap); + va_end(ap); + + return ret; +} + +/**************************************************************************** + * Name: i2ctool_write + * + * Description: + * write a buffer to the currently selected stream. + * + ****************************************************************************/ + +ssize_t i2ctool_write(FAR struct i2ctool_s *i2ctool, FAR const void *buffer, size_t nbytes) +{ + ssize_t ret; + + /* Write the data to the output stream */ + + ret = fwrite(buffer, 1, nbytes, OUTSTREAM(i2ctool)); + if (ret < 0) + { + dbg("[%d] Failed to send buffer: %d\n", OUTFD(i2ctool), errno); + } + return ret; +} + +/**************************************************************************** + * Name: i2ctool_flush + * + * Description: + * Flush buffered I/O to the currently selected stream. + * + ****************************************************************************/ + +void i2ctool_flush(FAR struct i2ctool_s *i2ctool) +{ + fflush(OUTSTREAM(i2ctool)); +} diff --git a/apps/system/i2c/i2c_set.c b/apps/system/i2c/i2c_set.c new file mode 100644 index 000000000..66fd8c85a --- /dev/null +++ b/apps/system/i2c/i2c_set.c @@ -0,0 +1,275 @@ +/**************************************************************************** + * apps/system/i2c/i2c_set.c + * + * Copyright (C) 2011 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * 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. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <stdlib.h> + +#include <nuttx/i2c.h> + +#include "i2ctool.h" + +/**************************************************************************** + * Definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: i2ccmd_set + ****************************************************************************/ + +int i2ccmd_set(FAR struct i2ctool_s *i2ctool, int argc, FAR char **argv) +{ + FAR struct i2c_dev_s *dev; + FAR char *ptr; + uint8_t regaddr; + long value; + long repititions; + int nargs; + int argndx; + int ret; + int i; + + /* Parse any command line arguments */ + + for (argndx = 1; argndx < argc; ) + { + /* Break out of the look when the last option has been parsed */ + + ptr = argv[argndx]; + if (*ptr != '-') + { + break; + } + + /* Otherwise, check for common options */ + + nargs = common_args(i2ctool, &argv[argndx]); + if (nargs < 0) + { + return ERROR; + } + argndx += nargs; + } + + /* There must be at least one more thing on the command line: The value + * to be written. + */ + + if (argndx < argc) + { + value = strtol(argv[argndx], NULL, 16); + if (i2ctool->width == 8) + { + if (value < 0 || value > 255) + { + i2ctool_printf(i2ctool, g_i2cargrange, argv[0]); + return ERROR; + } + } + else if (value < 0 || value > 65535) + { + i2ctool_printf(i2ctool, g_i2cargrange, argv[0]); + return ERROR; + } + + argndx++; + } + else + { + i2ctool_printf(i2ctool, g_i2cargrequired, argv[0]); + return ERROR; + } + + /* There may be one more thing on the command line: The repitition + * count. + */ + + repititions = 1; + if (argndx < argc) + { + repititions = strtol(argv[argndx], NULL, 16); + if (repititions < 1) + { + i2ctool_printf(i2ctool, g_i2cargrange, argv[0]); + return ERROR; + } + + argndx++; + } + + if (argndx != argc) + { + i2ctool_printf(i2ctool, g_i2ctoomanyargs, argv[0]); + return ERROR; + } + + /* Get a handle to the I2C bus */ + + dev = up_i2cinitialize(i2ctool->bus); + if (!dev) + { + i2ctool_printf(i2ctool, "Failed to get bus %d\n", i2ctool->bus); + return ERROR; + } + + /* Set the frequency and the address (NOTE: Only 7-bit address supported now) */ + + I2C_SETFREQUENCY(dev, i2ctool->freq); + I2C_SETADDRESS(dev, i2ctool->addr, 7); + + /* Loop for the requested number of repititions */ + + regaddr = i2ctool->regaddr; + ret = OK; + + for (i = 0; i < repititions; i++) + { + /* Write to the I2C bus */ + + ret = i2ctool_set(i2ctool, dev, regaddr, (uint16_t)value); + + /* Display the result */ + + if (ret == OK) + { + i2ctool_printf(i2ctool, "WROTE Bus: %d Addr: %02x Subaddr: %02x Value: ", + i2ctool->bus, i2ctool->addr, i2ctool->regaddr); + if (i2ctool->width == 8) + { + i2ctool_printf(i2ctool, "%02x\n", (int)value); + } + else + { + i2ctool_printf(i2ctool, "%04x\n", (int)value); + } + } + else + { + i2ctool_printf(i2ctool, g_i2cxfrerror, argv[0], -ret); + break; + } + + /* Auto-increment the address if so configured */ + + if (i2ctool->autoincr) + { + regaddr++; + } + } + + (void)up_i2cuninitialize(dev); + return ret; +} + +/**************************************************************************** + * Name: i2ctool_set + ****************************************************************************/ + +int i2ctool_set(FAR struct i2ctool_s *i2ctool, FAR struct i2c_dev_s *dev, + uint8_t regaddr, uint16_t value) +{ + struct i2c_msg_s msg[2]; + union + { + uint16_t data16; + uint8_t data8; + } u; + int ret; + + /* Set up data structures */ + + msg[0].addr = i2ctool->addr; + msg[0].flags = 0; + msg[0].buffer = ®addr; + msg[0].length = 1; + + msg[1].addr = i2ctool->addr; + msg[1].flags = 0; + if (i2ctool->width == 8) + { + u.data8 = (uint8_t)value; + msg[1].buffer = &u.data8; + msg[1].length = 1; + } + else + { + u.data16 = value; + msg[1].buffer = (uint8_t*)&u.data16; + msg[1].length = 2; + } + + if (i2ctool->start) + { + ret = I2C_TRANSFER(dev, &msg[0], 1); + if (ret == OK) + { + ret = I2C_TRANSFER(dev, &msg[1], 1); + } + } + else + { + ret = I2C_TRANSFER(dev, msg, 2); + } + + return ret; +} diff --git a/apps/system/i2c/i2c_verf.c b/apps/system/i2c/i2c_verf.c new file mode 100644 index 000000000..0fa34b96e --- /dev/null +++ b/apps/system/i2c/i2c_verf.c @@ -0,0 +1,249 @@ +/**************************************************************************** + * apps/system/i2c/i2c_verf.c + * + * Copyright (C) 2011 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * 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. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <stdlib.h> + +#include <nuttx/i2c.h> + +#include "i2ctool.h" + +/**************************************************************************** + * Definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: i2ccmd_verf + ****************************************************************************/ + +int i2ccmd_verf(FAR struct i2ctool_s *i2ctool, int argc, FAR char **argv) +{ + FAR struct i2c_dev_s *dev; + FAR char *ptr; + uint16_t rdvalue; + uint8_t regaddr; + bool addrinaddr; + long wrvalue; + long repititions; + int nargs; + int argndx; + int ret; + int i; + + /* Parse any command line arguments */ + + for (argndx = 1; argndx < argc; ) + { + /* Break out of the look when the last option has been parsed */ + + ptr = argv[argndx]; + if (*ptr != '-') + { + break; + } + + /* Otherwise, check for common options */ + + nargs = common_args(i2ctool, &argv[argndx]); + if (nargs < 0) + { + return ERROR; + } + argndx += nargs; + } + + /* The options may be followed by the optional wrvalue to be written. If omitted, then + * the register address will be used as the wrvalue, providing an address-in-address + * test. + */ + + addrinaddr = true; + wrvalue = 0; + + if (argndx < argc) + { + wrvalue = strtol(argv[argndx], NULL, 16); + if (i2ctool->width == 8) + { + if (wrvalue < 0 || wrvalue > 255) + { + i2ctool_printf(i2ctool, g_i2cargrange, argv[0]); + return ERROR; + } + } + else if (wrvalue < 0 || wrvalue > 65535) + { + i2ctool_printf(i2ctool, g_i2cargrange, argv[0]); + return ERROR; + } + + addrinaddr = false; + argndx++; + } + + /* There may be one more thing on the command line: The repitition + * count. + */ + + repititions = 1; + if (argndx < argc) + { + repititions = strtol(argv[argndx], NULL, 16); + if (repititions < 1) + { + i2ctool_printf(i2ctool, g_i2cargrange, argv[0]); + return ERROR; + } + + argndx++; + } + + if (argndx != argc) + { + i2ctool_printf(i2ctool, g_i2ctoomanyargs, argv[0]); + return ERROR; + } + + /* Get a handle to the I2C bus */ + + dev = up_i2cinitialize(i2ctool->bus); + if (!dev) + { + i2ctool_printf(i2ctool, "Failed to get bus %d\n", i2ctool->bus); + return ERROR; + } + + /* Set the frequency and the address (NOTE: Only 7-bit address supported now) */ + + I2C_SETFREQUENCY(dev, i2ctool->freq); + I2C_SETADDRESS(dev, i2ctool->addr, 7); + + /* Loop for the requested number of repititions */ + + regaddr = i2ctool->regaddr; + ret = OK; + + for (i = 0; i < repititions; i++) + { + /* If we are performing an address-in-address test, then use the register + * address as the value to write. + */ + + if (addrinaddr) + { + wrvalue = regaddr; + } + + /* Write to the I2C bus */ + + ret = i2ctool_set(i2ctool, dev, regaddr, (uint16_t)wrvalue); + if (ret == OK) + { + /* Read the value back from the I2C bus */ + + ret = i2ctool_get(i2ctool, dev, regaddr, &rdvalue); + } + + /* Display the result */ + + if (ret == OK) + { + i2ctool_printf(i2ctool, "VERIFY Bus: %d Addr: %02x Subaddr: %02x Wrote: ", + i2ctool->bus, i2ctool->addr, i2ctool->regaddr); + + if (i2ctool->width == 8) + { + i2ctool_printf(i2ctool, "%02x Read: %02x", (int)wrvalue, (int)rdvalue); + } + else + { + i2ctool_printf(i2ctool, "%04x Read: %04x", (int)wrvalue, (int)rdvalue); + } + + if (wrvalue != rdvalue) + { + i2ctool_printf(i2ctool, " <<< FAILURE\n"); + } + else + { + i2ctool_printf(i2ctool, "\n"); + } + } + else + { + i2ctool_printf(i2ctool, g_i2cxfrerror, argv[0], -ret); + break; + } + + /* Auto-increment the address if so configured */ + + if (i2ctool->autoincr) + { + regaddr++; + } + } + + (void)up_i2cuninitialize(dev); + return ret; +} diff --git a/apps/system/i2c/i2ctool.h b/apps/system/i2c/i2ctool.h new file mode 100644 index 000000000..9726f0083 --- /dev/null +++ b/apps/system/i2c/i2ctool.h @@ -0,0 +1,210 @@ +/**************************************************************************** + * apps/system/i2c/i2ctool.h + * + * Copyright (C) 2011 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * 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. + * + ****************************************************************************/ + +#ifndef __APPS_SYSTEM_I2C_I2CTOOLS_H +#define __APPS_SYSTEM_I2C_I2CTOOLS_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> +#include <nuttx/compiler.h> + +#include <sys/types.h> +#include <stdio.h> +#include <stdint.h> +#include <stdbool.h> +#include <errno.h> + +#include <nuttx/i2c.h> + +/**************************************************************************** + * Definitions + ****************************************************************************/ +/* Configuration ************************************************************/ +/* CONFIG_I2CTOOL_BUILTIN - Build the tools as an NSH built-in command + * CONFIG_I2CTOOL_MINBUS - Smallest bus index supported by the hardware (default 0). + * CONFIG_I2CTOOL_MAXBUS - Largest bus index supported by the hardware (default 3) + * CONFIG_I2CTOOL_MINADDR - Minium device address (default: 0x03) + * CONFIG_I2CTOOL_MAXADDR - Largest device address (default: 0x77) + * CONFIG_I2CTOOL_MAXREGADDR - Largest register address (default: 0xff) + * CONFIG_I2CTOOL_DEFFREQ - Default frequency (default: 4000000) + */ + +#ifndef CONFIG_I2C_TRANSFER +# error "CONFIG_I2C_TRANSFER is required in the configuration" +#endif + +#ifndef CONFIG_I2CTOOL_MINBUS +# define CONFIG_I2CTOOL_MINBUS 0 +#endif + +#ifndef CONFIG_I2CTOOL_MAXBUS +# define CONFIG_I2CTOOL_MAXBUS 3 +#endif + +#ifndef CONFIG_I2CTOOL_MINADDR +# define CONFIG_I2CTOOL_MINADDR 0x03 +#endif + +#ifndef CONFIG_I2CTOOL_MAXADDR +# define CONFIG_I2CTOOL_MAXADDR 0x77 +#endif + +#ifndef CONFIG_I2CTOOL_MAXREGADDR +# define CONFIG_I2CTOOL_MAXREGADDR 0xff +#endif + +#ifndef CONFIG_I2CTOOL_DEFFREQ +# define CONFIG_I2CTOOL_DEFFREQ 100000 +#endif + +/* This is the maximum number of arguments that will be accepted for a + * command. The only real limit is in the OS configuration that limits + * the number of parameters passed to a task. + */ + +#define MAX_ARGUMENTS (CONFIG_MAX_TASK_ARGS-1) + +/* Maximum size of one command line */ + +#define MAX_LINELEN 80 + +/* Are we using the NuttX console for I/O? Or some other character device? */ + +#ifdef CONFIG_I2CTOOL_INDEV +# define INFD(p) ((p)->ss_infd) +# define INSTREAM(p) ((p)->ss_instream) +#else +# define INFD(p) 0 +# define INSTREAM(p) stdin +#endif + +#ifdef CONFIG_I2CTOOL_OUTDEV +# define OUTFD(p) ((p)->ss_outfd) +# define OUTSTREAM(p) ((p)->ss_outstream) +#else +# define OUTFD(p) 1 +# define OUTSTREAM(p) stdout +#endif + +/* Output is via printf but can be changed using this macro */ + +#ifdef CONFIG_CPP_HAVE_VARARGS +# define i2c_output(v, fmt...) printf(v, ##fmt) +#else +# define i2c_output printf +#endif + +/**************************************************************************** + * Public Types + ****************************************************************************/ + +struct i2ctool_s +{ + /* Sticky options */ + + uint8_t addr; /* [-a addr] is the I2C device address */ + uint8_t bus; /* [-b bus] is the I2C bus number */ + uint8_t regaddr; /* [-r regaddr] is the I2C device register address */ + uint8_t width; /* [-w width] is the data width (8 or 16) */ + bool start; /* [-s|n], send|don't send start between command and data */ + bool autoincr; /* [-i|j], Auto increment|don't increment regaddr on repititions */ + uint32_t freq; /* [-f freq] I2C frequency */ + + /* Output streams */ + +#ifdef CONFIG_I2CTOOL_OUTDEV + int ss_outfd; /* Output file descriptor */ + FILE *ss_outstream; /* Output stream */ +#endif +}; + +typedef int (*cmd_t)(FAR struct i2ctool_s *i2ctool, int argc, FAR char **argv); + +struct cmdmap_s +{ + FAR const char *cmd; /* Name of the command */ + cmd_t handler; /* Function that handles the command */ + FAR const char *desc; /* Short description */ + FAR const char *usage; /* Usage instructions for 'help' command */ +}; + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +extern const char g_i2cargrequired[]; +extern const char g_i2carginvalid[]; +extern const char g_i2cargrange[]; +extern const char g_i2ccmdnotfound[]; +extern const char g_i2ctoomanyargs[]; +extern const char g_i2ccmdfailed[]; +extern const char g_i2cxfrerror[]; + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +/* Message handler */ + +ssize_t i2ctool_write(FAR struct i2ctool_s *i2ctool, FAR const void *buffer, size_t nbytes); +int i2ctool_printf(FAR struct i2ctool_s *i2ctool, const char *fmt, ...); +void i2ctool_flush(FAR struct i2ctool_s *i2ctool); + +/* Command handlers */ + +int i2ccmd_bus(FAR struct i2ctool_s *i2ctool, int argc, FAR char **argv); +int i2ccmd_dev(FAR struct i2ctool_s *i2ctool, int argc, FAR char **argv); +int i2ccmd_get(FAR struct i2ctool_s *i2ctool, int argc, FAR char **argv); +int i2ccmd_set(FAR struct i2ctool_s *i2ctool, int argc, FAR char **argv); +int i2ccmd_verf(FAR struct i2ctool_s *i2ctool, int argc, FAR char **argv); + +/* I2C access functions */ + +int i2ctool_get(FAR struct i2ctool_s *i2ctool, FAR struct i2c_dev_s *dev, + uint8_t addr, uint16_t *result); +int i2ctool_set(FAR struct i2ctool_s *i2ctool, FAR struct i2c_dev_s *dev, + uint8_t regaddr, uint16_t value); + +/* Common logic */ + +int common_args(FAR struct i2ctool_s *i2ctool, FAR char **arg); +int arg_string(FAR char **arg, FAR char **value); +int arg_decimal(FAR char **arg, FAR long *value); +int arg_hex(FAR char **arg, FAR long *value); + +#endif /* __APPS_SYSTEM_I2C_I2CTOOLS_H */ diff --git a/apps/system/install/Kconfig b/apps/system/install/Kconfig new file mode 100644 index 000000000..a48f67f20 --- /dev/null +++ b/apps/system/install/Kconfig @@ -0,0 +1,14 @@ +# +# For a description of the syntax of this configuration file, +# see misc/tools/kconfig-language.txt. +# + +config SYSTEM_INSTALL + bool "FLASH installation tool" + default n + ---help--- + Enable support for the FLASH installation tool. + +if SYSTEM_INSTALL +endif + diff --git a/apps/system/install/Makefile b/apps/system/install/Makefile new file mode 100755 index 000000000..4528ad44e --- /dev/null +++ b/apps/system/install/Makefile @@ -0,0 +1,114 @@ +############################################################################ +# apps/system/install/Makefile +# +# Copyright (C) 2011 Uros Platise. All rights reserved. +# Author: Uros Platise <uros.platise@isotel.eu> +# Gregory Nutt <spudmonkey@racsa.co.cr> +# +# 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. +# +############################################################################ + +# TODO, this makefile should run make under the app dirs, instead of +# sourcing the Make.defs! + +-include $(TOPDIR)/.config +-include $(TOPDIR)/Make.defs +include $(APPDIR)/Make.defs + +ifeq ($(WINTOOL),y) +INCDIROPT = -w +endif + +# Hello Application +# TODO: appname can be automatically extracted from the directory name + +APPNAME = install +PRIORITY = SCHED_PRIORITY_DEFAULT +STACKSIZE = 1024 + +ASRCS = +CSRCS = install.c + +AOBJS = $(ASRCS:.S=$(OBJEXT)) +COBJS = $(CSRCS:.c=$(OBJEXT)) + +SRCS = $(ASRCS) $(CSRCS) +OBJS = $(AOBJS) $(COBJS) + +ifeq ($(WINTOOL),y) + BIN = "${shell cygpath -w $(APPDIR)/libapps$(LIBEXT)}" +else + BIN = "$(APPDIR)/libapps$(LIBEXT)" +endif + +ROOTDEPPATH = --dep-path . + +# Common build + +VPATH = + +all: .built +.PHONY: context depend clean distclean + +$(AOBJS): %$(OBJEXT): %.S + $(call ASSEMBLE, $<, $@) + +$(COBJS): %$(OBJEXT): %.c + $(call COMPILE, $<, $@) + +.built: $(OBJS) + @( for obj in $(OBJS) ; do \ + $(call ARCHIVE, $(BIN), $${obj}); \ + done ; ) + @touch .built + +# Register application + +.context: + $(call REGISTER,$(APPNAME),$(PRIORITY),$(STACKSIZE),$(APPNAME)_main) + @touch $@ + +context: .context + +# Create dependencies + +.depend: Makefile $(SRCS) + @$(MKDEP) $(ROOTDEPPATH) $(CC) -- $(CFLAGS) -- $(SRCS) >Make.dep + @touch $@ + +depend: .depend + +clean: + @rm -f *.o *~ .*.swp .built + $(call CLEAN) + +distclean: clean + @rm -f .context Make.dep .depend + +-include Make.dep diff --git a/apps/system/install/README.txt b/apps/system/install/README.txt new file mode 100755 index 000000000..66e9b0939 --- /dev/null +++ b/apps/system/install/README.txt @@ -0,0 +1,26 @@ + +Install Program +=============== + + Source: NuttX + Author: Uros Platise + Date: 7. May 2011 + +This application installs XIP application by placing it directly into +the program memory (flash) area into free area and creates a start-up +script into selected directory (i.e. /usr/bin/progname). + +Usage: + install [--stack RequiredStackSpace] [--priority Priority] + source-file destination-directory + +If stackspace is not given default stack space of 4096 Bytes is used. +If priority is not given system default is used. + +Additional options: + + --remove destination-file i.e. install --remove /usr/bin/myapp + --force to replace existing installation + --start <page> install app at or after <page> + --margin <pages> leave some free space after the kernel + Default is 16 pages so kernel may grow. diff --git a/apps/system/install/install.c b/apps/system/install/install.c new file mode 100755 index 000000000..2f11c6434 --- /dev/null +++ b/apps/system/install/install.c @@ -0,0 +1,412 @@ +/**************************************************************************** + * apps/system/install/install.c + * + * Copyright (C) 2011 Uros Platise. All rights reserved. + * Author: Uros Platise <uros.platise@isotel.eu> + * + * 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 <nuttx/config.h> +#include <nuttx/progmem.h> +#include <sys/stat.h> + +#include <unistd.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <errno.h> + + +/**************************************************************************** + * Definitions + ****************************************************************************/ + +#define ACTION_INSTALL 0x01 +#define ACTION_REMOVE 0x00 +#define ACTION_REINSTALL 0x03 +#define ACTION_INSUFPARAM 0x80 + +#define INSTALL_PROGRAMBLOCKSIZE 1024 + + +/**************************************************************************** + * Private data + ****************************************************************************/ + +const char *install_help = + "Installs XIP program into flash and creates a start-up script in the\n" + "destination directory.\n\n" + "Usage:\t%s [options] source-file.xip destination-directory\n\n" + "Example:\n\t%s --stack 1024 /sdcard/demo.xip /usr/bin\n\n" + "Options:\n" + "\t--stack <required_stack_space>\n" + "\t--priority <priority>\n" + "\t--remove <dest-file>\tRemoves installed application\n" + "\t--force\t\t\tReplaces existing installation\n" + "\t--start <page>\t\tInstalls application at or after <page>\n" + "\t--margin <pages>\tLeave some free space after the kernel (default 16)\n"; + +const char *install_script_text = + "# XIP stacksize=%x priority=%x size=%x\n"; + +const char *install_script_exec = + "exec 0x%x\n"; + + +/**************************************************************************** + * Private functions + ****************************************************************************/ + +int install_getstartpage(int startpage, int pagemargin, int desiredsize) +{ + uint16_t page = 0, stpage = 0xFFFF; + uint16_t pagesize = 0; + int maxlen = -1; + int maxlen_start = 0xFFFF; + int status; + + for (status=0, page=0; status >= 0; page++) { + + status = up_progmem_ispageerased(page); + pagesize = up_progmem_pagesize(page); + + /* Is this beginning of new free space section */ + if (status == 0) { + if (stpage == 0xFFFF) stpage = page; + } + else if (status != 0) { + + if (stpage != 0xFFFF) { + + if ( (page - stpage) > maxlen) { + + if (maxlen==-1) { /* First time found sth? */ + stpage += pagemargin; + maxlen = 0; + } + + if(stpage < startpage) + stpage = startpage; + + if (page > stpage) { + maxlen = page - stpage; + maxlen_start = stpage; + } + + if (maxlen*pagesize >= desiredsize) { + /* printf("Found page at %d ... %d\n", stpage, page); */ + return maxlen_start*pagesize; + } + } + + stpage = 0xFFFF; + } + } + } + + /* Requested space is not available */ + + return -1; +} + + +int install_programflash(int startaddr, const char *source) +{ + int status; + int count; + int totalsize = 0; + char *buf; + FILE *fp; + + if ( (buf = malloc(INSTALL_PROGRAMBLOCKSIZE)) == NULL ) + return -errno; + + if ( (fp=fopen(source, "r")) ) { + do { + count = fread(buf, 1, INSTALL_PROGRAMBLOCKSIZE, fp); + + if ( (status = up_progmem_write(startaddr, buf, count)) < 0) { + totalsize = status; + break; + } + + startaddr += count; + totalsize += count; + } + while(count); + } + else totalsize = -errno; + + fclose(fp); + free(buf); + + return totalsize; +} + + +void install_getscriptname(char *scriptname, const char *progname, const char *destdir) +{ + const char * progonly; + + /* I.e. as /usr/bin */ + strcpy(scriptname, destdir); + + /* extract from i.e. /sdcard/demo -> /demo, together with / */ + progonly = strrchr(progname, '/'); + strcat(scriptname, progonly); +} + + +int install_getprogsize(const char *progname) +{ + struct stat fileinfo; + + if ( stat(progname, &fileinfo) < 0 ) + return -1; + + return fileinfo.st_size; +} + + +int install_alreadyexists(const char *scriptname) +{ + FILE *fp; + + if ( (fp=fopen(scriptname, "r"))==NULL ) + return 0; + + fclose(fp); + return 1; +} + + +int install_createscript(int addr, int stacksize, int progsize, + int priority, const char *scriptname) +{ + FILE *fp; + + if ( (fp=fopen(scriptname, "w+"))==NULL ) + return -errno; + + fprintf(fp, install_script_text, stacksize, priority, progsize); + fprintf(fp, install_script_exec, addr); + + fflush(fp); + fclose(fp); + + return 0; +} + + +int install_getlasthexvalue(FILE *fp, char delimiter) +{ + char buf[128]; + char *p; + + if (fgets(buf, 127, fp)) { + if ( (p = strrchr(buf, delimiter)) ) { + return strtol(p+1, NULL, 16); + } + } + return -1; +} + + +int install_remove(const char *scriptname) +{ + FILE *fp; + int progsize, addr, freedsize; + uint16_t page; + int status = 0; + + /* Parse script */ + + if ( (fp=fopen(scriptname, "r")) ) { + progsize = install_getlasthexvalue(fp,'='); + addr = install_getlasthexvalue(fp,' '); + freedsize = progsize; + } + else return -errno; + + fclose(fp); + + /* Remove pages */ + + if (progsize <= 0 || addr <= 0) + return -EIO; + + do { + if ((page = up_progmem_getpage(addr)) < 0) { + status = -page; + break; + } + + if ( up_progmem_erasepage(page) < 0) { + status = -page; + break; + } + + addr += up_progmem_pagesize(page); + progsize -= up_progmem_pagesize(page); + + } while(progsize > 0); + + if (status < 0) return status; + + /* Remove script file */ + + if (unlink(scriptname) < 0) return -errno; + + return freedsize; +} + + +/**************************************************************************** + * Start + ****************************************************************************/ + +int install_main(int argc, char *argv[]) +{ + int i; + int progsize; + int scrsta; + int stacksize = 4096; + int priority = SCHED_PRIORITY_DEFAULT; + int pagemargin = 16; + int startpage = 0; + int startaddr = 0; + int action = ACTION_INSTALL; + char scriptname[128]; + + /* Supported? */ + + if ( !up_progmem_isuniform() ) { + fprintf(stderr, "Error: install supports uniform organization only.\n"); + return -1; + } + + /* Parse arguments */ + + for (i=1; i<argc; i++) { + if (argv[i][0]=='-' && argv[i][1]=='-' && i<=argc) { + + if (strcmp(argv[i]+2, "stack")==0) { + stacksize = atoi(argv[++i]); + } + else if (strcmp(argv[i]+2, "priority")==0) { + priority = atoi(argv[++i]); + } + else if (strcmp(argv[i]+2, "start")==0) { + startpage = atoi(argv[++i]); + } + else if (strcmp(argv[i]+2, "margin")==0) { + pagemargin = atoi(argv[++i]); + } + else if (strcmp(argv[i]+2, "remove")==0) { + action = ACTION_REMOVE; + } + else if (strcmp(argv[i]+2, "force")==0) { + action = ACTION_REINSTALL; + } + else fprintf(stderr, "Unknown option: %s\n", argv[i]); + } + else break; + } + + /* Do the job */ + + switch(action & 1) { + + case ACTION_REMOVE: + if (i > argc-1) { + action = ACTION_INSUFPARAM; + break; /* are there sufficient parameters */ + } + if ( (scrsta=install_remove(argv[i])) < 0) { + fprintf(stderr, "Could not remove program: %s\n", strerror(-scrsta) ); + return -1; + } + printf("Removed %s and freed %d bytes\n", argv[i], scrsta); + return 0; + + + case ACTION_INSTALL: + if (i > argc-2) { + action = ACTION_INSUFPARAM; + break; /* are there sufficient parameters */ + } + + install_getscriptname(scriptname, argv[i], argv[i+1]); + + // script-exists? + if (install_alreadyexists(scriptname)==1) { + + if (action != ACTION_REINSTALL) { + fprintf(stderr, "Program with that name already exists.\n"); + return -EEXIST; + } + + if ( (scrsta=install_remove(scriptname)) < 0) { + fprintf(stderr, "Could not remove program: %s\n", strerror(-scrsta) ); + return -1; + } + + printf("Replacing %s\n", scriptname); + } + + startaddr = install_getstartpage(startpage, pagemargin, install_getprogsize(argv[i]) ); + if (startpage < 0) { + fprintf(stderr, "Not enough memory\n"); + return -ENOMEM; + } + + if ( (progsize = install_programflash(startaddr, argv[i])) <= 0) { + fprintf(stderr, "Error writing program memory: %s\n" + "Note: Flash pages are not released, so you may try again and program will be\n" + " written in other pages.\n", strerror(-progsize) ); + + return -EIO; + } + if ( (scrsta = install_createscript(startaddr, stacksize, progsize, + priority, scriptname)) < 0) { + fprintf(stderr, "Error writing program script at %s: %s\n", + argv[i+1], strerror(-scrsta) ); + return -EIO; + } + + printf("Installed application of size %d bytes to program memory [%xh - %xh].\n", + progsize, startaddr, startaddr + progsize); + + return 0; + } + + fprintf(stderr, install_help, argv[0], argv[0]); + return -1; +} diff --git a/apps/system/readline/Kconfig b/apps/system/readline/Kconfig new file mode 100644 index 000000000..2ed9cafd6 --- /dev/null +++ b/apps/system/readline/Kconfig @@ -0,0 +1,39 @@ +# +# For a description of the syntax of this configuration file, +# see misc/tools/kconfig-language.txt. +# + +config SYSTEM_READLINE + bool "readline() support" + default n + ---help--- + Enable support for the readline() function. + +if SYSTEM_READLINE +config READLINE_ECHO + bool "Echo character input" + default y + ---help--- + Echo received character input back via stdout. This is normal + behavior and should be selected unless the source of stdin input + already has local echo support or you need to suppress the back-channel + responses for any other reason. + +choice + prompt "Newline Options" + default EOL_IS_EITHER_CRLF + +config EOL_IS_CR + bool "EOL is CR" + +config EOL_IS_LF + bool "EOL is LF" + +config EOL_IS_BOTH_CRLF + bool "EOL is CR and LF" + +config EOL_IS_EITHER_CRLF + bool "EOL is CR or LF" + +endchoice +endif diff --git a/apps/system/readline/Makefile b/apps/system/readline/Makefile new file mode 100644 index 000000000..34fab7e81 --- /dev/null +++ b/apps/system/readline/Makefile @@ -0,0 +1,103 @@ +############################################################################ +# apps/system/readline/Makefile +# +# 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. +# +############################################################################ + +-include $(TOPDIR)/.config +-include $(TOPDIR)/Make.defs +include $(APPDIR)/Make.defs + +ifeq ($(WINTOOL),y) +INCDIROPT = -w +endif + +# The Readline Library + +ASRCS = +CSRCS = readline.c + +AOBJS = $(ASRCS:.S=$(OBJEXT)) +COBJS = $(CSRCS:.c=$(OBJEXT)) + +SRCS = $(ASRCS) $(CSRCS) +OBJS = $(AOBJS) $(COBJS) + +ifeq ($(WINTOOL),y) + BIN = "${shell cygpath -w $(APPDIR)/libapps$(LIBEXT)}" +else + BIN = "$(APPDIR)/libapps$(LIBEXT)" +endif + +ROOTDEPPATH = --dep-path . + +# Common build + +VPATH = + +all: .built +.PHONY: context depend clean distclean + +$(AOBJS): %$(OBJEXT): %.S + $(call ASSEMBLE, $<, $@) + +$(COBJS): %$(OBJEXT): %.c + $(call COMPILE, $<, $@) + +.built: $(OBJS) + @( for obj in $(OBJS) ; do \ + $(call ARCHIVE, $(BIN), $${obj}); \ + done ; ) + @touch .built + +# Context build phase target + +context: + +# Dependency build phase target + +.depend: Makefile $(SRCS) + @$(MKDEP) $(ROOTDEPPATH) $(CC) -- $(CFLAGS) -- $(SRCS) >Make.dep + @touch $@ + +depend: .depend + +# Housekeeping targets + +clean: + @rm -f *.o *~ .*.swp .built + $(call CLEAN) + +distclean: clean + @rm -f .context Make.dep .depend + +-include Make.dep diff --git a/apps/system/readline/readline.c b/apps/system/readline/readline.c new file mode 100644 index 000000000..f7fa6a635 --- /dev/null +++ b/apps/system/readline/readline.c @@ -0,0 +1,345 @@ +/**************************************************************************** + * lib/stdio/lib_fgets.c + * + * Copyright (C) 2007-2008, 2011-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. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <stdbool.h> +#include <stdio.h> +#include <unistd.h> +#include <string.h> +#include <ctype.h> +#include <errno.h> +#include <debug.h> + +#include <nuttx/ascii.h> +#include <nuttx/vt100.h> + +#include <apps/readline.h> + +/**************************************************************************** + * Definitions + ****************************************************************************/ +/* In some systems, the underlying serial logic may automatically echo + * characters back to the console. We will assume that that is not the case + & here + */ + +#define CONFIG_READLINE_ECHO 1 + +/* Some environments may return CR as end-of-line, others LF, and others + * both. The logic here assumes either but not both. + */ + +#undef CONFIG_EOL_IS_CR +#undef CONFIG_EOL_IS_LF +#undef CONFIG_EOL_IS_BOTH_CRLF +#define CONFIG_EOL_IS_EITHER_CRLF 1 + +/**************************************************************************** + * Private Type Declarations + ****************************************************************************/ + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +/**************************************************************************** + * Private Data + ****************************************************************************/ +/* <esc>[K is the VT100 command erases to the end of the line. */ + +static const char g_erasetoeol[] = VT100_CLEAREOL; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: readline_rawgetc + ****************************************************************************/ + +static inline int readline_rawgetc(int infd) +{ + char buffer; + ssize_t nread; + + nread = read(infd, &buffer, 1); + if (nread < 1) + { + /* Return EOF if the end of file (0) or error (-1) occurs */ + + return EOF; + } + return (int)buffer; +} + +/**************************************************************************** + * Name: readline_consoleputc + ****************************************************************************/ + +#ifdef CONFIG_READLINE_ECHO +static inline void readline_consoleputc(int ch, int outfd) +{ + char buffer = ch; + (void)write(outfd, &buffer, 1); +} +#endif + +/**************************************************************************** + * Name: readline_consolewrite + ****************************************************************************/ + +#ifdef CONFIG_READLINE_ECHO +static inline void readline_consolewrite(int outfd, FAR const char *buffer, size_t buflen) +{ + (void)write(outfd, buffer, buflen); +} +#endif + +/**************************************************************************** + * Global Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: readline + * + * readline() reads in at most one less than 'buflen' characters from + * 'instream' and stores them into the buffer pointed to by 'buf'. + * Characters are echoed on 'outstream'. Reading stops after an EOF or a + * newline. If a newline is read, it is stored into the buffer. A null + * terminator is stored after the last character in the buffer. + * + * This version of realine assumes that we are reading and writing to + * a VT100 console. This will not work well if 'instream' or 'outstream' + * corresponds to a raw byte steam. + * + * This function is inspired by the GNU readline but is an entirely + * different creature. + * + * Input Parameters: + * buf - The user allocated buffer to be filled. + * buflen - the size of the buffer. + * instream - The stream to read characters from + * outstream - The stream to each characters to. + * + * Returned values: + * On success, the (positive) number of bytes transferred is returned. + * A length of zero would indicate an end of file condition. On failure, + * a negated errno value is returned. + * + **************************************************************************/ + +ssize_t readline(FAR char *buf, int buflen, FILE *instream, FILE *outstream) +{ + int infd; + int outfd; + int escape; + int nch; + + /* Sanity checks */ + + if (!instream || !outstream || !buf || buflen < 1) + { + return -EINVAL; + } + + if (buflen < 2) + { + *buf = '\0'; + return 0; + } + + /* Extract the file descriptions. This is cheating (and horribly non- + * standard) + */ + + infd = instream->fs_filedes; + outfd = outstream->fs_filedes; + + /* <esc>[K is the VT100 command that erases to the end of the line. */ + +#ifdef CONFIG_READLINE_ECHO + readline_consolewrite(outfd, g_erasetoeol, sizeof(g_erasetoeol)); +#endif + + /* Read characters until we have a full line. On each the loop we must + * be assured that there are two free bytes in the line buffer: One for + * the next character and one for the null terminator. + */ + + escape = 0; + nch = 0; + + for(;;) + { + /* Get the next character */ + + int ch = readline_rawgetc(infd); + + /* Are we processing a VT100 escape sequence */ + + if (escape) + { + /* Yes, is it an <esc>[, 3 byte sequence */ + + if (ch != ASCII_LBRACKET || escape == 2) + { + /* We are finished with the escape sequence */ + + escape = 0; + ch = 'a'; + } + else + { + /* The next character is the end of a 3-byte sequence. + * NOTE: Some of the <esc>[ sequences are longer than + * 3-bytes, but I have not encountered any in normal use + * yet and, so, have not provided the decoding logic. + */ + + escape = 2; + } + } + + /* Check for backspace + * + * There are several notions of backspace, for an elaborate summary see + * http://www.ibb.net/~anne/keyboard.html. There is no clean solution. + * Here both DEL and backspace are treated like backspace here. The + * Unix/Linux screen terminal by default outputs DEL (0x7f) when the + * backspace key is pressed. + */ + + else if (ch == ASCII_BS || ch == ASCII_DEL) + { + /* Eliminate that last character in the buffer. */ + + if (nch > 0) + { + nch--; + +#ifdef CONFIG_READLINE_ECHO + /* Echo the backspace character on the console. Always output + * the backspace character because the VT100 terminal doesn't + * understand DEL properly. + */ + + readline_consoleputc(ASCII_BS, outfd); + readline_consolewrite(outfd, g_erasetoeol, sizeof(g_erasetoeol)); +#endif + } + } + + /* Check for the beginning of a VT100 escape sequence */ + + else if (ch == ASCII_ESC) + { + /* The next character is escaped */ + + escape = 1; + } + + /* Check for end-of-line. This is tricky only in that some + * environments may return CR as end-of-line, others LF, and + * others both. + */ + +#if defined(CONFIG_EOL_IS_LF) || defined(CONFIG_EOL_IS_BOTH_CRLF) + else if (ch == '\n') +#elif defined(CONFIG_EOL_IS_CR) + else if (ch == '\r') +#elif CONFIG_EOL_IS_EITHER_CRLF + else if (ch == '\n' || ch == '\r') +#endif + { + /* The newline is stored in the buffer along with the null + * terminator. + */ + + buf[nch++] = '\n'; + buf[nch] = '\0'; + +#ifdef CONFIG_READLINE_ECHO + /* Echo the newline to the console */ + + readline_consoleputc('\n', outfd); +#endif + return nch; + } + + /* Check for end-of-file */ + + else if (ch == EOF) + { + /* Terminate the line (which might be zero length) */ + + buf[nch] = '\0'; + return nch; + } + + /* Otherwise, check if the character is printable and, if so, put the + * character in the line buffer + */ + + else if (isprint(ch)) + { + buf[nch++] = ch; + +#ifdef CONFIG_READLINE_ECHO + /* Echo the character to the console */ + + readline_consoleputc(ch, outfd); +#endif + /* Check if there is room for another character and the line's + * null terminator. If not then we have to end the line now. + */ + + if (nch + 1 >= buflen) + { + buf[nch] = '\0'; + return nch; + } + } + } +} + |