cmd_spi.c 3.2 KB

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