i2s.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227
  1. // Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <string.h>
  15. #include <stdbool.h>
  16. #include <math.h>
  17. #include <esp_types.h>
  18. #include "freertos/FreeRTOS.h"
  19. #include "freertos/queue.h"
  20. #include "freertos/semphr.h"
  21. #include "soc/lldesc.h"
  22. #include "driver/gpio.h"
  23. #include "driver/i2s.h"
  24. #include "hal/gpio_hal.h"
  25. #if SOC_I2S_SUPPORTS_ADC_DAC
  26. #include "driver/dac.h"
  27. #include "hal/i2s_hal.h"
  28. #include "adc1_private.h"
  29. #endif
  30. #include "soc/rtc.h"
  31. #include "esp_intr_alloc.h"
  32. #include "esp_err.h"
  33. #include "esp_attr.h"
  34. #include "esp_log.h"
  35. #include "esp_pm.h"
  36. #include "soc/chip_revision.h"
  37. #include "hal/efuse_hal.h"
  38. #include "esp_rom_gpio.h"
  39. #include "sdkconfig.h"
  40. static const char* I2S_TAG = "I2S";
  41. #define I2S_CHECK(a, str, ret) if (!(a)) { \
  42. ESP_LOGE(I2S_TAG,"%s(%d): %s", __FUNCTION__, __LINE__, str); \
  43. return (ret); \
  44. }
  45. #define I2S_ENTER_CRITICAL_ISR() portENTER_CRITICAL_ISR(&i2s_spinlock[i2s_num])
  46. #define I2S_EXIT_CRITICAL_ISR() portEXIT_CRITICAL_ISR(&i2s_spinlock[i2s_num])
  47. #define I2S_ENTER_CRITICAL() portENTER_CRITICAL(&i2s_spinlock[i2s_num])
  48. #define I2S_EXIT_CRITICAL() portEXIT_CRITICAL(&i2s_spinlock[i2s_num])
  49. #define I2S_FULL_DUPLEX_SLAVE_MODE_MASK (I2S_MODE_TX | I2S_MODE_RX | I2S_MODE_SLAVE)
  50. #define I2S_FULL_DUPLEX_MASTER_MODE_MASK (I2S_MODE_TX | I2S_MODE_RX | I2S_MODE_MASTER)
  51. //TODO: Refactor to put this logic into LL
  52. #define I2S_AD_BCK_FACTOR (2)
  53. #define I2S_PDM_BCK_FACTOR (64)
  54. #define I2S_BASE_CLK (2*APB_CLK_FREQ)
  55. #define APLL_I2S_MIN_RATE (10675) //in Hz, I2S Clock rate limited by hardware
  56. /**
  57. * @brief DMA buffer object
  58. *
  59. */
  60. typedef struct {
  61. char **buf;
  62. int buf_size;
  63. int rw_pos;
  64. void *curr_ptr;
  65. SemaphoreHandle_t mux;
  66. xQueueHandle queue;
  67. lldesc_t **desc;
  68. } i2s_dma_t;
  69. /**
  70. * @brief I2S object instance
  71. *
  72. */
  73. typedef struct {
  74. i2s_port_t i2s_num; /*!< I2S port number*/
  75. int queue_size; /*!< I2S event queue size*/
  76. QueueHandle_t i2s_queue; /*!< I2S queue handler*/
  77. int dma_buf_count; /*!< DMA buffer count, number of buffer*/
  78. int dma_buf_len; /*!< DMA buffer length, length of each buffer*/
  79. i2s_dma_t *rx; /*!< DMA Tx buffer*/
  80. i2s_dma_t *tx; /*!< DMA Rx buffer*/
  81. i2s_isr_handle_t i2s_isr_handle; /*!< I2S Interrupt handle*/
  82. int channel_num; /*!< Number of channels*/
  83. int bytes_per_sample; /*!< Bytes per sample*/
  84. int bits_per_sample; /*!< Bits per sample*/
  85. i2s_mode_t mode; /*!< I2S Working mode*/
  86. uint32_t sample_rate; /*!< I2S sample rate */
  87. bool use_apll; /*!< I2S use APLL clock */
  88. bool tx_desc_auto_clear; /*!< I2S auto clear tx descriptor on underflow */
  89. int fixed_mclk; /*!< I2S fixed MLCK clock */
  90. double real_rate;
  91. #ifdef CONFIG_PM_ENABLE
  92. esp_pm_lock_handle_t pm_lock;
  93. #endif
  94. i2s_hal_context_t hal; /*!< I2S hal context*/
  95. } i2s_obj_t;
  96. static i2s_obj_t *p_i2s_obj[I2S_NUM_MAX] = {0};
  97. static portMUX_TYPE i2s_spinlock[I2S_NUM_MAX];
  98. #if SOC_I2S_SUPPORTS_ADC_DAC
  99. static int _i2s_adc_unit = -1;
  100. static int _i2s_adc_channel = -1;
  101. #endif
  102. static i2s_dma_t *i2s_create_dma_queue(i2s_port_t i2s_num, int dma_buf_count, int dma_buf_len);
  103. static esp_err_t i2s_destroy_dma_queue(i2s_port_t i2s_num, i2s_dma_t *dma);
  104. static inline void gpio_matrix_out_check(int gpio, uint32_t signal_idx, bool out_inv, bool oen_inv)
  105. {
  106. //if pin = -1, do not need to configure
  107. if (gpio != -1) {
  108. gpio_hal_iomux_func_sel(GPIO_PIN_MUX_REG[gpio], PIN_FUNC_GPIO);
  109. gpio_set_direction(gpio, GPIO_MODE_OUTPUT);
  110. esp_rom_gpio_connect_out_signal(gpio, signal_idx, out_inv, oen_inv);
  111. }
  112. }
  113. static inline void gpio_matrix_in_check(int gpio, uint32_t signal_idx, bool inv)
  114. {
  115. if (gpio != -1) {
  116. gpio_hal_iomux_func_sel(GPIO_PIN_MUX_REG[gpio], PIN_FUNC_GPIO);
  117. //Set direction, for some GPIOs, the input function are not enabled as default.
  118. gpio_set_direction(gpio, GPIO_MODE_INPUT);
  119. esp_rom_gpio_connect_in_signal(gpio, signal_idx, inv);
  120. }
  121. }
  122. esp_err_t i2s_clear_intr_status(i2s_port_t i2s_num, uint32_t clr_mask)
  123. {
  124. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  125. i2s_hal_clear_intr_status(&(p_i2s_obj[i2s_num]->hal), clr_mask);
  126. return ESP_OK;
  127. }
  128. esp_err_t i2s_enable_rx_intr(i2s_port_t i2s_num)
  129. {
  130. I2S_ENTER_CRITICAL();
  131. i2s_hal_enable_rx_intr(&(p_i2s_obj[i2s_num]->hal));
  132. I2S_EXIT_CRITICAL();
  133. return ESP_OK;
  134. }
  135. esp_err_t i2s_disable_rx_intr(i2s_port_t i2s_num)
  136. {
  137. I2S_ENTER_CRITICAL();
  138. i2s_hal_disable_rx_intr(&(p_i2s_obj[i2s_num]->hal));
  139. I2S_EXIT_CRITICAL();
  140. return ESP_OK;
  141. }
  142. esp_err_t i2s_disable_tx_intr(i2s_port_t i2s_num)
  143. {
  144. I2S_ENTER_CRITICAL();
  145. i2s_hal_disable_tx_intr(&(p_i2s_obj[i2s_num]->hal));
  146. I2S_EXIT_CRITICAL();
  147. return ESP_OK;
  148. }
  149. esp_err_t i2s_enable_tx_intr(i2s_port_t i2s_num)
  150. {
  151. I2S_ENTER_CRITICAL();
  152. i2s_hal_enable_tx_intr(&(p_i2s_obj[i2s_num]->hal));
  153. I2S_EXIT_CRITICAL();
  154. return ESP_OK;
  155. }
  156. float i2s_get_clk(i2s_port_t i2s_num)
  157. {
  158. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  159. return p_i2s_obj[i2s_num]->real_rate;
  160. }
  161. static esp_err_t i2s_isr_register(i2s_port_t i2s_num, int intr_alloc_flags, void (*fn)(void*), void * arg, i2s_isr_handle_t *handle)
  162. {
  163. return esp_intr_alloc(i2s_periph_signal[i2s_num].irq, intr_alloc_flags, fn, arg, handle);
  164. }
  165. static float i2s_apll_get_fi2s(int bits_per_sample, int sdm0, int sdm1, int sdm2, int odir)
  166. {
  167. int f_xtal = (int)rtc_clk_xtal_freq_get() * 1000000;
  168. #if CONFIG_IDF_TARGET_ESP32
  169. /* ESP32 rev0 silicon issue for APLL range/accuracy, please see ESP32 ECO document for more information on this */
  170. if (!ESP_CHIP_REV_ABOVE(efuse_hal_chip_revision(), 100)) {
  171. sdm0 = 0;
  172. sdm1 = 0;
  173. }
  174. #endif
  175. float fout = f_xtal * (sdm2 + sdm1 / 256.0f + sdm0 / 65536.0f + 4);
  176. if (fout < SOC_I2S_APLL_MIN_FREQ || fout > SOC_I2S_APLL_MAX_FREQ) {
  177. return SOC_I2S_APLL_MAX_FREQ;
  178. }
  179. float fpll = fout / (2 * (odir+2)); //== fi2s (N=1, b=0, a=1)
  180. return fpll/2;
  181. }
  182. /**
  183. * @brief APLL calculate function, was described by following:
  184. * APLL Output frequency is given by the formula:
  185. *
  186. * apll_freq = xtal_freq * (4 + sdm2 + sdm1/256 + sdm0/65536)/((o_div + 2) * 2)
  187. * apll_freq = fout / ((o_div + 2) * 2)
  188. *
  189. * The dividend in this expression should be in the range of 240 - 600 MHz.
  190. * In rev. 0 of ESP32, sdm0 and sdm1 are unused and always set to 0.
  191. * * sdm0 frequency adjustment parameter, 0..255
  192. * * sdm1 frequency adjustment parameter, 0..255
  193. * * sdm2 frequency adjustment parameter, 0..63
  194. * * o_div frequency divider, 0..31
  195. *
  196. * The most accurate way to find the sdm0..2 and odir parameters is to loop through them all,
  197. * then apply the above formula, finding the closest frequency to the desired one.
  198. * But 256*256*64*32 = 134.217.728 loops are too slow with ESP32
  199. * 1. We will choose the parameters with the highest level of change,
  200. * With 350MHz<fout<500MHz, we limit the sdm2 from 4 to 9,
  201. * Take average frequency close to the desired frequency, and select sdm2
  202. * 2. Next, we look for sequences of less influential and more detailed parameters,
  203. * also by taking the average of the largest and smallest frequencies closer to the desired frequency.
  204. * 3. And finally, loop through all the most detailed of the parameters, finding the best desired frequency
  205. *
  206. * @param[in] rate The I2S Frequency (MCLK)
  207. * @param[in] bits_per_sample The bits per sample
  208. * @param[out] sdm0 The sdm 0
  209. * @param[out] sdm1 The sdm 1
  210. * @param[out] sdm2 The sdm 2
  211. * @param[out] odir The odir
  212. *
  213. * @return ESP_ERR_INVALID_ARG or ESP_OK
  214. */
  215. static esp_err_t i2s_apll_calculate_fi2s(int rate, int bits_per_sample, int *sdm0, int *sdm1, int *sdm2, int *odir)
  216. {
  217. int _odir, _sdm0, _sdm1, _sdm2;
  218. float r = rtc_clk_xtal_freq_get() * 1000000. / (rate * 2 * 2);
  219. int _sdm2_max;
  220. uint32_t prec = -1;
  221. int o, s1, s0;
  222. if (rate/bits_per_sample/2/8 < APLL_I2S_MIN_RATE) {
  223. return ESP_ERR_INVALID_ARG;
  224. }
  225. *sdm0 = 0;
  226. *sdm1 = 0;
  227. *sdm2 = 0;
  228. *odir = 0;
  229. _sdm2 = 1/r * 2 - 4;
  230. if (_sdm2 < 4) _sdm2 = 4;
  231. _sdm2_max = ceil(1/r * (31 + 2) - (255/256 + 255/65536 + 4));
  232. if (_sdm2_max > 8) _sdm2_max = 8;
  233. // explore up to 5 sdm2 values
  234. for (; _sdm2 < _sdm2_max; _sdm2++) {
  235. _odir = r * (_sdm2 + 4) - 2;
  236. if (_odir < 0) _odir = 0;
  237. else if (_odir > 31) _odir = 31;
  238. for (o = 0; o < 2 && _odir + o < 32; o++) {
  239. _sdm1 = 256*((_odir + o + 2) / r - (_sdm2 + 4));
  240. if (_sdm1 < 0) _sdm1 = 0;
  241. else if (_sdm1 > 255) _sdm1 = 255;
  242. for (s1 = 0; s1 < 2 && _sdm1 + s1 < 256; s1++) {
  243. _sdm0 = 65536*((_odir + o + 2) / r - (_sdm2 + (float) (_sdm1 + s1)/256 + 4));
  244. if (_sdm0 < 0) _sdm1 = 0;
  245. else if (_sdm0 > 255) _sdm0 = 255;
  246. for (s0 = 0; s0 < 2 && _sdm2 + s0 < 256; s0++) {
  247. int _fi2s = i2s_apll_get_fi2s(bits_per_sample, _sdm0 + s0, _sdm1 + s1, _sdm2, _odir + o);
  248. if (abs(_fi2s - rate) < prec) {
  249. prec = abs(_fi2s - rate);
  250. *sdm0 = _sdm0 + s0;
  251. *sdm1 = _sdm1 + s1;
  252. *sdm2 = _sdm2;
  253. *odir = _odir + o;
  254. }
  255. }
  256. }
  257. }
  258. }
  259. if (*sdm2 + *sdm0 + *sdm0 + *odir) return ESP_OK;
  260. else return ESP_ERR_INVALID_ARG;
  261. }
  262. esp_err_t i2s_set_clk(i2s_port_t i2s_num, uint32_t rate, i2s_bits_per_sample_t bits, i2s_channel_t ch)
  263. {
  264. int factor = (256%bits)? 384 : 256; // According to hardware codec requirement(supported 256fs or 384fs)
  265. int clkmInteger, clkmDecimals, bck = 0;
  266. double denom = (double)1 / 64;
  267. int channel = 2;
  268. i2s_dma_t *save_tx = NULL, *save_rx = NULL;
  269. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  270. if (bits % 8 != 0 || bits > I2S_BITS_PER_SAMPLE_32BIT || bits < I2S_BITS_PER_SAMPLE_16BIT) {
  271. ESP_LOGE(I2S_TAG, "Invalid bits per sample");
  272. return ESP_ERR_INVALID_ARG;
  273. }
  274. if (p_i2s_obj[i2s_num] == NULL) {
  275. ESP_LOGE(I2S_TAG, "Not initialized yet");
  276. return ESP_ERR_INVALID_ARG;
  277. }
  278. p_i2s_obj[i2s_num]->sample_rate = rate;
  279. /**
  280. * Due to hardware issue, bck division on ESP32/ESP32-S2 should be greater than 8 in slave mode
  281. * So the factor need to be an appropriate value
  282. */
  283. if ((p_i2s_obj[i2s_num]->mode & I2S_MODE_SLAVE) && !p_i2s_obj[i2s_num]->use_apll) {
  284. factor = 64 * bits;
  285. }
  286. double clkmdiv = (double)I2S_BASE_CLK / (rate * factor);
  287. if (clkmdiv > 256) {
  288. ESP_LOGE(I2S_TAG, "clkmdiv is too large\r\n");
  289. return ESP_ERR_INVALID_ARG;
  290. }
  291. // wait all on-going writing finish
  292. if ((p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) && p_i2s_obj[i2s_num]->tx) {
  293. xSemaphoreTake(p_i2s_obj[i2s_num]->tx->mux, (portTickType)portMAX_DELAY);
  294. }
  295. if ((p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) && p_i2s_obj[i2s_num]->rx) {
  296. xSemaphoreTake(p_i2s_obj[i2s_num]->rx->mux, (portTickType)portMAX_DELAY);
  297. }
  298. i2s_stop(i2s_num);
  299. #if SOC_I2S_SUPPORTS_ADC_DAC
  300. /* I2S-ADC only support single channel format. */
  301. if (!(p_i2s_obj[i2s_num]->mode & I2S_MODE_ADC_BUILT_IN)) {
  302. i2s_hal_set_rx_mode(&(p_i2s_obj[i2s_num]->hal), ch, bits);
  303. }
  304. #else
  305. i2s_hal_set_rx_mode(&(p_i2s_obj[i2s_num]->hal), ch, bits);
  306. #endif
  307. i2s_hal_set_tx_mode(&(p_i2s_obj[i2s_num]->hal), ch, bits);
  308. if (p_i2s_obj[i2s_num]->channel_num != (int)ch) {
  309. p_i2s_obj[i2s_num]->channel_num = (ch == 2) ? 2 : 1;
  310. }
  311. if ((int)bits != p_i2s_obj[i2s_num]->bits_per_sample) {
  312. p_i2s_obj[i2s_num]->bits_per_sample = bits;
  313. // Round bytes_per_sample up to next multiple of 16 bits
  314. int halfwords_per_sample = (bits + 15) / 16;
  315. p_i2s_obj[i2s_num]->bytes_per_sample = halfwords_per_sample * 2;
  316. // Because limited of DMA buffer is 4092 bytes
  317. if (p_i2s_obj[i2s_num]->dma_buf_len * p_i2s_obj[i2s_num]->bytes_per_sample * p_i2s_obj[i2s_num]->channel_num > 4092) {
  318. p_i2s_obj[i2s_num]->dma_buf_len = 4092 / p_i2s_obj[i2s_num]->bytes_per_sample / p_i2s_obj[i2s_num]->channel_num;
  319. }
  320. // Re-create TX DMA buffer
  321. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) {
  322. save_tx = p_i2s_obj[i2s_num]->tx;
  323. p_i2s_obj[i2s_num]->tx = i2s_create_dma_queue(i2s_num, p_i2s_obj[i2s_num]->dma_buf_count, p_i2s_obj[i2s_num]->dma_buf_len);
  324. if (p_i2s_obj[i2s_num]->tx == NULL) {
  325. ESP_LOGE(I2S_TAG, "Failed to create tx dma buffer");
  326. i2s_driver_uninstall(i2s_num);
  327. return ESP_ERR_NO_MEM;
  328. }
  329. i2s_hal_set_out_link_addr(&(p_i2s_obj[i2s_num]->hal), (uint32_t) p_i2s_obj[i2s_num]->tx->desc[0]);
  330. //destroy old tx dma if exist
  331. if (save_tx) {
  332. i2s_destroy_dma_queue(i2s_num, save_tx);
  333. }
  334. }
  335. // Re-create RX DMA buffer
  336. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) {
  337. save_rx = p_i2s_obj[i2s_num]->rx;
  338. p_i2s_obj[i2s_num]->rx = i2s_create_dma_queue(i2s_num, p_i2s_obj[i2s_num]->dma_buf_count, p_i2s_obj[i2s_num]->dma_buf_len);
  339. if (p_i2s_obj[i2s_num]->rx == NULL){
  340. ESP_LOGE(I2S_TAG, "Failed to create rx dma buffer");
  341. i2s_driver_uninstall(i2s_num);
  342. return ESP_ERR_NO_MEM;
  343. }
  344. i2s_hal_set_in_link(&(p_i2s_obj[i2s_num]->hal), p_i2s_obj[i2s_num]->dma_buf_len * p_i2s_obj[i2s_num]->channel_num * p_i2s_obj[i2s_num]->bytes_per_sample, (uint32_t) p_i2s_obj[i2s_num]->rx->desc[0]);
  345. //destroy old rx dma if exist
  346. if (save_rx) {
  347. i2s_destroy_dma_queue(i2s_num, save_rx);
  348. }
  349. }
  350. }
  351. double mclk;
  352. //int sdm0, sdm1, sdm2, odir, m_scale = 8;
  353. int sdm0, sdm1, sdm2, odir, m_scale = (rate > 96000 && bits > 16) ? 4 : 8;
  354. int fi2s_clk = rate*channel*bits*m_scale;
  355. #if SOC_I2S_SUPPORTS_ADC_DAC
  356. if (p_i2s_obj[i2s_num]->mode & (I2S_MODE_DAC_BUILT_IN | I2S_MODE_ADC_BUILT_IN)) {
  357. //DAC uses bclk as sample clock, not WS. WS can be something arbitrary.
  358. //Rate as given to this function is the intended sample rate;
  359. //According to the TRM, WS clk equals to the sample rate, and bclk is double the speed of WS
  360. uint32_t b_clk = rate * I2S_AD_BCK_FACTOR;
  361. fi2s_clk /= I2S_AD_BCK_FACTOR;
  362. int factor2 = 60;
  363. mclk = b_clk * factor2;
  364. clkmdiv = ((double) I2S_BASE_CLK) / mclk;
  365. clkmInteger = clkmdiv;
  366. clkmDecimals = (clkmdiv - clkmInteger) / denom;
  367. bck = mclk / b_clk;
  368. #endif
  369. #if SOC_I2S_SUPPORTS_PDM
  370. } else if (p_i2s_obj[i2s_num]->mode & I2S_MODE_PDM) {
  371. uint32_t b_clk = 0;
  372. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) {
  373. uint32_t fp, fs;
  374. i2s_hal_get_tx_pdm(&(p_i2s_obj[i2s_num]->hal), &fp, &fs);
  375. // Recommended set `fp = 960, fs = sample_rate / 100`
  376. fs = rate / 100;
  377. i2s_hal_tx_pdm_cfg(&(p_i2s_obj[i2s_num]->hal), fp, fs);
  378. b_clk = rate * I2S_PDM_BCK_FACTOR * fp / fs;
  379. } else if (p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) {
  380. uint32_t dsr;
  381. i2s_hal_get_rx_pdm(&(p_i2s_obj[i2s_num]->hal), &dsr);
  382. b_clk = rate * I2S_PDM_BCK_FACTOR * (dsr ? 2 : 1);
  383. }
  384. fi2s_clk = b_clk * m_scale;
  385. int factor2 = 5 ;
  386. mclk = b_clk * factor2;
  387. clkmdiv = ((double) I2S_BASE_CLK) / mclk;
  388. clkmInteger = clkmdiv;
  389. clkmDecimals = (clkmdiv - clkmInteger) / denom;
  390. bck = mclk / b_clk;
  391. } else
  392. #endif
  393. {
  394. clkmInteger = clkmdiv;
  395. clkmDecimals = (clkmdiv - clkmInteger) / denom;
  396. mclk = clkmInteger + denom * clkmDecimals;
  397. bck = factor/(bits * channel);
  398. }
  399. if(p_i2s_obj[i2s_num]->use_apll && p_i2s_obj[i2s_num]->fixed_mclk) {
  400. fi2s_clk = p_i2s_obj[i2s_num]->fixed_mclk;
  401. m_scale = fi2s_clk/bits/rate/channel;
  402. }
  403. if(p_i2s_obj[i2s_num]->use_apll && i2s_apll_calculate_fi2s(fi2s_clk, bits, &sdm0, &sdm1, &sdm2, &odir) == ESP_OK) {
  404. ESP_LOGD(I2S_TAG, "sdm0=%d, sdm1=%d, sdm2=%d, odir=%d", sdm0, sdm1, sdm2, odir);
  405. rtc_clk_apll_enable(1, sdm0, sdm1, sdm2, odir);
  406. i2s_hal_set_clk_div(&(p_i2s_obj[i2s_num]->hal), 1, 1, 0, m_scale, m_scale);
  407. i2s_hal_set_clock_sel(&(p_i2s_obj[i2s_num]->hal), I2S_CLK_APLL);
  408. double fi2s_rate = i2s_apll_get_fi2s(bits, sdm0, sdm1, sdm2, odir);
  409. p_i2s_obj[i2s_num]->real_rate = fi2s_rate/bits/channel/m_scale;
  410. ESP_LOGI(I2S_TAG, "APLL: Req RATE: %d, real rate: %0.3f, BITS: %u, CLKM: %u, BCK_M: %u, MCLK: %0.3f, SCLK: %f, diva: %d, divb: %d",
  411. rate, fi2s_rate/bits/channel/m_scale, bits, 1, m_scale, fi2s_rate, fi2s_rate/8, 1, 0);
  412. } else {
  413. i2s_hal_set_clock_sel(&(p_i2s_obj[i2s_num]->hal), I2S_CLK_D2CLK);
  414. i2s_hal_set_clk_div(&(p_i2s_obj[i2s_num]->hal), clkmInteger, 63, clkmDecimals, bck, bck);
  415. double real_rate = (double) (I2S_BASE_CLK / (bck * bits * clkmInteger) / 2);
  416. p_i2s_obj[i2s_num]->real_rate = real_rate;
  417. ESP_LOGI(I2S_TAG, "PLL_D2: Req RATE: %d, real rate: %0.3f, BITS: %u, CLKM: %u, BCK: %u, MCLK: %0.3f, SCLK: %f, diva: %d, divb: %d",
  418. rate, real_rate, bits, clkmInteger, bck, (double)I2S_BASE_CLK / mclk, real_rate*bits*channel, 64, clkmDecimals);
  419. }
  420. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) {
  421. p_i2s_obj[i2s_num]->tx->curr_ptr = NULL;
  422. p_i2s_obj[i2s_num]->tx->rw_pos = 0;
  423. }
  424. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) {
  425. p_i2s_obj[i2s_num]->rx->curr_ptr = NULL;
  426. p_i2s_obj[i2s_num]->rx->rw_pos = 0;
  427. }
  428. i2s_hal_set_tx_bits_mod(&(p_i2s_obj[i2s_num]->hal), bits);
  429. i2s_hal_set_rx_bits_mod(&(p_i2s_obj[i2s_num]->hal), bits);
  430. // wait all writing on-going finish
  431. if ((p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) && p_i2s_obj[i2s_num]->tx) {
  432. xSemaphoreGive(p_i2s_obj[i2s_num]->tx->mux);
  433. }
  434. if ((p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) && p_i2s_obj[i2s_num]->rx) {
  435. xSemaphoreGive(p_i2s_obj[i2s_num]->rx->mux);
  436. }
  437. i2s_start(i2s_num);
  438. return ESP_OK;
  439. }
  440. static void IRAM_ATTR i2s_intr_handler_default(void *arg)
  441. {
  442. i2s_obj_t *p_i2s = (i2s_obj_t*) arg;
  443. uint32_t status;
  444. i2s_hal_get_intr_status(&(p_i2s->hal), &status);
  445. if(status == 0) {
  446. //Avoid spurious interrupt
  447. return;
  448. }
  449. i2s_event_t i2s_event;
  450. int dummy;
  451. portBASE_TYPE high_priority_task_awoken = 0;
  452. lldesc_t *finish_desc = NULL;
  453. if ((status & I2S_INTR_OUT_DSCR_ERR) || (status & I2S_INTR_IN_DSCR_ERR)) {
  454. ESP_EARLY_LOGE(I2S_TAG, "dma error, interrupt status: 0x%08x", status);
  455. if (p_i2s->i2s_queue) {
  456. i2s_event.type = I2S_EVENT_DMA_ERROR;
  457. if (xQueueIsQueueFullFromISR(p_i2s->i2s_queue)) {
  458. xQueueReceiveFromISR(p_i2s->i2s_queue, &dummy, &high_priority_task_awoken);
  459. }
  460. xQueueSendFromISR(p_i2s->i2s_queue, (void * )&i2s_event, &high_priority_task_awoken);
  461. }
  462. }
  463. if ((status & I2S_INTR_OUT_EOF) && p_i2s->tx) {
  464. i2s_hal_get_out_eof_des_addr(&(p_i2s->hal), (uint32_t *)&finish_desc);
  465. // All buffers are empty. This means we have an underflow on our hands.
  466. if (xQueueIsQueueFullFromISR(p_i2s->tx->queue)) {
  467. xQueueReceiveFromISR(p_i2s->tx->queue, &dummy, &high_priority_task_awoken);
  468. // See if tx descriptor needs to be auto cleared:
  469. // This will avoid any kind of noise that may get introduced due to transmission
  470. // of previous data from tx descriptor on I2S line.
  471. if (p_i2s->tx_desc_auto_clear == true) {
  472. memset((void *) dummy, 0, p_i2s->tx->buf_size);
  473. }
  474. }
  475. xQueueSendFromISR(p_i2s->tx->queue, (void*)(&finish_desc->buf), &high_priority_task_awoken);
  476. if (p_i2s->i2s_queue) {
  477. i2s_event.type = I2S_EVENT_TX_DONE;
  478. if (xQueueIsQueueFullFromISR(p_i2s->i2s_queue)) {
  479. xQueueReceiveFromISR(p_i2s->i2s_queue, &dummy, &high_priority_task_awoken);
  480. }
  481. xQueueSendFromISR(p_i2s->i2s_queue, (void * )&i2s_event, &high_priority_task_awoken);
  482. }
  483. }
  484. if ((status & I2S_INTR_IN_SUC_EOF) && p_i2s->rx) {
  485. // All buffers are full. This means we have an overflow.
  486. i2s_hal_get_in_eof_des_addr(&(p_i2s->hal), (uint32_t *)&finish_desc);
  487. if (xQueueIsQueueFullFromISR(p_i2s->rx->queue)) {
  488. xQueueReceiveFromISR(p_i2s->rx->queue, &dummy, &high_priority_task_awoken);
  489. }
  490. xQueueSendFromISR(p_i2s->rx->queue, (void*)(&finish_desc->buf), &high_priority_task_awoken);
  491. if (p_i2s->i2s_queue) {
  492. i2s_event.type = I2S_EVENT_RX_DONE;
  493. if (p_i2s->i2s_queue && xQueueIsQueueFullFromISR(p_i2s->i2s_queue)) {
  494. xQueueReceiveFromISR(p_i2s->i2s_queue, &dummy, &high_priority_task_awoken);
  495. }
  496. xQueueSendFromISR(p_i2s->i2s_queue, (void * )&i2s_event, &high_priority_task_awoken);
  497. }
  498. }
  499. i2s_hal_clear_intr_status(&(p_i2s->hal), status);
  500. if (high_priority_task_awoken == pdTRUE) {
  501. portYIELD_FROM_ISR();
  502. }
  503. }
  504. static esp_err_t i2s_destroy_dma_queue(i2s_port_t i2s_num, i2s_dma_t *dma)
  505. {
  506. int bux_idx;
  507. if (p_i2s_obj[i2s_num] == NULL) {
  508. ESP_LOGE(I2S_TAG, "Not initialized yet");
  509. return ESP_ERR_INVALID_ARG;
  510. }
  511. if (dma == NULL) {
  512. ESP_LOGE(I2S_TAG, "dma is NULL");
  513. return ESP_ERR_INVALID_ARG;
  514. }
  515. for (bux_idx = 0; bux_idx < p_i2s_obj[i2s_num]->dma_buf_count; bux_idx++) {
  516. if (dma->desc && dma->desc[bux_idx]) {
  517. free(dma->desc[bux_idx]);
  518. }
  519. if (dma->buf && dma->buf[bux_idx]) {
  520. free(dma->buf[bux_idx]);
  521. }
  522. }
  523. if (dma->buf) {
  524. free(dma->buf);
  525. }
  526. if (dma->desc) {
  527. free(dma->desc);
  528. }
  529. vQueueDelete(dma->queue);
  530. vSemaphoreDelete(dma->mux);
  531. free(dma);
  532. return ESP_OK;
  533. }
  534. static i2s_dma_t *i2s_create_dma_queue(i2s_port_t i2s_num, int dma_buf_count, int dma_buf_len)
  535. {
  536. int bux_idx;
  537. int sample_size = p_i2s_obj[i2s_num]->bytes_per_sample * p_i2s_obj[i2s_num]->channel_num;
  538. i2s_dma_t *dma = (i2s_dma_t*) malloc(sizeof(i2s_dma_t));
  539. if (dma == NULL) {
  540. ESP_LOGE(I2S_TAG, "Error malloc i2s_dma_t");
  541. return NULL;
  542. }
  543. memset(dma, 0, sizeof(i2s_dma_t));
  544. dma->buf = (char **)malloc(sizeof(char*) * dma_buf_count);
  545. if (dma->buf == NULL) {
  546. ESP_LOGE(I2S_TAG, "Error malloc dma buffer pointer");
  547. free(dma);
  548. return NULL;
  549. }
  550. memset(dma->buf, 0, sizeof(char*) * dma_buf_count);
  551. for (bux_idx = 0; bux_idx < dma_buf_count; bux_idx++) {
  552. dma->buf[bux_idx] = (char*) heap_caps_calloc(1, dma_buf_len * sample_size, MALLOC_CAP_DMA);
  553. if (dma->buf[bux_idx] == NULL) {
  554. ESP_LOGE(I2S_TAG, "Error malloc dma buffer");
  555. i2s_destroy_dma_queue(i2s_num, dma);
  556. return NULL;
  557. }
  558. ESP_LOGD(I2S_TAG, "Addr[%d] = %d", bux_idx, (int)dma->buf[bux_idx]);
  559. }
  560. dma->desc = (lldesc_t**) malloc(sizeof(lldesc_t*) * dma_buf_count);
  561. if (dma->desc == NULL) {
  562. ESP_LOGE(I2S_TAG, "Error malloc dma description");
  563. i2s_destroy_dma_queue(i2s_num, dma);
  564. return NULL;
  565. }
  566. for (bux_idx = 0; bux_idx < dma_buf_count; bux_idx++) {
  567. dma->desc[bux_idx] = (lldesc_t*) heap_caps_malloc(sizeof(lldesc_t), MALLOC_CAP_DMA);
  568. if (dma->desc[bux_idx] == NULL) {
  569. ESP_LOGE(I2S_TAG, "Error malloc dma description entry");
  570. i2s_destroy_dma_queue(i2s_num, dma);
  571. return NULL;
  572. }
  573. }
  574. for (bux_idx = 0; bux_idx < dma_buf_count; bux_idx++) {
  575. dma->desc[bux_idx]->owner = 1;
  576. dma->desc[bux_idx]->eof = 1;
  577. dma->desc[bux_idx]->sosf = 0;
  578. dma->desc[bux_idx]->length = dma_buf_len * sample_size;
  579. dma->desc[bux_idx]->size = dma_buf_len * sample_size;
  580. dma->desc[bux_idx]->buf = (uint8_t *) dma->buf[bux_idx];
  581. dma->desc[bux_idx]->offset = 0;
  582. dma->desc[bux_idx]->empty = (uint32_t)((bux_idx < (dma_buf_count - 1)) ? (dma->desc[bux_idx + 1]) : dma->desc[0]);
  583. }
  584. dma->queue = xQueueCreate(dma_buf_count - 1, sizeof(char*));
  585. dma->mux = xSemaphoreCreateMutex();
  586. dma->buf_size = dma_buf_len * sample_size;
  587. ESP_LOGI(I2S_TAG, "DMA Malloc info, datalen=blocksize=%d, dma_buf_count=%d", dma_buf_len * sample_size, dma_buf_count);
  588. return dma;
  589. }
  590. esp_err_t i2s_start(i2s_port_t i2s_num)
  591. {
  592. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  593. //start DMA link
  594. I2S_ENTER_CRITICAL();
  595. i2s_hal_reset(&(p_i2s_obj[i2s_num]->hal));
  596. esp_intr_disable(p_i2s_obj[i2s_num]->i2s_isr_handle);
  597. i2s_hal_clear_intr_status(&(p_i2s_obj[i2s_num]->hal), I2S_INTR_MAX);
  598. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) {
  599. i2s_enable_tx_intr(i2s_num);
  600. i2s_hal_start_tx(&(p_i2s_obj[i2s_num]->hal));
  601. }
  602. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) {
  603. i2s_enable_rx_intr(i2s_num);
  604. i2s_hal_start_rx(&(p_i2s_obj[i2s_num]->hal));
  605. }
  606. esp_intr_enable(p_i2s_obj[i2s_num]->i2s_isr_handle);
  607. I2S_EXIT_CRITICAL();
  608. return ESP_OK;
  609. }
  610. esp_err_t i2s_stop(i2s_port_t i2s_num)
  611. {
  612. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  613. I2S_ENTER_CRITICAL();
  614. esp_intr_disable(p_i2s_obj[i2s_num]->i2s_isr_handle);
  615. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) {
  616. i2s_hal_stop_tx(&(p_i2s_obj[i2s_num]->hal));
  617. i2s_disable_tx_intr(i2s_num);
  618. }
  619. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) {
  620. i2s_hal_stop_rx(&(p_i2s_obj[i2s_num]->hal));
  621. i2s_disable_rx_intr(i2s_num);
  622. }
  623. uint32_t mask;
  624. i2s_hal_get_intr_status(&(p_i2s_obj[i2s_num]->hal), &mask);
  625. i2s_hal_clear_intr_status(&(p_i2s_obj[i2s_num]->hal), mask);
  626. I2S_EXIT_CRITICAL();
  627. return ESP_OK;
  628. }
  629. /*
  630. * When a panic occurs during playback, the I2S interface can produce a loud noise burst.
  631. * This code runs just before the system panic handler to "emergency stop" the I2S iterface
  632. * to prevent the noise burst from happening. Note that when this code is called the system
  633. * has already crashed, so no need to disable interrupts, acquire locks, or otherwise be nice.
  634. *
  635. * This code makes use of the linker --wrap feature to intercept the call to esp_panic_handler.
  636. */
  637. void __real_esp_panic_handler(void*);
  638. void __wrap_esp_panic_handler (void* info) {
  639. esp_rom_printf("I2S abort!\r\n");
  640. i2s_hal_stop_tx(&(p_i2s_obj[CONFIG_I2S_NUM]->hal));
  641. /* Call the original panic handler function to finish processing this error */
  642. __real_esp_panic_handler(info);
  643. }
  644. #if SOC_I2S_SUPPORTS_ADC_DAC
  645. esp_err_t i2s_set_dac_mode(i2s_dac_mode_t dac_mode)
  646. {
  647. I2S_CHECK((dac_mode < I2S_DAC_CHANNEL_MAX), "i2s dac mode error", ESP_ERR_INVALID_ARG);
  648. if (dac_mode == I2S_DAC_CHANNEL_DISABLE) {
  649. dac_output_disable(DAC_CHANNEL_1);
  650. dac_output_disable(DAC_CHANNEL_2);
  651. dac_i2s_disable();
  652. } else {
  653. dac_i2s_enable();
  654. }
  655. if (dac_mode & I2S_DAC_CHANNEL_RIGHT_EN) {
  656. //DAC1, right channel
  657. dac_output_enable(DAC_CHANNEL_1);
  658. }
  659. if (dac_mode & I2S_DAC_CHANNEL_LEFT_EN) {
  660. //DAC2, left channel
  661. dac_output_enable(DAC_CHANNEL_2);
  662. }
  663. return ESP_OK;
  664. }
  665. static esp_err_t _i2s_adc_mode_recover(void)
  666. {
  667. I2S_CHECK(((_i2s_adc_unit != -1) && (_i2s_adc_channel != -1)), "i2s ADC recover error, not initialized...", ESP_ERR_INVALID_ARG);
  668. return adc_i2s_mode_init(_i2s_adc_unit, _i2s_adc_channel);
  669. }
  670. esp_err_t i2s_set_adc_mode(adc_unit_t adc_unit, adc1_channel_t adc_channel)
  671. {
  672. I2S_CHECK((adc_unit < ADC_UNIT_2), "i2s ADC unit error, only support ADC1 for now", ESP_ERR_INVALID_ARG);
  673. // For now, we only support SAR ADC1.
  674. _i2s_adc_unit = adc_unit;
  675. _i2s_adc_channel = adc_channel;
  676. return adc_i2s_mode_init(adc_unit, adc_channel);
  677. }
  678. #endif
  679. esp_err_t i2s_set_pin(i2s_port_t i2s_num, const i2s_pin_config_t *pin)
  680. {
  681. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  682. if (pin == NULL) {
  683. #if SOC_I2S_SUPPORTS_ADC_DAC
  684. return i2s_set_dac_mode(I2S_DAC_CHANNEL_BOTH_EN);
  685. #else
  686. return ESP_ERR_INVALID_ARG;
  687. #endif
  688. }
  689. if (pin->bck_io_num != -1 && !GPIO_IS_VALID_GPIO(pin->bck_io_num)) {
  690. ESP_LOGE(I2S_TAG, "bck_io_num error");
  691. return ESP_FAIL;
  692. }
  693. if (pin->ws_io_num != -1 && !GPIO_IS_VALID_GPIO(pin->ws_io_num)) {
  694. ESP_LOGE(I2S_TAG, "ws_io_num error");
  695. return ESP_FAIL;
  696. }
  697. if (pin->data_out_num != -1 && !GPIO_IS_VALID_OUTPUT_GPIO(pin->data_out_num)) {
  698. ESP_LOGE(I2S_TAG, "data_out_num error");
  699. return ESP_FAIL;
  700. }
  701. if (pin->data_in_num != -1 && !GPIO_IS_VALID_GPIO(pin->data_in_num)) {
  702. ESP_LOGE(I2S_TAG, "data_in_num error");
  703. return ESP_FAIL;
  704. }
  705. int bck_sig = -1, ws_sig = -1, data_out_sig = -1, data_in_sig = -1;
  706. //Each IIS hw module has a RX and TX unit.
  707. //For TX unit, the output signal index should be I2SnO_xxx_OUT_IDX
  708. //For TX unit, the input signal index should be I2SnO_xxx_IN_IDX
  709. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) {
  710. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_MASTER) {
  711. bck_sig = i2s_periph_signal[i2s_num].o_bck_out_sig;
  712. ws_sig = i2s_periph_signal[i2s_num].o_ws_out_sig;
  713. data_out_sig = i2s_periph_signal[i2s_num].o_data_out_sig;
  714. } else if (p_i2s_obj[i2s_num]->mode & I2S_MODE_SLAVE) {
  715. bck_sig = i2s_periph_signal[i2s_num].o_bck_in_sig;
  716. ws_sig = i2s_periph_signal[i2s_num].o_ws_in_sig;
  717. data_out_sig = i2s_periph_signal[i2s_num].o_data_out_sig;
  718. }
  719. }
  720. //For RX unit, the output signal index should be I2SnI_xxx_OUT_IDX
  721. //For RX unit, the input signal index shuld be I2SnI_xxx_IN_IDX
  722. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) {
  723. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_MASTER) {
  724. bck_sig = i2s_periph_signal[i2s_num].i_bck_out_sig;
  725. ws_sig = i2s_periph_signal[i2s_num].i_ws_out_sig;
  726. data_in_sig = i2s_periph_signal[i2s_num].i_data_in_sig;
  727. } else if (p_i2s_obj[i2s_num]->mode & I2S_MODE_SLAVE) {
  728. bck_sig = i2s_periph_signal[i2s_num].i_bck_in_sig;
  729. ws_sig = i2s_periph_signal[i2s_num].i_ws_in_sig;
  730. data_in_sig = i2s_periph_signal[i2s_num].i_data_in_sig;
  731. }
  732. }
  733. //For "full-duplex + slave" mode, we should select RX signal index for ws and bck.
  734. //For "full-duplex + master" mode, we should select TX signal index for ws and bck.
  735. if ((p_i2s_obj[i2s_num]->mode & I2S_FULL_DUPLEX_SLAVE_MODE_MASK) == I2S_FULL_DUPLEX_SLAVE_MODE_MASK) {
  736. bck_sig = i2s_periph_signal[i2s_num].i_bck_in_sig;
  737. ws_sig = i2s_periph_signal[i2s_num].i_ws_in_sig;
  738. } else if ((p_i2s_obj[i2s_num]->mode & I2S_FULL_DUPLEX_MASTER_MODE_MASK) == I2S_FULL_DUPLEX_MASTER_MODE_MASK) {
  739. bck_sig = i2s_periph_signal[i2s_num].o_bck_out_sig;
  740. ws_sig = i2s_periph_signal[i2s_num].o_ws_out_sig;
  741. }
  742. gpio_matrix_out_check(pin->data_out_num, data_out_sig, 0, 0);
  743. gpio_matrix_in_check(pin->data_in_num, data_in_sig, 0);
  744. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_MASTER) {
  745. gpio_matrix_out_check(pin->ws_io_num, ws_sig, 0, 0);
  746. gpio_matrix_out_check(pin->bck_io_num, bck_sig, 0, 0);
  747. } else if (p_i2s_obj[i2s_num]->mode & I2S_MODE_SLAVE) {
  748. gpio_matrix_in_check(pin->ws_io_num, ws_sig, 0);
  749. gpio_matrix_in_check(pin->bck_io_num, bck_sig, 0);
  750. }
  751. ESP_LOGD(I2S_TAG, "data: out %d, in: %d, ws: %d, bck: %d", data_out_sig, data_in_sig, ws_sig, bck_sig);
  752. return ESP_OK;
  753. }
  754. esp_err_t i2s_set_sample_rates(i2s_port_t i2s_num, uint32_t rate)
  755. {
  756. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  757. I2S_CHECK((p_i2s_obj[i2s_num]->bytes_per_sample > 0), "bits_per_sample not set", ESP_ERR_INVALID_ARG);
  758. return i2s_set_clk(i2s_num, rate, p_i2s_obj[i2s_num]->bits_per_sample, p_i2s_obj[i2s_num]->channel_num);
  759. }
  760. #if SOC_I2S_SUPPORTS_PDM
  761. esp_err_t i2s_set_pdm_rx_down_sample(i2s_port_t i2s_num, i2s_pdm_dsr_t dsr)
  762. {
  763. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  764. i2s_hal_rx_pdm_cfg(&(p_i2s_obj[i2s_num]->hal), dsr);
  765. return i2s_set_clk(i2s_num, p_i2s_obj[i2s_num]->sample_rate, p_i2s_obj[i2s_num]->bits_per_sample, p_i2s_obj[i2s_num]->channel_num);
  766. }
  767. #endif
  768. static esp_err_t i2s_check_cfg_static(i2s_port_t i2s_num, const i2s_config_t *cfg)
  769. {
  770. #if SOC_I2S_SUPPORTS_ADC_DAC
  771. //We only check if the I2S number is invalid when set to build in ADC and DAC mode.
  772. I2S_CHECK(!((cfg->mode & I2S_MODE_ADC_BUILT_IN) && (i2s_num != I2S_NUM_0)), "I2S ADC built-in only support on I2S0", ESP_ERR_INVALID_ARG);
  773. I2S_CHECK(!((cfg->mode & I2S_MODE_DAC_BUILT_IN) && (i2s_num != I2S_NUM_0)), "I2S DAC built-in only support on I2S0", ESP_ERR_INVALID_ARG);
  774. return ESP_OK;
  775. #endif
  776. #if SOC_I2S_SUPPORTS_PDM
  777. //We only check if the I2S number is invalid when set to PDM mode.
  778. I2S_CHECK(!((cfg->mode & I2S_MODE_PDM) && (i2s_num != I2S_NUM_0)), "I2S DAC PDM only support on I2S0", ESP_ERR_INVALID_ARG);
  779. return ESP_OK;
  780. #endif
  781. I2S_CHECK(cfg->communication_format && (cfg->communication_format < I2S_COMM_FORMAT_STAND_MAX), "invalid communication formats", ESP_ERR_INVALID_ARG);
  782. I2S_CHECK(!((cfg->communication_format & I2S_COMM_FORMAT_STAND_MSB) && (cfg->communication_format & I2S_COMM_FORMAT_STAND_PCM_LONG)), "multiple communication formats specified", ESP_ERR_INVALID_ARG);
  783. return ESP_OK;
  784. }
  785. static esp_err_t i2s_param_config(i2s_port_t i2s_num, const i2s_config_t *i2s_config)
  786. {
  787. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  788. I2S_CHECK((i2s_config), "param null", ESP_ERR_INVALID_ARG);
  789. I2S_CHECK((i2s_check_cfg_static(i2s_num, i2s_config) == ESP_OK), "param check error", ESP_ERR_INVALID_ARG);
  790. #if SOC_I2S_SUPPORTS_ADC_DAC
  791. if(i2s_config->mode & I2S_MODE_ADC_BUILT_IN) {
  792. //in ADC built-in mode, we need to call i2s_set_adc_mode to
  793. //initialize the specific ADC channel.
  794. //in the current stage, we only support ADC1 and single channel mode.
  795. //In default data mode, the ADC data is in 12-bit resolution mode.
  796. adc_power_acquire();
  797. }
  798. #endif
  799. // configure I2S data port interface.
  800. i2s_hal_config_param(&(p_i2s_obj[i2s_num]->hal), i2s_config);
  801. if ((p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) && (p_i2s_obj[i2s_num]->mode & I2S_MODE_TX)) {
  802. i2s_hal_enable_sig_loopback(&(p_i2s_obj[i2s_num]->hal));
  803. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_MASTER) {
  804. i2s_hal_enable_master_mode(&(p_i2s_obj[i2s_num]->hal));
  805. } else {
  806. i2s_hal_enable_slave_mode(&(p_i2s_obj[i2s_num]->hal));
  807. }
  808. }
  809. p_i2s_obj[i2s_num]->use_apll = i2s_config->use_apll;
  810. p_i2s_obj[i2s_num]->tx_desc_auto_clear = i2s_config->tx_desc_auto_clear;
  811. p_i2s_obj[i2s_num]->fixed_mclk = i2s_config->fixed_mclk;
  812. return ESP_OK;
  813. }
  814. esp_err_t i2s_zero_dma_buffer(i2s_port_t i2s_num)
  815. {
  816. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  817. if (p_i2s_obj[i2s_num]->rx && p_i2s_obj[i2s_num]->rx->buf != NULL && p_i2s_obj[i2s_num]->rx->buf_size != 0) {
  818. for (int i = 0; i < p_i2s_obj[i2s_num]->dma_buf_count; i++) {
  819. memset(p_i2s_obj[i2s_num]->rx->buf[i], 0, p_i2s_obj[i2s_num]->rx->buf_size);
  820. }
  821. }
  822. if (p_i2s_obj[i2s_num]->tx && p_i2s_obj[i2s_num]->tx->buf != NULL && p_i2s_obj[i2s_num]->tx->buf_size != 0) {
  823. int bytes_left = 0;
  824. bytes_left = (p_i2s_obj[i2s_num]->tx->buf_size - p_i2s_obj[i2s_num]->tx->rw_pos) % 4;
  825. if (bytes_left) {
  826. size_t zero_bytes = 0, bytes_written;
  827. i2s_write(i2s_num, (void *)&zero_bytes, bytes_left, &bytes_written, portMAX_DELAY);
  828. }
  829. for (int i = 0; i < p_i2s_obj[i2s_num]->dma_buf_count; i++) {
  830. memset(p_i2s_obj[i2s_num]->tx->buf[i], 0, p_i2s_obj[i2s_num]->tx->buf_size);
  831. }
  832. }
  833. return ESP_OK;
  834. }
  835. esp_err_t i2s_driver_install(i2s_port_t i2s_num, const i2s_config_t *i2s_config, int queue_size, void* i2s_queue)
  836. {
  837. esp_err_t err;
  838. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  839. I2S_CHECK((i2s_config != NULL), "I2S configuration must not NULL", ESP_ERR_INVALID_ARG);
  840. I2S_CHECK((i2s_config->dma_buf_count >= 2 && i2s_config->dma_buf_count <= 128), "I2S buffer count less than 128 and more than 2", ESP_ERR_INVALID_ARG);
  841. I2S_CHECK((i2s_config->dma_buf_len >= 8 && i2s_config->dma_buf_len <= 1024), "I2S buffer length at most 1024 and more than 8", ESP_ERR_INVALID_ARG);
  842. if (p_i2s_obj[i2s_num] == NULL) {
  843. p_i2s_obj[i2s_num] = (i2s_obj_t*) malloc(sizeof(i2s_obj_t));
  844. if (p_i2s_obj[i2s_num] == NULL) {
  845. ESP_LOGE(I2S_TAG, "Malloc I2S driver error");
  846. return ESP_ERR_NO_MEM;
  847. }
  848. memset(p_i2s_obj[i2s_num], 0, sizeof(i2s_obj_t));
  849. portMUX_TYPE i2s_spinlock_unlocked[1] = {portMUX_INITIALIZER_UNLOCKED};
  850. for (int x = 0; x < I2S_NUM_MAX; x++) {
  851. i2s_spinlock[x] = i2s_spinlock_unlocked[0];
  852. }
  853. //To make sure hardware is enabled before any hardware register operations.
  854. periph_module_enable(i2s_periph_signal[i2s_num].module);
  855. i2s_hal_init(&(p_i2s_obj[i2s_num]->hal), i2s_num);
  856. p_i2s_obj[i2s_num]->i2s_num = i2s_num;
  857. p_i2s_obj[i2s_num]->dma_buf_count = i2s_config->dma_buf_count;
  858. p_i2s_obj[i2s_num]->dma_buf_len = i2s_config->dma_buf_len;
  859. p_i2s_obj[i2s_num]->i2s_queue = i2s_queue;
  860. p_i2s_obj[i2s_num]->mode = i2s_config->mode;
  861. p_i2s_obj[i2s_num]->bits_per_sample = 0;
  862. p_i2s_obj[i2s_num]->bytes_per_sample = 0; // Not initialized yet
  863. p_i2s_obj[i2s_num]->channel_num = i2s_config->channel_format < I2S_CHANNEL_FMT_ONLY_RIGHT ? 2 : 1;
  864. #ifdef CONFIG_PM_ENABLE
  865. if (i2s_config->use_apll) {
  866. err = esp_pm_lock_create(ESP_PM_NO_LIGHT_SLEEP, 0, "i2s_driver", &p_i2s_obj[i2s_num]->pm_lock);
  867. } else {
  868. err = esp_pm_lock_create(ESP_PM_APB_FREQ_MAX, 0, "i2s_driver", &p_i2s_obj[i2s_num]->pm_lock);
  869. }
  870. if (err != ESP_OK) {
  871. free(p_i2s_obj[i2s_num]);
  872. p_i2s_obj[i2s_num] = NULL;
  873. ESP_LOGE(I2S_TAG, "I2S pm lock error");
  874. return err;
  875. }
  876. #endif //CONFIG_PM_ENABLE
  877. //initial interrupt
  878. err = i2s_isr_register(i2s_num, i2s_config->intr_alloc_flags, i2s_intr_handler_default, p_i2s_obj[i2s_num], &p_i2s_obj[i2s_num]->i2s_isr_handle);
  879. if (err != ESP_OK) {
  880. #ifdef CONFIG_PM_ENABLE
  881. if (p_i2s_obj[i2s_num]->pm_lock) {
  882. esp_pm_lock_delete(p_i2s_obj[i2s_num]->pm_lock);
  883. }
  884. #endif
  885. free(p_i2s_obj[i2s_num]);
  886. p_i2s_obj[i2s_num] = NULL;
  887. ESP_LOGE(I2S_TAG, "Register I2S Interrupt error");
  888. return err;
  889. }
  890. i2s_stop(i2s_num);
  891. err = i2s_param_config(i2s_num, i2s_config);
  892. if (err != ESP_OK) {
  893. i2s_driver_uninstall(i2s_num);
  894. ESP_LOGE(I2S_TAG, "I2S param configure error");
  895. return err;
  896. }
  897. if (i2s_queue) {
  898. p_i2s_obj[i2s_num]->i2s_queue = xQueueCreate(queue_size, sizeof(i2s_event_t));
  899. *((QueueHandle_t*) i2s_queue) = p_i2s_obj[i2s_num]->i2s_queue;
  900. ESP_LOGI(I2S_TAG, "queue free spaces: %d", uxQueueSpacesAvailable(p_i2s_obj[i2s_num]->i2s_queue));
  901. } else {
  902. p_i2s_obj[i2s_num]->i2s_queue = NULL;
  903. }
  904. //set clock and start
  905. return i2s_set_clk(i2s_num, i2s_config->sample_rate, i2s_config->bits_per_sample, p_i2s_obj[i2s_num]->channel_num);
  906. }
  907. ESP_LOGW(I2S_TAG, "I2S driver already installed");
  908. return ESP_OK;
  909. }
  910. esp_err_t i2s_driver_uninstall(i2s_port_t i2s_num)
  911. {
  912. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  913. if (p_i2s_obj[i2s_num] == NULL) {
  914. ESP_LOGI(I2S_TAG, "already uninstalled");
  915. return ESP_OK;
  916. }
  917. i2s_stop(i2s_num);
  918. esp_intr_free(p_i2s_obj[i2s_num]->i2s_isr_handle);
  919. if (p_i2s_obj[i2s_num]->tx != NULL && p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) {
  920. i2s_destroy_dma_queue(i2s_num, p_i2s_obj[i2s_num]->tx);
  921. p_i2s_obj[i2s_num]->tx = NULL;
  922. }
  923. if (p_i2s_obj[i2s_num]->rx != NULL && p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) {
  924. i2s_destroy_dma_queue(i2s_num, p_i2s_obj[i2s_num]->rx);
  925. p_i2s_obj[i2s_num]->rx = NULL;
  926. }
  927. if (p_i2s_obj[i2s_num]->i2s_queue) {
  928. vQueueDelete(p_i2s_obj[i2s_num]->i2s_queue);
  929. p_i2s_obj[i2s_num]->i2s_queue = NULL;
  930. }
  931. if(p_i2s_obj[i2s_num]->use_apll) {
  932. // switch back to PLL clock source
  933. i2s_hal_set_clock_sel(&(p_i2s_obj[i2s_num]->hal), I2S_CLK_D2CLK);
  934. rtc_clk_apll_enable(0, 0, 0, 0, 0);
  935. }
  936. #ifdef CONFIG_PM_ENABLE
  937. if (p_i2s_obj[i2s_num]->pm_lock) {
  938. esp_pm_lock_delete(p_i2s_obj[i2s_num]->pm_lock);
  939. }
  940. #endif
  941. free(p_i2s_obj[i2s_num]);
  942. p_i2s_obj[i2s_num] = NULL;
  943. periph_module_disable(i2s_periph_signal[i2s_num].module);
  944. return ESP_OK;
  945. }
  946. esp_err_t i2s_write(i2s_port_t i2s_num, const void *src, size_t size, size_t *bytes_written, TickType_t ticks_to_wait)
  947. {
  948. char *data_ptr, *src_byte;
  949. size_t bytes_can_write;
  950. *bytes_written = 0;
  951. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  952. I2S_CHECK((size < SOC_I2S_MAX_BUFFER_SIZE), "size is too large", ESP_ERR_INVALID_ARG);
  953. I2S_CHECK((p_i2s_obj[i2s_num]->tx), "tx NULL", ESP_ERR_INVALID_ARG);
  954. xSemaphoreTake(p_i2s_obj[i2s_num]->tx->mux, (portTickType)portMAX_DELAY);
  955. #ifdef CONFIG_PM_ENABLE
  956. esp_pm_lock_acquire(p_i2s_obj[i2s_num]->pm_lock);
  957. #endif
  958. src_byte = (char *)src;
  959. while (size > 0) {
  960. if (p_i2s_obj[i2s_num]->tx->rw_pos == p_i2s_obj[i2s_num]->tx->buf_size || p_i2s_obj[i2s_num]->tx->curr_ptr == NULL) {
  961. if (xQueueReceive(p_i2s_obj[i2s_num]->tx->queue, &p_i2s_obj[i2s_num]->tx->curr_ptr, ticks_to_wait) == pdFALSE) {
  962. break;
  963. }
  964. p_i2s_obj[i2s_num]->tx->rw_pos = 0;
  965. }
  966. ESP_LOGD(I2S_TAG, "size: %d, rw_pos: %d, buf_size: %d, curr_ptr: %d", size, p_i2s_obj[i2s_num]->tx->rw_pos, p_i2s_obj[i2s_num]->tx->buf_size, (int)p_i2s_obj[i2s_num]->tx->curr_ptr);
  967. data_ptr = (char*)p_i2s_obj[i2s_num]->tx->curr_ptr;
  968. data_ptr += p_i2s_obj[i2s_num]->tx->rw_pos;
  969. bytes_can_write = p_i2s_obj[i2s_num]->tx->buf_size - p_i2s_obj[i2s_num]->tx->rw_pos;
  970. if (bytes_can_write > size) {
  971. bytes_can_write = size;
  972. }
  973. memcpy(data_ptr, src_byte, bytes_can_write);
  974. size -= bytes_can_write;
  975. src_byte += bytes_can_write;
  976. p_i2s_obj[i2s_num]->tx->rw_pos += bytes_can_write;
  977. (*bytes_written) += bytes_can_write;
  978. }
  979. #ifdef CONFIG_PM_ENABLE
  980. esp_pm_lock_release(p_i2s_obj[i2s_num]->pm_lock);
  981. #endif
  982. xSemaphoreGive(p_i2s_obj[i2s_num]->tx->mux);
  983. return ESP_OK;
  984. }
  985. #if SOC_I2S_SUPPORTS_ADC_DAC
  986. esp_err_t i2s_adc_enable(i2s_port_t i2s_num)
  987. {
  988. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  989. I2S_CHECK((p_i2s_obj[i2s_num] != NULL), "Not initialized yet", ESP_ERR_INVALID_STATE);
  990. I2S_CHECK((p_i2s_obj[i2s_num]->mode & I2S_MODE_ADC_BUILT_IN), "i2s built-in adc not enabled", ESP_ERR_INVALID_STATE);
  991. adc1_dma_mode_acquire();
  992. _i2s_adc_mode_recover();
  993. i2s_hal_start_rx(&(p_i2s_obj[i2s_num]->hal));
  994. i2s_hal_reset(&(p_i2s_obj[i2s_num]->hal));
  995. return i2s_set_clk(i2s_num, p_i2s_obj[i2s_num]->sample_rate, p_i2s_obj[i2s_num]->bits_per_sample, p_i2s_obj[i2s_num]->channel_num);
  996. }
  997. esp_err_t i2s_adc_disable(i2s_port_t i2s_num)
  998. {
  999. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  1000. I2S_CHECK((p_i2s_obj[i2s_num] != NULL), "Not initialized yet", ESP_ERR_INVALID_STATE);
  1001. I2S_CHECK((p_i2s_obj[i2s_num]->mode & I2S_MODE_ADC_BUILT_IN), "i2s built-in adc not enabled", ESP_ERR_INVALID_STATE);
  1002. i2s_hal_stop_rx(&(p_i2s_obj[i2s_num]->hal));
  1003. adc1_lock_release();
  1004. return ESP_OK;
  1005. }
  1006. #endif
  1007. esp_err_t i2s_write_expand(i2s_port_t i2s_num, const void *src, size_t size, size_t src_bits, size_t aim_bits, size_t *bytes_written, TickType_t ticks_to_wait)
  1008. {
  1009. char *data_ptr;
  1010. int bytes_can_write, tail;
  1011. int src_bytes, aim_bytes, zero_bytes;
  1012. *bytes_written = 0;
  1013. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  1014. I2S_CHECK((size > 0), "size must greater than zero", ESP_ERR_INVALID_ARG);
  1015. I2S_CHECK((aim_bits * size < SOC_I2S_MAX_BUFFER_SIZE), "size is too large", ESP_ERR_INVALID_ARG);
  1016. I2S_CHECK((aim_bits >= src_bits), "aim_bits mustn't be less than src_bits", ESP_ERR_INVALID_ARG);
  1017. I2S_CHECK((p_i2s_obj[i2s_num]->tx), "tx NULL", ESP_ERR_INVALID_ARG);
  1018. if (src_bits < I2S_BITS_PER_SAMPLE_8BIT || aim_bits < I2S_BITS_PER_SAMPLE_8BIT) {
  1019. ESP_LOGE(I2S_TAG,"bits mustn't be less than 8, src_bits %d aim_bits %d", src_bits, aim_bits);
  1020. return ESP_ERR_INVALID_ARG;
  1021. }
  1022. if (src_bits > I2S_BITS_PER_SAMPLE_32BIT || aim_bits > I2S_BITS_PER_SAMPLE_32BIT) {
  1023. ESP_LOGE(I2S_TAG,"bits mustn't be greater than 32, src_bits %d aim_bits %d", src_bits, aim_bits);
  1024. return ESP_ERR_INVALID_ARG;
  1025. }
  1026. if ((src_bits == I2S_BITS_PER_SAMPLE_16BIT || src_bits == I2S_BITS_PER_SAMPLE_32BIT) && (size % 2 != 0)) {
  1027. ESP_LOGE(I2S_TAG,"size must be a even number while src_bits is even, src_bits %d size %d", src_bits, size);
  1028. return ESP_ERR_INVALID_ARG;
  1029. }
  1030. if (src_bits == I2S_BITS_PER_SAMPLE_24BIT && (size % 3 != 0)) {
  1031. ESP_LOGE(I2S_TAG,"size must be a multiple of 3 while src_bits is 24, size %d", size);
  1032. return ESP_ERR_INVALID_ARG;
  1033. }
  1034. src_bytes = src_bits / 8;
  1035. aim_bytes = aim_bits / 8;
  1036. zero_bytes = aim_bytes - src_bytes;
  1037. xSemaphoreTake(p_i2s_obj[i2s_num]->tx->mux, (portTickType)portMAX_DELAY);
  1038. size = size * aim_bytes / src_bytes;
  1039. ESP_LOGD(I2S_TAG,"aim_bytes %d src_bytes %d size %d", aim_bytes, src_bytes, size);
  1040. while (size > 0) {
  1041. if (p_i2s_obj[i2s_num]->tx->rw_pos == p_i2s_obj[i2s_num]->tx->buf_size || p_i2s_obj[i2s_num]->tx->curr_ptr == NULL) {
  1042. if (xQueueReceive(p_i2s_obj[i2s_num]->tx->queue, &p_i2s_obj[i2s_num]->tx->curr_ptr, ticks_to_wait) == pdFALSE) {
  1043. break;
  1044. }
  1045. p_i2s_obj[i2s_num]->tx->rw_pos = 0;
  1046. }
  1047. data_ptr = (char*)p_i2s_obj[i2s_num]->tx->curr_ptr;
  1048. data_ptr += p_i2s_obj[i2s_num]->tx->rw_pos;
  1049. bytes_can_write = p_i2s_obj[i2s_num]->tx->buf_size - p_i2s_obj[i2s_num]->tx->rw_pos;
  1050. if (bytes_can_write > (int)size) {
  1051. bytes_can_write = size;
  1052. }
  1053. tail = bytes_can_write % aim_bytes;
  1054. bytes_can_write = bytes_can_write - tail;
  1055. memset(data_ptr, 0, bytes_can_write);
  1056. for (int j = 0; j < bytes_can_write; j += (aim_bytes - zero_bytes)) {
  1057. j += zero_bytes;
  1058. memcpy(&data_ptr[j], (const char *)(src + *bytes_written), aim_bytes - zero_bytes);
  1059. (*bytes_written) += (aim_bytes - zero_bytes);
  1060. }
  1061. size -= bytes_can_write;
  1062. p_i2s_obj[i2s_num]->tx->rw_pos += bytes_can_write;
  1063. }
  1064. xSemaphoreGive(p_i2s_obj[i2s_num]->tx->mux);
  1065. return ESP_OK;
  1066. }
  1067. esp_err_t i2s_read(i2s_port_t i2s_num, void *dest, size_t size, size_t *bytes_read, TickType_t ticks_to_wait)
  1068. {
  1069. char *data_ptr, *dest_byte;
  1070. int bytes_can_read;
  1071. *bytes_read = 0;
  1072. dest_byte = (char *)dest;
  1073. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  1074. I2S_CHECK((size < SOC_I2S_MAX_BUFFER_SIZE), "size is too large", ESP_ERR_INVALID_ARG);
  1075. I2S_CHECK((p_i2s_obj[i2s_num]->rx), "rx NULL", ESP_ERR_INVALID_ARG);
  1076. xSemaphoreTake(p_i2s_obj[i2s_num]->rx->mux, (portTickType)portMAX_DELAY);
  1077. #ifdef CONFIG_PM_ENABLE
  1078. esp_pm_lock_acquire(p_i2s_obj[i2s_num]->pm_lock);
  1079. #endif
  1080. while (size > 0) {
  1081. if (p_i2s_obj[i2s_num]->rx->rw_pos == p_i2s_obj[i2s_num]->rx->buf_size || p_i2s_obj[i2s_num]->rx->curr_ptr == NULL) {
  1082. if (xQueueReceive(p_i2s_obj[i2s_num]->rx->queue, &p_i2s_obj[i2s_num]->rx->curr_ptr, ticks_to_wait) == pdFALSE) {
  1083. break;
  1084. }
  1085. p_i2s_obj[i2s_num]->rx->rw_pos = 0;
  1086. }
  1087. data_ptr = (char*)p_i2s_obj[i2s_num]->rx->curr_ptr;
  1088. data_ptr += p_i2s_obj[i2s_num]->rx->rw_pos;
  1089. bytes_can_read = p_i2s_obj[i2s_num]->rx->buf_size - p_i2s_obj[i2s_num]->rx->rw_pos;
  1090. if (bytes_can_read > (int)size) {
  1091. bytes_can_read = size;
  1092. }
  1093. memcpy(dest_byte, data_ptr, bytes_can_read);
  1094. size -= bytes_can_read;
  1095. dest_byte += bytes_can_read;
  1096. p_i2s_obj[i2s_num]->rx->rw_pos += bytes_can_read;
  1097. (*bytes_read) += bytes_can_read;
  1098. }
  1099. #ifdef CONFIG_PM_ENABLE
  1100. esp_pm_lock_release(p_i2s_obj[i2s_num]->pm_lock);
  1101. #endif
  1102. xSemaphoreGive(p_i2s_obj[i2s_num]->rx->mux);
  1103. return ESP_OK;
  1104. }