spi_hal.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // Copyright 2015-2019 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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /*******************************************************************************
  15. * NOTICE
  16. * The hal is not public api, don't use in application code.
  17. * See readme.md in soc/include/hal/readme.md
  18. ******************************************************************************/
  19. // The HAL layer for SPI master (common part)
  20. // SPI HAL usages:
  21. // 1. initialize the bus
  22. // 2. initialize the DMA descriptors if DMA used
  23. // 3. setup the clock speed (since this takes long time)
  24. // 4. call setup_device to update parameters for the specific device
  25. // 5. call setup_trans to update parameters for the specific transaction
  26. // 6. prepare data to send, and prepare the receiving buffer
  27. // 7. trigger user defined SPI transaction to start
  28. // 8. wait until the user transaction is done
  29. // 9. fetch the received data
  30. // Parameter to be updated only during ``setup_device`` will be highlighted in the
  31. // field comments.
  32. #pragma once
  33. #include "hal/spi_ll.h"
  34. #include <esp_err.h>
  35. #include "soc/lldesc.h"
  36. #include "soc/spi_caps.h"
  37. /**
  38. * Timing configuration structure that should be calculated by
  39. * ``spi_hal_setup_clock`` at initialization and hold. Filled into the
  40. * ``timing_conf`` member of the context of HAL before setup a device.
  41. */
  42. typedef struct {
  43. spi_ll_clock_val_t clock_reg; ///< Register value used by the LL layer
  44. int timing_dummy; ///< Extra dummy needed to compensate the timing
  45. int timing_miso_delay; ///< Extra miso delay clocks to compensate the timing
  46. } spi_hal_timing_conf_t;
  47. /**
  48. * Context that should be maintained by both the driver and the HAL.
  49. */
  50. typedef struct {
  51. /* configured by driver at initialization, don't touch */
  52. spi_dev_t *hw; ///< Beginning address of the peripheral registers.
  53. /* should be configured by driver at initialization */
  54. lldesc_t *dmadesc_tx; /**< Array of DMA descriptor used by the TX DMA.
  55. * The amount should be larger than dmadesc_n. The driver should ensure that
  56. * the data to be sent is shorter than the descriptors can hold.
  57. */
  58. lldesc_t *dmadesc_rx; /**< Array of DMA descriptor used by the RX DMA.
  59. * The amount should be larger than dmadesc_n. The driver should ensure that
  60. * the data to be sent is shorter than the descriptors can hold.
  61. */
  62. int dmadesc_n; ///< The amount of descriptors of both ``dmadesc_tx`` and ``dmadesc_rx`` that the HAL can use.
  63. /*
  64. * Device specific, all these parameters will be updated to the peripheral
  65. * only when ``spi_hal_setup_device``. They may not get updated when
  66. * ``spi_hal_setup_trans``.
  67. */
  68. int mode; ///< SPI mode, device specific
  69. int cs_setup; ///< Setup time of CS active edge before the first SPI clock, device specific
  70. int cs_hold; ///< Hold time of CS inactive edge after the last SPI clock, device specific
  71. int cs_pin_id; ///< CS pin to use, 0-2, otherwise all the CS pins are not used. Device specific
  72. spi_hal_timing_conf_t *timing_conf; /**< Pointer to an structure holding
  73. * the pre-calculated timing configuration for the device at initialization,
  74. * device specific
  75. */
  76. struct {
  77. uint32_t sio : 1; ///< Whether to use SIO mode, device specific
  78. uint32_t half_duplex : 1; ///< Whether half duplex mode is used, device specific
  79. uint32_t tx_lsbfirst : 1; ///< Whether LSB is sent first for TX data, device specific
  80. uint32_t rx_lsbfirst : 1; ///< Whether LSB is received first for RX data, device specific
  81. uint32_t dma_enabled : 1; ///< Whether the DMA is enabled, do not update after initialization
  82. uint32_t no_compensate : 1; ///< No need to add dummy to compensate the timing, device specific
  83. #ifdef SOC_SPI_SUPPORT_AS_CS
  84. uint32_t as_cs : 1; ///< Whether to toggle the CS while the clock toggles, device specific
  85. #endif
  86. uint32_t positive_cs : 1; ///< Whether the postive CS feature is abled, device specific
  87. };//boolean configurations
  88. /*
  89. * Transaction specific (data), all these parameters will be updated to the
  90. * peripheral every transaction.
  91. */
  92. uint16_t cmd; ///< Command value to be sent
  93. int cmd_bits; ///< Length (in bits) of the command phase
  94. int addr_bits; ///< Length (in bits) of the address phase
  95. int dummy_bits; ///< Base length (in bits) of the dummy phase. Note when the compensation is enabled, some extra dummy bits may be appended.
  96. int tx_bitlen; ///< TX length, in bits
  97. int rx_bitlen; ///< RX length, in bits
  98. uint64_t addr; ///< Address value to be sent
  99. uint8_t *send_buffer; ///< Data to be sent
  100. uint8_t *rcv_buffer; ///< Buffer to hold the receive data.
  101. spi_ll_io_mode_t io_mode; ///< IO mode of the master
  102. } spi_hal_context_t;
  103. /**
  104. * Init the peripheral and the context.
  105. *
  106. * @param hal Context of the HAL layer.
  107. * @param host_id Index of the SPI peripheral. 0 for SPI1, 1 for HSPI (SPI2) and 2 for VSPI (SPI3).
  108. */
  109. void spi_hal_init(spi_hal_context_t *hal, int host_id);
  110. /**
  111. * Deinit the peripheral (and the context if needed).
  112. *
  113. * @param hal Context of the HAL layer.
  114. */
  115. void spi_hal_deinit(spi_hal_context_t *hal);
  116. /**
  117. * Setup device-related configurations according to the settings in the context.
  118. *
  119. * @param hal Context of the HAL layer.
  120. */
  121. void spi_hal_setup_device(const spi_hal_context_t *hal);
  122. /**
  123. * Setup transaction related configurations according to the settings in the context.
  124. *
  125. * @param hal Context of the HAL layer.
  126. */
  127. void spi_hal_setup_trans(const spi_hal_context_t *hal);
  128. /**
  129. * Prepare the data for the current transaction.
  130. *
  131. * @param hal Context of the HAL layer.
  132. */
  133. void spi_hal_prepare_data(const spi_hal_context_t *hal);
  134. /**
  135. * Trigger start a user-defined transaction.
  136. *
  137. * @param hal Context of the HAL layer.
  138. */
  139. void spi_hal_user_start(const spi_hal_context_t *hal);
  140. /**
  141. * Check whether the transaction is done (trans_done is set).
  142. *
  143. * @param hal Context of the HAL layer.
  144. */
  145. bool spi_hal_usr_is_done(const spi_hal_context_t *hal);
  146. /**
  147. * Post transaction operations, mainly fetch data from the buffer.
  148. *
  149. * @param hal Context of the HAL layer.
  150. */
  151. void spi_hal_fetch_result(const spi_hal_context_t *hal);
  152. /*----------------------------------------------------------
  153. * Utils
  154. * ---------------------------------------------------------*/
  155. /**
  156. * Get the configuration of clock and timing. The configuration will be used when ``spi_hal_setup_device``.
  157. *
  158. * It is highly suggested to do this at initialization, since it takes long time.
  159. *
  160. * @param hal Context of the HAL layer.
  161. * @param speed_hz Desired frequency.
  162. * @param duty_cycle Desired duty cycle of SPI clock
  163. * @param use_gpio true if the GPIO matrix is used, otherwise false
  164. * @param input_delay_ns Maximum delay between SPI launch clock and the data to
  165. * be valid. This is used to compensate/calculate the maximum frequency
  166. * allowed. Left 0 if not known.
  167. * @param out_freq Output of the actual frequency, left NULL if not required.
  168. * @param timing_conf Output of the timing configuration.
  169. *
  170. * @return ESP_OK if desired is available, otherwise fail.
  171. */
  172. esp_err_t spi_hal_get_clock_conf(const spi_hal_context_t *hal, int speed_hz, int duty_cycle, bool use_gpio, int input_delay_ns, int *out_freq, spi_hal_timing_conf_t *timing_conf);
  173. /**
  174. * Get the frequency actual used.
  175. *
  176. * @param hal Context of the HAL layer.
  177. * @param fapb APB clock frequency.
  178. * @param hz Desired frequencyc.
  179. * @param duty_cycle Desired duty cycle.
  180. */
  181. int spi_hal_master_cal_clock(int fapb, int hz, int duty_cycle);
  182. /**
  183. * Get the timing configuration for given parameters.
  184. *
  185. * @param eff_clk Actual SPI clock frequency
  186. * @param gpio_is_used true if the GPIO matrix is used, otherwise false.
  187. * @param input_delay_ns Maximum delay between SPI launch clock and the data to
  188. * be valid. This is used to compensate/calculate the maximum frequency
  189. * allowed. Left 0 if not known.
  190. * @param dummy_n Dummy cycles required to correctly read the data.
  191. * @param miso_delay_n suggested delay on the MISO line, in APB clocks.
  192. */
  193. void spi_hal_cal_timing(int eff_clk, bool gpio_is_used, int input_delay_ns, int *dummy_n, int *miso_delay_n);
  194. /**
  195. * Get the maximum frequency allowed to read if no compensation is used.
  196. *
  197. * @param gpio_is_used true if the GPIO matrix is used, otherwise false.
  198. * @param input_delay_ns Maximum delay between SPI launch clock and the data to
  199. * be valid. This is used to compensate/calculate the maximum frequency
  200. * allowed. Left 0 if not known.
  201. */
  202. int spi_hal_get_freq_limit(bool gpio_is_used, int input_delay_ns);