test_i2s.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. #include "math.h"
  12. #define SAMPLE_RATE (36000)
  13. #define SAMPLE_BITS (16)
  14. #define MASTER_BCK_IO 18
  15. #define SLAVE_BCK_IO 19
  16. #define SLAVE_WS_IO 26
  17. #define DATA_IN_IO 21
  18. #if CONFIG_IDF_TARGET_ESP32
  19. #define MASTER_WS_IO 25
  20. #define DATA_OUT_IO 22
  21. #elif CONFIG_IDF_TARGET_ESP32S2BETA
  22. #define MASTER_WS_IO 28
  23. #define DATA_OUT_IO 20
  24. #endif
  25. #define PERCENT_DIFF 0.0001
  26. /**
  27. * i2s initialize test
  28. * 1. i2s_driver_install
  29. * 2. i2s_set_pin
  30. */
  31. TEST_CASE("I2S basic driver install, uninstall, set pin test", "[i2s]")
  32. {
  33. // dac, adc i2s
  34. i2s_config_t i2s_config = {
  35. .mode = I2S_MODE_MASTER | I2S_MODE_TX,
  36. .sample_rate = SAMPLE_RATE,
  37. .bits_per_sample = SAMPLE_BITS,
  38. .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
  39. .communication_format = I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB,
  40. .dma_buf_count = 6,
  41. .dma_buf_len = 60,
  42. .use_apll = 0,
  43. .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1 ,
  44. };
  45. //install and start i2s driver
  46. TEST_ESP_OK(i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL));
  47. //for internal DAC, this will enable both of the internal channels
  48. TEST_ESP_OK(i2s_set_pin(I2S_NUM_0, NULL));
  49. //stop & destroy i2s driver
  50. TEST_ESP_OK(i2s_driver_uninstall(I2S_NUM_0));
  51. // normal i2s
  52. i2s_pin_config_t pin_config = {
  53. .bck_io_num = MASTER_BCK_IO,
  54. .ws_io_num = MASTER_WS_IO,
  55. .data_out_num = DATA_OUT_IO,
  56. .data_in_num = -1
  57. };
  58. TEST_ESP_OK(i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL));
  59. TEST_ESP_OK(i2s_set_pin(I2S_NUM_0, &pin_config));
  60. TEST_ESP_OK(i2s_driver_uninstall(I2S_NUM_0));
  61. //error param test
  62. TEST_ASSERT(i2s_driver_install(I2S_NUM_MAX, &i2s_config, 0, NULL) == ESP_ERR_INVALID_ARG);
  63. TEST_ASSERT(i2s_driver_install(I2S_NUM_0, NULL, 0, NULL) == ESP_ERR_INVALID_ARG);
  64. i2s_config.dma_buf_count = 1;
  65. TEST_ASSERT(i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL) == ESP_ERR_INVALID_ARG);
  66. i2s_config.dma_buf_count = 129;
  67. TEST_ASSERT(i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL) == ESP_ERR_INVALID_ARG);
  68. TEST_ESP_OK(i2s_driver_uninstall(I2S_NUM_0));
  69. }
  70. #if !DISABLED_FOR_TARGETS(ESP32S2BETA)
  71. /* ESP32S2BETA has only single I2S port and hence following test cases are not applicable */
  72. TEST_CASE("I2S write and read test(master tx and slave rx)", "[i2s][test_env=UT_T1_I2S]")
  73. {
  74. // master driver installed and send data
  75. i2s_config_t master_i2s_config = {
  76. .mode = I2S_MODE_MASTER | I2S_MODE_TX,
  77. .sample_rate = SAMPLE_RATE,
  78. .bits_per_sample = SAMPLE_BITS,
  79. .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
  80. .communication_format = I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB,
  81. .dma_buf_count = 6,
  82. .dma_buf_len = 100,
  83. .use_apll = 0,
  84. .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1 ,
  85. };
  86. i2s_pin_config_t master_pin_config = {
  87. .bck_io_num = MASTER_BCK_IO,
  88. .ws_io_num = MASTER_WS_IO,
  89. .data_out_num = DATA_OUT_IO,
  90. .data_in_num = -1
  91. };
  92. TEST_ESP_OK(i2s_driver_install(I2S_NUM_0, &master_i2s_config, 0, NULL));
  93. TEST_ESP_OK(i2s_set_pin(I2S_NUM_0, &master_pin_config));
  94. printf("\r\nheap size: %d\n", esp_get_free_heap_size());
  95. i2s_config_t slave_i2s_config = {
  96. .mode = I2S_MODE_SLAVE | I2S_MODE_RX,
  97. .sample_rate = SAMPLE_RATE,
  98. .bits_per_sample = SAMPLE_BITS,
  99. .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
  100. .communication_format = I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB,
  101. .dma_buf_count = 6,
  102. .dma_buf_len = 100,
  103. .use_apll = 0,
  104. .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1 ,
  105. };
  106. i2s_pin_config_t slave_pin_config = {
  107. .bck_io_num = SLAVE_BCK_IO,
  108. .ws_io_num = SLAVE_WS_IO,
  109. .data_out_num = -1,
  110. .data_in_num = DATA_IN_IO,
  111. };
  112. // slave driver installed and receive data
  113. TEST_ESP_OK(i2s_driver_install(I2S_NUM_1, &slave_i2s_config, 0, NULL));
  114. TEST_ESP_OK(i2s_set_pin(I2S_NUM_1, &slave_pin_config));
  115. printf("\r\nheap size: %d\n", esp_get_free_heap_size());
  116. uint8_t* data_wr = (uint8_t*)malloc(sizeof(uint8_t)*400);
  117. size_t i2s_bytes_write = 0;
  118. size_t bytes_read = 0;
  119. int length = 0;
  120. uint8_t *i2s_read_buff = (uint8_t*)malloc(sizeof(uint8_t)*10000);
  121. for(int i=0; i<100; i++) {
  122. data_wr[i] = i+1;
  123. }
  124. int flag=0; // break loop flag
  125. int end_position = 0;
  126. // write data to slave
  127. i2s_write(I2S_NUM_0, data_wr, sizeof(uint8_t)*400, &i2s_bytes_write, 1000 / portTICK_PERIOD_MS);
  128. while(!flag){
  129. i2s_read(I2S_NUM_1, i2s_read_buff + length, sizeof(uint8_t)*500, &bytes_read, 1000/portMAX_DELAY);
  130. if(bytes_read>0) {
  131. printf("read data size: %d\n", bytes_read);
  132. for(int i=length; i<length + bytes_read; i++) {
  133. if(i2s_read_buff[i] == 100) {
  134. flag=1;
  135. end_position = i;
  136. break;
  137. }
  138. }
  139. }
  140. length = length + bytes_read;
  141. }
  142. // test the readed data right or not
  143. for(int i=end_position-99; i<=end_position; i++) {
  144. TEST_ASSERT(*(i2s_read_buff + i) == (i-end_position+100));
  145. }
  146. free(data_wr);
  147. free(i2s_read_buff);
  148. i2s_driver_uninstall(I2S_NUM_0);
  149. i2s_driver_uninstall(I2S_NUM_1);
  150. }
  151. TEST_CASE("I2S write and read test(master rx and slave tx)", "[i2s][test_env=UT_T1_I2S]")
  152. {
  153. // master driver installed and send data
  154. i2s_config_t master_i2s_config = {
  155. .mode = I2S_MODE_MASTER | I2S_MODE_RX,
  156. .sample_rate = SAMPLE_RATE,
  157. .bits_per_sample = SAMPLE_BITS,
  158. .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
  159. .communication_format = I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB,
  160. .dma_buf_count = 6,
  161. .dma_buf_len = 100,
  162. .use_apll = 0,
  163. .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1 ,
  164. };
  165. i2s_pin_config_t master_pin_config = {
  166. .bck_io_num = MASTER_BCK_IO,
  167. .ws_io_num = MASTER_WS_IO,
  168. .data_out_num = -1,
  169. .data_in_num = DATA_IN_IO,
  170. };
  171. TEST_ESP_OK(i2s_driver_install(I2S_NUM_0, &master_i2s_config, 0, NULL));
  172. TEST_ESP_OK(i2s_set_pin(I2S_NUM_0, &master_pin_config));
  173. printf("\r\nheap size: %d\n", esp_get_free_heap_size());
  174. i2s_config_t slave_i2s_config = {
  175. .mode = I2S_MODE_SLAVE | I2S_MODE_TX, // Only RX
  176. .sample_rate = SAMPLE_RATE,
  177. .bits_per_sample = SAMPLE_BITS,
  178. .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT, //2-channels
  179. .communication_format = I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB,
  180. .dma_buf_count = 6,
  181. .dma_buf_len = 100,
  182. .use_apll = 0,
  183. .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1 ,
  184. };
  185. i2s_pin_config_t slave_pin_config = {
  186. .bck_io_num = SLAVE_BCK_IO,
  187. .ws_io_num = SLAVE_WS_IO,
  188. .data_out_num = DATA_OUT_IO,
  189. .data_in_num = -1
  190. };
  191. // slave driver installed and receive data
  192. TEST_ESP_OK(i2s_driver_install(I2S_NUM_1, &slave_i2s_config, 0, NULL));
  193. TEST_ESP_OK(i2s_set_pin(I2S_NUM_1, &slave_pin_config));
  194. uint8_t* data_wr = (uint8_t*)malloc(sizeof(uint8_t)*400);
  195. size_t i2s_bytes_write = 0;
  196. size_t bytes_read = 0;
  197. int length = 0;
  198. uint8_t *i2s_read_buff = (uint8_t*)malloc(sizeof(uint8_t)*100000);
  199. for(int i=0; i<100; i++) {
  200. data_wr[i] = i+1;
  201. }
  202. // slave write data to master
  203. i2s_write(I2S_NUM_1, data_wr, sizeof(uint8_t)*400, &i2s_bytes_write, 1000 / portTICK_PERIOD_MS);
  204. int flag=0; // break loop flag
  205. int end_position = 0;
  206. // write data to slave
  207. while(!flag){
  208. TEST_ESP_OK(i2s_read(I2S_NUM_0, i2s_read_buff + length, 10000-length, &bytes_read, 1000/portMAX_DELAY));
  209. if(bytes_read > 0) {
  210. for(int i=length; i<length+bytes_read; i++) {
  211. if(i2s_read_buff[i] == 100) {
  212. flag=1;
  213. end_position = i;
  214. break;
  215. }
  216. }
  217. }
  218. length = length + bytes_read;
  219. }
  220. // test the readed data right or not
  221. for(int i=end_position-99; i<=end_position; i++) {
  222. TEST_ASSERT(*(i2s_read_buff + i) == (i-end_position+100));
  223. }
  224. free(data_wr);
  225. free(i2s_read_buff);
  226. i2s_driver_uninstall(I2S_NUM_0);
  227. i2s_driver_uninstall(I2S_NUM_1);
  228. }
  229. #endif /* CONFIG_IDF_TARGET_ESP32 */
  230. TEST_CASE("I2S memory leaking test", "[i2s]")
  231. {
  232. i2s_config_t master_i2s_config = {
  233. .mode = I2S_MODE_MASTER | I2S_MODE_RX,
  234. .sample_rate = SAMPLE_RATE,
  235. .bits_per_sample = SAMPLE_BITS,
  236. .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
  237. .communication_format = I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB,
  238. .dma_buf_count = 6,
  239. .dma_buf_len = 100,
  240. .use_apll = 0,
  241. .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1 ,
  242. };
  243. i2s_pin_config_t master_pin_config = {
  244. .bck_io_num = MASTER_BCK_IO,
  245. .ws_io_num = MASTER_WS_IO,
  246. .data_out_num = -1,
  247. .data_in_num = DATA_IN_IO
  248. };
  249. TEST_ESP_OK(i2s_driver_install(I2S_NUM_0, &master_i2s_config, 0, NULL));
  250. TEST_ESP_OK(i2s_set_pin(I2S_NUM_0, &master_pin_config));
  251. i2s_driver_uninstall(I2S_NUM_0);
  252. int initial_size = esp_get_free_heap_size();
  253. for(int i=0; i<100; i++) {
  254. TEST_ESP_OK(i2s_driver_install(I2S_NUM_0, &master_i2s_config, 0, NULL));
  255. TEST_ESP_OK(i2s_set_pin(I2S_NUM_0, &master_pin_config));
  256. i2s_driver_uninstall(I2S_NUM_0);
  257. TEST_ASSERT(initial_size == esp_get_free_heap_size());
  258. }
  259. vTaskDelay(100 / portTICK_PERIOD_MS);
  260. TEST_ASSERT(initial_size == esp_get_free_heap_size());
  261. }
  262. /*
  263. * The I2S APLL clock variation test used to test the difference between the different sample rates, different bits per sample
  264. * and the APLL clock generate for it. The TEST_CASE passes PERCENT_DIFF variation from the provided sample rate in APLL generated clock
  265. * The percentage difference calculated as (mod((obtained clock rate - desired clock rate)/(desired clock rate))) * 100.
  266. */
  267. TEST_CASE("I2S APLL clock variation test", "[i2s]")
  268. {
  269. i2s_pin_config_t pin_config = {
  270. .bck_io_num = MASTER_BCK_IO,
  271. .ws_io_num = MASTER_WS_IO,
  272. .data_out_num = DATA_OUT_IO,
  273. .data_in_num = -1
  274. };
  275. i2s_config_t i2s_config = {
  276. .mode = I2S_MODE_MASTER | I2S_MODE_TX,
  277. .sample_rate = SAMPLE_RATE,
  278. .bits_per_sample = SAMPLE_BITS,
  279. .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
  280. .communication_format = I2S_COMM_FORMAT_I2S,
  281. .dma_buf_count = 6,
  282. .dma_buf_len = 60,
  283. .use_apll = true,
  284. .intr_alloc_flags = 0,
  285. };
  286. TEST_ESP_OK(i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL));
  287. TEST_ESP_OK(i2s_set_pin(I2S_NUM_0, &pin_config));
  288. TEST_ESP_OK(i2s_driver_uninstall(I2S_NUM_0));
  289. int initial_size = esp_get_free_heap_size();
  290. uint32_t sample_rate_arr[8] = { 10675, 11025, 16000, 22050, 32000, 44100, 48000, 96000 };
  291. int bits_per_sample_arr[3] = { 16, 24, 32 };
  292. for (int i = 0; i < (sizeof(sample_rate_arr)/sizeof(sample_rate_arr[0])); i++) {
  293. for (int j = 0; j < (sizeof(bits_per_sample_arr)/sizeof(bits_per_sample_arr[0])); j++) {
  294. i2s_config.sample_rate = sample_rate_arr[i];
  295. i2s_config.bits_per_sample = bits_per_sample_arr[j];
  296. TEST_ESP_OK(i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL));
  297. TEST_ESP_OK(i2s_set_pin(I2S_NUM_0, &pin_config));
  298. TEST_ASSERT((fabs((i2s_get_clk(I2S_NUM_0) - sample_rate_arr[i]))/(sample_rate_arr[i]))*100 < PERCENT_DIFF);
  299. TEST_ESP_OK(i2s_driver_uninstall(I2S_NUM_0));
  300. TEST_ASSERT(initial_size == esp_get_free_heap_size());
  301. }
  302. }
  303. vTaskDelay(100 / portTICK_PERIOD_MS);
  304. TEST_ASSERT(initial_size == esp_get_free_heap_size());
  305. }