spi_slave.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <string.h>
  7. #include "esp_types.h"
  8. #include "esp_attr.h"
  9. #include "esp_intr_alloc.h"
  10. #include "esp_log.h"
  11. #include "esp_err.h"
  12. #include "esp_pm.h"
  13. #include "esp_heap_caps.h"
  14. #include "esp_rom_gpio.h"
  15. #include "esp_rom_sys.h"
  16. #include "soc/lldesc.h"
  17. #include "soc/soc_caps.h"
  18. #include "soc/spi_periph.h"
  19. #include "soc/soc_memory_layout.h"
  20. #include "hal/spi_ll.h"
  21. #include "hal/spi_slave_hal.h"
  22. #include "freertos/FreeRTOS.h"
  23. #include "freertos/semphr.h"
  24. #include "freertos/task.h"
  25. #include "sdkconfig.h"
  26. #include "driver/gpio.h"
  27. #include "driver/spi_common_internal.h"
  28. #include "driver/spi_slave.h"
  29. #include "hal/spi_slave_hal.h"
  30. static const char *SPI_TAG = "spi_slave";
  31. #define SPI_CHECK(a, str, ret_val) \
  32. if (!(a)) { \
  33. ESP_LOGE(SPI_TAG,"%s(%d): %s", __FUNCTION__, __LINE__, str); \
  34. return (ret_val); \
  35. }
  36. #ifdef CONFIG_SPI_SLAVE_ISR_IN_IRAM
  37. #define SPI_SLAVE_ISR_ATTR IRAM_ATTR
  38. #else
  39. #define SPI_SLAVE_ISR_ATTR
  40. #endif
  41. #ifdef CONFIG_SPI_SLAVE_IN_IRAM
  42. #define SPI_SLAVE_ATTR IRAM_ATTR
  43. #else
  44. #define SPI_SLAVE_ATTR
  45. #endif
  46. typedef struct {
  47. int id;
  48. spi_slave_interface_config_t cfg;
  49. intr_handle_t intr;
  50. spi_slave_hal_context_t hal;
  51. spi_slave_transaction_t *cur_trans;
  52. uint32_t flags;
  53. int max_transfer_sz;
  54. QueueHandle_t trans_queue;
  55. QueueHandle_t ret_queue;
  56. bool dma_enabled;
  57. uint32_t tx_dma_chan;
  58. uint32_t rx_dma_chan;
  59. #ifdef CONFIG_PM_ENABLE
  60. esp_pm_lock_handle_t pm_lock;
  61. #endif
  62. } spi_slave_t;
  63. static spi_slave_t *spihost[SOC_SPI_PERIPH_NUM];
  64. static void IRAM_ATTR spi_intr(void *arg);
  65. static inline bool is_valid_host(spi_host_device_t host)
  66. {
  67. //SPI1 can be used as GPSPI only on ESP32
  68. #if CONFIG_IDF_TARGET_ESP32
  69. return host >= SPI1_HOST && host <= SPI3_HOST;
  70. #elif (SOC_SPI_PERIPH_NUM == 2)
  71. return host == SPI2_HOST;
  72. #elif (SOC_SPI_PERIPH_NUM == 3)
  73. return host >= SPI2_HOST && host <= SPI3_HOST;
  74. #endif
  75. }
  76. static inline bool bus_is_iomux(spi_slave_t *host)
  77. {
  78. return host->flags&SPICOMMON_BUSFLAG_IOMUX_PINS;
  79. }
  80. static void freeze_cs(spi_slave_t *host)
  81. {
  82. esp_rom_gpio_connect_in_signal(GPIO_MATRIX_CONST_ONE_INPUT, spi_periph_signal[host->id].spics_in, false);
  83. }
  84. // Use this function instead of cs_initial to avoid overwrite the output config
  85. // This is used in test by internal gpio matrix connections
  86. static inline void restore_cs(spi_slave_t *host)
  87. {
  88. if (bus_is_iomux(host)) {
  89. gpio_iomux_in(host->cfg.spics_io_num, spi_periph_signal[host->id].spics_in);
  90. } else {
  91. esp_rom_gpio_connect_in_signal(host->cfg.spics_io_num, spi_periph_signal[host->id].spics_in, false);
  92. }
  93. }
  94. esp_err_t spi_slave_initialize(spi_host_device_t host, const spi_bus_config_t *bus_config, const spi_slave_interface_config_t *slave_config, spi_dma_chan_t dma_chan)
  95. {
  96. bool spi_chan_claimed;
  97. uint32_t actual_tx_dma_chan = 0;
  98. uint32_t actual_rx_dma_chan = 0;
  99. esp_err_t ret = ESP_OK;
  100. esp_err_t err;
  101. SPI_CHECK(is_valid_host(host), "invalid host", ESP_ERR_INVALID_ARG);
  102. #ifdef CONFIG_IDF_TARGET_ESP32
  103. SPI_CHECK(dma_chan >= SPI_DMA_DISABLED && dma_chan <= SPI_DMA_CH_AUTO, "invalid dma channel", ESP_ERR_INVALID_ARG );
  104. #elif CONFIG_IDF_TARGET_ESP32S2
  105. SPI_CHECK( dma_chan == SPI_DMA_DISABLED || dma_chan == (int)host || dma_chan == SPI_DMA_CH_AUTO, "invalid dma channel", ESP_ERR_INVALID_ARG );
  106. #elif SOC_GDMA_SUPPORTED
  107. SPI_CHECK( dma_chan == SPI_DMA_DISABLED || dma_chan == SPI_DMA_CH_AUTO, "invalid dma channel, chip only support spi dma channel auto-alloc", ESP_ERR_INVALID_ARG );
  108. #endif
  109. SPI_CHECK((bus_config->intr_flags & (ESP_INTR_FLAG_HIGH|ESP_INTR_FLAG_EDGE|ESP_INTR_FLAG_INTRDISABLED))==0, "intr flag not allowed", ESP_ERR_INVALID_ARG);
  110. #ifndef CONFIG_SPI_SLAVE_ISR_IN_IRAM
  111. SPI_CHECK((bus_config->intr_flags & ESP_INTR_FLAG_IRAM)==0, "ESP_INTR_FLAG_IRAM should be disabled when CONFIG_SPI_SLAVE_ISR_IN_IRAM is not set.", ESP_ERR_INVALID_ARG);
  112. #endif
  113. SPI_CHECK(slave_config->spics_io_num < 0 || GPIO_IS_VALID_GPIO(slave_config->spics_io_num), "spics pin invalid", ESP_ERR_INVALID_ARG);
  114. spi_chan_claimed=spicommon_periph_claim(host, "spi slave");
  115. SPI_CHECK(spi_chan_claimed, "host already in use", ESP_ERR_INVALID_STATE);
  116. spihost[host] = malloc(sizeof(spi_slave_t));
  117. if (spihost[host] == NULL) {
  118. ret = ESP_ERR_NO_MEM;
  119. goto cleanup;
  120. }
  121. memset(spihost[host], 0, sizeof(spi_slave_t));
  122. memcpy(&spihost[host]->cfg, slave_config, sizeof(spi_slave_interface_config_t));
  123. spihost[host]->id = host;
  124. bool use_dma = (dma_chan != SPI_DMA_DISABLED);
  125. spihost[host]->dma_enabled = use_dma;
  126. if (use_dma) {
  127. ret = spicommon_slave_dma_chan_alloc(host, dma_chan, &actual_tx_dma_chan, &actual_rx_dma_chan);
  128. if (ret != ESP_OK) {
  129. goto cleanup;
  130. }
  131. }
  132. err = spicommon_bus_initialize_io(host, bus_config, SPICOMMON_BUSFLAG_SLAVE|bus_config->flags, &spihost[host]->flags);
  133. if (err!=ESP_OK) {
  134. ret = err;
  135. goto cleanup;
  136. }
  137. if (slave_config->spics_io_num >= 0) {
  138. spicommon_cs_initialize(host, slave_config->spics_io_num, 0, !bus_is_iomux(spihost[host]));
  139. }
  140. // The slave DMA suffers from unexpected transactions. Forbid reading if DMA is enabled by disabling the CS line.
  141. if (use_dma) freeze_cs(spihost[host]);
  142. int dma_desc_ct = 0;
  143. spihost[host]->tx_dma_chan = actual_tx_dma_chan;
  144. spihost[host]->rx_dma_chan = actual_rx_dma_chan;
  145. if (use_dma) {
  146. //See how many dma descriptors we need and allocate them
  147. dma_desc_ct = (bus_config->max_transfer_sz + SPI_MAX_DMA_LEN - 1) / SPI_MAX_DMA_LEN;
  148. if (dma_desc_ct == 0) dma_desc_ct = 1; //default to 4k when max is not given
  149. spihost[host]->max_transfer_sz = dma_desc_ct * SPI_MAX_DMA_LEN;
  150. } else {
  151. //We're limited to non-DMA transfers: the SPI work registers can hold 64 bytes at most.
  152. spihost[host]->max_transfer_sz = SOC_SPI_MAXIMUM_BUFFER_SIZE;
  153. }
  154. #ifdef CONFIG_PM_ENABLE
  155. err = esp_pm_lock_create(ESP_PM_APB_FREQ_MAX, 0, "spi_slave",
  156. &spihost[host]->pm_lock);
  157. if (err != ESP_OK) {
  158. ret = err;
  159. goto cleanup;
  160. }
  161. // Lock APB frequency while SPI slave driver is in use
  162. esp_pm_lock_acquire(spihost[host]->pm_lock);
  163. #endif //CONFIG_PM_ENABLE
  164. //Create queues
  165. spihost[host]->trans_queue = xQueueCreate(slave_config->queue_size, sizeof(spi_slave_transaction_t *));
  166. spihost[host]->ret_queue = xQueueCreate(slave_config->queue_size, sizeof(spi_slave_transaction_t *));
  167. if (!spihost[host]->trans_queue || !spihost[host]->ret_queue) {
  168. ret = ESP_ERR_NO_MEM;
  169. goto cleanup;
  170. }
  171. int flags = bus_config->intr_flags | ESP_INTR_FLAG_INTRDISABLED;
  172. err = esp_intr_alloc(spicommon_irqsource_for_host(host), flags, spi_intr, (void *)spihost[host], &spihost[host]->intr);
  173. if (err != ESP_OK) {
  174. ret = err;
  175. goto cleanup;
  176. }
  177. spi_slave_hal_context_t *hal = &spihost[host]->hal;
  178. //assign the SPI, RX DMA and TX DMA peripheral registers beginning address
  179. spi_slave_hal_config_t hal_config = {
  180. .host_id = host,
  181. .dma_in = SPI_LL_GET_HW(host),
  182. .dma_out = SPI_LL_GET_HW(host)
  183. };
  184. spi_slave_hal_init(hal, &hal_config);
  185. if (dma_desc_ct) {
  186. hal->dmadesc_tx = heap_caps_malloc(sizeof(lldesc_t) * dma_desc_ct, MALLOC_CAP_DMA);
  187. hal->dmadesc_rx = heap_caps_malloc(sizeof(lldesc_t) * dma_desc_ct, MALLOC_CAP_DMA);
  188. if (!hal->dmadesc_tx || !hal->dmadesc_rx) {
  189. ret = ESP_ERR_NO_MEM;
  190. goto cleanup;
  191. }
  192. }
  193. hal->dmadesc_n = dma_desc_ct;
  194. hal->rx_lsbfirst = (slave_config->flags & SPI_SLAVE_RXBIT_LSBFIRST) ? 1 : 0;
  195. hal->tx_lsbfirst = (slave_config->flags & SPI_SLAVE_TXBIT_LSBFIRST) ? 1 : 0;
  196. hal->mode = slave_config->mode;
  197. hal->use_dma = use_dma;
  198. hal->tx_dma_chan = actual_tx_dma_chan;
  199. hal->rx_dma_chan = actual_rx_dma_chan;
  200. spi_slave_hal_setup_device(hal);
  201. return ESP_OK;
  202. cleanup:
  203. if (spihost[host]) {
  204. if (spihost[host]->trans_queue) vQueueDelete(spihost[host]->trans_queue);
  205. if (spihost[host]->ret_queue) vQueueDelete(spihost[host]->ret_queue);
  206. free(spihost[host]->hal.dmadesc_tx);
  207. free(spihost[host]->hal.dmadesc_rx);
  208. #ifdef CONFIG_PM_ENABLE
  209. if (spihost[host]->pm_lock) {
  210. esp_pm_lock_release(spihost[host]->pm_lock);
  211. esp_pm_lock_delete(spihost[host]->pm_lock);
  212. }
  213. #endif
  214. }
  215. spi_slave_hal_deinit(&spihost[host]->hal);
  216. if (spihost[host]->dma_enabled) {
  217. spicommon_slave_free_dma(host);
  218. }
  219. free(spihost[host]);
  220. spihost[host] = NULL;
  221. spicommon_periph_free(host);
  222. return ret;
  223. }
  224. esp_err_t spi_slave_free(spi_host_device_t host)
  225. {
  226. SPI_CHECK(is_valid_host(host), "invalid host", ESP_ERR_INVALID_ARG);
  227. SPI_CHECK(spihost[host], "host not slave", ESP_ERR_INVALID_ARG);
  228. if (spihost[host]->trans_queue) vQueueDelete(spihost[host]->trans_queue);
  229. if (spihost[host]->ret_queue) vQueueDelete(spihost[host]->ret_queue);
  230. if (spihost[host]->dma_enabled) {
  231. spicommon_slave_free_dma(host);
  232. }
  233. free(spihost[host]->hal.dmadesc_tx);
  234. free(spihost[host]->hal.dmadesc_rx);
  235. esp_intr_free(spihost[host]->intr);
  236. #ifdef CONFIG_PM_ENABLE
  237. esp_pm_lock_release(spihost[host]->pm_lock);
  238. esp_pm_lock_delete(spihost[host]->pm_lock);
  239. #endif //CONFIG_PM_ENABLE
  240. free(spihost[host]);
  241. spihost[host] = NULL;
  242. spicommon_periph_free(host);
  243. return ESP_OK;
  244. }
  245. esp_err_t SPI_SLAVE_ATTR spi_slave_queue_trans(spi_host_device_t host, const spi_slave_transaction_t *trans_desc, TickType_t ticks_to_wait)
  246. {
  247. BaseType_t r;
  248. SPI_CHECK(is_valid_host(host), "invalid host", ESP_ERR_INVALID_ARG);
  249. SPI_CHECK(spihost[host], "host not slave", ESP_ERR_INVALID_ARG);
  250. SPI_CHECK(spihost[host]->dma_enabled == 0 || trans_desc->tx_buffer==NULL || esp_ptr_dma_capable(trans_desc->tx_buffer),
  251. "txdata not in DMA-capable memory", ESP_ERR_INVALID_ARG);
  252. SPI_CHECK(spihost[host]->dma_enabled == 0 || trans_desc->rx_buffer==NULL ||
  253. (esp_ptr_dma_capable(trans_desc->rx_buffer) && esp_ptr_word_aligned(trans_desc->rx_buffer) &&
  254. (trans_desc->length%4==0)),
  255. "rxdata not in DMA-capable memory or not WORD aligned", ESP_ERR_INVALID_ARG);
  256. SPI_CHECK(trans_desc->length <= spihost[host]->max_transfer_sz * 8, "data transfer > host maximum", ESP_ERR_INVALID_ARG);
  257. r = xQueueSend(spihost[host]->trans_queue, (void *)&trans_desc, ticks_to_wait);
  258. if (!r) return ESP_ERR_TIMEOUT;
  259. esp_intr_enable(spihost[host]->intr);
  260. return ESP_OK;
  261. }
  262. esp_err_t SPI_SLAVE_ATTR spi_slave_get_trans_result(spi_host_device_t host, spi_slave_transaction_t **trans_desc, TickType_t ticks_to_wait)
  263. {
  264. BaseType_t r;
  265. SPI_CHECK(is_valid_host(host), "invalid host", ESP_ERR_INVALID_ARG);
  266. SPI_CHECK(spihost[host], "host not slave", ESP_ERR_INVALID_ARG);
  267. r = xQueueReceive(spihost[host]->ret_queue, (void *)trans_desc, ticks_to_wait);
  268. if (!r) return ESP_ERR_TIMEOUT;
  269. return ESP_OK;
  270. }
  271. esp_err_t SPI_SLAVE_ATTR spi_slave_transmit(spi_host_device_t host, spi_slave_transaction_t *trans_desc, TickType_t ticks_to_wait)
  272. {
  273. esp_err_t ret;
  274. spi_slave_transaction_t *ret_trans;
  275. //ToDo: check if any spi transfers in flight
  276. ret = spi_slave_queue_trans(host, trans_desc, ticks_to_wait);
  277. if (ret != ESP_OK) return ret;
  278. ret = spi_slave_get_trans_result(host, &ret_trans, ticks_to_wait);
  279. if (ret != ESP_OK) return ret;
  280. assert(ret_trans == trans_desc);
  281. return ESP_OK;
  282. }
  283. static void SPI_SLAVE_ISR_ATTR spi_slave_restart_after_dmareset(void *arg)
  284. {
  285. spi_slave_t *host = (spi_slave_t *)arg;
  286. esp_intr_enable(host->intr);
  287. }
  288. //This is run in interrupt context and apart from initialization and destruction, this is the only code
  289. //touching the host (=spihost[x]) variable. The rest of the data arrives in queues. That is why there are
  290. //no muxes in this code.
  291. static void SPI_SLAVE_ISR_ATTR spi_intr(void *arg)
  292. {
  293. BaseType_t r;
  294. BaseType_t do_yield = pdFALSE;
  295. spi_slave_transaction_t *trans = NULL;
  296. spi_slave_t *host = (spi_slave_t *)arg;
  297. spi_slave_hal_context_t *hal = &host->hal;
  298. assert(spi_slave_hal_usr_is_done(hal));
  299. bool use_dma = host->dma_enabled;
  300. if (host->cur_trans) {
  301. // When DMA is enabled, the slave rx dma suffers from unexpected transactions. Forbid reading until transaction ready.
  302. if (use_dma) freeze_cs(host);
  303. spi_slave_hal_store_result(hal);
  304. host->cur_trans->trans_len = spi_slave_hal_get_rcv_bitlen(hal);
  305. if (spi_slave_hal_dma_need_reset(hal)) {
  306. //On ESP32 and ESP32S2, actual_tx_dma_chan and actual_rx_dma_chan are always same
  307. spicommon_dmaworkaround_req_reset(host->tx_dma_chan, spi_slave_restart_after_dmareset, host);
  308. }
  309. if (host->cfg.post_trans_cb) host->cfg.post_trans_cb(host->cur_trans);
  310. //Okay, transaction is done.
  311. //Return transaction descriptor.
  312. xQueueSendFromISR(host->ret_queue, &host->cur_trans, &do_yield);
  313. host->cur_trans = NULL;
  314. }
  315. if (use_dma) {
  316. //On ESP32 and ESP32S2, actual_tx_dma_chan and actual_rx_dma_chan are always same
  317. spicommon_dmaworkaround_idle(host->tx_dma_chan);
  318. if (spicommon_dmaworkaround_reset_in_progress()) {
  319. //We need to wait for the reset to complete. Disable int (will be re-enabled on reset callback) and exit isr.
  320. esp_intr_disable(host->intr);
  321. if (do_yield) portYIELD_FROM_ISR();
  322. return;
  323. }
  324. }
  325. //Disable interrupt before checking to avoid concurrency issue.
  326. esp_intr_disable(host->intr);
  327. //Grab next transaction
  328. r = xQueueReceiveFromISR(host->trans_queue, &trans, &do_yield);
  329. if (r) {
  330. //enable the interrupt again if there is packet to send
  331. esp_intr_enable(host->intr);
  332. //We have a transaction. Send it.
  333. host->cur_trans = trans;
  334. hal->bitlen = trans->length;
  335. hal->rx_buffer = trans->rx_buffer;
  336. hal->tx_buffer = trans->tx_buffer;
  337. if (use_dma) {
  338. //On ESP32 and ESP32S2, actual_tx_dma_chan and actual_rx_dma_chan are always same
  339. spicommon_dmaworkaround_transfer_active(host->tx_dma_chan);
  340. }
  341. spi_slave_hal_prepare_data(hal);
  342. //The slave rx dma get disturbed by unexpected transaction. Only connect the CS when slave is ready.
  343. if (use_dma) {
  344. restore_cs(host);
  345. }
  346. //Kick off transfer
  347. spi_slave_hal_user_start(hal);
  348. if (host->cfg.post_setup_cb) host->cfg.post_setup_cb(trans);
  349. }
  350. if (do_yield) portYIELD_FROM_ISR();
  351. }