i2s.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346
  1. // Copyright 2015-2016 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. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include <string.h>
  14. #include <math.h>
  15. #include <esp_types.h>
  16. #include "freertos/FreeRTOS.h"
  17. #include "freertos/queue.h"
  18. #include "freertos/xtensa_api.h"
  19. #include "soc/rtc_periph.h"
  20. #include "soc/rtc.h"
  21. #include "soc/efuse_periph.h"
  22. #include "esp32/rom/lldesc.h"
  23. #include "driver/gpio.h"
  24. #include "driver/i2s.h"
  25. #include "driver/rtc_io.h"
  26. #include "driver/dac.h"
  27. #include "adc1_i2s_private.h"
  28. #include "esp_intr_alloc.h"
  29. #include "esp_err.h"
  30. #include "esp_log.h"
  31. #include "esp_pm.h"
  32. static const char* I2S_TAG = "I2S";
  33. #define I2S_CHECK(a, str, ret) if (!(a)) { \
  34. ESP_LOGE(I2S_TAG,"%s:%d (%s):%s", __FILE__, __LINE__, __FUNCTION__, str); \
  35. return (ret); \
  36. }
  37. #define I2S_MAX_BUFFER_SIZE (4 * 1024 * 1024) //the maximum RAM can be allocated
  38. #define I2S_BASE_CLK (2*APB_CLK_FREQ)
  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. #define APLL_MIN_FREQ (250000000)
  46. #define APLL_MAX_FREQ (500000000)
  47. #define APLL_I2S_MIN_RATE (10675) //in Hz, I2S Clock rate limited by hardware
  48. #define I2S_AD_BCK_FACTOR (2)
  49. #define I2S_PDM_BCK_FACTOR (64)
  50. /**
  51. * @brief DMA buffer object
  52. *
  53. */
  54. typedef struct {
  55. char **buf;
  56. int buf_size;
  57. int rw_pos;
  58. void *curr_ptr;
  59. SemaphoreHandle_t mux;
  60. xQueueHandle queue;
  61. lldesc_t **desc;
  62. } i2s_dma_t;
  63. /**
  64. * @brief I2S object instance
  65. *
  66. */
  67. typedef struct {
  68. i2s_port_t i2s_num; /*!< I2S port number*/
  69. int queue_size; /*!< I2S event queue size*/
  70. QueueHandle_t i2s_queue; /*!< I2S queue handler*/
  71. int dma_buf_count; /*!< DMA buffer count, number of buffer*/
  72. int dma_buf_len; /*!< DMA buffer length, length of each buffer*/
  73. i2s_dma_t *rx; /*!< DMA Tx buffer*/
  74. i2s_dma_t *tx; /*!< DMA Rx buffer*/
  75. i2s_isr_handle_t i2s_isr_handle; /*!< I2S Interrupt handle*/
  76. int channel_num; /*!< Number of channels*/
  77. int bytes_per_sample; /*!< Bytes per sample*/
  78. int bits_per_sample; /*!< Bits per sample*/
  79. i2s_mode_t mode; /*!< I2S Working mode*/
  80. uint32_t sample_rate; /*!< I2S sample rate */
  81. bool use_apll; /*!< I2S use APLL clock */
  82. bool tx_desc_auto_clear; /*!< I2S auto clear tx descriptor on underflow */
  83. int fixed_mclk; /*!< I2S fixed MLCK clock */
  84. double real_rate;
  85. #ifdef CONFIG_PM_ENABLE
  86. esp_pm_lock_handle_t pm_lock;
  87. #endif
  88. } i2s_obj_t;
  89. static i2s_obj_t *p_i2s_obj[I2S_NUM_MAX] = {0};
  90. /* DRAM_ATTR is required to avoid I2S array placed in flash, due to accessed from ISR */
  91. static DRAM_ATTR i2s_dev_t* I2S[I2S_NUM_MAX] = {&I2S0, &I2S1};
  92. static portMUX_TYPE i2s_spinlock[I2S_NUM_MAX] = {portMUX_INITIALIZER_UNLOCKED, portMUX_INITIALIZER_UNLOCKED};
  93. static int _i2s_adc_unit = -1;
  94. static int _i2s_adc_channel = -1;
  95. static i2s_dma_t *i2s_create_dma_queue(i2s_port_t i2s_num, int dma_buf_count, int dma_buf_len);
  96. static esp_err_t i2s_destroy_dma_queue(i2s_port_t i2s_num, i2s_dma_t *dma);
  97. static esp_err_t i2s_reset_fifo(i2s_port_t i2s_num)
  98. {
  99. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  100. I2S_ENTER_CRITICAL();
  101. I2S[i2s_num]->conf.rx_fifo_reset = 1;
  102. I2S[i2s_num]->conf.rx_fifo_reset = 0;
  103. I2S[i2s_num]->conf.tx_fifo_reset = 1;
  104. I2S[i2s_num]->conf.tx_fifo_reset = 0;
  105. I2S_EXIT_CRITICAL();
  106. return ESP_OK;
  107. }
  108. inline static void gpio_matrix_out_check(uint32_t gpio, uint32_t signal_idx, bool out_inv, bool oen_inv)
  109. {
  110. //if pin = -1, do not need to configure
  111. if (gpio != -1) {
  112. PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[gpio], PIN_FUNC_GPIO);
  113. gpio_set_direction(gpio, GPIO_MODE_DEF_OUTPUT);
  114. gpio_matrix_out(gpio, signal_idx, out_inv, oen_inv);
  115. }
  116. }
  117. inline static void gpio_matrix_in_check(uint32_t gpio, uint32_t signal_idx, bool inv)
  118. {
  119. if (gpio != -1) {
  120. PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[gpio], PIN_FUNC_GPIO);
  121. //Set direction, for some GPIOs, the input function are not enabled as default.
  122. gpio_set_direction(gpio, GPIO_MODE_DEF_INPUT);
  123. gpio_matrix_in(gpio, signal_idx, inv);
  124. }
  125. }
  126. esp_err_t i2s_clear_intr_status(i2s_port_t i2s_num, uint32_t clr_mask)
  127. {
  128. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  129. I2S[i2s_num]->int_clr.val = clr_mask;
  130. return ESP_OK;
  131. }
  132. esp_err_t i2s_enable_rx_intr(i2s_port_t i2s_num)
  133. {
  134. I2S_ENTER_CRITICAL();
  135. I2S[i2s_num]->int_ena.in_suc_eof = 1;
  136. I2S[i2s_num]->int_ena.in_dscr_err = 1;
  137. I2S_EXIT_CRITICAL();
  138. return ESP_OK;
  139. }
  140. esp_err_t i2s_disable_rx_intr(i2s_port_t i2s_num)
  141. {
  142. I2S_ENTER_CRITICAL();
  143. I2S[i2s_num]->int_ena.in_suc_eof = 0;
  144. I2S[i2s_num]->int_ena.in_dscr_err = 0;
  145. I2S_EXIT_CRITICAL();
  146. return ESP_OK;
  147. }
  148. esp_err_t i2s_disable_tx_intr(i2s_port_t i2s_num)
  149. {
  150. I2S_ENTER_CRITICAL();
  151. I2S[i2s_num]->int_ena.out_eof = 0;
  152. I2S[i2s_num]->int_ena.out_dscr_err = 0;
  153. I2S_EXIT_CRITICAL();
  154. return ESP_OK;
  155. }
  156. esp_err_t i2s_enable_tx_intr(i2s_port_t i2s_num)
  157. {
  158. I2S_ENTER_CRITICAL();
  159. I2S[i2s_num]->int_ena.out_eof = 1;
  160. I2S[i2s_num]->int_ena.out_dscr_err = 1;
  161. I2S_EXIT_CRITICAL();
  162. return ESP_OK;
  163. }
  164. float i2s_get_clk(i2s_port_t i2s_num)
  165. {
  166. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  167. return p_i2s_obj[i2s_num]->real_rate;
  168. }
  169. 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)
  170. {
  171. return esp_intr_alloc(ETS_I2S0_INTR_SOURCE + i2s_num, intr_alloc_flags, fn, arg, handle);
  172. }
  173. static float i2s_apll_get_fi2s(int bits_per_sample, int sdm0, int sdm1, int sdm2, int odir)
  174. {
  175. int f_xtal = (int)rtc_clk_xtal_freq_get() * 1000000;
  176. uint32_t is_rev0 = (GET_PERI_REG_BITS2(EFUSE_BLK0_RDATA3_REG, 1, 15) == 0);
  177. if (is_rev0) {
  178. sdm0 = 0;
  179. sdm1 = 0;
  180. }
  181. float fout = f_xtal * (sdm2 + sdm1 / 256.0f + sdm0 / 65536.0f + 4);
  182. if (fout < APLL_MIN_FREQ || fout > APLL_MAX_FREQ) {
  183. return APLL_MAX_FREQ;
  184. }
  185. float fpll = fout / (2 * (odir+2)); //== fi2s (N=1, b=0, a=1)
  186. return fpll/2;
  187. }
  188. /**
  189. * @brief APLL calculate function, was described by following:
  190. * APLL Output frequency is given by the formula:
  191. *
  192. * apll_freq = xtal_freq * (4 + sdm2 + sdm1/256 + sdm0/65536)/((o_div + 2) * 2)
  193. * apll_freq = fout / ((o_div + 2) * 2)
  194. *
  195. * The dividend in this expression should be in the range of 240 - 600 MHz.
  196. * In rev. 0 of ESP32, sdm0 and sdm1 are unused and always set to 0.
  197. * * sdm0 frequency adjustment parameter, 0..255
  198. * * sdm1 frequency adjustment parameter, 0..255
  199. * * sdm2 frequency adjustment parameter, 0..63
  200. * * o_div frequency divider, 0..31
  201. *
  202. * The most accurate way to find the sdm0..2 and odir parameters is to loop through them all,
  203. * then apply the above formula, finding the closest frequency to the desired one.
  204. * But 256*256*64*32 = 134.217.728 loops are too slow with ESP32
  205. * 1. We will choose the parameters with the highest level of change,
  206. * With 350MHz<fout<500MHz, we limit the sdm2 from 4 to 9,
  207. * Take average frequency close to the desired frequency, and select sdm2
  208. * 2. Next, we look for sequences of less influential and more detailed parameters,
  209. * also by taking the average of the largest and smallest frequencies closer to the desired frequency.
  210. * 3. And finally, loop through all the most detailed of the parameters, finding the best desired frequency
  211. *
  212. * @param[in] rate The I2S Frequency (MCLK)
  213. * @param[in] bits_per_sample The bits per sample
  214. * @param[out] sdm0 The sdm 0
  215. * @param[out] sdm1 The sdm 1
  216. * @param[out] sdm2 The sdm 2
  217. * @param[out] odir The odir
  218. *
  219. * @return ESP_ERR_INVALID_ARG or ESP_OK
  220. */
  221. static esp_err_t i2s_apll_calculate_fi2s(int rate, int bits_per_sample, int *sdm0, int *sdm1, int *sdm2, int *odir)
  222. {
  223. int _odir, _sdm0, _sdm1, _sdm2;
  224. float avg;
  225. float min_rate, max_rate, min_diff;
  226. if (rate/bits_per_sample/2/8 < APLL_I2S_MIN_RATE) {
  227. return ESP_ERR_INVALID_ARG;
  228. }
  229. *sdm0 = 0;
  230. *sdm1 = 0;
  231. *sdm2 = 0;
  232. *odir = 0;
  233. min_diff = APLL_MAX_FREQ;
  234. for (_sdm2 = 4; _sdm2 < 9; _sdm2 ++) {
  235. max_rate = i2s_apll_get_fi2s(bits_per_sample, 255, 255, _sdm2, 0);
  236. min_rate = i2s_apll_get_fi2s(bits_per_sample, 0, 0, _sdm2, 31);
  237. avg = (max_rate + min_rate)/2;
  238. if (abs(avg - rate) < min_diff) {
  239. min_diff = abs(avg - rate);
  240. *sdm2 = _sdm2;
  241. }
  242. }
  243. min_diff = APLL_MAX_FREQ;
  244. for (_odir = 0; _odir < 32; _odir ++) {
  245. max_rate = i2s_apll_get_fi2s(bits_per_sample, 255, 255, *sdm2, _odir);
  246. min_rate = i2s_apll_get_fi2s(bits_per_sample, 0, 0, *sdm2, _odir);
  247. avg = (max_rate + min_rate)/2;
  248. if (abs(avg - rate) < min_diff) {
  249. min_diff = abs(avg - rate);
  250. *odir = _odir;
  251. }
  252. }
  253. min_diff = APLL_MAX_FREQ;
  254. for (_sdm2 = 4; _sdm2 < 9; _sdm2 ++) {
  255. max_rate = i2s_apll_get_fi2s(bits_per_sample, 255, 255, _sdm2, *odir);
  256. min_rate = i2s_apll_get_fi2s(bits_per_sample, 0, 0, _sdm2, *odir);
  257. avg = (max_rate + min_rate)/2;
  258. if (abs(avg - rate) < min_diff) {
  259. min_diff = abs(avg - rate);
  260. *sdm2 = _sdm2;
  261. }
  262. }
  263. min_diff = APLL_MAX_FREQ;
  264. for (_sdm1 = 0; _sdm1 < 256; _sdm1 ++) {
  265. max_rate = i2s_apll_get_fi2s(bits_per_sample, 255, _sdm1, *sdm2, *odir);
  266. min_rate = i2s_apll_get_fi2s(bits_per_sample, 0, _sdm1, *sdm2, *odir);
  267. avg = (max_rate + min_rate)/2;
  268. if (abs(avg - rate) < min_diff) {
  269. min_diff = abs(avg - rate);
  270. *sdm1 = _sdm1;
  271. }
  272. }
  273. min_diff = APLL_MAX_FREQ;
  274. for (_sdm0 = 0; _sdm0 < 256; _sdm0 ++) {
  275. avg = i2s_apll_get_fi2s(bits_per_sample, _sdm0, *sdm1, *sdm2, *odir);
  276. if (abs(avg - rate) < min_diff) {
  277. min_diff = abs(avg - rate);
  278. *sdm0 = _sdm0;
  279. }
  280. }
  281. return ESP_OK;
  282. }
  283. esp_err_t i2s_set_clk(i2s_port_t i2s_num, uint32_t rate, i2s_bits_per_sample_t bits, i2s_channel_t ch)
  284. {
  285. int factor = (256%bits)? 384 : 256; // According to hardware codec requirement(supported 256fs or 384fs)
  286. int clkmInteger, clkmDecimals, bck = 0;
  287. double denom = (double)1 / 64;
  288. int channel = 2;
  289. i2s_dma_t *save_tx = NULL, *save_rx = NULL;
  290. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  291. if (bits % 8 != 0 || bits > I2S_BITS_PER_SAMPLE_32BIT || bits < I2S_BITS_PER_SAMPLE_16BIT) {
  292. ESP_LOGE(I2S_TAG, "Invalid bits per sample");
  293. return ESP_ERR_INVALID_ARG;
  294. }
  295. if (p_i2s_obj[i2s_num] == NULL) {
  296. ESP_LOGE(I2S_TAG, "Not initialized yet");
  297. return ESP_ERR_INVALID_ARG;
  298. }
  299. p_i2s_obj[i2s_num]->sample_rate = rate;
  300. double clkmdiv = (double)I2S_BASE_CLK / (rate * factor);
  301. if (clkmdiv > 256) {
  302. ESP_LOGE(I2S_TAG, "clkmdiv is too large\r\n");
  303. return ESP_ERR_INVALID_ARG;
  304. }
  305. // wait all on-going writing finish
  306. if ((p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) && p_i2s_obj[i2s_num]->tx) {
  307. xSemaphoreTake(p_i2s_obj[i2s_num]->tx->mux, (portTickType)portMAX_DELAY);
  308. }
  309. if ((p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) && p_i2s_obj[i2s_num]->rx) {
  310. xSemaphoreTake(p_i2s_obj[i2s_num]->rx->mux, (portTickType)portMAX_DELAY);
  311. }
  312. i2s_stop(i2s_num);
  313. uint32_t cur_mode = 0;
  314. if (p_i2s_obj[i2s_num]->channel_num != ch) {
  315. p_i2s_obj[i2s_num]->channel_num = (ch == 2) ? 2 : 1;
  316. cur_mode = I2S[i2s_num]->fifo_conf.tx_fifo_mod;
  317. I2S[i2s_num]->fifo_conf.tx_fifo_mod = (ch == 2) ? cur_mode - 1 : cur_mode + 1;
  318. cur_mode = I2S[i2s_num]->fifo_conf.rx_fifo_mod;
  319. I2S[i2s_num]->fifo_conf.rx_fifo_mod = (ch == 2) ? cur_mode -1 : cur_mode + 1;
  320. I2S[i2s_num]->conf_chan.tx_chan_mod = (ch == 2) ? 0 : 1;
  321. I2S[i2s_num]->conf_chan.rx_chan_mod = (ch == 2) ? 0 : 1;
  322. }
  323. if (bits != p_i2s_obj[i2s_num]->bits_per_sample) {
  324. //change fifo mode
  325. if (p_i2s_obj[i2s_num]->bits_per_sample <= 16 && bits > 16) {
  326. I2S[i2s_num]->fifo_conf.tx_fifo_mod += 2;
  327. I2S[i2s_num]->fifo_conf.rx_fifo_mod += 2;
  328. } else if (p_i2s_obj[i2s_num]->bits_per_sample > 16 && bits <= 16) {
  329. I2S[i2s_num]->fifo_conf.tx_fifo_mod -= 2;
  330. I2S[i2s_num]->fifo_conf.rx_fifo_mod -= 2;
  331. }
  332. p_i2s_obj[i2s_num]->bits_per_sample = bits;
  333. p_i2s_obj[i2s_num]->bytes_per_sample = p_i2s_obj[i2s_num]->bits_per_sample / 8;
  334. // Round bytes_per_sample up to next multiple of 16 bits
  335. int halfwords_per_sample = (p_i2s_obj[i2s_num]->bits_per_sample + 15) / 16;
  336. p_i2s_obj[i2s_num]->bytes_per_sample = halfwords_per_sample * 2;
  337. // Because limited of DMA buffer is 4092 bytes
  338. 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) {
  339. p_i2s_obj[i2s_num]->dma_buf_len = 4092 / p_i2s_obj[i2s_num]->bytes_per_sample / p_i2s_obj[i2s_num]->channel_num;
  340. }
  341. // Re-create TX DMA buffer
  342. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) {
  343. save_tx = p_i2s_obj[i2s_num]->tx;
  344. 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);
  345. if (p_i2s_obj[i2s_num]->tx == NULL) {
  346. ESP_LOGE(I2S_TAG, "Failed to create tx dma buffer");
  347. i2s_driver_uninstall(i2s_num);
  348. return ESP_ERR_NO_MEM;
  349. }
  350. I2S[i2s_num]->out_link.addr = (uint32_t) p_i2s_obj[i2s_num]->tx->desc[0];
  351. //destroy old tx dma if exist
  352. if (save_tx) {
  353. i2s_destroy_dma_queue(i2s_num, save_tx);
  354. }
  355. }
  356. // Re-create RX DMA buffer
  357. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) {
  358. save_rx = p_i2s_obj[i2s_num]->rx;
  359. 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);
  360. if (p_i2s_obj[i2s_num]->rx == NULL){
  361. ESP_LOGE(I2S_TAG, "Failed to create rx dma buffer");
  362. i2s_driver_uninstall(i2s_num);
  363. return ESP_ERR_NO_MEM;
  364. }
  365. I2S[i2s_num]->rx_eof_num = (p_i2s_obj[i2s_num]->dma_buf_len * p_i2s_obj[i2s_num]->channel_num * p_i2s_obj[i2s_num]->bytes_per_sample)/4;
  366. I2S[i2s_num]->in_link.addr = (uint32_t) p_i2s_obj[i2s_num]->rx->desc[0];
  367. //destroy old rx dma if exist
  368. if (save_rx) {
  369. i2s_destroy_dma_queue(i2s_num, save_rx);
  370. }
  371. }
  372. }
  373. double mclk;
  374. int sdm0, sdm1, sdm2, odir, m_scale = 8;
  375. int fi2s_clk = rate*channel*bits*m_scale;
  376. if (p_i2s_obj[i2s_num]->mode & (I2S_MODE_DAC_BUILT_IN | I2S_MODE_ADC_BUILT_IN)) {
  377. //DAC uses bclk as sample clock, not WS. WS can be something arbitrary.
  378. //Rate as given to this function is the intended sample rate;
  379. //According to the TRM, WS clk equals to the sample rate, and bclk is double the speed of WS
  380. uint32_t b_clk = rate * I2S_AD_BCK_FACTOR;
  381. fi2s_clk /= I2S_AD_BCK_FACTOR;
  382. int factor2 = 60;
  383. mclk = b_clk * factor2;
  384. clkmdiv = ((double) I2S_BASE_CLK) / mclk;
  385. clkmInteger = clkmdiv;
  386. clkmDecimals = (clkmdiv - clkmInteger) / denom;
  387. bck = mclk / b_clk;
  388. } else if (p_i2s_obj[i2s_num]->mode & I2S_MODE_PDM) {
  389. uint32_t b_clk = 0;
  390. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) {
  391. int fp = I2S[i2s_num]->pdm_freq_conf.tx_pdm_fp;
  392. int fs = I2S[i2s_num]->pdm_freq_conf.tx_pdm_fs;
  393. b_clk = rate * I2S_PDM_BCK_FACTOR * (fp / fs);
  394. fi2s_clk /= (I2S_PDM_BCK_FACTOR * (fp / fs));
  395. } else if (p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) {
  396. b_clk = rate * I2S_PDM_BCK_FACTOR * (I2S[i2s_num]->pdm_conf.rx_sinc_dsr_16_en + 1);
  397. fi2s_clk /= (I2S_PDM_BCK_FACTOR * (I2S[i2s_num]->pdm_conf.rx_sinc_dsr_16_en + 1));
  398. }
  399. int factor2 = 5 ;
  400. mclk = b_clk * factor2;
  401. clkmdiv = ((double) I2S_BASE_CLK) / mclk;
  402. clkmInteger = clkmdiv;
  403. clkmDecimals = (clkmdiv - clkmInteger) / denom;
  404. bck = mclk / b_clk;
  405. } else {
  406. clkmInteger = clkmdiv;
  407. clkmDecimals = (clkmdiv - clkmInteger) / denom;
  408. mclk = clkmInteger + denom * clkmDecimals;
  409. bck = factor/(bits * channel);
  410. }
  411. if(p_i2s_obj[i2s_num]->use_apll && p_i2s_obj[i2s_num]->fixed_mclk) {
  412. fi2s_clk = p_i2s_obj[i2s_num]->fixed_mclk;
  413. m_scale = fi2s_clk/bits/rate/channel;
  414. }
  415. if(p_i2s_obj[i2s_num]->use_apll && i2s_apll_calculate_fi2s(fi2s_clk, bits, &sdm0, &sdm1, &sdm2, &odir) == ESP_OK) {
  416. ESP_LOGD(I2S_TAG, "sdm0=%d, sdm1=%d, sdm2=%d, odir=%d", sdm0, sdm1, sdm2, odir);
  417. rtc_clk_apll_enable(1, sdm0, sdm1, sdm2, odir);
  418. I2S[i2s_num]->clkm_conf.clkm_div_num = 1;
  419. I2S[i2s_num]->clkm_conf.clkm_div_b = 0;
  420. I2S[i2s_num]->clkm_conf.clkm_div_a = 1;
  421. I2S[i2s_num]->sample_rate_conf.tx_bck_div_num = m_scale;
  422. I2S[i2s_num]->sample_rate_conf.rx_bck_div_num = m_scale;
  423. I2S[i2s_num]->clkm_conf.clka_en = 1;
  424. double fi2s_rate = i2s_apll_get_fi2s(bits, sdm0, sdm1, sdm2, odir);
  425. p_i2s_obj[i2s_num]->real_rate = fi2s_rate/bits/channel/m_scale;
  426. 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",
  427. rate, fi2s_rate/bits/channel/m_scale, bits, 1, m_scale, fi2s_rate, fi2s_rate/8, 1, 0);
  428. } else {
  429. I2S[i2s_num]->clkm_conf.clka_en = 0;
  430. I2S[i2s_num]->clkm_conf.clkm_div_a = 63;
  431. I2S[i2s_num]->clkm_conf.clkm_div_b = clkmDecimals;
  432. I2S[i2s_num]->clkm_conf.clkm_div_num = clkmInteger;
  433. I2S[i2s_num]->sample_rate_conf.tx_bck_div_num = bck;
  434. I2S[i2s_num]->sample_rate_conf.rx_bck_div_num = bck;
  435. double real_rate = (double) (I2S_BASE_CLK / (bck * bits * clkmInteger) / 2);
  436. p_i2s_obj[i2s_num]->real_rate = real_rate;
  437. 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",
  438. rate, real_rate, bits, clkmInteger, bck, (double)I2S_BASE_CLK / mclk, real_rate*bits*channel, 64, clkmDecimals);
  439. }
  440. I2S[i2s_num]->sample_rate_conf.tx_bits_mod = bits;
  441. I2S[i2s_num]->sample_rate_conf.rx_bits_mod = bits;
  442. // wait all writing on-going finish
  443. if ((p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) && p_i2s_obj[i2s_num]->tx) {
  444. xSemaphoreGive(p_i2s_obj[i2s_num]->tx->mux);
  445. }
  446. if ((p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) && p_i2s_obj[i2s_num]->rx) {
  447. xSemaphoreGive(p_i2s_obj[i2s_num]->rx->mux);
  448. }
  449. i2s_start(i2s_num);
  450. return ESP_OK;
  451. }
  452. static void IRAM_ATTR i2s_intr_handler_default(void *arg)
  453. {
  454. i2s_obj_t *p_i2s = (i2s_obj_t*) arg;
  455. uint8_t i2s_num = p_i2s->i2s_num;
  456. i2s_dev_t* i2s_reg = I2S[i2s_num];
  457. i2s_event_t i2s_event;
  458. int dummy;
  459. portBASE_TYPE high_priority_task_awoken = 0;
  460. lldesc_t *finish_desc;
  461. if (i2s_reg->int_st.out_dscr_err || i2s_reg->int_st.in_dscr_err) {
  462. ESP_EARLY_LOGE(I2S_TAG, "dma error, interrupt status: 0x%08x", i2s_reg->int_st.val);
  463. if (p_i2s->i2s_queue) {
  464. i2s_event.type = I2S_EVENT_DMA_ERROR;
  465. if (xQueueIsQueueFullFromISR(p_i2s->i2s_queue)) {
  466. xQueueReceiveFromISR(p_i2s->i2s_queue, &dummy, &high_priority_task_awoken);
  467. }
  468. xQueueSendFromISR(p_i2s->i2s_queue, (void * )&i2s_event, &high_priority_task_awoken);
  469. }
  470. }
  471. if (i2s_reg->int_st.out_eof && p_i2s->tx) {
  472. finish_desc = (lldesc_t*) i2s_reg->out_eof_des_addr;
  473. // All buffers are empty. This means we have an underflow on our hands.
  474. if (xQueueIsQueueFullFromISR(p_i2s->tx->queue)) {
  475. xQueueReceiveFromISR(p_i2s->tx->queue, &dummy, &high_priority_task_awoken);
  476. // See if tx descriptor needs to be auto cleared:
  477. // This will avoid any kind of noise that may get introduced due to transmission
  478. // of previous data from tx descriptor on I2S line.
  479. if (p_i2s->tx_desc_auto_clear == true) {
  480. memset((void *) dummy, 0, p_i2s->tx->buf_size);
  481. }
  482. }
  483. xQueueSendFromISR(p_i2s->tx->queue, (void*)(&finish_desc->buf), &high_priority_task_awoken);
  484. if (p_i2s->i2s_queue) {
  485. i2s_event.type = I2S_EVENT_TX_DONE;
  486. if (xQueueIsQueueFullFromISR(p_i2s->i2s_queue)) {
  487. xQueueReceiveFromISR(p_i2s->i2s_queue, &dummy, &high_priority_task_awoken);
  488. }
  489. xQueueSendFromISR(p_i2s->i2s_queue, (void * )&i2s_event, &high_priority_task_awoken);
  490. }
  491. }
  492. if (i2s_reg->int_st.in_suc_eof && p_i2s->rx) {
  493. // All buffers are full. This means we have an overflow.
  494. finish_desc = (lldesc_t*) i2s_reg->in_eof_des_addr;
  495. if (xQueueIsQueueFullFromISR(p_i2s->rx->queue)) {
  496. xQueueReceiveFromISR(p_i2s->rx->queue, &dummy, &high_priority_task_awoken);
  497. }
  498. xQueueSendFromISR(p_i2s->rx->queue, (void*)(&finish_desc->buf), &high_priority_task_awoken);
  499. if (p_i2s->i2s_queue) {
  500. i2s_event.type = I2S_EVENT_RX_DONE;
  501. if (p_i2s->i2s_queue && xQueueIsQueueFullFromISR(p_i2s->i2s_queue)) {
  502. xQueueReceiveFromISR(p_i2s->i2s_queue, &dummy, &high_priority_task_awoken);
  503. }
  504. xQueueSendFromISR(p_i2s->i2s_queue, (void * )&i2s_event, &high_priority_task_awoken);
  505. }
  506. }
  507. if (high_priority_task_awoken == pdTRUE) {
  508. portYIELD_FROM_ISR();
  509. }
  510. i2s_reg->int_clr.val = I2S[i2s_num]->int_st.val;
  511. }
  512. static esp_err_t i2s_destroy_dma_queue(i2s_port_t i2s_num, i2s_dma_t *dma)
  513. {
  514. int bux_idx;
  515. if (p_i2s_obj[i2s_num] == NULL) {
  516. ESP_LOGE(I2S_TAG, "Not initialized yet");
  517. return ESP_ERR_INVALID_ARG;
  518. }
  519. if (dma == NULL) {
  520. ESP_LOGE(I2S_TAG, "dma is NULL");
  521. return ESP_ERR_INVALID_ARG;
  522. }
  523. for (bux_idx = 0; bux_idx < p_i2s_obj[i2s_num]->dma_buf_count; bux_idx++) {
  524. if (dma->desc && dma->desc[bux_idx]) {
  525. free(dma->desc[bux_idx]);
  526. }
  527. if (dma->buf && dma->buf[bux_idx]) {
  528. free(dma->buf[bux_idx]);
  529. }
  530. }
  531. if (dma->buf) {
  532. free(dma->buf);
  533. }
  534. if (dma->desc) {
  535. free(dma->desc);
  536. }
  537. vQueueDelete(dma->queue);
  538. vSemaphoreDelete(dma->mux);
  539. free(dma);
  540. return ESP_OK;
  541. }
  542. static i2s_dma_t *i2s_create_dma_queue(i2s_port_t i2s_num, int dma_buf_count, int dma_buf_len)
  543. {
  544. int bux_idx;
  545. int sample_size = p_i2s_obj[i2s_num]->bytes_per_sample * p_i2s_obj[i2s_num]->channel_num;
  546. i2s_dma_t *dma = (i2s_dma_t*) malloc(sizeof(i2s_dma_t));
  547. if (dma == NULL) {
  548. ESP_LOGE(I2S_TAG, "Error malloc i2s_dma_t");
  549. return NULL;
  550. }
  551. memset(dma, 0, sizeof(i2s_dma_t));
  552. dma->buf = (char **)malloc(sizeof(char*) * dma_buf_count);
  553. if (dma->buf == NULL) {
  554. ESP_LOGE(I2S_TAG, "Error malloc dma buffer pointer");
  555. free(dma);
  556. return NULL;
  557. }
  558. memset(dma->buf, 0, sizeof(char*) * dma_buf_count);
  559. for (bux_idx = 0; bux_idx < dma_buf_count; bux_idx++) {
  560. dma->buf[bux_idx] = (char*) heap_caps_calloc(1, dma_buf_len * sample_size, MALLOC_CAP_DMA);
  561. if (dma->buf[bux_idx] == NULL) {
  562. ESP_LOGE(I2S_TAG, "Error malloc dma buffer");
  563. i2s_destroy_dma_queue(i2s_num, dma);
  564. return NULL;
  565. }
  566. ESP_LOGD(I2S_TAG, "Addr[%d] = %d", bux_idx, (int)dma->buf[bux_idx]);
  567. }
  568. dma->desc = (lldesc_t**) malloc(sizeof(lldesc_t*) * dma_buf_count);
  569. if (dma->desc == NULL) {
  570. ESP_LOGE(I2S_TAG, "Error malloc dma description");
  571. i2s_destroy_dma_queue(i2s_num, dma);
  572. return NULL;
  573. }
  574. for (bux_idx = 0; bux_idx < dma_buf_count; bux_idx++) {
  575. dma->desc[bux_idx] = (lldesc_t*) heap_caps_malloc(sizeof(lldesc_t), MALLOC_CAP_DMA);
  576. if (dma->desc[bux_idx] == NULL) {
  577. ESP_LOGE(I2S_TAG, "Error malloc dma description entry");
  578. i2s_destroy_dma_queue(i2s_num, dma);
  579. return NULL;
  580. }
  581. }
  582. for (bux_idx = 0; bux_idx < dma_buf_count; bux_idx++) {
  583. dma->desc[bux_idx]->owner = 1;
  584. dma->desc[bux_idx]->eof = 1;
  585. dma->desc[bux_idx]->sosf = 0;
  586. dma->desc[bux_idx]->length = dma_buf_len * sample_size;
  587. dma->desc[bux_idx]->size = dma_buf_len * sample_size;
  588. dma->desc[bux_idx]->buf = (uint8_t *) dma->buf[bux_idx];
  589. dma->desc[bux_idx]->offset = 0;
  590. dma->desc[bux_idx]->empty = (uint32_t)((bux_idx < (dma_buf_count - 1)) ? (dma->desc[bux_idx + 1]) : dma->desc[0]);
  591. }
  592. dma->queue = xQueueCreate(dma_buf_count - 1, sizeof(char*));
  593. dma->mux = xSemaphoreCreateMutex();
  594. dma->rw_pos = 0;
  595. dma->buf_size = dma_buf_len * sample_size;
  596. dma->curr_ptr = NULL;
  597. ESP_LOGI(I2S_TAG, "DMA Malloc info, datalen=blocksize=%d, dma_buf_count=%d", dma_buf_len * sample_size, dma_buf_count);
  598. return dma;
  599. }
  600. esp_err_t i2s_start(i2s_port_t i2s_num)
  601. {
  602. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  603. //start DMA link
  604. I2S_ENTER_CRITICAL();
  605. i2s_reset_fifo(i2s_num);
  606. //reset dma
  607. I2S[i2s_num]->lc_conf.in_rst = 1;
  608. I2S[i2s_num]->lc_conf.in_rst = 0;
  609. I2S[i2s_num]->lc_conf.out_rst = 1;
  610. I2S[i2s_num]->lc_conf.out_rst = 0;
  611. I2S[i2s_num]->conf.tx_reset = 1;
  612. I2S[i2s_num]->conf.tx_reset = 0;
  613. I2S[i2s_num]->conf.rx_reset = 1;
  614. I2S[i2s_num]->conf.rx_reset = 0;
  615. esp_intr_disable(p_i2s_obj[i2s_num]->i2s_isr_handle);
  616. I2S[i2s_num]->int_clr.val = 0xFFFFFFFF;
  617. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) {
  618. i2s_enable_tx_intr(i2s_num);
  619. I2S[i2s_num]->out_link.start = 1;
  620. I2S[i2s_num]->conf.tx_start = 1;
  621. }
  622. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) {
  623. i2s_enable_rx_intr(i2s_num);
  624. I2S[i2s_num]->in_link.start = 1;
  625. I2S[i2s_num]->conf.rx_start = 1;
  626. }
  627. esp_intr_enable(p_i2s_obj[i2s_num]->i2s_isr_handle);
  628. I2S_EXIT_CRITICAL();
  629. return ESP_OK;
  630. }
  631. esp_err_t i2s_stop(i2s_port_t i2s_num)
  632. {
  633. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  634. I2S_ENTER_CRITICAL();
  635. esp_intr_disable(p_i2s_obj[i2s_num]->i2s_isr_handle);
  636. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) {
  637. I2S[i2s_num]->out_link.stop = 1;
  638. I2S[i2s_num]->conf.tx_start = 0;
  639. i2s_disable_tx_intr(i2s_num);
  640. }
  641. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) {
  642. I2S[i2s_num]->in_link.stop = 1;
  643. I2S[i2s_num]->conf.rx_start = 0;
  644. i2s_disable_rx_intr(i2s_num);
  645. }
  646. I2S[i2s_num]->int_clr.val = I2S[i2s_num]->int_st.val; //clear pending interrupt
  647. I2S_EXIT_CRITICAL();
  648. return ESP_OK;
  649. }
  650. esp_err_t i2s_set_dac_mode(i2s_dac_mode_t dac_mode)
  651. {
  652. I2S_CHECK((dac_mode < I2S_DAC_CHANNEL_MAX), "i2s dac mode error", ESP_ERR_INVALID_ARG);
  653. if (dac_mode == I2S_DAC_CHANNEL_DISABLE) {
  654. dac_output_disable(DAC_CHANNEL_1);
  655. dac_output_disable(DAC_CHANNEL_2);
  656. dac_i2s_disable();
  657. } else {
  658. dac_i2s_enable();
  659. }
  660. if (dac_mode & I2S_DAC_CHANNEL_RIGHT_EN) {
  661. //DAC1, right channel, GPIO25
  662. dac_output_enable(DAC_CHANNEL_1);
  663. }
  664. if (dac_mode & I2S_DAC_CHANNEL_LEFT_EN) {
  665. //DAC2, left channel, GPIO26
  666. dac_output_enable(DAC_CHANNEL_2);
  667. }
  668. return ESP_OK;
  669. }
  670. static esp_err_t _i2s_adc_mode_recover()
  671. {
  672. I2S_CHECK(((_i2s_adc_unit != -1) && (_i2s_adc_channel != -1)), "i2s ADC recover error, not initialized...", ESP_ERR_INVALID_ARG);
  673. return adc_i2s_mode_init(_i2s_adc_unit, _i2s_adc_channel);
  674. }
  675. esp_err_t i2s_set_adc_mode(adc_unit_t adc_unit, adc1_channel_t adc_channel)
  676. {
  677. I2S_CHECK((adc_unit < ADC_UNIT_2), "i2s ADC unit error, only support ADC1 for now", ESP_ERR_INVALID_ARG);
  678. // For now, we only support SAR ADC1.
  679. _i2s_adc_unit = adc_unit;
  680. _i2s_adc_channel = adc_channel;
  681. return adc_i2s_mode_init(adc_unit, adc_channel);
  682. }
  683. esp_err_t i2s_set_pin(i2s_port_t i2s_num, const i2s_pin_config_t *pin)
  684. {
  685. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  686. if (pin == NULL) {
  687. return i2s_set_dac_mode(I2S_DAC_CHANNEL_BOTH_EN);
  688. }
  689. if (pin->bck_io_num != -1 && !GPIO_IS_VALID_GPIO(pin->bck_io_num)) {
  690. ESP_LOGE(I2S_TAG, "bck_io_num error");
  691. return ESP_FAIL;
  692. }
  693. if (pin->ws_io_num != -1 && !GPIO_IS_VALID_GPIO(pin->ws_io_num)) {
  694. ESP_LOGE(I2S_TAG, "ws_io_num error");
  695. return ESP_FAIL;
  696. }
  697. if (pin->data_out_num != -1 && !GPIO_IS_VALID_OUTPUT_GPIO(pin->data_out_num)) {
  698. ESP_LOGE(I2S_TAG, "data_out_num error");
  699. return ESP_FAIL;
  700. }
  701. if (pin->data_in_num != -1 && !GPIO_IS_VALID_GPIO(pin->data_in_num)) {
  702. ESP_LOGE(I2S_TAG, "data_in_num error");
  703. return ESP_FAIL;
  704. }
  705. int bck_sig = -1, ws_sig = -1, data_out_sig = -1, data_in_sig = -1;
  706. //Each IIS hw module has a RX and TX unit.
  707. //For TX unit, the output signal index should be I2SnO_xxx_OUT_IDX
  708. //For TX unit, the input signal index should be I2SnO_xxx_IN_IDX
  709. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) {
  710. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_MASTER) {
  711. if (i2s_num == I2S_NUM_0) {
  712. bck_sig = I2S0O_BCK_OUT_IDX;
  713. ws_sig = I2S0O_WS_OUT_IDX;
  714. data_out_sig = I2S0O_DATA_OUT23_IDX;
  715. } else {
  716. bck_sig = I2S1O_BCK_OUT_IDX;
  717. ws_sig = I2S1O_WS_OUT_IDX;
  718. data_out_sig = I2S1O_DATA_OUT23_IDX;
  719. }
  720. } else if (p_i2s_obj[i2s_num]->mode & I2S_MODE_SLAVE) {
  721. if (i2s_num == I2S_NUM_0) {
  722. bck_sig = I2S0O_BCK_IN_IDX;
  723. ws_sig = I2S0O_WS_IN_IDX;
  724. data_out_sig = I2S0O_DATA_OUT23_IDX;
  725. } else {
  726. bck_sig = I2S1O_BCK_IN_IDX;
  727. ws_sig = I2S1O_WS_IN_IDX;
  728. data_out_sig = I2S1O_DATA_OUT23_IDX;
  729. }
  730. }
  731. }
  732. //For RX unit, the output signal index should be I2SnI_xxx_OUT_IDX
  733. //For RX unit, the input signal index shuld be I2SnI_xxx_IN_IDX
  734. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) {
  735. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_MASTER) {
  736. if (i2s_num == I2S_NUM_0) {
  737. bck_sig = I2S0I_BCK_OUT_IDX;
  738. ws_sig = I2S0I_WS_OUT_IDX;
  739. data_in_sig = I2S0I_DATA_IN15_IDX;
  740. } else {
  741. bck_sig = I2S1I_BCK_OUT_IDX;
  742. ws_sig = I2S1I_WS_OUT_IDX;
  743. data_in_sig = I2S1I_DATA_IN15_IDX;
  744. }
  745. } else if (p_i2s_obj[i2s_num]->mode & I2S_MODE_SLAVE) {
  746. if (i2s_num == I2S_NUM_0) {
  747. bck_sig = I2S0I_BCK_IN_IDX;
  748. ws_sig = I2S0I_WS_IN_IDX;
  749. data_in_sig = I2S0I_DATA_IN15_IDX;
  750. } else {
  751. bck_sig = I2S1I_BCK_IN_IDX;
  752. ws_sig = I2S1I_WS_IN_IDX;
  753. data_in_sig = I2S1I_DATA_IN15_IDX;
  754. }
  755. }
  756. }
  757. //For "full-duplex + slave" mode, we should select RX signal index for ws and bck.
  758. //For "full-duplex + master" mode, we should select TX signal index for ws and bck.
  759. if ((p_i2s_obj[i2s_num]->mode & I2S_FULL_DUPLEX_SLAVE_MODE_MASK) == I2S_FULL_DUPLEX_SLAVE_MODE_MASK) {
  760. if (i2s_num == I2S_NUM_0) {
  761. bck_sig = I2S0I_BCK_IN_IDX;
  762. ws_sig = I2S0I_WS_IN_IDX;
  763. } else {
  764. bck_sig = I2S1I_BCK_IN_IDX;
  765. ws_sig = I2S1I_WS_IN_IDX;
  766. }
  767. } else if ((p_i2s_obj[i2s_num]->mode & I2S_FULL_DUPLEX_MASTER_MODE_MASK) == I2S_FULL_DUPLEX_MASTER_MODE_MASK) {
  768. if (i2s_num == I2S_NUM_0) {
  769. bck_sig = I2S0O_BCK_OUT_IDX;
  770. ws_sig = I2S0O_WS_OUT_IDX;
  771. } else {
  772. bck_sig = I2S1O_BCK_OUT_IDX;
  773. ws_sig = I2S1O_WS_OUT_IDX;
  774. }
  775. }
  776. gpio_matrix_out_check(pin->data_out_num, data_out_sig, 0, 0);
  777. gpio_matrix_in_check(pin->data_in_num, data_in_sig, 0);
  778. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_MASTER) {
  779. gpio_matrix_out_check(pin->ws_io_num, ws_sig, 0, 0);
  780. gpio_matrix_out_check(pin->bck_io_num, bck_sig, 0, 0);
  781. } else if (p_i2s_obj[i2s_num]->mode & I2S_MODE_SLAVE) {
  782. gpio_matrix_in_check(pin->ws_io_num, ws_sig, 0);
  783. gpio_matrix_in_check(pin->bck_io_num, bck_sig, 0);
  784. }
  785. ESP_LOGD(I2S_TAG, "data: out %d, in: %d, ws: %d, bck: %d", data_out_sig, data_in_sig, ws_sig, bck_sig);
  786. return ESP_OK;
  787. }
  788. esp_err_t i2s_set_sample_rates(i2s_port_t i2s_num, uint32_t rate)
  789. {
  790. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  791. I2S_CHECK((p_i2s_obj[i2s_num]->bytes_per_sample > 0), "bits_per_sample not set", ESP_ERR_INVALID_ARG);
  792. return i2s_set_clk(i2s_num, rate, p_i2s_obj[i2s_num]->bits_per_sample, p_i2s_obj[i2s_num]->channel_num);
  793. }
  794. esp_err_t i2s_set_pdm_rx_down_sample(i2s_port_t i2s_num, i2s_pdm_dsr_t dsr)
  795. {
  796. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  797. I2S[i2s_num]->pdm_conf.rx_sinc_dsr_16_en = dsr;
  798. 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);
  799. }
  800. static esp_err_t i2s_param_config(i2s_port_t i2s_num, const i2s_config_t *i2s_config)
  801. {
  802. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  803. I2S_CHECK((i2s_config), "param null", ESP_ERR_INVALID_ARG);
  804. 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);
  805. 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);
  806. I2S_CHECK(!((i2s_config->mode & I2S_MODE_PDM) && (i2s_num != I2S_NUM_0)), "I2S DAC PDM only support on I2S0", ESP_ERR_INVALID_ARG);
  807. if(i2s_config->mode & I2S_MODE_ADC_BUILT_IN) {
  808. //in ADC built-in mode, we need to call i2s_set_adc_mode to
  809. //initialize the specific ADC channel.
  810. //in the current stage, we only support ADC1 and single channel mode.
  811. //In default data mode, the ADC data is in 12-bit resolution mode.
  812. adc_power_always_on();
  813. }
  814. // configure I2S data port interface.
  815. i2s_reset_fifo(i2s_num);
  816. //reset i2s
  817. I2S[i2s_num]->conf.tx_reset = 1;
  818. I2S[i2s_num]->conf.tx_reset = 0;
  819. I2S[i2s_num]->conf.rx_reset = 1;
  820. I2S[i2s_num]->conf.rx_reset = 0;
  821. //reset dma
  822. I2S[i2s_num]->lc_conf.in_rst = 1;
  823. I2S[i2s_num]->lc_conf.in_rst = 0;
  824. I2S[i2s_num]->lc_conf.out_rst = 1;
  825. I2S[i2s_num]->lc_conf.out_rst = 0;
  826. //Enable and configure DMA
  827. I2S[i2s_num]->lc_conf.check_owner = 0;
  828. I2S[i2s_num]->lc_conf.out_loop_test = 0;
  829. I2S[i2s_num]->lc_conf.out_auto_wrback = 0;
  830. I2S[i2s_num]->lc_conf.out_data_burst_en = 0;
  831. I2S[i2s_num]->lc_conf.outdscr_burst_en = 0;
  832. I2S[i2s_num]->lc_conf.out_no_restart_clr = 0;
  833. I2S[i2s_num]->lc_conf.indscr_burst_en = 0;
  834. I2S[i2s_num]->lc_conf.out_eof_mode = 1;
  835. I2S[i2s_num]->conf2.lcd_en = 0;
  836. I2S[i2s_num]->conf2.camera_en = 0;
  837. I2S[i2s_num]->pdm_conf.pcm2pdm_conv_en = 0;
  838. I2S[i2s_num]->pdm_conf.pdm2pcm_conv_en = 0;
  839. I2S[i2s_num]->fifo_conf.dscr_en = 0;
  840. I2S[i2s_num]->conf_chan.tx_chan_mod = i2s_config->channel_format < I2S_CHANNEL_FMT_ONLY_RIGHT ? i2s_config->channel_format : (i2s_config->channel_format >> 1); // 0-two channel;1-right;2-left;3-righ;4-left
  841. I2S[i2s_num]->fifo_conf.tx_fifo_mod = i2s_config->channel_format < I2S_CHANNEL_FMT_ONLY_RIGHT ? 0 : 1; // 0-right&left channel;1-one channel
  842. I2S[i2s_num]->conf.tx_mono = 0;
  843. I2S[i2s_num]->conf_chan.rx_chan_mod = i2s_config->channel_format < I2S_CHANNEL_FMT_ONLY_RIGHT ? i2s_config->channel_format : (i2s_config->channel_format >> 1); // 0-two channel;1-right;2-left;3-righ;4-left
  844. I2S[i2s_num]->fifo_conf.rx_fifo_mod = i2s_config->channel_format < I2S_CHANNEL_FMT_ONLY_RIGHT ? 0 : 1; // 0-right&left channel;1-one channel
  845. I2S[i2s_num]->conf.rx_mono = 0;
  846. I2S[i2s_num]->fifo_conf.dscr_en = 1;//connect dma to fifo
  847. I2S[i2s_num]->conf.tx_start = 0;
  848. I2S[i2s_num]->conf.rx_start = 0;
  849. if (i2s_config->mode & I2S_MODE_TX) {
  850. I2S[i2s_num]->conf.tx_msb_right = 0;
  851. I2S[i2s_num]->conf.tx_right_first = 0;
  852. I2S[i2s_num]->conf.tx_slave_mod = 0; // Master
  853. I2S[i2s_num]->fifo_conf.tx_fifo_mod_force_en = 1;
  854. if (i2s_config->mode & I2S_MODE_SLAVE) {
  855. I2S[i2s_num]->conf.tx_slave_mod = 1;//TX Slave
  856. }
  857. }
  858. if (i2s_config->mode & I2S_MODE_RX) {
  859. I2S[i2s_num]->conf.rx_msb_right = 0;
  860. I2S[i2s_num]->conf.rx_right_first = 0;
  861. I2S[i2s_num]->conf.rx_slave_mod = 0; // Master
  862. I2S[i2s_num]->fifo_conf.rx_fifo_mod_force_en = 1;
  863. if (i2s_config->mode & I2S_MODE_SLAVE) {
  864. I2S[i2s_num]->conf.rx_slave_mod = 1;//RX Slave
  865. }
  866. }
  867. if (i2s_config->mode & (I2S_MODE_DAC_BUILT_IN | I2S_MODE_ADC_BUILT_IN)) {
  868. I2S[i2s_num]->conf2.lcd_en = 1;
  869. I2S[i2s_num]->conf.tx_right_first = 1;
  870. I2S[i2s_num]->conf2.camera_en = 0;
  871. }
  872. if (i2s_config->mode & I2S_MODE_PDM) {
  873. I2S[i2s_num]->fifo_conf.rx_fifo_mod_force_en = 1;
  874. I2S[i2s_num]->fifo_conf.tx_fifo_mod_force_en = 1;
  875. I2S[i2s_num]->pdm_freq_conf.tx_pdm_fp = 960;
  876. I2S[i2s_num]->pdm_freq_conf.tx_pdm_fs = i2s_config->sample_rate / 1000 * 10;
  877. I2S[i2s_num]->pdm_conf.tx_sinc_osr2 = I2S[i2s_num]->pdm_freq_conf.tx_pdm_fp / I2S[i2s_num]->pdm_freq_conf.tx_pdm_fs;
  878. I2S[i2s_num]->pdm_conf.rx_sinc_dsr_16_en = 0;
  879. I2S[i2s_num]->pdm_conf.rx_pdm_en = 1;
  880. I2S[i2s_num]->pdm_conf.tx_pdm_en = 1;
  881. I2S[i2s_num]->pdm_conf.pcm2pdm_conv_en = 1;
  882. I2S[i2s_num]->pdm_conf.pdm2pcm_conv_en = 1;
  883. } else {
  884. I2S[i2s_num]->pdm_conf.rx_pdm_en = 0;
  885. I2S[i2s_num]->pdm_conf.tx_pdm_en = 0;
  886. }
  887. if (i2s_config->communication_format & I2S_COMM_FORMAT_I2S) {
  888. I2S[i2s_num]->conf.tx_short_sync = 0;
  889. I2S[i2s_num]->conf.rx_short_sync = 0;
  890. I2S[i2s_num]->conf.tx_msb_shift = 1;
  891. I2S[i2s_num]->conf.rx_msb_shift = 1;
  892. if (i2s_config->communication_format & I2S_COMM_FORMAT_I2S_LSB) {
  893. if (i2s_config->mode & I2S_MODE_TX) {
  894. I2S[i2s_num]->conf.tx_msb_shift = 0;
  895. }
  896. if (i2s_config->mode & I2S_MODE_RX) {
  897. I2S[i2s_num]->conf.rx_msb_shift = 0;
  898. }
  899. }
  900. }
  901. if (i2s_config->communication_format & I2S_COMM_FORMAT_PCM) {
  902. I2S[i2s_num]->conf.tx_msb_shift = 0;
  903. I2S[i2s_num]->conf.rx_msb_shift = 0;
  904. I2S[i2s_num]->conf.tx_short_sync = 0;
  905. I2S[i2s_num]->conf.rx_short_sync = 0;
  906. if (i2s_config->communication_format & I2S_COMM_FORMAT_PCM_SHORT) {
  907. if (i2s_config->mode & I2S_MODE_TX) {
  908. I2S[i2s_num]->conf.tx_short_sync = 1;
  909. }
  910. if (i2s_config->mode & I2S_MODE_RX) {
  911. I2S[i2s_num]->conf.rx_short_sync = 1;
  912. }
  913. }
  914. }
  915. if ((p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) && (p_i2s_obj[i2s_num]->mode & I2S_MODE_TX)) {
  916. I2S[i2s_num]->conf.sig_loopback = 1;
  917. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_MASTER) {
  918. I2S[i2s_num]->conf.tx_slave_mod = 0; //MASTER Slave
  919. I2S[i2s_num]->conf.rx_slave_mod = 1; //RX Slave
  920. } else {
  921. I2S[i2s_num]->conf.tx_slave_mod = 1; //RX Slave
  922. I2S[i2s_num]->conf.rx_slave_mod = 1; //RX Slave
  923. }
  924. }
  925. p_i2s_obj[i2s_num]->use_apll = i2s_config->use_apll;
  926. p_i2s_obj[i2s_num]->tx_desc_auto_clear = i2s_config->tx_desc_auto_clear;
  927. p_i2s_obj[i2s_num]->fixed_mclk = i2s_config->fixed_mclk;
  928. return ESP_OK;
  929. }
  930. esp_err_t i2s_zero_dma_buffer(i2s_port_t i2s_num)
  931. {
  932. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  933. if (p_i2s_obj[i2s_num]->rx && p_i2s_obj[i2s_num]->rx->buf != NULL && p_i2s_obj[i2s_num]->rx->buf_size != 0) {
  934. for (int i = 0; i < p_i2s_obj[i2s_num]->dma_buf_count; i++) {
  935. memset(p_i2s_obj[i2s_num]->rx->buf[i], 0, p_i2s_obj[i2s_num]->rx->buf_size);
  936. }
  937. }
  938. if (p_i2s_obj[i2s_num]->tx && p_i2s_obj[i2s_num]->tx->buf != NULL && p_i2s_obj[i2s_num]->tx->buf_size != 0) {
  939. int bytes_left = 0;
  940. bytes_left = (p_i2s_obj[i2s_num]->tx->buf_size - p_i2s_obj[i2s_num]->tx->rw_pos) % 4;
  941. if (bytes_left) {
  942. size_t zero_bytes = 0, bytes_written;
  943. i2s_write(i2s_num, (void *)&zero_bytes, bytes_left, &bytes_written, portMAX_DELAY);
  944. }
  945. for (int i = 0; i < p_i2s_obj[i2s_num]->dma_buf_count; i++) {
  946. memset(p_i2s_obj[i2s_num]->tx->buf[i], 0, p_i2s_obj[i2s_num]->tx->buf_size);
  947. }
  948. }
  949. return ESP_OK;
  950. }
  951. esp_err_t i2s_driver_install(i2s_port_t i2s_num, const i2s_config_t *i2s_config, int queue_size, void* i2s_queue)
  952. {
  953. esp_err_t err;
  954. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  955. I2S_CHECK((i2s_config != NULL), "I2S configuration must not NULL", ESP_ERR_INVALID_ARG);
  956. 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);
  957. 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);
  958. if (p_i2s_obj[i2s_num] == NULL) {
  959. p_i2s_obj[i2s_num] = (i2s_obj_t*) malloc(sizeof(i2s_obj_t));
  960. if (p_i2s_obj[i2s_num] == NULL) {
  961. ESP_LOGE(I2S_TAG, "Malloc I2S driver error");
  962. return ESP_ERR_NO_MEM;
  963. }
  964. memset(p_i2s_obj[i2s_num], 0, sizeof(i2s_obj_t));
  965. p_i2s_obj[i2s_num]->i2s_num = i2s_num;
  966. p_i2s_obj[i2s_num]->dma_buf_count = i2s_config->dma_buf_count;
  967. p_i2s_obj[i2s_num]->dma_buf_len = i2s_config->dma_buf_len;
  968. p_i2s_obj[i2s_num]->i2s_queue = i2s_queue;
  969. p_i2s_obj[i2s_num]->mode = i2s_config->mode;
  970. p_i2s_obj[i2s_num]->bits_per_sample = 0;
  971. p_i2s_obj[i2s_num]->bytes_per_sample = 0; // Not initialized yet
  972. p_i2s_obj[i2s_num]->channel_num = i2s_config->channel_format < I2S_CHANNEL_FMT_ONLY_RIGHT ? 2 : 1;
  973. #ifdef CONFIG_PM_ENABLE
  974. if (i2s_config->use_apll) {
  975. err = esp_pm_lock_create(ESP_PM_NO_LIGHT_SLEEP, 0, "i2s_driver", &p_i2s_obj[i2s_num]->pm_lock);
  976. } else {
  977. err = esp_pm_lock_create(ESP_PM_APB_FREQ_MAX, 0, "i2s_driver", &p_i2s_obj[i2s_num]->pm_lock);
  978. }
  979. if (err != ESP_OK) {
  980. free(p_i2s_obj[i2s_num]);
  981. p_i2s_obj[i2s_num] = NULL;
  982. ESP_LOGE(I2S_TAG, "I2S pm lock error");
  983. return err;
  984. }
  985. #endif //CONFIG_PM_ENABLE
  986. //To make sure hardware is enabled before any hardware register operations.
  987. if (i2s_num == I2S_NUM_1) {
  988. periph_module_reset(PERIPH_I2S1_MODULE);
  989. periph_module_enable(PERIPH_I2S1_MODULE);
  990. } else {
  991. periph_module_reset(PERIPH_I2S0_MODULE);
  992. periph_module_enable(PERIPH_I2S0_MODULE);
  993. }
  994. //initial interrupt
  995. 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);
  996. if (err != ESP_OK) {
  997. #ifdef CONFIG_PM_ENABLE
  998. if (p_i2s_obj[i2s_num]->pm_lock) {
  999. esp_pm_lock_delete(p_i2s_obj[i2s_num]->pm_lock);
  1000. }
  1001. #endif
  1002. free(p_i2s_obj[i2s_num]);
  1003. p_i2s_obj[i2s_num] = NULL;
  1004. ESP_LOGE(I2S_TAG, "Register I2S Interrupt error");
  1005. return err;
  1006. }
  1007. i2s_stop(i2s_num);
  1008. err = i2s_param_config(i2s_num, i2s_config);
  1009. if (err != ESP_OK) {
  1010. i2s_driver_uninstall(i2s_num);
  1011. ESP_LOGE(I2S_TAG, "I2S param configure error");
  1012. return err;
  1013. }
  1014. if (i2s_queue) {
  1015. p_i2s_obj[i2s_num]->i2s_queue = xQueueCreate(queue_size, sizeof(i2s_event_t));
  1016. *((QueueHandle_t*) i2s_queue) = p_i2s_obj[i2s_num]->i2s_queue;
  1017. ESP_LOGI(I2S_TAG, "queue free spaces: %d", uxQueueSpacesAvailable(p_i2s_obj[i2s_num]->i2s_queue));
  1018. } else {
  1019. p_i2s_obj[i2s_num]->i2s_queue = NULL;
  1020. }
  1021. //set clock and start
  1022. return i2s_set_clk(i2s_num, i2s_config->sample_rate, i2s_config->bits_per_sample, p_i2s_obj[i2s_num]->channel_num);
  1023. }
  1024. ESP_LOGW(I2S_TAG, "I2S driver already installed");
  1025. return ESP_OK;
  1026. }
  1027. esp_err_t i2s_driver_uninstall(i2s_port_t i2s_num)
  1028. {
  1029. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  1030. if (p_i2s_obj[i2s_num] == NULL) {
  1031. ESP_LOGI(I2S_TAG, "already uninstalled");
  1032. return ESP_OK;
  1033. }
  1034. i2s_stop(i2s_num);
  1035. esp_intr_free(p_i2s_obj[i2s_num]->i2s_isr_handle);
  1036. if (p_i2s_obj[i2s_num]->tx != NULL && p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) {
  1037. i2s_destroy_dma_queue(i2s_num, p_i2s_obj[i2s_num]->tx);
  1038. p_i2s_obj[i2s_num]->tx = NULL;
  1039. }
  1040. if (p_i2s_obj[i2s_num]->rx != NULL && p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) {
  1041. i2s_destroy_dma_queue(i2s_num, p_i2s_obj[i2s_num]->rx);
  1042. p_i2s_obj[i2s_num]->rx = NULL;
  1043. }
  1044. if (p_i2s_obj[i2s_num]->i2s_queue) {
  1045. vQueueDelete(p_i2s_obj[i2s_num]->i2s_queue);
  1046. p_i2s_obj[i2s_num]->i2s_queue = NULL;
  1047. }
  1048. if(p_i2s_obj[i2s_num]->use_apll) {
  1049. rtc_clk_apll_enable(0, 0, 0, 0, 0);
  1050. }
  1051. #ifdef CONFIG_PM_ENABLE
  1052. if (p_i2s_obj[i2s_num]->pm_lock) {
  1053. esp_pm_lock_delete(p_i2s_obj[i2s_num]->pm_lock);
  1054. }
  1055. #endif
  1056. free(p_i2s_obj[i2s_num]);
  1057. p_i2s_obj[i2s_num] = NULL;
  1058. if (i2s_num == I2S_NUM_0) {
  1059. periph_module_disable(PERIPH_I2S0_MODULE);
  1060. } else if (i2s_num == I2S_NUM_1) {
  1061. periph_module_disable(PERIPH_I2S1_MODULE);
  1062. }
  1063. return ESP_OK;
  1064. }
  1065. 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)
  1066. {
  1067. char *data_ptr, *src_byte;
  1068. int bytes_can_write;
  1069. *bytes_written = 0;
  1070. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  1071. I2S_CHECK((size < I2S_MAX_BUFFER_SIZE), "size is too large", ESP_ERR_INVALID_ARG);
  1072. I2S_CHECK((p_i2s_obj[i2s_num]->tx), "tx NULL", ESP_ERR_INVALID_ARG);
  1073. xSemaphoreTake(p_i2s_obj[i2s_num]->tx->mux, (portTickType)portMAX_DELAY);
  1074. #ifdef CONFIG_PM_ENABLE
  1075. esp_pm_lock_acquire(p_i2s_obj[i2s_num]->pm_lock);
  1076. #endif
  1077. src_byte = (char *)src;
  1078. while (size > 0) {
  1079. 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) {
  1080. if (xQueueReceive(p_i2s_obj[i2s_num]->tx->queue, &p_i2s_obj[i2s_num]->tx->curr_ptr, ticks_to_wait) == pdFALSE) {
  1081. break;
  1082. }
  1083. p_i2s_obj[i2s_num]->tx->rw_pos = 0;
  1084. }
  1085. 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);
  1086. data_ptr = (char*)p_i2s_obj[i2s_num]->tx->curr_ptr;
  1087. data_ptr += p_i2s_obj[i2s_num]->tx->rw_pos;
  1088. bytes_can_write = p_i2s_obj[i2s_num]->tx->buf_size - p_i2s_obj[i2s_num]->tx->rw_pos;
  1089. if (bytes_can_write > size) {
  1090. bytes_can_write = size;
  1091. }
  1092. memcpy(data_ptr, src_byte, bytes_can_write);
  1093. size -= bytes_can_write;
  1094. src_byte += bytes_can_write;
  1095. p_i2s_obj[i2s_num]->tx->rw_pos += bytes_can_write;
  1096. (*bytes_written) += bytes_can_write;
  1097. }
  1098. #ifdef CONFIG_PM_ENABLE
  1099. esp_pm_lock_release(p_i2s_obj[i2s_num]->pm_lock);
  1100. #endif
  1101. xSemaphoreGive(p_i2s_obj[i2s_num]->tx->mux);
  1102. return ESP_OK;
  1103. }
  1104. esp_err_t i2s_adc_enable(i2s_port_t i2s_num)
  1105. {
  1106. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  1107. I2S_CHECK((p_i2s_obj[i2s_num] != NULL), "Not initialized yet", ESP_ERR_INVALID_STATE);
  1108. I2S_CHECK((p_i2s_obj[i2s_num]->mode & I2S_MODE_ADC_BUILT_IN), "i2s built-in adc not enabled", ESP_ERR_INVALID_STATE);
  1109. adc1_i2s_mode_acquire();
  1110. _i2s_adc_mode_recover();
  1111. 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);
  1112. }
  1113. esp_err_t i2s_adc_disable(i2s_port_t i2s_num)
  1114. {
  1115. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  1116. I2S_CHECK((p_i2s_obj[i2s_num] != NULL), "Not initialized yet", ESP_ERR_INVALID_STATE);
  1117. I2S_CHECK((p_i2s_obj[i2s_num]->mode & I2S_MODE_ADC_BUILT_IN), "i2s built-in adc not enabled", ESP_ERR_INVALID_STATE);
  1118. adc1_lock_release();
  1119. return ESP_OK;
  1120. }
  1121. 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)
  1122. {
  1123. char *data_ptr;
  1124. int bytes_can_write, tail;
  1125. int src_bytes, aim_bytes, zero_bytes;
  1126. *bytes_written = 0;
  1127. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  1128. I2S_CHECK((size > 0), "size must greater than zero", ESP_ERR_INVALID_ARG);
  1129. I2S_CHECK((aim_bits * size < I2S_MAX_BUFFER_SIZE), "size is too large", ESP_ERR_INVALID_ARG);
  1130. I2S_CHECK((aim_bits >= src_bits), "aim_bits musn't less than src_bits", ESP_ERR_INVALID_ARG);
  1131. I2S_CHECK((p_i2s_obj[i2s_num]->tx), "tx NULL", ESP_ERR_INVALID_ARG);
  1132. if (src_bits < I2S_BITS_PER_SAMPLE_8BIT || aim_bits < I2S_BITS_PER_SAMPLE_8BIT) {
  1133. ESP_LOGE(I2S_TAG,"bits musn't be less than 8, src_bits %d aim_bits %d", src_bits, aim_bits);
  1134. return ESP_ERR_INVALID_ARG;
  1135. }
  1136. if (src_bits > I2S_BITS_PER_SAMPLE_32BIT || aim_bits > I2S_BITS_PER_SAMPLE_32BIT) {
  1137. ESP_LOGE(I2S_TAG,"bits musn't be greater than 32, src_bits %d aim_bits %d", src_bits, aim_bits);
  1138. return ESP_ERR_INVALID_ARG;
  1139. }
  1140. if ((src_bits == I2S_BITS_PER_SAMPLE_16BIT || src_bits == I2S_BITS_PER_SAMPLE_32BIT) && (size % 2 != 0)) {
  1141. ESP_LOGE(I2S_TAG,"size must be a even number while src_bits is even, src_bits %d size %d", src_bits, size);
  1142. return ESP_ERR_INVALID_ARG;
  1143. }
  1144. if (src_bits == I2S_BITS_PER_SAMPLE_24BIT && (size % 3 != 0)) {
  1145. ESP_LOGE(I2S_TAG,"size must be a multiple of 3 while src_bits is 24, size %d", size);
  1146. return ESP_ERR_INVALID_ARG;
  1147. }
  1148. src_bytes = src_bits / 8;
  1149. aim_bytes = aim_bits / 8;
  1150. zero_bytes = aim_bytes - src_bytes;
  1151. xSemaphoreTake(p_i2s_obj[i2s_num]->tx->mux, (portTickType)portMAX_DELAY);
  1152. size = size * aim_bytes / src_bytes;
  1153. ESP_LOGD(I2S_TAG,"aim_bytes %d src_bytes %d size %d", aim_bytes, src_bytes, size);
  1154. while (size > 0) {
  1155. 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) {
  1156. if (xQueueReceive(p_i2s_obj[i2s_num]->tx->queue, &p_i2s_obj[i2s_num]->tx->curr_ptr, ticks_to_wait) == pdFALSE) {
  1157. break;
  1158. }
  1159. p_i2s_obj[i2s_num]->tx->rw_pos = 0;
  1160. }
  1161. data_ptr = (char*)p_i2s_obj[i2s_num]->tx->curr_ptr;
  1162. data_ptr += p_i2s_obj[i2s_num]->tx->rw_pos;
  1163. bytes_can_write = p_i2s_obj[i2s_num]->tx->buf_size - p_i2s_obj[i2s_num]->tx->rw_pos;
  1164. if (bytes_can_write > size) {
  1165. bytes_can_write = size;
  1166. }
  1167. tail = bytes_can_write % aim_bytes;
  1168. bytes_can_write = bytes_can_write - tail;
  1169. memset(data_ptr, 0, bytes_can_write);
  1170. for (int j = 0; j < bytes_can_write; j += (aim_bytes - zero_bytes)) {
  1171. j += zero_bytes;
  1172. memcpy(&data_ptr[j], (const char *)(src + *bytes_written), aim_bytes - zero_bytes);
  1173. (*bytes_written) += (aim_bytes - zero_bytes);
  1174. }
  1175. size -= bytes_can_write;
  1176. p_i2s_obj[i2s_num]->tx->rw_pos += bytes_can_write;
  1177. }
  1178. xSemaphoreGive(p_i2s_obj[i2s_num]->tx->mux);
  1179. return ESP_OK;
  1180. }
  1181. esp_err_t i2s_read(i2s_port_t i2s_num, void *dest, size_t size, size_t *bytes_read, TickType_t ticks_to_wait)
  1182. {
  1183. char *data_ptr, *dest_byte;
  1184. int bytes_can_read;
  1185. *bytes_read = 0;
  1186. dest_byte = (char *)dest;
  1187. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  1188. I2S_CHECK((size < I2S_MAX_BUFFER_SIZE), "size is too large", ESP_ERR_INVALID_ARG);
  1189. I2S_CHECK((p_i2s_obj[i2s_num]->rx), "rx NULL", ESP_ERR_INVALID_ARG);
  1190. xSemaphoreTake(p_i2s_obj[i2s_num]->rx->mux, (portTickType)portMAX_DELAY);
  1191. #ifdef CONFIG_PM_ENABLE
  1192. esp_pm_lock_acquire(p_i2s_obj[i2s_num]->pm_lock);
  1193. #endif
  1194. while (size > 0) {
  1195. 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) {
  1196. if (xQueueReceive(p_i2s_obj[i2s_num]->rx->queue, &p_i2s_obj[i2s_num]->rx->curr_ptr, ticks_to_wait) == pdFALSE) {
  1197. break;
  1198. }
  1199. p_i2s_obj[i2s_num]->rx->rw_pos = 0;
  1200. }
  1201. data_ptr = (char*)p_i2s_obj[i2s_num]->rx->curr_ptr;
  1202. data_ptr += p_i2s_obj[i2s_num]->rx->rw_pos;
  1203. bytes_can_read = p_i2s_obj[i2s_num]->rx->buf_size - p_i2s_obj[i2s_num]->rx->rw_pos;
  1204. if (bytes_can_read > size) {
  1205. bytes_can_read = size;
  1206. }
  1207. memcpy(dest_byte, data_ptr, bytes_can_read);
  1208. size -= bytes_can_read;
  1209. dest_byte += bytes_can_read;
  1210. p_i2s_obj[i2s_num]->rx->rw_pos += bytes_can_read;
  1211. (*bytes_read) += bytes_can_read;
  1212. }
  1213. #ifdef CONFIG_PM_ENABLE
  1214. esp_pm_lock_release(p_i2s_obj[i2s_num]->pm_lock);
  1215. #endif
  1216. xSemaphoreGive(p_i2s_obj[i2s_num]->rx->mux);
  1217. return ESP_OK;
  1218. }