mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 0/2 v2] add spi command
@ 2012-11-04  8:54 Jean-Christophe PLAGNIOL-VILLARD
  2012-11-04  8:59 ` [PATCH 1/2] spi: introduce spi_get_master Jean-Christophe PLAGNIOL-VILLARD
  0 siblings, 1 reply; 9+ messages in thread
From: Jean-Christophe PLAGNIOL-VILLARD @ 2012-11-04  8:54 UTC (permalink / raw)
  To: barebox

HI,

	v2:
	add default return usage
	use memory_display

	while debugging a spi issue I found we have a i2c commands but no spi
	command

	So introduce one

The following changes since commit 8f839b51047593bdd63d485006237111cd3a0547:

  spi: introduce spi_get_master (2012-11-02 08:55:10 +0800)

are available in the git repository at:

  git://git.jcrosoft.org/barebox.git delivery/command_spi

for you to fetch changes up to 2bfe0a6d58edae296b384b14e86e9efad148bfb7:

  introduce spi command (2012-11-04 01:45:34 +0800)

----------------------------------------------------------------
Jean-Christophe PLAGNIOL-VILLARD (1):
      introduce spi command

 commands/Kconfig  |    6 ++++++
 commands/Makefile |    1 +
 commands/spi.c    |  126 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 133 insertions(+)
 create mode 100644 commands/spi.c

Best Regards,
J.

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

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

* [PATCH 1/2] spi: introduce spi_get_master
  2012-11-04  8:54 [PATCH 0/2 v2] add spi command Jean-Christophe PLAGNIOL-VILLARD
@ 2012-11-04  8:59 ` Jean-Christophe PLAGNIOL-VILLARD
  2012-11-04  8:59   ` [PATCH 2/2] introduce spi command Jean-Christophe PLAGNIOL-VILLARD
  0 siblings, 1 reply; 9+ messages in thread
From: Jean-Christophe PLAGNIOL-VILLARD @ 2012-11-04  8:59 UTC (permalink / raw)
  To: barebox

so we can request a master usefull for the spi command

Signed-off-by: Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
---
 drivers/spi/spi.c |   12 ++++++++++++
 include/spi/spi.h |    2 ++
 2 files changed, 14 insertions(+)

diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
index 44040e5..6a5bd6d 100644
--- a/drivers/spi/spi.c
+++ b/drivers/spi/spi.c
@@ -219,6 +219,18 @@ int spi_register_master(struct spi_master *master)
 }
 EXPORT_SYMBOL(spi_register_master);
 
+struct spi_master *spi_get_master(int bus)
+{
+	struct spi_master* m;
+
+	list_for_each_entry(m, &spi_master_list, list) {
+		if (m->bus_num == bus)
+			return m;
+	}
+
+	return NULL;
+}
+
 int spi_sync(struct spi_device *spi, struct spi_message *message)
 {
 	return spi->master->transfer(spi, message);
diff --git a/include/spi/spi.h b/include/spi/spi.h
index 1773ca2..d6570a4 100644
--- a/include/spi/spi.h
+++ b/include/spi/spi.h
@@ -432,6 +432,8 @@ static inline ssize_t spi_w8r8(struct spi_device *spi, u8 cmd)
 
 extern struct bus_type spi_bus;
 
+struct spi_master *spi_get_master(int bus);
+
 static inline int spi_register_driver(struct driver_d *drv)
 {
 	drv->bus = &spi_bus;
-- 
1.7.10.4


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

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

* [PATCH 2/2] introduce spi command
  2012-11-04  8:59 ` [PATCH 1/2] spi: introduce spi_get_master Jean-Christophe PLAGNIOL-VILLARD
