test_i2s.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /**
  2. * I2S test environment UT_T1_I2S:
  3. * connect GPIO18 and GPIO19, GPIO25 and GPIO26, GPIO21 and GPIO22
  4. */
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include "freertos/FreeRTOS.h"
  8. #include "freertos/task.h"
  9. #include "driver/i2s.h"
  10. #include "unity.h"
  11. #define SAMPLE_RATE (36000)
  12. #define SAMPLE_BITS (16)
  13. #define MASTER_BCK_IO 18
  14. #define MASTER_WS_IO 25
  15. #define SLAVE_BCK_IO 19
  16. #define SLAVE_WS_IO 26
  17. #define DATA_IN_IO 21
  18. #define DATA_OUT_IO 22
  19. /**
  20. * i2s initialize test
  21. * 1. i2s_driver_install
  22. * 2. i2s_set_pin
  23. */
  24. TEST_CASE("I2S basic driver install, uninstall, set pin test", "[i2s]")
  25. {
  26. // dac, adc i2s
  27. i2s_config_t i2s_config = {
  28. .mode = I2S_MODE_MASTER | I2S_MODE_TX,
  29. .sample_rate = SAMPLE_RATE,
  30. .bits_per_sample = SAMPLE_BITS,
  31. .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
  32. .communication_format = I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB,
  33. .dma_buf_count = 6,
  34. .dma_buf_len = 60,
  35. .use_apll = 0,
  36. .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1 ,
  37. };
  38. //install and start i2s driver
  39. TEST_ESP_OK(i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL));
  40. //for internal DAC, this will enable both of the internal channels
  41. TEST_ESP_OK(i2s_set_pin(I2S_NUM_0, NULL));
  42. //stop & destroy i2s driver
  43. TEST_ESP_OK(i2s_driver_uninstall(I2S_NUM_0));
  44. // normal i2s
  45. i2s_pin_config_t pin_config = {
  46. .bck_io_num = MASTER_BCK_IO,
  47. .ws_io_num = MASTER_WS_IO,
  48. .data_out_num = DATA_OUT_IO,
  49. .data_in_num = -1
  50. };
  51. TEST_ESP_OK(i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL));
  52. TEST_ESP_OK(i2s_set_pin(I2S_NUM_0, &pin_config));
  53. TEST_ESP_OK(i2s_driver_uninstall(I2S_NUM_0));
  54. //error param test
  55. TEST_ASSERT(i2s_driver_install(I2S_NUM_MAX, &i2s_config, 0, NULL) == ESP_ERR_INVALID_ARG);
  56. TEST_ASSERT(i2s_driver_install(I2S_NUM_0, NULL, 0, NULL) == ESP_ERR_INVALID_ARG);
  57. i2s_config.dma_buf_count = 1;
  58. TEST_ASSERT(i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL) == ESP_ERR_INVALID_ARG);
  59. i2s_config.dma_buf_count = 129;
  60. TEST_ASSERT(i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL) == ESP_ERR_INVALID_ARG);
  61. TEST_ESP_OK(i2s_driver_uninstall(I2S_NUM_0));
  62. }
  63. TEST_CASE("I2S write and read test(master tx and slave rx)", "[i2s][test_env=UT_T1_I2S]")
  64. {
  65. // master driver installed and send data
  66. i2s_config_t master_i2s_config = {
  67. .mode = I2S_MODE_MASTER | I2S_MODE_TX,
  68. .sample_rate = SAMPLE_RATE,
  69. .bits_per_sample = SAMPLE_BITS,
  70. .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
  71. .communication_format = I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB,
  72. .dma_buf_count = 6,
  73. .dma_buf_len = 100,
  74. .use_apll = 0,
  75. .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1 ,
  76. };
  77. i2s_pin_config_t master_pin_config = {
  78. .bck_io_num = MASTER_BCK_IO,
  79. .ws_io_num = MASTER_WS_IO,
  80. .data_out_num = DATA_OUT_IO,
  81. .data_in_num = -1
  82. };
  83. TEST_ESP_OK(i2s_driver_install(I2S_NUM_0, &master_i2s_config, 0, NULL));
  84. TEST_ESP_OK(i2s_set_pin(I2S_NUM_0, &master_pin_config));
  85. printf("\r\nheap size: %d\n", esp_get_free_heap_size());
  86. i2s_config_t slave_i2s_config = {
  87. .mode = I2S_MODE_SLAVE | I2S_MODE_RX,
  88. .sample_rate = SAMPLE_RATE,
  89. .bits_per_sample = SAMPLE_BITS,
  90. .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
  91. .communication_format = I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB,
  92. .dma_buf_count = 6,
  93. .dma_buf_len = 100,
  94. .use_apll = 0,
  95. .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1 ,
  96. };
  97. i2s_pin_config_t slave_pin_config = {
  98. .bck_io_num = SLAVE_BCK_IO,
  99. .ws_io_num = SLAVE_WS_IO,
  100. .data_out_num = -1,
  101. .data_in_num = DATA_IN_IO,
  102. };
  103. // slave driver installed and receive data
  104. TEST_ESP_OK(i2s_driver_install(I2S_NUM_1, &slave_i2s_config, 0, NULL));
  105. TEST_ESP_OK(i2s_set_pin(I2S_NUM_1, &slave_pin_config));
  106. printf("\r\nheap size: %d\n", esp_get_free_heap_size());
  107. uint8_t* data_wr = (uint8_t*)malloc(sizeof(uint8_t)*400);
  108. size_t i2s_bytes_write = 0;
  109. size_t bytes_read = 0;
  110. int length = 0;
  111. uint8_t *i2s_read_buff = (uint8_t*)malloc(sizeof(uint8_t)*10000);
  112. for(int i=0; i<100; i++) {
  113. data_wr[i] = i+1;
  114. }
  115. int flag=0; // break loop flag
  116. int end_position = 0;
  117. // write data to slave
  118. i2s_write(I2S_NUM_0, data_wr, sizeof(uint8_t)*400, &i2s_bytes_write, 1000 / portTICK_PERIOD_MS);
  119. while(!flag){
  120. i2s_read(I2S_NUM_1, i2s_read_buff + length, sizeof(uint8_t)*500, &bytes_read, 1000/portMAX_DELAY);
  121. if(bytes_read>0) {
  122. printf("read data size: %d\n", bytes_read);
  123. for(int i=length; i<length + bytes_read; i++) {
  124. if(i2s_read_buff[i] == 100) {
  125. flag=1;
  126. end_position = i;
  127. break;
  128. }
  129. }
  130. }
  131. length = length + bytes_read;
  132. }
  133. // test the readed data right or not
  134. for(int i=end_position-99; i<=end_position; i++) {
  135. TEST_ASSERT(*(i2s_read_buff + i) == (i-end_position+100));
  136. }
  137. free(data_wr);
  138. free(i2s_read_buff);
  139. i2s_driver_uninstall(I2S_NUM_0);
  140. i2s_driver_uninstall(I2S_NUM_1);
  141. }
  142. TEST_CASE("I2S write and read test(master rx and slave tx)", "[i2s][test_env=UT_T1_I2S]")
  143. {
  144. // master driver installed and send data
  145. i2s_config_t master_i2s_config = {
  146. .mode = I2S_MODE_MASTER | I2S_MODE_RX,
  147. .sample_rate = SAMPLE_RATE,
  148. .bits_per_sample = SAMPLE_BITS,
  149. .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
  150. .communication_format = I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB,
  151. .dma_buf_count = 6,
  152. .dma_buf_len = 100,
  153. .use_apll = 0,
  154. .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1 ,
  155. };
  156. i2s_pin_config_t master_pin_config = {
  157. .bck_io_num = MASTER_BCK_IO,
  158. .ws_io_num = MASTER_WS_IO,
  159. .data_out_num = -1,
  160. .data_in_num = DATA_IN_IO,
  161. };
  162. TEST_ESP_OK(i2s_driver_install(I2S_NUM_0, &master_i2s_config, 0, NULL));
  163. TEST_ESP_OK(i2s_set_pin(I2S_NUM_0, &master_pin_config));
  164. printf("\r\nheap size: %d\n", esp_get_free_heap_size());
  165. i2s_config_t slave_i2s_config = {
  166. .mode = I2S_MODE_SLAVE | I2S_MODE_TX, // Only RX
  167. .sample_rate = SAMPLE_RATE,
  168. .bits_per_sample = SAMPLE_BITS,
  169. .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT, //2-channels
  170. .communication_format = I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB,
  171. .dma_buf_count = 6,
  172. .dma_buf_len = 100,
  173. .use_apll = 0,
  174. .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1 ,
  175. };
  176. i2s_pin_config_t slave_pin_config = {
  177. .bck_io_num = SLAVE_BCK_IO,
  178. .ws_io_num = SLAVE_WS_IO,
  179. .data_out_num = DATA_OUT_IO,
  180. .data_in_num = -1
  181. };
  182. // slave driver installed and receive data
  183. TEST_ESP_OK(i2s_driver_install(I2S_NUM_1, &slave_i2s_config, 0, NULL));
  184. TEST_ESP_OK(i2s_set_pin(I2S_NUM_1, &slave_pin_config));
  185. uint8_t* data_wr = (uint8_t*)malloc(sizeof(uint8_t)*400);
  186. size_t i2s_bytes_write = 0;
  187. size_t bytes_read = 0;
  188. int length = 0;
  189. uint8_t *i2s_read_buff = (uint8_t*)malloc(sizeof(uint8_t)*100000);
  190. for(int i=0; i<100; i++) {
  191. data_wr[i] = i+1;
  192. }
  193. // slave write data to master
  194. i2s_write(I2S_NUM_1, data_wr, sizeof(uint8_t)*400, &i2s_bytes_write, 1000 / portTICK_PERIOD_MS);
  195. int flag=0; // break loop flag
  196. int end_position = 0;
  197. // write data to slave
  198. while(!flag){
  199. TEST_ESP_OK(i2s_read(I2S_NUM_0, i2s_read_buff + length, 10000-length, &bytes_read, 1000/portMAX_DELAY));
  200. if(bytes_read > 0) {
  201. for(int i=length; i<length+bytes_read; i++) {
  202. if(i2s_read_buff[i] == 100) {
  203. flag=1;
  204. end_position = i;
  205. break;
  206. }
  207. }
  208. }
  209. length = length + bytes_read;
  210. }
  211. // test the readed data right or not
  212. for(int i=end_position-99; i<=end_position; i++) {
  213. TEST_ASSERT(*(i2s_read_buff + i) == (i-end_position+100));
  214. }
  215. free(data_wr);
  216. free(i2s_read_buff);
  217. i2s_driver_uninstall(I2S_NUM_0);
  218. i2s_driver_uninstall(I2S_NUM_1);
  219. }
  220. TEST_CASE("I2S memory leaking test", "[i2s]")
  221. {
  222. i2s_config_t master_i2s_config = {
  223. .mode = I2S_MODE_MASTER | I2S_MODE_RX,
  224. .sample_rate = SAMPLE_RATE,
  225. .bits_per_sample = SAMPLE_BITS,
  226. .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
  227. .communication_format = I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB,
  228. .dma_buf_count = 6,
  229. .dma_buf_len = 100,
  230. .use_apll = 0,
  231. .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1 ,
  232. };
  233. i2s_pin_config_t master_pin_config = {
  234. .bck_io_num = MASTER_BCK_IO,
  235. .ws_io_num = MASTER_WS_IO,
  236. .data_out_num = -1,
  237. .data_in_num = DATA_IN_IO
  238. };
  239. TEST_ESP_OK(i2s_driver_install(I2S_NUM_0, &master_i2s_config, 0, NULL));
  240. TEST_ESP_OK(i2s_set_pin(I2S_NUM_0, &master_pin_config));
  241. i2s_driver_uninstall(I2S_NUM_0);
  242. int initial_size = esp_get_free_heap_size();
  243. for(int i=0; i<100; i++) {
  244. TEST_ESP_OK(i2s_driver_install(I2S_NUM_0, &master_i2s_config, 0, NULL));
  245. TEST_ESP_OK(i2s_set_pin(I2S_NUM_0, &master_pin_config));
  246. i2s_driver_uninstall(I2S_NUM_0);
  247. TEST_ASSERT(initial_size == esp_get_free_heap_size());
  248. }
  249. vTaskDelay(100 / portTICK_PERIOD_MS);
  250. TEST_ASSERT(initial_size == esp_get_free_heap_size());
  251. }