i2s.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  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 <esp_types.h>
  15. #include "freertos/FreeRTOS.h"
  16. #include "freertos/queue.h"
  17. #include "freertos/xtensa_api.h"
  18. #include "soc/dport_reg.h"
  19. #include "soc/rtc_cntl_reg.h"
  20. #include "soc/rtc_io_reg.h"
  21. #include "soc/sens_reg.h"
  22. #include "rom/lldesc.h"
  23. #include "driver/gpio.h"
  24. #include "driver/i2s.h"
  25. #include "esp_intr.h"
  26. #include "esp_err.h"
  27. #include "esp_log.h"
  28. static const char* I2S_TAG = "I2S";
  29. #define I2S_CHECK(a, str, ret) if (!(a)) { \
  30. ESP_LOGE(I2S_TAG,"%s:%d (%s):%s", __FILE__, __LINE__, __FUNCTION__, str); \
  31. return (ret); \
  32. }
  33. #define I2S_BASE_CLK (2*APB_CLK_FREQ)
  34. #define I2S_ENTER_CRITICAL_ISR() portENTER_CRITICAL_ISR(&i2s_spinlock[i2s_num])
  35. #define I2S_EXIT_CRITICAL_ISR() portEXIT_CRITICAL_ISR(&i2s_spinlock[i2s_num])
  36. #define I2S_ENTER_CRITICAL() portENTER_CRITICAL(&i2s_spinlock[i2s_num])
  37. #define I2S_EXIT_CRITICAL() portEXIT_CRITICAL(&i2s_spinlock[i2s_num])
  38. #define gpio_matrix_out_check(a, b, c, d) if(a != -1) gpio_matrix_out(a, b, c, d) //if pin = -1, do not need to configure
  39. #define gpio_matrix_in_check(a, b, c) if(a != -1) gpio_matrix_in(a, b, c)
  40. /**
  41. * @brief DMA buffer object
  42. *
  43. */
  44. typedef struct {
  45. char **buf;
  46. int buf_size;
  47. int rw_pos;
  48. void *curr_ptr;
  49. SemaphoreHandle_t mux;
  50. xQueueHandle queue;
  51. lldesc_t **desc;
  52. } i2s_dma_t;
  53. /**
  54. * @brief I2S object instance
  55. *
  56. */
  57. typedef struct {
  58. i2s_port_t i2s_num; /*!< I2S port number*/
  59. int queue_size; /*!< I2S event queue size*/
  60. QueueHandle_t i2s_queue; /*!< I2S queue handler*/
  61. int dma_buf_count; /*!< DMA buffer count, number of buffer*/
  62. int dma_buf_len; /*!< DMA buffer length, length of each buffer*/
  63. i2s_dma_t *rx; /*!< DMA Tx buffer*/
  64. i2s_dma_t *tx; /*!< DMA Rx buffer*/
  65. i2s_isr_handle_t i2s_isr_handle; /*!< I2S Interrupt handle*/
  66. int channel_num; /*!< Number of channels*/
  67. int bytes_per_sample; /*!< Bytes per sample*/
  68. i2s_mode_t mode; /*!< I2S Working mode*/
  69. } i2s_obj_t;
  70. static i2s_obj_t *p_i2s_obj[I2S_NUM_MAX] = {0};
  71. static i2s_dev_t* I2S[I2S_NUM_MAX] = {&I2S0, &I2S1};
  72. static portMUX_TYPE i2s_spinlock[I2S_NUM_MAX] = {portMUX_INITIALIZER_UNLOCKED, portMUX_INITIALIZER_UNLOCKED};
  73. static esp_err_t i2s_reset_fifo(i2s_port_t i2s_num)
  74. {
  75. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_FAIL);
  76. I2S_ENTER_CRITICAL();
  77. I2S[i2s_num]->conf.rx_fifo_reset = 1;
  78. I2S[i2s_num]->conf.rx_fifo_reset = 0;
  79. I2S[i2s_num]->conf.tx_fifo_reset = 1;
  80. I2S[i2s_num]->conf.tx_fifo_reset = 0;
  81. I2S_EXIT_CRITICAL();
  82. return ESP_OK;
  83. }
  84. esp_err_t i2s_clear_intr_status(i2s_port_t i2s_num, uint32_t clr_mask)
  85. {
  86. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_FAIL);
  87. I2S[i2s_num]->int_clr.val = clr_mask;
  88. return ESP_OK;
  89. }
  90. esp_err_t i2s_enable_rx_intr(i2s_port_t i2s_num)
  91. {
  92. I2S_ENTER_CRITICAL();
  93. I2S[i2s_num]->int_ena.in_suc_eof = 1;
  94. I2S[i2s_num]->int_ena.in_dscr_err = 1;
  95. I2S_EXIT_CRITICAL();
  96. return ESP_OK;
  97. }
  98. esp_err_t i2s_disable_rx_intr(i2s_port_t i2s_num)
  99. {
  100. I2S_ENTER_CRITICAL();
  101. I2S[i2s_num]->int_ena.in_suc_eof = 0;
  102. I2S[i2s_num]->int_ena.in_dscr_err = 0;
  103. I2S_EXIT_CRITICAL();
  104. return ESP_OK;
  105. }
  106. esp_err_t i2s_disable_tx_intr(i2s_port_t i2s_num)
  107. {
  108. I2S_ENTER_CRITICAL();
  109. I2S[i2s_num]->int_ena.out_eof = 0;
  110. I2S[i2s_num]->int_ena.out_dscr_err = 0;
  111. I2S_EXIT_CRITICAL();
  112. return ESP_OK;
  113. }
  114. esp_err_t i2s_enable_tx_intr(i2s_port_t i2s_num)
  115. {
  116. I2S_ENTER_CRITICAL();
  117. I2S[i2s_num]->int_ena.out_eof = 1;
  118. I2S[i2s_num]->int_ena.out_dscr_err = 1;
  119. I2S_EXIT_CRITICAL();
  120. return ESP_OK;
  121. }
  122. static esp_err_t i2s_isr_register(i2s_port_t i2s_num, uint8_t intr_alloc_flags, void (*fn)(void*), void * arg, i2s_isr_handle_t *handle)
  123. {
  124. return esp_intr_alloc(ETS_I2S0_INTR_SOURCE + i2s_num, intr_alloc_flags, fn, arg, handle);
  125. }
  126. static esp_err_t i2s_set_clk(i2s_port_t i2s_num, uint32_t rate, uint8_t bits, bool fuzzy)
  127. {
  128. int factor = (256%bits)? 384 : 256; // According to hardware codec requirement(supported 256fs or 384fs)
  129. int clkmInteger, clkmDecimals, bck = 0;
  130. float denom = (float)1 / 64;
  131. int channel = 2;
  132. float clkmdiv = (float)I2S_BASE_CLK / (rate * factor);
  133. if (clkmdiv > 256) {
  134. ESP_LOGE(I2S_TAG, "clkmdiv is too large\r\n");
  135. return ESP_FAIL;
  136. }
  137. clkmInteger = clkmdiv;
  138. clkmDecimals = (clkmdiv - clkmInteger) / denom;
  139. float mclk = clkmInteger + denom * clkmDecimals;
  140. bck = factor/(bits * channel);
  141. I2S[i2s_num]->clkm_conf.clka_en = 0;
  142. I2S[i2s_num]->clkm_conf.clkm_div_a = 63;
  143. I2S[i2s_num]->clkm_conf.clkm_div_b = clkmDecimals;
  144. I2S[i2s_num]->clkm_conf.clkm_div_num = clkmInteger;
  145. I2S[i2s_num]->sample_rate_conf.tx_bck_div_num = bck;
  146. I2S[i2s_num]->sample_rate_conf.rx_bck_div_num = bck;
  147. I2S[i2s_num]->sample_rate_conf.tx_bits_mod = bits;
  148. I2S[i2s_num]->sample_rate_conf.rx_bits_mod = bits;
  149. float real_rate = (float)(I2S_BASE_CLK / (bck * bits * clkmInteger)/2);
  150. ESP_LOGI(I2S_TAG, "Req RATE: %d, real rate: %0.3f, BITS: %u, CLKM: %u, BCK: %u, MCLK: %0.3f, SCLK: %f, diva: %d, divb: %d",
  151. rate, real_rate, bits, clkmInteger, bck, (float)I2S_BASE_CLK / mclk, real_rate *16*2, 64, clkmDecimals);
  152. return ESP_OK;
  153. }
  154. static void IRAM_ATTR i2s_intr_handler_default(void *arg)
  155. {
  156. i2s_obj_t *p_i2s = (i2s_obj_t*) arg;
  157. uint8_t i2s_num = p_i2s->i2s_num;
  158. i2s_dev_t* i2s_reg = I2S[i2s_num];
  159. i2s_event_t i2s_event;
  160. int dummy;
  161. portBASE_TYPE high_priority_task_awoken = 0;
  162. lldesc_t *finish_desc;
  163. if (i2s_reg->int_st.out_dscr_err || i2s_reg->int_st.in_dscr_err) {
  164. ESP_LOGE(I2S_TAG, "out_dscr_err: %d or in_dscr_err:%d", i2s_reg->int_st.out_dscr_err == 1, i2s_reg->int_st.in_dscr_err == 1);
  165. if (p_i2s->i2s_queue) {
  166. i2s_event.type = I2S_EVENT_DMA_ERROR;
  167. if (xQueueIsQueueFullFromISR(p_i2s->i2s_queue)) {
  168. xQueueReceiveFromISR(p_i2s->i2s_queue, &dummy, &high_priority_task_awoken);
  169. }
  170. xQueueSendFromISR(p_i2s->i2s_queue, (void * )&i2s_event, &high_priority_task_awoken);
  171. }
  172. }
  173. if (i2s_reg->int_st.out_eof && p_i2s->tx) {
  174. finish_desc = (lldesc_t*) i2s_reg->out_eof_des_addr;
  175. // All buffers are empty. This means we have an underflow on our hands.
  176. if (xQueueIsQueueFullFromISR(p_i2s->tx->queue)) {
  177. xQueueReceiveFromISR(p_i2s->tx->queue, &dummy, &high_priority_task_awoken);
  178. }
  179. xQueueSendFromISR(p_i2s->tx->queue, (void*)(&finish_desc->buf), &high_priority_task_awoken);
  180. if (p_i2s->i2s_queue) {
  181. i2s_event.type = I2S_EVENT_TX_DONE;
  182. if (xQueueIsQueueFullFromISR(p_i2s->i2s_queue)) {
  183. xQueueReceiveFromISR(p_i2s->i2s_queue, &dummy, &high_priority_task_awoken);
  184. }
  185. xQueueSendFromISR(p_i2s->i2s_queue, (void * )&i2s_event, &high_priority_task_awoken);
  186. }
  187. }
  188. if (i2s_reg->int_st.in_suc_eof && p_i2s->rx) {
  189. // All buffers are full. This means we have an overflow.
  190. finish_desc = (lldesc_t*) i2s_reg->in_eof_des_addr;
  191. if (xQueueIsQueueFullFromISR(p_i2s->rx->queue)) {
  192. xQueueReceiveFromISR(p_i2s->rx->queue, &dummy, &high_priority_task_awoken);
  193. }
  194. xQueueSendFromISR(p_i2s->rx->queue, (void*)(&finish_desc->buf), &high_priority_task_awoken);
  195. if (p_i2s->i2s_queue) {
  196. i2s_event.type = I2S_EVENT_RX_DONE;
  197. if (p_i2s->i2s_queue && xQueueIsQueueFullFromISR(p_i2s->i2s_queue)) {
  198. xQueueReceiveFromISR(p_i2s->i2s_queue, &dummy, &high_priority_task_awoken);
  199. }
  200. xQueueSendFromISR(p_i2s->i2s_queue, (void * )&i2s_event, &high_priority_task_awoken);
  201. }
  202. }
  203. if (high_priority_task_awoken == pdTRUE) {
  204. portYIELD_FROM_ISR();
  205. }
  206. i2s_reg->int_clr.val = I2S[i2s_num]->int_st.val;
  207. }
  208. static esp_err_t i2s_destroy_dma_queue(i2s_port_t i2s_num, i2s_dma_t *dma)
  209. {
  210. int bux_idx;
  211. if (p_i2s_obj[i2s_num] == NULL) {
  212. ESP_LOGE(I2S_TAG, "Not initialized yet");
  213. return ESP_FAIL;
  214. }
  215. if (dma == NULL) {
  216. return ESP_FAIL;
  217. }
  218. for (bux_idx = 0; bux_idx < p_i2s_obj[i2s_num]->dma_buf_count; bux_idx++) {
  219. if (dma->desc && dma->desc[bux_idx])
  220. free(dma->desc[bux_idx]);
  221. if (dma->buf && dma->buf[bux_idx])
  222. free(dma->buf[bux_idx]);
  223. }
  224. if (dma->buf)
  225. free(dma->buf);
  226. if (dma->desc)
  227. free(dma->desc);
  228. vQueueDelete(dma->queue);
  229. vSemaphoreDelete(dma->mux);
  230. return ESP_OK;
  231. }
  232. static i2s_dma_t *i2s_create_dma_queue(i2s_port_t i2s_num, int dma_buf_count, int dma_buf_len)
  233. {
  234. int bux_idx;
  235. int sample_size = p_i2s_obj[i2s_num]->bytes_per_sample * p_i2s_obj[i2s_num]->channel_num;
  236. i2s_dma_t *dma = (i2s_dma_t*) malloc(sizeof(i2s_dma_t));
  237. if (dma == NULL) {
  238. ESP_LOGE(I2S_TAG, "Error malloc i2s_dma_t");
  239. return NULL;
  240. }
  241. memset(dma, 0, sizeof(i2s_dma_t));
  242. dma->buf = (char **)malloc(sizeof(char*) * dma_buf_count);
  243. if (dma->buf == NULL) {
  244. ESP_LOGE(I2S_TAG, "Error malloc dma buffer pointer");
  245. return NULL;
  246. }
  247. memset(dma->buf, 0, sizeof(char*) * dma_buf_count);
  248. for (bux_idx = 0; bux_idx < dma_buf_count; bux_idx++) {
  249. dma->buf[bux_idx] = (char*) malloc(dma_buf_len * sample_size);
  250. if (dma->buf[bux_idx] == NULL) {
  251. ESP_LOGE(I2S_TAG, "Error malloc dma buffer");
  252. i2s_destroy_dma_queue(i2s_num, dma);
  253. return NULL;
  254. }
  255. ESP_LOGD(I2S_TAG, "Addr[%d] = %d", bux_idx, (int)dma->buf[bux_idx]);
  256. memset(dma->buf[bux_idx], 0, dma_buf_len * sample_size);
  257. }
  258. dma->desc = (lldesc_t**) malloc(sizeof(lldesc_t*) * dma_buf_count);
  259. if (dma->desc == NULL) {
  260. ESP_LOGE(I2S_TAG, "Error malloc dma description");
  261. i2s_destroy_dma_queue(i2s_num, dma);
  262. return NULL;
  263. }
  264. for (bux_idx = 0; bux_idx < dma_buf_count; bux_idx++) {
  265. dma->desc[bux_idx] = (lldesc_t*) malloc(sizeof(lldesc_t));
  266. if (dma->desc[bux_idx] == NULL) {
  267. ESP_LOGE(I2S_TAG, "Error malloc dma description entry");
  268. i2s_destroy_dma_queue(i2s_num, dma);
  269. return NULL;
  270. }
  271. }
  272. for (bux_idx = 0; bux_idx < dma_buf_count; bux_idx++) {
  273. dma->desc[bux_idx]->owner = 1;
  274. dma->desc[bux_idx]->eof = 1;
  275. dma->desc[bux_idx]->sosf = 0;
  276. dma->desc[bux_idx]->length = dma_buf_len * sample_size;
  277. dma->desc[bux_idx]->size = dma_buf_len * sample_size;
  278. dma->desc[bux_idx]->buf = (uint8_t *) dma->buf[bux_idx];
  279. dma->desc[bux_idx]->offset = 0;
  280. dma->desc[bux_idx]->empty = (uint32_t)((bux_idx < (dma_buf_count - 1)) ? (dma->desc[bux_idx + 1]) : dma->desc[0]);
  281. }
  282. dma->queue = xQueueCreate(dma_buf_count - 1, sizeof(char*));
  283. dma->mux = xSemaphoreCreateMutex();
  284. dma->rw_pos = 0;
  285. dma->buf_size = dma_buf_len * sample_size;
  286. dma->curr_ptr = NULL;
  287. ESP_LOGI(I2S_TAG, "DMA Malloc info, datalen=blocksize=%d, dma_buf_count=%d", dma_buf_len * sample_size, dma_buf_count);
  288. return dma;
  289. }
  290. esp_err_t i2s_start(i2s_port_t i2s_num)
  291. {
  292. //start DMA link
  293. I2S_ENTER_CRITICAL();
  294. esp_intr_disable(p_i2s_obj[i2s_num]->i2s_isr_handle);
  295. I2S[i2s_num]->int_clr.val = 0xFFFFFFFF;
  296. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) {
  297. ESP_LOGD(I2S_TAG, "I2S_MODE_TX");
  298. i2s_enable_tx_intr(i2s_num);
  299. I2S[i2s_num]->out_link.start = 1;
  300. I2S[i2s_num]->conf.tx_start = 1;
  301. }
  302. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) {
  303. ESP_LOGD(I2S_TAG, "I2S_MODE_RX");
  304. i2s_enable_rx_intr(i2s_num);
  305. I2S[i2s_num]->in_link.start = 1;
  306. I2S[i2s_num]->conf.rx_start = 1;
  307. }
  308. esp_intr_enable(p_i2s_obj[i2s_num]->i2s_isr_handle);
  309. I2S_EXIT_CRITICAL();
  310. return ESP_OK;
  311. }
  312. esp_err_t i2s_stop(i2s_port_t i2s_num)
  313. {
  314. I2S_ENTER_CRITICAL();
  315. esp_intr_disable(p_i2s_obj[i2s_num]->i2s_isr_handle);
  316. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) {
  317. I2S[i2s_num]->out_link.stop = 1;
  318. I2S[i2s_num]->conf.tx_start = 0;
  319. i2s_disable_tx_intr(i2s_num);
  320. }
  321. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) {
  322. I2S[i2s_num]->in_link.stop = 1;
  323. I2S[i2s_num]->conf.rx_start = 0;
  324. i2s_disable_rx_intr(i2s_num);
  325. }
  326. I2S_EXIT_CRITICAL();
  327. return 0;
  328. }
  329. static esp_err_t configure_dac_pin(void)
  330. {
  331. SET_PERI_REG_MASK(SENS_SAR_DAC_CTRL1_REG, SENS_DAC_DIG_FORCE_M);
  332. SET_PERI_REG_MASK(SENS_SAR_DAC_CTRL1_REG, SENS_DAC_CLK_INV_M);
  333. SET_PERI_REG_MASK(RTC_IO_PAD_DAC1_REG, RTC_IO_PDAC1_DAC_XPD_FORCE_M);
  334. SET_PERI_REG_MASK(RTC_IO_PAD_DAC1_REG, RTC_IO_PDAC1_XPD_DAC_M);
  335. CLEAR_PERI_REG_MASK(RTC_IO_PAD_DAC1_REG, RTC_IO_PDAC1_RUE_M);
  336. CLEAR_PERI_REG_MASK(RTC_IO_PAD_DAC1_REG, RTC_IO_PDAC1_RDE_M);
  337. SET_PERI_REG_MASK(RTC_IO_PAD_DAC2_REG, RTC_IO_PDAC2_DAC_XPD_FORCE_M);
  338. SET_PERI_REG_MASK(RTC_IO_PAD_DAC2_REG, RTC_IO_PDAC2_XPD_DAC_M);
  339. CLEAR_PERI_REG_MASK(RTC_IO_PAD_DAC2_REG, RTC_IO_PDAC2_RUE_M);
  340. CLEAR_PERI_REG_MASK(RTC_IO_PAD_DAC2_REG, RTC_IO_PDAC2_RDE_M);
  341. return ESP_OK;
  342. }
  343. esp_err_t i2s_set_pin(i2s_port_t i2s_num, const i2s_pin_config_t *pin)
  344. {
  345. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_FAIL);
  346. if (pin == NULL) {
  347. return configure_dac_pin();
  348. }
  349. if (pin->bck_io_num != -1 && !GPIO_IS_VALID_GPIO(pin->bck_io_num)) {
  350. ESP_LOGE(I2S_TAG, "bck_io_num error");
  351. return ESP_FAIL;
  352. }
  353. if (pin->ws_io_num != -1 && !GPIO_IS_VALID_GPIO(pin->ws_io_num)) {
  354. ESP_LOGE(I2S_TAG, "ws_io_num error");
  355. return ESP_FAIL;
  356. }
  357. if (pin->data_out_num != -1 && !GPIO_IS_VALID_GPIO(pin->data_out_num)) {
  358. ESP_LOGE(I2S_TAG, "data_out_num error");
  359. return ESP_FAIL;
  360. }
  361. if (pin->data_in_num != -1 && !GPIO_IS_VALID_GPIO(pin->data_in_num)) {
  362. ESP_LOGE(I2S_TAG, "data_in_num error");
  363. return ESP_FAIL;
  364. }
  365. int bck_sig = -1, ws_sig = -1, data_out_sig = -1, data_in_sig = -1;
  366. //TX & RX
  367. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) {
  368. bck_sig = I2S0I_BCK_OUT_IDX;
  369. ws_sig = I2S0I_WS_OUT_IDX;
  370. data_in_sig = I2S0I_DATA_IN15_IDX;
  371. if (i2s_num == I2S_NUM_1) {
  372. bck_sig = I2S1I_BCK_OUT_IDX;
  373. ws_sig = I2S1I_WS_OUT_IDX;
  374. data_in_sig = I2S1I_DATA_IN15_IDX;
  375. }
  376. }
  377. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) {
  378. bck_sig = I2S0O_BCK_OUT_IDX;
  379. ws_sig = I2S0O_WS_OUT_IDX;
  380. data_out_sig = I2S0O_DATA_OUT23_IDX;
  381. if (i2s_num == I2S_NUM_1) {
  382. bck_sig = I2S1O_BCK_OUT_IDX;
  383. ws_sig = I2S1O_WS_OUT_IDX;
  384. data_out_sig = I2S1O_DATA_OUT23_IDX;
  385. }
  386. }
  387. gpio_matrix_out_check(pin->data_out_num, data_out_sig, 0, 0);
  388. gpio_matrix_in_check(pin->data_in_num, data_in_sig, 0);
  389. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_MASTER) {
  390. gpio_matrix_out_check(pin->ws_io_num, ws_sig, 0, 0);
  391. gpio_matrix_out_check(pin->bck_io_num, bck_sig, 0, 0);
  392. } else if (p_i2s_obj[i2s_num]->mode & I2S_MODE_SLAVE) {
  393. gpio_matrix_in_check(pin->ws_io_num, ws_sig, 0);
  394. gpio_matrix_in_check(pin->bck_io_num, bck_sig, 0);
  395. }
  396. ESP_LOGE(I2S_TAG, "data: out %d, in: %d, ws: %d, bck: %d", data_out_sig, data_in_sig, ws_sig, bck_sig);
  397. return ESP_OK;
  398. }
  399. esp_err_t i2s_set_sample_rates(i2s_port_t i2s_num, uint32_t rate)
  400. {
  401. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_FAIL);
  402. I2S_CHECK((p_i2s_obj[i2s_num]->bytes_per_sample > 0), "bits_per_sample not set", ESP_FAIL);
  403. return i2s_set_clk(i2s_num, rate, p_i2s_obj[i2s_num]->bytes_per_sample*8, 0);
  404. }
  405. static esp_err_t i2s_param_config(i2s_port_t i2s_num, const i2s_config_t *i2s_config)
  406. {
  407. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_FAIL);
  408. I2S_CHECK((i2s_config), "param null", ESP_FAIL);
  409. if (i2s_num == I2S_NUM_1) {
  410. periph_module_enable(PERIPH_I2S1_MODULE);
  411. } else {
  412. periph_module_enable(PERIPH_I2S0_MODULE);
  413. }
  414. // configure I2S data port interface.
  415. i2s_reset_fifo(i2s_num);
  416. //reset i2s
  417. I2S[i2s_num]->conf.tx_reset = 1;
  418. I2S[i2s_num]->conf.tx_reset = 0;
  419. I2S[i2s_num]->conf.rx_reset = 1;
  420. I2S[i2s_num]->conf.rx_reset = 0;
  421. //reset dma
  422. I2S[i2s_num]->lc_conf.in_rst = 1;
  423. I2S[i2s_num]->lc_conf.in_rst = 0;
  424. I2S[i2s_num]->lc_conf.out_rst = 1;
  425. I2S[i2s_num]->lc_conf.out_rst = 0;
  426. //Enable and configure DMA
  427. I2S[i2s_num]->lc_conf.check_owner = 0;
  428. I2S[i2s_num]->lc_conf.out_loop_test = 0;
  429. I2S[i2s_num]->lc_conf.out_auto_wrback = 0;
  430. I2S[i2s_num]->lc_conf.out_data_burst_en = 0;
  431. I2S[i2s_num]->lc_conf.outdscr_burst_en = 0;
  432. I2S[i2s_num]->lc_conf.out_no_restart_clr = 0;
  433. I2S[i2s_num]->lc_conf.indscr_burst_en = 0;
  434. I2S[i2s_num]->lc_conf.out_eof_mode = 1;
  435. I2S[i2s_num]->conf2.lcd_en = 0;
  436. I2S[i2s_num]->conf2.camera_en = 0;
  437. I2S[i2s_num]->pdm_conf.pcm2pdm_conv_en = 0;
  438. I2S[i2s_num]->pdm_conf.pdm2pcm_conv_en = 0;
  439. I2S[i2s_num]->fifo_conf.dscr_en = 0;
  440. p_i2s_obj[i2s_num]->channel_num = i2s_config->channel_format < I2S_CHANNEL_FMT_ONLY_RIGHT ? 2 : 1;
  441. 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
  442. 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
  443. I2S[i2s_num]->conf.tx_mono = i2s_config->channel_format < I2S_CHANNEL_FMT_ONLY_RIGHT ? 0 : 1; // 0-right&left channel;1-one channel
  444. 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
  445. 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
  446. I2S[i2s_num]->conf.rx_mono = i2s_config->channel_format < I2S_CHANNEL_FMT_ONLY_RIGHT ? 0 : 1; // 0-right&left channel;1-one channel
  447. I2S[i2s_num]->fifo_conf.dscr_en = 1;//connect dma to fifo
  448. I2S[i2s_num]->conf.tx_start = 0;
  449. I2S[i2s_num]->conf.rx_start = 0;
  450. if (i2s_config->mode & I2S_MODE_TX) {
  451. I2S[i2s_num]->conf.tx_msb_right = 0;
  452. I2S[i2s_num]->conf.tx_right_first = 0;
  453. I2S[i2s_num]->conf.tx_slave_mod = 0; // Master
  454. I2S[i2s_num]->fifo_conf.tx_fifo_mod_force_en = 1;//?
  455. if (i2s_config->mode & I2S_MODE_SLAVE) {
  456. I2S[i2s_num]->conf.tx_slave_mod = 1;//TX Slave
  457. }
  458. }
  459. if (i2s_config->mode & I2S_MODE_RX) {
  460. I2S[i2s_num]->conf.rx_msb_right = 0;
  461. I2S[i2s_num]->conf.rx_right_first = 0;
  462. I2S[i2s_num]->conf.rx_slave_mod = 0; // Master
  463. I2S[i2s_num]->fifo_conf.rx_fifo_mod_force_en = 1;//?
  464. I2S[i2s_num]->rx_eof_num = (i2s_config->dma_buf_len);
  465. if (i2s_config->mode & I2S_MODE_SLAVE) {
  466. I2S[i2s_num]->conf.rx_slave_mod = 1;//RX Slave
  467. }
  468. }
  469. if (i2s_config->mode & I2S_MODE_DAC_BUILT_IN) {
  470. I2S[i2s_num]->conf2.lcd_en = 1;
  471. I2S[i2s_num]->conf.tx_right_first = 1;
  472. I2S[i2s_num]->fifo_conf.tx_fifo_mod = 3;
  473. }
  474. if (i2s_config->communication_format & I2S_COMM_FORMAT_I2S) {
  475. I2S[i2s_num]->conf.tx_short_sync = 0;
  476. I2S[i2s_num]->conf.rx_short_sync = 0;
  477. I2S[i2s_num]->conf.tx_msb_shift = 1;
  478. I2S[i2s_num]->conf.rx_msb_shift = 1;
  479. if (i2s_config->communication_format & I2S_COMM_FORMAT_I2S_LSB) {
  480. if (i2s_config->mode & I2S_MODE_TX) {
  481. I2S[i2s_num]->conf.tx_msb_shift = 0;
  482. }
  483. if (i2s_config->mode & I2S_MODE_RX) {
  484. I2S[i2s_num]->conf.rx_msb_shift = 0;
  485. }
  486. }
  487. }
  488. if (i2s_config->communication_format & I2S_COMM_FORMAT_PCM) {
  489. I2S[i2s_num]->conf.tx_msb_shift = 0;
  490. I2S[i2s_num]->conf.rx_msb_shift = 0;
  491. I2S[i2s_num]->conf.tx_short_sync = 0;
  492. I2S[i2s_num]->conf.rx_short_sync = 0;
  493. if (i2s_config->communication_format & I2S_COMM_FORMAT_PCM_SHORT) {
  494. if (i2s_config->mode & I2S_MODE_TX) {
  495. I2S[i2s_num]->conf.tx_short_sync = 1;
  496. }
  497. if (i2s_config->mode & I2S_MODE_RX) {
  498. I2S[i2s_num]->conf.rx_short_sync = 1;
  499. }
  500. }
  501. }
  502. if ((p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) && (p_i2s_obj[i2s_num]->mode & I2S_MODE_TX)) {
  503. I2S[i2s_num]->conf.sig_loopback = 1;
  504. }
  505. i2s_set_clk(i2s_num, i2s_config->sample_rate, p_i2s_obj[i2s_num]->bytes_per_sample*8, 0);
  506. return ESP_OK;
  507. }
  508. esp_err_t i2s_zero_dma_buffer(i2s_port_t i2s_num)
  509. {
  510. int total_buffer_in_bytes = p_i2s_obj[i2s_num]->dma_buf_count * p_i2s_obj[i2s_num]->dma_buf_len * p_i2s_obj[i2s_num]->bytes_per_sample * p_i2s_obj[i2s_num]->channel_num;
  511. const uint32_t zero_sample[2] = { 0 };
  512. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_FAIL);
  513. while (total_buffer_in_bytes > 0) {
  514. i2s_push_sample(i2s_num, (const char*) zero_sample, 10);
  515. total_buffer_in_bytes -= p_i2s_obj[i2s_num]->bytes_per_sample * p_i2s_obj[i2s_num]->channel_num;
  516. }
  517. return ESP_OK;
  518. }
  519. esp_err_t i2s_driver_install(i2s_port_t i2s_num, const i2s_config_t *i2s_config, int queue_size, void* i2s_queue)
  520. {
  521. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_FAIL);
  522. I2S_CHECK((i2s_config != NULL), "I2S configuration must not NULL", ESP_FAIL);
  523. 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_FAIL);
  524. I2S_CHECK((i2s_config->dma_buf_len >= 8 && i2s_config->dma_buf_len <= 2048), "I2S buffer length at most 2048 and more than 8", ESP_FAIL);
  525. if (p_i2s_obj[i2s_num] == NULL) {
  526. p_i2s_obj[i2s_num] = (i2s_obj_t*) malloc(sizeof(i2s_obj_t));
  527. if (p_i2s_obj[i2s_num] == NULL) {
  528. ESP_LOGE(I2S_TAG, "Malloc I2S driver error");
  529. return ESP_FAIL;
  530. }
  531. p_i2s_obj[i2s_num]->i2s_num = i2s_num;
  532. p_i2s_obj[i2s_num]->dma_buf_count = i2s_config->dma_buf_count;
  533. p_i2s_obj[i2s_num]->dma_buf_len = i2s_config->dma_buf_len;
  534. p_i2s_obj[i2s_num]->i2s_queue = i2s_queue;
  535. p_i2s_obj[i2s_num]->mode = i2s_config->mode;
  536. p_i2s_obj[i2s_num]->bytes_per_sample = i2s_config->bits_per_sample/8;
  537. //initial dma
  538. if (ESP_FAIL == 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)) {
  539. free(p_i2s_obj[i2s_num]);
  540. ESP_LOGE(I2S_TAG, "Register I2S Interrupt error");
  541. return ESP_FAIL;
  542. }
  543. i2s_stop(i2s_num);
  544. i2s_param_config(i2s_num, i2s_config);
  545. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) {
  546. p_i2s_obj[i2s_num]->tx = i2s_create_dma_queue(i2s_num, i2s_config->dma_buf_count, i2s_config->dma_buf_len);
  547. if (p_i2s_obj[i2s_num]->tx == NULL) {
  548. ESP_LOGE(I2S_TAG, "Failed to create tx dma buffer");
  549. i2s_driver_uninstall(i2s_num);
  550. return ESP_FAIL;
  551. }
  552. I2S[i2s_num]->out_link.addr = (uint32_t) p_i2s_obj[i2s_num]->tx->desc[0];
  553. }
  554. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) {
  555. p_i2s_obj[i2s_num]->rx = i2s_create_dma_queue(i2s_num, i2s_config->dma_buf_count, i2s_config->dma_buf_len);
  556. if (p_i2s_obj[i2s_num]->rx == NULL){
  557. ESP_LOGE(I2S_TAG, "Failed to create rx dma buffer");
  558. i2s_driver_uninstall(i2s_num);
  559. return ESP_FAIL;
  560. }
  561. I2S[i2s_num]->in_link.addr = (uint32_t) p_i2s_obj[i2s_num]->rx->desc[0];
  562. }
  563. if (i2s_queue) {
  564. p_i2s_obj[i2s_num]->i2s_queue = xQueueCreate(queue_size, sizeof(i2s_event_t));
  565. *((QueueHandle_t*) i2s_queue) = p_i2s_obj[i2s_num]->i2s_queue;
  566. ESP_LOGI(I2S_TAG, "queue free spaces: %d", uxQueueSpacesAvailable(p_i2s_obj[i2s_num]->i2s_queue));
  567. } else {
  568. p_i2s_obj[i2s_num]->i2s_queue = NULL;
  569. }
  570. i2s_start(i2s_num);
  571. } else {
  572. ESP_LOGE(I2S_TAG, "I2S driver already installed");
  573. return ESP_FAIL;
  574. }
  575. return ESP_OK;
  576. }
  577. esp_err_t i2s_driver_uninstall(i2s_port_t i2s_num)
  578. {
  579. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_FAIL);
  580. if (p_i2s_obj[i2s_num] == NULL) {
  581. ESP_LOGI(I2S_TAG, "ALREADY NULL");
  582. return ESP_OK;
  583. }
  584. i2s_stop(i2s_num);
  585. esp_intr_free(p_i2s_obj[i2s_num]->i2s_isr_handle);
  586. if (p_i2s_obj[i2s_num]->tx != NULL && p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) {
  587. i2s_destroy_dma_queue(i2s_num, p_i2s_obj[i2s_num]->tx);
  588. p_i2s_obj[i2s_num]->tx = NULL;
  589. }
  590. if (p_i2s_obj[i2s_num]->rx != NULL && p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) {
  591. i2s_destroy_dma_queue(i2s_num, p_i2s_obj[i2s_num]->rx);
  592. p_i2s_obj[i2s_num]->rx = NULL;
  593. }
  594. if (p_i2s_obj[i2s_num]->i2s_queue) {
  595. vQueueDelete(p_i2s_obj[i2s_num]->i2s_queue);
  596. p_i2s_obj[i2s_num]->i2s_queue = NULL;
  597. }
  598. free(p_i2s_obj[i2s_num]);
  599. p_i2s_obj[i2s_num] = NULL;
  600. if (i2s_num == I2S_NUM_0) {
  601. periph_module_disable(PERIPH_I2S0_MODULE);
  602. } else if (i2s_num == I2S_NUM_1) {
  603. periph_module_disable(PERIPH_I2S1_MODULE);
  604. }
  605. return ESP_OK;
  606. }
  607. int i2s_write_bytes(i2s_port_t i2s_num, const char *src, size_t size, TickType_t ticks_to_wait)
  608. {
  609. char *data_ptr;
  610. int bytes_can_write, bytes_writen = 0;
  611. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_FAIL);
  612. if (p_i2s_obj[i2s_num]->tx == NULL) {
  613. return 0;
  614. }
  615. xSemaphoreTake(p_i2s_obj[i2s_num]->tx->mux, (portTickType)portMAX_DELAY);
  616. while (size > 0) {
  617. 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) {
  618. if (xQueueReceive(p_i2s_obj[i2s_num]->tx->queue, &p_i2s_obj[i2s_num]->tx->curr_ptr, ticks_to_wait) == pdFALSE) {
  619. break;
  620. }
  621. p_i2s_obj[i2s_num]->tx->rw_pos = 0;
  622. }
  623. 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);
  624. data_ptr = (char*)p_i2s_obj[i2s_num]->tx->curr_ptr;
  625. data_ptr += p_i2s_obj[i2s_num]->tx->rw_pos;
  626. bytes_can_write = p_i2s_obj[i2s_num]->tx->buf_size - p_i2s_obj[i2s_num]->tx->rw_pos;
  627. if (bytes_can_write > size) {
  628. bytes_can_write = size;
  629. }
  630. memcpy(data_ptr, src, bytes_can_write);
  631. size -= bytes_can_write;
  632. src += bytes_can_write;
  633. p_i2s_obj[i2s_num]->tx->rw_pos += bytes_can_write;
  634. bytes_writen += bytes_can_write;
  635. }
  636. xSemaphoreGive(p_i2s_obj[i2s_num]->tx->mux);
  637. return bytes_writen;
  638. }
  639. int i2s_read_bytes(i2s_port_t i2s_num, char* dest, size_t size, TickType_t ticks_to_wait)
  640. {
  641. char *data_ptr;
  642. int bytes_can_read, byte_read = 0;
  643. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_FAIL);
  644. if (p_i2s_obj[i2s_num]->rx == NULL) {
  645. return 0;
  646. }
  647. xSemaphoreTake(p_i2s_obj[i2s_num]->rx->mux, (portTickType)portMAX_DELAY);
  648. while (size > 0) {
  649. 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) {
  650. if (xQueueReceive(p_i2s_obj[i2s_num]->rx->queue, &p_i2s_obj[i2s_num]->rx->curr_ptr, ticks_to_wait) == pdFALSE) {
  651. break;
  652. }
  653. p_i2s_obj[i2s_num]->rx->rw_pos = 0;
  654. }
  655. data_ptr = (char*)p_i2s_obj[i2s_num]->rx->curr_ptr;
  656. data_ptr += p_i2s_obj[i2s_num]->rx->rw_pos;
  657. bytes_can_read = p_i2s_obj[i2s_num]->rx->buf_size - p_i2s_obj[i2s_num]->rx->rw_pos;
  658. if (bytes_can_read > size) {
  659. bytes_can_read = size;
  660. }
  661. memcpy(dest, data_ptr, bytes_can_read);
  662. size -= bytes_can_read;
  663. dest += bytes_can_read;
  664. p_i2s_obj[i2s_num]->rx->rw_pos += bytes_can_read;
  665. byte_read += bytes_can_read;
  666. }
  667. xSemaphoreGive(p_i2s_obj[i2s_num]->rx->mux);
  668. return byte_read;
  669. }
  670. int i2s_push_sample(i2s_port_t i2s_num, const char *sample, TickType_t ticks_to_wait)
  671. {
  672. int i, bytes_to_push = 0;
  673. char *data_ptr;
  674. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_FAIL);
  675. 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) {
  676. if (xQueueReceive(p_i2s_obj[i2s_num]->tx->queue, &p_i2s_obj[i2s_num]->tx->curr_ptr, ticks_to_wait) == pdFALSE) {
  677. return 0;
  678. }
  679. ESP_LOGD(I2S_TAG, "rw_pos: %d, buf_size: %d, curr_ptr: %d", 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);
  680. p_i2s_obj[i2s_num]->tx->rw_pos = 0;
  681. }
  682. data_ptr = (char*)p_i2s_obj[i2s_num]->tx->curr_ptr;
  683. data_ptr += p_i2s_obj[i2s_num]->tx->rw_pos;
  684. for (i = 0; i < p_i2s_obj[i2s_num]->bytes_per_sample; i++) {
  685. *data_ptr++ = *sample++;
  686. bytes_to_push ++;
  687. }
  688. if (p_i2s_obj[i2s_num]->channel_num == 2) {
  689. for (i = 0; i < p_i2s_obj[i2s_num]->bytes_per_sample; i++) {
  690. *data_ptr++ = *sample++;
  691. bytes_to_push ++;
  692. }
  693. }
  694. p_i2s_obj[i2s_num]->tx->rw_pos += p_i2s_obj[i2s_num]->bytes_per_sample * p_i2s_obj[i2s_num]->channel_num;
  695. return bytes_to_push;
  696. }
  697. int i2s_pop_sample(i2s_port_t i2s_num, char *sample, TickType_t ticks_to_wait)
  698. {
  699. int i, bytes_to_pop = 0;
  700. char *data_ptr;
  701. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_FAIL);
  702. 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) {
  703. if (xQueueReceive(p_i2s_obj[i2s_num]->rx->queue, &p_i2s_obj[i2s_num]->rx->curr_ptr, ticks_to_wait) == pdFALSE) {
  704. return 0;
  705. }
  706. p_i2s_obj[i2s_num]->rx->rw_pos = 0;
  707. }
  708. data_ptr = (char*)p_i2s_obj[i2s_num]->rx->curr_ptr;
  709. data_ptr += p_i2s_obj[i2s_num]->rx->rw_pos;
  710. for (i = 0; i < p_i2s_obj[i2s_num]->bytes_per_sample; i++) {
  711. *sample++ = *data_ptr++;
  712. bytes_to_pop++;
  713. }
  714. if (p_i2s_obj[i2s_num]->channel_num == 2) {
  715. for (i = 0; i < p_i2s_obj[i2s_num]->bytes_per_sample; i++) {
  716. *sample++ = *data_ptr++;
  717. bytes_to_pop++;
  718. }
  719. }
  720. p_i2s_obj[i2s_num]->rx->rw_pos += p_i2s_obj[i2s_num]->bytes_per_sample * p_i2s_obj[i2s_num]->channel_num;
  721. return bytes_to_pop;
  722. }