spi_common.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. // Copyright 2015-2016 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/gpio_sig_map.h"
  16. #include "soc/spi_reg.h"
  17. #include "soc/dport_reg.h"
  18. #include "soc/spi_struct.h"
  19. #include "rom/ets_sys.h"
  20. #include "esp_types.h"
  21. #include "esp_attr.h"
  22. #include "esp_intr.h"
  23. #include "esp_intr_alloc.h"
  24. #include "esp_log.h"
  25. #include "esp_err.h"
  26. #include "soc/soc.h"
  27. #include "soc/dport_reg.h"
  28. #include "rom/lldesc.h"
  29. #include "driver/gpio.h"
  30. #include "driver/periph_ctrl.h"
  31. #include "esp_heap_caps.h"
  32. #include "driver/spi_common.h"
  33. static const char *SPI_TAG = "spi";
  34. #define SPI_CHECK(a, str, ret_val) \
  35. if (!(a)) { \
  36. ESP_LOGE(SPI_TAG,"%s(%d): %s", __FUNCTION__, __LINE__, str); \
  37. return (ret_val); \
  38. }
  39. typedef struct spi_device_t spi_device_t;
  40. #define FUNC_SPI 1 //all pins of HSPI and VSPI shares this function number
  41. #define FUNC_GPIO PIN_FUNC_GPIO
  42. /*
  43. Stores a bunch of per-spi-peripheral data.
  44. */
  45. typedef struct {
  46. const uint8_t spiclk_out; //GPIO mux output signals
  47. const uint8_t spiclk_in;
  48. const uint8_t spid_out;
  49. const uint8_t spiq_out;
  50. const uint8_t spiwp_out;
  51. const uint8_t spihd_out;
  52. const uint8_t spid_in; //GPIO mux input signals
  53. const uint8_t spiq_in;
  54. const uint8_t spiwp_in;
  55. const uint8_t spihd_in;
  56. const uint8_t spics_out[3]; // /CS GPIO output mux signals
  57. const uint8_t spics_in;
  58. const uint8_t spiclk_native; //IO pins of IO_MUX muxed signals
  59. const uint8_t spid_native;
  60. const uint8_t spiq_native;
  61. const uint8_t spiwp_native;
  62. const uint8_t spihd_native;
  63. const uint8_t spics0_native;
  64. const uint8_t irq; //irq source for interrupt mux
  65. const uint8_t irq_dma; //dma irq source for interrupt mux
  66. const periph_module_t module; //peripheral module, for enabling clock etc
  67. spi_dev_t *hw; //Pointer to the hardware registers
  68. } spi_signal_conn_t;
  69. /*
  70. Bunch of constants for every SPI peripheral: GPIO signals, irqs, hw addr of registers etc
  71. */
  72. static const spi_signal_conn_t io_signal[3] = {
  73. {
  74. .spiclk_out = SPICLK_OUT_IDX,
  75. .spiclk_in = SPICLK_IN_IDX,
  76. .spid_out = SPID_OUT_IDX,
  77. .spiq_out = SPIQ_OUT_IDX,
  78. .spiwp_out = SPIWP_OUT_IDX,
  79. .spihd_out = SPIHD_OUT_IDX,
  80. .spid_in = SPID_IN_IDX,
  81. .spiq_in = SPIQ_IN_IDX,
  82. .spiwp_in = SPIWP_IN_IDX,
  83. .spihd_in = SPIHD_IN_IDX,
  84. .spics_out = {SPICS0_OUT_IDX, SPICS1_OUT_IDX, SPICS2_OUT_IDX},
  85. .spics_in = SPICS0_IN_IDX,
  86. .spiclk_native = 6,
  87. .spid_native = 8,
  88. .spiq_native = 7,
  89. .spiwp_native = 10,
  90. .spihd_native = 9,
  91. .spics0_native = 11,
  92. .irq = ETS_SPI1_INTR_SOURCE,
  93. .irq_dma = ETS_SPI1_DMA_INTR_SOURCE,
  94. .module = PERIPH_SPI_MODULE,
  95. .hw = &SPI1
  96. }, {
  97. .spiclk_out = HSPICLK_OUT_IDX,
  98. .spiclk_in = HSPICLK_IN_IDX,
  99. .spid_out = HSPID_OUT_IDX,
  100. .spiq_out = HSPIQ_OUT_IDX,
  101. .spiwp_out = HSPIWP_OUT_IDX,
  102. .spihd_out = HSPIHD_OUT_IDX,
  103. .spid_in = HSPID_IN_IDX,
  104. .spiq_in = HSPIQ_IN_IDX,
  105. .spiwp_in = HSPIWP_IN_IDX,
  106. .spihd_in = HSPIHD_IN_IDX,
  107. .spics_out = {HSPICS0_OUT_IDX, HSPICS1_OUT_IDX, HSPICS2_OUT_IDX},
  108. .spics_in = HSPICS0_IN_IDX,
  109. .spiclk_native = 14,
  110. .spid_native = 13,
  111. .spiq_native = 12,
  112. .spiwp_native = 2,
  113. .spihd_native = 4,
  114. .spics0_native = 15,
  115. .irq = ETS_SPI2_INTR_SOURCE,
  116. .irq_dma = ETS_SPI2_DMA_INTR_SOURCE,
  117. .module = PERIPH_HSPI_MODULE,
  118. .hw = &SPI2
  119. }, {
  120. .spiclk_out = VSPICLK_OUT_IDX,
  121. .spiclk_in = VSPICLK_IN_IDX,
  122. .spid_out = VSPID_OUT_IDX,
  123. .spiq_out = VSPIQ_OUT_IDX,
  124. .spiwp_out = VSPIWP_OUT_IDX,
  125. .spihd_out = VSPIHD_OUT_IDX,
  126. .spid_in = VSPID_IN_IDX,
  127. .spiq_in = VSPIQ_IN_IDX,
  128. .spiwp_in = VSPIWP_IN_IDX,
  129. .spihd_in = VSPIHD_IN_IDX,
  130. .spics_out = {VSPICS0_OUT_IDX, VSPICS1_OUT_IDX, VSPICS2_OUT_IDX},
  131. .spics_in = VSPICS0_IN_IDX,
  132. .spiclk_native = 18,
  133. .spid_native = 23,
  134. .spiq_native = 19,
  135. .spiwp_native = 22,
  136. .spihd_native = 21,
  137. .spics0_native = 5,
  138. .irq = ETS_SPI3_INTR_SOURCE,
  139. .irq_dma = ETS_SPI3_DMA_INTR_SOURCE,
  140. .module = PERIPH_VSPI_MODULE,
  141. .hw = &SPI3
  142. }
  143. };
  144. #define DMA_CHANNEL_ENABLED(dma_chan) (BIT(dma_chan-1))
  145. //Periph 1 is 'claimed' by SPI flash code.
  146. static bool spi_periph_claimed[3] = {true, false, false};
  147. static uint8_t spi_dma_chan_enabled = 0;
  148. static portMUX_TYPE spi_dma_spinlock = portMUX_INITIALIZER_UNLOCKED;
  149. //Returns true if this peripheral is successfully claimed, false if otherwise.
  150. bool spicommon_periph_claim(spi_host_device_t host)
  151. {
  152. bool ret = __sync_bool_compare_and_swap(&spi_periph_claimed[host], false, true);
  153. if (ret) periph_module_enable(io_signal[host].module);
  154. return ret;
  155. }
  156. //Returns true if this peripheral is successfully freed, false if otherwise.
  157. bool spicommon_periph_free(spi_host_device_t host)
  158. {
  159. bool ret = __sync_bool_compare_and_swap(&spi_periph_claimed[host], true, false);
  160. if (ret) periph_module_disable(io_signal[host].module);
  161. return ret;
  162. }
  163. int spicommon_irqsource_for_host(spi_host_device_t host)
  164. {
  165. return io_signal[host].irq;
  166. }
  167. spi_dev_t *spicommon_hw_for_host(spi_host_device_t host)
  168. {
  169. return io_signal[host].hw;
  170. }
  171. bool spicommon_dma_chan_claim (int dma_chan)
  172. {
  173. bool ret = false;
  174. assert( dma_chan == 1 || dma_chan == 2 );
  175. portENTER_CRITICAL(&spi_dma_spinlock);
  176. if ( !(spi_dma_chan_enabled & DMA_CHANNEL_ENABLED(dma_chan)) ) {
  177. // get the channel only when it's not claimed yet.
  178. spi_dma_chan_enabled |= DMA_CHANNEL_ENABLED(dma_chan);
  179. ret = true;
  180. }
  181. periph_module_enable( PERIPH_SPI_DMA_MODULE );
  182. portEXIT_CRITICAL(&spi_dma_spinlock);
  183. return ret;
  184. }
  185. bool spicommon_dma_chan_free(int dma_chan)
  186. {
  187. assert( dma_chan == 1 || dma_chan == 2 );
  188. assert( spi_dma_chan_enabled & DMA_CHANNEL_ENABLED(dma_chan) );
  189. portENTER_CRITICAL(&spi_dma_spinlock);
  190. spi_dma_chan_enabled &= ~DMA_CHANNEL_ENABLED(dma_chan);
  191. if ( spi_dma_chan_enabled == 0 ) {
  192. //disable the DMA only when all the channels are freed.
  193. periph_module_disable( PERIPH_SPI_DMA_MODULE );
  194. }
  195. portEXIT_CRITICAL(&spi_dma_spinlock);
  196. return true;
  197. }
  198. /*
  199. 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
  200. bus config struct and it'll set up the GPIO matrix and enable the device. If a pin is set to non-negative value,
  201. it should be able to be initialized.
  202. */
  203. 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)
  204. {
  205. bool native = true;
  206. uint32_t temp_flag=0;
  207. bool quad_pins_exist = true;
  208. //the MISO should be output capable in slave mode, or in DIO/QIO mode.
  209. bool miso_output = !(flags&SPICOMMON_BUSFLAG_MASTER) || flags&SPICOMMON_BUSFLAG_DUAL;
  210. //the MOSI should be output capble in master mode, or in DIO/QIO mode.
  211. bool mosi_output = (flags&SPICOMMON_BUSFLAG_MASTER)!=0 || flags&SPICOMMON_BUSFLAG_DUAL;
  212. //check pins existence and if the selected pins correspond to the native pins of the peripheral
  213. if (bus_config->sclk_io_num>=0) {
  214. temp_flag |= SPICOMMON_BUSFLAG_SCLK;
  215. SPI_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(bus_config->sclk_io_num), "sclk not valid", ESP_ERR_INVALID_ARG);
  216. if (bus_config->sclk_io_num != io_signal[host].spiclk_native) native = false;
  217. } else {
  218. SPI_CHECK((flags&SPICOMMON_BUSFLAG_SCLK)==0, "sclk pin required.", ESP_ERR_INVALID_ARG);
  219. }
  220. if (bus_config->quadwp_io_num>=0) {
  221. SPI_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(bus_config->quadwp_io_num), "spiwp not valid", ESP_ERR_INVALID_ARG);
  222. if (bus_config->quadwp_io_num != io_signal[host].spiwp_native) native = false;
  223. } else {
  224. quad_pins_exist = false;
  225. SPI_CHECK((flags&SPICOMMON_BUSFLAG_WPHD)==0, "spiwp pin required.", ESP_ERR_INVALID_ARG);
  226. }
  227. if (bus_config->quadhd_io_num>=0) {
  228. SPI_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(bus_config->quadhd_io_num), "spihd not valid", ESP_ERR_INVALID_ARG);
  229. if (bus_config->quadhd_io_num != io_signal[host].spihd_native) native = false;
  230. } else {
  231. quad_pins_exist = false;
  232. SPI_CHECK((flags&SPICOMMON_BUSFLAG_WPHD)==0, "spihd pin required.", ESP_ERR_INVALID_ARG);
  233. }
  234. if (bus_config->mosi_io_num >= 0) {
  235. temp_flag |= SPICOMMON_BUSFLAG_MOSI;
  236. if (mosi_output) {
  237. SPI_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(bus_config->mosi_io_num), "mosi not valid", ESP_ERR_INVALID_ARG);
  238. } else {
  239. SPI_CHECK(GPIO_IS_VALID_GPIO(bus_config->mosi_io_num), "mosi not valid", ESP_ERR_INVALID_ARG);
  240. }
  241. if (bus_config->mosi_io_num != io_signal[host].spid_native) native = false;
  242. } else {
  243. SPI_CHECK((flags&SPICOMMON_BUSFLAG_MOSI)==0, "mosi pin required.", ESP_ERR_INVALID_ARG);
  244. }
  245. if (bus_config->miso_io_num>=0) {
  246. temp_flag |= SPICOMMON_BUSFLAG_MISO;
  247. if (miso_output) {
  248. SPI_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(bus_config->miso_io_num), "miso not valid", ESP_ERR_INVALID_ARG);
  249. } else {
  250. SPI_CHECK(GPIO_IS_VALID_GPIO(bus_config->miso_io_num), "miso not valid", ESP_ERR_INVALID_ARG);
  251. }
  252. if (bus_config->miso_io_num != io_signal[host].spiq_native) native = false;
  253. } else {
  254. SPI_CHECK((flags&SPICOMMON_BUSFLAG_MISO)==0, "miso pin required.", ESP_ERR_INVALID_ARG);
  255. }
  256. //set flags for DUAL mode according to output-capability of MOSI and MISO pins.
  257. if ( (bus_config->mosi_io_num < 0 || GPIO_IS_VALID_OUTPUT_GPIO(bus_config->mosi_io_num)) &&
  258. (bus_config->miso_io_num < 0 || GPIO_IS_VALID_OUTPUT_GPIO(bus_config->miso_io_num)) ) {
  259. temp_flag |= SPICOMMON_BUSFLAG_DUAL;
  260. }
  261. //set flags for QUAD mode according to the existence of wp and hd
  262. if (quad_pins_exist) temp_flag |= SPICOMMON_BUSFLAG_WPHD;
  263. //check native pins if required.
  264. SPI_CHECK((flags&SPICOMMON_BUSFLAG_NATIVE_PINS)==0 || native, "not using native pins", ESP_ERR_INVALID_ARG);
  265. if (native) {
  266. //All SPI native pin selections resolve to 1, so we put that here instead of trying to figure
  267. //out which FUNC_GPIOx_xSPIxx to grab; they all are defined to 1 anyway.
  268. ESP_LOGD(SPI_TAG, "SPI%d use native pins.", host );
  269. if (bus_config->mosi_io_num >= 0) {
  270. gpio_iomux_in(bus_config->mosi_io_num, io_signal[host].spid_in);
  271. gpio_iomux_out(bus_config->mosi_io_num, FUNC_SPI, false);
  272. }
  273. if (bus_config->miso_io_num >= 0) {
  274. gpio_iomux_in(bus_config->miso_io_num, io_signal[host].spiq_in);
  275. gpio_iomux_out(bus_config->miso_io_num, FUNC_SPI, false);
  276. }
  277. if (bus_config->quadwp_io_num >= 0) {
  278. gpio_iomux_in(bus_config->quadwp_io_num, io_signal[host].spiwp_in);
  279. gpio_iomux_out(bus_config->quadwp_io_num, FUNC_SPI, false);
  280. }
  281. if (bus_config->quadhd_io_num >= 0) {
  282. gpio_iomux_in(bus_config->quadhd_io_num, io_signal[host].spihd_in);
  283. gpio_iomux_out(bus_config->quadhd_io_num, FUNC_SPI, false);
  284. }
  285. if (bus_config->sclk_io_num >= 0) {
  286. gpio_iomux_in(bus_config->sclk_io_num, io_signal[host].spiclk_in);
  287. gpio_iomux_out(bus_config->sclk_io_num, FUNC_SPI, false);
  288. }
  289. temp_flag |= SPICOMMON_BUSFLAG_NATIVE_PINS;
  290. } else {
  291. //Use GPIO matrix
  292. ESP_LOGD(SPI_TAG, "SPI%d use gpio matrix.", host );
  293. if (bus_config->mosi_io_num >= 0) {
  294. PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[bus_config->mosi_io_num], FUNC_GPIO);
  295. if (mosi_output || (temp_flag&SPICOMMON_BUSFLAG_DUAL)) {
  296. gpio_set_direction(bus_config->mosi_io_num, GPIO_MODE_INPUT_OUTPUT);
  297. gpio_matrix_out(bus_config->mosi_io_num, io_signal[host].spid_out, false, false);
  298. } else {
  299. gpio_set_direction(bus_config->mosi_io_num, GPIO_MODE_INPUT);
  300. }
  301. gpio_matrix_in(bus_config->mosi_io_num, io_signal[host].spid_in, false);
  302. }
  303. if (bus_config->miso_io_num >= 0) {
  304. PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[bus_config->miso_io_num], FUNC_GPIO);
  305. if (miso_output || (temp_flag&SPICOMMON_BUSFLAG_DUAL)) {
  306. gpio_set_direction(bus_config->miso_io_num, GPIO_MODE_INPUT_OUTPUT);
  307. gpio_matrix_out(bus_config->miso_io_num, io_signal[host].spiq_out, false, false);
  308. } else {
  309. gpio_set_direction(bus_config->miso_io_num, GPIO_MODE_INPUT);
  310. }
  311. gpio_matrix_in(bus_config->miso_io_num, io_signal[host].spiq_in, false);
  312. }
  313. if (bus_config->quadwp_io_num >= 0) {
  314. PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[bus_config->quadwp_io_num], FUNC_GPIO);
  315. gpio_set_direction(bus_config->quadwp_io_num, GPIO_MODE_INPUT_OUTPUT);
  316. gpio_matrix_out(bus_config->quadwp_io_num, io_signal[host].spiwp_out, false, false);
  317. gpio_matrix_in(bus_config->quadwp_io_num, io_signal[host].spiwp_in, false);
  318. }
  319. if (bus_config->quadhd_io_num >= 0) {
  320. PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[bus_config->quadhd_io_num], FUNC_GPIO);
  321. gpio_set_direction(bus_config->quadhd_io_num, GPIO_MODE_INPUT_OUTPUT);
  322. gpio_matrix_out(bus_config->quadhd_io_num, io_signal[host].spihd_out, false, false);
  323. gpio_matrix_in(bus_config->quadhd_io_num, io_signal[host].spihd_in, false);
  324. }
  325. if (bus_config->sclk_io_num >= 0) {
  326. PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[bus_config->sclk_io_num], FUNC_GPIO);
  327. gpio_set_direction(bus_config->sclk_io_num, GPIO_MODE_INPUT_OUTPUT);
  328. gpio_matrix_out(bus_config->sclk_io_num, io_signal[host].spiclk_out, false, false);
  329. gpio_matrix_in(bus_config->sclk_io_num, io_signal[host].spiclk_in, false);
  330. }
  331. }
  332. //Select DMA channel.
  333. DPORT_SET_PERI_REG_BITS(DPORT_SPI_DMA_CHAN_SEL_REG, 3, dma_chan, (host * 2));
  334. if (flags_o) *flags_o = temp_flag;
  335. return ESP_OK;
  336. }
  337. //Find any pin with output muxed to ``func`` and reset it to GPIO
  338. static void reset_func_to_gpio(int func)
  339. {
  340. for (int x = 0; x < GPIO_PIN_COUNT; x++) {
  341. if (GPIO_IS_VALID_GPIO(x) && (READ_PERI_REG(GPIO_FUNC0_OUT_SEL_CFG_REG + (x * 4))&GPIO_FUNC0_OUT_SEL_M) == func) {
  342. gpio_matrix_out(x, SIG_GPIO_OUT_IDX, false, false);
  343. }
  344. }
  345. }
  346. esp_err_t spicommon_bus_free_io(spi_host_device_t host)
  347. {
  348. if (REG_GET_FIELD(GPIO_PIN_MUX_REG[io_signal[host].spid_native], MCU_SEL) == 1) PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[io_signal[host].spid_native], PIN_FUNC_GPIO);
  349. if (REG_GET_FIELD(GPIO_PIN_MUX_REG[io_signal[host].spiq_native], MCU_SEL) == 1) PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[io_signal[host].spiq_native], PIN_FUNC_GPIO);
  350. if (REG_GET_FIELD(GPIO_PIN_MUX_REG[io_signal[host].spiclk_native], MCU_SEL) == 1) PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[io_signal[host].spiclk_native], PIN_FUNC_GPIO);
  351. if (REG_GET_FIELD(GPIO_PIN_MUX_REG[io_signal[host].spiwp_native], MCU_SEL) == 1) PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[io_signal[host].spiwp_native], PIN_FUNC_GPIO);
  352. if (REG_GET_FIELD(GPIO_PIN_MUX_REG[io_signal[host].spihd_native], MCU_SEL) == 1) PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[io_signal[host].spihd_native], PIN_FUNC_GPIO);
  353. reset_func_to_gpio(io_signal[host].spid_out);
  354. reset_func_to_gpio(io_signal[host].spiq_out);
  355. reset_func_to_gpio(io_signal[host].spiclk_out);
  356. reset_func_to_gpio(io_signal[host].spiwp_out);
  357. reset_func_to_gpio(io_signal[host].spihd_out);
  358. return ESP_OK;
  359. }
  360. void spicommon_cs_initialize(spi_host_device_t host, int cs_io_num, int cs_num, int force_gpio_matrix)
  361. {
  362. if (!force_gpio_matrix && cs_io_num == io_signal[host].spics0_native && cs_num == 0) {
  363. //The cs0s for all SPI peripherals map to pin mux source 1, so we use that instead of a define.
  364. gpio_iomux_in(cs_io_num, io_signal[host].spics_in);
  365. gpio_iomux_out(cs_io_num, FUNC_SPI, false);
  366. } else {
  367. //Use GPIO matrix
  368. PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[cs_io_num], FUNC_GPIO);
  369. gpio_matrix_out(cs_io_num, io_signal[host].spics_out[cs_num], false, false);
  370. if (cs_num == 0) gpio_matrix_in(cs_io_num, io_signal[host].spics_in, false);
  371. }
  372. }
  373. void spicommon_cs_free(spi_host_device_t host, int cs_io_num)
  374. {
  375. if (cs_io_num == 0 && REG_GET_FIELD(GPIO_PIN_MUX_REG[io_signal[host].spics0_native], MCU_SEL) == 1) {
  376. PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[io_signal[host].spics0_native], PIN_FUNC_GPIO);
  377. }
  378. reset_func_to_gpio(io_signal[host].spics_out[cs_io_num]);
  379. }
  380. //Set up a list of dma descriptors. dmadesc is an array of descriptors. Data is the buffer to point to.
  381. void spicommon_setup_dma_desc_links(lldesc_t *dmadesc, int len, const uint8_t *data, bool isrx)
  382. {
  383. int n = 0;
  384. while (len) {
  385. int dmachunklen = len;
  386. if (dmachunklen > SPI_MAX_DMA_LEN) dmachunklen = SPI_MAX_DMA_LEN;
  387. if (isrx) {
  388. //Receive needs DMA length rounded to next 32-bit boundary
  389. dmadesc[n].size = (dmachunklen + 3) & (~3);
  390. dmadesc[n].length = (dmachunklen + 3) & (~3);
  391. } else {
  392. dmadesc[n].size = dmachunklen;
  393. dmadesc[n].length = dmachunklen;
  394. }
  395. dmadesc[n].buf = (uint8_t *)data;
  396. dmadesc[n].eof = 0;
  397. dmadesc[n].sosf = 0;
  398. dmadesc[n].owner = 1;
  399. dmadesc[n].qe.stqe_next = &dmadesc[n + 1];
  400. len -= dmachunklen;
  401. data += dmachunklen;
  402. n++;
  403. }
  404. dmadesc[n - 1].eof = 1; //Mark last DMA desc as end of stream.
  405. dmadesc[n - 1].qe.stqe_next = NULL;
  406. }
  407. /*
  408. Code for workaround for DMA issue in ESP32 v0/v1 silicon
  409. */
  410. static volatile int dmaworkaround_channels_busy[2] = {0, 0};
  411. static dmaworkaround_cb_t dmaworkaround_cb;
  412. static void *dmaworkaround_cb_arg;
  413. static portMUX_TYPE dmaworkaround_mux = portMUX_INITIALIZER_UNLOCKED;
  414. static int dmaworkaround_waiting_for_chan = 0;
  415. bool IRAM_ATTR spicommon_dmaworkaround_req_reset(int dmachan, dmaworkaround_cb_t cb, void *arg)
  416. {
  417. int otherchan = (dmachan == 1) ? 2 : 1;
  418. bool ret;
  419. portENTER_CRITICAL_ISR(&dmaworkaround_mux);
  420. if (dmaworkaround_channels_busy[otherchan-1]) {
  421. //Other channel is busy. Call back when it's done.
  422. dmaworkaround_cb = cb;
  423. dmaworkaround_cb_arg = arg;
  424. dmaworkaround_waiting_for_chan = otherchan;
  425. ret = false;
  426. } else {
  427. //Reset DMA
  428. periph_module_reset( PERIPH_SPI_DMA_MODULE );
  429. ret = true;
  430. }
  431. portEXIT_CRITICAL_ISR(&dmaworkaround_mux);
  432. return ret;
  433. }
  434. bool IRAM_ATTR spicommon_dmaworkaround_reset_in_progress()
  435. {
  436. return (dmaworkaround_waiting_for_chan != 0);
  437. }
  438. void IRAM_ATTR spicommon_dmaworkaround_idle(int dmachan)
  439. {
  440. portENTER_CRITICAL_ISR(&dmaworkaround_mux);
  441. dmaworkaround_channels_busy[dmachan-1] = 0;
  442. if (dmaworkaround_waiting_for_chan == dmachan) {
  443. //Reset DMA
  444. periph_module_reset( PERIPH_SPI_DMA_MODULE );
  445. dmaworkaround_waiting_for_chan = 0;
  446. //Call callback
  447. dmaworkaround_cb(dmaworkaround_cb_arg);
  448. }
  449. portEXIT_CRITICAL_ISR(&dmaworkaround_mux);
  450. }
  451. void IRAM_ATTR spicommon_dmaworkaround_transfer_active(int dmachan)
  452. {
  453. portENTER_CRITICAL_ISR(&dmaworkaround_mux);
  454. dmaworkaround_channels_busy[dmachan-1] = 1;
  455. portEXIT_CRITICAL_ISR(&dmaworkaround_mux);
  456. }