test_i2s.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. /**
  2. * I2S test environment UT_T1_I2S:
  3. * We use internal signals instead of external wiring, but please keep the following IO connections, or connect nothing to prevent the signal from being disturbed.
  4. * connect GPIO15 and GPIO19, GPIO25(ESP32)/GPIO17(ESP32-S2) and GPIO26, GPIO21 and GPIO22(ESP32)/GPIO20(ESP32-S2)
  5. * Please do not connect GPIO32(ESP32) any pull-up resistors externally, it will be used to test i2s adc function.
  6. */
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include "freertos/FreeRTOS.h"
  10. #include "freertos/task.h"
  11. #include "driver/i2s.h"
  12. #include "driver/gpio.h"
  13. #include "unity.h"
  14. #include "math.h"
  15. #define SAMPLE_RATE (36000)
  16. #define SAMPLE_BITS (16)
  17. #define MASTER_BCK_IO 15
  18. #define SLAVE_BCK_IO 19
  19. #define SLAVE_WS_IO 26
  20. #define DATA_IN_IO 21
  21. #if CONFIG_IDF_TARGET_ESP32
  22. #define MASTER_WS_IO 25
  23. #define DATA_OUT_IO 22
  24. #define ADC1_CHANNEL_4_IO 32
  25. #elif CONFIG_IDF_TARGET_ESP32S2
  26. #define MASTER_WS_IO 28
  27. #define DATA_OUT_IO 20
  28. #endif
  29. #define PERCENT_DIFF 0.0001
  30. #define I2S_TEST_MODE_SLAVE_TO_MAXTER 0
  31. #define I2S_TEST_MODE_MASTER_TO_SLAVE 1
  32. #define I2S_TEST_MODE_LOOPBACK 2
  33. // mode: 0, master rx, slave tx. mode: 1, master tx, slave rx. mode: 2, master tx rx loopback
  34. // Since ESP32-S2 has only one I2S, only loop back test can be tested.
  35. static void i2s_test_io_config(int mode)
  36. {
  37. // Connect internal signals using IO matrix.
  38. PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[MASTER_BCK_IO], PIN_FUNC_GPIO);
  39. PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[MASTER_WS_IO], PIN_FUNC_GPIO);
  40. PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[DATA_OUT_IO], PIN_FUNC_GPIO);
  41. gpio_set_direction(MASTER_BCK_IO, GPIO_MODE_INPUT_OUTPUT);
  42. gpio_set_direction(MASTER_WS_IO, GPIO_MODE_INPUT_OUTPUT);
  43. gpio_set_direction(DATA_OUT_IO, GPIO_MODE_INPUT_OUTPUT);
  44. switch (mode) {
  45. #if SOC_I2S_NUM > 1
  46. case I2S_TEST_MODE_SLAVE_TO_MAXTER: {
  47. gpio_matrix_out(MASTER_BCK_IO, I2S0I_BCK_OUT_IDX, 0, 0);
  48. gpio_matrix_in(MASTER_BCK_IO, I2S1O_BCK_IN_IDX, 0);
  49. gpio_matrix_out(MASTER_WS_IO, I2S0I_WS_OUT_IDX, 0, 0);
  50. gpio_matrix_in(MASTER_WS_IO, I2S1O_WS_IN_IDX, 0);
  51. gpio_matrix_out(DATA_OUT_IO, I2S1O_DATA_OUT23_IDX, 0, 0);
  52. gpio_matrix_in(DATA_OUT_IO, I2S0I_DATA_IN15_IDX, 0);
  53. }
  54. break;
  55. case I2S_TEST_MODE_MASTER_TO_SLAVE: {
  56. gpio_matrix_out(MASTER_BCK_IO, I2S0O_BCK_OUT_IDX, 0, 0);
  57. gpio_matrix_in(MASTER_BCK_IO, I2S1I_BCK_IN_IDX, 0);
  58. gpio_matrix_out(MASTER_WS_IO, I2S0O_WS_OUT_IDX, 0, 0);
  59. gpio_matrix_in(MASTER_WS_IO, I2S1I_WS_IN_IDX, 0);
  60. gpio_matrix_out(DATA_OUT_IO, I2S0O_DATA_OUT23_IDX, 0, 0);
  61. gpio_matrix_in(DATA_OUT_IO, I2S1I_DATA_IN15_IDX, 0);
  62. }
  63. break;
  64. #endif
  65. case I2S_TEST_MODE_LOOPBACK: {
  66. gpio_matrix_out(DATA_OUT_IO, I2S0O_DATA_OUT23_IDX, 0, 0);
  67. gpio_matrix_in(DATA_OUT_IO, I2S0I_DATA_IN15_IDX, 0);
  68. }
  69. break;
  70. default: {
  71. TEST_FAIL_MESSAGE("error: mode not supported");
  72. }
  73. break;
  74. }
  75. }
  76. /**
  77. * i2s initialize test
  78. * 1. i2s_driver_install
  79. * 2. i2s_set_pin
  80. */
  81. TEST_CASE("I2S basic driver install, uninstall, set pin test", "[i2s]")
  82. {
  83. // dac, adc i2s
  84. i2s_config_t i2s_config = {
  85. .mode = I2S_MODE_MASTER | I2S_MODE_TX,
  86. .sample_rate = SAMPLE_RATE,
  87. .bits_per_sample = SAMPLE_BITS,
  88. .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
  89. .communication_format = I2S_COMM_FORMAT_STAND_I2S,
  90. .dma_buf_count = 6,
  91. .dma_buf_len = 60,
  92. .use_apll = 0,
  93. .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1 ,
  94. };
  95. #if CONFIG_IDF_TARGET_ESP32
  96. //install and start i2s driver
  97. TEST_ESP_OK(i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL));
  98. //for internal DAC, this will enable both of the internal channels
  99. TEST_ESP_OK(i2s_set_pin(I2S_NUM_0, NULL));
  100. //stop & destroy i2s driver
  101. TEST_ESP_OK(i2s_driver_uninstall(I2S_NUM_0));
  102. #endif
  103. // normal i2s
  104. i2s_pin_config_t pin_config = {
  105. .bck_io_num = MASTER_BCK_IO,
  106. .ws_io_num = MASTER_WS_IO,
  107. .data_out_num = DATA_OUT_IO,
  108. .data_in_num = -1
  109. };
  110. TEST_ESP_OK(i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL));
  111. TEST_ESP_OK(i2s_set_pin(I2S_NUM_0, &pin_config));
  112. TEST_ESP_OK(i2s_driver_uninstall(I2S_NUM_0));
  113. //error param test
  114. TEST_ASSERT(i2s_driver_install(I2S_NUM_MAX, &i2s_config, 0, NULL) == ESP_ERR_INVALID_ARG);
  115. TEST_ASSERT(i2s_driver_install(I2S_NUM_0, NULL, 0, NULL) == ESP_ERR_INVALID_ARG);
  116. i2s_config.dma_buf_count = 1;
  117. TEST_ASSERT(i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL) == ESP_ERR_INVALID_ARG);
  118. i2s_config.dma_buf_count = 129;
  119. TEST_ASSERT(i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL) == ESP_ERR_INVALID_ARG);
  120. TEST_ESP_OK(i2s_driver_uninstall(I2S_NUM_0));
  121. }
  122. TEST_CASE("I2S Loopback test(master tx and rx)", "[i2s]")
  123. {
  124. // master driver installed and send data
  125. i2s_config_t master_i2s_config = {
  126. .mode = I2S_MODE_MASTER | I2S_MODE_TX | I2S_MODE_RX,
  127. .sample_rate = SAMPLE_RATE,
  128. .bits_per_sample = SAMPLE_BITS,
  129. .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
  130. .communication_format = I2S_COMM_FORMAT_STAND_I2S,
  131. .dma_buf_count = 6,
  132. .dma_buf_len = 100,
  133. .use_apll = 0,
  134. .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1 ,
  135. };
  136. i2s_pin_config_t master_pin_config = {
  137. .bck_io_num = MASTER_BCK_IO,
  138. .ws_io_num = MASTER_WS_IO,
  139. .data_out_num = DATA_OUT_IO,
  140. .data_in_num = DATA_IN_IO
  141. };
  142. TEST_ESP_OK(i2s_driver_install(I2S_NUM_0, &master_i2s_config, 0, NULL));
  143. TEST_ESP_OK(i2s_set_pin(I2S_NUM_0, &master_pin_config));
  144. i2s_test_io_config(I2S_TEST_MODE_LOOPBACK);
  145. printf("\r\nheap size: %d\n", esp_get_free_heap_size());
  146. uint8_t* data_wr = (uint8_t*)malloc(sizeof(uint8_t)*400);
  147. size_t i2s_bytes_write = 0;
  148. size_t bytes_read = 0;
  149. int length = 0;
  150. uint8_t *i2s_read_buff = (uint8_t*)malloc(sizeof(uint8_t)*10000);
  151. for(int i=0; i<100; i++) {
  152. data_wr[i] = i+1;
  153. }
  154. int flag=0; // break loop flag
  155. int end_position = 0;
  156. // write data to slave
  157. i2s_write(I2S_NUM_0, data_wr, sizeof(uint8_t)*400, &i2s_bytes_write, 1000 / portTICK_PERIOD_MS);
  158. while(!flag){
  159. if (length >= 10000 - 500) {
  160. break;
  161. }
  162. i2s_read(I2S_NUM_0, i2s_read_buff + length, sizeof(uint8_t)*500, &bytes_read, 1000/portMAX_DELAY);
  163. if(bytes_read>0) {
  164. printf("read data size: %d\n", bytes_read);
  165. for(int i=length; i<length + bytes_read; i++) {
  166. if(i2s_read_buff[i] == 100) {
  167. flag=1;
  168. end_position = i;
  169. break;
  170. }
  171. }
  172. }
  173. length = length + bytes_read;
  174. }
  175. // test the read data right or not
  176. for(int i=end_position-99; i<=end_position; i++) {
  177. TEST_ASSERT_EQUAL_UINT8((i-end_position+100), *(i2s_read_buff + i));
  178. }
  179. free(data_wr);
  180. free(i2s_read_buff);
  181. i2s_driver_uninstall(I2S_NUM_0);
  182. }
  183. #if !DISABLED_FOR_TARGETS(ESP32S2)
  184. /* ESP32S2 has only single I2S port and hence following test cases are not applicable */
  185. TEST_CASE("I2S adc test", "[i2s]")
  186. {
  187. // init I2S ADC
  188. i2s_config_t i2s_config = {
  189. .mode = I2S_MODE_MASTER | I2S_MODE_RX | I2S_MODE_ADC_BUILT_IN,
  190. .sample_rate = SAMPLE_RATE,
  191. .bits_per_sample = SAMPLE_BITS,
  192. .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
  193. .intr_alloc_flags = 0,
  194. .dma_buf_count = 2,
  195. .dma_buf_len = 1024,
  196. .use_apll = 0,
  197. };
  198. // install and start I2S driver
  199. i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL);
  200. // init ADC pad
  201. i2s_set_adc_mode(ADC_UNIT_1, ADC1_CHANNEL_4);
  202. // enable adc sampling, ADC_WIDTH_BIT_12, ADC_ATTEN_DB_11 hard-coded in adc_i2s_mode_init
  203. i2s_adc_enable(I2S_NUM_0);
  204. // init read buffer
  205. uint16_t* i2sReadBuffer = (uint16_t*)calloc(1024, sizeof(uint16_t));
  206. size_t bytesRead;
  207. for (int loop = 0; loop < 10; loop++) {
  208. for (int level = 0; level <= 1; level++) {
  209. if (level == 0) {
  210. gpio_set_pull_mode(ADC1_CHANNEL_4_IO, GPIO_PULLDOWN_ONLY);
  211. } else {
  212. gpio_set_pull_mode(ADC1_CHANNEL_4_IO, GPIO_PULLUP_ONLY);
  213. }
  214. vTaskDelay(200 / portTICK_RATE_MS);
  215. // read data from adc, will block until buffer is full
  216. i2s_read(I2S_NUM_0, (void*)i2sReadBuffer, 1024 * sizeof(uint16_t), &bytesRead, portMAX_DELAY);
  217. // calc average
  218. int64_t adcSumValue = 0;
  219. for (size_t i = 0; i < 1024; i++) {
  220. adcSumValue += i2sReadBuffer[i] & 0xfff;
  221. }
  222. int adcAvgValue = adcSumValue / 1024;
  223. printf("adc average val: %d\n", adcAvgValue);
  224. if (level == 0) {
  225. TEST_ASSERT_LESS_THAN(100, adcAvgValue);
  226. } else {
  227. TEST_ASSERT_GREATER_THAN(4000, adcAvgValue);
  228. }
  229. }
  230. }
  231. i2s_adc_disable(I2S_NUM_0);
  232. free(i2sReadBuffer);
  233. i2s_driver_uninstall(I2S_NUM_0);
  234. }
  235. TEST_CASE("I2S write and read test(master tx and slave rx)", "[i2s]")
  236. {
  237. // master driver installed and send data
  238. i2s_config_t master_i2s_config = {
  239. .mode = I2S_MODE_MASTER | I2S_MODE_TX,
  240. .sample_rate = SAMPLE_RATE,
  241. .bits_per_sample = SAMPLE_BITS,
  242. .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
  243. .communication_format = I2S_COMM_FORMAT_STAND_I2S,
  244. .dma_buf_count = 6,
  245. .dma_buf_len = 100,
  246. .use_apll = 0,
  247. .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1 ,
  248. };
  249. i2s_pin_config_t master_pin_config = {
  250. .bck_io_num = MASTER_BCK_IO,
  251. .ws_io_num = MASTER_WS_IO,
  252. .data_out_num = DATA_OUT_IO,
  253. .data_in_num = -1
  254. };
  255. TEST_ESP_OK(i2s_driver_install(I2S_NUM_0, &master_i2s_config, 0, NULL));
  256. TEST_ESP_OK(i2s_set_pin(I2S_NUM_0, &master_pin_config));
  257. i2s_test_io_config(I2S_TEST_MODE_MASTER_TO_SLAVE);
  258. printf("\r\nheap size: %d\n", esp_get_free_heap_size());
  259. i2s_config_t slave_i2s_config = {
  260. .mode = I2S_MODE_SLAVE | I2S_MODE_RX,
  261. .sample_rate = SAMPLE_RATE,
  262. .bits_per_sample = SAMPLE_BITS,
  263. .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
  264. .communication_format = I2S_COMM_FORMAT_STAND_I2S,
  265. .dma_buf_count = 6,
  266. .dma_buf_len = 100,
  267. .use_apll = 0,
  268. .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1 ,
  269. };
  270. i2s_pin_config_t slave_pin_config = {
  271. .bck_io_num = SLAVE_BCK_IO,
  272. .ws_io_num = SLAVE_WS_IO,
  273. .data_out_num = -1,
  274. .data_in_num = DATA_IN_IO,
  275. };
  276. // slave driver installed and receive data
  277. TEST_ESP_OK(i2s_driver_install(I2S_NUM_1, &slave_i2s_config, 0, NULL));
  278. TEST_ESP_OK(i2s_set_pin(I2S_NUM_1, &slave_pin_config));
  279. i2s_test_io_config(I2S_TEST_MODE_MASTER_TO_SLAVE);
  280. printf("\r\nheap size: %d\n", esp_get_free_heap_size());
  281. uint8_t* data_wr = (uint8_t*)malloc(sizeof(uint8_t)*400);
  282. size_t i2s_bytes_write = 0;
  283. size_t bytes_read = 0;
  284. int length = 0;
  285. uint8_t *i2s_read_buff = (uint8_t*)malloc(sizeof(uint8_t)*10000);
  286. for(int i=0; i<100; i++) {
  287. data_wr[i] = i+1;
  288. }
  289. int flag=0; // break loop flag
  290. int end_position = 0;
  291. // write data to slave
  292. i2s_write(I2S_NUM_0, data_wr, sizeof(uint8_t)*400, &i2s_bytes_write, 1000 / portTICK_PERIOD_MS);
  293. while(!flag){
  294. i2s_read(I2S_NUM_1, i2s_read_buff + length, sizeof(uint8_t)*500, &bytes_read, 1000/portMAX_DELAY);
  295. if(bytes_read>0) {
  296. printf("read data size: %d\n", bytes_read);
  297. for(int i=length; i<length + bytes_read; i++) {
  298. if(i2s_read_buff[i] == 100) {
  299. flag=1;
  300. end_position = i;
  301. break;
  302. }
  303. }
  304. }
  305. length = length + bytes_read;
  306. }
  307. // test the readed data right or not
  308. for(int i=end_position-99; i<=end_position; i++) {
  309. TEST_ASSERT_EQUAL_UINT8((i-end_position+100), *(i2s_read_buff + i));
  310. }
  311. free(data_wr);
  312. free(i2s_read_buff);
  313. i2s_driver_uninstall(I2S_NUM_0);
  314. i2s_driver_uninstall(I2S_NUM_1);
  315. }
  316. TEST_CASE("I2S write and read test(master rx and slave tx)", "[i2s]")
  317. {
  318. // master driver installed and send data
  319. i2s_config_t master_i2s_config = {
  320. .mode = I2S_MODE_MASTER | I2S_MODE_RX,
  321. .sample_rate = SAMPLE_RATE,
  322. .bits_per_sample = SAMPLE_BITS,
  323. .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
  324. .communication_format = I2S_COMM_FORMAT_STAND_I2S,
  325. .dma_buf_count = 6,
  326. .dma_buf_len = 100,
  327. .use_apll = 1,
  328. .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1 ,
  329. };
  330. i2s_pin_config_t master_pin_config = {
  331. .bck_io_num = MASTER_BCK_IO,
  332. .ws_io_num = MASTER_WS_IO,
  333. .data_out_num = -1,
  334. .data_in_num = DATA_IN_IO,
  335. };
  336. TEST_ESP_OK(i2s_driver_install(I2S_NUM_0, &master_i2s_config, 0, NULL));
  337. TEST_ESP_OK(i2s_set_pin(I2S_NUM_0, &master_pin_config));
  338. i2s_test_io_config(I2S_TEST_MODE_SLAVE_TO_MAXTER);
  339. printf("\r\nheap size: %d\n", esp_get_free_heap_size());
  340. i2s_config_t slave_i2s_config = {
  341. .mode = I2S_MODE_SLAVE | I2S_MODE_TX, // Only RX
  342. .sample_rate = SAMPLE_RATE,
  343. .bits_per_sample = SAMPLE_BITS,
  344. .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT, //2-channels
  345. .communication_format = I2S_COMM_FORMAT_STAND_I2S,
  346. .dma_buf_count = 6,
  347. .dma_buf_len = 100,
  348. .use_apll = 1,
  349. .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1 ,
  350. };
  351. i2s_pin_config_t slave_pin_config = {
  352. .bck_io_num = SLAVE_BCK_IO,
  353. .ws_io_num = SLAVE_WS_IO,
  354. .data_out_num = DATA_OUT_IO,
  355. .data_in_num = -1
  356. };
  357. // slave driver installed and receive data
  358. TEST_ESP_OK(i2s_driver_install(I2S_NUM_1, &slave_i2s_config, 0, NULL));
  359. TEST_ESP_OK(i2s_set_pin(I2S_NUM_1, &slave_pin_config));
  360. i2s_test_io_config(I2S_TEST_MODE_SLAVE_TO_MAXTER);
  361. uint8_t* data_wr = (uint8_t*)malloc(sizeof(uint8_t)*400);
  362. size_t i2s_bytes_write = 0;
  363. size_t bytes_read = 0;
  364. int length = 0;
  365. uint8_t *i2s_read_buff = (uint8_t*)malloc(sizeof(uint8_t)*100000);
  366. for(int i=0; i<100; i++) {
  367. data_wr[i] = i+1;
  368. }
  369. // slave write data to master
  370. i2s_write(I2S_NUM_1, data_wr, sizeof(uint8_t)*400, &i2s_bytes_write, 1000 / portTICK_PERIOD_MS);
  371. int flag=0; // break loop flag
  372. int end_position = 0;
  373. // write data to slave
  374. while(!flag){
  375. TEST_ESP_OK(i2s_read(I2S_NUM_0, i2s_read_buff + length, 10000-length, &bytes_read, 1000/portMAX_DELAY));
  376. if(bytes_read > 0) {
  377. for(int i=length; i<length+bytes_read; i++) {
  378. if(i2s_read_buff[i] == 100) {
  379. flag=1;
  380. end_position = i;
  381. break;
  382. }
  383. }
  384. }
  385. length = length + bytes_read;
  386. }
  387. // test the readed data right or not
  388. for(int i=end_position-99; i<=end_position; i++) {
  389. TEST_ASSERT_EQUAL_UINT8((i-end_position+100), *(i2s_read_buff + i));
  390. }
  391. free(data_wr);
  392. free(i2s_read_buff);
  393. i2s_driver_uninstall(I2S_NUM_0);
  394. i2s_driver_uninstall(I2S_NUM_1);
  395. }
  396. #endif
  397. TEST_CASE("I2S memory leaking test", "[i2s]")
  398. {
  399. i2s_config_t master_i2s_config = {
  400. .mode = I2S_MODE_MASTER | I2S_MODE_RX,
  401. .sample_rate = SAMPLE_RATE,
  402. .bits_per_sample = SAMPLE_BITS,
  403. .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
  404. .communication_format = I2S_COMM_FORMAT_STAND_I2S,
  405. .dma_buf_count = 6,
  406. .dma_buf_len = 100,
  407. .use_apll = 0,
  408. .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1 ,
  409. };
  410. i2s_pin_config_t master_pin_config = {
  411. .bck_io_num = MASTER_BCK_IO,
  412. .ws_io_num = MASTER_WS_IO,
  413. .data_out_num = -1,
  414. .data_in_num = DATA_IN_IO
  415. };
  416. TEST_ESP_OK(i2s_driver_install(I2S_NUM_0, &master_i2s_config, 0, NULL));
  417. TEST_ESP_OK(i2s_set_pin(I2S_NUM_0, &master_pin_config));
  418. i2s_driver_uninstall(I2S_NUM_0);
  419. int initial_size = esp_get_free_heap_size();
  420. for(int i=0; i<100; i++) {
  421. TEST_ESP_OK(i2s_driver_install(I2S_NUM_0, &master_i2s_config, 0, NULL));
  422. TEST_ESP_OK(i2s_set_pin(I2S_NUM_0, &master_pin_config));
  423. i2s_driver_uninstall(I2S_NUM_0);
  424. TEST_ASSERT(initial_size == esp_get_free_heap_size());
  425. }
  426. vTaskDelay(100 / portTICK_PERIOD_MS);
  427. TEST_ASSERT(initial_size == esp_get_free_heap_size());
  428. }
  429. /*
  430. * The I2S APLL clock variation test used to test the difference between the different sample rates, different bits per sample
  431. * and the APLL clock generate for it. The TEST_CASE passes PERCENT_DIFF variation from the provided sample rate in APLL generated clock
  432. * The percentage difference calculated as (mod((obtained clock rate - desired clock rate)/(desired clock rate))) * 100.
  433. */
  434. TEST_CASE("I2S APLL clock variation test", "[i2s]")
  435. {
  436. i2s_pin_config_t pin_config = {
  437. .bck_io_num = MASTER_BCK_IO,
  438. .ws_io_num = MASTER_WS_IO,
  439. .data_out_num = DATA_OUT_IO,
  440. .data_in_num = -1
  441. };
  442. i2s_config_t i2s_config = {
  443. .mode = I2S_MODE_MASTER | I2S_MODE_TX,
  444. .sample_rate = SAMPLE_RATE,
  445. .bits_per_sample = SAMPLE_BITS,
  446. .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
  447. .communication_format = I2S_COMM_FORMAT_STAND_I2S,
  448. .dma_buf_count = 6,
  449. .dma_buf_len = 60,
  450. .use_apll = true,
  451. .intr_alloc_flags = 0,
  452. };
  453. TEST_ESP_OK(i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL));
  454. TEST_ESP_OK(i2s_set_pin(I2S_NUM_0, &pin_config));
  455. TEST_ESP_OK(i2s_driver_uninstall(I2S_NUM_0));
  456. int initial_size = esp_get_free_heap_size();
  457. uint32_t sample_rate_arr[8] = { 10675, 11025, 16000, 22050, 32000, 44100, 48000, 96000 };
  458. int bits_per_sample_arr[3] = { 16, 24, 32 };
  459. for (int i = 0; i < (sizeof(sample_rate_arr)/sizeof(sample_rate_arr[0])); i++) {
  460. for (int j = 0; j < (sizeof(bits_per_sample_arr)/sizeof(bits_per_sample_arr[0])); j++) {
  461. i2s_config.sample_rate = sample_rate_arr[i];
  462. i2s_config.bits_per_sample = bits_per_sample_arr[j];
  463. TEST_ESP_OK(i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL));
  464. TEST_ESP_OK(i2s_set_pin(I2S_NUM_0, &pin_config));
  465. TEST_ASSERT((fabs((i2s_get_clk(I2S_NUM_0) - sample_rate_arr[i]))/(sample_rate_arr[i]))*100 < PERCENT_DIFF);
  466. TEST_ESP_OK(i2s_driver_uninstall(I2S_NUM_0));
  467. TEST_ASSERT(initial_size == esp_get_free_heap_size());
  468. }
  469. }
  470. vTaskDelay(100 / portTICK_PERIOD_MS);
  471. TEST_ASSERT(initial_size == esp_get_free_heap_size());
  472. }