spi_slave.c 15 KB

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