cmd_spi.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * (C) Copyright 2002
  3. * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. /*
  8. * SPI Read/Write Utilities
  9. */
  10. #include <common.h>
  11. #include <command.h>
  12. #include <spi.h>
  13. /*-----------------------------------------------------------------------
  14. * Definitions
  15. */
  16. #ifndef MAX_SPI_BYTES
  17. # define MAX_SPI_BYTES 32 /* Maximum number of bytes we can handle */
  18. #endif
  19. #ifndef CONFIG_DEFAULT_SPI_BUS
  20. # define CONFIG_DEFAULT_SPI_BUS 0
  21. #endif
  22. #ifndef CONFIG_DEFAULT_SPI_MODE
  23. # define CONFIG_DEFAULT_SPI_MODE SPI_MODE_0
  24. #endif
  25. /*
  26. * Values from last command.
  27. */
  28. static unsigned int bus;
  29. static unsigned int cs;
  30. static unsigned int mode;
  31. static int bitlen;
  32. static uchar dout[MAX_SPI_BYTES];
  33. static uchar din[MAX_SPI_BYTES];
  34. /*
  35. * SPI read/write
  36. *
  37. * Syntax:
  38. * spi {dev} {num_bits} {dout}
  39. * {dev} is the device number for controlling chip select (see TBD)
  40. * {num_bits} is the number of bits to send & receive (base 10)
  41. * {dout} is a hexadecimal string of data to send
  42. * The command prints out the hexadecimal string received via SPI.
  43. */
  44. int do_spi (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  45. {
  46. struct spi_slave *slave;
  47. char *cp = 0;
  48. uchar tmp;
  49. int j;
  50. int rcode = 0;
  51. /*
  52. * We use the last specified parameters, unless new ones are
  53. * entered.
  54. */
  55. if ((flag & CMD_FLAG_REPEAT) == 0)
  56. {
  57. if (argc >= 2) {
  58. mode = CONFIG_DEFAULT_SPI_MODE;
  59. bus = simple_strtoul(argv[1], &cp, 10);
  60. if (*cp == ':') {
  61. cs = simple_strtoul(cp+1, &cp, 10);
  62. } else {
  63. cs = bus;
  64. bus = CONFIG_DEFAULT_SPI_BUS;
  65. }
  66. if (*cp == '.')
  67. mode = simple_strtoul(cp+1, NULL, 10);
  68. }
  69. if (argc >= 3)
  70. bitlen = simple_strtoul(argv[2], NULL, 10);
  71. if (argc >= 4) {
  72. cp = argv[3];
  73. for(j = 0; *cp; j++, cp++) {
  74. tmp = *cp - '0';
  75. if(tmp > 9)
  76. tmp -= ('A' - '0') - 10;
  77. if(tmp > 15)
  78. tmp -= ('a' - 'A');
  79. if(tmp > 15) {
  80. printf("Hex conversion error on %c\n", *cp);
  81. return 1;
  82. }
  83. if((j % 2) == 0)
  84. dout[j / 2] = (tmp << 4);
  85. else
  86. dout[j / 2] |= tmp;
  87. }
  88. }
  89. }
  90. if ((bitlen < 0) || (bitlen > (MAX_SPI_BYTES * 8))) {
  91. printf("Invalid bitlen %d\n", bitlen);
  92. return 1;
  93. }
  94. slave = spi_setup_slave(bus, cs, 1000000, mode);
  95. if (!slave) {
  96. printf("Invalid device %d:%d\n", bus, cs);
  97. return 1;
  98. }
  99. spi_claim_bus(slave);
  100. if(spi_xfer(slave, bitlen, dout, din,
  101. SPI_XFER_BEGIN | SPI_XFER_END) != 0) {
  102. printf("Error during SPI transaction\n");
  103. rcode = 1;
  104. } else {
  105. for(j = 0; j < ((bitlen + 7) / 8); j++) {
  106. printf("%02X", din[j]);
  107. }
  108. printf("\n");
  109. }
  110. spi_release_bus(slave);
  111. spi_free_slave(slave);
  112. return rcode;
  113. }
  114. /***************************************************/
  115. U_BOOT_CMD(
  116. sspi, 5, 1, do_spi,
  117. "SPI utility command",
  118. "[<bus>:]<cs>[.<mode>] <bit_len> <dout> - Send and receive bits\n"
  119. "<bus> - Identifies the SPI bus\n"
  120. "<cs> - Identifies the chip select\n"
  121. "<mode> - Identifies the SPI mode to use\n"
  122. "<bit_len> - Number of bits to send (base 10)\n"
  123. "<dout> - Hexadecimal string that gets sent"
  124. );