spi_common.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. // Copyright 2015-2018 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include <string.h>
  14. #include "driver/spi_master.h"
  15. #include "soc/dport_reg.h"
  16. #include "soc/spi_periph.h"
  17. #include "rom/ets_sys.h"
  18. #include "esp_types.h"
  19. #include "esp_attr.h"
  20. #include "esp_intr.h"
  21. #include "esp_intr_alloc.h"
  22. #include "esp_log.h"
  23. #include "esp_err.h"
  24. #include "soc/soc.h"
  25. #include "soc/dport_reg.h"
  26. #include "rom/lldesc.h"
  27. #include "driver/gpio.h"
  28. #include "driver/periph_ctrl.h"
  29. #include "esp_heap_caps.h"
  30. #include "driver/spi_common.h"
  31. #include "stdatomic.h"
  32. static const char *SPI_TAG = "spi";
  33. #define SPI_CHECK(a, str, ret_val) \
  34. if (!(a)) { \
  35. ESP_LOGE(SPI_TAG,"%s(%d): %s", __FUNCTION__, __LINE__, str); \
  36. return (ret_val); \
  37. }
  38. typedef struct spi_device_t spi_device_t;
  39. #define FUNC_SPI 1 //all pins of HSPI and VSPI shares this function number
  40. #define FUNC_GPIO PIN_FUNC_GPIO
  41. #define DMA_CHANNEL_ENABLED(dma_chan) (BIT(dma_chan-1))
  42. //Periph 1 is 'claimed' by SPI flash code.
  43. static atomic_bool spi_periph_claimed[3] = { ATOMIC_VAR_INIT(true), ATOMIC_VAR_INIT(false), ATOMIC_VAR_INIT(false)};
  44. static const char* spi_claiming_func[3] = {NULL, NULL, NULL};
  45. static uint8_t spi_dma_chan_enabled = 0;
  46. static portMUX_TYPE spi_dma_spinlock = portMUX_INITIALIZER_UNLOCKED;
  47. //Returns true if this peripheral is successfully claimed, false if otherwise.
  48. bool spicommon_periph_claim(spi_host_device_t host, const char* source)
  49. {
  50. bool false_var = false;
  51. bool ret = atomic_compare_exchange_strong(&spi_periph_claimed[host], &false_var, true);
  52. if (ret) {
  53. spi_claiming_func[host] = source;
  54. periph_module_enable(spi_periph_signal[host].module);
  55. } else {
  56. ESP_EARLY_LOGE(SPI_TAG, "SPI%d already claimed by %s.", host+1, spi_claiming_func[host]);
  57. }
  58. return ret;
  59. }
  60. bool spicommon_periph_in_use(spi_host_device_t host)
  61. {
  62. return atomic_load(&spi_periph_claimed[host]);
  63. }
  64. //Returns true if this peripheral is successfully freed, false if otherwise.
  65. bool spicommon_periph_free(spi_host_device_t host)
  66. {
  67. bool true_var = true;
  68. bool ret = atomic_compare_exchange_strong(&spi_periph_claimed[host], &true_var, false);
  69. if (ret) periph_module_disable(spi_periph_signal[host].module);
  70. return ret;
  71. }
  72. int spicommon_irqsource_for_host(spi_host_device_t host)
  73. {
  74. return spi_periph_signal[host].irq;
  75. }
  76. spi_dev_t *spicommon_hw_for_host(spi_host_device_t host)
  77. {
  78. return spi_periph_signal[host].hw;
  79. }
  80. bool spicommon_dma_chan_claim (int dma_chan)
  81. {
  82. bool ret = false;
  83. assert( dma_chan == 1 || dma_chan == 2 );
  84. portENTER_CRITICAL(&spi_dma_spinlock);
  85. if ( !(spi_dma_chan_enabled & DMA_CHANNEL_ENABLED(dma_chan)) ) {
  86. // get the channel only when it's not claimed yet.
  87. spi_dma_chan_enabled |= DMA_CHANNEL_ENABLED(dma_chan);
  88. ret = true;
  89. }
  90. periph_module_enable( PERIPH_SPI_DMA_MODULE );
  91. portEXIT_CRITICAL(&spi_dma_spinlock);
  92. return ret;
  93. }
  94. bool spicommon_dma_chan_in_use(int dma_chan)
  95. {
  96. assert(dma_chan==1 || dma_chan == 2);
  97. return spi_dma_chan_enabled & DMA_CHANNEL_ENABLED(dma_chan);
  98. }
  99. bool spicommon_dma_chan_free(int dma_chan)
  100. {
  101. assert( dma_chan == 1 || dma_chan == 2 );
  102. assert( spi_dma_chan_enabled & DMA_CHANNEL_ENABLED(dma_chan) );
  103. portENTER_CRITICAL(&spi_dma_spinlock);
  104. spi_dma_chan_enabled &= ~DMA_CHANNEL_ENABLED(dma_chan);
  105. if ( spi_dma_chan_enabled == 0 ) {
  106. //disable the DMA only when all the channels are freed.
  107. periph_module_disable( PERIPH_SPI_DMA_MODULE );
  108. }
  109. portEXIT_CRITICAL(&spi_dma_spinlock);
  110. return true;
  111. }
  112. /*
  113. 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
  114. bus config struct and it'll set up the GPIO matrix and enable the device. If a pin is set to non-negative value,
  115. it should be able to be initialized.
  116. */
  117. 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)
  118. {
  119. bool use_iomux = true;
  120. uint32_t temp_flag=0;
  121. bool quad_pins_exist = true;
  122. //the MISO should be output capable in slave mode, or in DIO/QIO mode.
  123. bool miso_output = !(flags&SPICOMMON_BUSFLAG_MASTER) || flags&SPICOMMON_BUSFLAG_DUAL;
  124. //the MOSI should be output capble in master mode, or in DIO/QIO mode.
  125. bool mosi_output = (flags&SPICOMMON_BUSFLAG_MASTER)!=0 || flags&SPICOMMON_BUSFLAG_DUAL;
  126. //check pins existence and if the selected pins correspond to the iomux pins of the peripheral
  127. if (bus_config->sclk_io_num>=0) {
  128. temp_flag |= SPICOMMON_BUSFLAG_SCLK;
  129. SPI_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(bus_config->sclk_io_num), "sclk not valid", ESP_ERR_INVALID_ARG);
  130. if (bus_config->sclk_io_num != spi_periph_signal[host].spiclk_iomux_pin) use_iomux = false;
  131. } else {
  132. SPI_CHECK((flags&SPICOMMON_BUSFLAG_SCLK)==0, "sclk pin required.", ESP_ERR_INVALID_ARG);
  133. }
  134. if (bus_config->quadwp_io_num>=0) {
  135. SPI_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(bus_config->quadwp_io_num), "spiwp not valid", ESP_ERR_INVALID_ARG);
  136. if (bus_config->quadwp_io_num != spi_periph_signal[host].spiwp_iomux_pin) use_iomux = false;
  137. } else {
  138. quad_pins_exist = false;
  139. SPI_CHECK((flags&SPICOMMON_BUSFLAG_WPHD)==0, "spiwp pin required.", ESP_ERR_INVALID_ARG);
  140. }
  141. if (bus_config->quadhd_io_num>=0) {
  142. SPI_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(bus_config->quadhd_io_num), "spihd not valid", ESP_ERR_INVALID_ARG);
  143. if (bus_config->quadhd_io_num != spi_periph_signal[host].spihd_iomux_pin) use_iomux = false;
  144. } else {
  145. quad_pins_exist = false;
  146. SPI_CHECK((flags&SPICOMMON_BUSFLAG_WPHD)==0, "spihd pin required.", ESP_ERR_INVALID_ARG);
  147. }
  148. if (bus_config->mosi_io_num >= 0) {
  149. temp_flag |= SPICOMMON_BUSFLAG_MOSI;
  150. if (mosi_output) {
  151. SPI_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(bus_config->mosi_io_num), "mosi not valid", ESP_ERR_INVALID_ARG);
  152. } else {
  153. SPI_CHECK(GPIO_IS_VALID_GPIO(bus_config->mosi_io_num), "mosi not valid", ESP_ERR_INVALID_ARG);
  154. }
  155. if (bus_config->mosi_io_num != spi_periph_signal[host].spid_iomux_pin) use_iomux = false;
  156. } else {
  157. SPI_CHECK((flags&SPICOMMON_BUSFLAG_MOSI)==0, "mosi pin required.", ESP_ERR_INVALID_ARG);
  158. }
  159. if (bus_config->miso_io_num>=0) {
  160. temp_flag |= SPICOMMON_BUSFLAG_MISO;
  161. if (miso_output) {
  162. SPI_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(bus_config->miso_io_num), "miso not valid", ESP_ERR_INVALID_ARG);
  163. } else {
  164. SPI_CHECK(GPIO_IS_VALID_GPIO(bus_config->miso_io_num), "miso not valid", ESP_ERR_INVALID_ARG);
  165. }
  166. if (bus_config->miso_io_num != spi_periph_signal[host].spiq_iomux_pin) use_iomux = false;
  167. } else {
  168. SPI_CHECK((flags&SPICOMMON_BUSFLAG_MISO)==0, "miso pin required.", ESP_ERR_INVALID_ARG);
  169. }
  170. //set flags for DUAL mode according to output-capability of MOSI and MISO pins.
  171. if ( (bus_config->mosi_io_num < 0 || GPIO_IS_VALID_OUTPUT_GPIO(bus_config->mosi_io_num)) &&
  172. (bus_config->miso_io_num < 0 || GPIO_IS_VALID_OUTPUT_GPIO(bus_config->miso_io_num)) ) {
  173. temp_flag |= SPICOMMON_BUSFLAG_DUAL;
  174. }
  175. //set flags for QUAD mode according to the existence of wp and hd
  176. if (quad_pins_exist) temp_flag |= SPICOMMON_BUSFLAG_WPHD;
  177. //check iomux pins if required.
  178. SPI_CHECK((flags&SPICOMMON_BUSFLAG_NATIVE_PINS)==0 || use_iomux, "not using iomux pins", ESP_ERR_INVALID_ARG);
  179. if (use_iomux) {
  180. //All SPI iomux pin selections resolve to 1, so we put that here instead of trying to figure
  181. //out which FUNC_GPIOx_xSPIxx to grab; they all are defined to 1 anyway.
  182. ESP_LOGD(SPI_TAG, "SPI%d use iomux pins.", host+1);
  183. if (bus_config->mosi_io_num >= 0) {
  184. gpio_iomux_in(bus_config->mosi_io_num, spi_periph_signal[host].spid_in);
  185. gpio_iomux_out(bus_config->mosi_io_num, FUNC_SPI, false);
  186. }
  187. if (bus_config->miso_io_num >= 0) {
  188. gpio_iomux_in(bus_config->miso_io_num, spi_periph_signal[host].spiq_in);
  189. gpio_iomux_out(bus_config->miso_io_num, FUNC_SPI, false);
  190. }
  191. if (bus_config->quadwp_io_num >= 0) {
  192. gpio_iomux_in(bus_config->quadwp_io_num, spi_periph_signal[host].spiwp_in);
  193. gpio_iomux_out(bus_config->quadwp_io_num, FUNC_SPI, false);
  194. }
  195. if (bus_config->quadhd_io_num >= 0) {
  196. gpio_iomux_in(bus_config->quadhd_io_num, spi_periph_signal[host].spihd_in);
  197. gpio_iomux_out(bus_config->quadhd_io_num, FUNC_SPI, false);
  198. }
  199. if (bus_config->sclk_io_num >= 0) {
  200. gpio_iomux_in(bus_config->sclk_io_num, spi_periph_signal[host].spiclk_in);
  201. gpio_iomux_out(bus_config->sclk_io_num, FUNC_SPI, false);
  202. }
  203. temp_flag |= SPICOMMON_BUSFLAG_NATIVE_PINS;
  204. } else {
  205. //Use GPIO matrix
  206. ESP_LOGD(SPI_TAG, "SPI%d use gpio matrix.", host+1);
  207. if (bus_config->mosi_io_num >= 0) {
  208. if (mosi_output || (temp_flag&SPICOMMON_BUSFLAG_DUAL)) {
  209. gpio_set_direction(bus_config->mosi_io_num, GPIO_MODE_INPUT_OUTPUT);
  210. gpio_matrix_out(bus_config->mosi_io_num, spi_periph_signal[host].spid_out, false, false);
  211. } else {
  212. gpio_set_direction(bus_config->mosi_io_num, GPIO_MODE_INPUT);
  213. }
  214. gpio_matrix_in(bus_config->mosi_io_num, spi_periph_signal[host].spid_in, false);
  215. PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[bus_config->mosi_io_num], FUNC_GPIO);
  216. }
  217. if (bus_config->miso_io_num >= 0) {
  218. if (miso_output || (temp_flag&SPICOMMON_BUSFLAG_DUAL)) {
  219. gpio_set_direction(bus_config->miso_io_num, GPIO_MODE_INPUT_OUTPUT);
  220. gpio_matrix_out(bus_config->miso_io_num, spi_periph_signal[host].spiq_out, false, false);
  221. } else {
  222. gpio_set_direction(bus_config->miso_io_num, GPIO_MODE_INPUT);
  223. }
  224. gpio_matrix_in(bus_config->miso_io_num, spi_periph_signal[host].spiq_in, false);
  225. PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[bus_config->miso_io_num], FUNC_GPIO);
  226. }
  227. if (bus_config->quadwp_io_num >= 0) {
  228. gpio_set_direction(bus_config->quadwp_io_num, GPIO_MODE_INPUT_OUTPUT);
  229. gpio_matrix_out(bus_config->quadwp_io_num, spi_periph_signal[host].spiwp_out, false, false);
  230. gpio_matrix_in(bus_config->quadwp_io_num, spi_periph_signal[host].spiwp_in, false);
  231. PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[bus_config->quadwp_io_num], FUNC_GPIO);
  232. }
  233. if (bus_config->quadhd_io_num >= 0) {
  234. gpio_set_direction(bus_config->quadhd_io_num, GPIO_MODE_INPUT_OUTPUT);
  235. gpio_matrix_out(bus_config->quadhd_io_num, spi_periph_signal[host].spihd_out, false, false);
  236. gpio_matrix_in(bus_config->quadhd_io_num, spi_periph_signal[host].spihd_in, false);
  237. PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[bus_config->quadhd_io_num], FUNC_GPIO);
  238. }
  239. if (bus_config->sclk_io_num >= 0) {
  240. gpio_set_direction(bus_config->sclk_io_num, GPIO_MODE_INPUT_OUTPUT);
  241. gpio_matrix_out(bus_config->sclk_io_num, spi_periph_signal[host].spiclk_out, false, false);
  242. gpio_matrix_in(bus_config->sclk_io_num, spi_periph_signal[host].spiclk_in, false);
  243. PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[bus_config->sclk_io_num], FUNC_GPIO);
  244. }
  245. }
  246. //Select DMA channel.
  247. DPORT_SET_PERI_REG_BITS(DPORT_SPI_DMA_CHAN_SEL_REG, 3, dma_chan, (host * 2));
  248. if (flags_o) *flags_o = temp_flag;
  249. return ESP_OK;
  250. }
  251. //Find any pin with output muxed to ``func`` and reset it to GPIO
  252. static void reset_func_to_gpio(int func)
  253. {
  254. for (int x = 0; x < GPIO_PIN_COUNT; x++) {
  255. if (GPIO_IS_VALID_GPIO(x) && (READ_PERI_REG(GPIO_FUNC0_OUT_SEL_CFG_REG + (x * 4))&GPIO_FUNC0_OUT_SEL_M) == func) {
  256. gpio_matrix_out(x, SIG_GPIO_OUT_IDX, false, false);
  257. }
  258. }
  259. }
  260. esp_err_t spicommon_bus_free_io(spi_host_device_t host)
  261. {
  262. if (REG_GET_FIELD(GPIO_PIN_MUX_REG[spi_periph_signal[host].spid_iomux_pin], MCU_SEL) == 1) PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[spi_periph_signal[host].spid_iomux_pin], PIN_FUNC_GPIO);
  263. if (REG_GET_FIELD(GPIO_PIN_MUX_REG[spi_periph_signal[host].spiq_iomux_pin], MCU_SEL) == 1) PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[spi_periph_signal[host].spiq_iomux_pin], PIN_FUNC_GPIO);
  264. if (REG_GET_FIELD(GPIO_PIN_MUX_REG[spi_periph_signal[host].spiclk_iomux_pin], MCU_SEL) == 1) PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[spi_periph_signal[host].spiclk_iomux_pin], PIN_FUNC_GPIO);
  265. if (REG_GET_FIELD(GPIO_PIN_MUX_REG[spi_periph_signal[host].spiwp_iomux_pin], MCU_SEL) == 1) PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[spi_periph_signal[host].spiwp_iomux_pin], PIN_FUNC_GPIO);
  266. if (REG_GET_FIELD(GPIO_PIN_MUX_REG[spi_periph_signal[host].spihd_iomux_pin], MCU_SEL) == 1) PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[spi_periph_signal[host].spihd_iomux_pin], PIN_FUNC_GPIO);
  267. reset_func_to_gpio(spi_periph_signal[host].spid_out);
  268. reset_func_to_gpio(spi_periph_signal[host].spiq_out);
  269. reset_func_to_gpio(spi_periph_signal[host].spiclk_out);
  270. reset_func_to_gpio(spi_periph_signal[host].spiwp_out);
  271. reset_func_to_gpio(spi_periph_signal[host].spihd_out);
  272. return ESP_OK;
  273. }
  274. esp_err_t spicommon_bus_free_io_cfg(const spi_bus_config_t *bus_cfg)
  275. {
  276. int pin_array[] = {
  277. bus_cfg->mosi_io_num,
  278. bus_cfg->miso_io_num,
  279. bus_cfg->sclk_io_num,
  280. bus_cfg->quadwp_io_num,
  281. bus_cfg->quadhd_io_num,
  282. };
  283. for (int i = 0; i < sizeof(pin_array)/sizeof(int); i ++) {
  284. const int io = pin_array[i];
  285. if (io >= 0 && GPIO_IS_VALID_GPIO(io)) gpio_reset_pin(io);
  286. }
  287. return ESP_OK;
  288. }
  289. void spicommon_cs_initialize(spi_host_device_t host, int cs_io_num, int cs_num, int force_gpio_matrix)
  290. {
  291. if (!force_gpio_matrix && cs_io_num == spi_periph_signal[host].spics0_iomux_pin && cs_num == 0) {
  292. //The cs0s for all SPI peripherals map to pin mux source 1, so we use that instead of a define.
  293. gpio_iomux_in(cs_io_num, spi_periph_signal[host].spics_in);
  294. gpio_iomux_out(cs_io_num, FUNC_SPI, false);
  295. } else {
  296. //Use GPIO matrix
  297. gpio_set_direction(cs_io_num, GPIO_MODE_INPUT_OUTPUT);
  298. gpio_matrix_out(cs_io_num, spi_periph_signal[host].spics_out[cs_num], false, false);
  299. if (cs_num == 0) gpio_matrix_in(cs_io_num, spi_periph_signal[host].spics_in, false);
  300. PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[cs_io_num], FUNC_GPIO);
  301. }
  302. }
  303. void spicommon_cs_free(spi_host_device_t host, int cs_io_num)
  304. {
  305. if (cs_io_num == 0 && REG_GET_FIELD(GPIO_PIN_MUX_REG[spi_periph_signal[host].spics0_iomux_pin], MCU_SEL) == 1) {
  306. PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[spi_periph_signal[host].spics0_iomux_pin], PIN_FUNC_GPIO);
  307. }
  308. reset_func_to_gpio(spi_periph_signal[host].spics_out[cs_io_num]);
  309. }
  310. void spicommon_cs_free_io(int cs_gpio_num)
  311. {
  312. assert(cs_gpio_num>=0 && GPIO_IS_VALID_GPIO(cs_gpio_num));
  313. gpio_reset_pin(cs_gpio_num);
  314. }
  315. //Set up a list of dma descriptors. dmadesc is an array of descriptors. Data is the buffer to point to.
  316. void IRAM_ATTR spicommon_setup_dma_desc_links(lldesc_t *dmadesc, int len, const uint8_t *data, bool isrx)
  317. {
  318. int n = 0;
  319. while (len) {
  320. int dmachunklen = len;
  321. if (dmachunklen > SPI_MAX_DMA_LEN) dmachunklen = SPI_MAX_DMA_LEN;
  322. if (isrx) {
  323. //Receive needs DMA length rounded to next 32-bit boundary
  324. dmadesc[n].size = (dmachunklen + 3) & (~3);
  325. dmadesc[n].length = (dmachunklen + 3) & (~3);
  326. } else {
  327. dmadesc[n].size = dmachunklen;
  328. dmadesc[n].length = dmachunklen;
  329. }
  330. dmadesc[n].buf = (uint8_t *)data;
  331. dmadesc[n].eof = 0;
  332. dmadesc[n].sosf = 0;
  333. dmadesc[n].owner = 1;
  334. dmadesc[n].qe.stqe_next = &dmadesc[n + 1];
  335. len -= dmachunklen;
  336. data += dmachunklen;
  337. n++;
  338. }
  339. dmadesc[n - 1].eof = 1; //Mark last DMA desc as end of stream.
  340. dmadesc[n - 1].qe.stqe_next = NULL;
  341. }
  342. /*
  343. Code for workaround for DMA issue in ESP32 v0/v1 silicon
  344. */
  345. static volatile int dmaworkaround_channels_busy[2] = {0, 0};
  346. static dmaworkaround_cb_t dmaworkaround_cb;
  347. static void *dmaworkaround_cb_arg;
  348. static portMUX_TYPE dmaworkaround_mux = portMUX_INITIALIZER_UNLOCKED;
  349. static int dmaworkaround_waiting_for_chan = 0;
  350. bool IRAM_ATTR spicommon_dmaworkaround_req_reset(int dmachan, dmaworkaround_cb_t cb, void *arg)
  351. {
  352. int otherchan = (dmachan == 1) ? 2 : 1;
  353. bool ret;
  354. portENTER_CRITICAL_ISR(&dmaworkaround_mux);
  355. if (dmaworkaround_channels_busy[otherchan-1]) {
  356. //Other channel is busy. Call back when it's done.
  357. dmaworkaround_cb = cb;
  358. dmaworkaround_cb_arg = arg;
  359. dmaworkaround_waiting_for_chan = otherchan;
  360. ret = false;
  361. } else {
  362. //Reset DMA
  363. periph_module_reset( PERIPH_SPI_DMA_MODULE );
  364. ret = true;
  365. }
  366. portEXIT_CRITICAL_ISR(&dmaworkaround_mux);
  367. return ret;
  368. }
  369. bool IRAM_ATTR spicommon_dmaworkaround_reset_in_progress()
  370. {
  371. return (dmaworkaround_waiting_for_chan != 0);
  372. }
  373. void IRAM_ATTR spicommon_dmaworkaround_idle(int dmachan)
  374. {
  375. portENTER_CRITICAL_ISR(&dmaworkaround_mux);
  376. dmaworkaround_channels_busy[dmachan-1] = 0;
  377. if (dmaworkaround_waiting_for_chan == dmachan) {
  378. //Reset DMA
  379. periph_module_reset( PERIPH_SPI_DMA_MODULE );
  380. dmaworkaround_waiting_for_chan = 0;
  381. //Call callback
  382. dmaworkaround_cb(dmaworkaround_cb_arg);
  383. }
  384. portEXIT_CRITICAL_ISR(&dmaworkaround_mux);
  385. }
  386. void IRAM_ATTR spicommon_dmaworkaround_transfer_active(int dmachan)
  387. {
  388. portENTER_CRITICAL_ISR(&dmaworkaround_mux);
  389. dmaworkaround_channels_busy[dmachan-1] = 1;
  390. portEXIT_CRITICAL_ISR(&dmaworkaround_mux);
  391. }