cmd_spi.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. /*-----------------------------------------------------------------------
  30. * Definitions
  31. */
  32. #ifndef MAX_SPI_BYTES
  33. # define MAX_SPI_BYTES 32 /* Maximum number of bytes we can handle */
  34. #endif
  35. #ifndef CONFIG_DEFAULT_SPI_BUS
  36. # define CONFIG_DEFAULT_SPI_BUS 0
  37. #endif
  38. #ifndef CONFIG_DEFAULT_SPI_MODE
  39. # define CONFIG_DEFAULT_SPI_MODE SPI_MODE_0
  40. #endif
  41. /*
  42. * Values from last command.
  43. */
  44. static unsigned int device;
  45. static int bitlen;
  46. static uchar dout[MAX_SPI_BYTES];
  47. static uchar din[MAX_SPI_BYTES];
  48. /*
  49. * SPI read/write
  50. *
  51. * Syntax:
  52. * spi {dev} {num_bits} {dout}
  53. * {dev} is the device number for controlling chip select (see TBD)
  54. * {num_bits} is the number of bits to send & receive (base 10)
  55. * {dout} is a hexadecimal string of data to send
  56. * The command prints out the hexadecimal string received via SPI.
  57. */
  58. int do_spi (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  59. {
  60. struct spi_slave *slave;
  61. char *cp = 0;
  62. uchar tmp;
  63. int j;
  64. int rcode = 0;
  65. /*
  66. * We use the last specified parameters, unless new ones are
  67. * entered.
  68. */
  69. if ((flag & CMD_FLAG_REPEAT) == 0)
  70. {
  71. if (argc >= 2)
  72. device = simple_strtoul(argv[1], NULL, 10);
  73. if (argc >= 3)
  74. bitlen = simple_strtoul(argv[2], NULL, 10);
  75. if (argc >= 4) {
  76. cp = argv[3];
  77. for(j = 0; *cp; j++, cp++) {
  78. tmp = *cp - '0';
  79. if(tmp > 9)
  80. tmp -= ('A' - '0') - 10;
  81. if(tmp > 15)
  82. tmp -= ('a' - 'A');
  83. if(tmp > 15) {
  84. printf("Hex conversion error on %c, giving up.\n", *cp);
  85. return 1;
  86. }
  87. if((j % 2) == 0)
  88. dout[j / 2] = (tmp << 4);
  89. else
  90. dout[j / 2] |= tmp;
  91. }
  92. }
  93. }
  94. if ((bitlen < 0) || (bitlen > (MAX_SPI_BYTES * 8))) {
  95. printf("Invalid bitlen %d, giving up.\n", bitlen);
  96. return 1;
  97. }
  98. /* FIXME: Make these parameters run-time configurable */
  99. slave = spi_setup_slave(CONFIG_DEFAULT_SPI_BUS, device, 1000000,
  100. CONFIG_DEFAULT_SPI_MODE);
  101. if (!slave) {
  102. printf("Invalid device %d, giving up.\n", device);
  103. return 1;
  104. }
  105. debug ("spi chipsel = %08X\n", device);
  106. spi_claim_bus(slave);
  107. if(spi_xfer(slave, bitlen, dout, din,
  108. SPI_XFER_BEGIN | SPI_XFER_END) != 0) {
  109. printf("Error with the SPI transaction.\n");
  110. rcode = 1;
  111. } else {
  112. for(j = 0; j < ((bitlen + 7) / 8); j++) {
  113. printf("%02X", din[j]);
  114. }
  115. printf("\n");
  116. }
  117. spi_release_bus(slave);
  118. spi_free_slave(slave);
  119. return rcode;
  120. }
  121. /***************************************************/
  122. U_BOOT_CMD(
  123. sspi, 5, 1, do_spi,
  124. "SPI utility commands",
  125. "<device> <bit_len> <dout> - Send <bit_len> bits from <dout> out the SPI\n"
  126. "<device> - Identifies the chip select of the device\n"
  127. "<bit_len> - Number of bits to send (base 10)\n"
  128. "<dout> - Hexadecimal string that gets sent"
  129. );