spi_slave.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. // Copyright 2015-2018 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. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include <string.h>
  14. #include "sdkconfig.h"
  15. #include <hal/spi_ll.h>
  16. #include <hal/spi_slave_hal.h>
  17. #include <soc/lldesc.h>
  18. #include "driver/spi_common_internal.h"
  19. #include "driver/spi_slave.h"
  20. #include "soc/spi_periph.h"
  21. #include "esp_types.h"
  22. #include "esp_attr.h"
  23. #include "esp_intr_alloc.h"
  24. #include "esp_log.h"
  25. #include "esp_err.h"
  26. #include "esp_pm.h"
  27. #include "freertos/FreeRTOS.h"
  28. #include "freertos/semphr.h"
  29. #include "freertos/xtensa_api.h"
  30. #include "freertos/task.h"
  31. #include "soc/soc_memory_layout.h"
  32. #include "driver/gpio.h"
  33. #include "esp_heap_caps.h"
  34. static const char *SPI_TAG = "spi_slave";
  35. #define SPI_CHECK(a, str, ret_val) \
  36. if (!(a)) { \
  37. ESP_LOGE(SPI_TAG,"%s(%d): %s", __FUNCTION__, __LINE__, str); \
  38. return (ret_val); \
  39. }
  40. #ifdef CONFIG_SPI_SLAVE_ISR_IN_IRAM
  41. #define SPI_SLAVE_ISR_ATTR IRAM_ATTR
  42. #else
  43. #define SPI_SLAVE_ISR_ATTR
  44. #endif
  45. #ifdef CONFIG_SPI_SLAVE_IN_IRAM
  46. #define SPI_SLAVE_ATTR IRAM_ATTR
  47. #else
  48. #define SPI_SLAVE_ATTR
  49. #endif
  50. typedef struct {
  51. int id;
  52. spi_slave_interface_config_t cfg;
  53. intr_handle_t intr;
  54. spi_slave_hal_context_t hal;
  55. spi_slave_transaction_t *cur_trans;
  56. uint32_t flags;
  57. int max_transfer_sz;
  58. QueueHandle_t trans_queue;
  59. QueueHandle_t ret_queue;
  60. int dma_chan;
  61. #ifdef CONFIG_PM_ENABLE
  62. esp_pm_lock_handle_t pm_lock;
  63. #endif
  64. } spi_slave_t;
  65. static spi_slave_t *spihost[SOC_SPI_PERIPH_NUM];
  66. static void IRAM_ATTR spi_intr(void *arg);
  67. static inline bool is_valid_host(spi_host_device_t host)
  68. {
  69. #if CONFIG_IDF_TARGET_ESP32
  70. return host >= SPI1_HOST && host <= SPI3_HOST;
  71. #elif CONFIG_IDF_TARGET_ESP32S2
  72. // SPI_HOST (SPI1_HOST) is not supported by the SPI Slave driver on ESP32-S2
  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. gpio_matrix_in(GPIO_FUNC_IN_HIGH, 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. gpio_matrix_in(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, int dma_chan)
  95. {
  96. bool spi_chan_claimed, dma_chan_claimed;
  97. esp_err_t ret = ESP_OK;
  98. esp_err_t err;
  99. //We only support HSPI/VSPI, period.
  100. SPI_CHECK(is_valid_host(host), "invalid host", ESP_ERR_INVALID_ARG);
  101. #if defined(CONFIG_IDF_TARGET_ESP32)
  102. SPI_CHECK( dma_chan >= 0 && dma_chan <= 2, "invalid dma channel", ESP_ERR_INVALID_ARG );
  103. #elif defined(CONFIG_IDF_TARGET_ESP32S2)
  104. SPI_CHECK( dma_chan == 0 || dma_chan == host, "invalid dma channel", ESP_ERR_INVALID_ARG );
  105. #endif
  106. 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);
  107. #ifndef CONFIG_SPI_SLAVE_ISR_IN_IRAM
  108. 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);
  109. #endif
  110. spi_chan_claimed=spicommon_periph_claim(host, "spi slave");
  111. SPI_CHECK(spi_chan_claimed, "host already in use", ESP_ERR_INVALID_STATE);
  112. bool use_dma = dma_chan != 0;
  113. if (use_dma) {
  114. dma_chan_claimed=spicommon_dma_chan_claim(dma_chan);
  115. if ( !dma_chan_claimed ) {
  116. spicommon_periph_free( host );
  117. SPI_CHECK(dma_chan_claimed, "dma channel already in use", ESP_ERR_INVALID_STATE);
  118. }
  119. }
  120. spihost[host] = malloc(sizeof(spi_slave_t));
  121. if (spihost[host] == NULL) {
  122. ret = ESP_ERR_NO_MEM;
  123. goto cleanup;
  124. }
  125. memset(spihost[host], 0, sizeof(spi_slave_t));
  126. memcpy(&spihost[host]->cfg, slave_config, sizeof(spi_slave_interface_config_t));
  127. spihost[host]->id = host;
  128. err = spicommon_bus_initialize_io(host, bus_config, dma_chan, SPICOMMON_BUSFLAG_SLAVE|bus_config->flags, &spihost[host]->flags);
  129. if (err!=ESP_OK) {
  130. ret = err;
  131. goto cleanup;
  132. }
  133. spicommon_cs_initialize(host, slave_config->spics_io_num, 0, !bus_is_iomux(spihost[host]));
  134. // The slave DMA suffers from unexpected transactions. Forbid reading if DMA is enabled by disabling the CS line.
  135. if (use_dma) freeze_cs(spihost[host]);
  136. int dma_desc_ct = 0;
  137. spihost[host]->dma_chan = dma_chan;
  138. if (use_dma) {
  139. //See how many dma descriptors we need and allocate them
  140. dma_desc_ct = (bus_config->max_transfer_sz + SPI_MAX_DMA_LEN - 1) / SPI_MAX_DMA_LEN;
  141. if (dma_desc_ct == 0) dma_desc_ct = 1; //default to 4k when max is not given
  142. spihost[host]->max_transfer_sz = dma_desc_ct * SPI_MAX_DMA_LEN;
  143. } else {
  144. //We're limited to non-DMA transfers: the SPI work registers can hold 64 bytes at most.
  145. spihost[host]->max_transfer_sz = SOC_SPI_MAXIMUM_BUFFER_SIZE;
  146. }
  147. #ifdef CONFIG_PM_ENABLE
  148. err = esp_pm_lock_create(ESP_PM_APB_FREQ_MAX, 0, "spi_slave",
  149. &spihost[host]->pm_lock);
  150. if (err != ESP_OK) {
  151. ret = err;
  152. goto cleanup;
  153. }
  154. // Lock APB frequency while SPI slave driver is in use
  155. esp_pm_lock_acquire(spihost[host]->pm_lock);
  156. #endif //CONFIG_PM_ENABLE
  157. //Create queues
  158. spihost[host]->trans_queue = xQueueCreate(slave_config->queue_size, sizeof(spi_slave_transaction_t *));
  159. spihost[host]->ret_queue = xQueueCreate(slave_config->queue_size, sizeof(spi_slave_transaction_t *));
  160. if (!spihost[host]->trans_queue || !spihost[host]->ret_queue) {
  161. ret = ESP_ERR_NO_MEM;
  162. goto cleanup;
  163. }
  164. int flags = bus_config->intr_flags | ESP_INTR_FLAG_INTRDISABLED;
  165. err = esp_intr_alloc(spicommon_irqsource_for_host(host), flags, spi_intr, (void *)spihost[host], &spihost[host]->intr);
  166. if (err != ESP_OK) {
  167. ret = err;
  168. goto cleanup;
  169. }
  170. spi_slave_hal_context_t *hal = &spihost[host]->hal;
  171. spi_slave_hal_init(hal, host);
  172. if (dma_desc_ct) {
  173. hal->dmadesc_tx = heap_caps_malloc(sizeof(lldesc_t) * dma_desc_ct, MALLOC_CAP_DMA);
  174. hal->dmadesc_rx = heap_caps_malloc(sizeof(lldesc_t) * dma_desc_ct, MALLOC_CAP_DMA);
  175. if (!hal->dmadesc_tx || !hal->dmadesc_rx) {
  176. ret = ESP_ERR_NO_MEM;
  177. goto cleanup;
  178. }
  179. }
  180. hal->dmadesc_n = dma_desc_ct;
  181. hal->rx_lsbfirst = (slave_config->flags & SPI_SLAVE_RXBIT_LSBFIRST) ? 1 : 0;
  182. hal->tx_lsbfirst = (slave_config->flags & SPI_SLAVE_TXBIT_LSBFIRST) ? 1 : 0;
  183. hal->mode = slave_config->mode;
  184. hal->use_dma = use_dma;
  185. spi_slave_hal_setup_device(hal);
  186. return ESP_OK;
  187. cleanup:
  188. if (spihost[host]) {
  189. if (spihost[host]->trans_queue) vQueueDelete(spihost[host]->trans_queue);
  190. if (spihost[host]->ret_queue) vQueueDelete(spihost[host]->ret_queue);
  191. free(spihost[host]->hal.dmadesc_tx);
  192. free(spihost[host]->hal.dmadesc_rx);
  193. #ifdef CONFIG_PM_ENABLE
  194. if (spihost[host]->pm_lock) {
  195. esp_pm_lock_release(spihost[host]->pm_lock);
  196. esp_pm_lock_delete(spihost[host]->pm_lock);
  197. }
  198. #endif
  199. }
  200. spi_slave_hal_deinit(&spihost[host]->hal);
  201. free(spihost[host]);
  202. spihost[host] = NULL;
  203. spicommon_periph_free(host);
  204. if (dma_chan != 0) spicommon_dma_chan_free(dma_chan);
  205. return ret;
  206. }
  207. esp_err_t spi_slave_free(spi_host_device_t host)
  208. {
  209. SPI_CHECK(is_valid_host(host), "invalid host", ESP_ERR_INVALID_ARG);
  210. SPI_CHECK(spihost[host], "host not slave", ESP_ERR_INVALID_ARG);
  211. if (spihost[host]->trans_queue) vQueueDelete(spihost[host]->trans_queue);
  212. if (spihost[host]->ret_queue) vQueueDelete(spihost[host]->ret_queue);
  213. if ( spihost[host]->dma_chan > 0 ) {
  214. spicommon_dma_chan_free ( spihost[host]->dma_chan );
  215. }
  216. free(spihost[host]->hal.dmadesc_tx);
  217. free(spihost[host]->hal.dmadesc_rx);
  218. esp_intr_free(spihost[host]->intr);
  219. #ifdef CONFIG_PM_ENABLE
  220. esp_pm_lock_release(spihost[host]->pm_lock);
  221. esp_pm_lock_delete(spihost[host]->pm_lock);
  222. #endif //CONFIG_PM_ENABLE
  223. free(spihost[host]);
  224. spihost[host] = NULL;
  225. spicommon_periph_free(host);
  226. return ESP_OK;
  227. }
  228. 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)
  229. {
  230. BaseType_t r;
  231. SPI_CHECK(is_valid_host(host), "invalid host", ESP_ERR_INVALID_ARG);
  232. SPI_CHECK(spihost[host], "host not slave", ESP_ERR_INVALID_ARG);
  233. SPI_CHECK(spihost[host]->dma_chan == 0 || trans_desc->tx_buffer==NULL || esp_ptr_dma_capable(trans_desc->tx_buffer),
  234. "txdata not in DMA-capable memory", ESP_ERR_INVALID_ARG);
  235. SPI_CHECK(spihost[host]->dma_chan == 0 || trans_desc->rx_buffer==NULL ||
  236. (esp_ptr_dma_capable(trans_desc->rx_buffer) && esp_ptr_word_aligned(trans_desc->rx_buffer) &&
  237. (trans_desc->length%4==0)),
  238. "rxdata not in DMA-capable memory or not WORD aligned", ESP_ERR_INVALID_ARG);
  239. SPI_CHECK(trans_desc->length <= spihost[host]->max_transfer_sz * 8, "data transfer > host maximum", ESP_ERR_INVALID_ARG);
  240. r = xQueueSend(spihost[host]->trans_queue, (void *)&trans_desc, ticks_to_wait);
  241. if (!r) return ESP_ERR_TIMEOUT;
  242. esp_intr_enable(spihost[host]->intr);
  243. return ESP_OK;
  244. }
  245. 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)
  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. r = xQueueReceive(spihost[host]->ret_queue, (void *)trans_desc, ticks_to_wait);
  251. if (!r) return ESP_ERR_TIMEOUT;
  252. return ESP_OK;
  253. }
  254. 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)
  255. {
  256. esp_err_t ret;
  257. spi_slave_transaction_t *ret_trans;
  258. //ToDo: check if any spi transfers in flight
  259. ret = spi_slave_queue_trans(host, trans_desc, ticks_to_wait);
  260. if (ret != ESP_OK) return ret;
  261. ret = spi_slave_get_trans_result(host, &ret_trans, ticks_to_wait);
  262. if (ret != ESP_OK) return ret;
  263. assert(ret_trans == trans_desc);
  264. return ESP_OK;
  265. }
  266. #ifdef DEBUG_SLAVE
  267. static void dumpregs(spi_dev_t *hw)
  268. {
  269. ets_printf("***REG DUMP ***\n");
  270. ets_printf("mosi_dlen : %08X\n", hw->mosi_dlen.val);
  271. ets_printf("miso_dlen : %08X\n", hw->miso_dlen.val);
  272. ets_printf("slv_wrbuf_dlen : %08X\n", hw->slv_wrbuf_dlen.val);
  273. ets_printf("slv_rdbuf_dlen : %08X\n", hw->slv_rdbuf_dlen.val);
  274. ets_printf("slave : %08X\n", hw->slave.val);
  275. ets_printf("slv_rdata_bit : %x\n", hw->slv_rd_bit.slv_rdata_bit);
  276. ets_printf("dma_rx_status : %08X\n", hw->dma_rx_status);
  277. ets_printf("dma_tx_status : %08X\n", hw->dma_tx_status);
  278. }
  279. static void dumpll(lldesc_t *ll)
  280. {
  281. ets_printf("****LL DUMP****\n");
  282. ets_printf("Size %d\n", ll->size);
  283. ets_printf("Len: %d\n", ll->length);
  284. ets_printf("Owner: %s\n", ll->owner ? "dma" : "cpu");
  285. }
  286. #endif
  287. static void SPI_SLAVE_ISR_ATTR spi_slave_restart_after_dmareset(void *arg)
  288. {
  289. spi_slave_t *host = (spi_slave_t *)arg;
  290. esp_intr_enable(host->intr);
  291. }
  292. //This is run in interrupt context and apart from initialization and destruction, this is the only code
  293. //touching the host (=spihost[x]) variable. The rest of the data arrives in queues. That is why there are
  294. //no muxes in this code.
  295. static void SPI_SLAVE_ISR_ATTR spi_intr(void *arg)
  296. {
  297. BaseType_t r;
  298. BaseType_t do_yield = pdFALSE;
  299. spi_slave_transaction_t *trans = NULL;
  300. spi_slave_t *host = (spi_slave_t *)arg;
  301. spi_slave_hal_context_t *hal = &host->hal;
  302. #ifdef DEBUG_SLAVE
  303. dumpregs(host->hw);
  304. if (host->dmadesc_rx) dumpll(&host->dmadesc_rx[0]);
  305. #endif
  306. assert(spi_slave_hal_usr_is_done(hal));
  307. bool use_dma = host->dma_chan != 0;
  308. if (host->cur_trans) {
  309. // When DMA is enabled, the slave rx dma suffers from unexpected transactions. Forbid reading until transaction ready.
  310. if (use_dma) freeze_cs(host);
  311. spi_slave_hal_store_result(hal);
  312. host->cur_trans->trans_len = spi_slave_hal_get_rcv_bitlen(hal);
  313. if (spi_slave_hal_dma_need_reset(hal)) {
  314. spicommon_dmaworkaround_req_reset(host->dma_chan, spi_slave_restart_after_dmareset, host);
  315. }
  316. if (host->cfg.post_trans_cb) host->cfg.post_trans_cb(host->cur_trans);
  317. //Okay, transaction is done.
  318. //Return transaction descriptor.
  319. xQueueSendFromISR(host->ret_queue, &host->cur_trans, &do_yield);
  320. host->cur_trans = NULL;
  321. }
  322. if (use_dma) {
  323. spicommon_dmaworkaround_idle(host->dma_chan);
  324. if (spicommon_dmaworkaround_reset_in_progress()) {
  325. //We need to wait for the reset to complete. Disable int (will be re-enabled on reset callback) and exit isr.
  326. esp_intr_disable(host->intr);
  327. if (do_yield) portYIELD_FROM_ISR();
  328. return;
  329. }
  330. }
  331. //Disable interrupt before checking to avoid concurrency issue.
  332. esp_intr_disable(host->intr);
  333. //Grab next transaction
  334. r = xQueueReceiveFromISR(host->trans_queue, &trans, &do_yield);
  335. if (r) {
  336. //enable the interrupt again if there is packet to send
  337. esp_intr_enable(host->intr);
  338. //We have a transaction. Send it.
  339. host->cur_trans = trans;
  340. hal->bitlen = trans->length;
  341. hal->rx_buffer = trans->rx_buffer;
  342. hal->tx_buffer = trans->tx_buffer;
  343. if (use_dma) {
  344. spicommon_dmaworkaround_transfer_active(host->dma_chan);
  345. }
  346. spi_slave_hal_prepare_data(hal);
  347. //The slave rx dma get disturbed by unexpected transaction. Only connect the CS when slave is ready.
  348. if (use_dma) {
  349. restore_cs(host);
  350. }
  351. //Kick off transfer
  352. spi_slave_hal_user_start(hal);
  353. if (host->cfg.post_setup_cb) host->cfg.post_setup_cb(trans);
  354. }
  355. if (do_yield) portYIELD_FROM_ISR();
  356. }