test_i2s.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. /*
  2. * SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: CC0-1.0
  5. */
  6. /**
  7. * I2S test environment UT_T1_I2S:
  8. * 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.
  9. * connect GPIO15 and GPIO19, GPIO25(ESP32)/GPIO17(ESP32-S2) and GPIO26, GPIO21 and GPIO22(ESP32)/GPIO20(ESP32-S2)
  10. * Please do not connect GPIO32(ESP32) any pull-up resistors externally, it will be used to test i2s adc function.
  11. */
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include "freertos/FreeRTOS.h"
  15. #include "freertos/task.h"
  16. #include "freertos/queue.h"
  17. #include "driver/i2s.h"
  18. #include "driver/gpio.h"
  19. #include "hal/gpio_hal.h"
  20. #include "unity.h"
  21. #include "math.h"
  22. #include "esp_rom_gpio.h"
  23. #define SAMPLE_RATE (36000)
  24. #define SAMPLE_BITS (16)
  25. #if CONFIG_IDF_TARGET_ESP32
  26. #define MASTER_BCK_IO 15
  27. #define MASTER_WS_IO 25
  28. #define SLAVE_BCK_IO 19
  29. #define SLAVE_WS_IO 26
  30. #define DATA_IN_IO 21
  31. #define DATA_OUT_IO 22
  32. #define ADC1_CHANNEL_4_IO 32
  33. #define I2S0_DATA_OUT_IDX I2S0O_DATA_OUT23_IDX
  34. #define I2S0_DATA_IN_IDX I2S0I_DATA_IN15_IDX
  35. #define I2S1_DATA_OUT_IDX I2S1O_DATA_OUT23_IDX
  36. #define I2S1_DATA_IN_IDX I2S1I_DATA_IN15_IDX
  37. #elif CONFIG_IDF_TARGET_ESP32S2
  38. #define MASTER_BCK_IO 15
  39. #define MASTER_WS_IO 28
  40. #define SLAVE_BCK_IO 19
  41. #define SLAVE_WS_IO 26
  42. #define DATA_IN_IO 21
  43. #define DATA_OUT_IO 20
  44. #define I2S0_DATA_OUT_IDX I2S0O_DATA_OUT23_IDX
  45. #define I2S0_DATA_IN_IDX I2S0I_DATA_IN15_IDX
  46. #elif CONFIG_IDF_TARGET_ESP32C3
  47. // TODO: change pins
  48. #define MASTER_BCK_IO 4
  49. #define MASTER_WS_IO 5
  50. #define SLAVE_BCK_IO 14
  51. #define SLAVE_WS_IO 15
  52. #define DATA_IN_IO 19
  53. #define DATA_OUT_IO 18
  54. #define I2S0_DATA_OUT_IDX I2SO_SD_OUT_IDX
  55. #define I2S0_DATA_IN_IDX I2SI_SD_IN_IDX
  56. #elif CONFIG_IDF_TARGET_ESP32S3
  57. #define MASTER_BCK_IO 4
  58. #define MASTER_WS_IO 5
  59. #define SLAVE_BCK_IO 14
  60. #define SLAVE_WS_IO 15
  61. #define DATA_IN_IO 19
  62. #define DATA_OUT_IO 18
  63. #define I2S0_DATA_OUT_IDX I2S0O_SD_OUT_IDX
  64. #define I2S0_DATA_IN_IDX I2S0I_SD_IN_IDX
  65. #define I2S1_DATA_OUT_IDX I2S1O_SD_OUT_IDX
  66. #define I2S1_DATA_IN_IDX I2S1I_SD_IN_IDX
  67. #endif
  68. #define PERCENT_DIFF 0.0001
  69. #define I2S_TEST_MODE_SLAVE_TO_MAXTER 0
  70. #define I2S_TEST_MODE_MASTER_TO_SLAVE 1
  71. #define I2S_TEST_MODE_LOOPBACK 2
  72. // mode: 0, master rx, slave tx. mode: 1, master tx, slave rx. mode: 2, master tx rx loopback
  73. // Since ESP32-S2 has only one I2S, only loop back test can be tested.
  74. static void i2s_test_io_config(int mode)
  75. {
  76. // Connect internal signals using IO matrix.
  77. gpio_hal_iomux_func_sel(GPIO_PIN_MUX_REG[MASTER_BCK_IO], PIN_FUNC_GPIO);
  78. gpio_hal_iomux_func_sel(GPIO_PIN_MUX_REG[MASTER_WS_IO], PIN_FUNC_GPIO);
  79. gpio_hal_iomux_func_sel(GPIO_PIN_MUX_REG[DATA_OUT_IO], PIN_FUNC_GPIO);
  80. gpio_set_direction(MASTER_BCK_IO, GPIO_MODE_INPUT_OUTPUT);
  81. gpio_set_direction(MASTER_WS_IO, GPIO_MODE_INPUT_OUTPUT);
  82. gpio_set_direction(DATA_OUT_IO, GPIO_MODE_INPUT_OUTPUT);
  83. switch (mode) {
  84. #if SOC_I2S_NUM > 1
  85. case I2S_TEST_MODE_SLAVE_TO_MAXTER: {
  86. esp_rom_gpio_connect_out_signal(MASTER_BCK_IO, I2S0I_BCK_OUT_IDX, 0, 0);
  87. esp_rom_gpio_connect_in_signal(MASTER_BCK_IO, I2S1O_BCK_IN_IDX, 0);
  88. esp_rom_gpio_connect_out_signal(MASTER_WS_IO, I2S0I_WS_OUT_IDX, 0, 0);
  89. esp_rom_gpio_connect_in_signal(MASTER_WS_IO, I2S1O_WS_IN_IDX, 0);
  90. esp_rom_gpio_connect_out_signal(DATA_OUT_IO, I2S1_DATA_OUT_IDX, 0, 0);
  91. esp_rom_gpio_connect_in_signal(DATA_OUT_IO, I2S0_DATA_IN_IDX, 0);
  92. }
  93. break;
  94. case I2S_TEST_MODE_MASTER_TO_SLAVE: {
  95. esp_rom_gpio_connect_out_signal(MASTER_BCK_IO, I2S0O_BCK_OUT_IDX, 0, 0);
  96. esp_rom_gpio_connect_in_signal(MASTER_BCK_IO, I2S1I_BCK_IN_IDX, 0);
  97. esp_rom_gpio_connect_out_signal(MASTER_WS_IO, I2S0O_WS_OUT_IDX, 0, 0);
  98. esp_rom_gpio_connect_in_signal(MASTER_WS_IO, I2S1I_WS_IN_IDX, 0);
  99. esp_rom_gpio_connect_out_signal(DATA_OUT_IO, I2S0_DATA_OUT_IDX, 0, 0);
  100. esp_rom_gpio_connect_in_signal(DATA_OUT_IO, I2S1_DATA_IN_IDX, 0);
  101. }
  102. break;
  103. #endif
  104. case I2S_TEST_MODE_LOOPBACK: {
  105. esp_rom_gpio_connect_out_signal(DATA_OUT_IO, I2S0_DATA_OUT_IDX, 0, 0);
  106. esp_rom_gpio_connect_in_signal(DATA_OUT_IO, I2S0_DATA_IN_IDX, 0);
  107. }
  108. break;
  109. default: {
  110. TEST_FAIL_MESSAGE("error: mode not supported");
  111. }
  112. break;
  113. }
  114. }
  115. /**
  116. * i2s initialize test
  117. * 1. i2s_driver_install
  118. * 2. i2s_set_pin
  119. */
  120. TEST_CASE("I2S basic driver install, uninstall, set pin test", "[i2s]")
  121. {
  122. // dac, adc i2s
  123. i2s_config_t i2s_config = {
  124. .mode = I2S_MODE_MASTER | I2S_MODE_TX,
  125. .sample_rate = SAMPLE_RATE,
  126. .bits_per_sample = SAMPLE_BITS,
  127. .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
  128. .communication_format = I2S_COMM_FORMAT_STAND_I2S,
  129. .dma_buf_count = 6,
  130. .dma_buf_len = 60,
  131. .use_apll = 0,
  132. .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
  133. #if SOC_I2S_SUPPORTS_TDM
  134. .chan_mask = I2S_TDM_ACTIVE_CH0 | I2S_TDM_ACTIVE_CH1,
  135. .total_chan = 2,
  136. .left_align = false,
  137. .big_edin = false,
  138. .bit_order_msb = false,
  139. .skip_msk = false
  140. #endif
  141. };
  142. // normal i2s
  143. i2s_pin_config_t pin_config = {
  144. .mck_io_num = -1,
  145. .bck_io_num = MASTER_BCK_IO,
  146. .ws_io_num = MASTER_WS_IO,
  147. .data_out_num = DATA_OUT_IO,
  148. .data_in_num = -1
  149. };
  150. QueueHandle_t evt_que;
  151. TEST_ESP_OK(i2s_driver_install(I2S_NUM_0, &i2s_config, 16, &evt_que));
  152. TEST_ASSERT(evt_que);
  153. TEST_ESP_OK(i2s_set_pin(I2S_NUM_0, &pin_config));
  154. TEST_ESP_OK(i2s_driver_uninstall(I2S_NUM_0));
  155. //error param test
  156. TEST_ASSERT(i2s_driver_install(I2S_NUM_MAX, &i2s_config, 0, NULL) == ESP_ERR_INVALID_ARG);
  157. TEST_ASSERT(i2s_driver_install(I2S_NUM_0, NULL, 0, NULL) == ESP_ERR_INVALID_ARG);
  158. i2s_config.dma_buf_count = 1;
  159. TEST_ASSERT(i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL) == ESP_ERR_INVALID_ARG);
  160. i2s_config.dma_buf_count = 129;
  161. TEST_ASSERT(i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL) == ESP_ERR_INVALID_ARG);
  162. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_STATE, i2s_driver_uninstall(I2S_NUM_0));
  163. }
  164. TEST_CASE("I2S Loopback test(master tx and rx)", "[i2s]")
  165. {
  166. // master driver installed and send data
  167. i2s_config_t master_i2s_config = {
  168. .mode = I2S_MODE_MASTER | I2S_MODE_TX | I2S_MODE_RX,
  169. .sample_rate = SAMPLE_RATE,
  170. .bits_per_sample = SAMPLE_BITS,
  171. .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
  172. .communication_format = I2S_COMM_FORMAT_STAND_I2S,
  173. .dma_buf_count = 6,
  174. .dma_buf_len = 100,
  175. .use_apll = 0,
  176. .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
  177. #if SOC_I2S_SUPPORTS_TDM
  178. .chan_mask = I2S_TDM_ACTIVE_CH0 | I2S_TDM_ACTIVE_CH1,
  179. .total_chan = 2,
  180. .left_align = false,
  181. .big_edin = false,
  182. .bit_order_msb = false,
  183. .skip_msk = false
  184. #endif
  185. };
  186. i2s_pin_config_t master_pin_config = {
  187. .mck_io_num = -1,
  188. .bck_io_num = MASTER_BCK_IO,
  189. .ws_io_num = MASTER_WS_IO,
  190. .data_out_num = DATA_OUT_IO,
  191. .data_in_num = DATA_IN_IO
  192. };
  193. TEST_ESP_OK(i2s_driver_install(I2S_NUM_0, &master_i2s_config, 0, NULL));
  194. TEST_ESP_OK(i2s_set_pin(I2S_NUM_0, &master_pin_config));
  195. i2s_test_io_config(I2S_TEST_MODE_LOOPBACK);
  196. printf("\r\nheap size: %d\n", esp_get_free_heap_size());
  197. uint8_t *data_wr = (uint8_t *)malloc(sizeof(uint8_t) * 400);
  198. size_t i2s_bytes_write = 0;
  199. size_t bytes_read = 0;
  200. int length = 0;
  201. uint8_t *i2s_read_buff = (uint8_t *)malloc(sizeof(uint8_t) * 10000);
  202. for (int i = 0; i < 100; i++) {
  203. data_wr[i] = i + 1;
  204. }
  205. int flag = 0; // break loop flag
  206. int end_position = 0;
  207. // write data to slave
  208. i2s_write(I2S_NUM_0, data_wr, sizeof(uint8_t) * 400, &i2s_bytes_write, 1000 / portTICK_PERIOD_MS);
  209. while (!flag) {
  210. if (length >= 10000 - 500) {
  211. break;
  212. }
  213. i2s_read(I2S_NUM_0, i2s_read_buff + length, sizeof(uint8_t) * 500, &bytes_read, 1000 / portMAX_DELAY);
  214. if (bytes_read > 0) {
  215. for (int i = length; i < length + bytes_read; i++) {
  216. if (i2s_read_buff[i] == 100) {
  217. flag = 1;
  218. end_position = i;
  219. break;
  220. }
  221. }
  222. }
  223. length = length + bytes_read;
  224. }
  225. // test the read data right or not
  226. for (int i = end_position - 99; i <= end_position; i++) {
  227. TEST_ASSERT_EQUAL_UINT8((i - end_position + 100), *(i2s_read_buff + i));
  228. }
  229. free(data_wr);
  230. free(i2s_read_buff);
  231. i2s_driver_uninstall(I2S_NUM_0);
  232. }
  233. #if SOC_I2S_SUPPORTS_TDM
  234. TEST_CASE("I2S TDM Loopback test(master tx and rx)", "[i2s]")
  235. {
  236. // master driver installed and send data
  237. i2s_config_t master_i2s_config = {
  238. .mode = I2S_MODE_MASTER | I2S_MODE_TX | I2S_MODE_RX,
  239. .sample_rate = SAMPLE_RATE,
  240. .bits_per_sample = SAMPLE_BITS,
  241. .channel_format = I2S_CHANNEL_FMT_MULTIPLE,
  242. .communication_format = I2S_COMM_FORMAT_STAND_I2S,
  243. .total_chan = 4,
  244. .chan_mask = I2S_TDM_ACTIVE_CH0 | I2S_TDM_ACTIVE_CH1 | I2S_TDM_ACTIVE_CH2 | I2S_TDM_ACTIVE_CH3,
  245. .dma_buf_count = 6,
  246. .dma_buf_len = 100,
  247. .use_apll = 0,
  248. .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
  249. };
  250. i2s_pin_config_t master_pin_config = {
  251. .mck_io_num = -1,
  252. .bck_io_num = MASTER_BCK_IO,
  253. .ws_io_num = MASTER_WS_IO,
  254. .data_out_num = DATA_OUT_IO,
  255. .data_in_num = DATA_IN_IO
  256. };
  257. TEST_ESP_OK(i2s_driver_install(I2S_NUM_0, &master_i2s_config, 0, NULL));
  258. TEST_ESP_OK(i2s_set_pin(I2S_NUM_0, &master_pin_config));
  259. i2s_test_io_config(I2S_TEST_MODE_LOOPBACK);
  260. printf("\r\nheap size: %d\n", esp_get_free_heap_size());
  261. uint8_t *data_wr = (uint8_t *)malloc(sizeof(uint8_t) * 400);
  262. size_t i2s_bytes_write = 0;
  263. size_t bytes_read = 0;
  264. int length = 0;
  265. uint8_t *i2s_read_buff = (uint8_t *)malloc(sizeof(uint8_t) * 10000);
  266. for (int i = 0; i < 100; i++) {
  267. data_wr[i] = i + 1;
  268. }
  269. int flag = 0; // break loop flag
  270. int end_position = 0;
  271. // write data to slave
  272. i2s_write(I2S_NUM_0, data_wr, sizeof(uint8_t) * 400, &i2s_bytes_write, 1000 / portTICK_PERIOD_MS);
  273. while (!flag) {
  274. if (length >= 10000 - 500) {
  275. break;
  276. }
  277. i2s_read(I2S_NUM_0, i2s_read_buff + length, sizeof(uint8_t) * 500, &bytes_read, 1000 / portMAX_DELAY);
  278. if (bytes_read > 0) {
  279. for (int i = length; i < length + bytes_read; i++) {
  280. if (i2s_read_buff[i] == 100) {
  281. flag = 1;
  282. end_position = i;
  283. break;
  284. }
  285. }
  286. }
  287. length = length + bytes_read;
  288. }
  289. // test the read data right or not
  290. for (int i = end_position - 99; i <= end_position; i++) {
  291. TEST_ASSERT_EQUAL_UINT8((i - end_position + 100), *(i2s_read_buff + i));
  292. }
  293. free(data_wr);
  294. free(i2s_read_buff);
  295. i2s_driver_uninstall(I2S_NUM_0);
  296. }
  297. #endif
  298. #if SOC_I2S_NUM > 1
  299. /* ESP32S2 and ESP32C3 has only single I2S port and hence following test cases are not applicable */
  300. TEST_CASE("I2S write and read test(master tx and slave rx)", "[i2s]")
  301. {
  302. // master driver installed and send data
  303. i2s_config_t master_i2s_config = {
  304. .mode = I2S_MODE_MASTER | I2S_MODE_TX,
  305. .sample_rate = SAMPLE_RATE,
  306. .bits_per_sample = SAMPLE_BITS,
  307. .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
  308. .communication_format = I2S_COMM_FORMAT_STAND_I2S,
  309. .dma_buf_count = 6,
  310. .dma_buf_len = 100,
  311. .use_apll = 0,
  312. .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
  313. #if SOC_I2S_SUPPORTS_TDM
  314. .chan_mask = I2S_TDM_ACTIVE_CH0 | I2S_TDM_ACTIVE_CH1,
  315. .total_chan = 2,
  316. .left_align = false,
  317. .big_edin = false,
  318. .bit_order_msb = false,
  319. .skip_msk = false
  320. #endif
  321. };
  322. i2s_pin_config_t master_pin_config = {
  323. .mck_io_num = -1,
  324. .bck_io_num = MASTER_BCK_IO,
  325. .ws_io_num = MASTER_WS_IO,
  326. .data_out_num = DATA_OUT_IO,
  327. .data_in_num = -1
  328. };
  329. TEST_ESP_OK(i2s_driver_install(I2S_NUM_0, &master_i2s_config, 0, NULL));
  330. TEST_ESP_OK(i2s_set_pin(I2S_NUM_0, &master_pin_config));
  331. i2s_test_io_config(I2S_TEST_MODE_MASTER_TO_SLAVE);
  332. printf("\r\nheap size: %d\n", esp_get_free_heap_size());
  333. i2s_config_t slave_i2s_config = {
  334. .mode = I2S_MODE_SLAVE | I2S_MODE_RX,
  335. .sample_rate = SAMPLE_RATE,
  336. .bits_per_sample = SAMPLE_BITS,
  337. .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
  338. .communication_format = I2S_COMM_FORMAT_STAND_I2S,
  339. .dma_buf_count = 6,
  340. .dma_buf_len = 100,
  341. .use_apll = 0,
  342. .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
  343. #if SOC_I2S_SUPPORTS_TDM
  344. .chan_mask = I2S_TDM_ACTIVE_CH0 | I2S_TDM_ACTIVE_CH1,
  345. .total_chan = 2,
  346. .left_align = false,
  347. .big_edin = false,
  348. .bit_order_msb = false,
  349. .skip_msk = false
  350. #endif
  351. };
  352. i2s_pin_config_t slave_pin_config = {
  353. .mck_io_num = -1,
  354. .bck_io_num = SLAVE_BCK_IO,
  355. .ws_io_num = SLAVE_WS_IO,
  356. .data_out_num = -1,
  357. .data_in_num = DATA_IN_IO,
  358. };
  359. // slave driver installed and receive data
  360. TEST_ESP_OK(i2s_driver_install(I2S_NUM_1, &slave_i2s_config, 0, NULL));
  361. TEST_ESP_OK(i2s_set_pin(I2S_NUM_1, &slave_pin_config));
  362. i2s_test_io_config(I2S_TEST_MODE_MASTER_TO_SLAVE);
  363. printf("\r\nheap size: %d\n", esp_get_free_heap_size());
  364. uint8_t *data_wr = (uint8_t *)malloc(sizeof(uint8_t) * 400);
  365. size_t i2s_bytes_write = 0;
  366. size_t bytes_read = 0;
  367. int length = 0;
  368. uint8_t *i2s_read_buff = (uint8_t *)malloc(sizeof(uint8_t) * 10000);
  369. for (int i = 0; i < 100; i++) {
  370. data_wr[i] = i + 1;
  371. }
  372. int flag = 0; // break loop flag
  373. int end_position = 0;
  374. // write data to slave
  375. i2s_write(I2S_NUM_0, data_wr, sizeof(uint8_t) * 400, &i2s_bytes_write, 1000 / portTICK_PERIOD_MS);
  376. printf("write data size: %d\n", i2s_bytes_write);
  377. while (!flag) {
  378. i2s_read(I2S_NUM_1, i2s_read_buff + length, sizeof(uint8_t) * 500, &bytes_read, 1000 / portTICK_PERIOD_MS);
  379. if (bytes_read > 0) {
  380. printf("read data size: %d\n", bytes_read);
  381. for (int i = length; i < length + bytes_read; i++) {
  382. if (i2s_read_buff[i] == 100) {
  383. flag = 1;
  384. end_position = i;
  385. break;
  386. }
  387. }
  388. }
  389. length = length + bytes_read;
  390. }
  391. // test the readed data right or not
  392. for (int i = end_position - 99; i <= end_position; i++) {
  393. TEST_ASSERT_EQUAL_UINT8((i - end_position + 100), *(i2s_read_buff + i));
  394. }
  395. free(data_wr);
  396. free(i2s_read_buff);
  397. i2s_driver_uninstall(I2S_NUM_0);
  398. i2s_driver_uninstall(I2S_NUM_1);
  399. }
  400. TEST_CASE("I2S write and read test(master rx and slave tx)", "[i2s]")
  401. {
  402. // master driver installed and send data
  403. i2s_config_t master_i2s_config = {
  404. .mode = I2S_MODE_MASTER | I2S_MODE_RX,
  405. .sample_rate = SAMPLE_RATE,
  406. .bits_per_sample = SAMPLE_BITS,
  407. .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
  408. .communication_format = I2S_COMM_FORMAT_STAND_I2S,
  409. .dma_buf_count = 6,
  410. .dma_buf_len = 100,
  411. .use_apll = 1,
  412. .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
  413. #if SOC_I2S_SUPPORTS_TDM
  414. .chan_mask = I2S_TDM_ACTIVE_CH0 | I2S_TDM_ACTIVE_CH1,
  415. .total_chan = 2,
  416. .left_align = false,
  417. .big_edin = false,
  418. .bit_order_msb = false,
  419. .skip_msk = false
  420. #endif
  421. };
  422. i2s_pin_config_t master_pin_config = {
  423. .mck_io_num = -1,
  424. .bck_io_num = MASTER_BCK_IO,
  425. .ws_io_num = MASTER_WS_IO,
  426. .data_out_num = -1,
  427. .data_in_num = DATA_IN_IO,
  428. };
  429. TEST_ESP_OK(i2s_driver_install(I2S_NUM_0, &master_i2s_config, 0, NULL));
  430. TEST_ESP_OK(i2s_set_pin(I2S_NUM_0, &master_pin_config));
  431. i2s_test_io_config(I2S_TEST_MODE_SLAVE_TO_MAXTER);
  432. printf("\r\nheap size: %d\n", esp_get_free_heap_size());
  433. i2s_config_t slave_i2s_config = {
  434. .mode = I2S_MODE_SLAVE | I2S_MODE_TX, // Only RX
  435. .sample_rate = SAMPLE_RATE,
  436. .bits_per_sample = SAMPLE_BITS,
  437. .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT, //2-channels
  438. .communication_format = I2S_COMM_FORMAT_STAND_I2S,
  439. .dma_buf_count = 6,
  440. .dma_buf_len = 100,
  441. .use_apll = 1,
  442. .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
  443. #if SOC_I2S_SUPPORTS_TDM
  444. .chan_mask = I2S_TDM_ACTIVE_CH0 | I2S_TDM_ACTIVE_CH1,
  445. .total_chan = 2,
  446. .left_align = false,
  447. .big_edin = false,
  448. .bit_order_msb = false,
  449. .skip_msk = false
  450. #endif
  451. };
  452. i2s_pin_config_t slave_pin_config = {
  453. .mck_io_num = -1,
  454. .bck_io_num = SLAVE_BCK_IO,
  455. .ws_io_num = SLAVE_WS_IO,
  456. .data_out_num = DATA_OUT_IO,
  457. .data_in_num = -1
  458. };
  459. // slave driver installed and receive data
  460. TEST_ESP_OK(i2s_driver_install(I2S_NUM_1, &slave_i2s_config, 0, NULL));
  461. TEST_ESP_OK(i2s_set_pin(I2S_NUM_1, &slave_pin_config));
  462. i2s_test_io_config(I2S_TEST_MODE_SLAVE_TO_MAXTER);
  463. printf("\r\nheap size: %d\n", esp_get_free_heap_size());
  464. uint8_t *data_wr = (uint8_t *)malloc(sizeof(uint8_t) * 400);
  465. size_t i2s_bytes_write = 0;
  466. size_t bytes_read = 0;
  467. int length = 0;
  468. uint8_t *i2s_read_buff = (uint8_t *)malloc(sizeof(uint8_t) * 10000);
  469. for (int i = 0; i < 100; i++) {
  470. data_wr[i] = i + 1;
  471. }
  472. // slave write data to master
  473. i2s_write(I2S_NUM_1, data_wr, sizeof(uint8_t) * 400, &i2s_bytes_write, 1000 / portTICK_PERIOD_MS);
  474. printf("write data size: %d\n", i2s_bytes_write);
  475. int flag = 0; // break loop flag
  476. volatile int end_position = 0;
  477. // write data to slave
  478. while (!flag) {
  479. TEST_ESP_OK(i2s_read(I2S_NUM_0, i2s_read_buff + length, 10000 - length, &bytes_read, 1000 / portTICK_PERIOD_MS));
  480. if (bytes_read > 0) {
  481. printf("read data size: %d\n", bytes_read);
  482. for (int i = length; i < length + bytes_read; i++) {
  483. if (i2s_read_buff[i] == 100) {
  484. flag = 1;
  485. end_position = i;
  486. break;
  487. }
  488. }
  489. }
  490. length = length + bytes_read;
  491. }
  492. // test the readed data right or not
  493. for (int i = end_position - 99; i <= end_position; i++) {
  494. TEST_ASSERT_EQUAL_UINT8((i - end_position + 100), *(i2s_read_buff + i));
  495. }
  496. free(data_wr);
  497. free(i2s_read_buff);
  498. i2s_driver_uninstall(I2S_NUM_0);
  499. i2s_driver_uninstall(I2S_NUM_1);
  500. }
  501. #endif
  502. TEST_CASE("I2S memory leaking test", "[i2s]")
  503. {
  504. i2s_config_t master_i2s_config = {
  505. .mode = I2S_MODE_MASTER | I2S_MODE_RX,
  506. .sample_rate = SAMPLE_RATE,
  507. .bits_per_sample = SAMPLE_BITS,
  508. .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
  509. .communication_format = I2S_COMM_FORMAT_STAND_I2S,
  510. .dma_buf_count = 6,
  511. .dma_buf_len = 100,
  512. .use_apll = 0,
  513. .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
  514. #if SOC_I2S_SUPPORTS_TDM
  515. .chan_mask = I2S_TDM_ACTIVE_CH0 | I2S_TDM_ACTIVE_CH1,
  516. .total_chan = 2,
  517. .left_align = false,
  518. .big_edin = false,
  519. .bit_order_msb = false,
  520. .skip_msk = false
  521. #endif
  522. };
  523. i2s_pin_config_t master_pin_config = {
  524. .mck_io_num = -1,
  525. .bck_io_num = MASTER_BCK_IO,
  526. .ws_io_num = MASTER_WS_IO,
  527. .data_out_num = -1,
  528. .data_in_num = DATA_IN_IO
  529. };
  530. TEST_ESP_OK(i2s_driver_install(I2S_NUM_0, &master_i2s_config, 0, NULL));
  531. TEST_ESP_OK(i2s_set_pin(I2S_NUM_0, &master_pin_config));
  532. i2s_driver_uninstall(I2S_NUM_0);
  533. int initial_size = esp_get_free_heap_size();
  534. for (int i = 0; i < 100; i++) {
  535. TEST_ESP_OK(i2s_driver_install(I2S_NUM_0, &master_i2s_config, 0, NULL));
  536. TEST_ESP_OK(i2s_set_pin(I2S_NUM_0, &master_pin_config));
  537. i2s_driver_uninstall(I2S_NUM_0);
  538. TEST_ASSERT(initial_size == esp_get_free_heap_size());
  539. }
  540. vTaskDelay(100 / portTICK_PERIOD_MS);
  541. TEST_ASSERT(initial_size == esp_get_free_heap_size());
  542. }
  543. #if SOC_I2S_SUPPORTS_APLL
  544. /*
  545. * The I2S APLL clock variation test used to test the difference between the different sample rates, different bits per sample
  546. * and the APLL clock generate for it. The TEST_CASE passes PERCENT_DIFF variation from the provided sample rate in APLL generated clock
  547. * The percentage difference calculated as (mod((obtained clock rate - desired clock rate)/(desired clock rate))) * 100.
  548. */
  549. TEST_CASE("I2S APLL clock variation test", "[i2s]")
  550. {
  551. i2s_pin_config_t pin_config = {
  552. .mck_io_num = -1,
  553. .bck_io_num = MASTER_BCK_IO,
  554. .ws_io_num = MASTER_WS_IO,
  555. .data_out_num = DATA_OUT_IO,
  556. .data_in_num = -1
  557. };
  558. i2s_config_t i2s_config = {
  559. .mode = I2S_MODE_MASTER | I2S_MODE_TX,
  560. .sample_rate = SAMPLE_RATE,
  561. .bits_per_sample = SAMPLE_BITS,
  562. .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
  563. .communication_format = I2S_COMM_FORMAT_STAND_I2S,
  564. .dma_buf_count = 6,
  565. .dma_buf_len = 60,
  566. .use_apll = true,
  567. .intr_alloc_flags = 0,
  568. #if SOC_I2S_SUPPORTS_TDM
  569. .chan_mask = I2S_TDM_ACTIVE_CH0 | I2S_TDM_ACTIVE_CH1,
  570. .total_chan = 2,
  571. .left_align = false,
  572. .big_edin = false,
  573. .bit_order_msb = false,
  574. .skip_msk = false
  575. #endif
  576. };
  577. TEST_ESP_OK(i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL));
  578. TEST_ESP_OK(i2s_set_pin(I2S_NUM_0, &pin_config));
  579. TEST_ESP_OK(i2s_driver_uninstall(I2S_NUM_0));
  580. int initial_size = esp_get_free_heap_size();
  581. uint32_t sample_rate_arr[8] = { 10675, 11025, 16000, 22050, 32000, 44100, 48000, 96000 };
  582. int bits_per_sample_arr[3] = { 16, 24, 32 };
  583. for (int i = 0; i < (sizeof(sample_rate_arr) / sizeof(sample_rate_arr[0])); i++) {
  584. for (int j = 0; j < (sizeof(bits_per_sample_arr) / sizeof(bits_per_sample_arr[0])); j++) {
  585. i2s_config.sample_rate = sample_rate_arr[i];
  586. i2s_config.bits_per_sample = bits_per_sample_arr[j];
  587. TEST_ESP_OK(i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL));
  588. TEST_ESP_OK(i2s_set_pin(I2S_NUM_0, &pin_config));
  589. TEST_ASSERT((fabs((i2s_get_clk(I2S_NUM_0) - sample_rate_arr[i])) / (sample_rate_arr[i])) * 100 < PERCENT_DIFF);
  590. TEST_ESP_OK(i2s_driver_uninstall(I2S_NUM_0));
  591. TEST_ASSERT(initial_size == esp_get_free_heap_size());
  592. }
  593. }
  594. vTaskDelay(100 / portTICK_PERIOD_MS);
  595. TEST_ASSERT(initial_size == esp_get_free_heap_size());
  596. }
  597. #endif
  598. #if SOC_I2S_SUPPORTS_ADC
  599. /* Only ESP32 need I2S adc/dac test */
  600. TEST_CASE("I2S adc test", "[i2s]")
  601. {
  602. // init I2S ADC
  603. i2s_config_t i2s_config = {
  604. .mode = I2S_MODE_MASTER | I2S_MODE_RX | I2S_MODE_ADC_BUILT_IN,
  605. .sample_rate = SAMPLE_RATE,
  606. .bits_per_sample = SAMPLE_BITS,
  607. .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
  608. .intr_alloc_flags = 0,
  609. .dma_buf_count = 2,
  610. .dma_buf_len = 1024,
  611. .use_apll = 0,
  612. };
  613. // install and start I2S driver
  614. i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL);
  615. // init ADC pad
  616. i2s_set_adc_mode(ADC_UNIT_1, ADC1_CHANNEL_4);
  617. // enable adc sampling, ADC_WIDTH_BIT_12, ADC_ATTEN_DB_11 hard-coded in adc_i2s_mode_init
  618. i2s_adc_enable(I2S_NUM_0);
  619. // init read buffer
  620. uint16_t *i2sReadBuffer = (uint16_t *)calloc(1024, sizeof(uint16_t));
  621. size_t bytesRead;
  622. for (int loop = 0; loop < 10; loop++) {
  623. for (int level = 0; level <= 1; level++) {
  624. if (level == 0) {
  625. gpio_set_pull_mode(ADC1_CHANNEL_4_IO, GPIO_PULLDOWN_ONLY);
  626. } else {
  627. gpio_set_pull_mode(ADC1_CHANNEL_4_IO, GPIO_PULLUP_ONLY);
  628. }
  629. vTaskDelay(200 / portTICK_RATE_MS);
  630. // read data from adc, will block until buffer is full
  631. i2s_read(I2S_NUM_0, (void *)i2sReadBuffer, 1024 * sizeof(uint16_t), &bytesRead, portMAX_DELAY);
  632. // calc average
  633. int64_t adcSumValue = 0;
  634. for (size_t i = 0; i < 1024; i++) {
  635. adcSumValue += i2sReadBuffer[i] & 0xfff;
  636. }
  637. int adcAvgValue = adcSumValue / 1024;
  638. printf("adc average val: %d\n", adcAvgValue);
  639. if (level == 0) {
  640. if (adcAvgValue > 100) {
  641. i2s_adc_disable(I2S_NUM_0);
  642. free(i2sReadBuffer);
  643. i2s_driver_uninstall(I2S_NUM_0);
  644. TEST_ASSERT_LESS_THAN(100, adcAvgValue);
  645. }
  646. } else {
  647. if (adcAvgValue < 4000) {
  648. i2s_adc_disable(I2S_NUM_0);
  649. free(i2sReadBuffer);
  650. i2s_driver_uninstall(I2S_NUM_0);
  651. TEST_ASSERT_GREATER_THAN(4000, adcAvgValue);
  652. }
  653. }
  654. }
  655. }
  656. i2s_adc_disable(I2S_NUM_0);
  657. free(i2sReadBuffer);
  658. i2s_driver_uninstall(I2S_NUM_0);
  659. }
  660. #endif
  661. #if SOC_I2S_SUPPORTS_DAC
  662. TEST_CASE("I2S dac test", "[i2s]")
  663. {
  664. // dac, adc i2s
  665. i2s_config_t i2s_config = {
  666. .mode = I2S_MODE_MASTER | I2S_MODE_TX,
  667. .sample_rate = SAMPLE_RATE,
  668. .bits_per_sample = SAMPLE_BITS,
  669. .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
  670. .communication_format = I2S_COMM_FORMAT_STAND_I2S,
  671. .dma_buf_count = 6,
  672. .dma_buf_len = 60,
  673. .use_apll = 0,
  674. .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
  675. };
  676. //install and start i2s driver
  677. TEST_ESP_OK(i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL));
  678. //for internal DAC, this will enable both of the internal channels
  679. TEST_ESP_OK(i2s_set_pin(I2S_NUM_0, NULL));
  680. //stop & destroy i2s driver
  681. TEST_ESP_OK(i2s_driver_uninstall(I2S_NUM_0));
  682. }
  683. #endif