cmd_spi.c 2.8 KB

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