spi_common.c 24 KB

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