spi_common.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  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. #include <string.h>
  15. #include "sdkconfig.h"
  16. #include "driver/spi_master.h"
  17. #include "soc/spi_periph.h"
  18. #include "esp_types.h"
  19. #include "esp_attr.h"
  20. #include "esp_log.h"
  21. #include "esp_err.h"
  22. #include "soc/soc.h"
  23. #include "soc/soc_caps.h"
  24. #include "soc/lldesc.h"
  25. #include "driver/gpio.h"
  26. #include "driver/periph_ctrl.h"
  27. #include "esp_heap_caps.h"
  28. #include "driver/spi_common_internal.h"
  29. #include "stdatomic.h"
  30. #include "hal/spi_hal.h"
  31. #include "esp_rom_gpio.h"
  32. #if CONFIG_IDF_TARGET_ESP32
  33. #include "soc/dport_reg.h"
  34. #endif
  35. #if SOC_GDMA_SUPPORTED
  36. #include "esp_private/gdma.h"
  37. #endif
  38. static const char *SPI_TAG = "spi";
  39. #define SPI_CHECK(a, str, ret_val) do { \
  40. if (!(a)) { \
  41. ESP_LOGE(SPI_TAG,"%s(%d): %s", __FUNCTION__, __LINE__, str); \
  42. return (ret_val); \
  43. } \
  44. } while(0)
  45. #define SPI_CHECK_PIN(pin_num, pin_name, check_output) if (check_output) { \
  46. SPI_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(pin_num), pin_name" not valid", ESP_ERR_INVALID_ARG); \
  47. } else { \
  48. SPI_CHECK(GPIO_IS_VALID_GPIO(pin_num), pin_name" not valid", ESP_ERR_INVALID_ARG); \
  49. }
  50. #define SPI_MAIN_BUS_DEFAULT() { \
  51. .host_id = 0, \
  52. .bus_attr = { \
  53. .tx_dma_chan = 0, \
  54. .rx_dma_chan = 0, \
  55. .max_transfer_sz = SOC_SPI_MAXIMUM_BUFFER_SIZE, \
  56. .dma_desc_num= 0, \
  57. }, \
  58. }
  59. #define FUNC_GPIO PIN_FUNC_GPIO
  60. typedef struct {
  61. int host_id;
  62. spi_destroy_func_t destroy_func;
  63. void* destroy_arg;
  64. spi_bus_attr_t bus_attr;
  65. #if SOC_GDMA_SUPPORTED
  66. gdma_channel_handle_t tx_channel;
  67. gdma_channel_handle_t rx_channel;
  68. #endif
  69. } spicommon_bus_context_t;
  70. //Periph 1 is 'claimed' by SPI flash code.
  71. static atomic_bool spi_periph_claimed[SOC_SPI_PERIPH_NUM] = { ATOMIC_VAR_INIT(true), ATOMIC_VAR_INIT(false),
  72. #if (SOC_SPI_PERIPH_NUM >= 3)
  73. ATOMIC_VAR_INIT(false),
  74. #endif
  75. #if (SOC_SPI_PERIPH_NUM >= 4)
  76. ATOMIC_VAR_INIT(false),
  77. #endif
  78. };
  79. static const char* spi_claiming_func[3] = {NULL, NULL, NULL};
  80. static spicommon_bus_context_t s_mainbus = SPI_MAIN_BUS_DEFAULT();
  81. static spicommon_bus_context_t* bus_ctx[SOC_SPI_PERIPH_NUM] = {&s_mainbus};
  82. #if !SOC_GDMA_SUPPORTED
  83. //Each bit stands for 1 dma channel, BIT(0) should be used for SPI1
  84. static uint8_t spi_dma_chan_enabled = 0;
  85. static portMUX_TYPE spi_dma_spinlock = portMUX_INITIALIZER_UNLOCKED;
  86. #endif //#if !SOC_GDMA_SUPPORTED
  87. static inline bool is_valid_host(spi_host_device_t host)
  88. {
  89. #if (SOC_SPI_PERIPH_NUM == 2)
  90. return host >= SPI1_HOST && host <= SPI2_HOST;
  91. #elif (SOC_SPI_PERIPH_NUM == 3)
  92. return host >= SPI1_HOST && host <= SPI3_HOST;
  93. #endif
  94. }
  95. //----------------------------------------------------------alloc spi periph-------------------------------------------------------//
  96. //Returns true if this peripheral is successfully claimed, false if otherwise.
  97. bool spicommon_periph_claim(spi_host_device_t host, const char* source)
  98. {
  99. bool false_var = false;
  100. bool ret = atomic_compare_exchange_strong(&spi_periph_claimed[host], &false_var, true);
  101. if (ret) {
  102. spi_claiming_func[host] = source;
  103. periph_module_enable(spi_periph_signal[host].module);
  104. } else {
  105. ESP_EARLY_LOGE(SPI_TAG, "SPI%d already claimed by %s.", host+1, spi_claiming_func[host]);
  106. }
  107. return ret;
  108. }
  109. bool spicommon_periph_in_use(spi_host_device_t host)
  110. {
  111. return atomic_load(&spi_periph_claimed[host]);
  112. }
  113. //Returns true if this peripheral is successfully freed, false if otherwise.
  114. bool spicommon_periph_free(spi_host_device_t host)
  115. {
  116. bool true_var = true;
  117. bool ret = atomic_compare_exchange_strong(&spi_periph_claimed[host], &true_var, false);
  118. if (ret) periph_module_disable(spi_periph_signal[host].module);
  119. return ret;
  120. }
  121. int spicommon_irqsource_for_host(spi_host_device_t host)
  122. {
  123. return spi_periph_signal[host].irq;
  124. }
  125. int spicommon_irqdma_source_for_host(spi_host_device_t host)
  126. {
  127. return spi_periph_signal[host].irq_dma;
  128. }
  129. //----------------------------------------------------------alloc dma periph-------------------------------------------------------//
  130. #if !SOC_GDMA_SUPPORTED
  131. static inline periph_module_t get_dma_periph(int dma_chan)
  132. {
  133. assert(dma_chan >= 1 && dma_chan <= SOC_SPI_DMA_CHAN_NUM);
  134. #if CONFIG_IDF_TARGET_ESP32S2
  135. if (dma_chan == 1) {
  136. return PERIPH_SPI2_DMA_MODULE;
  137. } else if (dma_chan == 2) {
  138. return PERIPH_SPI3_DMA_MODULE;
  139. } else {
  140. abort();
  141. }
  142. #elif CONFIG_IDF_TARGET_ESP32
  143. return PERIPH_SPI_DMA_MODULE;
  144. #endif
  145. }
  146. static bool spicommon_dma_chan_claim(int dma_chan, uint32_t *out_actual_dma_chan)
  147. {
  148. bool ret = false;
  149. portENTER_CRITICAL(&spi_dma_spinlock);
  150. bool is_used = (BIT(dma_chan) & spi_dma_chan_enabled);
  151. if (!is_used) {
  152. spi_dma_chan_enabled |= BIT(dma_chan);
  153. periph_module_enable(get_dma_periph(dma_chan));
  154. *out_actual_dma_chan = dma_chan;
  155. ret = true;
  156. }
  157. portEXIT_CRITICAL(&spi_dma_spinlock);
  158. return ret;
  159. }
  160. static void spicommon_connect_spi_and_dma(spi_host_device_t host, int dma_chan)
  161. {
  162. #if CONFIG_IDF_TARGET_ESP32
  163. DPORT_SET_PERI_REG_BITS(DPORT_SPI_DMA_CHAN_SEL_REG, 3, dma_chan, (host * 2));
  164. #elif CONFIG_IDF_TARGET_ESP32S2
  165. //On ESP32S2, each SPI controller has its own DMA channel. So there is no need to connect them.
  166. #endif
  167. }
  168. static esp_err_t spicommon_dma_chan_alloc(spi_host_device_t host_id, spi_dma_chan_t dma_chan, uint32_t *out_actual_tx_dma_chan, uint32_t *out_actual_rx_dma_chan)
  169. {
  170. assert(is_valid_host(host_id));
  171. #if CONFIG_IDF_TARGET_ESP32
  172. assert(dma_chan > SPI_DMA_DISABLED && dma_chan <= SPI_DMA_CH_AUTO);
  173. #elif CONFIG_IDF_TARGET_ESP32S2
  174. assert(dma_chan == (int)host_id || dma_chan == SPI_DMA_CH_AUTO);
  175. #endif
  176. esp_err_t ret = ESP_OK;
  177. bool success = false;
  178. uint32_t actual_dma_chan = 0;
  179. if (dma_chan == SPI_DMA_CH_AUTO) {
  180. #if CONFIG_IDF_TARGET_ESP32
  181. for (int i = 1; i < SOC_SPI_DMA_CHAN_NUM+1; i++) {
  182. success = spicommon_dma_chan_claim(i, &actual_dma_chan);
  183. if (success) {
  184. break;
  185. }
  186. }
  187. #elif CONFIG_IDF_TARGET_ESP32S2
  188. //On ESP32S2, each SPI controller has its own DMA channel
  189. success = spicommon_dma_chan_claim(host_id, &actual_dma_chan);
  190. #endif //#if CONFIG_IDF_TARGET_XXX
  191. } else {
  192. success = spicommon_dma_chan_claim((int)dma_chan, &actual_dma_chan);
  193. }
  194. //On ESP32 and ESP32S2, actual_tx_dma_chan and actual_rx_dma_chan are always same
  195. *out_actual_tx_dma_chan = actual_dma_chan;
  196. *out_actual_rx_dma_chan = actual_dma_chan;
  197. if (!success) {
  198. SPI_CHECK(false, "no available dma channel", ESP_ERR_NOT_FOUND);
  199. }
  200. spicommon_connect_spi_and_dma(host_id, *out_actual_tx_dma_chan);
  201. return ret;
  202. }
  203. #else //SOC_GDMA_SUPPORTED
  204. static esp_err_t spicommon_dma_chan_alloc(spi_host_device_t host_id, spi_dma_chan_t dma_chan, uint32_t *out_actual_tx_dma_chan, uint32_t *out_actual_rx_dma_chan)
  205. {
  206. assert(is_valid_host(host_id));
  207. assert(dma_chan == SPI_DMA_CH_AUTO);
  208. esp_err_t ret = ESP_OK;
  209. spicommon_bus_context_t *ctx = bus_ctx[host_id];
  210. if (dma_chan == SPI_DMA_CH_AUTO) {
  211. gdma_channel_alloc_config_t tx_alloc_config = {
  212. .flags.reserve_sibling = 1,
  213. .direction = GDMA_CHANNEL_DIRECTION_TX,
  214. };
  215. ret = gdma_new_channel(&tx_alloc_config, &ctx->tx_channel);
  216. if (ret != ESP_OK) {
  217. return ret;
  218. }
  219. gdma_channel_alloc_config_t rx_alloc_config = {
  220. .direction = GDMA_CHANNEL_DIRECTION_RX,
  221. .sibling_chan = ctx->tx_channel,
  222. };
  223. ret = gdma_new_channel(&rx_alloc_config, &ctx->rx_channel);
  224. if (ret != ESP_OK) {
  225. return ret;
  226. }
  227. if (host_id == SPI2_HOST) {
  228. gdma_connect(ctx->rx_channel, GDMA_MAKE_TRIGGER(GDMA_TRIG_PERIPH_SPI, 2));
  229. gdma_connect(ctx->tx_channel, GDMA_MAKE_TRIGGER(GDMA_TRIG_PERIPH_SPI, 2));
  230. }
  231. #if (SOC_SPI_PERIPH_NUM >= 3)
  232. else if (host_id == SPI3_HOST) {
  233. gdma_connect(ctx->rx_channel, GDMA_MAKE_TRIGGER(GDMA_TRIG_PERIPH_SPI, 3));
  234. gdma_connect(ctx->tx_channel, GDMA_MAKE_TRIGGER(GDMA_TRIG_PERIPH_SPI, 3));
  235. }
  236. #endif
  237. gdma_get_channel_id(ctx->tx_channel, (int *)out_actual_tx_dma_chan);
  238. gdma_get_channel_id(ctx->rx_channel, (int *)out_actual_rx_dma_chan);
  239. }
  240. return ret;
  241. }
  242. #endif //#if !SOC_GDMA_SUPPORTED
  243. esp_err_t spicommon_slave_dma_chan_alloc(spi_host_device_t host_id, spi_dma_chan_t dma_chan, uint32_t *out_actual_tx_dma_chan, uint32_t *out_actual_rx_dma_chan)
  244. {
  245. assert(is_valid_host(host_id));
  246. #if CONFIG_IDF_TARGET_ESP32
  247. assert(dma_chan > SPI_DMA_DISABLED && dma_chan <= SPI_DMA_CH_AUTO);
  248. #elif CONFIG_IDF_TARGET_ESP32S2
  249. assert(dma_chan == (int)host_id || dma_chan == SPI_DMA_CH_AUTO);
  250. #endif
  251. esp_err_t ret = ESP_OK;
  252. uint32_t actual_tx_dma_chan = 0;
  253. uint32_t actual_rx_dma_chan = 0;
  254. spicommon_bus_context_t *ctx = (spicommon_bus_context_t *)calloc(1, sizeof(spicommon_bus_context_t));
  255. if (!ctx) {
  256. ret = ESP_ERR_NO_MEM;
  257. goto cleanup;
  258. }
  259. bus_ctx[host_id] = ctx;
  260. ctx->host_id = host_id;
  261. ret = spicommon_dma_chan_alloc(host_id, dma_chan, &actual_tx_dma_chan, &actual_rx_dma_chan);
  262. if (ret != ESP_OK) {
  263. goto cleanup;
  264. }
  265. ctx->bus_attr.tx_dma_chan = actual_tx_dma_chan;
  266. ctx->bus_attr.rx_dma_chan = actual_rx_dma_chan;
  267. *out_actual_tx_dma_chan = actual_tx_dma_chan;
  268. *out_actual_rx_dma_chan = actual_rx_dma_chan;
  269. return ret;
  270. cleanup:
  271. free(ctx);
  272. ctx = NULL;
  273. return ret;
  274. }
  275. //----------------------------------------------------------free dma periph-------------------------------------------------------//
  276. static esp_err_t spicommon_dma_chan_free(spi_host_device_t host_id)
  277. {
  278. assert(is_valid_host(host_id));
  279. spicommon_bus_context_t *ctx = bus_ctx[host_id];
  280. #if !SOC_GDMA_SUPPORTED
  281. //On ESP32S2, each SPI controller has its own DMA channel
  282. int dma_chan = ctx->bus_attr.tx_dma_chan;
  283. assert(spi_dma_chan_enabled & BIT(dma_chan));
  284. portENTER_CRITICAL(&spi_dma_spinlock);
  285. spi_dma_chan_enabled &= ~BIT(dma_chan);
  286. periph_module_disable(get_dma_periph(dma_chan));
  287. portEXIT_CRITICAL(&spi_dma_spinlock);
  288. #else //SOC_GDMA_SUPPORTED
  289. if (ctx->rx_channel) {
  290. gdma_disconnect(ctx->rx_channel);
  291. gdma_del_channel(ctx->rx_channel);
  292. }
  293. if (ctx->tx_channel) {
  294. gdma_disconnect(ctx->tx_channel);
  295. gdma_del_channel(ctx->tx_channel);
  296. }
  297. #endif
  298. return ESP_OK;
  299. }
  300. esp_err_t spicommon_slave_free_dma(spi_host_device_t host_id)
  301. {
  302. assert(is_valid_host(host_id));
  303. esp_err_t ret = spicommon_dma_chan_free(host_id);
  304. free(bus_ctx[host_id]);
  305. bus_ctx[host_id] = NULL;
  306. return ret;
  307. }
  308. //----------------------------------------------------------IO general-------------------------------------------------------//
  309. static bool bus_uses_iomux_pins(spi_host_device_t host, const spi_bus_config_t* bus_config)
  310. {
  311. if (bus_config->sclk_io_num>=0 &&
  312. bus_config->sclk_io_num != spi_periph_signal[host].spiclk_iomux_pin) {
  313. return false;
  314. }
  315. if (bus_config->quadwp_io_num>=0 &&
  316. bus_config->quadwp_io_num != spi_periph_signal[host].spiwp_iomux_pin) {
  317. return false;
  318. }
  319. if (bus_config->quadhd_io_num>=0 &&
  320. bus_config->quadhd_io_num != spi_periph_signal[host].spihd_iomux_pin) {
  321. return false;
  322. }
  323. if (bus_config->mosi_io_num >= 0 &&
  324. bus_config->mosi_io_num != spi_periph_signal[host].spid_iomux_pin) {
  325. return false;
  326. }
  327. if (bus_config->miso_io_num>=0 &&
  328. bus_config->miso_io_num != spi_periph_signal[host].spiq_iomux_pin) {
  329. return false;
  330. }
  331. return true;
  332. }
  333. /*
  334. Do the common stuff to hook up a SPI host to a bus defined by a bunch of GPIO pins. Feed it a host number and a
  335. bus config struct and it'll set up the GPIO matrix and enable the device. If a pin is set to non-negative value,
  336. it should be able to be initialized.
  337. */
  338. esp_err_t spicommon_bus_initialize_io(spi_host_device_t host, const spi_bus_config_t *bus_config, uint32_t flags, uint32_t* flags_o)
  339. {
  340. uint32_t temp_flag = 0;
  341. bool miso_need_output;
  342. bool mosi_need_output;
  343. bool sclk_need_output;
  344. if ((flags&SPICOMMON_BUSFLAG_MASTER) != 0) {
  345. //initial for master
  346. miso_need_output = ((flags&SPICOMMON_BUSFLAG_DUAL) != 0) ? true : false;
  347. mosi_need_output = true;
  348. sclk_need_output = true;
  349. } else {
  350. //initial for slave
  351. miso_need_output = true;
  352. mosi_need_output = ((flags&SPICOMMON_BUSFLAG_DUAL) != 0) ? true : false;
  353. sclk_need_output = false;
  354. }
  355. const bool wp_need_output = true;
  356. const bool hd_need_output = true;
  357. //check pin capabilities
  358. if (bus_config->sclk_io_num>=0) {
  359. temp_flag |= SPICOMMON_BUSFLAG_SCLK;
  360. SPI_CHECK_PIN(bus_config->sclk_io_num, "sclk", sclk_need_output);
  361. }
  362. if (bus_config->quadwp_io_num>=0) {
  363. SPI_CHECK_PIN(bus_config->quadwp_io_num, "wp", wp_need_output);
  364. }
  365. if (bus_config->quadhd_io_num>=0) {
  366. SPI_CHECK_PIN(bus_config->quadhd_io_num, "hd", hd_need_output);
  367. }
  368. //set flags for QUAD mode according to the existence of wp and hd
  369. if (bus_config->quadhd_io_num >= 0 && bus_config->quadwp_io_num >= 0) temp_flag |= SPICOMMON_BUSFLAG_WPHD;
  370. if (bus_config->mosi_io_num >= 0) {
  371. temp_flag |= SPICOMMON_BUSFLAG_MOSI;
  372. SPI_CHECK_PIN(bus_config->mosi_io_num, "mosi", mosi_need_output);
  373. }
  374. if (bus_config->miso_io_num>=0) {
  375. temp_flag |= SPICOMMON_BUSFLAG_MISO;
  376. SPI_CHECK_PIN(bus_config->miso_io_num, "miso", miso_need_output);
  377. }
  378. //set flags for DUAL mode according to output-capability of MOSI and MISO pins.
  379. if ( (bus_config->mosi_io_num < 0 || GPIO_IS_VALID_OUTPUT_GPIO(bus_config->mosi_io_num)) &&
  380. (bus_config->miso_io_num < 0 || GPIO_IS_VALID_OUTPUT_GPIO(bus_config->miso_io_num)) ) {
  381. temp_flag |= SPICOMMON_BUSFLAG_DUAL;
  382. }
  383. //check if the selected pins correspond to the iomux pins of the peripheral
  384. bool use_iomux = !(flags & SPICOMMON_BUSFLAG_GPIO_PINS) && bus_uses_iomux_pins(host, bus_config);
  385. if (use_iomux) {
  386. temp_flag |= SPICOMMON_BUSFLAG_IOMUX_PINS;
  387. } else {
  388. temp_flag |= SPICOMMON_BUSFLAG_GPIO_PINS;
  389. }
  390. uint32_t missing_flag = flags & ~temp_flag;
  391. missing_flag &= ~SPICOMMON_BUSFLAG_MASTER;//don't check this flag
  392. if (missing_flag != 0) {
  393. //check pins existence
  394. if (missing_flag & SPICOMMON_BUSFLAG_SCLK) ESP_LOGE(SPI_TAG, "sclk pin required.");
  395. if (missing_flag & SPICOMMON_BUSFLAG_MOSI) ESP_LOGE(SPI_TAG, "mosi pin required.");
  396. if (missing_flag & SPICOMMON_BUSFLAG_MISO) ESP_LOGE(SPI_TAG, "miso pin required.");
  397. if (missing_flag & SPICOMMON_BUSFLAG_DUAL) ESP_LOGE(SPI_TAG, "not both mosi and miso output capable");
  398. if (missing_flag & SPICOMMON_BUSFLAG_WPHD) ESP_LOGE(SPI_TAG, "both wp and hd required.");
  399. if (missing_flag & SPICOMMON_BUSFLAG_IOMUX_PINS) ESP_LOGE(SPI_TAG, "not using iomux pins");
  400. SPI_CHECK(missing_flag == 0, "not all required capabilities satisfied.", ESP_ERR_INVALID_ARG);
  401. }
  402. if (use_iomux) {
  403. //All SPI iomux pin selections resolve to 1, so we put that here instead of trying to figure
  404. //out which FUNC_GPIOx_xSPIxx to grab; they all are defined to 1 anyway.
  405. ESP_LOGD(SPI_TAG, "SPI%d use iomux pins.", host+1);
  406. if (bus_config->mosi_io_num >= 0) {
  407. gpio_iomux_in(bus_config->mosi_io_num, spi_periph_signal[host].spid_in);
  408. gpio_iomux_out(bus_config->mosi_io_num, spi_periph_signal[host].func, false);
  409. }
  410. if (bus_config->miso_io_num >= 0) {
  411. gpio_iomux_in(bus_config->miso_io_num, spi_periph_signal[host].spiq_in);
  412. gpio_iomux_out(bus_config->miso_io_num, spi_periph_signal[host].func, false);
  413. }
  414. if (bus_config->quadwp_io_num >= 0) {
  415. gpio_iomux_in(bus_config->quadwp_io_num, spi_periph_signal[host].spiwp_in);
  416. gpio_iomux_out(bus_config->quadwp_io_num, spi_periph_signal[host].func, false);
  417. }
  418. if (bus_config->quadhd_io_num >= 0) {
  419. gpio_iomux_in(bus_config->quadhd_io_num, spi_periph_signal[host].spihd_in);
  420. gpio_iomux_out(bus_config->quadhd_io_num, spi_periph_signal[host].func, false);
  421. }
  422. if (bus_config->sclk_io_num >= 0) {
  423. gpio_iomux_in(bus_config->sclk_io_num, spi_periph_signal[host].spiclk_in);
  424. gpio_iomux_out(bus_config->sclk_io_num, spi_periph_signal[host].func, false);
  425. }
  426. temp_flag |= SPICOMMON_BUSFLAG_IOMUX_PINS;
  427. } else {
  428. //Use GPIO matrix
  429. ESP_LOGD(SPI_TAG, "SPI%d use gpio matrix.", host+1);
  430. if (bus_config->mosi_io_num >= 0) {
  431. if (mosi_need_output || (temp_flag&SPICOMMON_BUSFLAG_DUAL)) {
  432. gpio_set_direction(bus_config->mosi_io_num, GPIO_MODE_INPUT_OUTPUT);
  433. esp_rom_gpio_connect_out_signal(bus_config->mosi_io_num, spi_periph_signal[host].spid_out, false, false);
  434. } else {
  435. gpio_set_direction(bus_config->mosi_io_num, GPIO_MODE_INPUT);
  436. }
  437. esp_rom_gpio_connect_in_signal(bus_config->mosi_io_num, spi_periph_signal[host].spid_in, false);
  438. #if CONFIG_IDF_TARGET_ESP32S2
  439. PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[bus_config->mosi_io_num]);
  440. #endif
  441. PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[bus_config->mosi_io_num], FUNC_GPIO);
  442. }
  443. if (bus_config->miso_io_num >= 0) {
  444. if (miso_need_output || (temp_flag&SPICOMMON_BUSFLAG_DUAL)) {
  445. gpio_set_direction(bus_config->miso_io_num, GPIO_MODE_INPUT_OUTPUT);
  446. esp_rom_gpio_connect_out_signal(bus_config->miso_io_num, spi_periph_signal[host].spiq_out, false, false);
  447. } else {
  448. gpio_set_direction(bus_config->miso_io_num, GPIO_MODE_INPUT);
  449. }
  450. esp_rom_gpio_connect_in_signal(bus_config->miso_io_num, spi_periph_signal[host].spiq_in, false);
  451. #if CONFIG_IDF_TARGET_ESP32S2
  452. PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[bus_config->miso_io_num]);
  453. #endif
  454. PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[bus_config->miso_io_num], FUNC_GPIO);
  455. }
  456. if (bus_config->quadwp_io_num >= 0) {
  457. gpio_set_direction(bus_config->quadwp_io_num, GPIO_MODE_INPUT_OUTPUT);
  458. esp_rom_gpio_connect_out_signal(bus_config->quadwp_io_num, spi_periph_signal[host].spiwp_out, false, false);
  459. esp_rom_gpio_connect_in_signal(bus_config->quadwp_io_num, spi_periph_signal[host].spiwp_in, false);
  460. #if CONFIG_IDF_TARGET_ESP32S2
  461. PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[bus_config->quadwp_io_num]);
  462. #endif
  463. PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[bus_config->quadwp_io_num], FUNC_GPIO);
  464. }
  465. if (bus_config->quadhd_io_num >= 0) {
  466. gpio_set_direction(bus_config->quadhd_io_num, GPIO_MODE_INPUT_OUTPUT);
  467. esp_rom_gpio_connect_out_signal(bus_config->quadhd_io_num, spi_periph_signal[host].spihd_out, false, false);
  468. esp_rom_gpio_connect_in_signal(bus_config->quadhd_io_num, spi_periph_signal[host].spihd_in, false);
  469. #if CONFIG_IDF_TARGET_ESP32S2
  470. PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[bus_config->quadhd_io_num]);
  471. #endif
  472. PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[bus_config->quadhd_io_num], FUNC_GPIO);
  473. }
  474. if (bus_config->sclk_io_num >= 0) {
  475. if (sclk_need_output) {
  476. gpio_set_direction(bus_config->sclk_io_num, GPIO_MODE_INPUT_OUTPUT);
  477. esp_rom_gpio_connect_out_signal(bus_config->sclk_io_num, spi_periph_signal[host].spiclk_out, false, false);
  478. } else {
  479. gpio_set_direction(bus_config->sclk_io_num, GPIO_MODE_INPUT);
  480. }
  481. esp_rom_gpio_connect_in_signal(bus_config->sclk_io_num, spi_periph_signal[host].spiclk_in, false);
  482. #if CONFIG_IDF_TARGET_ESP32S2
  483. PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[bus_config->sclk_io_num]);
  484. #endif
  485. PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[bus_config->sclk_io_num], FUNC_GPIO);
  486. }
  487. }
  488. if (flags_o) *flags_o = temp_flag;
  489. return ESP_OK;
  490. }
  491. esp_err_t spicommon_bus_free_io_cfg(const spi_bus_config_t *bus_cfg)
  492. {
  493. int pin_array[] = {
  494. bus_cfg->mosi_io_num,
  495. bus_cfg->miso_io_num,
  496. bus_cfg->sclk_io_num,
  497. bus_cfg->quadwp_io_num,
  498. bus_cfg->quadhd_io_num,
  499. };
  500. for (int i = 0; i < sizeof(pin_array)/sizeof(int); i ++) {
  501. const int io = pin_array[i];
  502. if (io >= 0 && GPIO_IS_VALID_GPIO(io)) gpio_reset_pin(io);
  503. }
  504. return ESP_OK;
  505. }
  506. void spicommon_cs_initialize(spi_host_device_t host, int cs_io_num, int cs_num, int force_gpio_matrix)
  507. {
  508. if (!force_gpio_matrix && cs_io_num == spi_periph_signal[host].spics0_iomux_pin && cs_num == 0) {
  509. //The cs0s for all SPI peripherals map to pin mux source 1, so we use that instead of a define.
  510. gpio_iomux_in(cs_io_num, spi_periph_signal[host].spics_in);
  511. gpio_iomux_out(cs_io_num, spi_periph_signal[host].func, false);
  512. } else {
  513. //Use GPIO matrix
  514. if (GPIO_IS_VALID_OUTPUT_GPIO(cs_io_num)) {
  515. gpio_set_direction(cs_io_num, GPIO_MODE_INPUT_OUTPUT);
  516. esp_rom_gpio_connect_out_signal(cs_io_num, spi_periph_signal[host].spics_out[cs_num], false, false);
  517. } else {
  518. gpio_set_direction(cs_io_num, GPIO_MODE_INPUT);
  519. }
  520. if (cs_num == 0) esp_rom_gpio_connect_in_signal(cs_io_num, spi_periph_signal[host].spics_in, false);
  521. PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[cs_io_num]);
  522. PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[cs_io_num], FUNC_GPIO);
  523. }
  524. }
  525. void spicommon_cs_free_io(int cs_gpio_num)
  526. {
  527. assert(cs_gpio_num>=0 && GPIO_IS_VALID_GPIO(cs_gpio_num));
  528. gpio_reset_pin(cs_gpio_num);
  529. }
  530. bool spicommon_bus_using_iomux(spi_host_device_t host)
  531. {
  532. #define CHECK_IOMUX_PIN(HOST, PIN_NAME) if (GPIO.func_in_sel_cfg[spi_periph_signal[(HOST)].PIN_NAME##_in].sig_in_sel) return false
  533. CHECK_IOMUX_PIN(host, spid);
  534. CHECK_IOMUX_PIN(host, spiq);
  535. CHECK_IOMUX_PIN(host, spiwp);
  536. CHECK_IOMUX_PIN(host, spihd);
  537. return true;
  538. }
  539. void spi_bus_main_set_lock(spi_bus_lock_handle_t lock)
  540. {
  541. bus_ctx[0]->bus_attr.lock = lock;
  542. }
  543. spi_bus_lock_handle_t spi_bus_lock_get_by_id(spi_host_device_t host_id)
  544. {
  545. return bus_ctx[host_id]->bus_attr.lock;
  546. }
  547. //----------------------------------------------------------master bus init-------------------------------------------------------//
  548. esp_err_t spi_bus_initialize(spi_host_device_t host_id, const spi_bus_config_t *bus_config, spi_dma_chan_t dma_chan)
  549. {
  550. esp_err_t err = ESP_OK;
  551. spicommon_bus_context_t *ctx = NULL;
  552. spi_bus_attr_t *bus_attr = NULL;
  553. uint32_t actual_tx_dma_chan = 0;
  554. uint32_t actual_rx_dma_chan = 0;
  555. SPI_CHECK(is_valid_host(host_id), "invalid host_id", ESP_ERR_INVALID_ARG);
  556. SPI_CHECK(bus_ctx[host_id] == NULL, "SPI bus already initialized.", ESP_ERR_INVALID_STATE);
  557. #ifdef CONFIG_IDF_TARGET_ESP32
  558. SPI_CHECK(dma_chan >= SPI_DMA_DISABLED && dma_chan <= SPI_DMA_CH_AUTO, "invalid dma channel", ESP_ERR_INVALID_ARG );
  559. #elif CONFIG_IDF_TARGET_ESP32S2
  560. SPI_CHECK( dma_chan == SPI_DMA_DISABLED || dma_chan == (int)host_id || dma_chan == SPI_DMA_CH_AUTO, "invalid dma channel", ESP_ERR_INVALID_ARG );
  561. #elif SOC_GDMA_SUPPORTED
  562. 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 );
  563. #endif
  564. 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);
  565. #ifndef CONFIG_SPI_MASTER_ISR_IN_IRAM
  566. SPI_CHECK((bus_config->intr_flags & ESP_INTR_FLAG_IRAM)==0, "ESP_INTR_FLAG_IRAM should be disabled when CONFIG_SPI_MASTER_ISR_IN_IRAM is not set.", ESP_ERR_INVALID_ARG);
  567. #endif
  568. bool spi_chan_claimed = spicommon_periph_claim(host_id, "spi master");
  569. SPI_CHECK(spi_chan_claimed, "host_id already in use", ESP_ERR_INVALID_STATE);
  570. //clean and initialize the context
  571. ctx = (spicommon_bus_context_t *)calloc(1, sizeof(spicommon_bus_context_t));
  572. if (!ctx) {
  573. err = ESP_ERR_NO_MEM;
  574. goto cleanup;
  575. }
  576. bus_ctx[host_id] = ctx;
  577. ctx->host_id = host_id;
  578. bus_attr = &ctx->bus_attr;
  579. bus_attr->bus_cfg = *bus_config;
  580. if (dma_chan != SPI_DMA_DISABLED) {
  581. bus_attr->dma_enabled = 1;
  582. err = spicommon_dma_chan_alloc(host_id, dma_chan, &actual_tx_dma_chan, &actual_rx_dma_chan);
  583. if (err != ESP_OK) {
  584. goto cleanup;
  585. }
  586. bus_attr->tx_dma_chan = actual_tx_dma_chan;
  587. bus_attr->rx_dma_chan = actual_rx_dma_chan;
  588. int dma_desc_ct = lldesc_get_required_num(bus_config->max_transfer_sz);
  589. if (dma_desc_ct == 0) dma_desc_ct = 1; //default to 4k when max is not given
  590. bus_attr->max_transfer_sz = dma_desc_ct * LLDESC_MAX_NUM_PER_DESC;
  591. bus_attr->dmadesc_tx = heap_caps_malloc(sizeof(lldesc_t) * dma_desc_ct, MALLOC_CAP_DMA);
  592. bus_attr->dmadesc_rx = heap_caps_malloc(sizeof(lldesc_t) * dma_desc_ct, MALLOC_CAP_DMA);
  593. if (bus_attr->dmadesc_tx == NULL || bus_attr->dmadesc_rx == NULL) {
  594. err = ESP_ERR_NO_MEM;
  595. goto cleanup;
  596. }
  597. bus_attr->dma_desc_num = dma_desc_ct;
  598. } else {
  599. bus_attr->dma_enabled = 0;
  600. bus_attr->max_transfer_sz = SOC_SPI_MAXIMUM_BUFFER_SIZE;
  601. bus_attr->dma_desc_num = 0;
  602. }
  603. spi_bus_lock_config_t lock_config = {
  604. .host_id = host_id,
  605. .cs_num = SOC_SPI_PERIPH_CS_NUM(host_id),
  606. };
  607. err = spi_bus_init_lock(&bus_attr->lock, &lock_config);
  608. if (err != ESP_OK) {
  609. goto cleanup;
  610. }
  611. #ifdef CONFIG_PM_ENABLE
  612. err = esp_pm_lock_create(ESP_PM_APB_FREQ_MAX, 0, "spi_master",
  613. &bus_attr->pm_lock);
  614. if (err != ESP_OK) {
  615. goto cleanup;
  616. }
  617. #endif //CONFIG_PM_ENABLE
  618. err = spicommon_bus_initialize_io(host_id, bus_config, SPICOMMON_BUSFLAG_MASTER | bus_config->flags, &bus_attr->flags);
  619. if (err != ESP_OK) {
  620. goto cleanup;
  621. }
  622. return ESP_OK;
  623. cleanup:
  624. if (bus_attr) {
  625. #ifdef CONFIG_PM_ENABLE
  626. esp_pm_lock_delete(bus_attr->pm_lock);
  627. #endif
  628. if (bus_attr->lock) {
  629. spi_bus_deinit_lock(bus_attr->lock);
  630. }
  631. free(bus_attr->dmadesc_tx);
  632. free(bus_attr->dmadesc_rx);
  633. bus_attr->dmadesc_tx = NULL;
  634. bus_attr->dmadesc_rx = NULL;
  635. if (bus_attr->dma_enabled) {
  636. spicommon_dma_chan_free(host_id);
  637. }
  638. }
  639. spicommon_periph_free(host_id);
  640. free(bus_ctx[host_id]);
  641. bus_ctx[host_id] = NULL;
  642. return err;
  643. }
  644. const spi_bus_attr_t* spi_bus_get_attr(spi_host_device_t host_id)
  645. {
  646. if (bus_ctx[host_id] == NULL) return NULL;
  647. return &bus_ctx[host_id]->bus_attr;
  648. }
  649. esp_err_t spi_bus_free(spi_host_device_t host_id)
  650. {
  651. esp_err_t err = ESP_OK;
  652. spicommon_bus_context_t* ctx = bus_ctx[host_id];
  653. spi_bus_attr_t* bus_attr = &ctx->bus_attr;
  654. if (ctx->destroy_func) {
  655. err = ctx->destroy_func(ctx->destroy_arg);
  656. }
  657. spicommon_bus_free_io_cfg(&bus_attr->bus_cfg);
  658. #ifdef CONFIG_PM_ENABLE
  659. esp_pm_lock_delete(bus_attr->pm_lock);
  660. #endif
  661. spi_bus_deinit_lock(bus_attr->lock);
  662. free(bus_attr->dmadesc_rx);
  663. free(bus_attr->dmadesc_tx);
  664. bus_attr->dmadesc_tx = NULL;
  665. bus_attr->dmadesc_rx = NULL;
  666. if (bus_attr->dma_enabled > 0) {
  667. spicommon_dma_chan_free(host_id);
  668. }
  669. spicommon_periph_free(host_id);
  670. free(ctx);
  671. bus_ctx[host_id] = NULL;
  672. return err;
  673. }
  674. esp_err_t spi_bus_register_destroy_func(spi_host_device_t host_id,
  675. spi_destroy_func_t f, void *arg)
  676. {
  677. bus_ctx[host_id]->destroy_func = f;
  678. bus_ctx[host_id]->destroy_arg = arg;
  679. return ESP_OK;
  680. }
  681. /*
  682. Code for workaround for DMA issue in ESP32 v0/v1 silicon
  683. */
  684. #if CONFIG_IDF_TARGET_ESP32
  685. static volatile int dmaworkaround_channels_busy[2] = {0, 0};
  686. static dmaworkaround_cb_t dmaworkaround_cb;
  687. static void *dmaworkaround_cb_arg;
  688. static portMUX_TYPE dmaworkaround_mux = portMUX_INITIALIZER_UNLOCKED;
  689. static int dmaworkaround_waiting_for_chan = 0;
  690. #endif
  691. bool IRAM_ATTR spicommon_dmaworkaround_req_reset(int dmachan, dmaworkaround_cb_t cb, void *arg)
  692. {
  693. #if CONFIG_IDF_TARGET_ESP32
  694. int otherchan = (dmachan == 1) ? 2 : 1;
  695. bool ret;
  696. portENTER_CRITICAL_ISR(&dmaworkaround_mux);
  697. if (dmaworkaround_channels_busy[otherchan-1]) {
  698. //Other channel is busy. Call back when it's done.
  699. dmaworkaround_cb = cb;
  700. dmaworkaround_cb_arg = arg;
  701. dmaworkaround_waiting_for_chan = otherchan;
  702. ret = false;
  703. } else {
  704. //Reset DMA
  705. periph_module_reset( PERIPH_SPI_DMA_MODULE );
  706. ret = true;
  707. }
  708. portEXIT_CRITICAL_ISR(&dmaworkaround_mux);
  709. return ret;
  710. #else
  711. //no need to reset
  712. return true;
  713. #endif
  714. }
  715. bool IRAM_ATTR spicommon_dmaworkaround_reset_in_progress(void)
  716. {
  717. #if CONFIG_IDF_TARGET_ESP32
  718. return (dmaworkaround_waiting_for_chan != 0);
  719. #else
  720. return false;
  721. #endif
  722. }
  723. void IRAM_ATTR spicommon_dmaworkaround_idle(int dmachan)
  724. {
  725. #if CONFIG_IDF_TARGET_ESP32
  726. portENTER_CRITICAL_ISR(&dmaworkaround_mux);
  727. dmaworkaround_channels_busy[dmachan-1] = 0;
  728. if (dmaworkaround_waiting_for_chan == dmachan) {
  729. //Reset DMA
  730. periph_module_reset( PERIPH_SPI_DMA_MODULE );
  731. dmaworkaround_waiting_for_chan = 0;
  732. //Call callback
  733. dmaworkaround_cb(dmaworkaround_cb_arg);
  734. }
  735. portEXIT_CRITICAL_ISR(&dmaworkaround_mux);
  736. #endif
  737. }
  738. void IRAM_ATTR spicommon_dmaworkaround_transfer_active(int dmachan)
  739. {
  740. #if CONFIG_IDF_TARGET_ESP32
  741. portENTER_CRITICAL_ISR(&dmaworkaround_mux);
  742. dmaworkaround_channels_busy[dmachan-1] = 1;
  743. portEXIT_CRITICAL_ISR(&dmaworkaround_mux);
  744. #endif
  745. }