You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

164 lines
6.2 KiB

3 years ago
  1. // Copyright 2015-2020 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #ifndef _IOT_SPI_BUS_H_
  14. #define _IOT_SPI_BUS_H_
  15. #include "driver/spi_master.h"
  16. #include "driver/gpio.h"
  17. #define NULL_SPI_CS_PIN -1 /*!< set cs_io_num to NULL_SPI_CS_PIN if spi device has no CP pin */
  18. typedef void *spi_bus_handle_t; /*!< spi bus handle */
  19. typedef void *spi_bus_device_handle_t; /*!< spi device handle */
  20. /**
  21. * spi bus initialization parameters.
  22. * */
  23. typedef struct {
  24. gpio_num_t miso_io_num; /*!< GPIO pin for Master In Slave Out (=spi_q) signal, or -1 if not used.*/
  25. gpio_num_t mosi_io_num; /*!< GPIO pin for Master Out Slave In (=spi_d) signal, or -1 if not used.*/
  26. gpio_num_t sclk_io_num; /*!< GPIO pin for Spi CLocK signal, or -1 if not used*/
  27. int max_transfer_sz; /*!< <Maximum length of bytes available to send, if < 4096, 4096 will be set*/
  28. }spi_config_t;
  29. /**
  30. * spi device initialization parameters.
  31. * */
  32. typedef struct {
  33. gpio_num_t cs_io_num; /*!< GPIO pin to select this device (CS), or -1 if not used*/
  34. uint8_t mode; /*!< modes (0,1,2,3) that correspond to the four possible clocking configurations*/
  35. int clock_speed_hz; /*!< spi clock speed, divisors of 80MHz, in Hz. See ``SPI_MASTER_FREQ_*`*/
  36. }spi_device_config_t;
  37. #ifdef __cplusplus
  38. extern "C"
  39. {
  40. #endif
  41. /**
  42. * @brief Create and initialize a spi bus and return the spi bus handle
  43. *
  44. * @param host_id SPI peripheral that controls this bus, SPI2_HOST or SPI3_HOST
  45. * @param bus_conf spi bus configurations details in spi_config_t
  46. * @return spi_bus_handle_t handle for spi bus operation, NULL if failed.
  47. */
  48. spi_bus_handle_t spi_bus_create(spi_host_device_t host_id, const spi_config_t *bus_conf);
  49. /**
  50. * @brief Deinitialize and delete the spi bus
  51. *
  52. * @param p_bus_handle pointer to spi bus handle, if delete succeed handle will set to NULL.
  53. * @return esp_err_t
  54. * - ESP_ERR_INVALID_ARG if parameter is invalid
  55. * - ESP_FAIL Fail
  56. * - ESP_OK Success
  57. */
  58. esp_err_t spi_bus_delete(spi_bus_handle_t *p_bus_handle);
  59. /**
  60. * @brief Create and add a device on the spi bus.
  61. *
  62. * @param bus_handle handle for spi bus operation.
  63. * @param device_conf spi device configurations details in spi_device_config_t
  64. * @return spi_bus_device_handle_t handle for device operation, NULL if failed.
  65. */
  66. spi_bus_device_handle_t spi_bus_device_create(spi_bus_handle_t bus_handle, const spi_device_config_t *device_conf);
  67. /**
  68. * @brief Deinitialize and remove the device from spi bus.
  69. *
  70. * @param p_dev_handle pointer to device handle, if delete succeed handle will set to NULL.
  71. * @return esp_err_t
  72. * - ESP_ERR_INVALID_ARG if parameter is invalid
  73. * - ESP_FAIL Fail
  74. * - ESP_OK Success
  75. */
  76. esp_err_t spi_bus_device_delete(spi_bus_device_handle_t *p_dev_handle);
  77. /**
  78. * @brief Transfer one byte with the device.
  79. *
  80. * @param dev_handle handle for device operation.
  81. * @param data_out data will send to device.
  82. * @param data_in pointer to receive buffer, set NULL to skip receive phase.
  83. * @return esp_err_t
  84. * - ESP_ERR_INVALID_ARG if parameter is invalid
  85. * - ESP_ERR_TIMEOUT if bus is busy
  86. * - ESP_OK on success
  87. */
  88. esp_err_t spi_bus_transfer_byte(spi_bus_device_handle_t dev_handle, uint8_t data_out, uint8_t *data_in);
  89. /**
  90. * @brief Transfer multi-bytes with the device.
  91. *
  92. * @param dev_handle handle for device operation.
  93. * @param data_out pointer to sent buffer, set NULL to skip sent phase.
  94. * @param data_in pointer to receive buffer, set NULL to skip receive phase.
  95. * @param data_len number of bytes will transfer.
  96. * @return esp_err_t
  97. * - ESP_ERR_INVALID_ARG if parameter is invalid
  98. * - ESP_ERR_TIMEOUT if bus is busy
  99. * - ESP_OK on success
  100. */
  101. esp_err_t spi_bus_transfer_bytes(spi_bus_device_handle_t dev_handle, const uint8_t *data_out, uint8_t *data_in, uint32_t data_len);
  102. /**************************************** Public Functions (Low level)*********************************************/
  103. /**
  104. * @brief Send a polling transaction, wait for it to complete, and return the result
  105. * @note
  106. * Only call this function when ``spi_bus_transfer_xx`` do not meet the requirements
  107. *
  108. * @param dev_handle handle for device operation.
  109. * @param p_trans Description of transaction to execute
  110. * @return esp_err_t
  111. * - ESP_ERR_INVALID_ARG if parameter is invalid
  112. * - ESP_ERR_TIMEOUT if bus is busy
  113. * - ESP_OK on success
  114. */
  115. esp_err_t spi_bus_transmit_begin(spi_bus_device_handle_t dev_handle, spi_transaction_t *p_trans);
  116. /**
  117. * @brief Transfer one 16-bit value with the device. using msb by default.
  118. * For example 0x1234, 0x12 will send first then 0x34.
  119. *
  120. * @param dev_handle handle for device operation.
  121. * @param data_out data will send to device.
  122. * @param data_in pointer to receive buffer, set NULL to skip receive phase.
  123. * @return esp_err_t
  124. * - ESP_ERR_INVALID_ARG if parameter is invalid
  125. * - ESP_ERR_TIMEOUT if bus is busy
  126. * - ESP_OK on success
  127. */
  128. esp_err_t spi_bus_transfer_reg16(spi_bus_device_handle_t dev_handle, uint16_t data_out, uint16_t *data_in);
  129. /**
  130. * @brief Transfer one 32-bit value with the device. using msb by default.
  131. * For example 0x12345678, 0x12 will send first, 0x78 will send in the end.
  132. *
  133. * @param dev_handle handle for device operation.
  134. * @param data_out data will send to device.
  135. * @param data_in pointer to receive buffer, set NULL to skip receive phase.
  136. * @return esp_err_t
  137. * - ESP_ERR_INVALID_ARG if parameter is invalid
  138. * - ESP_ERR_TIMEOUT if bus is busy
  139. * - ESP_OK on success
  140. */
  141. esp_err_t spi_bus_transfer_reg32(spi_bus_device_handle_t dev_handle, uint32_t data_out, uint32_t *data_in);
  142. #ifdef __cplusplus
  143. }
  144. #endif
  145. #endif