cmd_spi.c 3.2 KB

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