i2s.c 47 KB

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