test_unal_dma.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #include <esp_types.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include "rom/ets_sys.h"
  6. #include "rom/lldesc.h"
  7. #include "rom/gpio.h"
  8. #include "freertos/FreeRTOS.h"
  9. #include "freertos/task.h"
  10. #include "freertos/semphr.h"
  11. #include "freertos/queue.h"
  12. #include "freertos/xtensa_api.h"
  13. #include "unity.h"
  14. #include "soc/uart_reg.h"
  15. #include "soc/dport_reg.h"
  16. #include "soc/io_mux_reg.h"
  17. #include "soc/gpio_sig_map.h"
  18. #include "soc/gpio_reg.h"
  19. #include "soc/i2s_reg.h"
  20. #define DPORT_I2S0_CLK_EN (BIT(4))
  21. #define DPORT_I2S0_RST (BIT(4))
  22. static volatile lldesc_t dmaDesc[2];
  23. //hacked up routine to essentially do a memcpy() using dma. Supports max 4K-1 bytes.
  24. static void dmaMemcpy(void *in, void *out, int len)
  25. {
  26. volatile int i;
  27. SET_PERI_REG_MASK(DPORT_PERIP_CLK_EN_REG, DPORT_I2S0_CLK_EN);
  28. CLEAR_PERI_REG_MASK(DPORT_PERIP_RST_EN_REG, DPORT_I2S0_RST);
  29. //Init pins to i2s functions
  30. SET_PERI_REG_MASK(GPIO_ENABLE_W1TS_REG, (1 << 11) | (1 << 3) | (1 << 0) | (1 << 2) | (1 << 5) | (1 << 16) | (1 << 17) | (1 << 18) | (1 << 19) | (1 << 20)); //ENABLE GPIO oe_enable
  31. PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO0_U, 0);
  32. PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U, 0);
  33. PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO5_U, 0);
  34. PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO16_U, 0);
  35. PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO17_U, 0);
  36. PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO18_U, 0);
  37. PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO19_U, 0);
  38. PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO20_U, 0);
  39. PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_CMD_U, 2); //11
  40. PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO26_U, 0); //RS
  41. WRITE_PERI_REG(GPIO_FUNC0_OUT_SEL_CFG_REG, (148 << GPIO_FUNC0_OUT_SEL_S));
  42. WRITE_PERI_REG(GPIO_FUNC2_OUT_SEL_CFG_REG, (149 << GPIO_FUNC0_OUT_SEL_S));
  43. WRITE_PERI_REG(GPIO_FUNC5_OUT_SEL_CFG_REG, (150 << GPIO_FUNC0_OUT_SEL_S));
  44. WRITE_PERI_REG(GPIO_FUNC16_OUT_SEL_CFG_REG, (151 << GPIO_FUNC0_OUT_SEL_S));
  45. WRITE_PERI_REG(GPIO_FUNC17_OUT_SEL_CFG_REG, (152 << GPIO_FUNC0_OUT_SEL_S));
  46. WRITE_PERI_REG(GPIO_FUNC18_OUT_SEL_CFG_REG, (153 << GPIO_FUNC0_OUT_SEL_S));
  47. WRITE_PERI_REG(GPIO_FUNC19_OUT_SEL_CFG_REG, (154 << GPIO_FUNC0_OUT_SEL_S));
  48. WRITE_PERI_REG(GPIO_FUNC20_OUT_SEL_CFG_REG, (155 << GPIO_FUNC0_OUT_SEL_S));
  49. WRITE_PERI_REG(GPIO_FUNC26_OUT_SEL_CFG_REG, (156 << GPIO_FUNC0_OUT_SEL_S)); //RS
  50. WRITE_PERI_REG(GPIO_FUNC11_OUT_SEL_CFG_REG, (I2S0O_WS_OUT_IDX << GPIO_FUNC0_OUT_SEL_S));
  51. // WRITE_PERI_REG(GPIO_FUNC11_OUT_SEL_CFG, (I2S0O_BCK_OUT_IDX<<GPIO_GPIO_FUNC0_OUT_SEL_S));
  52. //GPIO_SET_GPIO_FUNC11_OUT_INV_SEL(1); //old
  53. WRITE_PERI_REG(GPIO_FUNC11_OUT_SEL_CFG_REG, READ_PERI_REG(GPIO_FUNC11_OUT_SEL_CFG_REG) | GPIO_FUNC11_OUT_INV_SEL);
  54. //Reset I2S subsystem
  55. CLEAR_PERI_REG_MASK(I2S_CONF_REG(0), I2S_RX_RESET | I2S_TX_RESET);
  56. SET_PERI_REG_MASK(I2S_CONF_REG(0), I2S_RX_RESET | I2S_TX_RESET);
  57. CLEAR_PERI_REG_MASK(I2S_CONF_REG(0), I2S_RX_RESET | I2S_TX_RESET);
  58. WRITE_PERI_REG(I2S_CONF_REG(0), 0);//I2S_I2S_SIG_LOOPBACK);
  59. WRITE_PERI_REG(I2S_CONF2_REG(0), 0);
  60. WRITE_PERI_REG(I2S_SAMPLE_RATE_CONF_REG(0),
  61. (16 << I2S_RX_BITS_MOD_S) |
  62. (16 << I2S_TX_BITS_MOD_S) |
  63. (1 << I2S_RX_BCK_DIV_NUM_S) |
  64. (1 << I2S_TX_BCK_DIV_NUM_S));
  65. WRITE_PERI_REG(I2S_CLKM_CONF_REG(0),
  66. I2S_CLKA_ENA | I2S_CLK_EN |
  67. (1 << I2S_CLKM_DIV_A_S) |
  68. (1 << I2S_CLKM_DIV_B_S) |
  69. (1 << I2S_CLKM_DIV_NUM_S));
  70. WRITE_PERI_REG(I2S_FIFO_CONF_REG(0),
  71. (32 << I2S_TX_DATA_NUM_S) | //Low watermark for IRQ
  72. (32 << I2S_RX_DATA_NUM_S));
  73. WRITE_PERI_REG(I2S_CONF1_REG(0), I2S_RX_PCM_BYPASS | I2S_TX_PCM_BYPASS);
  74. WRITE_PERI_REG(I2S_CONF_CHAN_REG(0), (2 << I2S_TX_CHAN_MOD_S) | (2 << I2S_RX_CHAN_MOD_S));
  75. //Invert WS to active-low
  76. SET_PERI_REG_MASK(I2S_CONF_REG(0), I2S_TX_RIGHT_FIRST | I2S_RX_RIGHT_FIRST);
  77. WRITE_PERI_REG(I2S_TIMING_REG(0), 0);
  78. //--
  79. //Fill DMA descriptor
  80. dmaDesc[0].length = len;
  81. dmaDesc[0].size = len;
  82. dmaDesc[0].owner = 1;
  83. dmaDesc[0].sosf = 0;
  84. dmaDesc[0].buf = (uint8_t *)in;
  85. dmaDesc[0].offset = 0; //unused in hw
  86. dmaDesc[0].empty = 0;
  87. dmaDesc[0].eof = 1;
  88. dmaDesc[1].length = len;
  89. dmaDesc[1].size = len;
  90. dmaDesc[1].owner = 1;
  91. dmaDesc[1].sosf = 0;
  92. dmaDesc[1].buf = (uint8_t *)out;
  93. dmaDesc[1].offset = 0; //unused in hw
  94. dmaDesc[1].empty = 0;
  95. dmaDesc[1].eof = 1;
  96. //Reset DMA
  97. SET_PERI_REG_MASK(I2S_LC_CONF_REG(0), I2S_IN_RST | I2S_OUT_RST | I2S_AHBM_RST | I2S_AHBM_FIFO_RST);
  98. CLEAR_PERI_REG_MASK(I2S_LC_CONF_REG(0), I2S_IN_RST | I2S_OUT_RST | I2S_AHBM_RST | I2S_AHBM_FIFO_RST);
  99. //Reset I2S FIFO
  100. SET_PERI_REG_MASK(I2S_CONF_REG(0), I2S_RX_RESET | I2S_TX_RESET | I2S_TX_FIFO_RESET | I2S_RX_FIFO_RESET);
  101. CLEAR_PERI_REG_MASK(I2S_CONF_REG(0), I2S_RX_RESET | I2S_TX_RESET | I2S_TX_FIFO_RESET | I2S_RX_FIFO_RESET);
  102. //Set desc addr
  103. CLEAR_PERI_REG_MASK(I2S_OUT_LINK_REG(0), I2S_OUTLINK_ADDR);
  104. SET_PERI_REG_MASK(I2S_OUT_LINK_REG(0), ((uint32_t)(&dmaDesc[0]))&I2S_OUTLINK_ADDR);
  105. CLEAR_PERI_REG_MASK(I2S_IN_LINK_REG(0), I2S_INLINK_ADDR);
  106. SET_PERI_REG_MASK(I2S_IN_LINK_REG(0), ((uint32_t)(&dmaDesc[1]))&I2S_INLINK_ADDR);
  107. SET_PERI_REG_MASK(I2S_FIFO_CONF_REG(0), I2S_DSCR_EN); //Enable DMA mode
  108. WRITE_PERI_REG(I2S_RXEOF_NUM_REG(0), len);
  109. //Enable and configure DMA
  110. WRITE_PERI_REG(I2S_LC_CONF_REG(0), I2S_OUT_DATA_BURST_EN |
  111. I2S_OUT_EOF_MODE | I2S_OUTDSCR_BURST_EN | I2S_OUT_DATA_BURST_EN |
  112. I2S_INDSCR_BURST_EN | I2S_MEM_TRANS_EN);
  113. //Start transmission
  114. SET_PERI_REG_MASK(I2S_OUT_LINK_REG(0), I2S_OUTLINK_START);
  115. SET_PERI_REG_MASK(I2S_IN_LINK_REG(0), I2S_INLINK_START);
  116. SET_PERI_REG_MASK(I2S_CONF_REG(0), I2S_TX_START | I2S_RX_START);
  117. //Clear int flags
  118. WRITE_PERI_REG(I2S_INT_CLR_REG(0), 0xFFFFFFFF);
  119. //--
  120. //No need to finish if no DMA transfer going on
  121. if (!(READ_PERI_REG(I2S_FIFO_CONF_REG(0))&I2S_DSCR_EN)) {
  122. return;
  123. }
  124. //Wait till fifo done
  125. while (!(READ_PERI_REG(I2S_INT_RAW_REG(0))&I2S_TX_REMPTY_INT_RAW)) ;
  126. //Wait for last bytes to leave i2s xmit thing
  127. //ToDo: poll bit in next hw
  128. for (i = 0; i < (1 << 8); i++);
  129. while (!(READ_PERI_REG(I2S_STATE_REG(0))&I2S_TX_IDLE));
  130. //Reset I2S for next transfer
  131. CLEAR_PERI_REG_MASK(I2S_CONF_REG(0), I2S_TX_START | I2S_RX_START);
  132. CLEAR_PERI_REG_MASK(I2S_OUT_LINK_REG(0), I2S_OUTLINK_START | I2S_INLINK_START);
  133. SET_PERI_REG_MASK(I2S_CONF_REG(0), I2S_TX_RESET | I2S_TX_FIFO_RESET | I2S_RX_RESET | I2S_RX_FIFO_RESET);
  134. CLEAR_PERI_REG_MASK(I2S_CONF_REG(0), I2S_TX_RESET | I2S_TX_FIFO_RESET | I2S_RX_RESET | I2S_RX_FIFO_RESET);
  135. // for (i=0; i<(1<<8); i++);
  136. while ((READ_PERI_REG(I2S_STATE_REG(0))&I2S_TX_FIFO_RESET_BACK));
  137. }
  138. int mymemcmp(char *a, char *b, int len)
  139. {
  140. int x;
  141. for (x = 0; x < len; x++) {
  142. if (a[x] != b[x]) {
  143. printf("Not equal at byte %d. a=%x, b=%x\n", x, (int)a[x], (int)b[x]);
  144. return 1;
  145. }
  146. }
  147. return 0;
  148. }
  149. TEST_CASE("Unaligned DMA test (needs I2S)", "[hw][ignore]")
  150. {
  151. int x;
  152. char src[2049], dest[2049];
  153. for (x = 0; x < sizeof(src); x++) {
  154. src[x] = x & 0xff;
  155. }
  156. printf("Aligned dma\n");
  157. memset(dest, 0, 2049);
  158. dmaMemcpy(src, dest, 2048 + 1);
  159. TEST_ASSERT(mymemcmp(src, dest, 2048) == 0);
  160. printf("Src unaligned\n");
  161. dmaMemcpy(src + 1, dest, 2048 + 1);
  162. TEST_ASSERT(mymemcmp(src + 1, dest, 2048) == 0);
  163. printf("Dst unaligned\n");
  164. dmaMemcpy(src, dest + 1, 2048 + 2);
  165. TEST_ASSERT(mymemcmp(src, dest + 1, 2048) == 0);
  166. }