spi_slave_hal.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 slave (common part)
  20. // SPI slave HAL usages:
  21. // 1. initialize the bus
  22. // 2. initialize the DMA descriptors if DMA used
  23. // 3. call setup_device to update parameters for the device
  24. // 4. prepare data to send, and prepare the receiving buffer
  25. // 5. trigger user defined SPI transaction to start
  26. // 6. wait until the user transaction is done
  27. // 7. store the received data and get the length
  28. // 8. check and reset the DMA (if needed) before the next transaction
  29. #pragma once
  30. #include "soc/lldesc.h"
  31. #include "soc/spi_struct.h"
  32. #include <esp_types.h>
  33. #include "soc/spi_caps.h"
  34. /**
  35. * Context that should be maintained by both the driver and the HAL.
  36. */
  37. typedef struct {
  38. /* configured by driver at initialization, don't touch */
  39. spi_dev_t *hw; ///< Beginning address of the peripheral registers.
  40. /* should be configured by driver at initialization */
  41. lldesc_t *dmadesc_rx; /**< Array of DMA descriptor used by the TX DMA.
  42. * The amount should be larger than dmadesc_n. The driver should ensure that
  43. * the data to be sent is shorter than the descriptors can hold.
  44. */
  45. lldesc_t *dmadesc_tx; /**< Array of DMA descriptor used by the RX DMA.
  46. * The amount should be larger than dmadesc_n. The driver should ensure that
  47. * the data to be sent is shorter than the descriptors can hold.
  48. */
  49. int dmadesc_n; ///< The amount of descriptors of both ``dmadesc_tx`` and ``dmadesc_rx`` that the HAL can use.
  50. /*
  51. * configurations to be filled after ``spi_slave_hal_init``. Updated to
  52. * peripheral registers when ``spi_slave_hal_setup_device`` is called.
  53. */
  54. struct {
  55. uint32_t rx_lsbfirst : 1;
  56. uint32_t tx_lsbfirst : 1;
  57. uint32_t use_dma : 1;
  58. };
  59. int mode;
  60. /*
  61. * Transaction specific (data), all these parameters will be updated to the
  62. * peripheral every transaction.
  63. */
  64. uint32_t bitlen; ///< Expected maximum length of the transaction, in bits.
  65. const void *tx_buffer; ///< Data to be sent
  66. void *rx_buffer; ///< Buffer to hold the received data.
  67. /* Other transaction result after one transaction */
  68. uint32_t rcv_bitlen; ///< Length of the last transaction, in bits.
  69. } spi_slave_hal_context_t;
  70. /**
  71. * Init the peripheral and the context.
  72. *
  73. * @param hal Context of the HAL layer.
  74. * @param host_id Index of the SPI peripheral. 0 for SPI1, 1 for HSPI (SPI2) and 2 for VSPI (SPI3).
  75. */
  76. void spi_slave_hal_init(spi_slave_hal_context_t *hal, int host_id);
  77. /**
  78. * Deinit the peripheral (and the context if needed).
  79. *
  80. * @param hal Context of the HAL layer.
  81. */
  82. void spi_slave_hal_deinit(spi_slave_hal_context_t *hal);
  83. /**
  84. * Setup device-related configurations according to the settings in the context.
  85. *
  86. * @param hal Context of the HAL layer.
  87. */
  88. void spi_slave_hal_setup_device(const spi_slave_hal_context_t *hal);
  89. /**
  90. * Prepare the data for the current transaction.
  91. *
  92. * @param hal Context of the HAL layer.
  93. */
  94. void spi_slave_hal_prepare_data(const spi_slave_hal_context_t *hal);
  95. /**
  96. * Trigger start a user-defined transaction.
  97. *
  98. * @param hal Context of the HAL layer.
  99. */
  100. void spi_slave_hal_user_start(const spi_slave_hal_context_t *hal);
  101. /**
  102. * Check whether the transaction is done (trans_done is set).
  103. *
  104. * @param hal Context of the HAL layer.
  105. */
  106. bool spi_slave_hal_usr_is_done(spi_slave_hal_context_t* hal);
  107. /**
  108. * Post transaction operations, fetch data from the buffer and recored the length.
  109. *
  110. * @param hal Context of the HAL layer.
  111. */
  112. void spi_slave_hal_store_result(spi_slave_hal_context_t *hal);
  113. /**
  114. * Get the length of last transaction, in bits. Should be called after ``spi_slave_hal_store_result``.
  115. *
  116. * Note that if last transaction is longer than configured before, the return
  117. * value will be truncated to the configured length.
  118. *
  119. * @param hal Context of the HAL layer.
  120. *
  121. * @return Length of the last transaction, in bits.
  122. */
  123. uint32_t spi_slave_hal_get_rcv_bitlen(spi_slave_hal_context_t *hal);
  124. /**
  125. * Check whether we need to reset the DMA according to the status of last transactions.
  126. *
  127. * In ESP32, sometimes we may need to reset the DMA for the slave before the
  128. * next transaction. Call this to check it.
  129. *
  130. * @param hal Context of the HAL layer.
  131. *
  132. * @return true if reset is needed, else false.
  133. */
  134. bool spi_slave_hal_dma_need_reset(const spi_slave_hal_context_t *hal);