cmd_spi.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 bus;
  45. static unsigned int cs;
  46. static unsigned int mode;
  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 * const argv[])
  61. {
  62. struct spi_slave *slave;
  63. char *cp = 0;
  64. uchar tmp;
  65. int j;
  66. int rcode = 0;
  67. /*
  68. * We use the last specified parameters, unless new ones are
  69. * entered.
  70. */
  71. if ((flag & CMD_FLAG_REPEAT) == 0)
  72. {
  73. if (argc >= 2) {
  74. mode = CONFIG_DEFAULT_SPI_MODE;
  75. bus = simple_strtoul(argv[1], &cp, 10);
  76. if (*cp == ':') {
  77. cs = simple_strtoul(cp+1, &cp, 10);
  78. } else {
  79. cs = bus;
  80. bus = CONFIG_DEFAULT_SPI_BUS;
  81. }
  82. if (*cp == '.')
  83. mode = simple_strtoul(cp+1, NULL, 10);
  84. }
  85. if (argc >= 3)
  86. bitlen = simple_strtoul(argv[2], NULL, 10);
  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\n", *cp);
  97. return 1;
  98. }
  99. if((j % 2) == 0)
  100. dout[j / 2] = (tmp << 4);
  101. else
  102. dout[j / 2] |= tmp;
  103. }
  104. }
  105. }
  106. if ((bitlen < 0) || (bitlen > (MAX_SPI_BYTES * 8))) {
  107. printf("Invalid bitlen %d\n", bitlen);
  108. return 1;
  109. }
  110. slave = spi_setup_slave(bus, cs, 1000000, mode);
  111. if (!slave) {
  112. printf("Invalid device %d:%d\n", bus, cs);
  113. return 1;
  114. }
  115. spi_claim_bus(slave);
  116. if(spi_xfer(slave, bitlen, dout, din,
  117. SPI_XFER_BEGIN | SPI_XFER_END) != 0) {
  118. printf("Error during SPI transaction\n");
  119. rcode = 1;
  120. } else {
  121. for(j = 0; j < ((bitlen + 7) / 8); j++) {
  122. printf("%02X", din[j]);
  123. }
  124. printf("\n");
  125. }
  126. spi_release_bus(slave);
  127. spi_free_slave(slave);
  128. return rcode;
  129. }
  130. /***************************************************/
  131. U_BOOT_CMD(
  132. sspi, 5, 1, do_spi,
  133. "SPI utility command",
  134. "[<bus>:]<cs>[.<mode>] <bit_len> <dout> - Send and receive bits\n"
  135. "<bus> - Identifies the SPI bus\n"
  136. "<cs> - Identifies the chip select\n"
  137. "<mode> - Identifies the SPI mode to use\n"
  138. "<bit_len> - Number of bits to send (base 10)\n"
  139. "<dout> - Hexadecimal string that gets sent"
  140. );