test_unal_dma.c 7.4 KB

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