summaryrefslogtreecommitdiffstats
path: root/commands/i2c.c
diff options
context:
space:
mode:
Diffstat (limited to 'commands/i2c.c')
-rw-r--r--commands/i2c.c60
1 files changed, 35 insertions, 25 deletions
diff --git a/commands/i2c.c b/commands/i2c.c
index 77d65e3fa3..3a708531ee 100644
--- a/commands/i2c.c
+++ b/commands/i2c.c
@@ -1,19 +1,7 @@
-/*
- * i2c.c - i2c commands
- *
- * Copyright (c) 2010 Eric Bénard <eric@eukrea.Com>, Eukréa Electromatique
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of
- * the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- */
+// SPDX-License-Identifier: GPL-2.0-or-later
+// SPDX-FileCopyrightText: © 2010 Eric Bénard <eric@eukrea.Com>, Eukréa Electromatique
+
+/* i2c.c - i2c commands */
#include <common.h>
#include <command.h>
@@ -28,10 +16,12 @@ static void i2c_probe_range(struct i2c_adapter *adapter, int startaddr, int stop
printf("probing i2c%d range 0x%02x-0x%02x: ", adapter->nr, startaddr, stopaddr);
for (addr = startaddr; addr <= stopaddr && !ctrlc(); addr++) {
+ u8 buf[1];
struct i2c_msg msg = {
.addr = addr,
- .buf = NULL,
- .len = 0,
+ .buf = buf,
+ .len = sizeof(buf),
+ .flags = I2C_M_RD,
};
int ret = i2c_transfer(adapter, &msg, 1);
if (ret == 1)
@@ -45,17 +35,37 @@ static int do_i2c_probe(int argc, char *argv[])
{
struct i2c_adapter *adapter = NULL;
int startaddr = 4, stopaddr = 0x77;
+ int ret;
if (argc > 1) {
- adapter = i2c_get_adapter(simple_strtoul(argv[1], NULL, 0));
+ int busnum;
+
+ ret = kstrtoint(argv[1], 0, &busnum);
+ if (ret) {
+ printf("Cannot parse \"%s\" as a number\n", argv[1]);
+ return ret;
+ }
+
+ adapter = i2c_get_adapter(busnum);
if (!adapter)
return -ENODEV;
}
- if (argc > 2)
- startaddr = simple_strtol(argv[2], NULL, 0);
- if (argc > 3)
- stopaddr = simple_strtol(argv[3], NULL, 0);
+ if (argc > 2) {
+ ret = kstrtoint(argv[2], 0, &startaddr);
+ if (ret) {
+ printf("Cannot parse \"%s\" as a number\n", argv[1]);
+ return ret;
+ }
+ }
+
+ if (argc > 3) {
+ ret = kstrtoint(argv[3], 0, &stopaddr);
+ if (ret) {
+ printf("Cannot parse \"%s\" as a number\n", argv[1]);
+ return ret;
+ }
+ }
if (stopaddr > 0x7f)
stopaddr = 0x7f;
@@ -185,7 +195,7 @@ static int do_i2c_read(int argc, char *argv[])
struct i2c_adapter *adapter = NULL;
struct i2c_client client;
u8 *buf;
- int count = -1, addr = -1, reg = -1, verbose = 0, ret, opt, bus = 0, wide = 0;
+ int count = 1, addr = -1, reg = -1, verbose = 0, ret, opt, bus = 0, wide = 0;
while ((opt = getopt(argc, argv, "a:b:c:r:vw")) > 0) {
switch (opt) {
@@ -254,7 +264,7 @@ BAREBOX_CMD_HELP_OPT("-b BUS\t", "i2c bus number (default 0)")
BAREBOX_CMD_HELP_OPT("-a ADDR\t", "i2c device address")
BAREBOX_CMD_HELP_OPT("-r START", "start register (optional, master receive mode if none given)")
BAREBOX_CMD_HELP_OPT("-w\t", "use word (16 bit) wide access")
-BAREBOX_CMD_HELP_OPT("-c COUNT", "byte count")
+BAREBOX_CMD_HELP_OPT("-c COUNT", "byte count (default 1)")
BAREBOX_CMD_HELP_OPT("-v\t", "verbose")
BAREBOX_CMD_HELP_END