i2s.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. /* Copyright 2018 Canaan Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. #include <math.h>
  16. #include <stdint.h>
  17. #include <stdio.h>
  18. #include "i2s.h"
  19. #include "stdlib.h"
  20. #include "sysctl.h"
  21. #include "utils.h"
  22. volatile i2s_t *const i2s[3] =
  23. {
  24. (volatile i2s_t *)I2S0_BASE_ADDR,
  25. (volatile i2s_t *)I2S1_BASE_ADDR,
  26. (volatile i2s_t *)I2S2_BASE_ADDR};
  27. typedef struct _i2s_instance
  28. {
  29. i2s_device_number_t i2s_num;
  30. i2s_transfer_mode_t transfer_mode;
  31. dmac_channel_number_t dmac_channel;
  32. plic_instance_t i2s_int_instance;
  33. } i2s_instance_t;
  34. static i2s_instance_t g_i2s_send_instance[3];
  35. static i2s_instance_t g_i2s_recv_instance[3];
  36. static int i2s_recv_channel_enable(i2s_device_number_t device_num,
  37. i2s_channel_num_t channel_num, uint32_t enable)
  38. {
  39. rer_t u_rer;
  40. if(channel_num < I2S_CHANNEL_0 || channel_num > I2S_CHANNEL_3)
  41. return -1;
  42. u_rer.reg_data = readl(&i2s[device_num]->channel[channel_num].rer);
  43. u_rer.rer.rxchenx = enable;
  44. writel(u_rer.reg_data, &i2s[device_num]->channel[channel_num].rer);
  45. return 0;
  46. }
  47. static int i2s_transmit_channel_enable(i2s_device_number_t device_num,
  48. i2s_channel_num_t channel_num, uint32_t enable)
  49. {
  50. ter_t u_ter;
  51. if(channel_num < I2S_CHANNEL_0 || channel_num > I2S_CHANNEL_3)
  52. return -1;
  53. u_ter.reg_data = readl(&i2s[device_num]->channel[channel_num].ter);
  54. u_ter.ter.txchenx = enable;
  55. writel(u_ter.reg_data, &i2s[device_num]->channel[channel_num].ter);
  56. return 0;
  57. }
  58. static void i2s_receive_enable(i2s_device_number_t device_num, i2s_channel_num_t channel_num)
  59. {
  60. irer_t u_irer;
  61. u_irer.reg_data = readl(&i2s[device_num]->irer);
  62. u_irer.irer.rxen = 1;
  63. writel(u_irer.reg_data, &i2s[device_num]->irer);
  64. /* Receiver block enable */
  65. i2s_recv_channel_enable(device_num, channel_num, 1);
  66. /* Receive channel enable */
  67. }
  68. static void i2s_transimit_enable(i2s_device_number_t device_num, i2s_channel_num_t channel_num)
  69. {
  70. iter_t u_iter;
  71. u_iter.reg_data = readl(&i2s[device_num]->iter);
  72. u_iter.iter.txen = 1;
  73. writel(u_iter.reg_data, &i2s[device_num]->iter);
  74. /* Transmitter block enable */
  75. i2s_transmit_channel_enable(device_num, channel_num, 1);
  76. /* Transmit channel enable */
  77. }
  78. static void i2s_set_enable(i2s_device_number_t device_num, uint32_t enable)
  79. {
  80. ier_t u_ier;
  81. u_ier.reg_data = readl(&i2s[device_num]->ier);
  82. u_ier.ier.ien = enable;
  83. writel(u_ier.reg_data, &i2s[device_num]->ier);
  84. }
  85. static void i2s_disable_block(i2s_device_number_t device_num, i2s_transmit_t rxtx_mode)
  86. {
  87. irer_t u_irer;
  88. iter_t u_iter;
  89. if(rxtx_mode == I2S_RECEIVER)
  90. {
  91. u_irer.reg_data = readl(&i2s[device_num]->irer);
  92. u_irer.irer.rxen = 0;
  93. writel(u_irer.reg_data, &i2s[device_num]->irer);
  94. /* Receiver block disable */
  95. } else
  96. {
  97. u_iter.reg_data = readl(&i2s[device_num]->iter);
  98. u_iter.iter.txen = 0;
  99. writel(u_iter.reg_data, &i2s[device_num]->iter);
  100. /* Transmitter block disable */
  101. }
  102. }
  103. static int i2s_set_rx_word_length(i2s_device_number_t device_num,
  104. i2s_word_length_t word_length,
  105. i2s_channel_num_t channel_num)
  106. {
  107. rcr_tcr_t u_rcr;
  108. if(word_length > RESOLUTION_32_BIT || word_length < IGNORE_WORD_LENGTH)
  109. return -1;
  110. if(channel_num < I2S_CHANNEL_0 || channel_num > I2S_CHANNEL_3)
  111. return -1;
  112. u_rcr.reg_data = readl(&i2s[device_num]->channel[channel_num].rcr);
  113. u_rcr.rcr_tcr.wlen = word_length;
  114. writel(u_rcr.reg_data, &i2s[device_num]->channel[channel_num].rcr);
  115. return 0;
  116. }
  117. static int i2s_set_tx_word_length(i2s_device_number_t device_num,
  118. i2s_word_length_t word_length,
  119. i2s_channel_num_t channel_num)
  120. {
  121. rcr_tcr_t u_tcr;
  122. if(word_length > RESOLUTION_32_BIT || word_length < IGNORE_WORD_LENGTH)
  123. return -1;
  124. if(channel_num < I2S_CHANNEL_0 || channel_num > I2S_CHANNEL_3)
  125. return -1;
  126. u_tcr.reg_data = readl(&i2s[device_num]->channel[channel_num].tcr);
  127. u_tcr.rcr_tcr.wlen = word_length;
  128. writel(u_tcr.reg_data, &i2s[device_num]->channel[channel_num].tcr);
  129. return 0;
  130. }
  131. static void i2s_master_configure(i2s_device_number_t device_num,
  132. i2s_word_select_cycles_t word_select_size,
  133. i2s_sclk_gating_cycles_t gating_cycles,
  134. i2s_work_mode_t word_mode)
  135. {
  136. configASSERT(!(word_select_size < SCLK_CYCLES_16 ||
  137. word_select_size > SCLK_CYCLES_32));
  138. configASSERT(!(gating_cycles < NO_CLOCK_GATING ||
  139. gating_cycles > CLOCK_CYCLES_24));
  140. ccr_t u_ccr;
  141. cer_t u_cer;
  142. u_ccr.reg_data = readl(&i2s[device_num]->ccr);
  143. u_ccr.ccr.clk_word_size = word_select_size;
  144. u_ccr.ccr.clk_gate = gating_cycles;
  145. u_ccr.ccr.align_mode = word_mode;
  146. writel(u_ccr.reg_data, &i2s[device_num]->ccr);
  147. u_cer.reg_data = readl(&i2s[device_num]->cer);
  148. u_cer.cer.clken = 1;
  149. writel(u_cer.reg_data, &i2s[device_num]->cer);
  150. /* Clock generation enable */
  151. }
  152. static int i2s_set_rx_threshold(i2s_device_number_t device_num,
  153. i2s_fifo_threshold_t threshold,
  154. i2s_channel_num_t channel_num)
  155. {
  156. rfcr_t u_rfcr;
  157. if(threshold < TRIGGER_LEVEL_1 || threshold > TRIGGER_LEVEL_16)
  158. return -1;
  159. if(channel_num < I2S_CHANNEL_0 || channel_num > I2S_CHANNEL_3)
  160. return -1;
  161. u_rfcr.reg_data = readl(&i2s[device_num]->channel[channel_num].rfcr);
  162. u_rfcr.rfcr.rxchdt = threshold;
  163. writel(u_rfcr.reg_data, &i2s[device_num]->channel[channel_num].rfcr);
  164. return 0;
  165. }
  166. static int i2s_set_tx_threshold(i2s_device_number_t device_num,
  167. i2s_fifo_threshold_t threshold,
  168. i2s_channel_num_t channel_num)
  169. {
  170. tfcr_t u_tfcr;
  171. if(threshold < TRIGGER_LEVEL_1 || threshold > TRIGGER_LEVEL_16)
  172. return -1;
  173. if(channel_num < I2S_CHANNEL_0 || channel_num > I2S_CHANNEL_3)
  174. return -1;
  175. u_tfcr.reg_data = readl(&i2s[device_num]->channel[channel_num].tfcr);
  176. u_tfcr.tfcr.txchet = threshold;
  177. writel(u_tfcr.reg_data, &i2s[device_num]->channel[channel_num].tfcr);
  178. return 0;
  179. }
  180. static int i2s_set_mask_interrupt(i2s_device_number_t device_num,
  181. i2s_channel_num_t channel_num,
  182. uint32_t rx_available_int, uint32_t rx_overrun_int,
  183. uint32_t tx_empty_int, uint32_t tx_overrun_int)
  184. {
  185. imr_t u_imr;
  186. if(channel_num < I2S_CHANNEL_0 || channel_num > I2S_CHANNEL_3)
  187. return -1;
  188. u_imr.reg_data = readl(&i2s[device_num]->channel[channel_num].imr);
  189. if(rx_available_int == 1)
  190. u_imr.imr.rxdam = 1;
  191. else
  192. u_imr.imr.rxdam = 0;
  193. if(rx_overrun_int == 1)
  194. u_imr.imr.rxfom = 1;
  195. else
  196. u_imr.imr.rxfom = 0;
  197. if(tx_empty_int == 1)
  198. u_imr.imr.txfem = 1;
  199. else
  200. u_imr.imr.txfem = 0;
  201. if(tx_overrun_int == 1)
  202. u_imr.imr.txfom = 1;
  203. else
  204. u_imr.imr.txfom = 0;
  205. writel(u_imr.reg_data, &i2s[device_num]->channel[channel_num].imr);
  206. return 0;
  207. }
  208. static int i2s_transmit_dma_enable(i2s_device_number_t device_num, uint32_t enable)
  209. {
  210. ccr_t u_ccr;
  211. if(device_num >= I2S_DEVICE_MAX)
  212. return -1;
  213. u_ccr.reg_data = readl(&i2s[device_num]->ccr);
  214. u_ccr.ccr.dma_tx_en = enable;
  215. writel(u_ccr.reg_data, &i2s[device_num]->ccr);
  216. return 0;
  217. }
  218. static int i2s_receive_dma_enable(i2s_device_number_t device_num, uint32_t enable)
  219. {
  220. ccr_t u_ccr;
  221. if(device_num >= I2S_DEVICE_MAX)
  222. return -1;
  223. u_ccr.reg_data = readl(&i2s[device_num]->ccr);
  224. u_ccr.ccr.dma_rx_en = enable;
  225. writel(u_ccr.reg_data, &i2s[device_num]->ccr);
  226. return 0;
  227. }
  228. int i2s_set_dma_divide_16(i2s_device_number_t device_num, uint32_t enable)
  229. {
  230. ccr_t u_ccr;
  231. if(device_num >= I2S_DEVICE_MAX)
  232. return -1;
  233. u_ccr.reg_data = readl(&i2s[device_num]->ccr);
  234. u_ccr.ccr.dma_divide_16 = enable;
  235. writel(u_ccr.reg_data, &i2s[device_num]->ccr);
  236. return 0;
  237. }
  238. int i2s_get_dma_divide_16(i2s_device_number_t device_num)
  239. {
  240. if(device_num >= I2S_DEVICE_MAX)
  241. return -1;
  242. ccr_t u_ccr;
  243. u_ccr.reg_data = readl(&i2s[device_num]->ccr);
  244. return u_ccr.ccr.dma_divide_16;
  245. }
  246. int i2s_receive_data(i2s_device_number_t device_num, i2s_channel_num_t channel_num, uint64_t *buf, size_t buf_len)
  247. {
  248. uint32_t i = 0;
  249. isr_t u_isr;
  250. readl(&i2s[device_num]->channel[channel_num].ror);
  251. /*clear over run*/
  252. for(i = 0; i < buf_len;)
  253. {
  254. u_isr.reg_data = readl(&i2s[device_num]->channel[channel_num].isr);
  255. if(u_isr.isr.rxda == 1)
  256. {
  257. buf[i] = readl(&i2s[device_num]->channel[channel_num].left_rxtx);
  258. buf[i] <<= 32;
  259. buf[i++] |= readl(&i2s[device_num]->channel[channel_num].right_rxtx);
  260. }
  261. }
  262. return 0;
  263. }
  264. void i2s_receive_data_dma(i2s_device_number_t device_num, uint32_t *buf,
  265. size_t buf_len, dmac_channel_number_t channel_num)
  266. {
  267. dmac_wait_done(channel_num);
  268. sysctl_dma_select((sysctl_dma_channel_t)channel_num, SYSCTL_DMA_SELECT_I2S0_RX_REQ + device_num * 2);
  269. dmac_set_single_mode(channel_num, (void *)(&i2s[device_num]->rxdma), buf, DMAC_ADDR_NOCHANGE, DMAC_ADDR_INCREMENT,
  270. DMAC_MSIZE_1, DMAC_TRANS_WIDTH_32, buf_len);
  271. }
  272. int i2s_rx_to_tx(i2s_device_number_t device_src_num, i2s_device_number_t device_dest_num,
  273. size_t buf_len, dmac_channel_number_t channel_num)
  274. {
  275. static uint8_t dmac_recv_flag[6] = {0, 0, 0, 0, 0, 0};
  276. if(dmac_recv_flag[channel_num])
  277. dmac_wait_done(channel_num);
  278. else
  279. dmac_recv_flag[channel_num] = 1;
  280. sysctl_dma_select((sysctl_dma_channel_t)channel_num, SYSCTL_DMA_SELECT_I2S0_RX_REQ + device_src_num * 2);
  281. dmac_set_single_mode(channel_num, (void *)(&i2s[device_src_num]->rxdma), (void *)(&i2s[device_dest_num]->txdma), DMAC_ADDR_NOCHANGE, DMAC_ADDR_NOCHANGE,
  282. DMAC_MSIZE_1, DMAC_TRANS_WIDTH_32, buf_len);
  283. return 0;
  284. }
  285. int i2s_send_data(i2s_device_number_t device_num, i2s_channel_num_t channel_num, const uint8_t *pcm, size_t buf_len,
  286. size_t single_length)
  287. {
  288. isr_t u_isr;
  289. uint32_t left_buffer = 0;
  290. uint32_t right_buffer = 0;
  291. uint32_t i = 0;
  292. uint32_t j = 0;
  293. if(channel_num < I2S_CHANNEL_0 || channel_num > I2S_CHANNEL_3)
  294. return -1;
  295. buf_len = buf_len / (single_length / 8) / 2; /* sample num */
  296. readl(&i2s[device_num]->channel[channel_num].tor);
  297. /* read clear overrun flag */
  298. for(j = 0; j < buf_len;)
  299. {
  300. u_isr.reg_data = readl(&i2s[device_num]->channel[channel_num].isr);
  301. if(u_isr.isr.txfe == 1)
  302. {
  303. switch(single_length)
  304. {
  305. case 16:
  306. left_buffer = ((uint16_t *)pcm)[i++];
  307. right_buffer = ((uint16_t *)pcm)[i++];
  308. break;
  309. case 24:
  310. left_buffer = 0;
  311. left_buffer |= pcm[i++];
  312. left_buffer |= pcm[i++] << 8;
  313. left_buffer |= pcm[i++] << 16;
  314. right_buffer = 0;
  315. right_buffer |= pcm[i++];
  316. right_buffer |= pcm[i++] << 8;
  317. right_buffer |= pcm[i++] << 16;
  318. break;
  319. case 32:
  320. left_buffer = ((uint32_t *)pcm)[i++];
  321. right_buffer = ((uint32_t *)pcm)[i++];
  322. break;
  323. default:
  324. left_buffer = pcm[i++];
  325. right_buffer = pcm[i++];
  326. break;
  327. }
  328. writel(left_buffer, &i2s[device_num]->channel[channel_num].left_rxtx);
  329. writel(right_buffer, &i2s[device_num]->channel[channel_num].right_rxtx);
  330. j++;
  331. }
  332. }
  333. return 0;
  334. }
  335. void i2s_send_data_dma(i2s_device_number_t device_num, const void *buf, size_t buf_len, dmac_channel_number_t channel_num)
  336. {
  337. dmac_wait_done(channel_num);
  338. sysctl_dma_select((sysctl_dma_channel_t)channel_num, SYSCTL_DMA_SELECT_I2S0_TX_REQ + device_num * 2);
  339. dmac_set_single_mode(channel_num, buf, (void *)(&i2s[device_num]->txdma), DMAC_ADDR_INCREMENT,
  340. DMAC_ADDR_NOCHANGE, DMAC_MSIZE_1, DMAC_TRANS_WIDTH_32, buf_len);
  341. }
  342. static void i2s_parse_voice(i2s_device_number_t device_num, uint32_t *buf, const uint8_t *pcm, size_t length, size_t bits_per_sample,
  343. uint8_t track_num, size_t *send_len)
  344. {
  345. uint32_t i, j = 0;
  346. *send_len = length * 2;
  347. switch(bits_per_sample)
  348. {
  349. case 16:
  350. for(i = 0; i < length; i++)
  351. {
  352. buf[2 * i] = ((uint16_t *)pcm)[i];
  353. buf[2 * i + 1] = 0;
  354. }
  355. break;
  356. case 24:
  357. for(i = 0; i < length; i++)
  358. {
  359. buf[2 * i] = 0;
  360. buf[2 * i] |= pcm[j++];
  361. buf[2 * i] |= pcm[j++] << 8;
  362. buf[2 * i] |= pcm[j++] << 16;
  363. buf[2 * i + 1] = 0;
  364. if(track_num == 2)
  365. {
  366. buf[2 * i + 1] |= pcm[j++];
  367. buf[2 * i + 1] |= pcm[j++] << 8;
  368. buf[2 * i + 1] |= pcm[j++] << 16;
  369. }
  370. }
  371. break;
  372. case 32:
  373. default:
  374. for(i = 0; i < length; i++)
  375. {
  376. buf[2 * i] = ((uint32_t *)pcm)[i];
  377. buf[2 * i + 1] = 0;
  378. }
  379. break;
  380. }
  381. }
  382. void i2s_play(i2s_device_number_t device_num, dmac_channel_number_t channel_num,
  383. const uint8_t *buf, size_t buf_len, size_t frame, size_t bits_per_sample, uint8_t track_num)
  384. {
  385. const uint8_t *trans_buf;
  386. uint32_t i;
  387. size_t sample_cnt = buf_len / (bits_per_sample / 8) / track_num;
  388. size_t frame_cnt = sample_cnt / frame;
  389. size_t frame_remain = sample_cnt % frame;
  390. i2s_set_dma_divide_16(device_num, 0);
  391. if(bits_per_sample == 16 && track_num == 2)
  392. {
  393. i2s_set_dma_divide_16(device_num, 1);
  394. for(i = 0; i < frame_cnt; i++)
  395. {
  396. trans_buf = buf + i * frame * (bits_per_sample / 8) * track_num;
  397. i2s_send_data_dma(device_num, trans_buf, frame, channel_num);
  398. }
  399. if(frame_remain)
  400. {
  401. trans_buf = buf + frame_cnt * frame * (bits_per_sample / 8) * track_num;
  402. i2s_send_data_dma(device_num, trans_buf, frame_remain, channel_num);
  403. }
  404. } else if(bits_per_sample == 32 && track_num == 2)
  405. {
  406. for(i = 0; i < frame_cnt; i++)
  407. {
  408. trans_buf = buf + i * frame * (bits_per_sample / 8) * track_num;
  409. i2s_send_data_dma(device_num, trans_buf, frame * 2, channel_num);
  410. }
  411. if(frame_remain)
  412. {
  413. trans_buf = buf + frame_cnt * frame * (bits_per_sample / 8) * track_num;
  414. i2s_send_data_dma(device_num, trans_buf, frame_remain * 2, channel_num);
  415. }
  416. } else
  417. {
  418. uint32_t *buff[2];
  419. buff[0] = malloc(frame * 2 * sizeof(uint32_t) * 2);
  420. buff[1] = buff[0] + frame * 2;
  421. uint8_t flag = 0;
  422. size_t send_len = 0;
  423. for(i = 0; i < frame_cnt; i++)
  424. {
  425. trans_buf = buf + i * frame * (bits_per_sample / 8) * track_num;
  426. i2s_parse_voice(device_num, buff[flag], trans_buf, frame, bits_per_sample, track_num, &send_len);
  427. i2s_send_data_dma(device_num, buff[flag], send_len, channel_num);
  428. flag = !flag;
  429. }
  430. if(frame_remain)
  431. {
  432. trans_buf = buf + frame_cnt * frame * (bits_per_sample / 8) * track_num;
  433. i2s_parse_voice(device_num, buff[flag], trans_buf, frame_remain, bits_per_sample, track_num, &send_len);
  434. i2s_send_data_dma(device_num, trans_buf, send_len, channel_num);
  435. }
  436. free(buff[0]);
  437. }
  438. }
  439. static inline void i2s_set_sign_expand_en(i2s_device_number_t device_num, uint32_t enable)
  440. {
  441. ccr_t u_ccr;
  442. u_ccr.reg_data = readl(&i2s[device_num]->ccr);
  443. u_ccr.ccr.sign_expand_en = enable;
  444. writel(u_ccr.reg_data, &i2s[device_num]->ccr);
  445. }
  446. void i2s_rx_channel_config(i2s_device_number_t device_num,
  447. i2s_channel_num_t channel_num,
  448. i2s_word_length_t word_length,
  449. i2s_word_select_cycles_t word_select_size,
  450. i2s_fifo_threshold_t trigger_level,
  451. i2s_work_mode_t word_mode)
  452. {
  453. i2s_recv_channel_enable(device_num, channel_num, 0);
  454. /* Receive channel disable */
  455. writel(0, &i2s[device_num]->channel[channel_num].ter);
  456. /* disable tx */
  457. writel(1, &i2s[device_num]->channel[channel_num].rff);
  458. /* flash individual fifo */
  459. writel(1, &i2s[device_num]->rxffr);
  460. /* flush tx fifo*/
  461. i2s_set_rx_word_length(device_num, word_length, channel_num);
  462. /* Word buf_len is RESOLUTION_32_BIT */
  463. i2s_master_configure(device_num,
  464. word_select_size, NO_CLOCK_GATING, word_mode);
  465. /* word select size is 32 bits,no clock gating */
  466. i2s_set_rx_threshold(device_num, trigger_level, channel_num);
  467. /* Interrupt trigger when FIFO level is 8 */
  468. readl(&i2s[device_num]->channel[channel_num].ror);
  469. readl(&i2s[device_num]->channel[channel_num].tor);
  470. i2s_recv_channel_enable(device_num, channel_num, 1);
  471. }
  472. void i2s_tx_channel_config(i2s_device_number_t device_num,
  473. i2s_channel_num_t channel_num,
  474. i2s_word_length_t word_length,
  475. i2s_word_select_cycles_t word_select_size,
  476. i2s_fifo_threshold_t trigger_level,
  477. i2s_work_mode_t word_mode)
  478. {
  479. writel(0, &i2s[device_num]->channel[channel_num].rer);
  480. /* disable rx */
  481. i2s_transmit_channel_enable(device_num, channel_num, 0);
  482. /* Transmit channel disable */
  483. writel(1, &i2s[device_num]->txffr);
  484. /* flush tx fifo */
  485. writel(1, &i2s[device_num]->channel[channel_num].tff);
  486. /* flush individual fifo */
  487. i2s_set_tx_word_length(device_num, word_length, channel_num);
  488. /* Word buf_len is RESOLUTION_16_BIT */
  489. i2s_master_configure(device_num, word_select_size, NO_CLOCK_GATING, word_mode);
  490. /* word select size is 16 bits,gating after 16 bit */
  491. i2s_set_tx_threshold(device_num, trigger_level, channel_num);
  492. /* Interrupt trigger when FIFO level is 8 */
  493. i2s_transmit_channel_enable(device_num, channel_num, 1);
  494. }
  495. void i2s_init(i2s_device_number_t device_num, i2s_transmit_t rxtx_mode, uint32_t channel_mask)
  496. {
  497. sysctl_clock_enable(SYSCTL_CLOCK_I2S0 + device_num);
  498. sysctl_reset(SYSCTL_RESET_I2S0 + device_num);
  499. sysctl_clock_set_threshold(SYSCTL_THRESHOLD_I2S0 + device_num, 7);
  500. /*96k:5,44k:12,24k:23,22k:25 16k:35 sampling*/
  501. /*sample rate*32bit*2 =75MHz/((N+1)*2) */
  502. i2s_set_enable(device_num, 1);
  503. i2s_disable_block(device_num, I2S_TRANSMITTER);
  504. i2s_disable_block(device_num, I2S_RECEIVER);
  505. if(rxtx_mode == I2S_TRANSMITTER)
  506. {
  507. for(int i = 0; i < 4; i++)
  508. {
  509. if((channel_mask & 0x3) == 0x3)
  510. {
  511. i2s_set_mask_interrupt(device_num, I2S_CHANNEL_0 + i, 1, 1, 1, 1);
  512. i2s_transimit_enable(device_num, I2S_CHANNEL_0 + i);
  513. } else
  514. {
  515. i2s_transmit_channel_enable(device_num, I2S_CHANNEL_0 + i, 0);
  516. }
  517. channel_mask >>= 2;
  518. }
  519. i2s_transmit_dma_enable(device_num, 1);
  520. } else
  521. {
  522. for(int i = 0; i < 4; i++)
  523. {
  524. if((channel_mask & 0x3) == 0x3)
  525. {
  526. i2s_set_mask_interrupt(device_num, I2S_CHANNEL_0 + i, 1, 1, 1, 1);
  527. i2s_receive_enable(device_num, I2S_CHANNEL_0 + i);
  528. } else
  529. {
  530. i2s_recv_channel_enable(device_num, I2S_CHANNEL_0 + i, 0);
  531. }
  532. channel_mask >>= 2;
  533. }
  534. /* Set expand_en when receive */
  535. i2s_set_sign_expand_en(device_num, 1);
  536. i2s_receive_dma_enable(device_num, 1);
  537. }
  538. }
  539. uint32_t i2s_set_sample_rate(i2s_device_number_t device_num, uint32_t sample_rate)
  540. {
  541. ccr_t u_ccr;
  542. uint32_t pll2_clock = 0;
  543. pll2_clock = sysctl_pll_get_freq(SYSCTL_PLL2);
  544. u_ccr.reg_data = readl(&i2s[device_num]->ccr);
  545. /* 0x0 for 16sclk cycles, 0x1 for 24 sclk cycles 0x2 for 32 sclk */
  546. uint32_t v_clk_word_size = (u_ccr.ccr.clk_word_size + 2) * 8;
  547. uint32_t threshold = round(pll2_clock / (sample_rate * 2.0 * v_clk_word_size * 2.0) - 1);
  548. sysctl_clock_set_threshold(SYSCTL_THRESHOLD_I2S0 + device_num, threshold);
  549. return sysctl_clock_get_freq(SYSCTL_CLOCK_I2S0 + device_num);
  550. }
  551. int i2s_dmac_irq(void *ctx)
  552. {
  553. i2s_instance_t *v_instance = (i2s_instance_t *)ctx;
  554. dmac_irq_unregister(v_instance->dmac_channel);
  555. if(v_instance->i2s_int_instance.callback)
  556. {
  557. v_instance->i2s_int_instance.callback(v_instance->i2s_int_instance.ctx);
  558. }
  559. return 0;
  560. }
  561. void i2s_handle_data_dma(i2s_device_number_t device_num, i2s_data_t data, plic_interrupt_t *cb)
  562. {
  563. configASSERT(device_num < I2S_DEVICE_MAX);
  564. if(data.transfer_mode == I2S_SEND)
  565. {
  566. configASSERT(data.tx_buf && data.tx_len);
  567. if(!data.nowait_dma_idle)
  568. {
  569. dmac_wait_done(data.tx_channel);
  570. }
  571. if(cb)
  572. {
  573. g_i2s_send_instance[device_num].i2s_int_instance.callback = cb->callback;
  574. g_i2s_send_instance[device_num].i2s_int_instance.ctx = cb->ctx;
  575. g_i2s_send_instance[device_num].dmac_channel = data.tx_channel;
  576. g_i2s_send_instance[device_num].transfer_mode = I2S_SEND;
  577. dmac_irq_register(data.tx_channel, i2s_dmac_irq, &g_i2s_send_instance[device_num], cb->priority);
  578. }
  579. sysctl_dma_select((sysctl_dma_channel_t)data.tx_channel, SYSCTL_DMA_SELECT_I2S0_TX_REQ + device_num * 2);
  580. dmac_set_single_mode(data.tx_channel, data.tx_buf, (void *)(&i2s[device_num]->txdma), DMAC_ADDR_INCREMENT,
  581. DMAC_ADDR_NOCHANGE, DMAC_MSIZE_1, DMAC_TRANS_WIDTH_32, data.tx_len);
  582. if(!cb && data.wait_dma_done)
  583. {
  584. dmac_wait_done(data.tx_channel);
  585. }
  586. } else
  587. {
  588. configASSERT(data.rx_buf && data.rx_len);
  589. if(!data.nowait_dma_idle)
  590. {
  591. dmac_wait_done(data.rx_channel);
  592. }
  593. if(cb)
  594. {
  595. g_i2s_recv_instance[device_num].i2s_int_instance.callback = cb->callback;
  596. g_i2s_recv_instance[device_num].i2s_int_instance.ctx = cb->ctx;
  597. g_i2s_recv_instance[device_num].dmac_channel = data.rx_channel;
  598. g_i2s_recv_instance[device_num].transfer_mode = I2S_RECEIVE;
  599. dmac_irq_register(data.rx_channel, i2s_dmac_irq, &g_i2s_recv_instance[device_num], cb->priority);
  600. }
  601. sysctl_dma_select((sysctl_dma_channel_t)data.rx_channel, SYSCTL_DMA_SELECT_I2S0_RX_REQ + device_num * 2);
  602. dmac_set_single_mode(data.rx_channel, (void *)(&i2s[device_num]->rxdma), data.rx_buf, DMAC_ADDR_NOCHANGE, DMAC_ADDR_INCREMENT,
  603. DMAC_MSIZE_1, DMAC_TRANS_WIDTH_32, data.rx_len);
  604. if(!cb && data.wait_dma_done)
  605. {
  606. dmac_wait_done(data.rx_channel);
  607. }
  608. }
  609. }