]>
git.ipfire.org Git - people/ms/u-boot.git/blob - common/cmd_spi.c
3 * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com
5 * SPDX-License-Identifier: GPL-2.0+
9 * SPI Read/Write Utilities
18 /*-----------------------------------------------------------------------
23 # define MAX_SPI_BYTES 32 /* Maximum number of bytes we can handle */
26 #ifndef CONFIG_DEFAULT_SPI_BUS
27 # define CONFIG_DEFAULT_SPI_BUS 0
29 #ifndef CONFIG_DEFAULT_SPI_MODE
30 # define CONFIG_DEFAULT_SPI_MODE SPI_MODE_0
34 * Values from last command.
36 static unsigned int bus
;
37 static unsigned int cs
;
38 static unsigned int mode
;
40 static uchar dout
[MAX_SPI_BYTES
];
41 static uchar din
[MAX_SPI_BYTES
];
43 static int do_spi_xfer(int bus
, int cs
)
45 struct spi_slave
*slave
;
52 snprintf(name
, sizeof(name
), "generic_%d:%d", bus
, cs
);
54 ret
= spi_get_bus_and_cs(bus
, cs
, 1000000, mode
, "spi_generic_drv",
59 slave
= spi_setup_slave(bus
, cs
, 1000000, mode
);
61 printf("Invalid device %d:%d\n", bus
, cs
);
66 ret
= spi_claim_bus(slave
);
69 ret
= spi_xfer(slave
, bitlen
, dout
, din
,
70 SPI_XFER_BEGIN
| SPI_XFER_END
);
72 /* We don't get an error code in this case */
77 printf("Error %d during SPI transaction\n", ret
);
81 for (j
= 0; j
< ((bitlen
+ 7) / 8); j
++)
82 printf("%02X", din
[j
]);
86 spi_release_bus(slave
);
88 spi_free_slave(slave
);
98 * spi {dev} {num_bits} {dout}
99 * {dev} is the device number for controlling chip select (see TBD)
100 * {num_bits} is the number of bits to send & receive (base 10)
101 * {dout} is a hexadecimal string of data to send
102 * The command prints out the hexadecimal string received via SPI.
105 int do_spi (cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
112 * We use the last specified parameters, unless new ones are
116 if ((flag
& CMD_FLAG_REPEAT
) == 0)
119 mode
= CONFIG_DEFAULT_SPI_MODE
;
120 bus
= simple_strtoul(argv
[1], &cp
, 10);
122 cs
= simple_strtoul(cp
+1, &cp
, 10);
125 bus
= CONFIG_DEFAULT_SPI_BUS
;
128 mode
= simple_strtoul(cp
+1, NULL
, 10);
131 bitlen
= simple_strtoul(argv
[2], NULL
, 10);
134 for(j
= 0; *cp
; j
++, cp
++) {
137 tmp
-= ('A' - '0') - 10;
141 printf("Hex conversion error on %c\n", *cp
);
145 dout
[j
/ 2] = (tmp
<< 4);
152 if ((bitlen
< 0) || (bitlen
> (MAX_SPI_BYTES
* 8))) {
153 printf("Invalid bitlen %d\n", bitlen
);
157 if (do_spi_xfer(bus
, cs
))
163 /***************************************************/
167 "SPI utility command",
168 "[<bus>:]<cs>[.<mode>] <bit_len> <dout> - Send and receive bits\n"
169 "<bus> - Identifies the SPI bus\n"
170 "<cs> - Identifies the chip select\n"
171 "<mode> - Identifies the SPI mode to use\n"
172 "<bit_len> - Number of bits to send (base 10)\n"
173 "<dout> - Hexadecimal string that gets sent"