2011-08-29 22:12:03 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* apps/system/i2c/i2c_main.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 2011 Gregory Nutt. All rights reserved.
|
2012-09-13 18:58:49 +02:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2011-08-29 22:12:03 +02:00
|
|
|
*
|
|
|
|
* 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"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-09-18 19:52:00 +02:00
|
|
|
static int i2ccmd_help(FAR struct i2ctool_s *i2ctool, int argc, char **argv);
|
2019-10-15 00:30:57 +02:00
|
|
|
static int i2ccmd_unrecognized(FAR struct i2ctool_s *i2ctool, int argc,
|
|
|
|
FAR char **argv);
|
2011-08-29 22:12:03 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2017-05-19 15:13:12 +02:00
|
|
|
static struct i2ctool_s g_i2ctool;
|
2011-08-29 22:12:03 +02:00
|
|
|
|
|
|
|
static const struct cmdmap_s g_i2ccmds[] =
|
|
|
|
{
|
2021-03-03 21:14:17 +01:00
|
|
|
{ "?", i2ccmd_help, "Show help ", NULL },
|
|
|
|
{ "bus", i2ccmd_bus, "List buses ", NULL },
|
|
|
|
#ifdef CONFIG_I2C_RESET
|
|
|
|
{ "reset", i2ccmd_reset, "Reset bus ", NULL },
|
|
|
|
#endif
|
|
|
|
{ "dev", i2ccmd_dev, "List devices ", "[OPTIONS] <first> <last>" },
|
|
|
|
{ "get", i2ccmd_get, "Read register ", "[OPTIONS] [<repetitions>]" },
|
|
|
|
{ "dump", i2ccmd_dump, "Dump register ", "[OPTIONS] [<num bytes>]" },
|
|
|
|
{ "help", i2ccmd_help, "Show help ", NULL },
|
2021-02-23 14:44:21 +01:00
|
|
|
{
|
2021-03-03 21:14:17 +01:00
|
|
|
"set", i2ccmd_set, "Write register",
|
2021-02-23 14:44:21 +01:00
|
|
|
"[OPTIONS] <value> [<repetitions>]"
|
|
|
|
},
|
|
|
|
{
|
2021-03-03 21:14:17 +01:00
|
|
|
"verf", i2ccmd_verf, "Verify access ",
|
2021-02-23 14:44:21 +01:00
|
|
|
"[OPTIONS] [<value>] [<repetitions>]"
|
|
|
|
},
|
2011-09-18 19:52:00 +02:00
|
|
|
{ NULL, NULL, NULL, NULL }
|
2011-08-29 22:12:03 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Common, message formats */
|
|
|
|
|
2021-02-23 14:44:21 +01:00
|
|
|
const char g_i2cargrequired[] =
|
|
|
|
"i2ctool: %s: missing required argument(s)\n";
|
2019-10-15 00:30:57 +02:00
|
|
|
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";
|
2011-08-29 22:12:03 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2011-09-18 19:52:00 +02:00
|
|
|
* Name: i2ccmd_help
|
2011-08-29 22:12:03 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2011-09-18 19:52:00 +02:00
|
|
|
static int i2ccmd_help(FAR struct i2ctool_s *i2ctool, int argc, char **argv)
|
2011-08-29 22:12:03 +02:00
|
|
|
{
|
|
|
|
const struct cmdmap_s *ptr;
|
|
|
|
|
2011-08-30 19:20:46 +02:00
|
|
|
i2ctool_printf(i2ctool, "Usage: i2c <cmd> [arguments]\n");
|
|
|
|
i2ctool_printf(i2ctool, "Where <cmd> is one of:\n\n");
|
2011-08-29 22:12:03 +02:00
|
|
|
for (ptr = g_i2ccmds; ptr->cmd; ptr++)
|
|
|
|
{
|
|
|
|
if (ptr->usage)
|
|
|
|
{
|
2019-10-15 00:30:57 +02:00
|
|
|
i2ctool_printf(i2ctool, " %s: %s %s\n",
|
|
|
|
ptr->desc, ptr->cmd, ptr->usage);
|
2011-08-29 22:12:03 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-08-30 19:20:46 +02:00
|
|
|
i2ctool_printf(i2ctool, " %s: %s\n", ptr->desc, ptr->cmd);
|
2011-08-29 22:12:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-23 14:44:21 +01:00
|
|
|
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",
|
2011-08-30 17:07:21 +02:00
|
|
|
CONFIG_I2CTOOL_MINADDR, i2ctool->addr);
|
2021-02-23 14:44:21 +01:00
|
|
|
i2ctool_printf(i2ctool,
|
|
|
|
" [-b bus] is the I2C bus number (decimal)."
|
|
|
|
" Default: %d Current: %d\n",
|
2011-08-30 17:07:21 +02:00
|
|
|
CONFIG_I2CTOOL_MINBUS, i2ctool->bus);
|
2021-02-23 14:44:21 +01:00
|
|
|
i2ctool_printf(i2ctool,
|
|
|
|
" [-w width] is the data width (8 or 16 decimal)."
|
|
|
|
" Default: 8 Current: %d\n",
|
2011-08-30 17:07:21 +02:00
|
|
|
i2ctool->width);
|
2021-02-23 14:44:21 +01:00
|
|
|
i2ctool_printf(i2ctool,
|
|
|
|
" [-s|n], send/don't send start between command and data."
|
|
|
|
" Default: -n Current: %s\n",
|
2011-08-30 17:07:21 +02:00
|
|
|
i2ctool->start ? "-s" : "-n");
|
2021-02-23 14:44:21 +01:00
|
|
|
i2ctool_printf(i2ctool,
|
|
|
|
" [-i|j], Auto increment|don't increment regaddr on "
|
|
|
|
"repetitions."
|
|
|
|
" Default: NO Current: %s\n",
|
2011-09-18 19:52:00 +02:00
|
|
|
i2ctool->autoincr ? "YES" : "NO");
|
2021-02-23 14:44:21 +01:00
|
|
|
i2ctool_printf(i2ctool,
|
|
|
|
" [-f freq] I2C frequency."
|
|
|
|
" Default: %d Current: %d\n",
|
2011-08-30 17:07:21 +02:00
|
|
|
CONFIG_I2CTOOL_DEFFREQ, i2ctool->freq);
|
2019-08-20 22:29:21 +02:00
|
|
|
|
|
|
|
i2ctool_printf(i2ctool, "\nSpecial non-sticky options:\n");
|
2021-02-23 14:44:21 +01:00
|
|
|
i2ctool_printf(i2ctool,
|
|
|
|
" [-r regaddr] is the I2C device register index (hex)."
|
|
|
|
" Default: not used/sent\n");
|
2019-08-20 22:29:21 +02:00
|
|
|
|
2011-08-30 01:06:54 +02:00
|
|
|
i2ctool_printf(i2ctool, "\nNOTES:\n");
|
2011-08-29 22:12:03 +02:00
|
|
|
#ifndef CONFIG_DISABLE_ENVIRON
|
2021-02-23 14:44:21 +01:00
|
|
|
i2ctool_printf(i2ctool, "o An environment variable like $PATH may be used "
|
|
|
|
"for any argument.\n");
|
2011-08-29 22:12:03 +02:00
|
|
|
#endif
|
2021-02-23 14:44:21 +01:00
|
|
|
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");
|
2011-08-30 01:06:54 +02:00
|
|
|
i2ctool_printf(i2ctool, "\nWARNING:\n");
|
2021-02-23 14:44:21 +01:00
|
|
|
i2ctool_printf(i2ctool,
|
|
|
|
"o The I2C dev command may have bad side effects "
|
|
|
|
"on your I2C devices.\n");
|
2011-08-30 01:06:54 +02:00
|
|
|
i2ctool_printf(i2ctool, " Use only at your own risk.\n");
|
2011-08-29 22:12:03 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2011-09-18 19:52:00 +02:00
|
|
|
* Name: i2ccmd_unrecognized
|
2011-08-29 22:12:03 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2019-10-15 00:30:57 +02:00
|
|
|
static int i2ccmd_unrecognized(FAR struct i2ctool_s *i2ctool, int argc,
|
|
|
|
FAR char **argv)
|
2011-08-29 22:12:03 +02:00
|
|
|
{
|
2011-08-30 17:07:21 +02:00
|
|
|
i2ctool_printf(i2ctool, g_i2ccmdnotfound, argv[0]);
|
2011-08-29 22:12:03 +02:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: i2c_execute
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-10-15 00:30:57 +02:00
|
|
|
static int i2c_execute(FAR struct i2ctool_s *i2ctool, int argc,
|
|
|
|
FAR char *argv[])
|
2011-08-29 22:12:03 +02:00
|
|
|
{
|
2019-10-15 00:30:57 +02:00
|
|
|
FAR const struct cmdmap_s *cmdmap;
|
|
|
|
FAR 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;
|
2011-08-29 22:12:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: i2c_argument
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-05-19 15:13:12 +02:00
|
|
|
static FAR char *i2c_argument(FAR struct i2ctool_s *i2ctool,
|
|
|
|
int argc, char *argv[], int *pindex)
|
2011-08-29 22:12:03 +02:00
|
|
|
{
|
|
|
|
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 */
|
|
|
|
|
2019-10-15 00:30:57 +02:00
|
|
|
FAR char *value = getenv(arg + 1);
|
2011-08-29 22:12:03 +02:00
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-10-15 00:30:57 +02:00
|
|
|
return (FAR char *)"";
|
2011-08-29 22:12:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Return the next argument. */
|
|
|
|
|
|
|
|
return arg;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: i2c_parse
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-05-19 15:13:12 +02:00
|
|
|
static int i2c_parse(FAR struct i2ctool_s *i2ctool, int argc, char *argv[])
|
2011-08-29 22:12:03 +02:00
|
|
|
{
|
2019-10-15 00:30:57 +02:00
|
|
|
FAR char *newargs[MAX_ARGUMENTS + 2];
|
2011-08-29 22:12:03 +02:00
|
|
|
FAR char *cmd;
|
|
|
|
int nargs;
|
|
|
|
int index;
|
|
|
|
|
2019-10-15 00:30:57 +02:00
|
|
|
/* Parse out the command, skipping the first argument (the program name) */
|
2011-08-29 22:12:03 +02:00
|
|
|
|
|
|
|
index = 1;
|
2011-08-30 01:06:54 +02:00
|
|
|
cmd = i2c_argument(i2ctool, argc, argv, &index);
|
2011-08-29 22:12:03 +02:00
|
|
|
|
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
|
2011-09-18 19:52:00 +02:00
|
|
|
return i2ccmd_help(i2ctool, 0, NULL);
|
2011-08-29 22:12:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse all of the arguments following the command name. */
|
|
|
|
|
|
|
|
newargs[0] = cmd;
|
2011-09-06 23:00:48 +02:00
|
|
|
for (nargs = 1; nargs <= MAX_ARGUMENTS; nargs++)
|
2011-08-29 22:12:03 +02:00
|
|
|
{
|
2011-08-30 01:06:54 +02:00
|
|
|
newargs[nargs] = i2c_argument(i2ctool, argc, argv, &index);
|
2011-08-29 22:12:03 +02:00
|
|
|
if (!newargs[nargs])
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-02-06 18:34:39 +01:00
|
|
|
|
2011-08-29 22:12:03 +02:00
|
|
|
newargs[nargs] = NULL;
|
|
|
|
|
|
|
|
/* Then execute the command */
|
|
|
|
|
2011-08-30 01:06:54 +02:00
|
|
|
return i2c_execute(i2ctool, nargs, newargs);
|
2011-08-29 22:12:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: i2c_setup
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-08-30 21:48:47 +02:00
|
|
|
static inline int i2c_setup(FAR struct i2ctool_s *i2ctool)
|
2011-08-29 22:12:03 +02:00
|
|
|
{
|
|
|
|
/* Initialize the output stream */
|
|
|
|
|
|
|
|
#ifdef CONFIG_I2CTOOL_OUTDEV
|
2011-08-30 21:48:47 +02:00
|
|
|
i2ctool->ss_outfd = open(CONFIG_I2CTOOL_OUTDEV, O_WRONLY);
|
|
|
|
if (i2ctool->ss_outfd < 0)
|
2011-08-29 22:12:03 +02:00
|
|
|
{
|
2011-08-30 17:07:21 +02:00
|
|
|
fprintf(stderr, g_i2ccmdfailed, "open", errno);
|
2011-08-29 22:12:03 +02:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create a standard C stream on the console device */
|
|
|
|
|
2011-08-30 21:48:47 +02:00
|
|
|
i2ctool->ss_outstream = fdopen(i2ctool->ss_outfd, "w");
|
|
|
|
if (!i2ctool->ss_outstream)
|
2011-08-29 22:12:03 +02:00
|
|
|
{
|
2011-08-30 17:07:21 +02:00
|
|
|
fprintf(stderr, g_i2ccmdfailed, "fdopen", errno);
|
2011-08-29 22:12:03 +02:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: i2c_teardown
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Close the output stream if it is not the standard output stream.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-08-30 21:48:47 +02:00
|
|
|
static void i2c_teardown(FAR struct i2ctool_s *i2ctool)
|
2011-08-29 22:12:03 +02:00
|
|
|
{
|
|
|
|
fflush(OUTSTREAM(&g_i2ctool));
|
|
|
|
|
|
|
|
#ifdef CONFIG_I2CTOOL_OUTDEV
|
2011-08-30 21:48:47 +02:00
|
|
|
fclose(i2ctool->ss_outstream);
|
2011-08-29 22:12:03 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: i2c_main
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-09-06 17:23:23 +02:00
|
|
|
int main(int argc, FAR char *argv[])
|
2011-08-29 22:12:03 +02:00
|
|
|
{
|
2011-08-30 01:06:54 +02:00
|
|
|
/* Verify settings */
|
|
|
|
|
2016-02-06 18:34:39 +01:00
|
|
|
if (g_i2ctool.bus < CONFIG_I2CTOOL_MINBUS ||
|
|
|
|
g_i2ctool.bus > CONFIG_I2CTOOL_MAXBUS)
|
2011-08-30 01:06:54 +02:00
|
|
|
{
|
|
|
|
g_i2ctool.bus = CONFIG_I2CTOOL_MINBUS;
|
|
|
|
}
|
|
|
|
|
2016-02-06 18:34:39 +01:00
|
|
|
if (g_i2ctool.addr < CONFIG_I2CTOOL_MINADDR ||
|
|
|
|
g_i2ctool.addr > CONFIG_I2CTOOL_MAXADDR)
|
2011-08-30 01:06:54 +02:00
|
|
|
{
|
|
|
|
g_i2ctool.addr = CONFIG_I2CTOOL_MINADDR;
|
|
|
|
}
|
|
|
|
|
2018-08-30 18:33:32 +02:00
|
|
|
if (g_i2ctool.regaddr > CONFIG_I2CTOOL_MAXREGADDR)
|
2011-08-30 01:06:54 +02:00
|
|
|
{
|
|
|
|
g_i2ctool.regaddr = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (g_i2ctool.width != 8 && g_i2ctool.width != 16)
|
|
|
|
{
|
|
|
|
g_i2ctool.width = 8;
|
|
|
|
}
|
|
|
|
|
2011-08-30 17:07:21 +02:00
|
|
|
if (g_i2ctool.freq == 0)
|
|
|
|
{
|
|
|
|
g_i2ctool.freq = CONFIG_I2CTOOL_DEFFREQ;
|
|
|
|
}
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2019-08-20 22:29:21 +02:00
|
|
|
g_i2ctool.hasregindx = false;
|
|
|
|
|
2016-02-06 18:34:39 +01:00
|
|
|
/* Parse and process the command line */
|
2011-08-29 22:12:03 +02:00
|
|
|
|
2011-08-30 21:48:47 +02:00
|
|
|
i2c_setup(&g_i2ctool);
|
2020-01-02 13:09:50 +01:00
|
|
|
i2c_parse(&g_i2ctool, argc, argv);
|
2011-08-29 22:12:03 +02:00
|
|
|
|
2011-08-30 21:48:47 +02:00
|
|
|
i2ctool_flush(&g_i2ctool);
|
|
|
|
i2c_teardown(&g_i2ctool);
|
2011-08-29 22:12:03 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: i2ctool_printf
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Print a string to the currently selected stream.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-08-30 01:06:54 +02:00
|
|
|
int i2ctool_printf(FAR struct i2ctool_s *i2ctool, const char *fmt, ...)
|
2011-08-29 22:12:03 +02:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2011-08-30 01:06:54 +02:00
|
|
|
ret = vfprintf(OUTSTREAM(i2ctool), fmt, ap);
|
2011-08-29 22:12:03 +02:00
|
|
|
va_end(ap);
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2011-08-29 22:12:03 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: i2ctool_write
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* write a buffer to the currently selected stream.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-10-15 00:30:57 +02:00
|
|
|
ssize_t i2ctool_write(FAR struct i2ctool_s *i2ctool, FAR const void *buffer,
|
|
|
|
size_t nbytes)
|
2011-08-29 22:12:03 +02:00
|
|
|
{
|
|
|
|
ssize_t ret;
|
|
|
|
|
|
|
|
/* Write the data to the output stream */
|
|
|
|
|
2011-08-30 01:06:54 +02:00
|
|
|
ret = fwrite(buffer, 1, nbytes, OUTSTREAM(i2ctool));
|
2011-08-29 22:12:03 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-16 20:12:34 +02:00
|
|
|
_err("ERROR: [%d] Failed to send buffer: %d\n", OUTFD(i2ctool), errno);
|
2011-08-29 22:12:03 +02:00
|
|
|
}
|
2016-02-06 18:34:39 +01:00
|
|
|
|
2011-08-29 22:12:03 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-08-30 21:48:47 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: i2ctool_flush
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Flush buffered I/O to the currently selected stream.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void i2ctool_flush(FAR struct i2ctool_s *i2ctool)
|
|
|
|
{
|
|
|
|
fflush(OUTSTREAM(i2ctool));
|
|
|
|
}
|