From mboxrd@z Thu Jan 1 00:00:00 1970 Return-path: Received: from 6.mo5.mail-out.ovh.net ([178.32.119.138] helo=mo5.mail-out.ovh.net) by merlin.infradead.org with esmtp (Exim 4.76 #1 (Red Hat Linux)) id 1TUJyO-0003db-BG for barebox@lists.infradead.org; Fri, 02 Nov 2012 16:19:57 +0000 Received: from mail183.ha.ovh.net (b6.ovh.net [213.186.33.56]) by mo5.mail-out.ovh.net (Postfix) with SMTP id 21EF9FFB1A4 for ; Fri, 2 Nov 2012 17:27:06 +0100 (CET) From: Jean-Christophe PLAGNIOL-VILLARD Date: Fri, 2 Nov 2012 17:17:40 +0100 Message-Id: <1351873060-27968-2-git-send-email-plagnioj@jcrosoft.com> In-Reply-To: <1351873060-27968-1-git-send-email-plagnioj@jcrosoft.com> References: <20121102160659.GS29599@game.jcrosoft.org> <1351873060-27968-1-git-send-email-plagnioj@jcrosoft.com> List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: barebox-bounces@lists.infradead.org Errors-To: barebox-bounces+u.kleine-koenig=pengutronix.de@lists.infradead.org Subject: [PATCH 2/2] introduce spi command To: barebox@lists.infradead.org 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 spi bus number (default = 0) -r to read -c chip select (default = 0) -m spi mode (default = 0) -f max_speed_hz (default = 1MHz) -w bits_per_word (default = 8) -v verbose Signed-off-by: Jean-Christophe PLAGNIOL-VILLARD --- 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 + * + * Under GPLv2 only + * + */ + +#include +#include +#include +#include +#include +#include + +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 spi bus number (default = 0)\n" +" -r to read\n" +" -c chip select (default = 0)\n" +" -m spi mode (default = 0)\n" +" -f max_speed_hz (default = 1MHz)\n" +" -w 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