spi.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #ifndef _SPI_H
  2. #define _SPI_H
  3. #include "FreeRTOS.h"
  4. #include "semphr.h"
  5. #ifdef __cplusplus
  6. extern "C" {
  7. #endif
  8. /* SPI mode flags */
  9. #define SPI_CPHA BIT(0) /* clock phase */
  10. #define SPI_CPOL BIT(1) /* clock polarity */
  11. #define SPI_MODE_0 (0|0) /* (original MicroWire) */
  12. #define SPI_MODE_1 (0|SPI_CPHA)
  13. #define SPI_MODE_2 (SPI_CPOL|0)
  14. #define SPI_MODE_3 (SPI_CPOL|SPI_CPHA)
  15. #define SPI_CS_HIGH BIT(2) /* CS active high */
  16. #define SPI_LSB_FIRST BIT(3) /* per-word bits-on-wire */
  17. #define SPI_3WIRE BIT(4) /* SI/SO signals shared */
  18. #define SPI_LOOP BIT(5) /* loopback mode */
  19. #define SPI_SLAVE BIT(6) /* slave mode */
  20. #define SPI_PREAMBLE BIT(7) /* Skip preamble bytes */
  21. #define SPI_TX_BYTE BIT(8) /* transmit with 1 wire byte */
  22. #define SPI_TX_DUAL BIT(9) /* transmit with 2 wires */
  23. #define SPI_TX_QUAD BIT(10) /* transmit with 4 wires */
  24. #define SPI_RX_SLOW BIT(11) /* receive with 1 wire slow */
  25. #define SPI_RX_DUAL BIT(12) /* receive with 2 wires */
  26. #define SPI_RX_QUAD BIT(13) /* receive with 4 wires */
  27. #define SPI_READY BIT(14) /* Slave pulls low to pause */
  28. #define SPI_NO_CS BIT(15) /* No chipselect */
  29. #define SPI_DEFAULT_WORDLEN 8
  30. /**
  31. * SPI message structure
  32. */
  33. struct spi_message
  34. {
  35. const void *send_buf;
  36. void *recv_buf;
  37. size_t length;
  38. struct spi_message *next;
  39. unsigned cs_take : 1;
  40. unsigned cs_release : 1;
  41. };
  42. struct qspi_message
  43. {
  44. struct spi_message message;
  45. /* instruction stage */
  46. struct
  47. {
  48. uint8_t content;
  49. uint8_t qspi_lines;
  50. } instruction;
  51. /* address and alternate_bytes stage */
  52. struct
  53. {
  54. uint32_t content;
  55. uint8_t size;
  56. uint8_t qspi_lines;
  57. } address, alternate_bytes;
  58. /* dummy_cycles stage */
  59. uint32_t dummy_cycles;
  60. /* number of lines in qspi data stage, the other configuration items are in parent */
  61. uint8_t qspi_data_lines;
  62. };
  63. /**
  64. * SPI configuration structure
  65. */
  66. struct spi_configuration
  67. {
  68. uint32_t mode;
  69. uint32_t data_width;
  70. uint32_t max_hz;
  71. uint32_t qspi_max_hz;
  72. uint32_t reserved;
  73. };
  74. /**
  75. * struct spi_slave - Representation of a SPI slave
  76. */
  77. struct spi_slave {
  78. unsigned int bus;
  79. unsigned int cs;
  80. unsigned int mode;
  81. unsigned int wordlen;
  82. int (*xfer)(struct spi_slave *slave, struct spi_message *message);
  83. int (*qspi_read)(struct spi_slave *slave, struct qspi_message *qspi_message);
  84. int (*configure)(struct spi_slave *slave, struct spi_configuration *configuration);
  85. SemaphoreHandle_t xMutex;
  86. SemaphoreHandle_t xSfudMutex;
  87. int open_count;
  88. char name[16];
  89. };
  90. int ecspi_init(void);
  91. int dwspi_init(void);
  92. void spi_init(void);
  93. int spi_add_slave(struct spi_slave *slave);
  94. struct spi_slave *spi_open(const char *spidev);
  95. void spi_close(struct spi_slave *slave);
  96. int spi_send_then_recv(struct spi_slave *slave, const void *send_buf,
  97. size_t send_length, void *recv_buf,
  98. size_t recv_length);
  99. int spi_transfer(struct spi_slave *slave, const void *send_buf,
  100. void *recv_buf, size_t length);
  101. int spi_configure(struct spi_slave *slave, struct spi_configuration *cfg);
  102. int spi_recv(struct spi_slave *slave, void *recv_buf, size_t length);
  103. int spi_send(struct spi_slave *slave, const void *send_buf, size_t length);
  104. #ifdef __cplusplus
  105. }
  106. #endif
  107. #endif