i2s.c 47 KB

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