summaryrefslogtreecommitdiff
path: root/apps/system
diff options
context:
space:
mode:
authorpatacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3>2011-08-29 23:06:54 +0000
committerpatacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3>2011-08-29 23:06:54 +0000
commit61e71aad8571ec108e00df46983f5c3252c32e7c (patch)
tree01cb8f6553b741bd989f25130b3a04bba23d9bfa /apps/system
parenta9327f5a8598ff8e9bce1a2a1db53e047ee8f01d (diff)
downloadnuttx-61e71aad8571ec108e00df46983f5c3252c32e7c.tar.gz
nuttx-61e71aad8571ec108e00df46983f5c3252c32e7c.tar.bz2
nuttx-61e71aad8571ec108e00df46983f5c3252c32e7c.zip
More I2C tool logic
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@3924 42af7a65-404d-4744-a932-0658087f49c3
Diffstat (limited to 'apps/system')
-rw-r--r--apps/system/i2c/Makefile2
-rw-r--r--apps/system/i2c/i2c_bus.c99
-rw-r--r--apps/system/i2c/i2c_common.c198
-rw-r--r--apps/system/i2c/i2c_dev.c (renamed from apps/system/i2c/i2c_detect.c)6
-rw-r--r--apps/system/i2c/i2c_dump.c2
-rw-r--r--apps/system/i2c/i2c_get.c32
-rw-r--r--apps/system/i2c/i2c_main.c174
-rw-r--r--apps/system/i2c/i2c_set.c32
-rw-r--r--apps/system/i2c/i2ctool.h91
9 files changed, 515 insertions, 121 deletions
diff --git a/apps/system/i2c/Makefile b/apps/system/i2c/Makefile
index d6c09cf13..02ca64aa3 100644
--- a/apps/system/i2c/Makefile
+++ b/apps/system/i2c/Makefile
@@ -40,7 +40,7 @@ include $(APPDIR)/Make.defs
# I2C tool
ASRCS =
-CSRCS = i2c_detect.c i2c_dump.c i2c_get.c i2c_main.c i2c_set.c
+CSRCS = i2c_bus.c i2c_common.c i2c_dev.c i2c_dump.c i2c_get.c i2c_main.c i2c_set.c
AOBJS = $(ASRCS:.S=$(OBJEXT))
diff --git a/apps/system/i2c/i2c_bus.c b/apps/system/i2c/i2c_bus.c
new file mode 100644
index 000000000..23bd83f58
--- /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: cmd_bus
+ ****************************************************************************/
+
+int cmd_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..2d7a9f171
--- /dev/null
+++ b/apps/system/i2c/i2c_common.c
@@ -0,0 +1,198 @@
+/****************************************************************************
+ * 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[0])
+ {
+ 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 '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_fmtarginvalid, arg);
+ return ERROR;
+
+out_of_range:
+ i2ctool_printf(i2ctool, g_fmtargrange, arg);
+ 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_detect.c b/apps/system/i2c/i2c_dev.c
index 30e0560aa..2458aec18 100644
--- a/apps/system/i2c/i2c_detect.c
+++ b/apps/system/i2c/i2c_dev.c
@@ -1,5 +1,5 @@
/****************************************************************************
- * apps/system/i2c/i2c_detect.c
+ * apps/system/i2c/i2c_dev.c
*
* Copyright (C) 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
@@ -70,10 +70,10 @@
****************************************************************************/
/****************************************************************************
- * Name: cmd_detect
+ * Name: cmd_dev
****************************************************************************/
-int cmd_detect(FAR void *handle, int argc, char **argv)
+int cmd_dev(FAR struct i2ctool_s *i2ctool, int argc, char **argv)
{
return OK;
}
diff --git a/apps/system/i2c/i2c_dump.c b/apps/system/i2c/i2c_dump.c
index e910c3bc1..44b3ff8de 100644
--- a/apps/system/i2c/i2c_dump.c
+++ b/apps/system/i2c/i2c_dump.c
@@ -73,7 +73,7 @@
* Name: cmd_dump
****************************************************************************/
-int cmd_dump(FAR void *handle, int argc, char **argv)
+int cmd_dump(FAR struct i2ctool_s *i2ctool, int argc, char **argv)
{
return OK;
}
diff --git a/apps/system/i2c/i2c_get.c b/apps/system/i2c/i2c_get.c
index 9e98705cc..4f07ab630 100644
--- a/apps/system/i2c/i2c_get.c
+++ b/apps/system/i2c/i2c_get.c
@@ -39,6 +39,8 @@
#include <nuttx/config.h>
+#include <nuttx/i2c.h>
+
#include "i2ctool.h"
/****************************************************************************
@@ -73,7 +75,35 @@
* Name: cmd_get
****************************************************************************/
-int cmd_get(FAR void *handle, int argc, char **argv)
+int cmd_get(FAR struct i2ctool_s *i2ctool, int argc, char **argv)
{
+ FAR struct i2c_dev_s *dev;
+ int nargs;
+ int i;
+
+ /* Parse any command line arguments */
+
+ for (i = 1; i < argc; )
+ {
+ nargs = common_args(i2ctool, &argv[i]);
+ if (nargs < 0)
+ {
+ return ERROR;
+ }
+ i += nargs;
+ }
+
+ /* 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;
+ }
+
+#warning "missing logic"
+
+ (void)up_i2cuninitialize(dev);
return OK;
}
diff --git a/apps/system/i2c/i2c_main.c b/apps/system/i2c/i2c_main.c
index 286319db5..971889758 100644
--- a/apps/system/i2c/i2c_main.c
+++ b/apps/system/i2c/i2c_main.c
@@ -56,44 +56,16 @@
* Pre-processor Definitions
****************************************************************************/
-/* 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
-
/****************************************************************************
* Private Types
****************************************************************************/
-struct i2ctool_s
-{
- /* Output streams */
-
-#ifdef CONFIG_I2CTOOL_OUTDEV
- int ss_outfd; /* Output file descriptor */
- FILE *ss_outstream; /* Output stream */
-#endif
-};
-
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
-static int cmd_help(FAR void *handle, int argc, char **argv);
-static int cmd_unrecognized(FAR void *handle, int argc, char **argv);
+static int cmd_help(FAR struct i2ctool_s *i2ctool, int argc, char **argv);
+static int cmd_unrecognized(FAR struct i2ctool_s *i2ctool, int argc, char **argv);
/****************************************************************************
* Private Data
@@ -103,12 +75,13 @@ struct i2ctool_s g_i2ctool;
static const struct cmdmap_s g_i2ccmds[] =
{
- { "?", cmd_help, 1, 1, NULL },
- { "detect", cmd_detect, 1, 1, NULL },
- { "dump", cmd_dump, 1, 1, NULL },
- { "get", cmd_get, 1, 1, NULL },
- { "help", cmd_help, 1, 1, NULL },
- { "set", cmd_set, 1, 1, NULL },
+ { "?", cmd_help, "Show help", NULL },
+ { "bus", cmd_bus, "List busses" "[OPTIONS]" },
+ { "dev", cmd_dev, "List devices", "[OPTIONS]" },
+ { "dump", cmd_dump, "Dump registers", "[OPTIONS]" },
+ { "get", cmd_get, "Read registers", "[OPTIONS]" },
+ { "help", cmd_help, "Show help", NULL },
+ { "set", cmd_set, "Write registers", "[OPTIONS]]" },
};
/****************************************************************************
@@ -138,33 +111,41 @@ const char g_fmtinternalerror[] = "i2ctool: %s: Internal error\n";
* Name: cmd_help
****************************************************************************/
-static int cmd_help(FAR void *handle, int argc, char **argv)
+static int cmd_help(FAR struct i2ctool_s *i2ctool, int argc, char **argv)
{
const struct cmdmap_s *ptr;
- i2ctool_printf(handle, "Usage: i2ctool <cmd> [arguments]\n");
- i2ctool_printf(handle, "Where <cmd> is one of:\n");
+ i2ctool_printf(i2ctool, "Usage: i2ctool <cmd> [arguments]\n");
+ i2ctool_printf(i2ctool, "Where <cmd> is one of:\n");
for (ptr = g_i2ccmds; ptr->cmd; ptr++)
{
if (ptr->usage)
{
- i2ctool_printf(handle, " %s %s\n", ptr->cmd, ptr->usage);
+ i2ctool_printf(i2ctool, "%s: %s %s\n", ptr->desc, ptr->cmd, ptr->usage);
}
else
{
- i2ctool_printf(handle, " %s\n", ptr->cmd);
+ i2ctool_printf(i2ctool, "%s: %s\n", ptr->desc, ptr->cmd);
}
}
- i2ctool_printf(handle, "NOTES:\n");
+ i2ctool_printf(i2ctool, "Where common OPTIONS include:\n");
+ i2ctool_printf(i2ctool, "[-a addr] is the I2C device address (hex). Default: %02x\n",
+ CONFIG_I2CTOOL_MINADDR);
+ i2ctool_printf(i2ctool, "[-b bus] is the I2C bus number (decimal). Default: %d\n",
+ CONFIG_I2CTOOL_MINBUS);
+ i2ctool_printf(i2ctool, "[-r regaddr] is the I2C device register address (hex). Default: 0\n");
+ i2ctool_printf(i2ctool, "[-w width] is the data width (8 or 16 decimal). Default: 8 \n");
+ i2ctool_printf(i2ctool, "[-s|n], send/don't send start between command and data. Default: -n\n");
+ i2ctool_printf(i2ctool, "\nNOTES:\n");
#ifndef CONFIG_DISABLE_ENVIRON
- i2ctool_printf(handle, "- An environment variable like $PATH may be used for any argument.\n");
+ i2ctool_printf(i2ctool, "o An environment variable like $PATH may be used for any argument.\n");
#endif
- i2ctool_printf(handle, "- Arguments are persistent. For example, once the I2C address is\n");
- i2ctool_printf(handle, " specified, that address will be re-used until it changes.\n");
- i2ctool_printf(handle, "WARNING:\n");
- i2ctool_printf(handle, "- The I2C detect command may have bad side effects on your I2C devices.\n");
- i2ctool_printf(handle, " Use only at your own risk.\n");
+ 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 changes.\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;
}
@@ -172,9 +153,9 @@ static int cmd_help(FAR void *handle, int argc, char **argv)
* Name: cmd_unrecognized
****************************************************************************/
-static int cmd_unrecognized(FAR void *handle, int argc, char **argv)
+static int cmd_unrecognized(FAR struct i2ctool_s *i2ctool, int argc, char **argv)
{
- i2ctool_printf(handle, g_fmtcmdnotfound, argv[0]);
+ i2ctool_printf(i2ctool, g_fmtcmdnotfound, argv[0]);
return ERROR;
}
@@ -182,11 +163,11 @@ static int cmd_unrecognized(FAR void *handle, int argc, char **argv)
* Name: i2c_execute
****************************************************************************/
-static int i2c_execute(FAR void *handle, int argc, char *argv[])
+static int i2c_execute(FAR struct i2ctool_s *i2ctool, int argc, char *argv[])
{
const struct cmdmap_s *cmdmap;
const char *cmd;
- cmd_t handler = cmd_unrecognized;
+ cmd_t handler;
int ret;
/* The form of argv is:
@@ -197,46 +178,21 @@ static int i2c_execute(FAR void *handle, int argc, char *argv[])
* argv[argc]: NULL terminating pointer
*/
- cmd = argv[0];
-
/* See if the command is one that we understand */
+ cmd = argv[0];
+ handler = cmd_unrecognized;
+
for (cmdmap = g_i2ccmds; cmdmap->cmd; cmdmap++)
{
if (strcmp(cmdmap->cmd, cmd) == 0)
{
- /* Check if a valid number of arguments was provided. We
- * do this simple, imperfect checking here so that it does
- * not have to be performed in each command.
- */
-
- if (argc < cmdmap->minargs)
- {
- /* Fewer than the minimum number were provided */
-
- i2ctool_printf(handle, g_fmtargrequired, cmd);
- return ERROR;
- }
- else if (argc > cmdmap->maxargs)
- {
- /* More than the maximum number were provided */
-
- i2ctool_printf(handle, g_fmttoomanyargs, cmd);
- return ERROR;
- }
- else
- {
- /* A valid number of arguments were provided (this does
- * not mean they are right).
- */
-
- handler = cmdmap->handler;
- break;
- }
+ handler = cmdmap->handler;
+ break;
}
}
- ret = handler(handle, argc, argv);
+ ret = handler(i2ctool, argc, argv);
return ret;
}
@@ -244,7 +200,7 @@ static int i2c_execute(FAR void *handle, int argc, char *argv[])
* Name: i2c_argument
****************************************************************************/
-FAR char *i2c_argument(FAR void *handle, int argc, char *argv[], int *pindex)
+FAR char *i2c_argument(FAR struct i2ctool_s *i2ctool, int argc, char *argv[], int *pindex)
{
FAR char *arg;
int index = *pindex;
@@ -289,7 +245,7 @@ FAR char *i2c_argument(FAR void *handle, int argc, char *argv[], int *pindex)
* Name: i2c_parse
****************************************************************************/
-int i2c_parse(FAR void *handle, int argc, char *argv[])
+int i2c_parse(FAR struct i2ctool_s *i2ctool, int argc, char *argv[])
{
FAR char *newargs[MAX_ARGUMENTS+1];
FAR char *cmd;
@@ -303,7 +259,7 @@ int i2c_parse(FAR void *handle, int argc, char *argv[])
/* Parse out the command, skipping the first argument (the program name)*/
index = 1;
- cmd = i2c_argument(handle, argc, argv, &index);
+ cmd = i2c_argument(i2ctool, argc, argv, &index);
/* Check if any command was provided */
@@ -314,7 +270,7 @@ int i2c_parse(FAR void *handle, int argc, char *argv[])
* command status.
*/
- return cmd_help(handle, 0, NULL);
+ return cmd_help(i2ctool, 0, NULL);
}
/* Parse all of the arguments following the command name. */
@@ -322,7 +278,7 @@ int i2c_parse(FAR void *handle, int argc, char *argv[])
newargs[0] = cmd;
for (nargs = 1; nargs < MAX_ARGUMENTS; nargs++)
{
- newargs[nargs] = i2c_argument(handle, argc, argv, &index);
+ newargs[nargs] = i2c_argument(i2ctool, argc, argv, &index);
if (!newargs[nargs])
{
break;
@@ -332,7 +288,7 @@ int i2c_parse(FAR void *handle, int argc, char *argv[])
/* Then execute the command */
- return i2c_execute(handle, nargs, newargs);
+ return i2c_execute(i2ctool, nargs, newargs);
}
/****************************************************************************
@@ -399,10 +355,32 @@ static void i2c_teardown(void)
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;
+ }
+
/* Parse process the command line */
i2c_setup();
- (void)i2c_parse((FAR void *)&g_i2ctool, argc, argv);
+ (void)i2c_parse(&g_i2ctool, argc, argv);
i2c_teardown();
return OK;
@@ -416,16 +394,13 @@ int MAIN_NAME(int argc, char *argv[])
*
****************************************************************************/
-int i2ctool_printf(FAR void *handle, const char *fmt, ...)
+int i2ctool_printf(FAR struct i2ctool_s *i2ctool, const char *fmt, ...)
{
-#ifdef CONFIG_I2CTOOL_OUTDEV
- FAR struct i2ctool_s *pstate = (FAR struct i2ctool_s *)handle;
-#endif
va_list ap;
int ret;
va_start(ap, fmt);
- ret = vfprintf(OUTSTREAM(pstate), fmt, ap);
+ ret = vfprintf(OUTSTREAM(i2ctool), fmt, ap);
va_end(ap);
return ret;
@@ -439,19 +414,16 @@ int i2ctool_printf(FAR void *handle, const char *fmt, ...)
*
****************************************************************************/
-ssize_t i2ctool_write(FAR void *handle, FAR const void *buffer, size_t nbytes)
+ssize_t i2ctool_write(FAR struct i2ctool_s *i2ctool, FAR const void *buffer, size_t nbytes)
{
-#ifdef CONFIG_I2CTOOL_OUTDEV
- FAR struct i2ctool_s *pstate = (FAR struct i2ctool_s *)handle;
-#endif
ssize_t ret;
/* Write the data to the output stream */
- ret = fwrite(buffer, 1, nbytes, OUTSTREAM(pstate));
+ ret = fwrite(buffer, 1, nbytes, OUTSTREAM(i2ctool));
if (ret < 0)
{
- dbg("[%d] Failed to send buffer: %d\n", OUTFD(pstate), errno);
+ dbg("[%d] Failed to send buffer: %d\n", OUTFD(i2ctool), errno);
}
return ret;
}
diff --git a/apps/system/i2c/i2c_set.c b/apps/system/i2c/i2c_set.c
index 501eca9c5..1d276fd82 100644
--- a/apps/system/i2c/i2c_set.c
+++ b/apps/system/i2c/i2c_set.c
@@ -39,6 +39,8 @@
#include <nuttx/config.h>
+#include <nuttx/i2c.h>
+
#include "i2ctool.h"
/****************************************************************************
@@ -73,7 +75,35 @@
* Name: cmd_set
****************************************************************************/
-int cmd_set(FAR void *handle, int argc, char **argv)
+int cmd_set(FAR struct i2ctool_s *i2ctool, int argc, char **argv)
{
+ FAR struct i2c_dev_s *dev;
+ int nargs;
+ int i;
+
+ /* Parse any command line arguments */
+
+ for (i = 1; i < argc; )
+ {
+ nargs = common_args(i2ctool, &argv[i]);
+ if (nargs < 0)
+ {
+ return ERROR;
+ }
+ i += nargs;
+ }
+
+ /* 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;
+ }
+
+#warning "missing logic"
+
+ (void)up_i2cuninitialize(dev);
return OK;
}
diff --git a/apps/system/i2c/i2ctool.h b/apps/system/i2c/i2ctool.h
index 36726c7dc..dde1b7a13 100644
--- a/apps/system/i2c/i2ctool.h
+++ b/apps/system/i2c/i2ctool.h
@@ -52,8 +52,30 @@
/****************************************************************************
* Definitions
****************************************************************************/
+/* Configuration ************************************************************/
+#ifndef CONFIG_I2CTOOL_MINBUS
+# define CONFIG_I2CTOOL_MINBUS 0
+#endif
+
+#ifndef CONFIG_I2CTOOL_MAXBUS
+# define CONFIG_I2CTOOL_MAXBUS 3
+#endif
-/* This is the maximum number of arguments that will be accepted for a command */
+#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
+
+/* This is the maximum number of arguments that will be accepted for a
+ * command
+ */
#define MAX_ARGUMENTS 6
@@ -61,6 +83,24 @@
#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
@@ -73,15 +113,32 @@
* Public Types
****************************************************************************/
-typedef int (*cmd_t)(FAR void *handle, int argc, char **argv);
+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 */
+
+ /* 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, char **argv);
struct cmdmap_s
{
- const char *cmd; /* Name of the command */
- cmd_t handler; /* Function that handles the command */
- uint8_t minargs; /* Minimum number of arguments (including command) */
- uint8_t maxargs; /* Maximum number of arguments (including command) */
- const char *usage; /* Usage instructions for 'help' command */
+ 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 */
};
/****************************************************************************
@@ -105,14 +162,22 @@ extern const char g_fmtinternalerror[];
/* Message handler */
-ssize_t i2ctool_write(FAR void *handle, FAR const void *buffer, size_t nbytes);
-int i2ctool_printf(FAR void *handle, const char *fmt, ...);
+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, ...);
/* Command handlers */
-extern int cmd_detect(FAR void *handle, int argc, char **argv);
-extern int cmd_dump(FAR void *handle, int argc, char **argv);
-extern int cmd_get(FAR void *handle, int argc, char **argv);
-extern int cmd_set(FAR void *handle, int argc, char **argv);
+int cmd_bus(FAR struct i2ctool_s *i2ctool, int argc, char **argv);
+int cmd_dev(FAR struct i2ctool_s *i2ctool, int argc, char **argv);
+int cmd_dump(FAR struct i2ctool_s *i2ctool, int argc, char **argv);
+int cmd_get(FAR struct i2ctool_s *i2ctool, int argc, char **argv);
+int cmd_set(FAR struct i2ctool_s *i2ctool, int argc, char **argv);
+
+/* 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 */