@ 2012-11-04  8:59   ` Jean-Christophe PLAGNIOL-VILLARD
  0 siblings, 0 replies; 9+ messages in thread
From: Jean-Christophe PLAGNIOL-VILLARD @ 2012-11-04  8:59 UTC (permalink / raw)
  To: barebox

usefull to debug spi

The command allow to read/write on a spi device

Usage: spi [OPTION] [data to write 0xXX]
write/read spi device.
  -b <bus_num>  spi bus number (default = 0)
  -r <count>    to read
  -c <cs>       chip select (default = 0)
  -m <mode>     spi mode (default = 0)
  -f <hz>       max_speed_hz (default = 1MHz)
  -w <bit>      bits_per_word (default = 8)
  -v            verbose

Signed-off-by: Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
---
 commands/Kconfig  |    6 +++
 commands/Makefile |    1 +
 commands/spi.c    |  126 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 133 insertions(+)
 create mode 100644 commands/spi.c

diff --git a/commands/Kconfig b/commands/Kconfig
index 16706d3..f7cbd67 100644
--- a/commands/Kconfig
+++ b/commands/Kconfig
@@ -597,6 +597,12 @@ config CMD_I2C
 	  include i2c_probe, i2c_read and i2c_write commands to communicate
 	  on i2c bus.
 
+config CMD_SPI
+	bool
+	depends on SPI
+	prompt "spi command"
+	help
+
 config CMD_LED
 	bool
 	depends on LED
diff --git a/commands/Makefile b/commands/Makefile
index 610be55..19d496e 100644
--- a/commands/Makefile
+++ b/commands/Makefile
@@ -55,6 +55,7 @@ obj-$(CONFIG_USB_GADGET_SERIAL)	+= usbserial.o
 obj-$(CONFIG_CMD_GPIO)		+= gpio.o
 obj-$(CONFIG_CMD_UNCOMPRESS)	+= uncompress.o
 obj-$(CONFIG_CMD_I2C)		+= i2c.o
+obj-$(CONFIG_CMD_SPI)		+= spi.o
 obj-$(CONFIG_CMD_UBI)		+= ubi.o
 obj-$(CONFIG_CMD_MENU)		+= menu.o
 obj-$(CONFIG_CMD_PASSWD)	+= passwd.o
diff --git a/commands/spi.c b/commands/spi.c
new file mode 100644
index 0000000..b4663f2
--- /dev/null
+++ b/commands/spi.c
@@ -0,0 +1,126 @@
+/*
+ * Copyright (c) 2012 Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
+ *
+ * Under GPLv2 only
+ *
+ */
+
+#include <common.h>
+#include <command.h>
+#include <errno.h>
+#include <malloc.h>
+#include <getopt.h>
+#include <spi/spi.h>
+
+static int do_spi(int argc, char *argv[])
+{
+	struct spi_device spi;
+	int bus = 0;
+	int read = 0;
+	int verbose = 0;
+	int opt, count, i, ret;
+	int byte_per_word;
+
+	u8 *tx_buf, *rx_buf;
+
+	memset(&spi, 0, sizeof(struct spi_device));
+
+	spi.max_speed_hz = 1 * 1000 * 1000;
+	spi.bits_per_word = 8;
+
+	while ((opt = getopt(argc, argv, "b:c:r:m:f:w:v")) > 0) {
+		switch (opt) {
+		case 'b':
+			bus = simple_strtol(optarg, NULL, 0);
+			break;
+		case 'r':
+			read = simple_strtol(optarg, NULL, 0);
+			break;
+		case 'c':
+			spi.chip_select = simple_strtoul(optarg, NULL, 0);
+			break;
+		case 'm':
+			spi.mode = simple_strtoul(optarg, NULL, 0);
+			break;
+		case 'f':
+			spi.max_speed_hz = simple_strtoul(optarg, NULL, 0);
+			break;
+		case 'w':
+			spi.bits_per_word = simple_strtoul(optarg, NULL, 0);
+			break;
+		case 'v':
+			verbose = 1;
+			break;
+		default:
+			return COMMAND_ERROR_USAGE;
+		}
+	}
+
+	count = argc - optind;
+
+	if ((!read && !count) || !spi.max_speed_hz || !spi.bits_per_word)
+		return COMMAND_ERROR_USAGE;
+
+
+	spi.master = spi_get_master(bus);
+	if (!spi.master) {
+		printf("spi bus %d not found\n", bus);
+		return -ENODEV;
+	}
+
+	if (spi.chip_select > spi.master->num_chipselect) {
+		printf("spi chip select (%d)> master num chipselect (%d)\n",
+			spi.chip_select, spi.master->num_chipselect);
+		return -EINVAL;
+	}
+
+	tx_buf = xmalloc(count);
+	rx_buf = xmalloc(count);
+
+	for (i = 0; i < count; i++)
+		tx_buf[i] = (u8) simple_strtol(argv[optind + i], NULL, 16);
+
+	ret = spi_write_then_read(&spi, tx_buf, count, rx_buf, read);
+	if (ret)
+		goto out;
+
+	byte_per_word = min(spi.bits_per_word / 8, 1);
+	if (verbose) {
+		printf("device config\n");
+		printf("    bus_num       = %d\n", spi.master->bus_num);
+		printf("    max_speed_hz  = %d\n", spi.max_speed_hz);
+		printf("    chip_select   = %d\n", spi.chip_select);
+		printf("    mode          = 0x%x\n", spi.mode);
+		printf("    bits_per_word = %d\n", spi.bits_per_word);
+		printf("\n");
+
+		printf("wrote %i bytes\n", count);
+		memory_display(tx_buf, 0, count, byte_per_word);
+
+		printf("read %i bytes\n", read);
+	}
+
+	memory_display(rx_buf, 0, read, byte_per_word);
+
+out:
+	free(rx_buf);
+	free(tx_buf);
+	return ret;
+}
+
+static const __maybe_unused char cmd_spi_help[] =
+"Usage: spi [OPTION] [data to write 0xXX]\n"
+"write/read spi device.\n"
+"  -b <bus_num>  spi bus number (default = 0)\n"
+"  -r <count>    to read\n"
+"  -c <cs>       chip select (default = 0)\n"
+"  -m <mode>     spi mode (default = 0)\n"
+"  -f <hz>       max_speed_hz (default = 1MHz)\n"
+"  -w <bit>      bits_per_word (default = 8)\n"
+"  -v            verbose\n";
+
+BAREBOX_CMD_START(spi)
+	.cmd		= do_spi,
+	.usage		= "write/read spi device",
+	BAREBOX_CMD_HELP(cmd_spi_help)
+BAREBOX_CMD_END
-- 
1.7.10.4


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

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

* [PATCH 2/2] introduce spi command
  2012-11-05 18:30 ` [PATCH 1/2] spi: introduce spi_get_master Jean-Christophe PLAGNIOL-VILLARD
