]>
git.ipfire.org Git - people/ms/u-boot.git/blob - common/cmd_spi.c
3 * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com
5 * See file CREDITS for list of people who contributed to this
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
25 * SPI Read/Write Utilities
32 /*-----------------------------------------------------------------------
37 # define MAX_SPI_BYTES 32 /* Maximum number of bytes we can handle */
40 #ifndef CONFIG_DEFAULT_SPI_BUS
41 # define CONFIG_DEFAULT_SPI_BUS 0
43 #ifndef CONFIG_DEFAULT_SPI_MODE
44 # define CONFIG_DEFAULT_SPI_MODE SPI_MODE_0
48 * Values from last command.
50 static unsigned int bus
;
51 static unsigned int cs
;
52 static unsigned int mode
;
54 static uchar dout
[MAX_SPI_BYTES
];
55 static uchar din
[MAX_SPI_BYTES
];
61 * spi {dev} {num_bits} {dout}
62 * {dev} is the device number for controlling chip select (see TBD)
63 * {num_bits} is the number of bits to send & receive (base 10)
64 * {dout} is a hexadecimal string of data to send
65 * The command prints out the hexadecimal string received via SPI.
68 int do_spi (cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
70 struct spi_slave
*slave
;
77 * We use the last specified parameters, unless new ones are
81 if ((flag
& CMD_FLAG_REPEAT
) == 0)
84 mode
= CONFIG_DEFAULT_SPI_MODE
;
85 bus
= simple_strtoul(argv
[1], &cp
, 10);
87 cs
= simple_strtoul(cp
+1, &cp
, 10);
90 bus
= CONFIG_DEFAULT_SPI_BUS
;
93 mode
= simple_strtoul(cp
+1, NULL
, 10);
96 bitlen
= simple_strtoul(argv
[2], NULL
, 10);
99 for(j
= 0; *cp
; j
++, cp
++) {
102 tmp
-= ('A' - '0') - 10;
106 printf("Hex conversion error on %c\n", *cp
);
110 dout
[j
/ 2] = (tmp
<< 4);
117 if ((bitlen
< 0) || (bitlen
> (MAX_SPI_BYTES
* 8))) {
118 printf("Invalid bitlen %d\n", bitlen
);
122 slave
= spi_setup_slave(bus
, cs
, 1000000, mode
);
124 printf("Invalid device %d:%d\n", bus
, cs
);
128 spi_claim_bus(slave
);
129 if(spi_xfer(slave
, bitlen
, dout
, din
,
130 SPI_XFER_BEGIN
| SPI_XFER_END
) != 0) {
131 printf("Error during SPI transaction\n");
134 for(j
= 0; j
< ((bitlen
+ 7) / 8); j
++) {
135 printf("%02X", din
[j
]);
139 spi_release_bus(slave
);
140 spi_free_slave(slave
);
145 /***************************************************/
149 "SPI utility command",
150 "[<bus>:]<cs>[.<mode>] <bit_len> <dout> - Send and receive bits\n"
151 "<bus> - Identifies the SPI bus\n"
152 "<cs> - Identifies the chip select\n"
153 "<mode> - Identifies the SPI mode to use\n"
154 "<bit_len> - Number of bits to send (base 10)\n"
155 "<dout> - Hexadecimal string that gets sent"