spi_common.c 25 KB

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