@ 2012-11-05 18:30   ` Jean-Christophe PLAGNIOL-VILLARD
  0 siblings, 0 replies; 9+ messages in thread
From: Jean-Christophe PLAGNIOL-VILLARD @ 2012-11-05 18:30 UTC (permalink / raw)
  To: barebox

usefull to debug spi

The command allow to read/write on a spi device

Usage: spi [OPTION] [data to write 0xXX]
write/read spi device.
  -b <bus_num>  spi bus number (default = 0)
  -r <count>    to read
  -c <cs>       chip select (default = 0)
  -m <mode>     spi mode (default = 0)
  -f <hz>       max_speed_hz (default = 1MHz)
  -w <bit>      bits_per_word (default = 8)
  -v            verbose

Signed-off-by: Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
---
 commands/Kconfig  |    6 +++
 commands/Makefile |    1 +
 commands/spi.c    |  132 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 139 insertions(+)
 create mode 100644 commands/spi.c

diff --git a/commands/Kconfig b/commands/Kconfig
index 16706d3..f7cbd67 100644
--- a/commands/Kconfig
+++ b/commands/Kconfig
@@ -597,6 +597,12 @@ config CMD_I2C
 	  include i2c_probe, i2c_read and i2c_write commands to communicate
 	  on i2c bus.
 
+config CMD_SPI
+	bool
+	depends on SPI
+	prompt "spi command"
+	help
+
 config CMD_LED
 	bool
 	depends on LED
diff --git a/commands/Makefile b/commands/Makefile
index 610be55..19d496e 100644
--- a/commands/Makefile
+++ b/commands/Makefile
@@ -55,6 +55,7 @@ obj-$(CONFIG_USB_GADGET_SERIAL)	+= usbserial.o
 obj-$(CONFIG_CMD_GPIO)		+= gpio.o
 obj-$(CONFIG_CMD_UNCOMPRESS)	+= uncompress.o
 obj-$(CONFIG_CMD_I2C)		+= i2c.o
+obj-$(CONFIG_CMD_SPI)		+= spi.o
 obj-$(CONFIG_CMD_UBI)		+= ubi.o
 obj-$(CONFIG_CMD_MENU)		+= menu.o
 obj-$(CONFIG_CMD_PASSWD)	+= passwd.o
diff --git a/commands/spi.c b/commands/spi.c
new file mode 100644
index 0000000..899bf62
--- /dev/null
+++ b/commands/spi.c
@@ -0,0 +1,132 @@
+/*
+ * Copyright (c) 2012 Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
+ *
+ * Under GPLv2 only
+ *
+ */
+
+#include <common.h>
+#include <command.h>
+#include <errno.h>
+#include <malloc.h>
+#include <getopt.h>
+#include <spi/spi.h>
+
+static int do_spi(int argc, char *argv[])
+{
+	struct spi_device spi;
+	int bus = 0;
+	int read = 0;
+	int verbose = 0;
+	int opt, count, i, ret;
+	int byte_per_word;
+
+	u8 *tx_buf, *rx_buf;
+
+	memset(&spi, 0, sizeof(struct spi_device));
+
+	spi.max_speed_hz = 1 * 1000 * 1000;
+	spi.bits_per_word = 8;
+
+	while ((opt = getopt(argc, argv, "b:c:r:m:f:w:v")) > 0) {
+		switch (opt) {
+		case 'b':
+			bus = simple_strtol(optarg, NULL, 0);
+			break;
+		case 'r':
+			read = simple_strtol(optarg, NULL, 0);
+			break;
+		case 'c':
+			spi.chip_select = simple_strtoul(optarg, NULL, 0);
+			break;
+		case 'm':
+			spi.mode = simple_strtoul(optarg, NULL, 0);
+			break;
+		case 'f':
+			spi.max_speed_hz = simple_strtoul(optarg, NULL, 0);
+			break;
+		case 'w':
+			spi.bits_per_word = simple_strtoul(optarg, NULL, 0);
+			break;
+		case 'v':
+			verbose = 1;
+			break;
+		default:
+			return COMMAND_ERROR_USAGE;
+		}
+	}
+
+	count = argc - optind;
+
+	if ((!read && !count) || !spi.max_speed_hz || !spi.bits_per_word)
+		return COMMAND_ERROR_USAGE;
+
+
+	spi.master = spi_get_master(bus);
+	if (!spi.master) {
+		printf("spi bus %d not found\n", bus);
+		return -ENODEV;
+	}
+
+	if (spi.chip_select > spi.master->num_chipselect) {
+		printf("spi chip select (%d)> master num chipselect (%d)\n",
+			spi.chip_select, spi.master->num_chipselect);
+		return -EINVAL;
+	}
+
+	ret = spi.master->setup(&spi);
+	if (ret) {
+		printf("can not setup the master (%d)\n", ret);
+		return ret;
+	}
+
+	tx_buf = xmalloc(count);
+	rx_buf = xmalloc(count);
+
+	for (i = 0; i < count; i++)
+		tx_buf[i] = (u8) simple_strtol(argv[optind + i], NULL, 16);
+
+	ret = spi_write_then_read(&spi, tx_buf, count, rx_buf, read);
+	if (ret)
+		goto out;
+
+	byte_per_word = max(spi.bits_per_word / 8, 1);
+	if (verbose) {
+		printf("device config\n");
+		printf("    bus_num       = %d\n", spi.master->bus_num);
+		printf("    max_speed_hz  = %d\n", spi.max_speed_hz);
+		printf("    chip_select   = %d\n", spi.chip_select);
+		printf("    mode          = 0x%x\n", spi.mode);
+		printf("    bits_per_word = %d\n", spi.bits_per_word);
+		printf("\n");
+
+		printf("wrote %i bytes\n", count);
+		memory_display(tx_buf, 0, count, byte_per_word);
+
+		printf("read %i bytes\n", read);
+	}
+
+	memory_display(rx_buf, 0, read, byte_per_word);
+
+out:
+	free(rx_buf);
+	free(tx_buf);
+	return ret;
+}
+
+static const __maybe_unused char cmd_spi_help[] =
+"Usage: spi [OPTION] [data to write 0xXX]\n"
+"write/read spi device.\n"
+"  -b <bus_num>  spi bus number (default = 0)\n"
+"  -r <count>    to read\n"
+"  -c <cs>       chip select (default = 0)\n"
+"  -m <mode>     spi mode (default = 0)\n"
+"  -f <hz>       max_speed_hz (default = 1MHz)\n"
+"  -w <bit>      bits_per_word (default = 8)\n"
+"  -v            verbose\n";
+
+BAREBOX_CMD_START(spi)
+	.cmd		= do_spi,
+	.usage		= "write/read spi device",
+	BAREBOX_CMD_HELP(cmd_spi_help)
+BAREBOX_CMD_END
-- 
1.7.10.4


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

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

* [PATCH 2/2] introduce spi command
  2012-11-05  9:36 ` [PATCH 1/2] spi: introduce spi_get_master Jean-Christophe PLAGNIOL-VILLARD
