mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 1/2] i2c_probe: limit slave addresses to [0x04, 0x77]
@ 2019-01-25 20:59 Uwe Kleine-König
  2019-01-25 20:59 ` [PATCH 2/2] i2c_probe: Use a quick write transfer instead of writing a zero Uwe Kleine-König
  2019-01-28  9:01 ` [PATCH 1/2] i2c_probe: limit slave addresses to [0x04, 0x77] Sascha Hauer
  0 siblings, 2 replies; 3+ messages in thread
From: Uwe Kleine-König @ 2019-01-25 20:59 UTC (permalink / raw)
  To: barebox

Adresses below 0x04 and above 0x77 are reserved in the i2c bus
specification, so don't probe these addresses unless requested
explicitly.

Also do more strict boundary checking:

 - ensure start address is greater or equal to zero;
 - don't decrease stopaddr after checking it being greater or equal to
   startaddr.

Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
---
 commands/i2c.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/commands/i2c.c b/commands/i2c.c
index 2f7f820d4d55..65ff7378ec14 100644
--- a/commands/i2c.c
+++ b/commands/i2c.c
@@ -44,7 +44,7 @@ static void i2c_probe_range(struct i2c_adapter *adapter, int startaddr, int stop
 static int do_i2c_probe(int argc, char *argv[])
 {
 	struct i2c_adapter *adapter = NULL;
-	int startaddr = 0, stopaddr = 0x7f;
+	int startaddr = 4, stopaddr = 0x77;
 
 	if (argc > 1) {
 		adapter = i2c_get_adapter(simple_strtoul(argv[1], NULL, 0));
@@ -57,13 +57,15 @@ static int do_i2c_probe(int argc, char *argv[])
 	if (argc > 3)
 		stopaddr = simple_strtol(argv[3], NULL, 0);
 
+	if (stopaddr > 0x7f)
+		stopaddr = 0x7f;
+
+	if (startaddr < 0)
+		startaddr = 0;
 
 	if (startaddr > stopaddr)
 		return COMMAND_ERROR_USAGE;
 
-	if (stopaddr > 0x7F)
-		stopaddr = 0x7F;
-
 	if (adapter) {
 		i2c_probe_range(adapter, startaddr, stopaddr);
 	} else {
-- 
2.20.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 2/2] i2c_probe: Use a quick write transfer instead of writing a zero
  2019-01-25 20:59 [PATCH 1/2] i2c_probe: limit slave addresses to [0x04, 0x77] Uwe Kleine-König
@ 2019-01-25 20:59 ` Uwe Kleine-König
  2019-01-28  9:01 ` [PATCH 1/2] i2c_probe: limit slave addresses to [0x04, 0x77] Sascha Hauer
  1 sibling, 0 replies; 3+ messages in thread
From: Uwe Kleine-König @ 2019-01-25 20:59 UTC (permalink / raw)
  To: barebox

This matches the method implemented in i2cdetect(8) when using its -q
option.

With this change an rx8130 RTC is detectable using i2c_probe. This
failed before because this chip acks the first byte (containing its
address and the R/̅W bit) but nacks the following 0 (representing the
target address to write nothing to) which makes i2c_write_reg() return
with an error and so the chip is not listed as available.

Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
---
 commands/i2c.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/commands/i2c.c b/commands/i2c.c
index 65ff7378ec14..77d65e3fa32b 100644
--- a/commands/i2c.c
+++ b/commands/i2c.c
@@ -24,19 +24,19 @@
 
 static void i2c_probe_range(struct i2c_adapter *adapter, int startaddr, int stopaddr)
 {
-	struct i2c_client client = {};
 	int addr;
-	int ret;
-	u8 reg;
-
-	client.adapter = adapter;
 
 	printf("probing i2c%d range 0x%02x-0x%02x: ", adapter->nr, startaddr, stopaddr);
 	for (addr = startaddr; addr <= stopaddr && !ctrlc(); addr++) {
-		client.addr = addr;
-		ret = i2c_write_reg(&client, 0x00, &reg, 0);
-		if (ret == 0)
+		struct i2c_msg msg = {
+			.addr = addr,
+			.buf = NULL,
+			.len = 0,
+		};
+		int ret = i2c_transfer(adapter, &msg, 1);
+		if (ret == 1)
 			printf("0x%02x ", addr);
+
 	}
 	printf("\n");
 }
-- 
2.20.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH 1/2] i2c_probe: limit slave addresses to [0x04, 0x77]
  2019-01-25 20:59 [PATCH 1/2] i2c_probe: limit slave addresses to [0x04, 0x77] Uwe Kleine-König
  2019-01-25 20:59 ` [PATCH 2/2] i2c_probe: Use a quick write transfer instead of writing a zero Uwe Kleine-König
@ 2019-01-28  9:01 ` Sascha Hauer
  1 sibling, 0 replies; 3+ messages in thread
From: Sascha Hauer @ 2019-01-28  9:01 UTC (permalink / raw)
  To: Uwe Kleine-König; +Cc: barebox

On Fri, Jan 25, 2019 at 09:59:34PM +0100, Uwe Kleine-König wrote:
> Adresses below 0x04 and above 0x77 are reserved in the i2c bus
> specification, so don't probe these addresses unless requested
> explicitly.
> 
> Also do more strict boundary checking:
> 
>  - ensure start address is greater or equal to zero;
>  - don't decrease stopaddr after checking it being greater or equal to
>    startaddr.
> 
> Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
> ---
>  commands/i2c.c | 10 ++++++----
>  1 file changed, 6 insertions(+), 4 deletions(-)

Applied, thanks

Sascha

> 
> diff --git a/commands/i2c.c b/commands/i2c.c
> index 2f7f820d4d55..65ff7378ec14 100644
> --- a/commands/i2c.c
> +++ b/commands/i2c.c
> @@ -44,7 +44,7 @@ static void i2c_probe_range(struct i2c_adapter *adapter, int startaddr, int stop
>  static int do_i2c_probe(int argc, char *argv[])
>  {
>  	struct i2c_adapter *adapter = NULL;
> -	int startaddr = 0, stopaddr = 0x7f;
> +	int startaddr = 4, stopaddr = 0x77;
>  
>  	if (argc > 1) {
>  		adapter = i2c_get_adapter(simple_strtoul(argv[1], NULL, 0));
> @@ -57,13 +57,15 @@ static int do_i2c_probe(int argc, char *argv[])
>  	if (argc > 3)
>  		stopaddr = simple_strtol(argv[3], NULL, 0);
>  
> +	if (stopaddr > 0x7f)
> +		stopaddr = 0x7f;
> +
> +	if (startaddr < 0)
> +		startaddr = 0;
>  
>  	if (startaddr > stopaddr)
>  		return COMMAND_ERROR_USAGE;
>  
> -	if (stopaddr > 0x7F)
> -		stopaddr = 0x7F;
> -
>  	if (adapter) {
>  		i2c_probe_range(adapter, startaddr, stopaddr);
>  	} else {
> -- 
> 2.20.1
> 
> 
> _______________________________________________
> barebox mailing list
> barebox@lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/barebox

-- 
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2019-01-28  9:01 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-01-25 20:59 [PATCH 1/2] i2c_probe: limit slave addresses to [0x04, 0x77] Uwe Kleine-König
2019-01-25 20:59 ` [PATCH 2/2] i2c_probe: Use a quick write transfer instead of writing a zero Uwe Kleine-König
2019-01-28  9:01 ` [PATCH 1/2] i2c_probe: limit slave addresses to [0x04, 0x77] Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox