spi_hal_iram.c 7.2 KB

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