@ 2012-11-05  9:36   ` Jean-Christophe PLAGNIOL-VILLARD
  0 siblings, 0 replies; 9+ messages in thread
From: Jean-Christophe PLAGNIOL-VILLARD @ 2012-11-05  9:36 UTC (permalink / raw)
  To: barebox

usefull to debug spi

The command allow to read/write on a spi device

Usage: spi [OPTION] [data to write 0xXX]
write/read spi device.
  -b <bus_num>  spi bus number (default = 0)
  -r <count>    to read
  -c <cs>       chip select (default = 0)
  -m <mode>     spi mode (default = 0)
  -f <hz>       max_speed_hz (default = 1MHz)
  -w <bit>      bits_per_word (default = 8)
  -v            verbose

Signed-off-by: Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
---
 commands/Kconfig        |    6 +++
 commands/Makefile       |    1 +
 commands/spi.c          |  132 +++++++++++++++++++++++++++++++++++++++++++++++
 drivers/spi/atmel_spi.c |    1 +
 4 files changed, 140 insertions(+)
 create mode 100644 commands/spi.c

diff --git a/commands/Kconfig b/commands/Kconfig
index 16706d3..f7cbd67 100644
--- a/commands/Kconfig
+++ b/commands/Kconfig
@@ -597,6 +597,12 @@ config CMD_I2C
 	  include i2c_probe, i2c_read and i2c_write commands to communicate
 	  on i2c bus.
 
+config CMD_SPI
+	bool
+	depends on SPI
+	prompt "spi command"
+	help
+
 config CMD_LED
 	bool
 	depends on LED
diff --git a/commands/Makefile b/commands/Makefile
index 610be55..19d496e 100644
--- a/commands/Makefile
+++ b/commands/Makefile
@@ -55,6 +55,7 @@ obj-$(CONFIG_USB_GADGET_SERIAL)	+= usbserial.o
 obj-$(CONFIG_CMD_GPIO)		+= gpio.o
 obj-$(CONFIG_CMD_UNCOMPRESS)	+= uncompress.o
 obj-$(CONFIG_CMD_I2C)		+= i2c.o
+obj-$(CONFIG_CMD_SPI)		+= spi.o
 obj-$(CONFIG_CMD_UBI)		+= ubi.o
 obj-$(CONFIG_CMD_MENU)		+= menu.o
 obj-$(CONFIG_CMD_PASSWD)	+= passwd.o
diff --git a/commands/spi.c b/commands/spi.c
new file mode 100644
index 0000000..32a047d
--- /dev/null
+++ b/commands/spi.c
@@ -0,0 +1,132 @@
+/*
+ * Copyright (c) 2012 Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
+ *
+ * Under GPLv2 only
+ *
+ */
+
+#include <common.h>
+#include <command.h>
+#include <errno.h>
+#include <malloc.h>
+#include <getopt.h>
+#include <spi/spi.h>
+
+static int do_spi(int argc, char *argv[])
+{
+	struct spi_device spi;
+	int bus = 0;
+	int read = 0;
+	int verbose = 0;
+	int opt, count, i, ret;
+	int byte_per_word;
+
+	u8 *tx_buf, *rx_buf;
+
+	memset(&spi, 0, sizeof(struct spi_device));
+
+	spi.max_speed_hz = 1 * 1000 * 1000;
+	spi.bits_per_word = 8;
+
+	while ((opt = getopt(argc, argv, "b:c:r:m:f:w:v")) > 0) {
+		switch (opt) {
+		case 'b':
+			bus = simple_strtol(optarg, NULL, 0);
+			break;
+		case 'r':
+			read = simple_strtol(optarg, NULL, 0);
+			break;
+		case 'c':
+			spi.chip_select = simple_strtoul(optarg, NULL, 0);
+			break;
+		case 'm':
+			spi.mode = simple_strtoul(optarg, NULL, 0);
+			break;
+		case 'f':
+			spi.max_speed_hz = simple_strtoul(optarg, NULL, 0);
+			break;
+		case 'w':
+			spi.bits_per_word = simple_strtoul(optarg, NULL, 0);
+			break;
+		case 'v':
+			verbose = 1;
+			break;
+		default:
+			return COMMAND_ERROR_USAGE;
+		}
+	}
+
+	count = argc - optind;
+
+	if ((!read && !count) || !spi.max_speed_hz || !spi.bits_per_word)
+		return COMMAND_ERROR_USAGE;
+
+
+	spi.master = spi_get_master(bus);
+	if (!spi.master) {
+		printf("spi bus %d not found\n", bus);
+		return -ENODEV;
+	}
+
+	if (spi.chip_select > spi.master->num_chipselect) {
+		printf("spi chip select (%d)> master num chipselect (%d)\n",
+			spi.chip_select, spi.master->num_chipselect);
+		return -EINVAL;
+	}
+
+	ret = spi.master->setup(&spi);
+	if (ret) {
+		printf("can not setup the master (%d)\n", ret);
+		return -ret;
+	}
+
+	tx_buf = xmalloc(count);
+	rx_buf = xmalloc(count);
+
+	for (i = 0; i < count; i++)
+		tx_buf[i] = (u8) simple_strtol(argv[optind + i], NULL, 16);
+
+	ret = spi_write_then_read(&spi, tx_buf, count, rx_buf, read);
+	if (ret)
+		goto out;
+
+	byte_per_word = max(spi.bits_per_word / 8, 1);
+	if (verbose) {
+		printf("device config\n");
+		printf("    bus_num       = %d\n", spi.master->bus_num);
+		printf("    max_speed_hz  = %d\n", spi.max_speed_hz);
+		printf("    chip_select   = %d\n", spi.chip_select);
+		printf("    mode          = 0x%x\n", spi.mode);
+		printf("    bits_per_word = %d\n", spi.bits_per_word);
+		printf("\n");
+
+		printf("wrote %i bytes\n", count);
+		memory_display(tx_buf, 0, count, byte_per_word);
+
+		printf("read %i bytes\n", read);
+	}
+
+	memory_display(rx_buf, 0, read, byte_per_word);
+
+out:
+	free(rx_buf);
+	free(tx_buf);
+	return ret;
+}
+
+static const __maybe_unused char cmd_spi_help[] =
+"Usage: spi [OPTION] [data to write 0xXX]\n"
+"write/read spi device.\n"
+"  -b <bus_num>  spi bus number (default = 0)\n"
+"  -r <count>    to read\n"
+"  -c <cs>       chip select (default = 0)\n"
+"  -m <mode>     spi mode (default = 0)\n"
+"  -f <hz>       max_speed_hz (default = 1MHz)\n"
+"  -w <bit>      bits_per_word (default = 8)\n"
+"  -v            verbose\n";
+
+BAREBOX_CMD_START(spi)
+	.cmd		= do_spi,
+	.usage		= "write/read spi device",
+	BAREBOX_CMD_HELP(cmd_spi_help)
+BAREBOX_CMD_END
diff --git a/drivers/spi/atmel_spi.c b/drivers/spi/atmel_spi.c
index 43aec8e..4a6ed96 100644
--- a/drivers/spi/atmel_spi.c
+++ b/drivers/spi/atmel_spi.c
@@ -21,6 +21,7 @@
  *
  *
  */
+#define DEBUG
 
 #include <common.h>
 #include <init.h>
-- 
1.7.10.4


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

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

* Re: [PATCH 2/2] introduce spi command
  2012-11-02 16:17   ` [PATCH 2/2] introduce spi command Jean-Christophe PLAGNIOL-VILLARD
  2012-11-02 16:26     ` Alexander Shiyan
@ 2012-11-03 21:39     ` Sascha Hauer
  1 sibling, 0 replies; 9+ messages in thread
