i2s.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160
  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. inline static void gpio_matrix_out_check(uint32_t gpio, uint32_t signal_idx, bool out_inv, bool oen_inv)
  92. {
  93. //if pin = -1, do not need to configure
  94. if (gpio != -1) {
  95. PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[gpio], PIN_FUNC_GPIO);
  96. gpio_set_direction(gpio, GPIO_MODE_DEF_OUTPUT);
  97. gpio_matrix_out(gpio, signal_idx, out_inv, oen_inv);
  98. }
  99. }
  100. inline static void gpio_matrix_in_check(uint32_t gpio, uint32_t signal_idx, bool inv)
  101. {
  102. if (gpio != -1) {
  103. PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[gpio], PIN_FUNC_GPIO);
  104. //Set direction, for some GPIOs, the input function are not enabled as default.
  105. gpio_set_direction(gpio, GPIO_MODE_DEF_INPUT);
  106. gpio_matrix_in(gpio, signal_idx, inv);
  107. }
  108. }
  109. esp_err_t i2s_clear_intr_status(i2s_port_t i2s_num, uint32_t clr_mask)
  110. {
  111. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  112. i2s_hal_clear_intr_status(&(p_i2s_obj[i2s_num]->hal), clr_mask);
  113. return ESP_OK;
  114. }
  115. esp_err_t i2s_enable_rx_intr(i2s_port_t i2s_num)
  116. {
  117. I2S_ENTER_CRITICAL();
  118. i2s_hal_enable_rx_intr(&(p_i2s_obj[i2s_num]->hal));
  119. I2S_EXIT_CRITICAL();
  120. return ESP_OK;
  121. }
  122. esp_err_t i2s_disable_rx_intr(i2s_port_t i2s_num)
  123. {
  124. I2S_ENTER_CRITICAL();
  125. i2s_hal_disable_rx_intr(&(p_i2s_obj[i2s_num]->hal));
  126. I2S_EXIT_CRITICAL();
  127. return ESP_OK;
  128. }
  129. esp_err_t i2s_disable_tx_intr(i2s_port_t i2s_num)
  130. {
  131. I2S_ENTER_CRITICAL();
  132. i2s_hal_disable_tx_intr(&(p_i2s_obj[i2s_num]->hal));
  133. I2S_EXIT_CRITICAL();
  134. return ESP_OK;
  135. }
  136. esp_err_t i2s_enable_tx_intr(i2s_port_t i2s_num)
  137. {
  138. I2S_ENTER_CRITICAL();
  139. i2s_hal_enable_tx_intr(&(p_i2s_obj[i2s_num]->hal));
  140. I2S_EXIT_CRITICAL();
  141. return ESP_OK;
  142. }
  143. float i2s_get_clk(i2s_port_t i2s_num)
  144. {
  145. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  146. return p_i2s_obj[i2s_num]->real_rate;
  147. }
  148. 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)
  149. {
  150. return esp_intr_alloc(i2s_periph_signal[i2s_num].irq, intr_alloc_flags, fn, arg, handle);
  151. }
  152. static float i2s_apll_get_fi2s(int bits_per_sample, int sdm0, int sdm1, int sdm2, int odir)
  153. {
  154. int f_xtal = (int)rtc_clk_xtal_freq_get() * 1000000;
  155. #if CONFIG_IDF_TARGET_ESP32
  156. /* ESP32 rev0 silicon issue for APLL range/accuracy, please see ESP32 ECO document for more information on this */
  157. if (esp_efuse_get_chip_ver() == 0) {
  158. sdm0 = 0;
  159. sdm1 = 0;
  160. }
  161. #endif
  162. float fout = f_xtal * (sdm2 + sdm1 / 256.0f + sdm0 / 65536.0f + 4);
  163. if (fout < APLL_MIN_FREQ || fout > APLL_MAX_FREQ) {
  164. return APLL_MAX_FREQ;
  165. }
  166. float fpll = fout / (2 * (odir+2)); //== fi2s (N=1, b=0, a=1)
  167. return fpll/2;
  168. }
  169. /**
  170. * @brief APLL calculate function, was described by following:
  171. * APLL Output frequency is given by the formula:
  172. *
  173. * apll_freq = xtal_freq * (4 + sdm2 + sdm1/256 + sdm0/65536)/((o_div + 2) * 2)
  174. * apll_freq = fout / ((o_div + 2) * 2)
  175. *
  176. * The dividend in this expression should be in the range of 240 - 600 MHz.
  177. * In rev. 0 of ESP32, sdm0 and sdm1 are unused and always set to 0.
  178. * * sdm0 frequency adjustment parameter, 0..255
  179. * * sdm1 frequency adjustment parameter, 0..255
  180. * * sdm2 frequency adjustment parameter, 0..63
  181. * * o_div frequency divider, 0..31
  182. *
  183. * The most accurate way to find the sdm0..2 and odir parameters is to loop through them all,
  184. * then apply the above formula, finding the closest frequency to the desired one.
  185. * But 256*256*64*32 = 134.217.728 loops are too slow with ESP32
  186. * 1. We will choose the parameters with the highest level of change,
  187. * With 350MHz<fout<500MHz, we limit the sdm2 from 4 to 9,
  188. * Take average frequency close to the desired frequency, and select sdm2
  189. * 2. Next, we look for sequences of less influential and more detailed parameters,
  190. * also by taking the average of the largest and smallest frequencies closer to the desired frequency.
  191. * 3. And finally, loop through all the most detailed of the parameters, finding the best desired frequency
  192. *
  193. * @param[in] rate The I2S Frequency (MCLK)
  194. * @param[in] bits_per_sample The bits per sample
  195. * @param[out] sdm0 The sdm 0
  196. * @param[out] sdm1 The sdm 1
  197. * @param[out] sdm2 The sdm 2
  198. * @param[out] odir The odir
  199. *
  200. * @return ESP_ERR_INVALID_ARG or ESP_OK
  201. */
  202. static esp_err_t i2s_apll_calculate_fi2s(int rate, int bits_per_sample, int *sdm0, int *sdm1, int *sdm2, int *odir)
  203. {
  204. int _odir, _sdm0, _sdm1, _sdm2;
  205. float avg;
  206. float min_rate, max_rate, min_diff;
  207. if (rate/bits_per_sample/2/8 < APLL_I2S_MIN_RATE) {
  208. return ESP_ERR_INVALID_ARG;
  209. }
  210. *sdm0 = 0;
  211. *sdm1 = 0;
  212. *sdm2 = 0;
  213. *odir = 0;
  214. min_diff = APLL_MAX_FREQ;
  215. for (_sdm2 = 4; _sdm2 < 9; _sdm2 ++) {
  216. max_rate = i2s_apll_get_fi2s(bits_per_sample, 255, 255, _sdm2, 0);
  217. min_rate = i2s_apll_get_fi2s(bits_per_sample, 0, 0, _sdm2, 31);
  218. avg = (max_rate + min_rate)/2;
  219. if (abs(avg - rate) < min_diff) {
  220. min_diff = abs(avg - rate);
  221. *sdm2 = _sdm2;
  222. }
  223. }
  224. min_diff = APLL_MAX_FREQ;
  225. for (_odir = 0; _odir < 32; _odir ++) {
  226. max_rate = i2s_apll_get_fi2s(bits_per_sample, 255, 255, *sdm2, _odir);
  227. min_rate = i2s_apll_get_fi2s(bits_per_sample, 0, 0, *sdm2, _odir);
  228. avg = (max_rate + min_rate)/2;
  229. if (abs(avg - rate) < min_diff) {
  230. min_diff = abs(avg - rate);
  231. *odir = _odir;
  232. }
  233. }
  234. min_diff = APLL_MAX_FREQ;
  235. for (_sdm2 = 4; _sdm2 < 9; _sdm2 ++) {
  236. max_rate = i2s_apll_get_fi2s(bits_per_sample, 255, 255, _sdm2, *odir);
  237. min_rate = i2s_apll_get_fi2s(bits_per_sample, 0, 0, _sdm2, *odir);
  238. avg = (max_rate + min_rate)/2;
  239. if (abs(avg - rate) < min_diff) {
  240. min_diff = abs(avg - rate);
  241. *sdm2 = _sdm2;
  242. }
  243. }
  244. min_diff = APLL_MAX_FREQ;
  245. for (_sdm1 = 0; _sdm1 < 256; _sdm1 ++) {
  246. max_rate = i2s_apll_get_fi2s(bits_per_sample, 255, _sdm1, *sdm2, *odir);
  247. min_rate = i2s_apll_get_fi2s(bits_per_sample, 0, _sdm1, *sdm2, *odir);
  248. avg = (max_rate + min_rate)/2;
  249. if (abs(avg - rate) < min_diff) {
  250. min_diff = abs(avg - rate);
  251. *sdm1 = _sdm1;
  252. }
  253. }
  254. min_diff = APLL_MAX_FREQ;
  255. for (_sdm0 = 0; _sdm0 < 256; _sdm0 ++) {
  256. avg = i2s_apll_get_fi2s(bits_per_sample, _sdm0, *sdm1, *sdm2, *odir);
  257. if (abs(avg - rate) < min_diff) {
  258. min_diff = abs(avg - rate);
  259. *sdm0 = _sdm0;
  260. }
  261. }
  262. return ESP_OK;
  263. }
  264. esp_err_t i2s_set_clk(i2s_port_t i2s_num, uint32_t rate, i2s_bits_per_sample_t bits, i2s_channel_t ch)
  265. {
  266. int factor = (256%bits)? 384 : 256; // According to hardware codec requirement(supported 256fs or 384fs)
  267. int clkmInteger, clkmDecimals, bck = 0;
  268. double denom = (double)1 / 64;
  269. int channel = 2;
  270. i2s_dma_t *save_tx = NULL, *save_rx = NULL;
  271. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  272. if (bits % 8 != 0 || bits > I2S_BITS_PER_SAMPLE_32BIT || bits < I2S_BITS_PER_SAMPLE_16BIT) {
  273. ESP_LOGE(I2S_TAG, "Invalid bits per sample");
  274. return ESP_ERR_INVALID_ARG;
  275. }
  276. if (p_i2s_obj[i2s_num] == NULL) {
  277. ESP_LOGE(I2S_TAG, "Not initialized yet");
  278. return ESP_ERR_INVALID_ARG;
  279. }
  280. p_i2s_obj[i2s_num]->sample_rate = rate;
  281. double clkmdiv = (double)I2S_BASE_CLK / (rate * factor);
  282. if (clkmdiv > 256) {
  283. ESP_LOGE(I2S_TAG, "clkmdiv is too large\r\n");
  284. return ESP_ERR_INVALID_ARG;
  285. }
  286. // wait all on-going writing finish
  287. if ((p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) && p_i2s_obj[i2s_num]->tx) {
  288. xSemaphoreTake(p_i2s_obj[i2s_num]->tx->mux, (portTickType)portMAX_DELAY);
  289. }
  290. if ((p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) && p_i2s_obj[i2s_num]->rx) {
  291. xSemaphoreTake(p_i2s_obj[i2s_num]->rx->mux, (portTickType)portMAX_DELAY);
  292. }
  293. i2s_stop(i2s_num);
  294. i2s_hal_set_tx_mode(&(p_i2s_obj[i2s_num]->hal), ch, bits);
  295. i2s_hal_set_rx_mode(&(p_i2s_obj[i2s_num]->hal), ch, bits);
  296. if (p_i2s_obj[i2s_num]->channel_num != ch) {
  297. p_i2s_obj[i2s_num]->channel_num = (ch == 2) ? 2 : 1;
  298. }
  299. if (bits != p_i2s_obj[i2s_num]->bits_per_sample) {
  300. p_i2s_obj[i2s_num]->bits_per_sample = bits;
  301. p_i2s_obj[i2s_num]->bytes_per_sample = p_i2s_obj[i2s_num]->bits_per_sample / 8;
  302. // Round bytes_per_sample up to next multiple of 16 bits
  303. int halfwords_per_sample = (p_i2s_obj[i2s_num]->bits_per_sample + 15) / 16;
  304. p_i2s_obj[i2s_num]->bytes_per_sample = halfwords_per_sample * 2;
  305. // Because limited of DMA buffer is 4092 bytes
  306. 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) {
  307. p_i2s_obj[i2s_num]->dma_buf_len = 4092 / p_i2s_obj[i2s_num]->bytes_per_sample / p_i2s_obj[i2s_num]->channel_num;
  308. }
  309. // Re-create TX DMA buffer
  310. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) {
  311. save_tx = p_i2s_obj[i2s_num]->tx;
  312. 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);
  313. if (p_i2s_obj[i2s_num]->tx == NULL) {
  314. ESP_LOGE(I2S_TAG, "Failed to create tx dma buffer");
  315. i2s_driver_uninstall(i2s_num);
  316. return ESP_ERR_NO_MEM;
  317. }
  318. i2s_hal_set_out_link_addr(&(p_i2s_obj[i2s_num]->hal), (uint32_t) p_i2s_obj[i2s_num]->tx->desc[0]);
  319. //destroy old tx dma if exist
  320. if (save_tx) {
  321. i2s_destroy_dma_queue(i2s_num, save_tx);
  322. }
  323. }
  324. // Re-create RX DMA buffer
  325. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) {
  326. save_rx = p_i2s_obj[i2s_num]->rx;
  327. 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);
  328. if (p_i2s_obj[i2s_num]->rx == NULL){
  329. ESP_LOGE(I2S_TAG, "Failed to create rx dma buffer");
  330. i2s_driver_uninstall(i2s_num);
  331. return ESP_ERR_NO_MEM;
  332. }
  333. 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]);
  334. //destroy old rx dma if exist
  335. if (save_rx) {
  336. i2s_destroy_dma_queue(i2s_num, save_rx);
  337. }
  338. }
  339. }
  340. double mclk;
  341. int sdm0, sdm1, sdm2, odir, m_scale = 8;
  342. int fi2s_clk = rate*channel*bits*m_scale;
  343. if (p_i2s_obj[i2s_num]->mode & (I2S_MODE_DAC_BUILT_IN | I2S_MODE_ADC_BUILT_IN)) {
  344. //DAC uses bclk as sample clock, not WS. WS can be something arbitrary.
  345. //Rate as given to this function is the intended sample rate;
  346. //According to the TRM, WS clk equals to the sample rate, and bclk is double the speed of WS
  347. uint32_t b_clk = rate * I2S_AD_BCK_FACTOR;
  348. fi2s_clk /= I2S_AD_BCK_FACTOR;
  349. int factor2 = 60;
  350. mclk = b_clk * factor2;
  351. clkmdiv = ((double) I2S_BASE_CLK) / mclk;
  352. clkmInteger = clkmdiv;
  353. clkmDecimals = (clkmdiv - clkmInteger) / denom;
  354. bck = mclk / b_clk;
  355. #if SOC_I2S_SUPPORT_PDM
  356. } else if (p_i2s_obj[i2s_num]->mode & I2S_MODE_PDM) {
  357. uint32_t b_clk = 0;
  358. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) {
  359. int fp;
  360. int fs;
  361. i2s_hal_get_tx_pdm(&(p_i2s_obj[i2s_num]->hal), &fp, &fs);
  362. b_clk = rate * I2S_PDM_BCK_FACTOR * (fp / fs);
  363. fi2s_clk /= (I2S_PDM_BCK_FACTOR * (fp / fs));
  364. } else if (p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) {
  365. bool en;
  366. i2s_hal_get_rx_sinc_dsr_16_en(&(p_i2s_obj[i2s_num]->hal), &en);
  367. b_clk = rate * I2S_PDM_BCK_FACTOR * (en ? 2 : 1);
  368. fi2s_clk /= (I2S_PDM_BCK_FACTOR * (en ? 2 : 1));
  369. }
  370. int factor2 = 5 ;
  371. mclk = b_clk * factor2;
  372. clkmdiv = ((double) I2S_BASE_CLK) / mclk;
  373. clkmInteger = clkmdiv;
  374. clkmDecimals = (clkmdiv - clkmInteger) / denom;
  375. bck = mclk / b_clk;
  376. #endif
  377. } else {
  378. clkmInteger = clkmdiv;
  379. clkmDecimals = (clkmdiv - clkmInteger) / denom;
  380. mclk = clkmInteger + denom * clkmDecimals;
  381. bck = factor/(bits * channel);
  382. }
  383. if(p_i2s_obj[i2s_num]->use_apll && p_i2s_obj[i2s_num]->fixed_mclk) {
  384. fi2s_clk = p_i2s_obj[i2s_num]->fixed_mclk;
  385. m_scale = fi2s_clk/bits/rate/channel;
  386. }
  387. if(p_i2s_obj[i2s_num]->use_apll && i2s_apll_calculate_fi2s(fi2s_clk, bits, &sdm0, &sdm1, &sdm2, &odir) == ESP_OK) {
  388. ESP_LOGD(I2S_TAG, "sdm0=%d, sdm1=%d, sdm2=%d, odir=%d", sdm0, sdm1, sdm2, odir);
  389. rtc_clk_apll_enable(1, sdm0, sdm1, sdm2, odir);
  390. i2s_hal_set_clk_div(&(p_i2s_obj[i2s_num]->hal), 1, 1, 0, m_scale, m_scale);
  391. i2s_hal_set_clock_sel(&(p_i2s_obj[i2s_num]->hal), I2S_CLK_APLL);
  392. double fi2s_rate = i2s_apll_get_fi2s(bits, sdm0, sdm1, sdm2, odir);
  393. p_i2s_obj[i2s_num]->real_rate = fi2s_rate/bits/channel/m_scale;
  394. 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",
  395. rate, fi2s_rate/bits/channel/m_scale, bits, 1, m_scale, fi2s_rate, fi2s_rate/8, 1, 0);
  396. } else {
  397. i2s_hal_set_clock_sel(&(p_i2s_obj[i2s_num]->hal), I2S_CLK_D2CLK);
  398. i2s_hal_set_clk_div(&(p_i2s_obj[i2s_num]->hal), clkmInteger, 63, clkmDecimals, bck, bck);
  399. double real_rate = (double) (I2S_BASE_CLK / (bck * bits * clkmInteger) / 2);
  400. p_i2s_obj[i2s_num]->real_rate = real_rate;
  401. 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",
  402. rate, real_rate, bits, clkmInteger, bck, (double)I2S_BASE_CLK / mclk, real_rate*bits*channel, 64, clkmDecimals);
  403. }
  404. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) {
  405. p_i2s_obj[i2s_num]->tx->curr_ptr = NULL;
  406. p_i2s_obj[i2s_num]->tx->rw_pos = 0;
  407. }
  408. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) {
  409. p_i2s_obj[i2s_num]->rx->curr_ptr = NULL;
  410. p_i2s_obj[i2s_num]->rx->rw_pos = 0;
  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_hal_reset(&(p_i2s_obj[i2s_num]->hal));
  582. esp_intr_disable(p_i2s_obj[i2s_num]->i2s_isr_handle);
  583. i2s_hal_clear_intr_status(&(p_i2s_obj[i2s_num]->hal), I2S_INTR_MAX);
  584. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) {
  585. i2s_enable_tx_intr(i2s_num);
  586. i2s_hal_start_tx(&(p_i2s_obj[i2s_num]->hal));
  587. }
  588. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) {
  589. i2s_enable_rx_intr(i2s_num);
  590. i2s_hal_start_rx(&(p_i2s_obj[i2s_num]->hal));
  591. }
  592. esp_intr_enable(p_i2s_obj[i2s_num]->i2s_isr_handle);
  593. I2S_EXIT_CRITICAL();
  594. return ESP_OK;
  595. }
  596. esp_err_t i2s_stop(i2s_port_t i2s_num)
  597. {
  598. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  599. I2S_ENTER_CRITICAL();
  600. esp_intr_disable(p_i2s_obj[i2s_num]->i2s_isr_handle);
  601. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) {
  602. i2s_hal_stop_tx(&(p_i2s_obj[i2s_num]->hal));
  603. i2s_disable_tx_intr(i2s_num);
  604. }
  605. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) {
  606. i2s_hal_stop_rx(&(p_i2s_obj[i2s_num]->hal));
  607. i2s_disable_rx_intr(i2s_num);
  608. }
  609. uint32_t mask;
  610. i2s_hal_get_intr_status(&(p_i2s_obj[i2s_num]->hal), &mask);
  611. i2s_hal_clear_intr_status(&(p_i2s_obj[i2s_num]->hal), mask);
  612. I2S_EXIT_CRITICAL();
  613. return ESP_OK;
  614. }
  615. esp_err_t i2s_set_dac_mode(i2s_dac_mode_t dac_mode)
  616. {
  617. I2S_CHECK((dac_mode < I2S_DAC_CHANNEL_MAX), "i2s dac mode error", ESP_ERR_INVALID_ARG);
  618. if (dac_mode == I2S_DAC_CHANNEL_DISABLE) {
  619. dac_output_disable(DAC_CHANNEL_1);
  620. dac_output_disable(DAC_CHANNEL_2);
  621. dac_i2s_disable();
  622. } else {
  623. dac_i2s_enable();
  624. }
  625. if (dac_mode & I2S_DAC_CHANNEL_RIGHT_EN) {
  626. //DAC1, right channel, GPIO25
  627. dac_output_enable(DAC_CHANNEL_1);
  628. }
  629. if (dac_mode & I2S_DAC_CHANNEL_LEFT_EN) {
  630. //DAC2, left channel, GPIO26
  631. dac_output_enable(DAC_CHANNEL_2);
  632. }
  633. return ESP_OK;
  634. }
  635. static esp_err_t _i2s_adc_mode_recover(void)
  636. {
  637. I2S_CHECK(((_i2s_adc_unit != -1) && (_i2s_adc_channel != -1)), "i2s ADC recover error, not initialized...", ESP_ERR_INVALID_ARG);
  638. return adc_i2s_mode_init(_i2s_adc_unit, _i2s_adc_channel);
  639. }
  640. esp_err_t i2s_set_adc_mode(adc_unit_t adc_unit, adc1_channel_t adc_channel)
  641. {
  642. I2S_CHECK((adc_unit < ADC_UNIT_2), "i2s ADC unit error, only support ADC1 for now", ESP_ERR_INVALID_ARG);
  643. // For now, we only support SAR ADC1.
  644. _i2s_adc_unit = adc_unit;
  645. _i2s_adc_channel = adc_channel;
  646. return adc_i2s_mode_init(adc_unit, adc_channel);
  647. }
  648. esp_err_t i2s_set_pin(i2s_port_t i2s_num, const i2s_pin_config_t *pin)
  649. {
  650. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  651. if (pin == NULL) {
  652. return i2s_set_dac_mode(I2S_DAC_CHANNEL_BOTH_EN);
  653. }
  654. if (pin->bck_io_num != -1 && !GPIO_IS_VALID_GPIO(pin->bck_io_num)) {
  655. ESP_LOGE(I2S_TAG, "bck_io_num error");
  656. return ESP_FAIL;
  657. }
  658. if (pin->ws_io_num != -1 && !GPIO_IS_VALID_GPIO(pin->ws_io_num)) {
  659. ESP_LOGE(I2S_TAG, "ws_io_num error");
  660. return ESP_FAIL;
  661. }
  662. if (pin->data_out_num != -1 && !GPIO_IS_VALID_OUTPUT_GPIO(pin->data_out_num)) {
  663. ESP_LOGE(I2S_TAG, "data_out_num error");
  664. return ESP_FAIL;
  665. }
  666. if (pin->data_in_num != -1 && !GPIO_IS_VALID_GPIO(pin->data_in_num)) {
  667. ESP_LOGE(I2S_TAG, "data_in_num error");
  668. return ESP_FAIL;
  669. }
  670. int bck_sig = -1, ws_sig = -1, data_out_sig = -1, data_in_sig = -1;
  671. //Each IIS hw module has a RX and TX unit.
  672. //For TX unit, the output signal index should be I2SnO_xxx_OUT_IDX
  673. //For TX unit, the input signal index should be I2SnO_xxx_IN_IDX
  674. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) {
  675. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_MASTER) {
  676. bck_sig = i2s_periph_signal[i2s_num].o_bck_out_sig;
  677. ws_sig = i2s_periph_signal[i2s_num].o_ws_out_sig;
  678. data_out_sig = i2s_periph_signal[i2s_num].o_data_out_sig;
  679. } else if (p_i2s_obj[i2s_num]->mode & I2S_MODE_SLAVE) {
  680. bck_sig = i2s_periph_signal[i2s_num].o_bck_in_sig;
  681. ws_sig = i2s_periph_signal[i2s_num].o_ws_in_sig;
  682. data_out_sig = i2s_periph_signal[i2s_num].o_data_out_sig;
  683. }
  684. }
  685. //For RX unit, the output signal index should be I2SnI_xxx_OUT_IDX
  686. //For RX unit, the input signal index shuld be I2SnI_xxx_IN_IDX
  687. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) {
  688. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_MASTER) {
  689. bck_sig = i2s_periph_signal[i2s_num].i_bck_out_sig;
  690. ws_sig = i2s_periph_signal[i2s_num].i_ws_out_sig;
  691. data_in_sig = i2s_periph_signal[i2s_num].i_data_in_sig;
  692. } else if (p_i2s_obj[i2s_num]->mode & I2S_MODE_SLAVE) {
  693. bck_sig = i2s_periph_signal[i2s_num].i_bck_in_sig;
  694. ws_sig = i2s_periph_signal[i2s_num].i_ws_in_sig;
  695. data_in_sig = i2s_periph_signal[i2s_num].i_data_in_sig;
  696. }
  697. }
  698. //For "full-duplex + slave" mode, we should select RX signal index for ws and bck.
  699. //For "full-duplex + master" mode, we should select TX signal index for ws and bck.
  700. if ((p_i2s_obj[i2s_num]->mode & I2S_FULL_DUPLEX_SLAVE_MODE_MASK) == I2S_FULL_DUPLEX_SLAVE_MODE_MASK) {
  701. bck_sig = i2s_periph_signal[i2s_num].i_bck_in_sig;
  702. ws_sig = i2s_periph_signal[i2s_num].i_ws_in_sig;
  703. } else if ((p_i2s_obj[i2s_num]->mode & I2S_FULL_DUPLEX_MASTER_MODE_MASK) == I2S_FULL_DUPLEX_MASTER_MODE_MASK) {
  704. bck_sig = i2s_periph_signal[i2s_num].o_bck_out_sig;
  705. ws_sig = i2s_periph_signal[i2s_num].o_ws_out_sig;
  706. }
  707. gpio_matrix_out_check(pin->data_out_num, data_out_sig, 0, 0);
  708. gpio_matrix_in_check(pin->data_in_num, data_in_sig, 0);
  709. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_MASTER) {
  710. gpio_matrix_out_check(pin->ws_io_num, ws_sig, 0, 0);
  711. gpio_matrix_out_check(pin->bck_io_num, bck_sig, 0, 0);
  712. } else if (p_i2s_obj[i2s_num]->mode & I2S_MODE_SLAVE) {
  713. gpio_matrix_in_check(pin->ws_io_num, ws_sig, 0);
  714. gpio_matrix_in_check(pin->bck_io_num, bck_sig, 0);
  715. }
  716. ESP_LOGD(I2S_TAG, "data: out %d, in: %d, ws: %d, bck: %d", data_out_sig, data_in_sig, ws_sig, bck_sig);
  717. return ESP_OK;
  718. }
  719. esp_err_t i2s_set_sample_rates(i2s_port_t i2s_num, uint32_t rate)
  720. {
  721. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  722. I2S_CHECK((p_i2s_obj[i2s_num]->bytes_per_sample > 0), "bits_per_sample not set", ESP_ERR_INVALID_ARG);
  723. return i2s_set_clk(i2s_num, rate, p_i2s_obj[i2s_num]->bits_per_sample, p_i2s_obj[i2s_num]->channel_num);
  724. }
  725. #if SOC_I2S_SUPPORT_PDM
  726. esp_err_t i2s_set_pdm_rx_down_sample(i2s_port_t i2s_num, i2s_pdm_dsr_t dsr)
  727. {
  728. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  729. i2s_hal_set_pdm_rx_down_sample(&(p_i2s_obj[i2s_num]->hal), dsr);
  730. 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);
  731. }
  732. #endif
  733. static esp_err_t i2s_param_config(i2s_port_t i2s_num, const i2s_config_t *i2s_config)
  734. {
  735. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  736. I2S_CHECK((i2s_config), "param null", ESP_ERR_INVALID_ARG);
  737. 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);
  738. 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);
  739. #if SOC_I2S_SUPPORT_PDM
  740. I2S_CHECK(!((i2s_config->mode & I2S_MODE_PDM) && (i2s_num != I2S_NUM_0)), "I2S DAC PDM only support on I2S0", ESP_ERR_INVALID_ARG);
  741. #endif
  742. if(i2s_config->mode & I2S_MODE_ADC_BUILT_IN) {
  743. //in ADC built-in mode, we need to call i2s_set_adc_mode to
  744. //initialize the specific ADC channel.
  745. //in the current stage, we only support ADC1 and single channel mode.
  746. //In default data mode, the ADC data is in 12-bit resolution mode.
  747. adc_power_acquire();
  748. }
  749. // configure I2S data port interface.
  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. // switch back to PLL clock source
  883. i2s_hal_set_clock_sel(&(p_i2s_obj[i2s_num]->hal), I2S_CLK_D2CLK);
  884. rtc_clk_apll_enable(0, 0, 0, 0, 0);
  885. }
  886. #ifdef CONFIG_PM_ENABLE
  887. if (p_i2s_obj[i2s_num]->pm_lock) {
  888. esp_pm_lock_delete(p_i2s_obj[i2s_num]->pm_lock);
  889. }
  890. #endif
  891. free(p_i2s_obj[i2s_num]);
  892. p_i2s_obj[i2s_num] = NULL;
  893. periph_module_disable(i2s_periph_signal[i2s_num].module);
  894. return ESP_OK;
  895. }
  896. 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)
  897. {
  898. char *data_ptr, *src_byte;
  899. int bytes_can_write;
  900. *bytes_written = 0;
  901. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  902. I2S_CHECK((size < I2S_MAX_BUFFER_SIZE), "size is too large", ESP_ERR_INVALID_ARG);
  903. I2S_CHECK((p_i2s_obj[i2s_num]->tx), "tx NULL", ESP_ERR_INVALID_ARG);
  904. xSemaphoreTake(p_i2s_obj[i2s_num]->tx->mux, (portTickType)portMAX_DELAY);
  905. #ifdef CONFIG_PM_ENABLE
  906. esp_pm_lock_acquire(p_i2s_obj[i2s_num]->pm_lock);
  907. #endif
  908. src_byte = (char *)src;
  909. while (size > 0) {
  910. 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) {
  911. if (xQueueReceive(p_i2s_obj[i2s_num]->tx->queue, &p_i2s_obj[i2s_num]->tx->curr_ptr, ticks_to_wait) == pdFALSE) {
  912. break;
  913. }
  914. p_i2s_obj[i2s_num]->tx->rw_pos = 0;
  915. }
  916. 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);
  917. data_ptr = (char*)p_i2s_obj[i2s_num]->tx->curr_ptr;
  918. data_ptr += p_i2s_obj[i2s_num]->tx->rw_pos;
  919. bytes_can_write = p_i2s_obj[i2s_num]->tx->buf_size - p_i2s_obj[i2s_num]->tx->rw_pos;
  920. if (bytes_can_write > size) {
  921. bytes_can_write = size;
  922. }
  923. memcpy(data_ptr, src_byte, bytes_can_write);
  924. size -= bytes_can_write;
  925. src_byte += bytes_can_write;
  926. p_i2s_obj[i2s_num]->tx->rw_pos += bytes_can_write;
  927. (*bytes_written) += bytes_can_write;
  928. }
  929. #ifdef CONFIG_PM_ENABLE
  930. esp_pm_lock_release(p_i2s_obj[i2s_num]->pm_lock);
  931. #endif
  932. xSemaphoreGive(p_i2s_obj[i2s_num]->tx->mux);
  933. return ESP_OK;
  934. }
  935. esp_err_t i2s_adc_enable(i2s_port_t i2s_num)
  936. {
  937. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  938. I2S_CHECK((p_i2s_obj[i2s_num] != NULL), "Not initialized yet", ESP_ERR_INVALID_STATE);
  939. I2S_CHECK((p_i2s_obj[i2s_num]->mode & I2S_MODE_ADC_BUILT_IN), "i2s built-in adc not enabled", ESP_ERR_INVALID_STATE);
  940. adc1_i2s_mode_acquire();
  941. _i2s_adc_mode_recover();
  942. i2s_hal_start_rx(&(p_i2s_obj[i2s_num]->hal));
  943. i2s_hal_reset(&(p_i2s_obj[i2s_num]->hal));
  944. 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);
  945. }
  946. esp_err_t i2s_adc_disable(i2s_port_t i2s_num)
  947. {
  948. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  949. I2S_CHECK((p_i2s_obj[i2s_num] != NULL), "Not initialized yet", ESP_ERR_INVALID_STATE);
  950. I2S_CHECK((p_i2s_obj[i2s_num]->mode & I2S_MODE_ADC_BUILT_IN), "i2s built-in adc not enabled", ESP_ERR_INVALID_STATE);
  951. adc1_lock_release();
  952. return ESP_OK;
  953. }
  954. 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)
  955. {
  956. char *data_ptr;
  957. int bytes_can_write, tail;
  958. int src_bytes, aim_bytes, zero_bytes;
  959. *bytes_written = 0;
  960. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  961. I2S_CHECK((size > 0), "size must greater than zero", ESP_ERR_INVALID_ARG);
  962. I2S_CHECK((aim_bits * size < I2S_MAX_BUFFER_SIZE), "size is too large", ESP_ERR_INVALID_ARG);
  963. I2S_CHECK((aim_bits >= src_bits), "aim_bits mustn't be less than src_bits", ESP_ERR_INVALID_ARG);
  964. I2S_CHECK((p_i2s_obj[i2s_num]->tx), "tx NULL", ESP_ERR_INVALID_ARG);
  965. if (src_bits < I2S_BITS_PER_SAMPLE_8BIT || aim_bits < I2S_BITS_PER_SAMPLE_8BIT) {
  966. ESP_LOGE(I2S_TAG,"bits mustn't be less than 8, 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_32BIT || aim_bits > I2S_BITS_PER_SAMPLE_32BIT) {
  970. ESP_LOGE(I2S_TAG,"bits mustn't be greater than 32, src_bits %d aim_bits %d", src_bits, aim_bits);
  971. return ESP_ERR_INVALID_ARG;
  972. }
  973. if ((src_bits == I2S_BITS_PER_SAMPLE_16BIT || src_bits == I2S_BITS_PER_SAMPLE_32BIT) && (size % 2 != 0)) {
  974. ESP_LOGE(I2S_TAG,"size must be a even number while src_bits is even, src_bits %d size %d", src_bits, size);
  975. return ESP_ERR_INVALID_ARG;
  976. }
  977. if (src_bits == I2S_BITS_PER_SAMPLE_24BIT && (size % 3 != 0)) {
  978. ESP_LOGE(I2S_TAG,"size must be a multiple of 3 while src_bits is 24, size %d", size);
  979. return ESP_ERR_INVALID_ARG;
  980. }
  981. src_bytes = src_bits / 8;
  982. aim_bytes = aim_bits / 8;
  983. zero_bytes = aim_bytes - src_bytes;
  984. xSemaphoreTake(p_i2s_obj[i2s_num]->tx->mux, (portTickType)portMAX_DELAY);
  985. size = size * aim_bytes / src_bytes;
  986. ESP_LOGD(I2S_TAG,"aim_bytes %d src_bytes %d size %d", aim_bytes, src_bytes, size);
  987. while (size > 0) {
  988. 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) {
  989. if (xQueueReceive(p_i2s_obj[i2s_num]->tx->queue, &p_i2s_obj[i2s_num]->tx->curr_ptr, ticks_to_wait) == pdFALSE) {
  990. break;
  991. }
  992. p_i2s_obj[i2s_num]->tx->rw_pos = 0;
  993. }
  994. data_ptr = (char*)p_i2s_obj[i2s_num]->tx->curr_ptr;
  995. data_ptr += p_i2s_obj[i2s_num]->tx->rw_pos;
  996. bytes_can_write = p_i2s_obj[i2s_num]->tx->buf_size - p_i2s_obj[i2s_num]->tx->rw_pos;
  997. if (bytes_can_write > size) {
  998. bytes_can_write = size;
  999. }
  1000. tail = bytes_can_write % aim_bytes;
  1001. bytes_can_write = bytes_can_write - tail;
  1002. memset(data_ptr, 0, bytes_can_write);
  1003. for (int j = 0; j < bytes_can_write; j += (aim_bytes - zero_bytes)) {
  1004. j += zero_bytes;
  1005. memcpy(&data_ptr[j], (const char *)(src + *bytes_written), aim_bytes - zero_bytes);
  1006. (*bytes_written) += (aim_bytes - zero_bytes);
  1007. }
  1008. size -= bytes_can_write;
  1009. p_i2s_obj[i2s_num]->tx->rw_pos += bytes_can_write;
  1010. }
  1011. xSemaphoreGive(p_i2s_obj[i2s_num]->tx->mux);
  1012. return ESP_OK;
  1013. }
  1014. esp_err_t i2s_read(i2s_port_t i2s_num, void *dest, size_t size, size_t *bytes_read, TickType_t ticks_to_wait)
  1015. {
  1016. char *data_ptr, *dest_byte;
  1017. int bytes_can_read;
  1018. *bytes_read = 0;
  1019. dest_byte = (char *)dest;
  1020. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  1021. I2S_CHECK((size < I2S_MAX_BUFFER_SIZE), "size is too large", ESP_ERR_INVALID_ARG);
  1022. I2S_CHECK((p_i2s_obj[i2s_num]->rx), "rx NULL", ESP_ERR_INVALID_ARG);
  1023. xSemaphoreTake(p_i2s_obj[i2s_num]->rx->mux, (portTickType)portMAX_DELAY);
  1024. #ifdef CONFIG_PM_ENABLE
  1025. esp_pm_lock_acquire(p_i2s_obj[i2s_num]->pm_lock);
  1026. #endif
  1027. while (size > 0) {
  1028. 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) {
  1029. if (xQueueReceive(p_i2s_obj[i2s_num]->rx->queue, &p_i2s_obj[i2s_num]->rx->curr_ptr, ticks_to_wait) == pdFALSE) {
  1030. break;
  1031. }
  1032. p_i2s_obj[i2s_num]->rx->rw_pos = 0;
  1033. }
  1034. data_ptr = (char*)p_i2s_obj[i2s_num]->rx->curr_ptr;
  1035. data_ptr += p_i2s_obj[i2s_num]->rx->rw_pos;
  1036. bytes_can_read = p_i2s_obj[i2s_num]->rx->buf_size - p_i2s_obj[i2s_num]->rx->rw_pos;
  1037. if (bytes_can_read > size) {
  1038. bytes_can_read = size;
  1039. }
  1040. memcpy(dest_byte, data_ptr, bytes_can_read);
  1041. size -= bytes_can_read;
  1042. dest_byte += bytes_can_read;
  1043. p_i2s_obj[i2s_num]->rx->rw_pos += bytes_can_read;
  1044. (*bytes_read) += bytes_can_read;
  1045. }
  1046. #ifdef CONFIG_PM_ENABLE
  1047. esp_pm_lock_release(p_i2s_obj[i2s_num]->pm_lock);
  1048. #endif
  1049. xSemaphoreGive(p_i2s_obj[i2s_num]->rx->mux);
  1050. return ESP_OK;
  1051. }