spi_hal_iram.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. // The HAL layer for SPI (common part, in iram)
  7. // make these functions in a seperate file to make sure all LL functions are in the IRAM.
  8. #include "hal/spi_hal.h"
  9. #include "hal/assert.h"
  10. #include "soc/soc_caps.h"
  11. //This GDMA related part will be introduced by GDMA dedicated APIs in the future. Here we temporarily use macros.
  12. #if SOC_GDMA_SUPPORTED
  13. #include "soc/gdma_struct.h"
  14. #include "hal/gdma_ll.h"
  15. #define spi_dma_ll_rx_reset(dev, chan) gdma_ll_rx_reset_channel(&GDMA, chan)
  16. #define spi_dma_ll_tx_reset(dev, chan) gdma_ll_tx_reset_channel(&GDMA, chan);
  17. #define spi_dma_ll_rx_start(dev, chan, addr) do {\
  18. gdma_ll_rx_set_desc_addr(&GDMA, chan, (uint32_t)addr);\
  19. gdma_ll_rx_start(&GDMA, chan);\
  20. } while (0)
  21. #define spi_dma_ll_tx_start(dev, chan, addr) do {\
  22. gdma_ll_tx_set_desc_addr(&GDMA, chan, (uint32_t)addr);\
  23. gdma_ll_tx_start(&GDMA, chan);\
  24. } while (0)
  25. #endif
  26. void spi_hal_setup_device(spi_hal_context_t *hal, const spi_hal_dev_config_t *dev)
  27. {
  28. //Configure clock settings
  29. spi_dev_t *hw = hal->hw;
  30. #if SOC_SPI_AS_CS_SUPPORTED
  31. spi_ll_master_set_cksel(hw, dev->cs_pin_id, dev->as_cs);
  32. #endif
  33. spi_ll_master_set_pos_cs(hw, dev->cs_pin_id, dev->positive_cs);
  34. spi_ll_master_set_clock_by_reg(hw, &dev->timing_conf.clock_reg);
  35. //Configure bit order
  36. spi_ll_set_rx_lsbfirst(hw, dev->rx_lsbfirst);
  37. spi_ll_set_tx_lsbfirst(hw, dev->tx_lsbfirst);
  38. spi_ll_master_set_mode(hw, dev->mode);
  39. //Configure misc stuff
  40. spi_ll_set_half_duplex(hw, dev->half_duplex);
  41. spi_ll_set_sio_mode(hw, dev->sio);
  42. //Configure CS pin and timing
  43. spi_ll_master_set_cs_setup(hw, dev->cs_setup);
  44. spi_ll_master_set_cs_hold(hw, dev->cs_hold);
  45. spi_ll_master_select_cs(hw, dev->cs_pin_id);
  46. }
  47. void spi_hal_setup_trans(spi_hal_context_t *hal, const spi_hal_dev_config_t *dev, const spi_hal_trans_config_t *trans)
  48. {
  49. spi_dev_t *hw = hal->hw;
  50. //clear int bit
  51. spi_ll_clear_int_stat(hal->hw);
  52. //We should be done with the transmission.
  53. HAL_ASSERT(spi_ll_get_running_cmd(hw) == 0);
  54. //set transaction line mode
  55. spi_ll_master_set_line_mode(hw, trans->line_mode);
  56. int extra_dummy = 0;
  57. //when no_dummy is not set and in half-duplex mode, sets the dummy bit if RX phase exist
  58. if (trans->rcv_buffer && !dev->no_compensate && dev->half_duplex) {
  59. extra_dummy = dev->timing_conf.timing_dummy;
  60. }
  61. //SPI iface needs to be configured for a delay in some cases.
  62. //configure dummy bits
  63. spi_ll_set_dummy(hw, extra_dummy + trans->dummy_bits);
  64. uint32_t miso_delay_num = 0;
  65. uint32_t miso_delay_mode = 0;
  66. if (dev->timing_conf.timing_miso_delay < 0) {
  67. //if the data comes too late, delay half a SPI clock to improve reading
  68. switch (dev->mode) {
  69. case 0:
  70. miso_delay_mode = 2;
  71. break;
  72. case 1:
  73. miso_delay_mode = 1;
  74. break;
  75. case 2:
  76. miso_delay_mode = 1;
  77. break;
  78. case 3:
  79. miso_delay_mode = 2;
  80. break;
  81. }
  82. miso_delay_num = 0;
  83. } else {
  84. //if the data is so fast that dummy_bit is used, delay some apb clocks to meet the timing
  85. miso_delay_num = extra_dummy ? dev->timing_conf.timing_miso_delay : 0;
  86. miso_delay_mode = 0;
  87. }
  88. spi_ll_set_miso_delay(hw, miso_delay_mode, miso_delay_num);
  89. spi_ll_set_mosi_bitlen(hw, trans->tx_bitlen);
  90. if (dev->half_duplex) {
  91. spi_ll_set_miso_bitlen(hw, trans->rx_bitlen);
  92. } else {
  93. //rxlength is not used in full-duplex mode
  94. spi_ll_set_miso_bitlen(hw, trans->tx_bitlen);
  95. }
  96. //Configure bit sizes, load addr and command
  97. int cmdlen = trans->cmd_bits;
  98. int addrlen = trans->addr_bits;
  99. if (!dev->half_duplex && dev->cs_setup != 0) {
  100. /* The command and address phase is not compatible with cs_ena_pretrans
  101. * in full duplex mode.
  102. */
  103. cmdlen = 0;
  104. addrlen = 0;
  105. }
  106. spi_ll_set_addr_bitlen(hw, addrlen);
  107. spi_ll_set_command_bitlen(hw, cmdlen);
  108. spi_ll_set_command(hw, trans->cmd, cmdlen, dev->tx_lsbfirst);
  109. spi_ll_set_address(hw, trans->addr, addrlen, dev->tx_lsbfirst);
  110. //Configure keep active CS
  111. spi_ll_master_keep_cs(hw, trans->cs_keep_active);
  112. //Save the transaction attributes for internal usage.
  113. memcpy(&hal->trans_config, trans, sizeof(spi_hal_trans_config_t));
  114. }
  115. void spi_hal_prepare_data(spi_hal_context_t *hal, const spi_hal_dev_config_t *dev, const spi_hal_trans_config_t *trans)
  116. {
  117. spi_dev_t *hw = hal->hw;
  118. //Fill DMA descriptors
  119. if (trans->rcv_buffer) {
  120. if (!hal->dma_enabled) {
  121. //No need to setup anything; we'll copy the result out of the work registers directly later.
  122. } else {
  123. lldesc_setup_link(hal->dmadesc_rx, trans->rcv_buffer, ((trans->rx_bitlen + 7) / 8), true);
  124. spi_dma_ll_rx_reset(hal->dma_in, hal->rx_dma_chan);
  125. spi_ll_dma_rx_fifo_reset(hal->hw);
  126. spi_ll_infifo_full_clr(hal->hw);
  127. spi_ll_dma_rx_enable(hal->hw, 1);
  128. spi_dma_ll_rx_start(hal->dma_in, hal->rx_dma_chan, hal->dmadesc_rx);
  129. }
  130. }
  131. #if CONFIG_IDF_TARGET_ESP32
  132. else {
  133. //DMA temporary workaround: let RX DMA work somehow to avoid the issue in ESP32 v0/v1 silicon
  134. if (hal->dma_enabled && !dev->half_duplex) {
  135. spi_ll_dma_rx_enable(hal->hw, 1);
  136. spi_dma_ll_rx_start(hal->dma_in, hal->rx_dma_chan, 0);
  137. }
  138. }
  139. #endif
  140. if (trans->send_buffer) {
  141. if (!hal->dma_enabled) {
  142. //Need to copy data to registers manually
  143. spi_ll_write_buffer(hw, trans->send_buffer, trans->tx_bitlen);
  144. } else {
  145. lldesc_setup_link(hal->dmadesc_tx, trans->send_buffer, (trans->tx_bitlen + 7) / 8, false);
  146. spi_dma_ll_tx_reset(hal->dma_out, hal->tx_dma_chan);
  147. spi_ll_dma_tx_fifo_reset(hal->hw);
  148. spi_ll_outfifo_empty_clr(hal->hw);
  149. spi_ll_dma_tx_enable(hal->hw, 1);
  150. spi_dma_ll_tx_start(hal->dma_out, hal->tx_dma_chan, hal->dmadesc_tx);
  151. }
  152. }
  153. //in ESP32 these registers should be configured after the DMA is set
  154. if ((!dev->half_duplex && trans->rcv_buffer) || trans->send_buffer) {
  155. spi_ll_enable_mosi(hw, 1);
  156. } else {
  157. spi_ll_enable_mosi(hw, 0);
  158. }
  159. spi_ll_enable_miso(hw, (trans->rcv_buffer) ? 1 : 0);
  160. }
  161. void spi_hal_user_start(const spi_hal_context_t *hal)
  162. {
  163. spi_ll_master_user_start(hal->hw);
  164. }
  165. bool spi_hal_usr_is_done(const spi_hal_context_t *hal)
  166. {
  167. return spi_ll_usr_is_done(hal->hw);
  168. }
  169. void spi_hal_fetch_result(const spi_hal_context_t *hal)
  170. {
  171. const spi_hal_trans_config_t *trans = &hal->trans_config;
  172. if (trans->rcv_buffer && !hal->dma_enabled) {
  173. //Need to copy from SPI regs to result buffer.
  174. spi_ll_read_buffer(hal->hw, trans->rcv_buffer, trans->rx_bitlen);
  175. }
  176. }