From: Sascha Hauer @ 2012-11-03 21:39 UTC (permalink / raw)
  To: Jean-Christophe PLAGNIOL-VILLARD; +Cc: barebox

On Fri, Nov 02, 2012 at 05:17:40PM +0100, Jean-Christophe PLAGNIOL-VILLARD wrote:
> usefull to debug spi
> 
> The command allow to read/write on a spi device
> 
> Usage: spi [OPTION] [data to write 0xXX]
> write/read spi device.
>   -b <bus_num>  spi bus number (default = 0)
>   -r <count>    to read
>   -c <cs>       chip select (default = 0)
>   -m <mode>     spi mode (default = 0)
>   -f <hz>       max_speed_hz (default = 1MHz)
>   -w <bit>      bits_per_word (default = 8)
>   -v            verbose
> 
> Signed-off-by: Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
> ---
>  commands/Kconfig  |    6 +++
>  commands/Makefile |    1 +
>  commands/spi.c    |  126 +++++++++++++++++++++++++++++++++++++++++++++++++++++
>  3 files changed, 133 insertions(+)
>  create mode 100644 commands/spi.c
> 
> diff --git a/commands/Kconfig b/commands/Kconfig
> index 16706d3..f7cbd67 100644
> --- a/commands/Kconfig
> +++ b/commands/Kconfig
> @@ -597,6 +597,12 @@ config CMD_I2C
>  	  include i2c_probe, i2c_read and i2c_write commands to communicate
>  	  on i2c bus.
>  
> +config CMD_SPI
> +	bool
> +	depends on SPI
> +	prompt "spi command"
> +	help
> +
>  config CMD_LED
>  	bool
>  	depends on LED
> diff --git a/commands/Makefile b/commands/Makefile
> index 610be55..19d496e 100644
> --- a/commands/Makefile
> +++ b/commands/Makefile
> @@ -55,6 +55,7 @@ obj-$(CONFIG_USB_GADGET_SERIAL)	+= usbserial.o
>  obj-$(CONFIG_CMD_GPIO)		+= gpio.o
>  obj-$(CONFIG_CMD_UNCOMPRESS)	+= uncompress.o
>  obj-$(CONFIG_CMD_I2C)		+= i2c.o
> +obj-$(CONFIG_CMD_SPI)		+= spi.o
>  obj-$(CONFIG_CMD_UBI)		+= ubi.o
>  obj-$(CONFIG_CMD_MENU)		+= menu.o
>  obj-$(CONFIG_CMD_PASSWD)	+= passwd.o
> diff --git a/commands/spi.c b/commands/spi.c
> new file mode 100644
> index 0000000..599637c
> --- /dev/null
> +++ b/commands/spi.c
> @@ -0,0 +1,126 @@
> +/*
> + * Copyright (c) 2012 Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
> + *
> + * Under GPLv2 only
> + *
> + */
> +
> +#include <common.h>
> +#include <command.h>
> +#include <errno.h>
> +#include <malloc.h>
> +#include <getopt.h>
> +#include <spi/spi.h>
> +
> +static int do_spi(int argc, char *argv[])
> +{
> +	struct spi_device spi;
> +	int bus = 0;
> +	int read = 0;
> +	int verbose = 0;
> +	int opt, count, i, ret;
> +
> +	u8 *tx_buf, *rx_buf;
> +
> +	memset(&spi, 0, sizeof(struct spi_device));
> +
> +	spi.max_speed_hz = 1 * 1000 * 1000;
> +	spi.bits_per_word = 8;
> +
> +	while ((opt = getopt(argc, argv, "b:c:r:m:f:w:v")) > 0) {
> +		switch (opt) {
> +		case 'b':
> +			bus = simple_strtol(optarg, NULL, 0);
> +			break;
> +		case 'r':
> +			read = simple_strtol(optarg, NULL, 0);
> +			break;
> +		case 'c':
> +			spi.chip_select = simple_strtoul(optarg, NULL, 0);
> +			break;
> +		case 'm':
> +			spi.mode = simple_strtoul(optarg, NULL, 0);
> +			break;
> +		case 'f':
> +			spi.max_speed_hz = simple_strtoul(optarg, NULL, 0);
> +			break;
> +		case 'w':
> +			spi.bits_per_word = simple_strtoul(optarg, NULL, 0);
> +			break;
> +		case 'v':
> +			verbose = 1;
> +			break;

		default:
			return COMMAND_ERROR_USAGE;

I still didn't find the time to fix the other commands, but for new ones
we should get used to it.

Sascha

-- 
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] 9+ messages in thread

* Re: [PATCH 2/2] introduce spi command
  2012-11-02 16:26     ` Alexander Shiyan
@ 2012-11-02 16:45       ` Jean-Christophe PLAGNIOL-VILLARD
  0 siblings, 0 replies; 9+ messages in thread
From: Jean-Christophe PLAGNIOL-VILLARD @ 2012-11-02 16:45 UTC (permalink / raw)
  To: Alexander Shiyan; +Cc: barebox

On 20:26 Fri 02 Nov     , Alexander Shiyan wrote:
> Fri,  2 Nov 2012 17:17:40 +0100 от Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>:
> > usefull to debug spi
> > 
> > The command allow to read/write on a spi device
> > 
> > Usage: spi [OPTION] [data to write 0xXX]
> > write/read spi device.
> >   -b <bus_num>  spi bus number (default = 0)
> >   -r <count>    to read
> >   -c <cs>       chip select (default = 0)
> >   -m <mode>     spi mode (default = 0)
> >   -f <hz>       max_speed_hz (default = 1MHz)
> >   -w <bit>      bits_per_word (default = 8)
> >   -v            verbose
> ...
> > +static int do_spi(int argc, char *argv[])
> > +{
> > +	struct spi_device spi;
> > +	int bus = 0;
> > +	int read = 0;
> > +	int verbose = 0;
> > +	int opt, count, i, ret;
> > +
> > +	u8 *tx_buf, *rx_buf;
> > +
> > +	memset(&spi, 0, sizeof(struct spi_device));
> > +
> > +	spi.max_speed_hz = 1 * 1000 * 1000;
> > +	spi.bits_per_word = 8;
> 
> I suggest using the default data from master for these parameters.
no otherwise we can not write the help and bits_et_word it's default value at
spi framework level is 8

and thh max_speed_hz is slow already

and it has always been the device to control it no the master

Best Regards,
J.

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

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

* Re: [PATCH 2/2] introduce spi command
  2012-11-02 16:17   ` [PATCH 2/2] introduce spi command Jean-Christophe PLAGNIOL-VILLARD
@ 2012-11-02 16:26     ` Alexander Shiyan
  2012-11-02 16:45       ` Jean-Christophe PLAGNIOL-VILLARD
  2012-11-03 21:39     ` Sascha Hauer
  1 sibling, 1 reply; 9+ messages in thread
From: Alexander Shiyan @ 2012-11-02 16:26 UTC (permalink / raw)
  To: Jean-Christophe PLAGNIOL-VILLARD; +Cc: barebox

Fri,  2 Nov 2012 17:17:40 +0100 от Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>:
> usefull to debug spi
> 
> The command allow to read/write on a spi device
> 
> Usage: spi [OPTION] [data to write 0xXX]
> write/read spi device.
>   -b <bus_num>  spi bus number (default = 0)
>   -r <count>    to read
>   -c <cs>       chip select (default = 0)
>   -m <mode>     spi mode (default = 0)
>   -f <hz>       max_speed_hz (default = 1MHz)
>   -w <bit>      bits_per_word (default = 8)
>   -v            verbose
...
> +static int do_spi(int argc, char *argv[])
> +{
> +	struct spi_device spi;
> +	int bus = 0;
> +	int read = 0;
> +	int verbose = 0;
> +	int opt, count, i, ret;
> +
> +	u8 *tx_buf, *rx_buf;
> +
> +	memset(&spi, 0, sizeof(struct spi_device));
> +
> +	spi.max_speed_hz = 1 * 1000 * 1000;
> +	spi.bits_per_word = 8;

I suggest using the default data from master for these parameters.

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

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

* [PATCH 2/2] introduce spi command
  2012-11-02 16:17 ` [PATCH 1/2] spi: introduce spi_get_master Jean-Christophe PLAGNIOL-VILLARD
@ 2012-11-02 16:17   ` Jean-Christophe PLAGNIOL-VILLARD
  2012-11-02 16:26     ` Alexander Shiyan
  2012-11-03 21:39     ` Sascha Hauer
  0 siblings, 2 replies; 9+ messages in thread
From: Jean-Christophe PLAGNIOL-VILLARD @ 2012-11-02 16:17 UTC (permalink / raw)
  To: barebox

usefull to debug spi

The command allow to read/write on a spi device

Usage: spi [OPTION] [data to write 0xXX]
write/read spi device.
  -b <bus_num>  spi bus number (default = 0)
  -r <count>    to read
  -c <cs>       chip select (default = 0)
  -m <mode>     spi mode (default = 0)
  -f <hz>       max_speed_hz (default = 1MHz)
  -w <bit>      bits_per_word (default = 8)
  -v            verbose

Signed-off-by: Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
---
 commands/Kconfig  |    6 +++
 commands/Makefile |    1 +
 commands/spi.c    |  126 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 133 insertions(+)
 create mode 100644 commands/spi.c

diff --git a/commands/Kconfig b/commands/Kconfig
index 16706d3..f7cbd67 100644
--- a/commands/Kconfig
+++ b/commands/Kconfig
@@ -597,6 +597,12 @@ config CMD_I2C
 	  include i2c_probe, i2c_read and i2c_write commands to communicate
 	  on i2c bus.
 
+config CMD_SPI
+	bool
+	depends on SPI
+	prompt "spi command"
+	help
+
 config CMD_LED
 	bool
 	depends on LED
diff --git a/commands/Makefile b/commands/Makefile
index 610be55..19d496e 100644
--- a/commands/Makefile
+++ b/commands/Makefile
@@ -55,6 +55,7 @@ obj-$(CONFIG_USB_GADGET_SERIAL)	+= usbserial.o
 obj-$(CONFIG_CMD_GPIO)		+= gpio.o
 obj-$(CONFIG_CMD_UNCOMPRESS)	+= uncompress.o
 obj-$(CONFIG_CMD_I2C)		+= i2c.o
+obj-$(CONFIG_CMD_SPI)		+= spi.o
 obj-$(CONFIG_CMD_UBI)		+= ubi.o
 obj-$(CONFIG_CMD_MENU)		+= menu.o
 obj-$(CONFIG_CMD_PASSWD)	+= passwd.o
diff --git a/commands/spi.c b/commands/spi.c
new file mode 100644
index 0000000..599637c
--- /dev/null
+++ b/commands/spi.c
@@ -0,0 +1,126 @@
+/*
+ * Copyright (c) 2012 Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
+ *
+ * Under GPLv2 only
+ *
+ */
+
+#include <common.h>
+#include <command.h>
+#include <errno.h>
+#include <malloc.h>
+#include <getopt.h>
+#include <spi/spi.h>
+
+static int do_spi(int argc, char *argv[])
+{
+	struct spi_device spi;
+	int bus = 0;
+	int read = 0;
+	int verbose = 0;
+	int opt, count, i, ret;
+
+	u8 *tx_buf, *rx_buf;
+
+	memset(&spi, 0, sizeof(struct spi_device));
+
+	spi.max_speed_hz = 1 * 1000 * 1000;
+	spi.bits_per_word = 8;
+
+	while ((opt = getopt(argc, argv, "b:c:r:m:f:w:v")) > 0) {
+		switch (opt) {
+		case 'b':
+			bus = simple_strtol(optarg, NULL, 0);
+			break;
+		case 'r':
+			read = simple_strtol(optarg, NULL, 0);
+			break;
+		case 'c':
+			spi.chip_select = simple_strtoul(optarg, NULL, 0);
+			break;
+		case 'm':
+			spi.mode = simple_strtoul(optarg, NULL, 0);
+			break;
+		case 'f':
+			spi.max_speed_hz = simple_strtoul(optarg, NULL, 0);
+			break;
+		case 'w':
+			spi.bits_per_word = simple_strtoul(optarg, NULL, 0);
+			break;
+		case 'v':
+			verbose = 1;
+			break;
+		}
+	}
+
+	count = argc - optind;
+
+	if ((!read && !count) || !spi.max_speed_hz || !spi.bits_per_word)
+		return COMMAND_ERROR_USAGE;
+
+
+	spi.master = spi_get_master(bus);
+	if (!spi.master) {
+		printf("spi bus %d not found\n", bus);
+		return -ENODEV;
+	}
+
+	if (spi.chip_select > spi.master->num_chipselect) {
+		printf("spi chip select (%d)> master num chipselect (%d)\n",
+			spi.chip_select, spi.master->num_chipselect);
+		return -EINVAL;
+	}
+
+	tx_buf = xmalloc(count);
+	rx_buf = xmalloc(count);
+
+	for (i = 0; i < count; i++)
+		tx_buf[i] = (char) simple_strtol(argv[optind+i], NULL, 16);
+
+	ret = spi_write_then_read(&spi, tx_buf, count, rx_buf, read);
+	if (ret)
+		goto out;
+
+	if (verbose) {
+		printf("device config\n");
+		printf("    bus_num       = %d\n", spi.master->bus_num);
+		printf("    max_speed_hz  = %d\n", spi.max_speed_hz);
+		printf("    chip_select   = %d\n", spi.chip_select);
+		printf("    mode          = 0x%x\n", spi.mode);
+		printf("    bits_per_word = %d\n", spi.bits_per_word);
+		printf("\n");
+
+		printf("wrote %i bytes\n", count);
+		for (i = 0; i < count; i++)
+			printf("0x%02x ", tx_buf[i]);
+		printf("\n");
+
+		printf("read %i bytes\n", read);
+	}
+
+	for (i = 0; i < read; i++)
+		printf("0x%02x ", rx_buf[i]);
+	printf("\n");
+
+out:
+	free(rx_buf);
+	free(tx_buf);
+	return ret;
+}
+
+static const __maybe_unused char cmd_spi_help[] =
+"Usage: spi [OPTION] [data to write 0xXX]\n"
+"write/read spi device.\n"
+"  -b <bus_num>  spi bus number (default = 0)\n"
+"  -r <count>    to read\n"
+"  -c <cs>       chip select (default = 0)\n"
+"  -m <mode>     spi mode (default = 0)\n"
+"  -f <hz>       max_speed_hz (default = 1MHz)\n"
+"  -w <bit>      bits_per_word (default = 8)\n"
+"  -v            verbose\n";
+
+BAREBOX_CMD_START(spi)
+	.cmd		= do_spi,
+	.usage		= "write/read spi device",
+	BAREBOX_CMD_HELP(cmd_spi_help)
+BAREBOX_CMD_END
-- 
1.7.10.4


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

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

end of thread, other threads:[~2012-11-05 18:32 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-11-04  8:54 [PATCH 0/2 v2] add spi command Jean-Christophe PLAGNIOL-VILLARD
2012-11-04  8:59 ` [PATCH 1/2] spi: introduce spi_get_master Jean-Christophe PLAGNIOL-VILLARD
2012-11-04  8:59   ` [PATCH 2/2] introduce spi command Jean-Christophe PLAGNIOL-VILLARD
  -- strict thread matches above, loose matches on Subject: below --
2012-11-05 18:21 [PATCH 0/2 v4] add " Jean-Christophe PLAGNIOL-VILLARD
2012-11-05 18:30 ` [PATCH 1/2] spi: introduce spi_get_master Jean-Christophe PLAGNIOL-VILLARD
2012-11-05 18:30   ` [PATCH 2/2] introduce spi command Jean-Christophe PLAGNIOL-VILLARD
2012-11-05  9:29 [PATCH 0/2 v3] add " Jean-Christophe PLAGNIOL-VILLARD
2012-11-05  9:36 ` [PATCH 1/2] spi: introduce spi_get_master Jean-Christophe PLAGNIOL-VILLARD
2012-11-05  9:36   ` [PATCH 2/2] introduce spi command Jean-Christophe PLAGNIOL-VILLARD
2012-11-02 16:06 [PATCH 0/2] add " Jean-Christophe PLAGNIOL-VILLARD
2012-11-02 16:17 ` [PATCH 1/2] spi: introduce spi_get_master Jean-Christophe PLAGNIOL-VILLARD
2012-11-02 16:17   ` [PATCH 2/2] introduce spi command Jean-Christophe PLAGNIOL-VILLARD
2012-11-02 16:26     ` Alexander Shiyan
2012-11-02 16:45       ` Jean-Christophe PLAGNIOL-VILLARD
2012-11-03 21:39     ` Sascha Hauer

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