Blame view
common/cmd_spi.c
3.03 KB
e3093091a Initial revision |
1 2 3 4 |
/* * (C) Copyright 2002 * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com * |
1a4596601 Add GPL-2.0+ SPDX... |
5 |
* SPDX-License-Identifier: GPL-2.0+ |
e3093091a Initial revision |
6 7 8 9 10 11 12 13 14 |
*/ /* * SPI Read/Write Utilities */ #include <common.h> #include <command.h> #include <spi.h> |
e3093091a Initial revision |
15 |
|
eb9401e3e * Patch by Andrea... |
16 17 18 19 20 21 22 |
/*----------------------------------------------------------------------- * Definitions */ #ifndef MAX_SPI_BYTES # define MAX_SPI_BYTES 32 /* Maximum number of bytes we can handle */ #endif |
e3093091a Initial revision |
23 |
|
d255bb0e7 SPI API improvements |
24 25 26 27 28 29 |
#ifndef CONFIG_DEFAULT_SPI_BUS # define CONFIG_DEFAULT_SPI_BUS 0 #endif #ifndef CONFIG_DEFAULT_SPI_MODE # define CONFIG_DEFAULT_SPI_MODE SPI_MODE_0 #endif |
e3093091a Initial revision |
30 31 32 33 |
/* * Values from last command. */ |
21032b35a sspi: add options... |
34 35 36 |
static unsigned int bus; static unsigned int cs; static unsigned int mode; |
d255bb0e7 SPI API improvements |
37 38 39 |
static int bitlen; static uchar dout[MAX_SPI_BYTES]; static uchar din[MAX_SPI_BYTES]; |
e3093091a Initial revision |
40 41 42 43 44 45 46 47 48 49 50 |
/* * SPI read/write * * Syntax: * spi {dev} {num_bits} {dout} * {dev} is the device number for controlling chip select (see TBD) * {num_bits} is the number of bits to send & receive (base 10) * {dout} is a hexadecimal string of data to send * The command prints out the hexadecimal string received via SPI. */ |
54841ab50 Make sure that ar... |
51 |
int do_spi (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
e3093091a Initial revision |
52 |
{ |
d255bb0e7 SPI API improvements |
53 |
struct spi_slave *slave; |
e3093091a Initial revision |
54 55 56 57 58 59 60 61 62 63 64 65 |
char *cp = 0; uchar tmp; int j; int rcode = 0; /* * We use the last specified parameters, unless new ones are * entered. */ if ((flag & CMD_FLAG_REPEAT) == 0) { |
21032b35a sspi: add options... |
66 67 68 69 70 71 72 73 74 |
if (argc >= 2) { mode = CONFIG_DEFAULT_SPI_MODE; bus = simple_strtoul(argv[1], &cp, 10); if (*cp == ':') { cs = simple_strtoul(cp+1, &cp, 10); } else { cs = bus; bus = CONFIG_DEFAULT_SPI_BUS; } |
2d5e7c7a4 cmd_spi: remove s... |
75 |
if (*cp == '.') |
21032b35a sspi: add options... |
76 77 |
mode = simple_strtoul(cp+1, NULL, 10); } |
e3093091a Initial revision |
78 79 |
if (argc >= 3) bitlen = simple_strtoul(argv[2], NULL, 10); |
eb9401e3e * Patch by Andrea... |
80 81 82 83 84 85 86 87 88 |
if (argc >= 4) { cp = argv[3]; for(j = 0; *cp; j++, cp++) { tmp = *cp - '0'; if(tmp > 9) tmp -= ('A' - '0') - 10; if(tmp > 15) tmp -= ('a' - 'A'); if(tmp > 15) { |
21032b35a sspi: add options... |
89 90 |
printf("Hex conversion error on %c ", *cp); |
eb9401e3e * Patch by Andrea... |
91 92 93 94 95 96 |
return 1; } if((j % 2) == 0) dout[j / 2] = (tmp << 4); else dout[j / 2] |= tmp; |
e3093091a Initial revision |
97 |
} |
e3093091a Initial revision |
98 99 |
} } |
eb9401e3e * Patch by Andrea... |
100 |
if ((bitlen < 0) || (bitlen > (MAX_SPI_BYTES * 8))) { |
21032b35a sspi: add options... |
101 102 |
printf("Invalid bitlen %d ", bitlen); |
eb9401e3e * Patch by Andrea... |
103 |
return 1; |
8bde7f776 * Code cleanup: |
104 |
} |
eb9401e3e * Patch by Andrea... |
105 |
|
21032b35a sspi: add options... |
106 |
slave = spi_setup_slave(bus, cs, 1000000, mode); |
d255bb0e7 SPI API improvements |
107 |
if (!slave) { |
21032b35a sspi: add options... |
108 109 |
printf("Invalid device %d:%d ", bus, cs); |
d255bb0e7 SPI API improvements |
110 111 |
return 1; } |
d255bb0e7 SPI API improvements |
112 113 114 |
spi_claim_bus(slave); if(spi_xfer(slave, bitlen, dout, din, SPI_XFER_BEGIN | SPI_XFER_END) != 0) { |
21032b35a sspi: add options... |
115 116 |
printf("Error during SPI transaction "); |
e3093091a Initial revision |
117 118 |
rcode = 1; } else { |
e3093091a Initial revision |
119 |
for(j = 0; j < ((bitlen + 7) / 8); j++) { |
c46980f6d cmd_spi: remove b... |
120 |
printf("%02X", din[j]); |
e3093091a Initial revision |
121 122 123 124 |
} printf(" "); } |
d255bb0e7 SPI API improvements |
125 126 |
spi_release_bus(slave); spi_free_slave(slave); |
e3093091a Initial revision |
127 128 129 |
return rcode; } |
8bde7f776 * Code cleanup: |
130 |
/***************************************************/ |
0d4983930 Patch by Kenneth ... |
131 132 |
U_BOOT_CMD( sspi, 5, 1, do_spi, |
21032b35a sspi: add options... |
133 134 135 136 137 138 139 140 141 |
"SPI utility command", "[<bus>:]<cs>[.<mode>] <bit_len> <dout> - Send and receive bits " "<bus> - Identifies the SPI bus " "<cs> - Identifies the chip select " "<mode> - Identifies the SPI mode to use " |
8bde7f776 * Code cleanup: |
142 143 |
"<bit_len> - Number of bits to send (base 10) " |
a89c33db9 General help mess... |
144 |
"<dout> - Hexadecimal string that gets sent" |
8bde7f776 * Code cleanup: |
145 |
); |