First cut at I2C tool
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@3927 42af7a65-404d-4744-a932-0658087f49c3
This commit is contained in:
parent
e1c666abbc
commit
74ae84aa22
@ -40,7 +40,7 @@ include $(APPDIR)/Make.defs
|
||||
# I2C tool
|
||||
|
||||
ASRCS =
|
||||
CSRCS = i2c_bus.c i2c_common.c i2c_dev.c i2c_dump.c i2c_get.c i2c_main.c i2c_set.c
|
||||
CSRCS = i2c_bus.c i2c_common.c i2c_dev.c i2c_get.c i2c_main.c i2c_set.c
|
||||
|
||||
|
||||
AOBJS = $(ASRCS:.S=$(OBJEXT))
|
||||
@ -58,6 +58,10 @@ endif
|
||||
ROOTDEPPATH = --dep-path .
|
||||
VPATH =
|
||||
|
||||
APPNAME = i2c
|
||||
PRIORITY = SCHED_PRIORITY_DEFAULT
|
||||
STACKSIZE = 2048
|
||||
|
||||
# Build targets
|
||||
|
||||
all: .built
|
||||
@ -76,6 +80,8 @@ $(COBJS): %$(OBJEXT): %.c
|
||||
@touch .built
|
||||
|
||||
context:
|
||||
$(call REGISTER,$(APPNAME),$(PRIORITY),$(STACKSIZE),$(APPNAME)_main)
|
||||
@touch $@
|
||||
|
||||
.depend: Makefile $(SRCS)
|
||||
@$(MKDEP) $(ROOTDEPPATH) \
|
||||
|
@ -108,6 +108,16 @@ int common_args(FAR struct i2ctool_s *i2ctool, FAR char **arg)
|
||||
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 'n':
|
||||
i2ctool->start = false;
|
||||
return 1;
|
||||
@ -141,11 +151,11 @@ int common_args(FAR struct i2ctool_s *i2ctool, FAR char **arg)
|
||||
}
|
||||
|
||||
invalid_argument:
|
||||
i2ctool_printf(i2ctool, g_fmtarginvalid, arg);
|
||||
i2ctool_printf(i2ctool, g_i2carginvalid, arg);
|
||||
return ERROR;
|
||||
|
||||
out_of_range:
|
||||
i2ctool_printf(i2ctool, g_fmtargrange, arg);
|
||||
i2ctool_printf(i2ctool, g_i2cargrange, arg);
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
|
@ -39,6 +39,10 @@
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <nuttx/i2c.h>
|
||||
|
||||
#include "i2ctool.h"
|
||||
|
||||
/****************************************************************************
|
||||
@ -75,5 +79,156 @@
|
||||
|
||||
int cmd_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[2].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");
|
||||
}
|
||||
|
||||
(void)up_i2cuninitialize(dev);
|
||||
return OK;
|
||||
}
|
||||
|
@ -1,79 +0,0 @@
|
||||
/****************************************************************************
|
||||
* apps/system/i2c/i2c_dump.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 "i2ctool.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Types
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: cmd_dump
|
||||
****************************************************************************/
|
||||
|
||||
int cmd_dump(FAR struct i2ctool_s *i2ctool, int argc, char **argv)
|
||||
{
|
||||
return OK;
|
||||
}
|
@ -75,10 +75,18 @@
|
||||
* Name: cmd_get
|
||||
****************************************************************************/
|
||||
|
||||
int cmd_get(FAR struct i2ctool_s *i2ctool, int argc, char **argv)
|
||||
int cmd_get(FAR struct i2ctool_s *i2ctool, int argc, FAR char **argv)
|
||||
{
|
||||
FAR struct i2c_dev_s *dev;
|
||||
struct i2c_msg_s msg[2];
|
||||
union
|
||||
{
|
||||
uint16_t data16;
|
||||
uint8_t data8;
|
||||
} u;
|
||||
|
||||
int nargs;
|
||||
int ret;
|
||||
int i;
|
||||
|
||||
/* Parse any command line arguments */
|
||||
@ -102,8 +110,71 @@ int cmd_get(FAR struct i2ctool_s *i2ctool, int argc, char **argv)
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
#warning "missing logic"
|
||||
/* Set the frequency and address (NOTE: Only 7-bit address supported now) */
|
||||
|
||||
I2C_SETFREQUENCY(dev, i2ctool->freq);
|
||||
I2C_SETADDRESS(dev, i2ctool->addr, 7);
|
||||
|
||||
/* Set up data structures */
|
||||
|
||||
msg[0].addr = i2ctool->addr;
|
||||
msg[0].flags = 0;
|
||||
msg[0].buffer = &i2ctool->regaddr;
|
||||
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 < 0)
|
||||
{
|
||||
i2ctool_printf(i2ctool, g_i2cxfrerror, argv[0], -ret);
|
||||
goto errout;
|
||||
}
|
||||
ret = I2C_TRANSFER(dev, &msg[1], 1);
|
||||
if (ret < 0)
|
||||
{
|
||||
i2ctool_printf(i2ctool, g_i2cxfrerror, argv[0], -ret);
|
||||
goto errout;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ret = I2C_TRANSFER(dev, msg, 2);
|
||||
if (ret < 0)
|
||||
{
|
||||
goto errout;
|
||||
}
|
||||
}
|
||||
|
||||
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", u.data8);
|
||||
}
|
||||
else
|
||||
{
|
||||
i2ctool_printf(i2ctool, "%04x\n", u.data16);
|
||||
}
|
||||
|
||||
(void)up_i2cuninitialize(dev);
|
||||
return OK;
|
||||
|
||||
errout:
|
||||
i2ctool_printf(i2ctool, g_i2cxfrerror, argv[0], -ret);
|
||||
(void)up_i2cuninitialize(dev);
|
||||
return ERROR;
|
||||
}
|
||||
|
@ -77,11 +77,10 @@ static const struct cmdmap_s g_i2ccmds[] =
|
||||
{
|
||||
{ "?", 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]" },
|
||||
{ "dev", cmd_dev, "List devices", "[OPTIONS] <first> <last>" },
|
||||
{ "get", cmd_get, "Read register", "[OPTIONS]" },
|
||||
{ "help", cmd_help, "Show help", NULL },
|
||||
{ "set", cmd_set, "Write registers", "[OPTIONS]]" },
|
||||
{ "set", cmd_set, "Write register", "[OPTIONS] <value>" },
|
||||
};
|
||||
|
||||
/****************************************************************************
|
||||
@ -90,18 +89,13 @@ static const struct cmdmap_s g_i2ccmds[] =
|
||||
|
||||
/* Common, message formats */
|
||||
|
||||
const char g_syntax[] = "i2ctool: %s: syntax error\n";
|
||||
const char g_fmtargrequired[] = "i2ctool: %s: missing required argument(s)\n";
|
||||
const char g_fmtarginvalid[] = "i2ctool: %s: argument invalid\n";
|
||||
const char g_fmtargrange[] = "i2ctool: %s: value out of range\n";
|
||||
const char g_fmtcmdnotfound[] = "i2ctool: %s: command not found\n";
|
||||
const char g_fmtnosuch[] = "i2ctool: %s: no such %s: %s\n";
|
||||
const char g_fmttoomanyargs[] = "i2ctool: %s: too many arguments\n";
|
||||
const char g_fmtdeepnesting[] = "i2ctool: %s: nesting too deep\n";
|
||||
const char g_fmtcontext[] = "i2ctool: %s: not valid in this context\n";
|
||||
const char g_fmtcmdfailed[] = "i2ctool: %s: %s failed: %d\n";
|
||||
const char g_fmtcmdoutofmemory[] = "i2ctool: %s: out of memory\n";
|
||||
const char g_fmtinternalerror[] = "i2ctool: %s: Internal error\n";
|
||||
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
|
||||
@ -129,14 +123,25 @@ static int cmd_help(FAR struct i2ctool_s *i2ctool, int argc, char **argv)
|
||||
}
|
||||
}
|
||||
|
||||
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, "Where 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, "[-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");
|
||||
@ -155,7 +160,7 @@ 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)
|
||||
{
|
||||
i2ctool_printf(i2ctool, g_fmtcmdnotfound, argv[0]);
|
||||
i2ctool_printf(i2ctool, g_i2ccmdnotfound, argv[0]);
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
@ -303,7 +308,7 @@ static inline int i2c_setup(void)
|
||||
g_i2ctool.ss_outfd = open(CONFIG_I2CTOOL_OUTDEV, O_WRONLY);
|
||||
if (g_i2ctool.ss_outfd < 0)
|
||||
{
|
||||
fprintf(stderr, g_fmtcmdfailed, "open", errno);
|
||||
fprintf(stderr, g_i2ccmdfailed, "open", errno);
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
@ -312,7 +317,7 @@ static inline int i2c_setup(void)
|
||||
g_i2ctool.ss_outstream = fdopen(g_i2ctool.ss_outfd, "w");
|
||||
if (!g_i2ctool.ss_outstream)
|
||||
{
|
||||
fprintf(stderr, g_fmtcmdfailed, "fdopen", errno);
|
||||
fprintf(stderr, g_i2ccmdfailed, "fdopen", errno);
|
||||
return ERROR;
|
||||
}
|
||||
#endif
|
||||
@ -377,6 +382,11 @@ int MAIN_NAME(int argc, char *argv[])
|
||||
g_i2ctool.width = 8;
|
||||
}
|
||||
|
||||
if (g_i2ctool.freq == 0)
|
||||
{
|
||||
g_i2ctool.freq = CONFIG_I2CTOOL_DEFFREQ;
|
||||
}
|
||||
|
||||
/* Parse process the command line */
|
||||
|
||||
i2c_setup();
|
||||
|
@ -39,6 +39,8 @@
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <nuttx/i2c.h>
|
||||
|
||||
#include "i2ctool.h"
|
||||
@ -75,22 +77,77 @@
|
||||
* Name: cmd_set
|
||||
****************************************************************************/
|
||||
|
||||
int cmd_set(FAR struct i2ctool_s *i2ctool, int argc, char **argv)
|
||||
int cmd_set(FAR struct i2ctool_s *i2ctool, int argc, FAR 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;
|
||||
|
||||
long value;
|
||||
int nargs;
|
||||
int i;
|
||||
int argndx;
|
||||
int ret;
|
||||
|
||||
/* Parse any command line arguments */
|
||||
|
||||
for (i = 1; i < argc; )
|
||||
for (argndx = 1; argndx < argc; )
|
||||
{
|
||||
nargs = common_args(i2ctool, &argv[i]);
|
||||
/* 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;
|
||||
}
|
||||
i += nargs;
|
||||
argndx += nargs;
|
||||
}
|
||||
|
||||
/* There should be exactly 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;
|
||||
}
|
||||
|
||||
if (argndx != argc)
|
||||
{
|
||||
i2ctool_printf(i2ctool, g_i2ctoomanyargs, argv[0]);
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
/* Get a handle to the I2C bus */
|
||||
@ -102,8 +159,73 @@ int cmd_set(FAR struct i2ctool_s *i2ctool, int argc, char **argv)
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
#warning "missing logic"
|
||||
/* Set the frequency and address (NOTE: Only 7-bit address supported now) */
|
||||
|
||||
I2C_SETFREQUENCY(dev, i2ctool->freq);
|
||||
I2C_SETADDRESS(dev, i2ctool->addr, 7);
|
||||
|
||||
/* Set up data structures */
|
||||
|
||||
msg[0].addr = i2ctool->addr;
|
||||
msg[0].flags = 0;
|
||||
msg[0].buffer = &i2ctool->regaddr;
|
||||
msg[0].length = 1;
|
||||
|
||||
msg[1].addr = i2ctool->addr;
|
||||
msg[1].flags = 0;
|
||||
if (i2ctool->width == 8)
|
||||
{
|
||||
u.data8 = 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 < 0)
|
||||
{
|
||||
i2ctool_printf(i2ctool, g_i2cxfrerror, argv[0], -ret);
|
||||
goto errout;
|
||||
}
|
||||
ret = I2C_TRANSFER(dev, &msg[1], 1);
|
||||
if (ret < 0)
|
||||
{
|
||||
i2ctool_printf(i2ctool, g_i2cxfrerror, argv[0], -ret);
|
||||
goto errout;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ret = I2C_TRANSFER(dev, msg, 2);
|
||||
if (ret < 0)
|
||||
{
|
||||
goto errout;
|
||||
}
|
||||
}
|
||||
|
||||
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", u.data8);
|
||||
}
|
||||
else
|
||||
{
|
||||
i2ctool_printf(i2ctool, "%04x\n", u.data16);
|
||||
}
|
||||
|
||||
(void)up_i2cuninitialize(dev);
|
||||
return OK;
|
||||
|
||||
errout:
|
||||
i2ctool_printf(i2ctool, g_i2cxfrerror, argv[0], -ret);
|
||||
(void)up_i2cuninitialize(dev);
|
||||
return ERROR;
|
||||
}
|
||||
|
@ -53,6 +53,19 @@
|
||||
* 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
|
||||
@ -73,6 +86,10 @@
|
||||
# define CONFIG_I2CTOOL_MAXREGADDR 0xff
|
||||
#endif
|
||||
|
||||
#ifndef CONFIG_I2CTOOL_DEFFREQ
|
||||
# define CONFIG_I2CTOOL_DEFFREQ 400000
|
||||
#endif
|
||||
|
||||
/* This is the maximum number of arguments that will be accepted for a
|
||||
* command
|
||||
*/
|
||||
@ -117,11 +134,12 @@ 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 */
|
||||
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 */
|
||||
uint32_t freq; /* [-f freq] I2C frequency */
|
||||
|
||||
/* Output streams */
|
||||
|
||||
@ -131,7 +149,7 @@ struct i2ctool_s
|
||||
#endif
|
||||
};
|
||||
|
||||
typedef int (*cmd_t)(FAR struct i2ctool_s *i2ctool, int argc, char **argv);
|
||||
typedef int (*cmd_t)(FAR struct i2ctool_s *i2ctool, int argc, FAR char **argv);
|
||||
|
||||
struct cmdmap_s
|
||||
{
|
||||
@ -145,16 +163,13 @@ struct cmdmap_s
|
||||
* Public Data
|
||||
****************************************************************************/
|
||||
|
||||
extern const char g_syntax[];
|
||||
extern const char g_fmtargrequired[];
|
||||
extern const char g_fmtarginvalid[];
|
||||
extern const char g_fmtargrange[];
|
||||
extern const char g_fmtcmdnotfound[];
|
||||
extern const char g_fmtnosuch[];
|
||||
extern const char g_fmttoomanyargs[];
|
||||
extern const char g_fmtcmdfailed[];
|
||||
extern const char g_fmtcmdoutofmemory[];
|
||||
extern const char g_fmtinternalerror[];
|
||||
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
|
||||
@ -167,11 +182,11 @@ int i2ctool_printf(FAR struct i2ctool_s *i2ctool, const char *fmt, ...);
|
||||
|
||||
/* Command handlers */
|
||||
|
||||
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);
|
||||
int cmd_bus(FAR struct i2ctool_s *i2ctool, int argc, FAR char **argv);
|
||||
int cmd_dev(FAR struct i2ctool_s *i2ctool, int argc, FAR char **argv);
|
||||
int cmd_dump(FAR struct i2ctool_s *i2ctool, int argc, FAR char **argv);
|
||||
int cmd_get(FAR struct i2ctool_s *i2ctool, int argc, FAR char **argv);
|
||||
int cmd_set(FAR struct i2ctool_s *i2ctool, int argc, FAR char **argv);
|
||||
|
||||
/* Common logic */
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user