spi_common.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  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. //This GDMA related part will be introduced by GDMA dedicated APIs in the future. Here we temporarily use macros.
  36. #if SOC_GDMA_SUPPORTED
  37. #include "hal/gdma_ll.h"
  38. #include "soc/spi_caps.h"
  39. #define spi_dma_set_rx_channel_priority(gdma_chan, priority) gdma_ll_rx_set_priority(&GDMA, gdma_chan, priority);
  40. #define spi_dma_set_tx_channel_priority(gdma_chan, priority) gdma_ll_tx_set_priority(&GDMA, gdma_chan, priority);
  41. #define spi_dma_connect_rx_channel_to_periph(gdma_chan, periph_id) gdma_ll_rx_connect_to_periph(&GDMA, gdma_chan, periph_id);
  42. #define spi_dma_connect_tx_channel_to_periph(gdma_chan, periph_id) gdma_ll_tx_connect_to_periph(&GDMA, gdma_chan, periph_id);
  43. #endif
  44. static const char *SPI_TAG = "spi";
  45. #define SPI_CHECK(a, str, ret_val) do { \
  46. if (!(a)) { \
  47. ESP_LOGE(SPI_TAG,"%s(%d): %s", __FUNCTION__, __LINE__, str); \
  48. return (ret_val); \
  49. } \
  50. } while(0)
  51. #define SPI_CHECK_PIN(pin_num, pin_name, check_output) if (check_output) { \
  52. SPI_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(pin_num), pin_name" not valid", ESP_ERR_INVALID_ARG); \
  53. } else { \
  54. SPI_CHECK(GPIO_IS_VALID_GPIO(pin_num), pin_name" not valid", ESP_ERR_INVALID_ARG); \
  55. }
  56. typedef struct spi_device_t spi_device_t;
  57. #define FUNC_GPIO PIN_FUNC_GPIO
  58. #define DMA_CHANNEL_ENABLED(dma_chan) (BIT(dma_chan-1))
  59. typedef struct {
  60. int host_id;
  61. spi_destroy_func_t destroy_func;
  62. void* destroy_arg;
  63. spi_bus_attr_t bus_attr;
  64. } spicommon_bus_context_t;
  65. #define MAIN_BUS_DEFAULT() { \
  66. .host_id = 0, \
  67. .bus_attr = { \
  68. .dma_chan = 0, \
  69. .max_transfer_sz = SOC_SPI_MAXIMUM_BUFFER_SIZE, \
  70. .dma_desc_num= 0, \
  71. }, \
  72. }
  73. //Periph 1 is 'claimed' by SPI flash code.
  74. static atomic_bool spi_periph_claimed[SOC_SPI_PERIPH_NUM] = { ATOMIC_VAR_INIT(true), ATOMIC_VAR_INIT(false), ATOMIC_VAR_INIT(false),
  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 uint8_t spi_dma_chan_enabled = 0;
  81. static portMUX_TYPE spi_dma_spinlock = portMUX_INITIALIZER_UNLOCKED;
  82. static spicommon_bus_context_t s_mainbus = MAIN_BUS_DEFAULT();
  83. static spicommon_bus_context_t* bus_ctx[SOC_SPI_PERIPH_NUM] = {&s_mainbus};
  84. //Returns true if this peripheral is successfully claimed, false if otherwise.
  85. bool spicommon_periph_claim(spi_host_device_t host, const char* source)
  86. {
  87. bool false_var = false;
  88. bool ret = atomic_compare_exchange_strong(&spi_periph_claimed[host], &false_var, true);
  89. if (ret) {
  90. spi_claiming_func[host] = source;
  91. periph_module_enable(spi_periph_signal[host].module);
  92. } else {
  93. ESP_EARLY_LOGE(SPI_TAG, "SPI%d already claimed by %s.", host+1, spi_claiming_func[host]);
  94. }
  95. return ret;
  96. }
  97. bool spicommon_periph_in_use(spi_host_device_t host)
  98. {
  99. return atomic_load(&spi_periph_claimed[host]);
  100. }
  101. //Returns true if this peripheral is successfully freed, false if otherwise.
  102. bool spicommon_periph_free(spi_host_device_t host)
  103. {
  104. bool true_var = true;
  105. bool ret = atomic_compare_exchange_strong(&spi_periph_claimed[host], &true_var, false);
  106. if (ret) periph_module_disable(spi_periph_signal[host].module);
  107. return ret;
  108. }
  109. int spicommon_irqsource_for_host(spi_host_device_t host)
  110. {
  111. return spi_periph_signal[host].irq;
  112. }
  113. int spicommon_irqdma_source_for_host(spi_host_device_t host)
  114. {
  115. return spi_periph_signal[host].irq_dma;
  116. }
  117. static inline periph_module_t get_dma_periph(int dma_chan)
  118. {
  119. #if CONFIG_IDF_TARGET_ESP32S2
  120. if (dma_chan == 1) {
  121. return PERIPH_SPI2_DMA_MODULE;
  122. } else if (dma_chan==2) {
  123. return PERIPH_SPI3_DMA_MODULE;
  124. } else {
  125. abort();
  126. return -1;
  127. }
  128. #elif CONFIG_IDF_TARGET_ESP32
  129. return PERIPH_SPI_DMA_MODULE;
  130. #elif SOC_GDMA_SUPPORTED
  131. return PERIPH_GDMA_MODULE;
  132. #else
  133. return 0;
  134. #endif
  135. }
  136. bool spicommon_dma_chan_claim(int dma_chan)
  137. {
  138. bool ret = false;
  139. assert(dma_chan >= 1 && dma_chan <= SOC_SPI_DMA_CHAN_NUM);
  140. portENTER_CRITICAL(&spi_dma_spinlock);
  141. if ( !(spi_dma_chan_enabled & DMA_CHANNEL_ENABLED(dma_chan)) ) {
  142. // get the channel only when it's not claimed yet.
  143. spi_dma_chan_enabled |= DMA_CHANNEL_ENABLED(dma_chan);
  144. ret = true;
  145. }
  146. periph_module_enable(get_dma_periph(dma_chan));
  147. portEXIT_CRITICAL(&spi_dma_spinlock);
  148. return ret;
  149. }
  150. bool spicommon_dma_chan_in_use(int dma_chan)
  151. {
  152. assert(dma_chan ==1 || dma_chan == 2);
  153. return spi_dma_chan_enabled & DMA_CHANNEL_ENABLED(dma_chan);
  154. }
  155. bool spicommon_dma_chan_free(int dma_chan)
  156. {
  157. assert( dma_chan == 1 || dma_chan == 2 );
  158. assert( spi_dma_chan_enabled & DMA_CHANNEL_ENABLED(dma_chan) );
  159. portENTER_CRITICAL(&spi_dma_spinlock);
  160. spi_dma_chan_enabled &= ~DMA_CHANNEL_ENABLED(dma_chan);
  161. periph_module_disable(get_dma_periph(dma_chan));
  162. portEXIT_CRITICAL(&spi_dma_spinlock);
  163. return true;
  164. }
  165. void spicommon_connect_spi_and_dma(spi_host_device_t host, int dma_chan)
  166. {
  167. #if CONFIG_IDF_TARGET_ESP32
  168. DPORT_SET_PERI_REG_BITS(DPORT_SPI_DMA_CHAN_SEL_REG, 3, dma_chan, (host * 2));
  169. #elif CONFIG_IDF_TARGET_ESP32S2
  170. //On ESP32S2, each SPI controller has its own DMA channel. So there is no need to connect them.
  171. #elif SOC_GDMA_SUPPORTED
  172. int gdma_chan, periph_id;
  173. if (dma_chan == 1) {
  174. gdma_chan = SOC_GDMA_SPI2_DMA_CHANNEL;
  175. periph_id = GDMA_LL_TRIG_SRC_SPI2;
  176. #ifdef GDMA_LL_TRIG_SRC_SPI3
  177. } else if (dma_chan == 2) {
  178. gdma_chan = SOC_GDMA_SPI3_DMA_CHANNEL;
  179. periph_id = GDMA_LL_TRIG_SRC_SPI3;
  180. #endif
  181. } else {
  182. abort();
  183. }
  184. spi_dma_connect_rx_channel_to_periph(gdma_chan, periph_id);
  185. spi_dma_connect_tx_channel_to_periph(gdma_chan, periph_id);
  186. spi_dma_set_rx_channel_priority(gdma_chan, 1);
  187. spi_dma_set_tx_channel_priority(gdma_chan, 1);
  188. #endif
  189. }
  190. static bool bus_uses_iomux_pins(spi_host_device_t host, const spi_bus_config_t* bus_config)
  191. {
  192. if (bus_config->sclk_io_num>=0 &&
  193. bus_config->sclk_io_num != spi_periph_signal[host].spiclk_iomux_pin) {
  194. return false;
  195. }
  196. if (bus_config->quadwp_io_num>=0 &&
  197. bus_config->quadwp_io_num != spi_periph_signal[host].spiwp_iomux_pin) {
  198. return false;
  199. }
  200. if (bus_config->quadhd_io_num>=0 &&
  201. bus_config->quadhd_io_num != spi_periph_signal[host].spihd_iomux_pin) {
  202. return false;
  203. }
  204. if (bus_config->mosi_io_num >= 0 &&
  205. bus_config->mosi_io_num != spi_periph_signal[host].spid_iomux_pin) {
  206. return false;
  207. }
  208. if (bus_config->miso_io_num>=0 &&
  209. bus_config->miso_io_num != spi_periph_signal[host].spiq_iomux_pin) {
  210. return false;
  211. }
  212. return true;
  213. }
  214. /*
  215. 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
  216. bus config struct and it'll set up the GPIO matrix and enable the device. If a pin is set to non-negative value,
  217. it should be able to be initialized.
  218. */
  219. esp_err_t spicommon_bus_initialize_io(spi_host_device_t host, const spi_bus_config_t *bus_config, int dma_chan, uint32_t flags, uint32_t* flags_o)
  220. {
  221. uint32_t temp_flag = 0;
  222. bool miso_need_output;
  223. bool mosi_need_output;
  224. bool sclk_need_output;
  225. if ((flags&SPICOMMON_BUSFLAG_MASTER) != 0) {
  226. //initial for master
  227. miso_need_output = ((flags&SPICOMMON_BUSFLAG_DUAL) != 0) ? true : false;
  228. mosi_need_output = true;
  229. sclk_need_output = true;
  230. } else {
  231. //initial for slave
  232. miso_need_output = true;
  233. mosi_need_output = ((flags&SPICOMMON_BUSFLAG_DUAL) != 0) ? true : false;
  234. sclk_need_output = false;
  235. }
  236. const bool wp_need_output = true;
  237. const bool hd_need_output = true;
  238. //check pin capabilities
  239. if (bus_config->sclk_io_num>=0) {
  240. temp_flag |= SPICOMMON_BUSFLAG_SCLK;
  241. SPI_CHECK_PIN(bus_config->sclk_io_num, "sclk", sclk_need_output);
  242. }
  243. if (bus_config->quadwp_io_num>=0) {
  244. SPI_CHECK_PIN(bus_config->quadwp_io_num, "wp", wp_need_output);
  245. }
  246. if (bus_config->quadhd_io_num>=0) {
  247. SPI_CHECK_PIN(bus_config->quadhd_io_num, "hd", hd_need_output);
  248. }
  249. //set flags for QUAD mode according to the existence of wp and hd
  250. if (bus_config->quadhd_io_num >= 0 && bus_config->quadwp_io_num >= 0) temp_flag |= SPICOMMON_BUSFLAG_WPHD;
  251. if (bus_config->mosi_io_num >= 0) {
  252. temp_flag |= SPICOMMON_BUSFLAG_MOSI;
  253. SPI_CHECK_PIN(bus_config->mosi_io_num, "mosi", mosi_need_output);
  254. }
  255. if (bus_config->miso_io_num>=0) {
  256. temp_flag |= SPICOMMON_BUSFLAG_MISO;
  257. SPI_CHECK_PIN(bus_config->miso_io_num, "miso", miso_need_output);
  258. }
  259. //set flags for DUAL mode according to output-capability of MOSI and MISO pins.
  260. if ( (bus_config->mosi_io_num < 0 || GPIO_IS_VALID_OUTPUT_GPIO(bus_config->mosi_io_num)) &&
  261. (bus_config->miso_io_num < 0 || GPIO_IS_VALID_OUTPUT_GPIO(bus_config->miso_io_num)) ) {
  262. temp_flag |= SPICOMMON_BUSFLAG_DUAL;
  263. }
  264. //check if the selected pins correspond to the iomux pins of the peripheral
  265. bool use_iomux = !(flags & SPICOMMON_BUSFLAG_GPIO_PINS) && bus_uses_iomux_pins(host, bus_config);
  266. if (use_iomux) {
  267. temp_flag |= SPICOMMON_BUSFLAG_IOMUX_PINS;
  268. } else {
  269. temp_flag |= SPICOMMON_BUSFLAG_GPIO_PINS;
  270. }
  271. uint32_t missing_flag = flags & ~temp_flag;
  272. missing_flag &= ~SPICOMMON_BUSFLAG_MASTER;//don't check this flag
  273. if (missing_flag != 0) {
  274. //check pins existence
  275. if (missing_flag & SPICOMMON_BUSFLAG_SCLK) ESP_LOGE(SPI_TAG, "sclk pin required.");
  276. if (missing_flag & SPICOMMON_BUSFLAG_MOSI) ESP_LOGE(SPI_TAG, "mosi pin required.");
  277. if (missing_flag & SPICOMMON_BUSFLAG_MISO) ESP_LOGE(SPI_TAG, "miso pin required.");
  278. if (missing_flag & SPICOMMON_BUSFLAG_DUAL) ESP_LOGE(SPI_TAG, "not both mosi and miso output capable");
  279. if (missing_flag & SPICOMMON_BUSFLAG_WPHD) ESP_LOGE(SPI_TAG, "both wp and hd required.");
  280. if (missing_flag & SPICOMMON_BUSFLAG_IOMUX_PINS) ESP_LOGE(SPI_TAG, "not using iomux pins");
  281. SPI_CHECK(missing_flag == 0, "not all required capabilities satisfied.", ESP_ERR_INVALID_ARG);
  282. }
  283. if (use_iomux) {
  284. //All SPI iomux pin selections resolve to 1, so we put that here instead of trying to figure
  285. //out which FUNC_GPIOx_xSPIxx to grab; they all are defined to 1 anyway.
  286. ESP_LOGD(SPI_TAG, "SPI%d use iomux pins.", host+1);
  287. if (bus_config->mosi_io_num >= 0) {
  288. gpio_iomux_in(bus_config->mosi_io_num, spi_periph_signal[host].spid_in);
  289. gpio_iomux_out(bus_config->mosi_io_num, spi_periph_signal[host].func, false);
  290. }
  291. if (bus_config->miso_io_num >= 0) {
  292. gpio_iomux_in(bus_config->miso_io_num, spi_periph_signal[host].spiq_in);
  293. gpio_iomux_out(bus_config->miso_io_num, spi_periph_signal[host].func, false);
  294. }
  295. if (bus_config->quadwp_io_num >= 0) {
  296. gpio_iomux_in(bus_config->quadwp_io_num, spi_periph_signal[host].spiwp_in);
  297. gpio_iomux_out(bus_config->quadwp_io_num, spi_periph_signal[host].func, false);
  298. }
  299. if (bus_config->quadhd_io_num >= 0) {
  300. gpio_iomux_in(bus_config->quadhd_io_num, spi_periph_signal[host].spihd_in);
  301. gpio_iomux_out(bus_config->quadhd_io_num, spi_periph_signal[host].func, false);
  302. }
  303. if (bus_config->sclk_io_num >= 0) {
  304. gpio_iomux_in(bus_config->sclk_io_num, spi_periph_signal[host].spiclk_in);
  305. gpio_iomux_out(bus_config->sclk_io_num, spi_periph_signal[host].func, false);
  306. }
  307. temp_flag |= SPICOMMON_BUSFLAG_IOMUX_PINS;
  308. } else {
  309. //Use GPIO matrix
  310. ESP_LOGD(SPI_TAG, "SPI%d use gpio matrix.", host+1);
  311. if (bus_config->mosi_io_num >= 0) {
  312. if (mosi_need_output || (temp_flag&SPICOMMON_BUSFLAG_DUAL)) {
  313. gpio_set_direction(bus_config->mosi_io_num, GPIO_MODE_INPUT_OUTPUT);
  314. esp_rom_gpio_connect_out_signal(bus_config->mosi_io_num, spi_periph_signal[host].spid_out, false, false);
  315. } else {
  316. gpio_set_direction(bus_config->mosi_io_num, GPIO_MODE_INPUT);
  317. }
  318. esp_rom_gpio_connect_in_signal(bus_config->mosi_io_num, spi_periph_signal[host].spid_in, false);
  319. #if CONFIG_IDF_TARGET_ESP32S2
  320. PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[bus_config->mosi_io_num]);
  321. #endif
  322. PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[bus_config->mosi_io_num], FUNC_GPIO);
  323. }
  324. if (bus_config->miso_io_num >= 0) {
  325. if (miso_need_output || (temp_flag&SPICOMMON_BUSFLAG_DUAL)) {
  326. gpio_set_direction(bus_config->miso_io_num, GPIO_MODE_INPUT_OUTPUT);
  327. esp_rom_gpio_connect_out_signal(bus_config->miso_io_num, spi_periph_signal[host].spiq_out, false, false);
  328. } else {
  329. gpio_set_direction(bus_config->miso_io_num, GPIO_MODE_INPUT);
  330. }
  331. esp_rom_gpio_connect_in_signal(bus_config->miso_io_num, spi_periph_signal[host].spiq_in, false);
  332. #if CONFIG_IDF_TARGET_ESP32S2
  333. PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[bus_config->miso_io_num]);
  334. #endif
  335. PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[bus_config->miso_io_num], FUNC_GPIO);
  336. }
  337. if (bus_config->quadwp_io_num >= 0) {
  338. gpio_set_direction(bus_config->quadwp_io_num, GPIO_MODE_INPUT_OUTPUT);
  339. esp_rom_gpio_connect_out_signal(bus_config->quadwp_io_num, spi_periph_signal[host].spiwp_out, false, false);
  340. esp_rom_gpio_connect_in_signal(bus_config->quadwp_io_num, spi_periph_signal[host].spiwp_in, false);
  341. #if CONFIG_IDF_TARGET_ESP32S2
  342. PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[bus_config->quadwp_io_num]);
  343. #endif
  344. PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[bus_config->quadwp_io_num], FUNC_GPIO);
  345. }
  346. if (bus_config->quadhd_io_num >= 0) {
  347. gpio_set_direction(bus_config->quadhd_io_num, GPIO_MODE_INPUT_OUTPUT);
  348. esp_rom_gpio_connect_out_signal(bus_config->quadhd_io_num, spi_periph_signal[host].spihd_out, false, false);
  349. esp_rom_gpio_connect_in_signal(bus_config->quadhd_io_num, spi_periph_signal[host].spihd_in, false);
  350. #if CONFIG_IDF_TARGET_ESP32S2
  351. PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[bus_config->quadhd_io_num]);
  352. #endif
  353. PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[bus_config->quadhd_io_num], FUNC_GPIO);
  354. }
  355. if (bus_config->sclk_io_num >= 0) {
  356. if (sclk_need_output) {
  357. gpio_set_direction(bus_config->sclk_io_num, GPIO_MODE_INPUT_OUTPUT);
  358. esp_rom_gpio_connect_out_signal(bus_config->sclk_io_num, spi_periph_signal[host].spiclk_out, false, false);
  359. } else {
  360. gpio_set_direction(bus_config->sclk_io_num, GPIO_MODE_INPUT);
  361. }
  362. esp_rom_gpio_connect_in_signal(bus_config->sclk_io_num, spi_periph_signal[host].spiclk_in, false);
  363. #if CONFIG_IDF_TARGET_ESP32S2
  364. PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[bus_config->sclk_io_num]);
  365. #endif
  366. PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[bus_config->sclk_io_num], FUNC_GPIO);
  367. }
  368. }
  369. if (flags_o) *flags_o = temp_flag;
  370. return ESP_OK;
  371. }
  372. esp_err_t spicommon_bus_free_io_cfg(const spi_bus_config_t *bus_cfg)
  373. {
  374. int pin_array[] = {
  375. bus_cfg->mosi_io_num,
  376. bus_cfg->miso_io_num,
  377. bus_cfg->sclk_io_num,
  378. bus_cfg->quadwp_io_num,
  379. bus_cfg->quadhd_io_num,
  380. };
  381. for (int i = 0; i < sizeof(pin_array)/sizeof(int); i ++) {
  382. const int io = pin_array[i];
  383. if (io >= 0 && GPIO_IS_VALID_GPIO(io)) gpio_reset_pin(io);
  384. }
  385. return ESP_OK;
  386. }
  387. void spicommon_cs_initialize(spi_host_device_t host, int cs_io_num, int cs_num, int force_gpio_matrix)
  388. {
  389. if (!force_gpio_matrix && cs_io_num == spi_periph_signal[host].spics0_iomux_pin && cs_num == 0) {
  390. //The cs0s for all SPI peripherals map to pin mux source 1, so we use that instead of a define.
  391. gpio_iomux_in(cs_io_num, spi_periph_signal[host].spics_in);
  392. gpio_iomux_out(cs_io_num, spi_periph_signal[host].func, false);
  393. } else {
  394. //Use GPIO matrix
  395. if (GPIO_IS_VALID_OUTPUT_GPIO(cs_io_num)) {
  396. gpio_set_direction(cs_io_num, GPIO_MODE_INPUT_OUTPUT);
  397. esp_rom_gpio_connect_out_signal(cs_io_num, spi_periph_signal[host].spics_out[cs_num], false, false);
  398. } else {
  399. gpio_set_direction(cs_io_num, GPIO_MODE_INPUT);
  400. }
  401. if (cs_num == 0) esp_rom_gpio_connect_in_signal(cs_io_num, spi_periph_signal[host].spics_in, false);
  402. PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[cs_io_num]);
  403. PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[cs_io_num], FUNC_GPIO);
  404. }
  405. }
  406. void spicommon_cs_free_io(int cs_gpio_num)
  407. {
  408. assert(cs_gpio_num>=0 && GPIO_IS_VALID_GPIO(cs_gpio_num));
  409. gpio_reset_pin(cs_gpio_num);
  410. }
  411. bool spicommon_bus_using_iomux(spi_host_device_t host)
  412. {
  413. #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
  414. CHECK_IOMUX_PIN(host, spid);
  415. CHECK_IOMUX_PIN(host, spiq);
  416. CHECK_IOMUX_PIN(host, spiwp);
  417. CHECK_IOMUX_PIN(host, spihd);
  418. return true;
  419. }
  420. void spi_bus_main_set_lock(spi_bus_lock_handle_t lock)
  421. {
  422. bus_ctx[0]->bus_attr.lock = lock;
  423. }
  424. spi_bus_lock_handle_t spi_bus_lock_get_by_id(spi_host_device_t host_id)
  425. {
  426. return bus_ctx[host_id]->bus_attr.lock;
  427. }
  428. static inline bool is_valid_host(spi_host_device_t host)
  429. {
  430. return host >= SPI1_HOST && host <= SPI3_HOST;
  431. }
  432. esp_err_t spi_bus_initialize(spi_host_device_t host_id, const spi_bus_config_t *bus_config, int dma_chan)
  433. {
  434. esp_err_t err = ESP_OK;
  435. spicommon_bus_context_t *ctx = NULL;
  436. spi_bus_attr_t *bus_attr = NULL;
  437. SPI_CHECK(is_valid_host(host_id), "invalid host_id", ESP_ERR_INVALID_ARG);
  438. SPI_CHECK(bus_ctx[host_id] == NULL, "SPI bus already initialized.", ESP_ERR_INVALID_STATE);
  439. #ifdef CONFIG_IDF_TARGET_ESP32
  440. SPI_CHECK( dma_chan >= 0 && dma_chan <= 2, "invalid dma channel", ESP_ERR_INVALID_ARG );
  441. #elif CONFIG_IDF_TARGET_ESP32S2
  442. SPI_CHECK( dma_chan == 0 || dma_chan == host_id, "invalid dma channel", ESP_ERR_INVALID_ARG );
  443. #endif
  444. 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);
  445. #ifndef CONFIG_SPI_MASTER_ISR_IN_IRAM
  446. 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);
  447. #endif
  448. bool spi_chan_claimed = spicommon_periph_claim(host_id, "spi master");
  449. SPI_CHECK(spi_chan_claimed, "host_id already in use", ESP_ERR_INVALID_STATE);
  450. if (dma_chan != 0) {
  451. bool dma_chan_claimed = spicommon_dma_chan_claim(dma_chan);
  452. if (!dma_chan_claimed) {
  453. spicommon_periph_free(host_id);
  454. SPI_CHECK(false, "dma channel already in use", ESP_ERR_INVALID_STATE);
  455. }
  456. spicommon_connect_spi_and_dma(host_id, dma_chan);
  457. }
  458. //clean and initialize the context
  459. ctx = (spicommon_bus_context_t*)malloc(sizeof(spicommon_bus_context_t));
  460. if (!ctx) {
  461. err = ESP_ERR_NO_MEM;
  462. goto cleanup;
  463. }
  464. *ctx = (spicommon_bus_context_t) {
  465. .host_id = host_id,
  466. .bus_attr = {
  467. .bus_cfg = *bus_config,
  468. .dma_chan = dma_chan,
  469. },
  470. };
  471. bus_attr = &ctx->bus_attr;
  472. if (dma_chan == 0) {
  473. bus_attr->max_transfer_sz = SOC_SPI_MAXIMUM_BUFFER_SIZE;
  474. bus_attr->dma_desc_num = 0;
  475. } else {
  476. //See how many dma descriptors we need and allocate them
  477. int dma_desc_ct = lldesc_get_required_num(bus_config->max_transfer_sz);
  478. if (dma_desc_ct == 0) dma_desc_ct = 1; //default to 4k when max is not given
  479. bus_attr->max_transfer_sz = dma_desc_ct * LLDESC_MAX_NUM_PER_DESC;
  480. bus_attr->dmadesc_tx = heap_caps_malloc(sizeof(lldesc_t) * dma_desc_ct, MALLOC_CAP_DMA);
  481. bus_attr->dmadesc_rx = heap_caps_malloc(sizeof(lldesc_t) * dma_desc_ct, MALLOC_CAP_DMA);
  482. if (bus_attr->dmadesc_tx == NULL || bus_attr->dmadesc_rx == NULL) {
  483. err = ESP_ERR_NO_MEM;
  484. goto cleanup;
  485. }
  486. bus_attr->dma_desc_num = dma_desc_ct;
  487. }
  488. spi_bus_lock_config_t lock_config = {
  489. .host_id = host_id,
  490. .cs_num = SOC_SPI_PERIPH_CS_NUM(host_id),
  491. };
  492. err = spi_bus_init_lock(&bus_attr->lock, &lock_config);
  493. if (err != ESP_OK) {
  494. goto cleanup;
  495. }
  496. #ifdef CONFIG_PM_ENABLE
  497. err = esp_pm_lock_create(ESP_PM_APB_FREQ_MAX, 0, "spi_master",
  498. &bus_attr->pm_lock);
  499. if (err != ESP_OK) {
  500. goto cleanup;
  501. }
  502. #endif //CONFIG_PM_ENABLE
  503. err = spicommon_bus_initialize_io(host_id, bus_config, dma_chan, SPICOMMON_BUSFLAG_MASTER | bus_config->flags, &bus_attr->flags);
  504. if (err != ESP_OK) {
  505. goto cleanup;
  506. }
  507. bus_ctx[host_id] = ctx;
  508. return ESP_OK;
  509. cleanup:
  510. if (bus_attr) {
  511. #ifdef CONFIG_PM_ENABLE
  512. esp_pm_lock_delete(bus_attr->pm_lock);
  513. #endif
  514. if (bus_attr->lock) {
  515. spi_bus_deinit_lock(bus_attr->lock);
  516. }
  517. free(bus_attr->dmadesc_tx);
  518. free(bus_attr->dmadesc_rx);
  519. }
  520. free(ctx);
  521. if (dma_chan) {
  522. spicommon_dma_chan_free(dma_chan);
  523. }
  524. spicommon_periph_free(host_id);
  525. return err;
  526. }
  527. const spi_bus_attr_t* spi_bus_get_attr(spi_host_device_t host_id)
  528. {
  529. if (bus_ctx[host_id] == NULL) return NULL;
  530. return &bus_ctx[host_id]->bus_attr;
  531. }
  532. esp_err_t spi_bus_free(spi_host_device_t host_id)
  533. {
  534. esp_err_t err = ESP_OK;
  535. spicommon_bus_context_t* ctx = bus_ctx[host_id];
  536. spi_bus_attr_t* bus_attr = &ctx->bus_attr;
  537. if (ctx->destroy_func) {
  538. err = ctx->destroy_func(ctx->destroy_arg);
  539. }
  540. spicommon_bus_free_io_cfg(&bus_attr->bus_cfg);
  541. #ifdef CONFIG_PM_ENABLE
  542. esp_pm_lock_delete(bus_attr->pm_lock);
  543. #endif
  544. spi_bus_deinit_lock(bus_attr->lock);
  545. free(bus_attr->dmadesc_rx);
  546. free(bus_attr->dmadesc_tx);
  547. if (bus_attr->dma_chan > 0) {
  548. spicommon_dma_chan_free (bus_attr->dma_chan);
  549. }
  550. spicommon_periph_free(host_id);
  551. free(ctx);
  552. bus_ctx[host_id] = NULL;
  553. return err;
  554. }
  555. esp_err_t spi_bus_register_destroy_func(spi_host_device_t host_id,
  556. spi_destroy_func_t f, void *arg)
  557. {
  558. bus_ctx[host_id]->destroy_func = f;
  559. bus_ctx[host_id]->destroy_arg = arg;
  560. return ESP_OK;
  561. }
  562. /*
  563. Code for workaround for DMA issue in ESP32 v0/v1 silicon
  564. */
  565. #if CONFIG_IDF_TARGET_ESP32
  566. static volatile int dmaworkaround_channels_busy[2] = {0, 0};
  567. static dmaworkaround_cb_t dmaworkaround_cb;
  568. static void *dmaworkaround_cb_arg;
  569. static portMUX_TYPE dmaworkaround_mux = portMUX_INITIALIZER_UNLOCKED;
  570. static int dmaworkaround_waiting_for_chan = 0;
  571. #endif
  572. bool IRAM_ATTR spicommon_dmaworkaround_req_reset(int dmachan, dmaworkaround_cb_t cb, void *arg)
  573. {
  574. #if CONFIG_IDF_TARGET_ESP32
  575. int otherchan = (dmachan == 1) ? 2 : 1;
  576. bool ret;
  577. portENTER_CRITICAL_ISR(&dmaworkaround_mux);
  578. if (dmaworkaround_channels_busy[otherchan-1]) {
  579. //Other channel is busy. Call back when it's done.
  580. dmaworkaround_cb = cb;
  581. dmaworkaround_cb_arg = arg;
  582. dmaworkaround_waiting_for_chan = otherchan;
  583. ret = false;
  584. } else {
  585. //Reset DMA
  586. periph_module_reset( PERIPH_SPI_DMA_MODULE );
  587. ret = true;
  588. }
  589. portEXIT_CRITICAL_ISR(&dmaworkaround_mux);
  590. return ret;
  591. #else
  592. //no need to reset
  593. return true;
  594. #endif
  595. }
  596. bool IRAM_ATTR spicommon_dmaworkaround_reset_in_progress(void)
  597. {
  598. #if CONFIG_IDF_TARGET_ESP32
  599. return (dmaworkaround_waiting_for_chan != 0);
  600. #else
  601. return false;
  602. #endif
  603. }
  604. void IRAM_ATTR spicommon_dmaworkaround_idle(int dmachan)
  605. {
  606. #if CONFIG_IDF_TARGET_ESP32
  607. portENTER_CRITICAL_ISR(&dmaworkaround_mux);
  608. dmaworkaround_channels_busy[dmachan-1] = 0;
  609. if (dmaworkaround_waiting_for_chan == dmachan) {
  610. //Reset DMA
  611. periph_module_reset( PERIPH_SPI_DMA_MODULE );
  612. dmaworkaround_waiting_for_chan = 0;
  613. //Call callback
  614. dmaworkaround_cb(dmaworkaround_cb_arg);
  615. }
  616. portEXIT_CRITICAL_ISR(&dmaworkaround_mux);
  617. #endif
  618. }
  619. void IRAM_ATTR spicommon_dmaworkaround_transfer_active(int dmachan)
  620. {
  621. #if CONFIG_IDF_TARGET_ESP32
  622. portENTER_CRITICAL_ISR(&dmaworkaround_mux);
  623. dmaworkaround_channels_busy[dmachan-1] = 1;
  624. portEXIT_CRITICAL_ISR(&dmaworkaround_mux);
  625. #endif
  626. }