wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2002 |
| 3 | * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com |
| 4 | * |
| 5 | * See file CREDITS for list of people who contributed to this |
| 6 | * project. |
| 7 | * |
| 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. |
| 12 | * |
| 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. |
| 17 | * |
| 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, |
| 21 | * MA 02111-1307 USA |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * SPI Read/Write Utilities |
| 26 | */ |
| 27 | |
| 28 | #include <common.h> |
| 29 | #include <command.h> |
| 30 | #include <spi.h> |
| 31 | #include <cmd_spi.h> |
| 32 | |
| 33 | #if (CONFIG_COMMANDS & CFG_CMD_SPI) |
| 34 | |
wdenk | c217f6d | 2002-11-11 02:11:37 +0000 | [diff] [blame] | 35 | /*----------------------------------------------------------------------- |
| 36 | * Definitions |
| 37 | */ |
| 38 | |
| 39 | #ifndef MAX_SPI_BYTES |
| 40 | # define MAX_SPI_BYTES 32 /* Maximum number of bytes we can handle */ |
| 41 | #endif |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 42 | |
| 43 | /* |
| 44 | * External table of chip select functions (see the appropriate board |
| 45 | * support for the actual definition of the table). |
| 46 | */ |
| 47 | extern spi_chipsel_type spi_chipsel[]; |
wdenk | c217f6d | 2002-11-11 02:11:37 +0000 | [diff] [blame] | 48 | extern int spi_chipsel_cnt; |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 49 | |
| 50 | /* |
| 51 | * Values from last command. |
| 52 | */ |
| 53 | static int device; |
| 54 | static int bitlen; |
| 55 | static uchar dout[MAX_SPI_BYTES]; |
| 56 | static uchar din[MAX_SPI_BYTES]; |
| 57 | |
| 58 | /* |
| 59 | * SPI read/write |
| 60 | * |
| 61 | * Syntax: |
| 62 | * spi {dev} {num_bits} {dout} |
| 63 | * {dev} is the device number for controlling chip select (see TBD) |
| 64 | * {num_bits} is the number of bits to send & receive (base 10) |
| 65 | * {dout} is a hexadecimal string of data to send |
| 66 | * The command prints out the hexadecimal string received via SPI. |
| 67 | */ |
| 68 | |
wdenk | c217f6d | 2002-11-11 02:11:37 +0000 | [diff] [blame] | 69 | int do_spi (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 70 | { |
| 71 | char *cp = 0; |
| 72 | uchar tmp; |
| 73 | int j; |
| 74 | int rcode = 0; |
| 75 | |
| 76 | /* |
| 77 | * We use the last specified parameters, unless new ones are |
| 78 | * entered. |
| 79 | */ |
| 80 | |
| 81 | if ((flag & CMD_FLAG_REPEAT) == 0) |
| 82 | { |
| 83 | if (argc >= 2) |
| 84 | device = simple_strtoul(argv[1], NULL, 10); |
| 85 | if (argc >= 3) |
| 86 | bitlen = simple_strtoul(argv[2], NULL, 10); |
wdenk | c217f6d | 2002-11-11 02:11:37 +0000 | [diff] [blame] | 87 | if (argc >= 4) { |
| 88 | cp = argv[3]; |
| 89 | for(j = 0; *cp; j++, cp++) { |
| 90 | tmp = *cp - '0'; |
| 91 | if(tmp > 9) |
| 92 | tmp -= ('A' - '0') - 10; |
| 93 | if(tmp > 15) |
| 94 | tmp -= ('a' - 'A'); |
| 95 | if(tmp > 15) { |
| 96 | printf("Hex conversion error on %c, giving up.\n", *cp); |
| 97 | return 1; |
| 98 | } |
| 99 | if((j % 2) == 0) |
| 100 | dout[j / 2] = (tmp << 4); |
| 101 | else |
| 102 | dout[j / 2] |= tmp; |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 103 | } |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | |
wdenk | c217f6d | 2002-11-11 02:11:37 +0000 | [diff] [blame] | 107 | if ((device < 0) || (device >= spi_chipsel_cnt)) { |
| 108 | printf("Invalid device %d, giving up.\n", device); |
| 109 | return 1; |
| 110 | } |
| 111 | if ((bitlen < 0) || (bitlen > (MAX_SPI_BYTES * 8))) { |
| 112 | printf("Invalid bitlen %d, giving up.\n", bitlen); |
| 113 | return 1; |
| 114 | } |
| 115 | |
| 116 | debug ("spi_chipsel[%d] = %08X\n", |
| 117 | device, (uint)spi_chipsel[device]); |
| 118 | |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 119 | if(spi_xfer(spi_chipsel[device], bitlen, dout, din) != 0) { |
| 120 | printf("Error with the SPI transaction.\n"); |
| 121 | rcode = 1; |
| 122 | } else { |
| 123 | cp = din; |
| 124 | for(j = 0; j < ((bitlen + 7) / 8); j++) { |
| 125 | printf("%02X", *cp++); |
| 126 | } |
| 127 | printf("\n"); |
| 128 | } |
| 129 | |
| 130 | return rcode; |
| 131 | } |
| 132 | |
| 133 | #endif /* CFG_CMD_SPI */ |